Actual source code: ex23.c

petsc-3.9.2 2018-05-20
Report Typos and Errors

  2: static char help[] = "Scatters from a parallel vector to a sequential vector.\n\
  3:   Using a blocked send and a strided receive.\n\n";

  5: /*
  6:         0 1 2 3 | 4 5 6 7 ||  8 9 10 11

  8:      Scatter first and third block to first processor and
  9:      second and third block to second processor
 10: */
 11: /*T
 12:    requires: x
 13: T*/

 15:  #include <petscvec.h>

 17: int main(int argc,char **argv)
 18: {
 20:   PetscInt       i,blocks[2],nlocal;
 21:   PetscMPIInt    size,rank;
 22:   PetscScalar    value;
 23:   Vec            x,y;
 24:   IS             is1,is2;
 25:   VecScatter     ctx = 0;
 26:   PetscViewer    subviewer;

 28:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 29:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 30:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 32:   if (size != 2) SETERRQ(PETSC_COMM_SELF,1,"Must run with 2 processors");

 34:   /* create two vectors */
 35:   if (!rank) nlocal = 8;
 36:   else nlocal = 4;
 37:   VecCreate(PETSC_COMM_WORLD,&x);
 38:   VecSetSizes(x,nlocal,12);
 39:   VecSetFromOptions(x);
 40:   VecCreateSeq(PETSC_COMM_SELF,8,&y);

 42:   /* create two index sets */
 43:   if (!rank) {
 44:     blocks[0] = 0; blocks[1] = 2;
 45:   } else {
 46:     blocks[0] = 1; blocks[1] = 2;
 47:   }
 48:   ISCreateBlock(PETSC_COMM_SELF,4,2,blocks,PETSC_COPY_VALUES,&is1);
 49:   ISCreateStride(PETSC_COMM_SELF,8,0,1,&is2);

 51:   for (i=0; i<12; i++) {
 52:     value = i;
 53:     VecSetValues(x,1,&i,&value,INSERT_VALUES);
 54:   }
 55:   VecAssemblyBegin(x);
 56:   VecAssemblyEnd(x);

 58:   VecScatterCreate(x,is1,y,is2,&ctx);
 59:   VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
 60:   VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
 61:   VecScatterDestroy(&ctx);

 63:   PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&subviewer);
 64:   VecView(y,subviewer);
 65:   PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&subviewer);

 67:   VecDestroy(&x);
 68:   VecDestroy(&y);
 69:   ISDestroy(&is1);
 70:   ISDestroy(&is2);

 72:   PetscFinalize();
 73:   return ierr;
 74: }



 78: /*TEST

 80:    test:
 81:       nsize: 2

 83: TEST*/