Actual source code: ex10.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

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

  4:  #include <petscvec.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:   PetscErrorCode    ierr;
 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,ishdf5;
 20:   PetscScalar const *values;
 21: #if defined(PETSC_USE_LOG)
 22:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ;
 23: #endif

 25:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 26:   isbinary  = ishdf5 = PETSC_FALSE;
 27:   mpiio_use = vstage2 = vstage3 = PETSC_FALSE;

 29:   PetscOptionsGetBool(NULL,NULL,"-binary",&isbinary,NULL);
 30:   PetscOptionsGetBool(NULL,NULL,"-hdf5",&ishdf5,NULL);
 31:   PetscOptionsGetBool(NULL,NULL,"-mpiio",&mpiio_use,NULL);
 32:   PetscOptionsGetBool(NULL,NULL,"-sizes_set",&vstage2,NULL);
 33:   PetscOptionsGetBool(NULL,NULL,"-type_set",&vstage3,NULL);

 35:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 36:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 37:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);

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

 41:   PetscLogEventRegister("Generate Vector",VEC_CLASSID,&VECTOR_GENERATE);
 42:   PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);
 43:   /* Generate vector */
 44:   VecCreate(PETSC_COMM_WORLD,&u);
 45:   PetscObjectSetName((PetscObject)u, "Test_Vec");
 46:   VecSetSizes(u,PETSC_DECIDE,m);
 47:   VecSetFromOptions(u);
 48:   VecGetOwnershipRange(u,&low,&high);
 49:   VecGetLocalSize(u,&ldim);
 50:   for (i=0; i<ldim; i++) {
 51:     iglobal = i + low;
 52:     v       = (PetscScalar)(i + 100*rank);
 53:     VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
 54:   }
 55:   VecAssemblyBegin(u);
 56:   VecAssemblyEnd(u);
 57:   VecView(u,PETSC_VIEWER_STDOUT_WORLD);

 59:   if (isbinary) {
 60:     PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
 61:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
 62: #if defined(PETSC_HAVE_HDF5)
 63:   } else if (ishdf5) {
 64:     PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");
 65:     PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
 66: #endif
 67:   } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No data format specified, run with either -binary or -hdf5 option\n");
 68:   VecView(u,viewer);
 69:   PetscViewerDestroy(&viewer);
 70:   VecDestroy(&u);


 73:   PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);

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

 77:   /* Read new vector in binary format */
 78:   PetscLogEventRegister("Read Vector",VEC_CLASSID,&VECTOR_READ);
 79:   PetscLogEventBegin(VECTOR_READ,0,0,0,0);
 80:   if (mpiio_use) {
 81:     PetscPrintf(PETSC_COMM_WORLD,"Using MPI IO for reading the vector\n");
 82:     PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");
 83:   }
 84:   if (isbinary) {
 85:     PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
 86:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
 87:     PetscViewerBinarySetFlowControl(viewer,2);
 88: #if defined(PETSC_HAVE_HDF5)
 89:   } else if (ishdf5) {
 90:     PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");
 91:     PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
 92: #endif
 93:   }
 94:   VecCreate(PETSC_COMM_WORLD,&u);
 95:   PetscObjectSetName((PetscObject) u,"Test_Vec");

 97:   if (vstage2) {
 98:     PetscPrintf(PETSC_COMM_WORLD,"Setting vector sizes...\n");
 99:     if (size > 1) {
100:       if (!rank) {
101:         lsize = m/size + size;
102:         VecSetSizes(u,lsize,m);
103:       } else if (rank == size-1) {
104:         lsize = m/size - size;
105:         VecSetSizes(u,lsize,m);
106:       } else {
107:         lsize = m/size;
108:         VecSetSizes(u,lsize,m);
109:       }
110:     } else {
111:       VecSetSizes(u,m,m);
112:     }
113:   }

115:   if (vstage3) {
116:     PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
117:     VecSetType(u, VECMPI);
118:   }
119:   VecLoad(u,viewer);
120:   PetscViewerDestroy(&viewer);
121:   PetscLogEventEnd(VECTOR_READ,0,0,0,0);
122:   VecView(u,PETSC_VIEWER_STDOUT_WORLD);
123:   VecGetArrayRead(u,&values);
124:   for (i=0; i<ldim; i++) {
125:     if (values[i] != (PetscScalar)(i + 100*rank)) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Data check failed!\n");
126:   }
127:   VecRestoreArrayRead(u,&values);

129:   /* Free data structures */
130:   VecDestroy(&u);
131:   PetscFinalize();
132:   return ierr;
133: }