Actual source code: ex100.cu


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

  4: #include <petscvec.h>
  5: #include <petscdevice_cuda.h>
  6: #include <petscviewerhdf5.h>

  8: /* Note:  Most applications would not read and write a vector within
  9:   the same program.  This example is intended only to demonstrate
 10:   both input and output and is written for use with either 1,2,or 4 processors. */

 12: int main(int argc, char **args)
 13: {
 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:   VecType     vectype;
 21: #if defined(PETSC_HAVE_HDF5)
 22:   PetscBool ishdf5 = PETSC_FALSE;
 23: #endif
 24: #if defined(PETSC_HAVE_ADIOS)
 25:   PetscBool isadios = PETSC_FALSE;
 26: #endif
 27:   PetscScalar const *values;

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

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

 37:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

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

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

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

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

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

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

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

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

153: /*TEST

155:      build:
156:        requires: cuda

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

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

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

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

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

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

189: TEST*/