<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 style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thank you for your correction.</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I rebuilt the code under Linux, the bug disappeared. It seems this is a bug of WSL.</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Satish Balay <balay@mcs.anl.gov><br>
<b>Sent:</b> Tuesday, April 10, 2018 0:20<br>
<b>To:</b> Songtao Chu<br>
<b>Cc:</b> Matthew Knepley; petsc-users@mcs.anl.gov<br>
<b>Subject:</b> Re: [petsc-users] How to access Vector data properly</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">On Mon, 9 Apr 2018, Songtao Chu wrote:<br>
<br>
> static char help[] = "\n\n";<br>
> #include <petscdm.h><br>
> #include <petscdmda.h><br>
> <br>
> <br>
> int main(int argc,char **argv)<br>
> {<br>
> PetscMPIInt rank;<br>
> PetscErrorCode ierr;<br>
> Vec global,local,natural;<br>
> DM da;<br>
> PetscReal *val;<br>
> PetscInt i,x,xm;<br>
> <br>
> <br>
> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;<br>
> ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);<br>
> <br>
> ierr = DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);<br>
> ierr = DMSetUp(da);CHKERRQ(ierr);<br>
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);<br>
> <br>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);<br>
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);<br>
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", rank);CHKERRQ(ierr);<br>
<br>
Shouldn't this call be removed? Nothing of substance is printed here.<br>
<br>
> for (i = x; i < x + xm; ++i) {<br>
> val[i] = i;<br>
> }<br>
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);<br>
> <br>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);<br>
> <br>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);<br>
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);<br>
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", rank);CHKERRQ(ierr);<br>
<br>
Shouldn't this be on PETSC_COMM_WORLD?<br>
<br>
> for (i = x; i < x + xm; ++i) {<br>
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", val[i]);CHKERRQ(ierr);<br>
<br>
Again PETSC_COMM_WORLD?<br>
<br>
<br>
> }<br>
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);<br>
> PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);<br>
> <br>
> ierr = PetscFinalize();<br>
> return ierr;<br>
> }<br>
<br>
Attaching the fixed code.<br>
<br>
Satish<br>
-------<br>
balay@asterix /home/balay/download-pine<br>
$ ./ex1<br>
Vec Object: 1 MPI processes<br>
type: seq<br>
0.<br>
1.<br>
2.<br>
3.<br>
4.<br>
Rank=0<br>
0 1 2 3 4 <br>
balay@asterix /home/balay/download-pine<br>
$ mpiexec -n 2 ./ex1<br>
Vec Object: 2 MPI processes<br>
type: mpi<br>
Process [0]<br>
0.<br>
1.<br>
2.<br>
Process [1]<br>
3.<br>
4.<br>
Rank=0<br>
0 1 2 <br>
Rank=1<br>
3 4 <br>
balay@asterix /home/balay/download-pine<br>
$ mpiexec -n 3 ./ex1<br>
Vec Object: 3 MPI processes<br>
type: mpi<br>
Process [0]<br>
0.<br>
1.<br>
Process [1]<br>
2.<br>
3.<br>
Process [2]<br>
4.<br>
Rank=0<br>
0 1 <br>
Rank=1<br>
2 3 <br>
Rank=2<br>
4 <br>
balay@asterix /home/balay/download-pine<br>
$</div>
</span></font></div>
</body>
</html>