Actual source code: ex5.c

  1: /*$Id: ex5.c,v 1.48 2001/04/10 19:35:06 bsmith Exp $*/

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

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

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

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

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

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

 42:   PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...n");

 44:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",PETSC_BINARY_CREATE,&viewer);
 45:   VecView(u,viewer);
 46:   PetscViewerDestroy(viewer);
 47:   VecDestroy(u);
 48:   PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);

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

 52:   /* All processors wait until test vector has been dumped */
 53:   MPI_Barrier(PETSC_COMM_WORLD);
 54:   PetscSleep(10);

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

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