xref: /petsc/src/vec/is/sf/tests/ex16.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
1 static char help[]= "Test PetscSFCreateByMatchingIndices\n\n";
2 
3 #include <petsc.h>
4 #include <petscsf.h>
5 
6 /* Test PetscSFCreateByMatchingIndices.
7 
8 testnum 0:
9 
10   rank             : 0            1            2
11   numRootIndices   : 3            1            1
12   rootIndices      : [1 0 2]      [3]          [3]
13   rootLocalOffset  : 100          200          300
14   layout           : [0 1]        [2]          [3]
15   numLeafIndices   : 1            1            2
16   leafIndices      : [0]          [2]          [0 3]
17   leafLocalOffset  : 400          500          600
18 
19 would build the following SF:
20 
21   [0] 400 <- (0,101)
22   [1] 500 <- (0,102)
23   [2] 600 <- (0,101)
24   [2] 601 <- (2,300)
25 
26 testnum 1:
27 
28   rank             : 0               1               2
29   numRootIndices   : 3               1               1
30   rootIndices      : [1 0 2]         [3]             [3]
31   rootLocalOffset  : 100             200             300
32   layout           : [0 1]           [2]             [3]
33   numLeafIndices   : numRootIndices  numRootIndices  numRootIndices
34   leafIndices      : rootIndices     rootIndices     rootIndices
35   leafLocalOffset  : rootLocalOffset rootLocalOffset rootLocalOffset
36 
37 would build the following SF:
38 
39   [1] 200 <- (2,300)
40 
41 testnum 2:
42 
43   No one claims ownership of global index 1, but no one needs it.
44 
45   rank             : 0            1            2
46   numRootIndices   : 2            1            1
47   rootIndices      : [0 2]        [3]          [3]
48   rootLocalOffset  : 100          200          300
49   layout           : [0 1]        [2]          [3]
50   numLeafIndices   : 1            1            2
51   leafIndices      : [0]          [2]          [0 3]
52   leafLocalOffset  : 400          500          600
53 
54 would build the following SF:
55 
56   [0] 400 <- (0,100)
57   [1] 500 <- (0,101)
58   [2] 600 <- (0,100)
59   [2] 601 <- (2,300)
60 
61 */
62 
63 int main(int argc, char **argv)
64 {
65   PetscSF         sf;
66   PetscLayout     layout;
67   PetscInt        N, n;
68   PetscInt        nA=-1, *A, offsetA=-1;
69   PetscInt        nB=-1, *B, offsetB=-1;
70   PetscMPIInt     size, rank;
71   PetscInt        testnum;
72 
73   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
74   PetscCall(PetscOptionsGetInt(NULL,NULL, "-testnum", &testnum, NULL));
75   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
76   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
77   PetscCheckFalse(size != 3,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 3 MPI processes");
78 
79   switch (testnum) {
80   case 0:
81     N = 4;
82     n = PETSC_DECIDE;
83     switch (rank) {
84     case 0: nA = 3; offsetA = 100; nB = 1; offsetB = 400; break;
85     case 1: nA = 1; offsetA = 200; nB = 1; offsetB = 500; break;
86     case 2: nA = 1; offsetA = 300; nB = 2; offsetB = 600; break;
87     default: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 3 MPI processes");
88     }
89     PetscCall(PetscMalloc1(nA, &A));
90     PetscCall(PetscMalloc1(nB, &B));
91     switch (rank) {
92     case 0:
93       A[0] = 1; A[1] = 0; A[2] = 2;
94       B[0] = 0;
95       break;
96     case 1:
97       A[0] = 3;
98       B[0] = 2;
99       break;
100     case 2:
101       A[0] = 3;
102       B[0] = 0; B[1] = 3;
103       break;
104     default: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 3 MPI processes");
105     }
106     break;
107   case 1:
108     N = 4;
109     n = PETSC_DECIDE;
110     switch (rank) {
111     case 0: nA = 3; offsetA = 100; break;
112     case 1: nA = 1; offsetA = 200; break;
113     case 2: nA = 1; offsetA = 300; break;
114     default: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 3 MPI processes");
115     }
116     PetscCall(PetscMalloc1(nA, &A));
117     switch (rank) {
118     case 0:
119       A[0] = 1; A[1] = 0; A[2] = 2;
120       break;
121     case 1:
122       A[0] = 3;
123       break;
124     case 2:
125       A[0] = 3;
126       break;
127     default: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 3 MPI processes");
128     }
129     nB = nA;
130     B = A;
131     offsetB = offsetA;
132     break;
133   case 2:
134     N = 4;
135     n = PETSC_DECIDE;
136     switch (rank) {
137     case 0: nA = 2; offsetA = 100; nB = 1; offsetB = 400; break;
138     case 1: nA = 1; offsetA = 200; nB = 1; offsetB = 500; break;
139     case 2: nA = 1; offsetA = 300; nB = 2; offsetB = 600; break;
140     default: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 3 MPI processes");
141     }
142     PetscCall(PetscMalloc1(nA, &A));
143     PetscCall(PetscMalloc1(nB, &B));
144     switch (rank) {
145     case 0:
146       A[0] = 0; A[1] = 2;
147       B[0] = 0;
148       break;
149     case 1:
150       A[0] = 3;
151       B[0] = 2;
152       break;
153     case 2:
154       A[0] = 3;
155       B[0] = 0; B[1] = 3;
156       break;
157     default: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 3 MPI processes");
158     }
159     break;
160   }
161   PetscCall(PetscLayoutCreate(PETSC_COMM_WORLD, &layout));
162   PetscCall(PetscLayoutSetSize(layout, N));
163   PetscCall(PetscLayoutSetLocalSize(layout, n));
164   PetscCall(PetscLayoutSetBlockSize(layout, 1));
165   PetscCall(PetscSFCreateByMatchingIndices(layout, nA, A, NULL, offsetA, nB, B, NULL, offsetB, NULL, &sf));
166   PetscCall(PetscLayoutDestroy(&layout));
167   PetscCall(PetscFree(A));
168   if (testnum != 1) PetscCall(PetscFree(B));
169   PetscCall(PetscObjectSetName((PetscObject)sf, "sf"));
170   PetscCall(PetscSFView(sf, NULL));
171   PetscCall(PetscSFDestroy(&sf));
172 
173   PetscCall(PetscFinalize());
174   return 0;
175 }
176 
177 /*TEST
178 
179   test:
180     suffix: 0
181     nsize: 3
182     args: -testnum 0
183 
184   test:
185     suffix: 1
186     nsize: 3
187     args: -testnum 1
188 
189   test:
190     suffix: 2
191     nsize: 3
192     args: -testnum 2
193 
194 TEST*/
195