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

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

      4:  #include <petscvec.h>

      6: int main(int argc,char **argv)
      7: {
      9:   PetscInt       n = 5,N,i;
     10:   PetscMPIInt    size,rank;
     11:   PetscScalar    value,zero = 0.0;
     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:   N    = size*n;
     22:   VecCreate(PETSC_COMM_WORLD,&y);
     23:   VecSetSizes(y,n,PETSC_DECIDE);
     24:   VecSetFromOptions(y);

     26:   VecCreate(PETSC_COMM_WORLD,&x);
     27:   VecSetSizes(x,n,PETSC_DECIDE);
     28:   VecSetFromOptions(x);

     30:   /* create two index sets */
     31:   ISCreateStride(PETSC_COMM_WORLD,n,n*rank,1,&is1);
     32:   ISCreateStride(PETSC_COMM_WORLD,n,(n*(rank+1))%N,1,&is2);

     34:   /* fill local part of parallel vector x */
     35:   value = (PetscScalar)(rank+1);
     36:   for (i=n*rank; i<n*(rank+1); i++) {
     37:     VecSetValues(x,1,&i,&value,INSERT_VALUES);
     38:   }
     39:   VecAssemblyBegin(x);
     40:   VecAssemblyEnd(x);

     42:   VecSet(y,zero);

     44:   VecScatterCreate(x,is1,y,is2,&ctx);
     45:   for (i=0; i<100; i++) {
     46:     PetscReal ynorm;
     47:     PetscInt  j;
     48:     VecNormBegin(y,NORM_2,&ynorm);
     49:     PetscCommSplitReductionBegin(PetscObjectComm((PetscObject)y));
     50:     for (j=0; j<3; j++) {
     51:       VecScatterBegin(ctx,x,y,ADD_VALUES,SCATTER_FORWARD);
     52:       VecScatterEnd(ctx,x,y,ADD_VALUES,SCATTER_FORWARD);
     53:     }
     54:     VecNormEnd(y,NORM_2,&ynorm);
     55:     /* PetscPrintf(PETSC_COMM_WORLD,"ynorm = %8.2G\n",ynorm); */
     56:   }
     57:   VecScatterDestroy(&ctx);
     58:   VecView(y,PETSC_VIEWER_STDOUT_WORLD);

     60:   VecDestroy(&x);
     61:   VecDestroy(&y);
     62:   ISDestroy(&is1);
     63:   ISDestroy(&is2);

     65:   PetscFinalize();
     66:   return ierr;
     67: }