Actual source code: ex16.c
1: static char help[]= "Test PetscSFCreateByMatchingIndices\n\n";
3: #include <petsc.h>
4: #include <petscsf.h>
6: /* Test PetscSFCreateByMatchingIndices.
8: testnum 0:
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
19: would build the following SF:
21: [0] 400 <- (0,101)
22: [1] 500 <- (0,102)
23: [2] 600 <- (0,101)
24: [2] 601 <- (2,300)
26: testnum 1:
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
37: would build the following SF:
39: [1] 200 <- (2,300)
41: testnum 2:
43: No one claims ownership of global index 1, but no one needs it.
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
54: would build the following SF:
56: [0] 400 <- (0,100)
57: [1] 500 <- (0,101)
58: [2] 600 <- (0,100)
59: [2] 601 <- (2,300)
61: */
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;
73: PetscInitialize(&argc,&argv,NULL,help);
74: PetscOptionsGetInt(NULL,NULL, "-testnum", &testnum, NULL);
75: MPI_Comm_size(PETSC_COMM_WORLD,&size);
76: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
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: PetscMalloc1(nA, &A);
90: 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: 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: PetscMalloc1(nA, &A);
143: 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: PetscLayoutCreate(PETSC_COMM_WORLD, &layout);
162: PetscLayoutSetSize(layout, N);
163: PetscLayoutSetLocalSize(layout, n);
164: PetscLayoutSetBlockSize(layout, 1);
165: PetscSFCreateByMatchingIndices(layout, nA, A, NULL, offsetA, nB, B, NULL, offsetB, NULL, &sf);
166: PetscLayoutDestroy(&layout);
167: PetscFree(A);
168: if (testnum != 1) PetscFree(B);
169: PetscObjectSetName((PetscObject)sf, "sf");
170: PetscSFView(sf, NULL);
171: PetscSFDestroy(&sf);
173: PetscFinalize();
174: return 0;
175: }
177: /*TEST
179: test:
180: suffix: 0
181: nsize: 3
182: args: -testnum 0
184: test:
185: suffix: 1
186: nsize: 3
187: args: -testnum 1
189: test:
190: suffix: 2
191: nsize: 3
192: args: -testnum 2
194: TEST*/