Date: 2021-03-03T22:25:59+00:00

Actual source code: ex5f.F90
petsc-3.14.5 2021-03-03
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: #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:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     25:   if (ierr /= 0) then
     26:    print*,'PetscInitialize failed'
     27:    stop
     28:   endif

     30:   call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

     32:   call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr);CHKERRA(ierr)  !gives number of processes in the group of comm (integer)
     33:   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.

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

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

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

     63: #if defined(PETSC_USE_LOG)
     64:   call PetscLogEventEnd(VECTOR_GENERATE,ierr);CHKERRA(ierr)
     65: #endif

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

     69:   ! Read new vector in binary format
     70: #if defined(PETSC_USE_LOG)
     71:   call PetscLogEventRegister("Read Vector",classid,VECTOR_READ,ierr);CHKERRA(ierr)
     72:   call PetscLogEventBegin(VECTOR_READ,ierr);CHKERRA(ierr)
     73: #endif
     74:   call PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n",ierr);CHKERRA(ierr)
     75:   call PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,viewer,ierr);CHKERRA(ierr)
     76:   call VecCreate(PETSC_COMM_WORLD,u,ierr);CHKERRA(ierr)
     77:   call VecLoad(u,viewer,ierr);CHKERRA(ierr)
     78:   call PetscViewerDestroy(viewer,ierr);CHKERRA(ierr)

     80: #if defined(PETSC_USE_LOG)
     81:   call PetscLogEventEnd(VECTOR_READ,ierr);CHKERRA(ierr)
     82: #endif
     83:   call VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)

     85:   ! Free data structures
     86:   call VecDestroy(u,ierr);CHKERRA(ierr)
     87:   call PetscFinalize(ierr);CHKERRA(ierr)

     89: end program

     92: !/*TEST
     93: !
     94: !     test:
     95: !       nsize: 1
     96: !       requires: mpiio
     97: !       output_file: output/ex5_1.out
     98: !
     99: !     test:
    100: !       suffix: 2
    101: !       nsize: 2
    102: !       requires: mpiio
    103: !       output_file: output/ex5_2.out
    104: !
    105: !TEST*/