Actual source code: ex2.c
1: static char help[] = "Tests ISView() and ISLoad() \n\n";
3: #include <petscis.h>
4: #include <petscviewer.h>
6: int main(int argc, char **argv)
7: {
8: PetscInt n = 3, *izero, j, i;
9: PetscInt ix[3][3][3] = {
10: {{3, 5, 4}, {1, 7, 9}, {0, 2, 8}},
11: {{0, 2, 8}, {3, 5, 4}, {1, 7, 9}},
12: {{1, 7, 9}, {0, 2, 8}, {3, 5, 4}}
13: };
14: IS isx[3], il;
15: PetscMPIInt size, rank;
16: PetscViewer vx, vl;
17: PetscBool equal;
19: PetscFunctionBeginUser;
20: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
22: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
23: PetscCheck(size < 4, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Example only works with up to three processes");
25: PetscCall(PetscCalloc1(size * n, &izero));
26: for (i = 0; i < 3; i++) PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n, ix[i][rank], PETSC_COPY_VALUES, &isx[i]));
28: for (j = 0; j < 3; j++) {
29: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_WRITE, &vx));
30: PetscCall(ISView(isx[0], vx));
31: PetscCall(PetscViewerDestroy(&vx));
33: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl));
34: PetscCall(ISCreate(PETSC_COMM_WORLD, &il));
35: PetscCall(ISLoad(il, vl));
36: PetscCall(ISEqual(il, isx[0], &equal));
37: PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set loaded from file does not match", j);
38: PetscCall(ISDestroy(&il));
39: PetscCall(PetscViewerDestroy(&vl));
41: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_APPEND, &vx));
42: PetscCall(ISView(isx[1], vx));
43: PetscCall(ISView(isx[2], vx));
44: PetscCall(PetscViewerDestroy(&vx));
46: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl));
47: for (i = 0; i < 3; i++) {
48: PetscCall(ISCreate(PETSC_COMM_WORLD, &il));
49: PetscCall(ISLoad(il, vl));
50: PetscCall(ISEqual(il, isx[i], &equal));
51: PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
52: PetscCall(ISDestroy(&il));
53: }
54: PetscCall(PetscViewerDestroy(&vl));
56: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl));
57: for (i = 0; i < 3; i++) {
58: PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n, izero, PETSC_COPY_VALUES, &il));
59: PetscCall(ISLoad(il, vl));
60: PetscCall(ISEqual(il, isx[i], &equal));
61: PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
62: PetscCall(ISDestroy(&il));
63: }
64: PetscCall(PetscViewerDestroy(&vl));
65: }
67: for (j = 0; j < 3; j++) {
68: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile_noheader", FILE_MODE_WRITE, &vx));
69: PetscCall(PetscViewerBinarySetSkipHeader(vx, PETSC_TRUE));
70: for (i = 0; i < 3; i++) PetscCall(ISView(isx[i], vx));
71: PetscCall(PetscViewerDestroy(&vx));
73: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile_noheader", FILE_MODE_READ, &vl));
74: PetscCall(PetscViewerBinarySetSkipHeader(vl, PETSC_TRUE));
75: for (i = 0; i < 3; i++) {
76: PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n, izero, PETSC_COPY_VALUES, &il));
77: PetscCall(ISLoad(il, vl));
78: PetscCall(ISEqual(il, isx[i], &equal));
79: PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
80: PetscCall(ISDestroy(&il));
81: }
82: PetscCall(PetscViewerDestroy(&vl));
83: }
85: for (i = 0; i < 3; i++) PetscCall(ISDestroy(&isx[i]));
87: for (j = 0; j < 2; j++) {
88: const char *filename = (j == 0) ? "testfile_isstride" : "testfile_isblock";
89: PetscInt blocksize = (j == 0) ? 1 : size;
90: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_WRITE, &vx));
91: for (i = 0; i < 3; i++) {
92: if (j == 0) {
93: PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, rank, rank + 1, &isx[i]));
94: } else {
95: PetscCall(ISCreateBlock(PETSC_COMM_WORLD, blocksize, n, ix[i][rank], PETSC_COPY_VALUES, &isx[i]));
96: }
97: PetscCall(ISView(isx[i], vx));
98: PetscCall(ISToGeneral(isx[i]));
99: }
100: PetscCall(PetscViewerDestroy(&vx));
101: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_READ, &vl));
102: for (i = 0; i < 3; i++) {
103: PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, blocksize * n, izero, PETSC_COPY_VALUES, &il));
104: PetscCall(ISLoad(il, vl));
105: PetscCall(ISEqual(il, isx[i], &equal));
106: PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
107: PetscCall(ISDestroy(&il));
108: }
109: PetscCall(PetscViewerDestroy(&vl));
110: for (i = 0; i < 3; i++) PetscCall(ISDestroy(&isx[i]));
111: }
112: PetscCall(PetscFree(izero));
114: PetscCall(PetscFinalize());
115: return 0;
116: }
118: /*TEST
120: testset:
121: args: -viewer_binary_mpiio 0
122: output_file: output/ex2_1.out
123: test:
124: suffix: stdio_1
125: nsize: 1
126: test:
127: suffix: stdio_2
128: nsize: 2
129: test:
130: suffix: stdio_3
131: nsize: 3
133: testset:
134: requires: mpiio
135: args: -viewer_binary_mpiio 1
136: output_file: output/ex2_1.out
137: test:
138: suffix: mpiio_1
139: nsize: 1
140: test:
141: suffix: mpiio_2
142: nsize: 2
143: test:
144: suffix: mpiio_3
145: nsize: 3
147: TEST*/