Actual source code: ex5f.F90

  1: program main
  2: #include <petsc/finclude/petscvec.h>
  3: use petscvec
  4: implicit none

  6:   PetscErrorCode ::ierr
  7:   PetscMPIInt ::   rank,size
  8:   PetscInt :: i
  9:   PetscInt, parameter :: one = 1
 10:   PetscInt :: m = 10
 11:   PetscInt :: low,high,ldim,iglobal
 12:   PetscScalar :: v
 13:   Vec ::         u
 14:   PetscViewer :: viewer
 15: #if defined(PETSC_USE_LOG)
 16:   PetscClassId classid
 17: #endif

 19:   PetscBool :: flg
 20: #if defined(PETSC_USE_LOG)
 21:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ
 22: #endif

 24:   PetscCallA(PetscInitialize(ierr))

 26:   PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr))

 28:   PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD,size,ierr))  !gives number of processes in the group of comm (integer)
 29:   PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-m',m,flg,ierr)) !gives the integer value for a particular option in the database.

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

 33: #if defined(PETSC_USE_LOG)
 34:   PetscCallA(PetscLogEventRegister('Generate Vector',classid,VECTOR_GENERATE,ierr))
 35:   PetscCallA(PetscLogEventBegin(VECTOR_GENERATE,ierr))
 36: #endif
 37:   ! Create vector
 38:   PetscCallA(VecCreate(PETSC_COMM_WORLD,u,ierr))
 39:   PetscCallA(VecSetSizes(u,PETSC_DECIDE,m,ierr))
 40:   PetscCallA(VecSetFromOptions(u,ierr))
 41:   PetscCallA(VecGetOwnershipRange(u,low,high,ierr))
 42:   PetscCallA(VecGetLocalSize(u,ldim,ierr))
 43:   do i=0,ldim-1
 44:    iglobal = i + low
 45:    v       = real(i + 100*rank)
 46:    PetscCallA(VecSetValues(u,one,[iglobal],[v],INSERT_VALUES,ierr))
 47:   end do
 48:   PetscCallA(VecAssemblyBegin(u,ierr))
 49:   PetscCallA(VecAssemblyEnd(u,ierr))
 50:   PetscCallA(VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr))

 52:   PetscCallA(PetscPrintf(PETSC_COMM_WORLD,'writing vector in binary to vector.dat ...\n',ierr))
 53:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_WORLD,'vector.dat',FILE_MODE_WRITE,viewer,ierr))
 54:   PetscCallA(VecView(u,viewer,ierr))
 55:   PetscCallA(PetscViewerDestroy(viewer,ierr))
 56:   PetscCallA(VecDestroy(u,ierr))
 57:   PetscCallA(PetscOptionsSetValue(PETSC_NULL_OPTIONS,'-viewer_binary_mpiio',PETSC_NULL_CHARACTER,ierr))

 59: #if defined(PETSC_USE_LOG)
 60:   PetscCallA(PetscLogEventEnd(VECTOR_GENERATE,ierr))
 61: #endif

 63:   ! PART 2:  Read in vector in binary format

 65:   ! Read new vector in binary format
 66: #if defined(PETSC_USE_LOG)
 67:   PetscCallA(PetscLogEventRegister('Read Vector',classid,VECTOR_READ,ierr))
 68:   PetscCallA(PetscLogEventBegin(VECTOR_READ,ierr))
 69: #endif
 70:   PetscCallA(PetscPrintf(PETSC_COMM_WORLD,'reading vector in binary from vector.dat ...\n',ierr))
 71:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_WORLD,'vector.dat',FILE_MODE_READ,viewer,ierr))
 72:   PetscCallA(VecCreate(PETSC_COMM_WORLD,u,ierr))
 73:   PetscCallA(VecLoad(u,viewer,ierr))
 74:   PetscCallA(PetscViewerDestroy(viewer,ierr))

 76: #if defined(PETSC_USE_LOG)
 77:   PetscCallA(PetscLogEventEnd(VECTOR_READ,ierr))
 78: #endif
 79:   PetscCallA(VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr))

 81:   ! Free data structures
 82:   PetscCallA(VecDestroy(u,ierr))
 83:   PetscCallA(PetscFinalize(ierr))

 85: end program

 87: !/*TEST
 88: !
 89: !     test:
 90: !       nsize: 1
 91: !       requires: mpiio
 92: !       output_file: output/ex5_1.out
 93: !
 94: !     test:
 95: !       suffix: 2
 96: !       nsize: 2
 97: !       requires: mpiio
 98: !       output_file: output/ex5_2.out
 99: !
100: !TEST*/