Actual source code: ex11.c

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

  3: #include <petscvec.h>

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

 14:   PetscFunctionBeginUser;
 15:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 16:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 17:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

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

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

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

 39:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));

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

 46:   if (rank == 0) {
 47:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "----\n"));
 48:     PetscCall(VecView(y, PETSC_VIEWER_STDOUT_SELF));
 49:   }

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

 56:   PetscCall(PetscFinalize());
 57:   return 0;
 58: }

 60: /*TEST

 62:    test:
 63:       nsize: 2

 65:    test:
 66:       suffix: bts
 67:       nsize: 2
 68:       args: -vec_assembly_legacy
 69:       output_file: output/ex11_1.out

 71: TEST*/