Actual source code: ex10.c


  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:   PetscMPIInt       rank,size;
 14:   PetscInt          i,m = 20,low,high,ldim,iglobal,lsize;
 15:   PetscScalar       v;
 16:   Vec               u;
 17:   PetscViewer       viewer;
 18:   PetscBool         vstage2,vstage3,mpiio_use,isbinary = PETSC_FALSE;
 19: #if defined(PETSC_HAVE_HDF5)
 20:   PetscBool         ishdf5 = PETSC_FALSE;
 21: #endif
 22: #if defined(PETSC_HAVE_ADIOS)
 23:   PetscBool         isadios = PETSC_FALSE;
 24: #endif
 25:   PetscScalar const *values;
 26: #if defined(PETSC_USE_LOG)
 27:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ;
 28: #endif

 30:   PetscInitialize(&argc,&args,(char*)0,help);
 31:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

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

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

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

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

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

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

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

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

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

133:   if (vstage3) {
134:     PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
135:     VecSetType(u, VECMPI);
136:   }
137:   VecLoad(u,viewer);
138:   PetscViewerDestroy(&viewer);
139:   PetscLogEventEnd(VECTOR_READ,0,0,0,0);
140:   VecView(u,PETSC_VIEWER_STDOUT_WORLD);
141:   VecGetArrayRead(u,&values);
142:   VecGetLocalSize(u,&ldim);
143:   VecGetOwnershipRange(u,&low,NULL);
144:   for (i=0; i<ldim; i++) {
146:   }
147:   VecRestoreArrayRead(u,&values);

149:   /* Free data structures */
150:   VecDestroy(&u);
151:   PetscFinalize();
152:   return 0;
153: }

155: /*TEST

157:      test:
158:        nsize: 2
159:        args: -binary

161:      test:
162:        suffix: 2
163:        nsize: 3
164:        args: -binary

166:      test:
167:        suffix: 3
168:        nsize: 5
169:        args: -binary

171:      test:
172:        suffix: 4
173:        requires: hdf5
174:        nsize: 2
175:        args: -hdf5

177:      test:
178:        suffix: 5
179:        nsize: 4
180:        args: -binary -sizes_set

182:      test:
183:        suffix: 6
184:        requires: hdf5
185:        nsize: 4
186:        args: -hdf5 -sizes_set

188: TEST*/