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
85: ! Duplicate some work vectors (of the same format and
86: ! partitioning as the initial vector).
88: call VecDuplicate(x,y,ierr)
89: call VecDuplicate(x,w,ierr)
91: ! Duplicate more work vectors (of the same format and
92: ! partitioning as the initial vector). Here we duplicate
93: ! an array of vectors, which is often more convenient than
94: ! duplicating individual ones.
96: call VecDuplicateVecs(x,ithree,z,ierr)
98: ! Set the vectors to entries to a constant value.
100: call VecSet(x,one,ierr)
101: call VecSet(y,two,ierr)
102: call VecSet(z(1),one,ierr)
103: call VecSet(z(2),two,ierr)
104: call VecSet(z(3),three,ierr)
106: ! Demonstrate various basic vector routines.
108: call VecDot(x,x,dot,ierr)
109: call VecMDot(ithree,x,z,dots,ierr)
111: ! Note: If using a complex numbers version of PETSc, then
112: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
113: ! (when using real numbers) it is undefined.
115: if (rank .eq. 0) then
116: #if defined(PETSC_USE_COMPLEX)
117: write(6,100) int(PetscRealPart(dot))
118: write(6,110) int(PetscRealPart(dots(1))), &
119: & int(PetscRealPart(dots(2))), &
120: & int(PetscRealPart(dots(3)))
121: #else
122: write(6,100) int(dot)
123: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
124: #endif
125: write(6,120)
126: endif
127: 100 format ('Vector length ',i6)
128: 110 format ('Vector length ',3(i6))
129: 120 format ('All other values should be near zero')
131: call VecScale(x,two,ierr)
132: call VecNorm(x,NORM_2,norm,ierr)
133: v = norm-2.0*sqrt(dble(n))
134: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
135: if (rank .eq. 0) write(6,130) v
136: 130 format ('VecScale ',1pe8.2)
138: call VecCopy(x,w,ierr)
139: call VecNorm(w,NORM_2,norm,ierr)
140: v = norm-2.0*sqrt(dble(n))
141: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
142: if (rank .eq. 0) write(6,140) v
143: 140 format ('VecCopy ',1pe8.2)
145: call VecAXPY(y,three,x,ierr)
146: call VecNorm(y,NORM_2,norm,ierr)
147: v = norm-8.0*sqrt(dble(n))
148: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
149: if (rank .eq. 0) write(6,150) v
150: 150 format ('VecAXPY ',1pe8.2)
152: call VecAYPX(y,two,x,ierr)
153: call VecNorm(y,NORM_2,norm,ierr)
154: v = norm-18.0*sqrt(dble(n))
155: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
156: if (rank .eq. 0) write(6,160) v
157: 160 format ('VecAYXP ',1pe8.2)
159: call VecSwap(x,y,ierr)
160: call VecNorm(y,NORM_2,norm,ierr)
161: v = norm-2.0*sqrt(dble(n))
162: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
163: if (rank .eq. 0) write(6,170) v
164: 170 format ('VecSwap ',1pe8.2)
166: call VecNorm(x,NORM_2,norm,ierr)
167: v = norm-18.0*sqrt(dble(n))
168: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
169: if (rank .eq. 0) write(6,180) v
170: 180 format ('VecSwap ',1pe8.2)
172: call VecWAXPY(w,two,x,y,ierr)
173: call VecNorm(w,NORM_2,norm,ierr)
174: v = norm-38.0*sqrt(dble(n))
175: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
176: if (rank .eq. 0) write(6,190) v
177: 190 format ('VecWAXPY ',1pe8.2)
179: call VecPointwiseMult(w,y,x,ierr)
180: call VecNorm(w,NORM_2,norm,ierr)
181: v = norm-36.0*sqrt(dble(n))
182: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
183: if (rank .eq. 0) write(6,200) v
184: 200 format ('VecPointwiseMult ',1pe8.2)
186: call VecPointwiseDivide(w,x,y,ierr)
187: call VecNorm(w,NORM_2,norm,ierr)
188: v = norm-9.0*sqrt(dble(n))
189: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
190: if (rank .eq. 0) write(6,210) v
191: 210 format ('VecPointwiseDivide ',1pe8.2)
193:
194: dots(1) = one
195: dots(2) = three
196: dots(3) = two
197: call VecSet(x,one,ierr)
198: call VecMAXPY(x,ithree,dots,z,ierr)
199: call VecNorm(z(1),NORM_2,norm,ierr)
200: v = norm-sqrt(dble(n))
201: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
202: call VecNorm(z(2),NORM_2,norm,ierr)
203: v1 = norm-2.0*sqrt(dble(n))
204: if (v1 .gt. -PETSC_SMALL .and. v1 .lt. PETSC_SMALL) v1 = 0.0
205: call VecNorm(z(3),NORM_2,norm,ierr)
206: v2 = norm-3.0*sqrt(dble(n))
207: if (v2 .gt. -PETSC_SMALL .and. v2 .lt. PETSC_SMALL) v2 = 0.0
208: if (rank .eq. 0) write(6,220) v,v1,v2
209: 220 format ('VecMAXPY ',3(1pe8.2))
212: ! Test whether vector has been corrupted (just to demonstrate this
213: ! routine) not needed in most application codes.
215: call VecValid(x,flg,ierr)
216: if (flg .ne. PETSC_TRUE) then
217: if (rank .eq. 0) then
218: write(6,*) 'Corrupted vector!'
219: endif
220: SETERRQ(1,' ',ierr)
221: endif
223: ! Free work space. All PETSc objects should be destroyed when they
224: ! are no longer needed.
226: call VecDestroy(x,ierr)
227: call VecDestroy(y,ierr)
228: call VecDestroy(w,ierr)
229: call VecDestroyVecs(z,ithree,ierr)
230: call PetscFinalize(ierr)
232: end
233: