Actual source code: ex13.c


  2: static char help[] = "Demonstrates scattering with the indices specified by a process that is not sender or receiver.\n\n";

  4: #include <petscvec.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscMPIInt rank, size;
  9:   Vec         x, y;
 10:   IS          is1, is2;
 11:   PetscInt    n, N, ix[2], iy[2];
 12:   VecScatter  ctx;

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

 20:   /* create two vectors */
 21:   n = 2;
 22:   N = 2 * size;

 24:   VecCreateMPI(PETSC_COMM_WORLD, n, N, &x);
 25:   VecDuplicate(x, &y);

 27:   /* Specify indices to send from the next process in the ring */
 28:   ix[0] = ((rank + 1) * n + 0) % N;
 29:   ix[1] = ((rank + 1) * n + 1) % N;
 30:   /* And put them on the process after that in the ring */
 31:   iy[0] = ((rank + 2) * n + 0) % N;
 32:   iy[1] = ((rank + 2) * n + 1) % N;

 34:   /* create two index sets */
 35:   ISCreateGeneral(PETSC_COMM_WORLD, n, ix, PETSC_USE_POINTER, &is1);
 36:   ISCreateGeneral(PETSC_COMM_WORLD, n, iy, PETSC_USE_POINTER, &is2);

 38:   VecSetValue(x, rank * n, rank * n, INSERT_VALUES);
 39:   VecSetValue(x, rank * n + 1, rank * n + 1, INSERT_VALUES);

 41:   VecView(x, PETSC_VIEWER_STDOUT_WORLD);
 42:   PetscPrintf(PETSC_COMM_WORLD, "----\n");

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

 49:   VecView(y, PETSC_VIEWER_STDOUT_WORLD);

 51:   ISDestroy(&is1);
 52:   ISDestroy(&is2);
 53:   VecDestroy(&x);
 54:   VecDestroy(&y);
 55:   PetscFinalize();
 56:   return 0;
 57: }

 59: /*TEST

 61:    test:
 62:       nsize: 4

 64: TEST*/