<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello,<div class=""><br class=""></div><div class="">Here is a trimmed down piece of code that illustrates a strange behaviour that I am desperately trying to understand.</div><div class=""><br class=""></div><div class="">The problem is that I would expect successive calls to MatView(C,…) to display the same numbers. However, the numbers displayed are different, even though the content of the matrix C should be the same.</div><div class=""><br class=""></div><div class="">Any help to resolve this would be appreciated.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Bernard.</div><div class=""><br class=""></div><div class="">Code:</div><div class="">******</div><div class=""><br class=""></div><div class=""><div class="">static char help[] = "Elemental test\n\n";</div><div class=""><br class=""></div><div class="">/*T</div><div class=""> </div><div class="">T*/</div><div class=""><br class=""></div><div class="">#include <petscmat.h></div><div class=""><br class=""></div><div class="">PetscScalar pi=3.141592653589793;</div><div class=""><br class=""></div><div class="">/* Prototypes */</div><div class="">PetscErrorCode matrixC(Mat C, PetscScalar t);</div><div class=""><br class=""></div><div class="">#undef __FUNCT__</div><div class="">#define __FUNCT__ "main"</div><div class="">int main(int argc, char **args){</div><div class=""><br class=""></div><div class=""> Mat C;</div><div class=""> </div><div class=""> PetscInt N = 8;</div><div class=""> PetscScalar t=0.1, dt=0.0001;</div><div class=""> PetscErrorCode ierr;</div><div class=""><br class=""></div><div class=""> /* parameters */</div><div class=""> ierr = PetscInitialize(&argc, &args, (char*) 0, help) ;CHKERRQ(ierr);</div><div class=""> </div><div class=""> ierr = MatCreate(PETSC_COMM_WORLD, &C); CHKERRQ(ierr);</div><div class=""> ierr = MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, N, N); CHKERRQ(ierr);</div><div class=""> ierr = MatSetType(C, MATELEMENTAL); CHKERRQ(ierr);</div><div class=""> ierr = MatSetFromOptions(C); CHKERRQ(ierr);</div><div class=""> ierr = MatSetUp(C); CHKERRQ(ierr);</div><div class=""> </div><div class=""> ierr=matrixC(C,t);</div><div class=""> ierr = MatView(C,PETSC_VIEWER_STDOUT_WORLD);</div><div class=""> </div><div class=""> ierr=matrixC(C,t);</div><div class=""> ierr = MatView(C,PETSC_VIEWER_STDOUT_WORLD);</div><div class=""> </div><div class=""> ierr = MatDestroy(&C);CHKERRQ(ierr);</div><div class=""> </div><div class=""> ierr = PetscFinalize();</div><div class=""> </div><div class=""> return ierr;</div><div class="">}</div><div class=""><br class=""></div><div class="">/* Matrix C*/</div><div class="">PetscErrorCode matrixC(Mat C, PetscScalar t){</div><div class=""> </div><div class=""> PetscErrorCode ierr; </div><div class=""> IS isrows,iscols;</div><div class=""> const PetscInt *rows,*cols;</div><div class=""> PetscScalar *v;</div><div class=""> PetscInt i,j,nrows,ncols;</div><div class=""> PetscInt n,m;</div><div class=""> </div><div class=""> /* Set local matrix entries */</div><div class=""> ierr = MatGetOwnershipIS(C,&isrows,&iscols);CHKERRQ(ierr);</div><div class=""> ierr = ISGetLocalSize(isrows,&nrows);CHKERRQ(ierr);</div><div class=""> ierr = ISGetIndices(isrows,&rows);CHKERRQ(ierr);</div><div class=""> ierr = ISGetLocalSize(iscols,&ncols);CHKERRQ(ierr);</div><div class=""> ierr = ISGetIndices(iscols,&cols);CHKERRQ(ierr);</div><div class=""> ierr = PetscMalloc1(nrows*ncols,&v);CHKERRQ(ierr);</div><div class=""> </div><div class=""> for (i=0; i<nrows; i++) {</div><div class=""> n=rows[i];</div><div class=""> for (j=0; j<ncols; j++) {</div><div class=""> m=cols[j];</div><div class=""> v[i*ncols+j] = -0.5*(exp(-(m+n+1.5)*(m+n+1.5)*pi*pi*t)-exp(-(m-n+0.5)*(m-n+0.5)*pi*pi*t));</div><div class=""> }</div><div class=""> }</div><div class=""> </div><div class=""> ierr = MatSetValues(C,nrows,rows,ncols,cols,v,INSERT_VALUES);CHKERRQ(ierr);</div><div class=""> ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);</div><div class=""> ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);</div><div class=""> ierr = ISRestoreIndices(isrows,&rows);CHKERRQ(ierr);</div><div class=""> ierr = ISRestoreIndices(iscols,&cols);CHKERRQ(ierr);</div><div class=""> ierr = ISDestroy(&isrows);CHKERRQ(ierr);</div><div class=""> ierr = ISDestroy(&iscols);CHKERRQ(ierr);</div><div class=""> ierr = PetscFree(v);CHKERRQ(ierr);</div><div class=""><br class=""></div><div class=""> PetscFunctionReturn(0);</div><div class="">}</div><div class=""><br class=""></div></div><div class="">Output:</div><div class="">********</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">mpirun -n 2 ./elemental</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Mat Object: 2 MPI processes</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> type: elemental</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Elemental matrix (cyclic ordering)</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.336403 2.8059e-06 0.0532215 1.04511e-09 0.00104438 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.00104718 0.390672 0.0542687 0.0542687 0.390672 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.389625 0.00104718 0.390669 2.80695e-06 0.0542687 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">2.80695e-06 0.390672 0.00104718 0.390672 0.0542687 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.0542659 0.0542687 0.390672 0.00104718 0.390672 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Elemental matrix (explicit ordering)</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> Mat Object: 2 MPI processes</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> type: mpidense</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">3.3640319378153799e-01 5.3221486768492490e-02 1.0443777750910219e-03 2.8059034408741489e-06 1.0451056906250189e-09 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">3.8962468055003047e-01 3.9066905832512150e-01 5.4268670447024388e-02 1.0471847236375868e-03 2.8069486006233780e-06 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">5.4265864543583515e-02 3.9067186422856237e-01 3.9067186527366804e-01 5.4268671492184138e-02 1.0471847236916457e-03 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">1.0471836785318962e-03 5.4268671492130077e-02 3.9067186527372211e-01 3.9067186527372211e-01 5.4268671492184138e-02 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">2.8069485465647739e-06 1.0471847236916453e-03 5.4268671492184138e-02 3.9067186527372211e-01 3.9067186527372211e-01 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Mat Object: 2 MPI processes</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> type: elemental</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Elemental matrix (cyclic ordering)</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.336403 2.8059e-06 0.390672 2.80695e-06 0.00104718 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.0532215 1.04511e-09 0.389625 0.390672 0.0542687 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.00104438 0.390672 0.390669 0.390672 0.0542659 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.00104718 0.0542687 0.0542687 0.0542687 0.390672 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0.0542687 0.00104718 2.80695e-06 0.00104718 0.390672 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Elemental matrix (explicit ordering)</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> Mat Object: 2 MPI processes</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> type: mpidense</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">3.3640319378153799e-01 3.9067186527372211e-01 1.0471847236916453e-03 2.8059034408741489e-06 2.8069486006233780e-06 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">1.0443777750910219e-03 3.9066905832512150e-01 5.4265864543583515e-02 3.9067186527372211e-01 3.9067186527372211e-01 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">5.4268671492130077e-02 2.8069485465647739e-06 3.9067186527366804e-01 1.0471847236375868e-03 1.0471847236916457e-03 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">5.3221486768492490e-02 3.8962468055003047e-01 5.4268671492184138e-02 1.0451056906250189e-09 3.9067186527372211e-01 </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">1.0471836785318962e-03 5.4268670447024388e-02 3.9067186422856237e-01 5.4268671492184138e-02 5.4268671492184138e-02 </span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>