xref: /petsc/src/vec/is/is/tests/ex13.c (revision e66e6034addb69ed78763a9959635ebb267bdbde)
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   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
42   comm = PETSC_COMM_WORLD;
43   PetscCall(PetscArrayzero(is, sizeof(is)/sizeof(is[0])));
44   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
45   PetscCall(PetscOptionsGetInt(NULL,NULL,"-first",&first,NULL));
46   PetscCall(PetscOptionsGetInt(NULL,NULL,"-step",&step,NULL));
47   PetscCall(PetscOptionsGetInt(NULL,NULL,"-offset",&offset,NULL));
48   PetscCall(PetscOptionsGetBool(NULL,NULL,"-verbose",&verbose,NULL));
49 
50   for (type=0; type<2; type++) {
51     PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
52     j++;
53 
54     PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
55     PetscCall(ISCopy(is[j], is[j]));
56     j++;
57 
58     PetscCall(CreateIS(comm, type, n, first + offset, step, &tmp));
59     PetscCall(ISDuplicate(tmp, &is[j]));
60     PetscCall(ISCopy(tmp, is[j]));
61     PetscCall(ISDestroy(&tmp));
62     j++;
63 
64     PetscCall(CreateIS(comm, type, n, first + offset, step, &is[j]));
65     PetscCall(ISShift(is[j], 0, is[j]));
66     j++;
67 
68     PetscCall(CreateIS(comm, type, n, first, step, &is[j]));
69     PetscCall(ISShift(is[j], offset, is[j]));
70     j++;
71 
72     PetscCall(CreateIS(comm, type, n, first + offset, step, &tmp));
73     PetscCall(ISDuplicate(tmp, &is[j]));
74     PetscCall(ISShift(tmp, 0, is[j]));
75     PetscCall(ISDestroy(&tmp));
76     j++;
77 
78     PetscCall(CreateIS(comm, type, n, first, step, &tmp));
79     PetscCall(ISDuplicate(tmp, &is[j]));
80     PetscCall(ISShift(tmp, offset, is[j]));
81     PetscCall(ISDestroy(&tmp));
82     j++;
83 
84     PetscCall(CreateIS(comm, type, n, first + 2*offset, step, &is[j]));
85     PetscCall(ISShift(is[j], -offset, is[j]));
86     j++;
87   }
88   PetscAssert(j < (PetscInt) (sizeof(is)/sizeof(is[0])), comm, PETSC_ERR_ARG_OUTOFRANGE, "assertion failed: j < sizeof(is)/sizeof(is[0])");
89   PetscCall(ISViewFromOptions(is[0], NULL, "-is0_view"));
90   PetscCall(ISViewFromOptions(is[j/2], NULL, "-is1_view"));
91   for (i=0; i<j; i++) {
92     if (!is[i]) continue;
93     PetscCall(ISEqualUnsorted(is[i], is[0], &flg));
94     PetscCheck(flg, comm, PETSC_ERR_PLIB, "is[%02" PetscInt_FMT "] differs from is[0]", i);
95     if (verbose) PetscCall(PetscPrintf(comm, "is[%02" PetscInt_FMT "] identical to is[0]\n", i));
96   }
97   for (i=0; i<j; i++) {
98     PetscCall(ISDestroy(&is[i]));
99   }
100   PetscCall(PetscFinalize());
101   return 0;
102 }
103 
104 /*TEST
105 
106     test:
107       suffix: 1
108       nsize: 3
109       args: -n 6 -first {{-2 0 1 3}} -step {{-2 0 1 3}}
110 
111     test:
112       suffix: 2
113       nsize: 2
114       args: -n 3 -first 2 -step -1 -is0_view -is1_view -verbose
115 
116 TEST*/
117