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