Actual source code: ex5f.F90

petsc-3.13.6 2020-09-29
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, parameter :: one = 1
 10:   PetscInt :: m = 10
 11:   PetscInt :: low,high,ldim,iglobal
 12:   PetscScalar :: v
 13:   Vec ::         u
 14:   PetscViewer :: viewer
 15:   PetscClassId classid
 16:   
 17:   PetscBool :: flg
 18: #if defined(PETSC_USE_LOG)
 19:   PetscLogEvent  VECTOR_GENERATE,VECTOR_READ
 20: #endif

 22:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 23:   if (ierr /= 0) then
 24:    print*,'PetscInitialize failed'
 25:    stop
 26:   endif
 27:   
 28:   call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr) 
 29:   
 30:   call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr);CHKERRA(ierr)  !gives number of processes in the group of comm (integer) 
 31:   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. 

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

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

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

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

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

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

 72:   call PetscLogEventEnd(VECTOR_READ,ierr);CHKERRA(ierr)
 73:   call VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)

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


 82: !/*TEST
 83: !
 84: !     test:
 85: !       nsize: 1
 86: !       requires: mpiio
 87: !       output_file: output/ex5_1.out
 88: !
 89: !     test:
 90: !       suffix: 2
 91: !       nsize: 2
 92: !       requires: mpiio
 93: !       output_file: output/ex5_2.out
 94: !
 95: !TEST*/