xref: /petsc/src/vec/vec/tests/ex16.c (revision f1580f4e3ce5d5b2393648fd039d0d41b440385d)
1 static char help[] = "Tests VecSetValuesBlocked() on MPI vectors.\n\n";
2 
3 #include <petscvec.h>
4 
5 int main(int argc, char **argv) {
6   PetscMPIInt size, rank;
7   PetscInt    i, n = 8, bs = 2, indices[2];
8   PetscScalar values[4];
9   Vec         x;
10 
11   PetscFunctionBeginUser;
12   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
13   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
14   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
15 
16   PetscCheck(size == 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must be run with two processors");
17 
18   /* create vector */
19   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
20   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
21   PetscCall(VecSetBlockSize(x, bs));
22   PetscCall(VecSetFromOptions(x));
23 
24   if (rank == 0) {
25     for (i = 0; i < 4; i++) values[i] = i + 1;
26     indices[0] = 0;
27     indices[1] = 2;
28     PetscCall(VecSetValuesBlocked(x, 2, indices, values, INSERT_VALUES));
29   }
30   PetscCall(VecAssemblyBegin(x));
31   PetscCall(VecAssemblyEnd(x));
32 
33   /*
34       Resulting vector should be 1 2 0 0 3 4 0 0
35   */
36   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
37 
38   /* test insertion with negative indices */
39   PetscCall(VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE));
40   if (rank == 0) {
41     for (i = 0; i < 4; i++) values[i] = -(i + 1);
42     indices[0] = -1;
43     indices[1] = 3;
44     PetscCall(VecSetValuesBlocked(x, 2, indices, values, INSERT_VALUES));
45   }
46   PetscCall(VecAssemblyBegin(x));
47   PetscCall(VecAssemblyEnd(x));
48 
49   /*
50       Resulting vector should be 1 2 0 0 3 4 -3 -4
51   */
52   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
53 
54   PetscCall(VecDestroy(&x));
55 
56   PetscCall(PetscFinalize());
57   return 0;
58 }
59 
60 /*TEST
61 
62    test:
63       nsize: 2
64 
65 TEST*/
66