Actual source code: ex3.c

  1: static char help[] = "Tests parallel vector assembly.  Input arguments are\n\
  2:   -n <length> : local vector length\n\n";

  4: #include <petscvec.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscMPIInt size, rank;
  9:   PetscInt    n   = 5, idx;
 10:   PetscScalar one = 1.0, two = 2.0, three = 3.0;
 11:   Vec         x, y;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 15:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 16:   if (n < 5) n = 5;
 17:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 18:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

 20:   PetscCheck(size >= 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must be run with at least two processors");

 22:   /* create two vector */
 23:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &x));
 24:   PetscCall(VecCreate(PETSC_COMM_WORLD, &y));
 25:   PetscCall(VecSetSizes(y, n, PETSC_DECIDE));
 26:   PetscCall(VecSetFromOptions(y));
 27:   PetscCall(VecSet(x, one));
 28:   PetscCall(VecSet(y, two));

 30:   if (rank == 1) {
 31:     idx = 2;
 32:     PetscCall(VecSetValues(y, 1, &idx, &three, INSERT_VALUES));
 33:     idx = 0;
 34:     PetscCall(VecSetValues(y, 1, &idx, &two, INSERT_VALUES));
 35:     idx = 0;
 36:     PetscCall(VecSetValues(y, 1, &idx, &one, INSERT_VALUES));
 37:   } else {
 38:     idx = 7;
 39:     PetscCall(VecSetValues(y, 1, &idx, &three, INSERT_VALUES));
 40:   }
 41:   PetscCall(VecAssemblyBegin(y));
 42:   PetscCall(VecAssemblyEnd(y));

 44:   PetscCall(VecView(y, PETSC_VIEWER_STDOUT_WORLD));

 46:   PetscCall(VecDestroy(&x));
 47:   PetscCall(VecDestroy(&y));

 49:   PetscCall(PetscFinalize());
 50:   return 0;
 51: }

 53: /*TEST

 55:      test:
 56:        nsize: 2

 58:      test:
 59:        suffix: 2
 60:        nsize: 2
 61:        args: -vec_view ascii::ascii_info

 63: TEST*/