Actual source code: ex6f.F90
petsc-3.12.1 2019-10-22
1: program main
2: #include <petsc/finclude/petscvec.h>
3: use petscvec
5: implicit none
7: PetscErrorCode ierr
8: PetscMPIInt :: mySize
9: integer :: fd
10: PetscInt :: i,sz
11: PetscInt,parameter :: m = 10
12: PetscInt,parameter :: one = 1
13: PetscInt, allocatable,dimension(:) :: t
14: PetscScalar, pointer, dimension(:) :: avec
15: PetscScalar, pointer, dimension(:) :: array
16: Vec vec
17: PetscViewer view_out,view_in
18: character(len=256) :: outstring
19: PetscBool :: flg
20:
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_size(PETSC_COMM_WORLD,mySize,ierr)
28:
29: if (mySize /= 1) then
30: SETERRA(PETSC_COMM_SELF,1,"This is a uniprocessor example only!")
31: endif
32:
33:
35: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-m",m,flg,ierr);CHKERRA(ierr)
37: ! ----------------------------------------------------------------------
38: ! PART 1: Write some data to a file in binary format
39: ! ----------------------------------------------------------------------
41: ! Allocate array and set values
42:
43: allocate(array(0:m-1))
44: do i=0,m-1
45: array(i) = real(i)*10.0
46: end do
47:
48: allocate(t(1))
49: t(1) = m
50: ! Open viewer for binary output
51: call PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_WRITE,view_out,ierr);CHKERRA(ierr)
52: call PetscViewerBinaryGetDescriptor(view_out,fd,ierr);CHKERRA(ierr)
53:
54: ! Write binary output
55: call PetscBinaryWrite(fd,t,one,PETSC_INT,PETSC_FALSE,ierr);CHKERRA(ierr)
56: call PetscBinaryWrite(fd,array,m,PETSC_SCALAR,PETSC_FALSE,ierr);CHKERRA(ierr)
57:
58: ! Destroy the output viewer and work array
59: call PetscViewerDestroy(view_out,ierr);CHKERRA(ierr)
60:
61: ! ----------------------------------------------------------------------
62: ! PART 2: Read data from file and form a vector
63: ! ----------------------------------------------------------------------
65: ! Open input binary viewer
66: call PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_READ,view_in,ierr);CHKERRA(ierr)
67: call PetscViewerBinaryGetDescriptor(view_in,fd,ierr);CHKERRA(ierr)
69: ! Create vector and get pointer to data space
70: call VecCreate(PETSC_COMM_SELF,vec,ierr);CHKERRA(ierr)
71: call VecSetSizes(vec,PETSC_DECIDE,m,ierr);CHKERRA(ierr)
72:
73: call VecSetFromOptions(vec,ierr);CHKERRA(ierr)
74:
75: call VecGetArrayF90(vec,avec,ierr);CHKERRA(ierr)
77: ! Read data into vector
78: call PetscBinaryRead(fd,t,one,PETSC_NULL_INTEGER,PETSC_INT,ierr);CHKERRA(ierr)
79: sz=t(1)
80:
81: if (sz <= 0) then
82: SETERRA(PETSC_COMM_SELF,1,"Error: Must have array length > 0")
83: endif
84:
85: write(outstring,'(a,i2.2,a)') "reading data in binary from input.dat, sz =", sz, " ...\n"
86: call PetscPrintf(PETSC_COMM_SELF,trim(outstring),ierr);CHKERRA(ierr)
87:
88: call PetscBinaryRead(fd,avec,sz,PETSC_NULL_INTEGER,PETSC_SCALAR,ierr);CHKERRA(ierr)
90: ! View vector
91: call VecRestoreArrayF90(vec,avec,ierr);CHKERRA(ierr)
92: call VecView(vec,PETSC_VIEWER_STDOUT_SELF,ierr);CHKERRA(ierr)
94: ! Free data structures
95: call VecDestroy(vec,ierr);CHKERRA(ierr)
96: call PetscViewerDestroy(view_in,ierr);CHKERRA(ierr)
97: call PetscFinalize(ierr);CHKERRA(ierr)
98:
99: end program
101: !/*TEST
102: !
103: ! test:
104: ! output_file: output/ex6_1.out
105: !
106: !TEST*/