Actual source code: ex10.c
petsc-3.11.4 2019-09-28
2: static char help[] = "Tests I/O of vectors for different data formats (binary,HDF5) and illustrates the use of user-defined event logging\n\n";
4: #include <petscvec.h>
5: #include <petscviewerhdf5.h>
7: /* Note: Most applications would not read and write a vector within
8: the same program. This example is intended only to demonstrate
9: both input and output and is written for use with either 1,2,or 4 processors. */
11: int main(int argc,char **args)
12: {
13: PetscErrorCode ierr;
14: PetscMPIInt rank,size;
15: PetscInt i,m = 20,low,high,ldim,iglobal,lsize;
16: PetscScalar v;
17: Vec u;
18: PetscViewer viewer;
19: PetscBool vstage2,vstage3,mpiio_use,isbinary = PETSC_FALSE;
20: #if defined(PETSC_HAVE_HDF5)
21: PetscBool ishdf5 = PETSC_FALSE;
22: #endif
23: #if defined(PETSC_HAVE_ADIOS)
24: PetscBool isadios = PETSC_FALSE;
25: #endif
26: #if defined(PETSC_HAVE_ADIOS2)
27: PetscBool isadios2 = PETSC_FALSE;
28: #endif
29: PetscScalar const *values;
30: #if defined(PETSC_USE_LOG)
31: PetscLogEvent VECTOR_GENERATE,VECTOR_READ;
32: #endif
34: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
35: mpiio_use = vstage2 = vstage3 = PETSC_FALSE;
37: PetscOptionsGetBool(NULL,NULL,"-binary",&isbinary,NULL);
38: #if defined(PETSC_HAVE_HDF5)
39: PetscOptionsGetBool(NULL,NULL,"-hdf5",&ishdf5,NULL);
40: #endif
41: #if defined(PETSC_HAVE_ADIOS)
42: PetscOptionsGetBool(NULL,NULL,"-adios",&isadios,NULL);
43: #endif
44: #if defined(PETSC_HAVE_ADIOS2)
45: PetscOptionsGetBool(NULL,NULL,"-adios2",&isadios2,NULL);
46: #endif
47: PetscOptionsGetBool(NULL,NULL,"-mpiio",&mpiio_use,NULL);
48: PetscOptionsGetBool(NULL,NULL,"-sizes_set",&vstage2,NULL);
49: PetscOptionsGetBool(NULL,NULL,"-type_set",&vstage3,NULL);
51: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
52: MPI_Comm_size(PETSC_COMM_WORLD,&size);
53: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
55: /* PART 1: Generate vector, then write it in the given data format */
57: PetscLogEventRegister("Generate Vector",VEC_CLASSID,&VECTOR_GENERATE);
58: PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);
59: /* Generate vector */
60: VecCreate(PETSC_COMM_WORLD,&u);
61: PetscObjectSetName((PetscObject)u, "Test_Vec");
62: VecSetSizes(u,PETSC_DECIDE,m);
63: VecSetFromOptions(u);
64: VecGetOwnershipRange(u,&low,&high);
65: VecGetLocalSize(u,&ldim);
66: for (i=0; i<ldim; i++) {
67: iglobal = i + low;
68: v = (PetscScalar)(i + low);
69: VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
70: }
71: VecAssemblyBegin(u);
72: VecAssemblyEnd(u);
73: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
75: if (isbinary) {
76: PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
77: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
78: #if defined(PETSC_HAVE_HDF5)
79: } else if (ishdf5) {
80: PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");
81: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
82: #endif
83: #if defined(PETSC_HAVE_ADIOS)
84: } else if (isadios) {
85: PetscPrintf(PETSC_COMM_WORLD,"writing vector in adios to vector.dat ...\n");
86: PetscViewerADIOSOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
87: #endif
88: #if defined(PETSC_HAVE_ADIOS2)
89: } else if (isadios2) {
90: PetscPrintf(PETSC_COMM_WORLD,"writing vector in adios to vector.dat ...\n");
91: PetscViewerADIOS2Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
92: #endif
93: } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No data format specified, run with either -binary or -hdf5 -adios -adios2 option\n");
94: VecView(u,viewer);
95: PetscViewerDestroy(&viewer);
96: VecDestroy(&u);
99: PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);
101: /* PART 2: Read in vector in binary format */
103: /* Read new vector in binary format */
104: PetscLogEventRegister("Read Vector",VEC_CLASSID,&VECTOR_READ);
105: PetscLogEventBegin(VECTOR_READ,0,0,0,0);
106: if (mpiio_use) {
107: PetscPrintf(PETSC_COMM_WORLD,"Using MPI IO for reading the vector\n");
108: PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");
109: }
110: if (isbinary) {
111: PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
112: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
113: PetscViewerBinarySetFlowControl(viewer,2);
114: #if defined(PETSC_HAVE_HDF5)
115: } else if (ishdf5) {
116: PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");
117: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
118: #endif
119: #if defined(PETSC_HAVE_ADIOS)
120: } else if (isadios) {
121: PetscPrintf(PETSC_COMM_WORLD,"reading vector in adios from vector.dat ...\n");
122: PetscViewerADIOSOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
123: #endif
124: #if defined(PETSC_HAVE_ADIOS2)
125: } else if (isadios2) {
126: PetscPrintf(PETSC_COMM_WORLD,"reading vector in adios2 from vector.dat ...\n");
127: PetscViewerADIOS2Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
128: #endif
129: }
130: VecCreate(PETSC_COMM_WORLD,&u);
131: PetscObjectSetName((PetscObject) u,"Test_Vec");
133: if (vstage2) {
134: PetscPrintf(PETSC_COMM_WORLD,"Setting vector sizes...\n");
135: if (size > 1) {
136: if (!rank) {
137: lsize = m/size + size;
138: VecSetSizes(u,lsize,m);
139: } else if (rank == size-1) {
140: lsize = m/size - size;
141: VecSetSizes(u,lsize,m);
142: } else {
143: lsize = m/size;
144: VecSetSizes(u,lsize,m);
145: }
146: } else {
147: VecSetSizes(u,m,m);
148: }
149: }
151: if (vstage3) {
152: PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
153: VecSetType(u, VECMPI);
154: }
155: VecLoad(u,viewer);
156: PetscViewerDestroy(&viewer);
157: PetscLogEventEnd(VECTOR_READ,0,0,0,0);
158: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
159: VecGetArrayRead(u,&values);
160: VecGetLocalSize(u,&ldim);
161: VecGetOwnershipRange(u,&low,NULL);
162: for (i=0; i<ldim; i++) {
163: if (values[i] != (PetscScalar)(i + low)) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Data check failed!\n");
164: }
165: VecRestoreArrayRead(u,&values);
167: /* Free data structures */
168: VecDestroy(&u);
169: PetscFinalize();
170: return ierr;
171: }
173: /*TEST
175: test:
176: nsize: 2
177: args: -binary
179: test:
180: suffix: 2
181: nsize: 3
182: args: -binary
184: test:
185: suffix: 3
186: nsize: 5
187: args: -binary
189: test:
190: suffix: 4
191: requires: hdf5
192: nsize: 2
193: args: -hdf5
195: test:
196: suffix: 5
197: nsize: 4
198: args: -binary -sizes_set
200: test:
201: suffix: 6
202: requires: hdf5
203: nsize: 4
204: args: -hdf5 -sizes_set
207: TEST*/