Date: 2026-06-01T23:13:54+00:00

Actual source code: ex5f.F90

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

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

     16:   PetscBool :: flg
     17: #if defined(PETSC_USE_LOG)
     18:   PetscLogEvent VECTOR_GENERATE, VECTOR_READ
     19: #endif

     21:   PetscCallA(PetscInitialize(ierr))

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

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

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

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

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

     57: #if defined(PETSC_USE_LOG)
     58:   PetscCallA(PetscLogEventEnd(VECTOR_GENERATE, ierr))
     59: #endif

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

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

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

     79:   ! Free data structures
     80:   PetscCallA(VecDestroy(u, ierr))
     81:   PetscCallA(PetscFinalize(ierr))

     83: end program

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