Actual source code: ex13.c
1: static char help[] = "Tests ISDuplicate(), ISCopy(), ISShift(), ISEqualUnsorted(), ISEqual().\n\n";
3: #include <petscis.h>
4: #include <petscviewer.h>
6: /*
7: type = 0 general
8: type = 1 stride
9: type = 2 block
10: */
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;
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: }
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;
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));
56: for (type = 0; type < 3; type++) {
57: PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
58: j++;
60: PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
61: PetscCall(ISCopy(is[j], is[j]));
62: j++;
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++;
70: PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
71: PetscCall(ISShift(is[j], 0, is[j]));
72: j++;
74: PetscCall(CreateIS(comm, type, n, first, step, &is[j]));
75: PetscCall(ISShift(is[j], offset, is[j]));
76: j++;
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++;
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++;
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: }
108: /*TEST
110: test:
111: suffix: 1
112: nsize: 3
113: args: -n 6 -first {{-2 0 1 3}} -step {{-2 0 1 3}}
115: test:
116: suffix: 2
117: nsize: 2
118: args: -n 3 -first 2 -step -1 -is0_view -is1_view -verbose
120: TEST*/