[petsc-users] VecGetValues and VecGetArray return nothing but zeros
Tonio Herrmann
tonioherrmann at gmail.com
Thu Sep 8 10:43:29 CDT 2016
Hello,
during my attempts to learn PETSc, I am trying to understand the relation
between global and local vectors on a DMDA. There is a problem in my code
below, which only returns zeros when I try to access the local values,
although VecView shows that the values are correctly transferred from the
global vector g to the local vector l.
Is it obvious, what I am doing wrong?
Another question, I was expecting that "VecView(l,PETSC_VIEWER_STDOUT_WORLD);"
would print l synchronized from each process, but only l from process #0 is
shown. How can I see the other parts of l?
Thank you
Herrmann
The example code:
#include "petsc.h"
int main(int argc,char **argv){
PetscInt sz;
int ierr,rank;
Vec g,l;
DM da;
ierr=PetscInitialize(&argc, &argv, NULL, NULL);CHKERRQ(ierr);
MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
int nx=2,ny=4;
//create DA
ierr=DMDACreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_GHOSTED,DM_
BOUNDARY_GHOSTED,DMDA_STENCIL_BOX,nx,ny,PETSC_DECIDE,PETSC_
DECIDE,1,1,NULL,NULL,&da);CHKERRQ(ierr);
//create global vector and fill with numbers 101,102,...
ierr=DMCreateGlobalVector(da,&g);CHKERRQ(ierr);
ierr=VecGetSize(g,&sz);CHKERRQ(ierr);
for (int k=0;k<sz;k++){
VecSetValue(g,k,100+k,INSERT_VALUES);
}
VecAssemblyBegin(g);
VecAssemblyEnd(g);
//create local vector and transfer global values into local
ierr=DMCreateLocalVector(da,&l);CHKERRQ(ierr);
ierr=VecGetSize(l,&sz);CHKERRQ(ierr);
DMGlobalToLocalBegin(da,g,INSERT_VALUES,l);
DMGlobalToLocalEnd(da,g,INSERT_VALUES,l);
VecAssemblyBegin(l);
VecAssemblyEnd(l);
//view local vector, ok
VecView(l,PETSC_VIEWER_STDOUT_WORLD);
//access local values by VecGetValues, fails (only zeros!)
double *data=(double*)malloc(100*sizeof(double));
PetscInt ix[100];
ierr=VecGetSize(l,&sz);CHKERRQ(ierr);
for (int k=0;k<sz;k++) ix[k]=k;
ierr=VecGetValues(l,sz,ix,data);CHKERRQ(ierr);
PetscSynchronizedPrintf(PETSC_COMM_WORLD,"VecGetValues on rank
%d:\n",rank);
for (int k=0;k<sz;k++) PetscSynchronizedPrintf(PETSC_COMM_WORLD,"%d
",data[k]);
PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\n");
//access local values by VecGetArrays, fails (only zeros!)
ierr=VecGetArray(l,&data);CHKERRQ(ierr);
PetscSynchronizedPrintf(PETSC_COMM_WORLD,"VecGetArray on rank
%d:\n",rank);
for (int k=0;k<sz;k++){
PetscSynchronizedPrintf(PETSC_COMM_WORLD,"%d ",data[k]);
}
PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\n");
PetscSynchronizedFlush(PETSC_COMM_WORLD,NULL);
VecRestoreArray(l,&data);CHKERRQ(ierr);
ierr = PetscFinalize();CHKERRQ(ierr);
return ierr;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20160908/27d2e79a/attachment.html>
More information about the petsc-users
mailing list