Actual source code: ex5f.F90

petsc-3.11.2 2019-05-18
Report Typos and Errors
  1: program main
  2:  #include <petsc/finclude/petscvec.h>
  3: use petscvec
  4: implicit none

  6:   PetscErrorCode ierr
  7:   PetscMPIInt    rank,mySize
  8:   PetscInt       i
  9:   PetscInt :: m = 10
 10:   PetscInt :: low,high,ldim,iglobal
 11:   PetscScalar    v
 12:   Vec            u
 13:   PetscViewer    viewer
 14: 
 15:   PetscBool :: flg
 16: #if defined(PETSC_USE_LOG)
 17:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ
 18: #endif

 20:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 21:   if (ierr /= 0) then
 22:    print*,'PetscInitialize failed'
 23:    stop
 24:   endif
 25: 
 26:   call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 27: 
 28:   call MPI_Comm_Size(PETSC_COMM_WORLD,mySize,ierr);CHKERRA(ierr)  !gives number of processes in the group of comm (integer)
 29:   call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-m",m,flg,ierr);CHKERRA(ierr) !gives the integer value for a particular option in the database.

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

 33:   call PetscLogEventRegister("Generate Vector",0,VECTOR_GENERATE,ierr);CHKERRA(ierr)
 34:   call PetscLogEventBegin(VECTOR_GENERATE,ierr);CHKERRA(ierr)
 35:   ! Generate vector
 36:   call VecCreate(PETSC_COMM_WORLD,u,ierr);CHKERRA(ierr)
 37:   call VecSetSizes(u,PETSC_DECIDE,m,ierr);CHKERRA(ierr)
 38:   call VecSetFromOptions(u,ierr);CHKERRA(ierr)
 39:   call VecGetOwnershipRange(u,low,high,ierr);CHKERRA(ierr)
 40:   call VecGetLocalSize(u,ldim,ierr);CHKERRA(ierr)
 41:   do i=0,ldim-1
 42:    iglobal = i + low
 43:    v       = real(i + 100*rank)
 44:    call VecSetValues(u,1,iglobal,v,INSERT_VALUES,ierr);CHKERRA(ierr)
 45:   end do
 46:   call VecAssemblyBegin(u,ierr);CHKERRA(ierr)
 47:   call VecAssemblyEnd(u,ierr);CHKERRA(ierr)
 48:   call VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)

 50:   call PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n",ierr);CHKERRA(ierr)
 51:   call PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,viewer,ierr);CHKERRA(ierr)
 52:   call VecView(u,viewer,ierr);CHKERRA(ierr)
 53:   call PetscViewerDestroy(viewer,ierr);CHKERRA(ierr)
 54:   call VecDestroy(u,ierr);CHKERRA(ierr)
 55:   call PetscOptionsSetValue(PETSC_NULL_OPTIONS,"-viewer_binary_mpiio",PETSC_NULL_CHARACTER,ierr);CHKERRA(ierr)

 57:   call PetscLogEventEnd(VECTOR_GENERATE,ierr);CHKERRA(ierr)

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

 61:   ! Read new vector in binary format
 62:   call PetscLogEventRegister("Read Vector",0,VECTOR_READ,ierr);CHKERRA(ierr)
 63:   call PetscLogEventBegin(VECTOR_READ,ierr);CHKERRA(ierr)
 64:   call PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n",ierr);CHKERRA(ierr)
 65:   call PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,viewer,ierr);CHKERRA(ierr)
 66:   call VecCreate(PETSC_COMM_WORLD,u,ierr);CHKERRA(ierr)
 67:   call VecLoad(u,viewer,ierr);CHKERRA(ierr)
 68:   call PetscViewerDestroy(viewer,ierr);CHKERRA(ierr)

 70:   call PetscLogEventEnd(VECTOR_READ,ierr);CHKERRA(ierr)
 71:   call VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)

 73:   ! Free data structures
 74:   call VecDestroy(u,ierr);CHKERRA(ierr)
 75:   call PetscFinalize(ierr);CHKERRA(ierr)
 76: 
 77: end program