Actual source code: ex10.c

      1: 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";

      3: #include <petscvec.h>
      4: #include <petscviewerhdf5.h>

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

     10: int main(int argc, char **args)
     11: {
     12:   PetscMPIInt rank, size;
     13:   PetscInt    i, m = 20, low, high, ldim, iglobal, lsize;
     14:   PetscScalar v;
     15:   Vec         u;
     16:   PetscViewer viewer;
     17:   PetscBool   vstage2, vstage3, mpiio_use, isbinary = PETSC_FALSE;
     18: #if defined(PETSC_HAVE_HDF5)
     19:   PetscBool ishdf5 = PETSC_FALSE;
     20: #endif
     21: #if defined(PETSC_HAVE_ADIOS)
     22:   PetscBool isadios = PETSC_FALSE;
     23: #endif
     24:   PetscScalar const *values;
     25:   PetscLogEvent      VECTOR_GENERATE, VECTOR_READ;

     27:   PetscFunctionBeginUser;
     28:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
     29:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

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

     42:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
     43:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
     44:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));

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

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

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

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

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

     88:   char read_file[PETSC_MAX_PATH_LEN] = "vector.dat";
     89:   // By default, read the same file we just wrote, but with -read_file, read a different input file.
     90:   PetscCall(PetscOptionsGetString(NULL, NULL, "-read_file", read_file, sizeof read_file, NULL));
     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 %s ...\n", read_file));
    100:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, read_file, 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 %s ...\n", read_file));
    105:     PetscCall(PetscViewerHDF5Open(PETSC_COMM_WORLD, read_file, 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 %s ...\n", read_file));
    110:     PetscCall(PetscViewerADIOSOpen(PETSC_COMM_WORLD, read_file, 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: binary_i32
    162:        requires: double !complex
    163:        nsize: 2
    164:        args: -binary -read_file ${wPETSC_DIR}/share/petsc/datafiles/vectors/vector-i32.dat
    165:        filter: sed "s|in binary from .*|in binary from vector-i32.dat ...|"

    167:      test:
    168:        suffix: binary_i64
    169:        requires: double !complex
    170:        nsize: 2
    171:        args: -binary -read_file ${wPETSC_DIR}/share/petsc/datafiles/vectors/vector-i64.dat
    172:        filter: sed "s|in binary from .*|in binary from vector-i64.dat ...|"

    174:      test:
    175:        suffix: 2
    176:        nsize: 3
    177:        args: -binary

    179:      test:
    180:        suffix: 3
    181:        nsize: 5
    182:        args: -binary

    184:      test:
    185:        suffix: 4
    186:        requires: hdf5
    187:        nsize: 2
    188:        args: -hdf5

    190:      test:
    191:        suffix: 5
    192:        nsize: 4
    193:        args: -binary -sizes_set

    195:      test:
    196:        suffix: 6
    197:        requires: hdf5
    198:        nsize: 4
    199:        args: -hdf5 -sizes_set

    201: TEST*/