Actual source code: ex5.c

  1: static char help[] = "Tests binary I/O of vectors and illustrates the use of user-defined event logging.\n\n";

  3: #include <petscvec.h>

  5: /* Note:  Most applications would not read and write a vector within
  6:   the same program.  This example is intended only to demonstrate
  7:   both input and output. */

  9: int main(int argc, char **args)
 10: {
 11:   PetscMPIInt   rank, size;
 12:   PetscInt      i, m = 10, low, high, ldim, iglobal;
 13:   PetscScalar   v;
 14:   Vec           u;
 15:   PetscViewer   viewer;
 16:   PetscLogEvent VECTOR_GENERATE, VECTOR_READ;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
 20:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 21:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 22:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));

 24:   /* PART 1:  Generate vector, then write it in binary format */

 26:   PetscCall(PetscLogEventRegister("Generate Vector", VEC_CLASSID, &VECTOR_GENERATE));
 27:   PetscCall(PetscLogEventBegin(VECTOR_GENERATE, 0, 0, 0, 0));
 28:   /* Generate vector */
 29:   PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
 30:   PetscCall(VecSetSizes(u, PETSC_DECIDE, m));
 31:   PetscCall(VecSetFromOptions(u));
 32:   PetscCall(VecGetOwnershipRange(u, &low, &high));
 33:   PetscCall(VecGetLocalSize(u, &ldim));
 34:   for (i = 0; i < ldim; i++) {
 35:     iglobal = i + low;
 36:     v       = (PetscScalar)(i + 100 * rank);
 37:     PetscCall(VecSetValues(u, 1, &iglobal, &v, INSERT_VALUES));
 38:   }
 39:   PetscCall(VecAssemblyBegin(u));
 40:   PetscCall(VecAssemblyEnd(u));
 41:   PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));

 43:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing vector in binary to vector.dat ...\n"));
 44:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_WRITE, &viewer));
 45:   PetscCall(VecView(u, viewer));
 46:   PetscCall(PetscViewerDestroy(&viewer));
 47:   PetscCall(VecDestroy(&u));
 48:   PetscCall(PetscOptionsSetValue(NULL, "-viewer_binary_mpiio", ""));

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

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

 54:   /* Read new vector in binary format */
 55:   PetscCall(PetscLogEventRegister("Read Vector", VEC_CLASSID, &VECTOR_READ));
 56:   PetscCall(PetscLogEventBegin(VECTOR_READ, 0, 0, 0, 0));
 57:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading vector in binary from vector.dat ...\n"));
 58:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_READ, &viewer));
 59:   PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
 60:   PetscCall(VecLoad(u, viewer));
 61:   PetscCall(PetscViewerDestroy(&viewer));
 62:   PetscCall(PetscLogEventEnd(VECTOR_READ, 0, 0, 0, 0));
 63:   PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));

 65:   /* Free data structures */
 66:   PetscCall(VecDestroy(&u));
 67:   PetscCall(PetscFinalize());
 68:   return 0;
 69: }

 71: /*TEST

 73:      test:
 74:        nsize: 1
 75:        requires: mpiio

 77:      test:
 78:        suffix: 2
 79:        nsize: 2
 80:        requires: mpiio

 82: TEST*/