[petsc-users] How to access Vector data properly
Chu Songtao
st.chu at outlook.com
Mon Apr 9 07:55:16 CDT 2018
Hello,
I am just a beginner. Now I get confused on how to correctly use Vec and DMDA.
I set 1D DMDA global vector with natural values for a test. Then use VecView and DMDAVecGetArray to view the data, but the results are different. I don't know why.
this is the code:
PetscMPIInt rank;
PetscErrorCode ierr;
Vec global,local,natural;
DM da;
PetscReal *xnatural,*val;
PetscInt i,start,end,x,xm;
ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
ierr = DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
ierr = VecGetOwnershipRange(natural,&start,&end);CHKERRQ(ierr);
ierr = VecGetArray(natural,&xnatural);CHKERRQ(ierr);
for (i=start; i<end; i++) {
xnatural[i-start] = i;
}
ierr = VecRestoreArray(natural,&xnatural);CHKERRQ(ierr);
ierr = DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,global);CHKERRQ(ierr);
ierr = DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,global);CHKERRQ(ierr);
ierr = VecDestroy(&natural);CHKERRQ(ierr);
VecView(global,PETSC_VIEWER_STDOUT_WORLD);
ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", val[i]);CHKERRQ(ierr);
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
and the results are:
$ ./test0
Vec Object: 1 MPI processes
type: seq
Vec Object: Vec_0x7ffff3cbc500_0 1 MPI processes
type: mpi
Process [0]
0.
1.
2.
3.
4.
Rank=0
0 1 2 3 4
$ mpiexec -n 2 ./test0
Vec Object: 2 MPI processes
type: mpi
Vec Object: Vec_0x7fffcf948a90_0 2 MPI processes
type: mpi
Process [0]
0.
1.
2.
Process [1]
3.
4.
Rank=0
0 0 0 Rank=1
0 0
the seq version is correct, but the mpi version is wrong. Every element in val is 0.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20180409/554a2430/attachment-0001.html>
More information about the petsc-users
mailing list