Actual source code: ex5.c

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

      4: #include <petscvec.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. */

     10: int main(int argc,char **args)
     11: {
     13:   PetscMPIInt    rank,size;
     14:   PetscInt       i,m = 10,low,high,ldim,iglobal;
     15:   PetscScalar    v;
     16:   Vec            u;
     17:   PetscViewer    viewer;
     18: #if defined(PETSC_USE_LOG)
     19:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ;
     20: #endif

     22:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
     23:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     24:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     25:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);

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

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

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

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

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

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

     68:   /* Free data structures */
     69:   VecDestroy(&u);
     70:   PetscFinalize();
     71:   return ierr;
     72: }

     74: /*TEST

     76:      test:
     77:        nsize: 1
     78:        requires: mpiio

     80:      test:
     81:        suffix: 2
     82:        nsize: 2
     83:        requires: mpiio

     85: TEST*/