xref: /petsc/src/vec/is/tests/ex2.c (revision 800f99ff9e85495c69e9e5819c0be0dbd8cbc57c)
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 {
8   PetscInt               n = 3, *izero, j, i;
9   PetscInt               ix[3][3][3] = {{{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   IS                     isx[3],il;
13   PetscMPIInt            size,rank;
14   PetscViewer            vx,vl;
15   PetscBool              equal;
16 
17   PetscFunctionBeginUser;
18   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
19   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
20   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
21   PetscCheck(size < 4,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Example only works with up to three processes");
22 
23   PetscCall(PetscCalloc1(size*n,&izero));
24   for (i = 0; i < 3; i++) {
25     PetscCall(ISCreateGeneral(PETSC_COMM_WORLD,n,ix[i][rank],PETSC_COPY_VALUES,&isx[i]));
26   }
27 
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));
32 
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));
40 
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));
45 
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));
55 
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   }
66 
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++) {
71       PetscCall(ISView(isx[i],vx));
72     }
73     PetscCall(PetscViewerDestroy(&vx));
74 
75     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"testfile_noheader",FILE_MODE_READ,&vl));
76     PetscCall(PetscViewerBinarySetSkipHeader(vl,PETSC_TRUE));
77     for (i = 0; i < 3; i++) {
78       PetscCall(ISCreateGeneral(PETSC_COMM_WORLD,n,izero,PETSC_COPY_VALUES,&il));
79       PetscCall(ISLoad(il,vl));
80       PetscCall(ISEqual(il,isx[i],&equal));
81       PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match",j,i);
82       PetscCall(ISDestroy(&il));
83     }
84     PetscCall(PetscViewerDestroy(&vl));
85   }
86 
87   for (i = 0; i < 3; i++) {
88     PetscCall(ISDestroy(&isx[i]));
89   }
90 
91   for (j = 0; j < 2; j++) {
92     const char *filename  = (j == 0) ? "testfile_isstride" : "testfile_isblock";
93     PetscInt    blocksize = (j == 0) ? 1 : size;
94     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&vx));
95     for (i = 0; i < 3; i++) {
96       if (j == 0) {
97         PetscCall(ISCreateStride(PETSC_COMM_WORLD,n,rank,rank+1,&isx[i]));
98       } else {
99         PetscCall(ISCreateBlock(PETSC_COMM_WORLD,blocksize,n,ix[i][rank],PETSC_COPY_VALUES,&isx[i]));
100       }
101       PetscCall(ISView(isx[i],vx));
102       PetscCall(ISToGeneral(isx[i]));
103     }
104     PetscCall(PetscViewerDestroy(&vx));
105     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&vl));
106     for (i = 0; i < 3; i++) {
107       PetscCall(ISCreateGeneral(PETSC_COMM_WORLD,blocksize*n,izero,PETSC_COPY_VALUES,&il));
108       PetscCall(ISLoad(il,vl));
109       PetscCall(ISEqual(il,isx[i],&equal));
110       PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match",j,i);
111       PetscCall(ISDestroy(&il));
112     }
113     PetscCall(PetscViewerDestroy(&vl));
114     for (i = 0; i < 3; i++) {
115       PetscCall(ISDestroy(&isx[i]));
116     }
117   }
118   PetscCall(PetscFree(izero));
119 
120   PetscCall(PetscFinalize());
121   return 0;
122 }
123 
124 /*TEST
125 
126    testset:
127       args: -viewer_binary_mpiio 0
128       output_file: output/ex2_1.out
129       test:
130         suffix: stdio_1
131         nsize: 1
132       test:
133         suffix: stdio_2
134         nsize: 2
135       test:
136         suffix: stdio_3
137         nsize: 3
138 
139    testset:
140       requires: mpiio
141       args: -viewer_binary_mpiio 1
142       output_file: output/ex2_1.out
143       test:
144         suffix: mpiio_1
145         nsize: 1
146       test:
147         suffix: mpiio_2
148         nsize: 2
149       test:
150         suffix: mpiio_3
151         nsize: 3
152 
153 TEST*/
154