Actual source code: ex1f.F
1: !
2: ! Program usage: mpirun ex1f [-help] [all PETSc options]
3: !
4: !/*T
5: ! Concepts: vectors^basic routines
6: ! Processors: n
7: !T*/
8: !
9: ! -----------------------------------------------------------------------
11: program main
12: implicit none
14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15: ! Include files
16: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17: !
18: ! The following include statements are required for Fortran programs
19: ! that use PETSc vectors:
20: ! petsc.h - base PETSc routines
21: ! petscvec.h - vectors
22: ! Additional include statements may be needed if using additional
23: ! PETSc routines in a Fortran program, e.g.,
24: ! petscviewer.h - viewers
25: ! petscis.h - index sets
26: !
27: #include include/finclude/petsc.h
28: #include include/finclude/petscvec.h
30: !
31: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32: ! Variable declarations
33: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34: !
35: ! Variables:
36: ! x, y, w - vectors
37: ! z - array of vectors
39: Vec x,y,w,z(5)
40: PetscReal norm,v,v1,v2
41: PetscInt n,ithree
42: PetscTruth flg
43: PetscErrorCode ierr
44: PetscMPIInt rank
45: PetscScalar one,two,three,dots(3),dot
46: character*(80) name
48: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49: ! Beginning of program
50: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
53: one = 1.0
54: two = 2.0
55: three = 3.0
56: n = 20
57: ithree = 3
58: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
59: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
61: ! Create a vector, specifying only its global dimension.
62: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
63: ! the vector format (currently parallel
64: ! or sequential) is determined at runtime. Also, the parallel
65: ! partitioning of the vector is determined by PETSc at runtime.
66: !
67: ! Routines for creating particular vector types directly are:
68: ! VecCreateSeq() - uniprocessor vector
69: ! VecCreateMPI() - distributed vector, where the user can
70: ! determine the parallel partitioning
71: ! VecCreateShared() - parallel vector that uses shared memory
72: ! (available only on the SGI); otherwise,
73: ! is the same as VecCreateMPI()
74: !
75: ! VecCreate(), VecSetSizes() and VecSetFromOptions() allows one
76: ! to determine at runtime which version to use
77: ! with the options -vec_type mpi or -vec_type shared
78: !
79: call VecCreate(PETSC_COMM_WORLD,x,ierr)
80: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
81: call VecSetFromOptions(x,ierr)
82: call VecGetType(x,name,ierr)
83: print*,name
84: ! Duplicate some work vectors (of the same format and
85: ! partitioning as the initial vector).
87: call VecDuplicate(x,y,ierr)
88: call VecDuplicate(x,w,ierr)
90: ! Duplicate more work vectors (of the same format and
91: ! partitioning as the initial vector). Here we duplicate
92: ! an array of vectors, which is often more convenient than
93: ! duplicating individual ones.
95: call VecDuplicateVecs(x,ithree,z,ierr)
97: ! Set the vectors to entries to a constant value.
99: call VecSet(one,x,ierr)
100: call VecSet(two,y,ierr)
101: call VecSet(one,z(1),ierr)
102: call VecSet(two,z(2),ierr)
103: call VecSet(three,z(3),ierr)
105: ! Demonstrate various basic vector routines.
107: call VecDot(x,x,dot,ierr)
108: call VecMDot(ithree,x,z,dots,ierr)
110: ! Note: If using a complex numbers version of PETSc, then
111: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
112: ! (when using real numbers) it is undefined.
114: if (rank .eq. 0) then
115: #if defined(PETSC_USE_COMPLEX)
116: write(6,100) int(PetscRealPart(dot))
117: write(6,110) int(PetscRealPart(dots(1))), &
118: & int(PetscRealPart(dots(2))), &
119: & int(PetscRealPart(dots(3)))
120: #else
121: write(6,100) int(dot)
122: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
123: #endif
124: write(6,120)
125: endif
126: 100 format ('Vector length ',i6)
127: 110 format ('Vector length ',3(i6))
128: 120 format ('All other values should be near zero')
130: call VecScale(two,x,ierr)
131: call VecNorm(x,NORM_2,norm,ierr)
132: v = norm-2.0*sqrt(dble(n))
133: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
134: if (rank .eq. 0) write(6,130) v
135: 130 format ('VecScale ',1pe8.2)
137: call VecCopy(x,w,ierr)
138: call VecNorm(w,NORM_2,norm,ierr)
139: v = norm-2.0*sqrt(dble(n))
140: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
141: if (rank .eq. 0) write(6,140) v
142: 140 format ('VecCopy ',1pe8.2)
144: call VecAXPY(three,x,y,ierr)
145: call VecNorm(y,NORM_2,norm,ierr)
146: v = norm-8.0*sqrt(dble(n))
147: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
148: if (rank .eq. 0) write(6,150) v
149: 150 format ('VecAXPY ',1pe8.2)
151: call VecAYPX(two,x,y,ierr)
152: call VecNorm(y,NORM_2,norm,ierr)
153: v = norm-18.0*sqrt(dble(n))
154: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
155: if (rank .eq. 0) write(6,160) v
156: 160 format ('VecAYXP ',1pe8.2)
158: call VecSwap(x,y,ierr)
159: call VecNorm(y,NORM_2,norm,ierr)
160: v = norm-2.0*sqrt(dble(n))
161: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
162: if (rank .eq. 0) write(6,170) v
163: 170 format ('VecSwap ',1pe8.2)
165: call VecNorm(x,NORM_2,norm,ierr)
166: v = norm-18.0*sqrt(dble(n))
167: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
168: if (rank .eq. 0) write(6,180) v
169: 180 format ('VecSwap ',1pe8.2)
171: call VecWAXPY(two,x,y,w,ierr)
172: call VecNorm(w,NORM_2,norm,ierr)
173: v = norm-38.0*sqrt(dble(n))
174: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
175: if (rank .eq. 0) write(6,190) v
176: 190 format ('VecWAXPY ',1pe8.2)
178: call VecPointwiseMult(y,x,w,ierr)
179: call VecNorm(w,NORM_2,norm,ierr)
180: v = norm-36.0*sqrt(dble(n))
181: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
182: if (rank .eq. 0) write(6,200) v
183: 200 format ('VecPointwiseMult ',1pe8.2)
185: call VecPointwiseDivide(x,y,w,ierr)
186: call VecNorm(w,NORM_2,norm,ierr)
187: v = norm-9.0*sqrt(dble(n))
188: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
189: if (rank .eq. 0) write(6,210) v
190: 210 format ('VecPointwiseDivide ',1pe8.2)
192:
193: dots(1) = one
194: dots(2) = three
195: dots(3) = two
196: call VecSet(one,x,ierr)
197: call VecMAXPY(ithree,dots,x,z,ierr)
198: call VecNorm(z(1),NORM_2,norm,ierr)
199: v = norm-sqrt(dble(n))
200: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
201: call VecNorm(z(2),NORM_2,norm,ierr)
202: v1 = norm-2.0*sqrt(dble(n))
203: if (v1 .gt. -PETSC_SMALL .and. v1 .lt. PETSC_SMALL) v1 = 0.0
204: call VecNorm(z(3),NORM_2,norm,ierr)
205: v2 = norm-3.0*sqrt(dble(n))
206: if (v2 .gt. -PETSC_SMALL .and. v2 .lt. PETSC_SMALL) v2 = 0.0
207: if (rank .eq. 0) write(6,220) v,v1,v2
208: 220 format ('VecMAXPY ',3(1pe8.2))
211: ! Test whether vector has been corrupted (just to demonstrate this
212: ! routine) not needed in most application codes.
214: call VecValid(x,flg,ierr)
215: if (flg .ne. PETSC_TRUE) then
216: if (rank .eq. 0) then
217: write(6,*) 'Corrupted vector!'
218: endif
219: SETERRQ(1,' ',ierr)
220: endif
222: ! Free work space. All PETSc objects should be destroyed when they
223: ! are no longer needed.
225: call VecDestroy(x,ierr)
226: call VecDestroy(y,ierr)
227: call VecDestroy(w,ierr)
228: call VecDestroyVecs(z,ithree,ierr)
229: call PetscFinalize(ierr)
231: end
232: