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