Actual source code: ex1f.F
1: !
2: ! Program usage: mpiexec 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: ! petscsys.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 finclude/petscsys.h
28: #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
46: PetscScalar dots(3),dot
47: character*(40) name
49: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50: ! Beginning of program
51: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
54: one = 1.0
55: two = 2.0
56: three = 3.0
57: n = 20
58: ithree = 3
59: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
60: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
62: ! Create a vector, specifying only its global dimension.
63: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
64: ! the vector format (currently parallel
65: ! or sequential) is determined at runtime. Also, the parallel
66: ! partitioning of the vector is determined by PETSc at runtime.
67: !
68: ! Routines for creating particular vector types directly are:
69: ! VecCreateSeq() - uniprocessor vector
70: ! VecCreateMPI() - distributed vector, where the user can
71: ! determine the parallel partitioning
72: ! VecCreateShared() - parallel vector that uses shared memory
73: ! (available only on the SGI); otherwise,
74: ! is the same as VecCreateMPI()
75: !
76: ! VecCreate(), VecSetSizes() and VecSetFromOptions() allows one
77: ! to determine at runtime which version to use
78: ! with the options -vec_type mpi or -vec_type shared
79: !
80: call VecCreate(PETSC_COMM_WORLD,x,ierr)
81: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
82: call VecSetFromOptions(x,ierr)
83: call VecGetType(x,name,ierr)
84: print*,name
86: ! Duplicate some work vectors (of the same format and
87: ! partitioning as the initial vector).
89: call VecDuplicate(x,y,ierr)
90: call VecDuplicate(x,w,ierr)
92: ! Duplicate more work vectors (of the same format and
93: ! partitioning as the initial vector). Here we duplicate
94: ! an array of vectors, which is often more convenient than
95: ! duplicating individual ones.
97: call VecDuplicateVecs(x,ithree,z,ierr)
99: ! Set the vectors to entries to a constant value.
101: call VecSet(x,one,ierr)
103: call VecSet(y,two,ierr)
104: call VecSet(z(1),one,ierr)
105: call VecSet(z(2),two,ierr)
106: call VecSet(z(3),three,ierr)
108: ! Demonstrate various basic vector routines.
110: call VecDot(x,x,dot,ierr)
111: call VecMDot(x,ithree,z,dots,ierr)
113: ! Note: If using a complex numbers version of PETSc, then
114: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
115: ! (when using real numbers) it is undefined.
117: if (rank .eq. 0) then
118: #if defined(PETSC_USE_COMPLEX)
119: write(6,100) int(PetscRealPart(dot))
120: write(6,110) int(PetscRealPart(dots(1))), &
121: & int(PetscRealPart(dots(2))), &
122: & int(PetscRealPart(dots(3)))
123: #else
124: write(6,100) int(dot)
125: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
126: #endif
127: write(6,120)
128: endif
129: 100 format ('Vector length ',i6)
130: 110 format ('Vector length ',3(i6))
131: 120 format ('All other values should be near zero')
133: call VecScale(x,two,ierr)
134: call VecNorm(x,NORM_2,norm,ierr)
135: v = norm-2.0*sqrt(dble(n))
136: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
137: if (rank .eq. 0) write(6,130) v
138: 130 format ('VecScale ',1pe8.2)
140: call VecCopy(x,w,ierr)
141: call VecNorm(w,NORM_2,norm,ierr)
142: v = norm-2.0*sqrt(dble(n))
143: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
144: if (rank .eq. 0) write(6,140) v
145: 140 format ('VecCopy ',1pe8.2)
147: call VecAXPY(y,three,x,ierr)
148: call VecNorm(y,NORM_2,norm,ierr)
149: v = norm-8.0*sqrt(dble(n))
150: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
151: if (rank .eq. 0) write(6,150) v
152: 150 format ('VecAXPY ',1pe8.2)
154: call VecAYPX(y,two,x,ierr)
155: call VecNorm(y,NORM_2,norm,ierr)
156: v = norm-18.0*sqrt(dble(n))
157: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
158: if (rank .eq. 0) write(6,160) v
159: 160 format ('VecAYXP ',1pe8.2)
161: call VecSwap(x,y,ierr)
162: call VecNorm(y,NORM_2,norm,ierr)
163: v = norm-2.0*sqrt(dble(n))
164: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
165: if (rank .eq. 0) write(6,170) v
166: 170 format ('VecSwap ',1pe8.2)
168: call VecNorm(x,NORM_2,norm,ierr)
169: v = norm-18.0*sqrt(dble(n))
170: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
171: if (rank .eq. 0) write(6,180) v
172: 180 format ('VecSwap ',1pe8.2)
174: call VecWAXPY(w,two,x,y,ierr)
175: call VecNorm(w,NORM_2,norm,ierr)
176: v = norm-38.0*sqrt(dble(n))
177: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
178: if (rank .eq. 0) write(6,190) v
179: 190 format ('VecWAXPY ',1pe8.2)
181: call VecPointwiseMult(w,y,x,ierr)
182: call VecNorm(w,NORM_2,norm,ierr)
183: v = norm-36.0*sqrt(dble(n))
184: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
185: if (rank .eq. 0) write(6,200) v
186: 200 format ('VecPointwiseMult ',1pe8.2)
188: call VecPointwiseDivide(w,x,y,ierr)
189: call VecNorm(w,NORM_2,norm,ierr)
190: v = norm-9.0*sqrt(dble(n))
191: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
192: if (rank .eq. 0) write(6,210) v
193: 210 format ('VecPointwiseDivide ',1pe8.2)
195:
196: dots(1) = one
197: dots(2) = three
198: dots(3) = two
199: call VecSet(x,one,ierr)
200: call VecMAXPY(x,ithree,dots,z,ierr)
201: call VecNorm(z(1),NORM_2,norm,ierr)
202: v = norm-sqrt(dble(n))
203: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
204: call VecNorm(z(2),NORM_2,norm,ierr)
205: v1 = norm-2.0*sqrt(dble(n))
206: if (v1 .gt. -PETSC_SMALL .and. v1 .lt. PETSC_SMALL) v1 = 0.0
207: call VecNorm(z(3),NORM_2,norm,ierr)
208: v2 = norm-3.0*sqrt(dble(n))
209: if (v2 .gt. -PETSC_SMALL .and. v2 .lt. PETSC_SMALL) v2 = 0.0
210: if (rank .eq. 0) write(6,220) v,v1,v2
211: 220 format ('VecMAXPY ',3(1pe8.2))
214: ! Test whether vector has been corrupted (just to demonstrate this
215: ! routine) not needed in most application codes.
217: call VecValid(x,flg,ierr)
218: if (.not. flg) then
219: if (rank .eq. 0) then
220: write(6,*) 'Corrupted vector!'
221: endif
222: SETERRQ(1,' ',ierr)
223: endif
225: ! Free work space. All PETSc objects should be destroyed when they
226: ! are no longer needed.
228: call VecDestroy(x,ierr)
229: call VecDestroy(y,ierr)
230: call VecDestroy(w,ierr)
231: call VecDestroyVecs(z,ithree,ierr)
232: call PetscFinalize(ierr)
234: end
235: