Actual source code: ex21f.F
1: !
2: ! Solves a linear system in parallel with KSP. Also indicates
3: ! use of a user-provided preconditioner. Input parameters include:
4: !
5: ! Program usage: mpirun ex21f [-help] [all PETSc options]
6: !
7: !/*T
8: ! Concepts: KSP^basic parallel example
9: ! Concepts: PC^setting a user-defined shell preconditioner
10: ! Processors: n
11: !T*/
12: !
13: ! -------------------------------------------------------------------------
15: program main
16: implicit none
18: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
19: ! Include files
20: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
21: !
22: ! petsc.h - base PETSc routines petscvec.h - vectors
23: ! petscsys.h - system routines petscmat.h - matrices
24: ! petscksp.h - Krylov subspace methods petscpc.h - preconditioners
26: #include include/finclude/petsc.h
27: #include include/finclude/petscvec.h
28: #include include/finclude/petscmat.h
29: #include include/finclude/petscpc.h
30: #include include/finclude/petscksp.h
32: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33: ! Variable declarations
34: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35: !
36: ! Variables:
37: ! ksp - linear solver context
38: ! ksp - Krylov subspace method context
39: ! pc - preconditioner context
40: ! x, b, u - approx solution, right-hand-side, exact solution vectors
41: ! A - matrix that defines linear system
42: ! its - iterations for convergence
43: ! norm - norm of solution error
45: Vec x,b,u
46: Mat A
47: PC pc
48: KSP ksp
49: PetscScalar v,one,neg_one
50: double precision norm,tol
51: PetscInt i,j,II,JJ,Istart,Iend,m,n,its,ione
52: PetscMPIInt rank
53: PetscTruth flg
54: PetscErrorCode ierr
56: ! Note: Any user-defined Fortran routines MUST be declared as external.
58: external SampleShellPCSetUp,SampleShellPCApply
60: ! Common block to store data for user-provided preconditioner
61: common /mypcs/ jacobi,sor,work
62: PC jacobi,sor
63: Vec work
65: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66: ! Beginning of program
67: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
69: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
70: one = 1.0
71: neg_one = -1.0
72: m = 8
73: n = 7
74: ione = 1
75: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-m',m,flg,ierr)
76: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
77: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
79: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80: ! Compute the matrix and right-hand-side vector that define
81: ! the linear system, Ax = b.
82: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84: ! Create parallel matrix, specifying only its global dimensions.
85: ! When using MatCreate(), the matrix format can be specified at
86: ! runtime. Also, the parallel partitioning of the matrix is
87: ! determined by PETSc at runtime.
89: call MatCreate(PETSC_COMM_WORLD,A,ierr)
90: call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,ierr)
92: call MatSetFromOptions(A,ierr)
94: ! Currently, all PETSc parallel matrix formats are partitioned by
95: ! contiguous chunks of rows across the processors. Determine which
96: ! rows of the matrix are locally owned.
98: call MatGetOwnershipRange(A,Istart,Iend,ierr)
100: ! Set matrix elements for the 2-D, five-point stencil in parallel.
101: ! - Each processor needs to insert only elements that it owns
102: ! locally (but any non-local elements will be sent to the
103: ! appropriate processor during matrix assembly).
104: ! - Always specify global row and columns of matrix entries.
105: ! - Note that MatSetValues() uses 0-based row and column numbers
106: ! in Fortran as well as in C.
108: do 10, II=Istart,Iend-1
109: v = -1.0
110: i = II/n
111: j = II - i*n
112: if (i.gt.0) then
113: JJ = II - n
114: call MatSetValues(A,ione,II,ione,JJ,v,ADD_VALUES,ierr)
115: endif
116: if (i.lt.m-1) then
117: JJ = II + n
118: call MatSetValues(A,ione,II,ione,JJ,v,ADD_VALUES,ierr)
119: endif
120: if (j.gt.0) then
121: JJ = II - 1
122: call MatSetValues(A,ione,II,ione,JJ,v,ADD_VALUES,ierr)
123: endif
124: if (j.lt.n-1) then
125: JJ = II + 1
126: call MatSetValues(A,ione,II,ione,JJ,v,ADD_VALUES,ierr)
127: endif
128: v = 4.0
129: call MatSetValues(A,ione,II,ione,II,v,ADD_VALUES,ierr)
130: 10 continue
132: ! Assemble matrix, using the 2-step process:
133: ! MatAssemblyBegin(), MatAssemblyEnd()
134: ! Computations can be done while messages are in transition,
135: ! by placing code between these two statements.
137: call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
138: call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
140: ! Create parallel vectors.
141: ! - Here, the parallel partitioning of the vector is determined by
142: ! PETSc at runtime. We could also specify the local dimensions
143: ! if desired -- or use the more general routine VecCreate().
144: ! - When solving a linear system, the vectors and matrices MUST
145: ! be partitioned accordingly. PETSc automatically generates
146: ! appropriately partitioned matrices and vectors when MatCreate()
147: ! and VecCreate() are used with the same communicator.
148: ! - Note: We form 1 vector from scratch and then duplicate as needed.
150: call VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,m*n,u,ierr)
151: call VecDuplicate(u,b,ierr)
152: call VecDuplicate(b,x,ierr)
154: ! Set exact solution; then compute right-hand-side vector.
156: call VecSet(u,one,ierr)
157: call MatMult(A,u,b,ierr)
159: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160: ! Create the linear solver and set various options
161: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
163: ! Create linear solver context
165: call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)
167: ! Set operators. Here the matrix that defines the linear system
168: ! also serves as the preconditioning matrix.
170: call KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN,ierr)
172: ! Set linear solver defaults for this problem (optional).
173: ! - By extracting the KSP and PC contexts from the KSP context,
174: ! we can then directly directly call any KSP and PC routines
175: ! to set various options.
177: call KSPGetPC(ksp,pc,ierr)
178: tol = 1.e-7
179: call KSPSetTolerances(ksp,tol,PETSC_DEFAULT_DOUBLE_PRECISION, &
180: & PETSC_DEFAULT_DOUBLE_PRECISION,PETSC_DEFAULT_INTEGER,ierr)
182: !
183: ! Set a user-defined shell preconditioner
184: !
186: ! (Required) Indicate to PETSc that we are using a shell preconditioner
187: call PCSetType(pc,PCSHELL,ierr)
189: ! (Required) Set the user-defined routine for applying the preconditioner
190: call PCShellSetApply(pc,SampleShellPCApply,ierr)
192: ! (Optional) Do any setup required for the preconditioner
193: call SampleShellPCSetUp(A,x,ierr)
196: ! Set runtime options, e.g.,
197: ! -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
198: ! These options will override those specified above as long as
199: ! KSPSetFromOptions() is called _after_ any other customization
200: ! routines.
202: call KSPSetFromOptions(ksp,ierr)
204: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
205: ! Solve the linear system
206: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
208: call KSPSolve(ksp,b,x,ierr)
210: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
211: ! Check solution and clean up
212: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
214: ! Check the error
216: call VecAXPY(x,neg_one,u,ierr)
217: call VecNorm(x,NORM_2,norm,ierr)
218: call KSPGetIterationNumber(ksp,its,ierr)
220: if (rank .eq. 0) then
221: if (norm .gt. 1.e-12) then
222: write(6,100) norm,its
223: else
224: write(6,110) its
225: endif
226: endif
227: 100 format('Norm of error ',1pe10.4,' iterations ',i5)
228: 110 format('Norm of error < 1.e-12,iterations ',i5)
231: ! Free work space. All PETSc objects should be destroyed when they
232: ! are no longer needed.
234: call KSPDestroy(ksp,ierr)
235: call VecDestroy(u,ierr)
236: call VecDestroy(x,ierr)
237: call VecDestroy(b,ierr)
238: call MatDestroy(A,ierr)
240: ! Free up PCShell data
241: call PCDestroy(sor,ierr)
242: call PCDestroy(jacobi,ierr)
243: call VecDestroy(work,ierr)
246: ! Always call PetscFinalize() before exiting a program.
248: call PetscFinalize(ierr)
249: end
251: !/***********************************************************************/
252: !/* Routines for a user-defined shell preconditioner */
253: !/***********************************************************************/
255: !
256: ! SampleShellPCSetUp - This routine sets up a user-defined
257: ! preconditioner context.
258: !
259: ! Input Parameters:
260: ! pmat - preconditioner matrix
261: ! x - vector
262: !
263: ! Output Parameter:
264: ! ierr - error code (nonzero if error has been detected)
265: !
266: ! Notes:
267: ! In this example, we define the shell preconditioner to be Jacobi
268: ! method. Thus, here we create a work vector for storing the reciprocal
269: ! of the diagonal of the preconditioner matrix; this vector is then
270: ! used within the routine SampleShellPCApply().
271: !
272: subroutine SampleShellPCSetUp(pmat,x,ierr)
274: implicit none
276: #include include/finclude/petsc.h
277: #include include/finclude/petscvec.h
278: #include include/finclude/petscmat.h
280: Vec x
281: Mat pmat
282: PetscErrorCode ierr
284: ! Common block to store data for user-provided preconditioner
285: common /mypcs/ jacobi,sor,work
286: PC jacobi,sor
287: Vec work
289: call PCCreate(PETSC_COMM_WORLD,jacobi,ierr)
290: call PCSetType(jacobi,PCJACOBI,ierr)
291: call PCSetOperators(jacobi,pmat,pmat,DIFFERENT_NONZERO_PATTERN, &
292: & ierr)
293: call PCSetUp(jacobi,ierr)
295: call PCCreate(PETSC_COMM_WORLD,sor,ierr)
296: call PCSetType(sor,PCSOR,ierr)
297: call PCSetOperators(sor,pmat,pmat,DIFFERENT_NONZERO_PATTERN, &
298: & ierr)
299: ! call PCSORSetSymmetric(sor,SOR_LOCAL_SYMMETRIC_SWEEP,ierr)
300: call PCSetUp(sor,ierr)
302: call VecDuplicate(x,work,ierr)
304: end
306: ! -------------------------------------------------------------------
307: !
308: ! SampleShellPCApply - This routine demonstrates the use of a
309: ! user-provided preconditioner.
310: !
311: ! Input Parameters:
312: ! dummy - optional user-defined context, not used here
313: ! x - input vector
314: !
315: ! Output Parameters:
316: ! y - preconditioned vector
317: ! ierr - error code (nonzero if error has been detected)
318: !
319: ! Notes:
320: ! This code implements the Jacobi preconditioner plus the
321: ! SOR preconditioner
322: !
323: ! YOU CAN GET THE EXACT SAME EFFECT WITH THE PCCOMPOSITE preconditioner using
324: ! mpirun -np 1 ex21f -ksp_monitor -pc_type composite -pc_composite_pcs jacobi,sor -pc_composite_type additive
325: !
326: subroutine SampleShellPCApply(dummy,x,y,ierr)
328: implicit none
330: #include include/finclude/petsc.h
331: #include include/finclude/petscvec.h
332: #include include/finclude/petscpc.h
334: Vec x,y
335: integer dummy
336: PetscErrorCode ierr
337: PetscScalar one
338:
339: ! Common block to store data for user-provided preconditioner
340: common /mypcs/ jacobi,sor,work
341: PC jacobi,sor
342: Vec work
344: one = 1.0
345: call PCApply(jacobi,x,y,ierr)
346: call PCApply(sor,x,work,ierr)
347: call VecAXPY(y,one,work,ierr)
349: end