Actual source code: ex16.c

  1: static char help[] = "Tests VecSetValuesBlocked() on MPI vectors.\n\n";

  3: #include <petscvec.h>

  5: int main(int argc, char **argv)
  6: {
  7:   PetscMPIInt size, rank;
  8:   PetscInt    i, n = 8, bs = 2, indices[2];
  9:   PetscScalar values[4];
 10:   Vec         x;

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

 17:   PetscCheck(size == 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must be run with two processors");

 19:   /* create vector */
 20:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
 21:   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
 22:   PetscCall(VecSetBlockSize(x, bs));
 23:   PetscCall(VecSetFromOptions(x));

 25:   if (rank == 0) {
 26:     for (i = 0; i < 4; i++) values[i] = i + 1;
 27:     indices[0] = 0;
 28:     indices[1] = 2;
 29:     PetscCall(VecSetValuesBlocked(x, 2, indices, values, INSERT_VALUES));
 30:   }
 31:   PetscCall(VecAssemblyBegin(x));
 32:   PetscCall(VecAssemblyEnd(x));

 34:   /*
 35:       Resulting vector should be 1 2 0 0 3 4 0 0
 36:   */
 37:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));

 39:   /* test insertion with negative indices */
 40:   PetscCall(VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE));
 41:   if (rank == 0) {
 42:     for (i = 0; i < 4; i++) values[i] = -(i + 1);
 43:     indices[0] = -1;
 44:     indices[1] = 3;
 45:     PetscCall(VecSetValuesBlocked(x, 2, indices, values, INSERT_VALUES));
 46:   }
 47:   PetscCall(VecAssemblyBegin(x));
 48:   PetscCall(VecAssemblyEnd(x));

 50:   /*
 51:       Resulting vector should be 1 2 0 0 3 4 -3 -4
 52:   */
 53:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));

 55:   PetscCall(VecDestroy(&x));

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

 61: /*TEST

 63:    test:
 64:       nsize: 2

 66: TEST*/