xref: /petsc/src/vec/is/is/tests/ex13.c (revision a44be8dc6eeccc8754dbff0bc56030e29894a2e1)
1 
2 static char help[] = "Tests ISDuplicate(), ISCopy(), ISShift(), ISEqualUnsorted(), ISEqual().\n\n";
3 
4 #include <petscis.h>
5 #include <petscviewer.h>
6 
7 /*
8 type = 0 general
9 type = 1 stride
10 type = 2 block
11 */
12 static PetscErrorCode CreateIS(MPI_Comm comm, PetscInt type, PetscInt n, PetscInt first, PetscInt step, IS *is) {
13   PetscInt   *idx, i, j;
14   PetscMPIInt rank;
15 
16   PetscFunctionBegin;
17   PetscCallMPI(MPI_Comm_rank(comm, &rank));
18   first += rank * n * step;
19   switch (type) {
20   case 0:
21     PetscCall(PetscMalloc1(n, &idx));
22     for (i = 0, j = first; i < n; i++, j += step) idx[i] = j;
23     PetscCall(ISCreateGeneral(comm, n, idx, PETSC_OWN_POINTER, is));
24     break;
25   case 1: PetscCall(ISCreateStride(comm, n, first, step, is)); break;
26   case 2:
27     PetscCall(PetscMalloc1(n, &idx));
28     for (i = 0, j = first; i < n; i++, j += step) idx[i] = j;
29     PetscCall(ISCreateBlock(comm, 1, n, idx, PETSC_OWN_POINTER, is));
30     break;
31   }
32   PetscFunctionReturn(0);
33 }
34 
35 int main(int argc, char **argv) {
36   IS        is[128];
37   IS        tmp;
38   PetscInt  n = 10, first = 0, step = 1, offset = 0;
39   PetscInt  i, j = 0, type;
40   PetscBool verbose = PETSC_FALSE, flg;
41   MPI_Comm  comm;
42 
43   PetscFunctionBeginUser;
44   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
45   comm = PETSC_COMM_WORLD;
46   PetscCall(PetscArrayzero(is, sizeof(is) / sizeof(is[0])));
47   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
48   PetscCall(PetscOptionsGetInt(NULL, NULL, "-first", &first, NULL));
49   PetscCall(PetscOptionsGetInt(NULL, NULL, "-step", &step, NULL));
50   PetscCall(PetscOptionsGetInt(NULL, NULL, "-offset", &offset, NULL));
51   PetscCall(PetscOptionsGetBool(NULL, NULL, "-verbose", &verbose, NULL));
52 
53   for (type = 0; type < 3; type++) {
54     PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
55     j++;
56 
57     PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
58     PetscCall(ISCopy(is[j], is[j]));
59     j++;
60 
61     PetscCall(CreateIS(comm, type, n, first + offset, step, &tmp));
62     PetscCall(ISDuplicate(tmp, &is[j]));
63     PetscCall(ISCopy(tmp, is[j]));
64     PetscCall(ISDestroy(&tmp));
65     j++;
66 
67     PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
68     PetscCall(ISShift(is[j], 0, is[j]));
69     j++;
70 
71     PetscCall(CreateIS(comm, type, n, first, step, &is[j]));
72     PetscCall(ISShift(is[j], offset, is[j]));
73     j++;
74 
75     PetscCall(CreateIS(comm, type, n, first + offset, step, &tmp));
76     PetscCall(ISDuplicate(tmp, &is[j]));
77     PetscCall(ISShift(tmp, 0, is[j]));
78     PetscCall(ISDestroy(&tmp));
79     j++;
80 
81     PetscCall(CreateIS(comm, type, n, first, step, &tmp));
82     PetscCall(ISDuplicate(tmp, &is[j]));
83     PetscCall(ISShift(tmp, offset, is[j]));
84     PetscCall(ISDestroy(&tmp));
85     j++;
86 
87     PetscCall(CreateIS(comm, type, n, first + 2 * offset, step, &is[j]));
88     PetscCall(ISShift(is[j], -offset, is[j]));
89     j++;
90   }
91   PetscAssert(j < (PetscInt)(sizeof(is) / sizeof(is[0])), comm, PETSC_ERR_ARG_OUTOFRANGE, "assertion failed: j < sizeof(is)/sizeof(is[0])");
92   PetscCall(ISViewFromOptions(is[0], NULL, "-is0_view"));
93   PetscCall(ISViewFromOptions(is[j / 2], NULL, "-is1_view"));
94   for (i = 0; i < j; i++) {
95     if (!is[i]) continue;
96     PetscCall(ISEqualUnsorted(is[i], is[0], &flg));
97     PetscCheck(flg, comm, PETSC_ERR_PLIB, "is[%02" PetscInt_FMT "] differs from is[0]", i);
98     if (verbose) PetscCall(PetscPrintf(comm, "is[%02" PetscInt_FMT "] identical to is[0]\n", i));
99   }
100   for (i = 0; i < j; i++) PetscCall(ISDestroy(&is[i]));
101   PetscCall(PetscFinalize());
102   return 0;
103 }
104 
105 /*TEST
106 
107     test:
108       suffix: 1
109       nsize: 3
110       args: -n 6 -first {{-2 0 1 3}} -step {{-2 0 1 3}}
111 
112     test:
113       suffix: 2
114       nsize: 2
115       args: -n 3 -first 2 -step -1 -is0_view -is1_view -verbose
116 
117 TEST*/
118