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