Actual source code: ex10.c

petsc-main 2021-04-15
Report Typos and Errors

  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:   PetscScalar const *values;
 27: #if defined(PETSC_USE_LOG)
 28:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ;
 29: #endif

 31:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 32:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

 34:   PetscOptionsGetBool(NULL,NULL,"-binary",&isbinary,NULL);
 35: #if defined(PETSC_HAVE_HDF5)
 36:   PetscOptionsGetBool(NULL,NULL,"-hdf5",&ishdf5,NULL);
 37: #endif
 38: #if defined(PETSC_HAVE_ADIOS)
 39:   PetscOptionsGetBool(NULL,NULL,"-adios",&isadios,NULL);
 40: #endif
 41:   PetscOptionsGetBool(NULL,NULL,"-mpiio",&mpiio_use,NULL);
 42:   PetscOptionsGetBool(NULL,NULL,"-sizes_set",&vstage2,NULL);
 43:   PetscOptionsGetBool(NULL,NULL,"-type_set",&vstage3,NULL);

 45:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 46:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 47:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);

 49:   /* PART 1:  Generate vector, then write it in the given data format */

 51:   PetscLogEventRegister("Generate Vector",VEC_CLASSID,&VECTOR_GENERATE);
 52:   PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);
 53:   /* Generate vector */
 54:   VecCreate(PETSC_COMM_WORLD,&u);
 55:   PetscObjectSetName((PetscObject)u, "Test_Vec");
 56:   VecSetSizes(u,PETSC_DECIDE,m);
 57:   VecSetFromOptions(u);
 58:   VecGetOwnershipRange(u,&low,&high);
 59:   VecGetLocalSize(u,&ldim);
 60:   for (i=0; i<ldim; i++) {
 61:     iglobal = i + low;
 62:     v       = (PetscScalar)(i + low);
 63:     VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
 64:   }
 65:   VecAssemblyBegin(u);
 66:   VecAssemblyEnd(u);
 67:   VecView(u,PETSC_VIEWER_STDOUT_WORLD);

 69:   if (isbinary) {
 70:     PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
 71:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
 72: #if defined(PETSC_HAVE_HDF5)
 73:   } else if (ishdf5) {
 74:     PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");
 75:     PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
 76: #endif
 77: #if defined(PETSC_HAVE_ADIOS)
 78:   } else if (isadios) {
 79:     PetscPrintf(PETSC_COMM_WORLD,"writing vector in adios to vector.dat ...\n");
 80:     PetscViewerADIOSOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
 81: #endif
 82:   } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No data format specified, run with one of -binary -hdf5 -adios options\n");
 83:   VecView(u,viewer);
 84:   PetscViewerDestroy(&viewer);
 85:   VecDestroy(&u);


 88:   PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);

 90:   /* PART 2:  Read in vector in binary format */

 92:   /* Read new vector in binary format */
 93:   PetscLogEventRegister("Read Vector",VEC_CLASSID,&VECTOR_READ);
 94:   PetscLogEventBegin(VECTOR_READ,0,0,0,0);
 95:   if (mpiio_use) {
 96:     PetscPrintf(PETSC_COMM_WORLD,"Using MPI IO for reading the vector\n");
 97:     PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");
 98:   }
 99:   if (isbinary) {
100:     PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
101:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
102:     PetscViewerBinarySetFlowControl(viewer,2);
103: #if defined(PETSC_HAVE_HDF5)
104:   } else if (ishdf5) {
105:     PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");
106:     PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
107: #endif
108: #if defined(PETSC_HAVE_ADIOS)
109:   } else if (isadios) {
110:     PetscPrintf(PETSC_COMM_WORLD,"reading vector in adios from vector.dat ...\n");
111:     PetscViewerADIOSOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
112: #endif
113:   }
114:   VecCreate(PETSC_COMM_WORLD,&u);
115:   PetscObjectSetName((PetscObject) u,"Test_Vec");

117:   if (vstage2) {
118:     PetscPrintf(PETSC_COMM_WORLD,"Setting vector sizes...\n");
119:     if (size > 1) {
120:       if (!rank) {
121:         lsize = m/size + size;
122:         VecSetSizes(u,lsize,m);
123:       } else if (rank == size-1) {
124:         lsize = m/size - size;
125:         VecSetSizes(u,lsize,m);
126:       } else {
127:         lsize = m/size;
128:         VecSetSizes(u,lsize,m);
129:       }
130:     } else {
131:       VecSetSizes(u,m,m);
132:     }
133:   }

135:   if (vstage3) {
136:     PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
137:     VecSetType(u, VECMPI);
138:   }
139:   VecLoad(u,viewer);
140:   PetscViewerDestroy(&viewer);
141:   PetscLogEventEnd(VECTOR_READ,0,0,0,0);
142:   VecView(u,PETSC_VIEWER_STDOUT_WORLD);
143:   VecGetArrayRead(u,&values);
144:   VecGetLocalSize(u,&ldim);
145:   VecGetOwnershipRange(u,&low,NULL);
146:   for (i=0; i<ldim; i++) {
147:     if (values[i] != (PetscScalar)(i + low)) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Data check failed!\n");
148:   }
149:   VecRestoreArrayRead(u,&values);

151:   /* Free data structures */
152:   VecDestroy(&u);
153:   PetscFinalize();
154:   return ierr;
155: }

157: /*TEST

159:      test:
160:        nsize: 2
161:        args: -binary

163:      test:
164:        suffix: 2
165:        nsize: 3
166:        args: -binary

168:      test:
169:        suffix: 3
170:        nsize: 5
171:        args: -binary

173:      test:
174:        suffix: 4
175:        requires: hdf5
176:        nsize: 2
177:        args: -hdf5

179:      test:
180:        suffix: 5
181:        nsize: 4
182:        args: -binary -sizes_set

184:      test:
185:        suffix: 6
186:        requires: hdf5
187:        nsize: 4
188:        args: -hdf5 -sizes_set


191: TEST*/