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:   PetscFunctionBeginUser;
 31:   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
 32:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

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

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

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

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

 69:   if (isbinary) {
 70:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing vector in binary to vector.dat ...\n"));
 71:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_WRITE, &viewer));
 72: #if defined(PETSC_HAVE_HDF5)
 73:   } else if (ishdf5) {
 74:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing vector in hdf5 to vector.dat ...\n"));
 75:     PetscCall(PetscViewerHDF5Open(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_WRITE, &viewer));
 76: #endif
 77: #if defined(PETSC_HAVE_ADIOS)
 78:   } else if (isadios) {
 79:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing vector in adios to vector.dat ...\n"));
 80:     PetscCall(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");
 83:   PetscCall(VecView(u, viewer));
 84:   PetscCall(PetscViewerDestroy(&viewer));
 85:   PetscCall(VecDestroy(&u));

 87:   PetscCall(PetscLogEventEnd(VECTOR_GENERATE, 0, 0, 0, 0));

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

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

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

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

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

154: /*TEST

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

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

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

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

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

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

187: TEST*/