Actual source code: ex11.c
petsc-3.12.4 2020-02-04
Report Typos and Errors

      2: static char help[] = "Scatters from a parallel vector to a sequential vector.\n\n";

      4:  #include <petscvec.h>

      6: int main(int argc,char **argv)
      7: {
      9:   PetscMPIInt    size,rank;
     10:   PetscInt       i,N;
     11:   PetscScalar    value;
     12:   Vec            x,y;
     13:   IS             is1,is2;
     14:   VecScatter     ctx = 0;

     16:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     17:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     18:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

     20:   /* create two vectors */
     21:   VecCreate(PETSC_COMM_WORLD,&x);
     22:   VecSetSizes(x,rank+1,PETSC_DECIDE);
     23:   VecSetFromOptions(x);
     24:   VecGetSize(x,&N);
     25:   VecCreateSeq(PETSC_COMM_SELF,N-rank,&y);

     27:   /* create two index sets */
     28:   ISCreateStride(PETSC_COMM_SELF,N-rank,rank,1,&is1);
     29:   ISCreateStride(PETSC_COMM_SELF,N-rank,0,1,&is2);

     31:   /* fill parallel vector: note this is not efficient way*/
     32:   for (i=0; i<N; i++) {
     33:     value = (PetscScalar) i;
     34:     VecSetValues(x,1,&i,&value,INSERT_VALUES);
     35:   }
     36:   VecAssemblyBegin(x);
     37:   VecAssemblyEnd(x);
     38:   VecSet(y,-1.0);

     40:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);

     42:   VecScatterCreate(x,is1,y,is2,&ctx);
     43:   VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
     44:   VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
     45:   VecScatterDestroy(&ctx);

     47:   if (!rank) {
     48:     printf("----\n");
     49:     VecView(y,PETSC_VIEWER_STDOUT_SELF);
     50:   }

     52:   ISDestroy(&is1);
     53:   ISDestroy(&is2);
     54:   VecDestroy(&x);
     55:   VecDestroy(&y);

     57:   PetscFinalize();
     58:   return ierr;
     59: }

     63: /*TEST

     65:    test:
     66:       nsize: 2

     68:    test:
     69:       suffix: bts
     70:       nsize: 2
     71:       args: -vec_assembly_legacy
     72:       output_file: output/ex11_1.out

     74: TEST*/