Actual source code: ex1f90.F90
1: !
2: !
3: !
4: ! -----------------------------------------------------------------------
6: program main
7: #include <petsc/finclude/petscvec.h>
8: use petscvec
9: implicit none
11: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12: ! Variable declarations
13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14: !
15: ! Variables:
16: ! x, y, w - vectors
17: ! z - array of vectors
18: !
19: Vec x,y,w
20: Vec, pointer :: z(:)
21: PetscReal norm,v,v1,v2
22: PetscInt n,ithree
23: PetscErrorCode ierr
24: PetscMPIInt rank
25: PetscBool flg
26: PetscScalar one,two,three
27: PetscScalar dots(3),dot
28: PetscReal nfloat
30: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31: ! Beginning of program
32: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
35: if (ierr .ne. 0) then
36: print*,'Unable to initialize PETSc'
37: stop
38: endif
39: one = 1.0
40: two = 2.0
41: three = 3.0
42: n = 20
43: ithree = 3
45: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr);CHKERRA(ierr)
46: nfloat = n
47: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRA(ierr)
49: ! Create a vector, specifying only its global dimension.
50: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
51: ! the vector format (currently parallel
52: ! or sequential) is determined at runtime. Also, the parallel
53: ! partitioning of the vector is determined by PETSc at runtime.
54: !
55: ! Routines for creating particular vector types directly are:
56: ! VecCreateSeq() - uniprocessor vector
57: ! VecCreateMPI() - distributed vector, where the user can
58: ! determine the parallel partitioning
60: call VecCreate(PETSC_COMM_WORLD,x,ierr);CHKERRA(ierr)
61: call VecSetSizes(x,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
62: call VecSetFromOptions(x,ierr);CHKERRA(ierr)
64: ! Duplicate some work vectors (of the same format and
65: ! partitioning as the initial vector).
67: call VecDuplicate(x,y,ierr);CHKERRA(ierr)
68: call VecDuplicate(x,w,ierr);CHKERRA(ierr)
70: ! Duplicate more work vectors (of the same format and
71: ! partitioning as the initial vector). Here we duplicate
72: ! an array of vectors, which is often more convenient than
73: ! duplicating individual ones.
75: call VecDuplicateVecsF90(x,ithree,z,ierr);CHKERRA(ierr)
77: ! Set the vectors to entries to a constant value.
79: call VecSet(x,one,ierr);CHKERRA(ierr)
80: call VecSet(y,two,ierr);CHKERRA(ierr)
81: call VecSet(z(1),one,ierr);CHKERRA(ierr)
82: call VecSet(z(2),two,ierr);CHKERRA(ierr)
83: call VecSet(z(3),three,ierr);CHKERRA(ierr)
85: ! Demonstrate various basic vector routines.
87: call VecDot(x,x,dot,ierr);CHKERRA(ierr)
88: call VecMDot(x,ithree,z,dots,ierr);CHKERRA(ierr)
90: ! Note: If using a complex numbers version of PETSc, then
91: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
92: ! (when using real numbers) it is undefined.
94: if (rank .eq. 0) then
95: #if defined(PETSC_USE_COMPLEX)
96: write(6,100) int(PetscRealPart(dot))
97: write(6,110) int(PetscRealPart(dots(1))),int(PetscRealPart(dots(2))),int(PetscRealPart(dots(3)))
98: #else
99: write(6,100) int(dot)
100: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
101: #endif
102: write(6,120)
103: endif
104: 100 format ('Vector length ',i6)
105: 110 format ('Vector length ',3(i6))
106: 120 format ('All other values should be near zero')
108: call VecScale(x,two,ierr);CHKERRA(ierr)
109: call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
110: v = abs(norm-2.0*sqrt(nfloat))
111: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
112: if (rank .eq. 0) write(6,130) v
113: 130 format ('VecScale ',1pe9.2)
115: call VecCopy(x,w,ierr);CHKERRA(ierr)
116: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
117: v = abs(norm-2.0*sqrt(nfloat))
118: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
119: if (rank .eq. 0) write(6,140) v
120: 140 format ('VecCopy ',1pe9.2)
122: call VecAXPY(y,three,x,ierr);CHKERRA(ierr)
123: call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
124: v = abs(norm-8.0*sqrt(nfloat))
125: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
126: if (rank .eq. 0) write(6,150) v
127: 150 format ('VecAXPY ',1pe9.2)
129: call VecAYPX(y,two,x,ierr);CHKERRA(ierr)
130: call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
131: v = abs(norm-18.0*sqrt(nfloat))
132: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
133: if (rank .eq. 0) write(6,160) v
134: 160 format ('VecAYXP ',1pe9.2)
136: call VecSwap(x,y,ierr);CHKERRA(ierr)
137: call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
138: v = abs(norm-2.0*sqrt(nfloat))
139: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
140: if (rank .eq. 0) write(6,170) v
141: 170 format ('VecSwap ',1pe9.2)
143: call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
144: v = abs(norm-18.0*sqrt(nfloat))
145: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
146: if (rank .eq. 0) write(6,180) v
147: 180 format ('VecSwap ',1pe9.2)
149: call VecWAXPY(w,two,x,y,ierr);CHKERRA(ierr)
150: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
151: v = abs(norm-38.0*sqrt(nfloat))
152: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
153: if (rank .eq. 0) write(6,190) v
154: 190 format ('VecWAXPY ',1pe9.2)
156: call VecPointwiseMult(w,y,x,ierr);CHKERRA(ierr)
157: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
158: v = abs(norm-36.0*sqrt(nfloat))
159: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
160: if (rank .eq. 0) write(6,200) v
161: 200 format ('VecPointwiseMult ',1pe9.2)
163: call VecPointwiseDivide(w,x,y,ierr);CHKERRA(ierr)
164: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
165: v = abs(norm-9.0*sqrt(nfloat))
166: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
167: if (rank .eq. 0) write(6,210) v
168: 210 format ('VecPointwiseDivide ',1pe9.2)
170: dots(1) = one
171: dots(2) = three
172: dots(3) = two
173: call VecSet(x,one,ierr);CHKERRA(ierr)
174: call VecMAXPY(x,ithree,dots,z,ierr);CHKERRA(ierr)
175: call VecNorm(z(1),NORM_2,norm,ierr);CHKERRA(ierr)
176: v = abs(norm-sqrt(nfloat))
177: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
178: call VecNorm(z(2),NORM_2,norm,ierr);CHKERRA(ierr)
179: v1 = abs(norm-2.0*sqrt(nfloat))
180: if (v1 .gt. -1.d-10 .and. v1 .lt. 1.d-10) v1 = 0.0
181: call VecNorm(z(3),NORM_2,norm,ierr);CHKERRA(ierr)
182: v2 = abs(norm-3.0*sqrt(nfloat))
183: if (v2 .gt. -1.d-10 .and. v2 .lt. 1.d-10) v2 = 0.0
184: if (rank .eq. 0) write(6,220) v,v1,v2
185: 220 format ('VecMAXPY ',3(1pe9.2))
187: ! Free work space. All PETSc objects should be destroyed when they
188: ! are no longer needed.
190: call VecDestroy(x,ierr);CHKERRA(ierr)
191: call VecDestroy(y,ierr);CHKERRA(ierr)
192: call VecDestroy(w,ierr);CHKERRA(ierr)
193: call VecDestroyVecsF90(ithree,z,ierr);CHKERRA(ierr)
194: call PetscFinalize(ierr)
196: end
198: !
199: !/*TEST
200: !
201: ! test:
202: ! nsize: 2
203: !
204: !TEST*/