[petsc-users] VecGetValues and VecGetArray return nothing but zeros
Barry Smith
bsmith at mcs.anl.gov
Thu Sep 8 13:30:41 CDT 2016
> On Sep 8, 2016, at 10:43 AM, Tonio Herrmann <tonioherrmann at gmail.com> wrote:
>
> 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.
This will not work; when you do a view the MPI communicator of the object must be the same as the MPI communicator in the viewer.
I don't really recommend ever looking at all the subdomains local vectors but if you really want to you can use
PetscViewerGetSubViewer() to get a sub viewer for each MPI process one at a time and call the VecView() one at at time with each viewer and local vector.
Barry
> 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;
> }
More information about the petsc-users
mailing list