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