Actual source code: ex24.c

petsc-3.12.1 2019-10-22
Report Typos and Errors

  2: static char help[] = "Scatters from a parallel vector to a sequential vector.\n\
  3: Tests where the local part of the scatter is a copy.\n\n";

  5:  #include <petscvec.h>

  7: int main(int argc,char **argv)
  8: {
 10:   PetscMPIInt    size,rank;
 11:   PetscInt       n = 5,i,*blks,bs = 1,m = 2;
 12:   PetscScalar    value;
 13:   Vec            x,y;
 14:   IS             is1,is2;
 15:   VecScatter     ctx = 0;
 16:   PetscViewer    sviewer;

 18:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 20:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 21:   PetscOptionsGetInt(NULL,NULL,"-bs",&bs,NULL);

 23:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 24:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 26:   /* create two vectors */
 27:   VecCreate(PETSC_COMM_WORLD,&x);
 28:   VecSetSizes(x,PETSC_DECIDE,size*bs*n);
 29:   VecSetFromOptions(x);

 31:   /* create two index sets */
 32:   if (rank < size-1) m = n + 2;
 33:   else m = n;

 35:   PetscMalloc1(m,&blks);
 36:   blks[0] = n*rank;
 37:   for (i=1; i<m; i++) blks[i] = blks[i-1] + 1;
 38:   ISCreateBlock(PETSC_COMM_SELF,bs,m,blks,PETSC_COPY_VALUES,&is1);
 39:   PetscFree(blks);

 41:   VecCreateSeq(PETSC_COMM_SELF,bs*m,&y);
 42:   ISCreateStride(PETSC_COMM_SELF,bs*m,0,1,&is2);

 44:   /* each processor inserts the entire vector */
 45:   /* this is redundant but tests assembly */
 46:   for (i=0; i<bs*n*size; i++) {
 47:     value = (PetscScalar) i;
 48:     VecSetValues(x,1,&i,&value,INSERT_VALUES);
 49:   }
 50:   VecAssemblyBegin(x);
 51:   VecAssemblyEnd(x);
 52:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);

 54:   VecScatterCreate(x,is1,y,is2,&ctx);
 55:   VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
 56:   VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);

 58:   PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);
 59:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"----\n");
 60:   PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 61:   VecView(y,sviewer); fflush(stdout);
 62:   PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 63:   PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
 64:   PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);

 66:   VecScatterDestroy(&ctx);

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

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



 79: /*TEST

 81:    testset:
 82:       nsize: 3
 83:       output_file: output/ex24_1.out
 84:       filter: grep -v "  type:"
 85:       test:
 86:         suffix: standard
 87:         args: -vec_type standard
 88:       test:
 89:         requires: cuda
 90:         suffix: cuda
 91:         args: -vec_type cuda
 92:       test:
 93:         requires: viennacl
 94:         suffix:  viennacl
 95:         args: -vec_type viennacl

 97: TEST*/