Actual source code: ex100.cu

  1: static char help[] = "Tests I/O of vectors for different data formats (binary,HDF5)\n\n";

  3: #include <petscvec.h>
  4: #include <petscdevice_cuda.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:   VecType     vectype;
 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;

 28:   PetscFunctionBeginUser;
 29:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 30:   {
 31:     PetscDeviceContext dctx; /* unused, only there to force initialization of device */

 33:     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
 34:   }

 36:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

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

 49:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 50:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 51:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));

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

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

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

 90:   /* PART 2:  Read in vector in binary format */
 91:   /* Read new vector in binary format */
 92:   if (mpiio_use) {
 93:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Using MPI IO for reading the vector\n"));
 94:     PetscCall(PetscOptionsSetValue(NULL, "-viewer_binary_mpiio", ""));
 95:   }
 96:   if (isbinary) {
 97:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading vector in binary from vector.dat ...\n"));
 98:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_READ, &viewer));
 99:     PetscCall(PetscViewerBinarySetFlowControl(viewer, 2));
100: #if defined(PETSC_HAVE_HDF5)
101:   } else if (ishdf5) {
102:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading vector in hdf5 from vector.dat ...\n"));
103:     PetscCall(PetscViewerHDF5Open(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_READ, &viewer));
104: #endif
105: #if defined(PETSC_HAVE_ADIOS)
106:   } else if (isadios) {
107:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading vector in adios from vector.dat ...\n"));
108:     PetscCall(PetscViewerADIOSOpen(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_READ, &viewer));
109: #endif
110:   }
111:   PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
112:   PetscCall(PetscObjectSetName((PetscObject)u, "Test_Vec"));
113:   if (vstage2) {
114:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Setting vector sizes...\n"));
115:     if (size > 1) {
116:       if (rank == 0) {
117:         lsize = m / size + size;
118:         PetscCall(VecSetSizes(u, lsize, m));
119:       } else if (rank == size - 1) {
120:         lsize = PetscMax(m / size - size, 0);
121:         PetscCall(VecSetSizes(u, lsize, m));
122:       } else {
123:         lsize = m / size;
124:         PetscCall(VecSetSizes(u, lsize, m));
125:       }
126:     } else {
127:       PetscCall(VecSetSizes(u, m, m));
128:     }
129:   }

131:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Setting vector type...\n"));
132:   PetscCall(VecSetType(u, VECCUDA));
133:   PetscCall(VecGetType(u, &vectype));
134:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Before load, vectype is : %s\n", (char *)vectype));
135:   PetscCall(VecLoad(u, viewer));
136:   PetscCall(VecGetType(u, &vectype));
137:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "After load, vectype is : %s\n", (char *)vectype));
138:   PetscCall(PetscViewerDestroy(&viewer));
139:   PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));
140:   PetscCall(VecGetArrayRead(u, &values));
141:   PetscCall(VecGetLocalSize(u, &ldim));
142:   PetscCall(VecGetOwnershipRange(u, &low, NULL));
143:   for (i = 0; i < ldim; i++) PetscCheck(values[i] == (PetscScalar)(i + low), PETSC_COMM_WORLD, PETSC_ERR_SUP, "Data check failed!");
144:   PetscCall(VecRestoreArrayRead(u, &values));

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

152: /*TEST

154:      build:
155:        requires: cuda

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*/