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;
20: PetscInitialize(&argc, &argv, NULL, help);
21: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
22: MPI_Comm_size(PETSC_COMM_WORLD, &size);
25: PetscCalloc1(size * n, &izero);
26: for (i = 0; i < 3; i++) ISCreateGeneral(PETSC_COMM_WORLD, n, ix[i][rank], PETSC_COPY_VALUES, &isx[i]);
28: for (j = 0; j < 3; j++) {
29: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_WRITE, &vx);
30: ISView(isx[0], vx);
31: PetscViewerDestroy(&vx);
33: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl);
34: ISCreate(PETSC_COMM_WORLD, &il);
35: ISLoad(il, vl);
36: ISEqual(il, isx[0], &equal);
38: ISDestroy(&il);
39: PetscViewerDestroy(&vl);
41: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_APPEND, &vx);
42: ISView(isx[1], vx);
43: ISView(isx[2], vx);
44: PetscViewerDestroy(&vx);
46: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl);
47: for (i = 0; i < 3; i++) {
48: ISCreate(PETSC_COMM_WORLD, &il);
49: ISLoad(il, vl);
50: ISEqual(il, isx[i], &equal);
52: ISDestroy(&il);
53: }
54: PetscViewerDestroy(&vl);
56: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl);
57: for (i = 0; i < 3; i++) {
58: ISCreateGeneral(PETSC_COMM_WORLD, n, izero, PETSC_COPY_VALUES, &il);
59: ISLoad(il, vl);
60: ISEqual(il, isx[i], &equal);
62: ISDestroy(&il);
63: }
64: PetscViewerDestroy(&vl);
65: }
67: for (j = 0; j < 3; j++) {
68: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile_noheader", FILE_MODE_WRITE, &vx);
69: PetscViewerBinarySetSkipHeader(vx, PETSC_TRUE);
70: for (i = 0; i < 3; i++) ISView(isx[i], vx);
71: PetscViewerDestroy(&vx);
73: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile_noheader", FILE_MODE_READ, &vl);
74: PetscViewerBinarySetSkipHeader(vl, PETSC_TRUE);
75: for (i = 0; i < 3; i++) {
76: ISCreateGeneral(PETSC_COMM_WORLD, n, izero, PETSC_COPY_VALUES, &il);
77: ISLoad(il, vl);
78: ISEqual(il, isx[i], &equal);
80: ISDestroy(&il);
81: }
82: PetscViewerDestroy(&vl);
83: }
85: for (i = 0; i < 3; i++) 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: PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_WRITE, &vx);
91: for (i = 0; i < 3; i++) {
92: if (j == 0) {
93: ISCreateStride(PETSC_COMM_WORLD, n, rank, rank + 1, &isx[i]);
94: } else {
95: ISCreateBlock(PETSC_COMM_WORLD, blocksize, n, ix[i][rank], PETSC_COPY_VALUES, &isx[i]);
96: }
97: ISView(isx[i], vx);
98: ISToGeneral(isx[i]);
99: }
100: PetscViewerDestroy(&vx);
101: PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_READ, &vl);
102: for (i = 0; i < 3; i++) {
103: ISCreateGeneral(PETSC_COMM_WORLD, blocksize * n, izero, PETSC_COPY_VALUES, &il);
104: ISLoad(il, vl);
105: ISEqual(il, isx[i], &equal);
107: ISDestroy(&il);
108: }
109: PetscViewerDestroy(&vl);
110: for (i = 0; i < 3; i++) ISDestroy(&isx[i]);
111: }
112: PetscFree(izero);
114: 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*/