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;

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

 34:     PetscDeviceContextGetCurrentContext(&dctx);
 35:   }

 37:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

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

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

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

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

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

 91:   /* PART 2:  Read in vector in binary format */
 92:   /* Read new vector in binary format */
 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");
114:   if (vstage2) {
115:     PetscPrintf(PETSC_COMM_WORLD, "Setting vector sizes...\n");
116:     if (size > 1) {
117:       if (rank == 0) {
118:         lsize = m / size + size;
119:         VecSetSizes(u, lsize, m);
120:       } else if (rank == size - 1) {
121:         lsize = PetscMax(m / size - size, 0);
122:         VecSetSizes(u, lsize, m);
123:       } else {
124:         lsize = m / size;
125:         VecSetSizes(u, lsize, m);
126:       }
127:     } else {
128:       VecSetSizes(u, m, m);
129:     }
130:   }

132:   PetscPrintf(PETSC_COMM_WORLD, "Setting vector type...\n");
133:   VecSetType(u, VECCUDA);
134:   VecGetType(u, &vectype);
135:   PetscPrintf(PETSC_COMM_WORLD, "Before load, vectype is : %s\n", (char *)vectype);
136:   VecLoad(u, viewer);
137:   VecGetType(u, &vectype);
138:   PetscPrintf(PETSC_COMM_WORLD, "After load, vectype is : %s\n", (char *)vectype);
139:   PetscViewerDestroy(&viewer);
140:   VecView(u, PETSC_VIEWER_STDOUT_WORLD);
141:   VecGetArrayRead(u, &values);
142:   VecGetLocalSize(u, &ldim);
143:   VecGetOwnershipRange(u, &low, NULL);
145:   VecRestoreArrayRead(u, &values);

147:   /* Free data structures */
148:   VecDestroy(&u);
149:   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*/