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