Actual source code: ex10.c

petsc-master 2017-05-25
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: {
 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: #if defined(PETSC_USE_LOG)
 21:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ;
 22: #endif

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

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

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

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

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

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


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

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

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

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

114:   if (vstage3) {
115:     PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
116:     VecSetType(u, VECMPI);
117:   }
118:   VecLoad(u,viewer);
119:   PetscViewerDestroy(&viewer);
120:   PetscLogEventEnd(VECTOR_READ,0,0,0,0);
121:   VecView(u,PETSC_VIEWER_STDOUT_WORLD);

123:   /* Free data structures */
124:   VecDestroy(&u);
125:   PetscFinalize();
126:   return ierr;
127: }