Actual source code: ex5f.F90
petsc-3.12.1 2019-10-22
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:
16: PetscBool :: flg
17: #if defined(PETSC_USE_LOG)
18: PetscLogEvent VECTOR_GENERATE,VECTOR_READ
19: #endif
21: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
22: if (ierr /= 0) then
23: print*,'PetscInitialize failed'
24: stop
25: endif
26:
27: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
28:
29: call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr);CHKERRA(ierr) !gives number of processes in the group of comm (integer)
30: 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.
32: ! PART 1: Generate vector, then write it in binary format */
34: call PetscLogEventRegister("Generate Vector",0,VECTOR_GENERATE,ierr);CHKERRA(ierr)
35: call PetscLogEventBegin(VECTOR_GENERATE,ierr);CHKERRA(ierr)
36: ! Generate vector
37: call VecCreate(PETSC_COMM_WORLD,u,ierr);CHKERRA(ierr)
38: call VecSetSizes(u,PETSC_DECIDE,m,ierr);CHKERRA(ierr)
39: call VecSetFromOptions(u,ierr);CHKERRA(ierr)
40: call VecGetOwnershipRange(u,low,high,ierr);CHKERRA(ierr)
41: call VecGetLocalSize(u,ldim,ierr);CHKERRA(ierr)
42: do i=0,ldim-1
43: iglobal = i + low
44: v = real(i + 100*rank)
45: call VecSetValues(u,one,iglobal,v,INSERT_VALUES,ierr);CHKERRA(ierr)
46: end do
47: call VecAssemblyBegin(u,ierr);CHKERRA(ierr)
48: call VecAssemblyEnd(u,ierr);CHKERRA(ierr)
49: call VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
51: call PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n",ierr);CHKERRA(ierr)
52: call PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,viewer,ierr);CHKERRA(ierr)
53: call VecView(u,viewer,ierr);CHKERRA(ierr)
54: call PetscViewerDestroy(viewer,ierr);CHKERRA(ierr)
55: call VecDestroy(u,ierr);CHKERRA(ierr)
56: call PetscOptionsSetValue(PETSC_NULL_OPTIONS,"-viewer_binary_mpiio",PETSC_NULL_CHARACTER,ierr);CHKERRA(ierr)
58: call PetscLogEventEnd(VECTOR_GENERATE,ierr);CHKERRA(ierr)
60: ! PART 2: Read in vector in binary format
62: ! Read new vector in binary format
63: call PetscLogEventRegister("Read Vector",0,VECTOR_READ,ierr);CHKERRA(ierr)
64: call PetscLogEventBegin(VECTOR_READ,ierr);CHKERRA(ierr)
65: call PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n",ierr);CHKERRA(ierr)
66: call PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,viewer,ierr);CHKERRA(ierr)
67: call VecCreate(PETSC_COMM_WORLD,u,ierr);CHKERRA(ierr)
68: call VecLoad(u,viewer,ierr);CHKERRA(ierr)
69: call PetscViewerDestroy(viewer,ierr);CHKERRA(ierr)
71: call PetscLogEventEnd(VECTOR_READ,ierr);CHKERRA(ierr)
72: call VecView(u,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
74: ! Free data structures
75: call VecDestroy(u,ierr);CHKERRA(ierr)
76: call PetscFinalize(ierr);CHKERRA(ierr)
77:
78: end program
81: !/*TEST
82: !
83: ! test:
84: ! nsize: 1
85: ! requires: mpiio
86: ! output_file: output/ex5_1.out
87: !
88: ! test:
89: ! suffix: 2
90: ! nsize: 2
91: ! requires: mpiio
92: ! output_file: output/ex5_2.out
93: !
94: !TEST*/