<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" dir="ltr" style="font-size: 12pt; color: rgb(0, 0, 0); font-family: Calibri, Helvetica, sans-serif, Helvetica, EmojiFont, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols;">
<p>Hello, </p>
<p><br>
</p>
<p>I have a code that handles a PETSc Vec on many procs and I would like to use VecScatterCreateToZero to have all elements of this vector on a single proc, call VecGetArrayRead on this proc to get the corresponding array to carry out some diagnostics.</p>
<p>Unfortunately, what I noticed is that the ordering of the initial Vec is not preserved after the VecScatterCreateToZero call. Is there a way to have the same ordering for both Vecs? </p>
<p>You will find below a minimal example that demonstrates the issue.</p>
<p>Many thanks,</p>
<p><br>
</p>
<p>Zakariae </p>
<p><br>
</p>
<p>-------------------------------------------------------------</p>
<p><br>
</p>
<p>static char help[] = "Demonstrates ordering change after VecScatterCreateToZero call.\n\n";</p>
<p><br>
</p>
<p><br>
</p>
<p>#include <petscpf.h></p>
<p>#include <petscdm.h></p>
<p>#include <petscdmda.h></p>
<p><br>
</p>
<p>int main(int argc,char **argv)</p>
<p>{</p>
<p> Vec xy;</p>
<p> DM da;</p>
<p> PetscErrorCode ierr;</p>
<p> PetscInt m = 11, n = 11, dof = 2;</p>
<p> PetscMPIInt rank;</p>
<p> DMDACoor2d **coors;</p>
<p><br>
</p>
<p> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;</p>
<p> ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,m,n,PETSC_DECIDE,PETSC_DECIDE,dof,1,0,0,&da);CHKERRQ(ierr);</p>
<p> ierr = DMSetFromOptions(da);CHKERRQ(ierr);</p>
<p> ierr = DMSetUp(da);CHKERRQ(ierr);</p>
<p> ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,1.0);CHKERRQ(ierr);</p>
<p> ierr = DMGetCoordinates(da,&xy);CHKERRQ(ierr);</p>
<p><br>
</p>
<p> PetscInt i, j, ixs, ixm, iys, iym;</p>
<p> MPI_Comm_rank(PETSC_COMM_WORLD, &rank);</p>
<p><br>
</p>
<p> DMDAGetCorners(da, &ixs, &iys, 0, &ixm, &iym, 0);</p>
<p> DMDAVecGetArray(da, xy, &coors);</p>
<p> for (j = iys; j < iys + iym; j++) {</p>
<p> for (i = ixs; i < ixs + ixm; i++) {</p>
<p> PetscPrintf(PETSC_COMM_SELF, "rank=%d, %d, %d, (%g, %g)\n",rank, </p>
<p> i, j,coors[j][i].x,coors[j][i].y);</p>
<p> }</p>
<p> }</p>
<p> DMDAVecRestoreArray(da, xy, &coors);</p>
<p><br>
</p>
<p> VecScatter scat;</p>
<p> Vec Xseq;</p>
<p> const PetscScalar *array; </p>
<p> </p>
<p> /* create scater to zero */</p>
<p> VecScatterCreateToZero(xy, &scat, &Xseq);</p>
<p> VecScatterBegin(scat, xy, Xseq, INSERT_VALUES, SCATTER_FORWARD);</p>
<p> VecScatterEnd(scat, xy, Xseq, INSERT_VALUES, SCATTER_FORWARD);</p>
<p><br>
</p>
<p> if (rank == 0) {</p>
<p> PetscInt sizeX;</p>
<p> VecGetSize(Xseq, &sizeX); </p>
<p> PetscPrintf(PETSC_COMM_SELF,"The size of Xseq is %d, and the grid size is %d\n",sizeX,11*11);</p>
<p> VecGetArrayRead(Xseq, &array);</p>
<p><br>
</p>
<p> for (j = 0; j < 11; j++) {</p>
<p> for (i = 0; i < 11; i++) {</p>
<p> PetscPrintf(PETSC_COMM_SELF, "%d,%d, (%g,%g)\n", i, j, (double)array[2*(j*11+i)], (double)array[1+2*(j*11+i)]);</p>
<p> }</p>
<p> }</p>
<p> VecRestoreArrayRead(Xseq, &array);</p>
<p> }</p>
<p><br>
</p>
<p> /*</p>
<p> Free work space. All PETSc objects should be destroyed when they</p>
<p> are no longer needed.</p>
<p> */</p>
<p> ierr = DMDestroy(&da);CHKERRQ(ierr);</p>
<p> ierr = PetscFinalize();</p>
<p> return ierr;</p>
<p>}</p>
<div><br>
</div>
</div>
</body>
</html>