Actual source code: ex11.c


  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: {
  8:   PetscMPIInt size, rank;
  9:   PetscInt    i, N;
 10:   PetscScalar value;
 11:   Vec         x, y;
 12:   IS          is1, is2;
 13:   VecScatter  ctx = 0;

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

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

 27:   /* create two index sets */
 28:   PetscCall(ISCreateStride(PETSC_COMM_SELF, N - rank, rank, 1, &is1));
 29:   PetscCall(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:     PetscCall(VecSetValues(x, 1, &i, &value, INSERT_VALUES));
 35:   }
 36:   PetscCall(VecAssemblyBegin(x));
 37:   PetscCall(VecAssemblyEnd(x));
 38:   PetscCall(VecSet(y, -1.0));

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

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

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

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

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

 61: /*TEST

 63:    test:
 64:       nsize: 2

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

 72: TEST*/