Actual source code: ex15f.F
1: !
2: ! "$Id: ex15f.F,v 1.17 2001/08/07 03:04:00 balay Exp $";
3: !
4: ! Solves a linear system in parallel with KSP. Also indicates
5: ! use of a user-provided preconditioner. Input parameters include:
6: ! -user_defined_pc : Activate a user-defined preconditioner
7: !
8: ! Program usage: mpirun ex15f [-help] [all PETSc options]
9: !
10: !/*T
11: ! Concepts: KSP^basic parallel example
12: ! Concepts: PC^setting a user-defined shell preconditioner
13: ! Processors: n
14: !T*/
15: !
16: ! -------------------------------------------------------------------------
18: program main
19: implicit none
21: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22: ! Include files
23: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
24: !
25: ! petsc.h - base PETSc routines petscvec.h - vectors
26: ! petscsys.h - system routines petscmat.h - matrices
27: ! petscksp.h - Krylov subspace methods petscpc.h - preconditioners
29: #include include/finclude/petsc.h
30: #include include/finclude/petscvec.h
31: #include include/finclude/petscmat.h
32: #include include/finclude/petscpc.h
33: #include include/finclude/petscksp.h
35: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36: ! Variable declarations
37: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
38: !
39: ! Variables:
40: ! ksp - linear solver context
41: ! ksp - Krylov subspace method context
42: ! pc - preconditioner context
43: ! x, b, u - approx solution, right-hand-side, exact solution vectors
44: ! A - matrix that defines linear system
45: ! its - iterations for convergence
46: ! norm - norm of solution error
48: Vec x,b,u
49: Mat A
50: PC pc
51: KSP ksp
52: PetscScalar v,one,neg_one
53: double precision norm,tol
54: integer i,j,II,JJ,Istart,Iend,ierr,m,n
55: integer user_defined_pc,its,flg,rank
57: ! Note: Any user-defined Fortran routines MUST be declared as external.
59: external SampleShellPCSetUp, SampleShellPCApply
61: ! Common block to store data for user-provided preconditioner
62: common /myshellpc/ diag
63: Vec diag
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: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-m',m,flg,ierr)
75: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
76: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
78: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79: ! Compute the matrix and right-hand-side vector that define
80: ! the linear system, Ax = b.
81: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83: ! Create parallel matrix, specifying only its global dimensions.
84: ! When using MatCreate(), the matrix format can be specified at
85: ! runtime. Also, the parallel partitioning of the matrix is
86: ! determined by PETSc at runtime.
88: call MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n, &
89: & m*n,A,ierr)
90: call MatSetFromOptions(A,ierr)
92: ! Currently, all PETSc parallel matrix formats are partitioned by
93: ! contiguous chunks of rows across the processors. Determine which
94: ! rows of the matrix are locally owned.
96: call MatGetOwnershipRange(A,Istart,Iend,ierr)
98: ! Set matrix elements for the 2-D, five-point stencil in parallel.
99: ! - Each processor needs to insert only elements that it owns
100: ! locally (but any non-local elements will be sent to the
101: ! appropriate processor during matrix assembly).
102: ! - Always specify global row and columns of matrix entries.
103: ! - Note that MatSetValues() uses 0-based row and column numbers
104: ! in Fortran as well as in C.
106: do 10, II=Istart,Iend-1
107: v = -1.0
108: i = II/n
109: j = II - i*n
110: if (i.gt.0) then
111: JJ = II - n
112: call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
113: endif
114: if (i.lt.m-1) then
115: JJ = II + n
116: call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
117: endif
118: if (j.gt.0) then
119: JJ = II - 1
120: call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
121: endif
122: if (j.lt.n-1) then
123: JJ = II + 1
124: call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
125: endif
126: v = 4.0
127: call MatSetValues(A,1,II,1,II,v,ADD_VALUES,ierr)
128: 10 continue
130: ! Assemble matrix, using the 2-step process:
131: ! MatAssemblyBegin(), MatAssemblyEnd()
132: ! Computations can be done while messages are in transition,
133: ! by placing code between these two statements.
135: call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
136: call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
138: ! Create parallel vectors.
139: ! - Here, the parallel partitioning of the vector is determined by
140: ! PETSc at runtime. We could also specify the local dimensions
141: ! if desired -- or use the more general routine VecCreate().
142: ! - When solving a linear system, the vectors and matrices MUST
143: ! be partitioned accordingly. PETSc automatically generates
144: ! appropriately partitioned matrices and vectors when MatCreate()
145: ! and VecCreate() are used with the same communicator.
146: ! - Note: We form 1 vector from scratch and then duplicate as needed.
148: call VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,m*n,u,ierr)
149: call VecDuplicate(u,b,ierr)
150: call VecDuplicate(b,x,ierr)
152: ! Set exact solution; then compute right-hand-side vector.
154: call VecSet(one,u,ierr)
155: call MatMult(A,u,b,ierr)
157: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158: ! Create the linear solver and set various options
159: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161: ! Create linear solver context
163: call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)
165: ! Set operators. Here the matrix that defines the linear system
166: ! also serves as the preconditioning matrix.
168: call KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN,ierr)
170: ! Set linear solver defaults for this problem (optional).
171: ! - By extracting the KSP and PC contexts from the KSP context,
172: ! we can then directly directly call any KSP and PC routines
173: ! to set various options.
175: call KSPGetPC(ksp,pc,ierr)
176: tol = 1.e-7
177: call KSPSetTolerances(ksp,tol,PETSC_DEFAULT_DOUBLE_PRECISION, &
178: & PETSC_DEFAULT_DOUBLE_PRECISION,PETSC_DEFAULT_INTEGER,ierr)
180: !
181: ! Set a user-defined shell preconditioner if desired
182: !
183: call PetscOptionsHasName(PETSC_NULL_CHARACTER,'-user_defined_pc', &
184: & user_defined_pc,ierr)
186: if (user_defined_pc .eq. 1) then
188: ! (Required) Indicate to PETSc that we are using a shell preconditioner
189: call PCSetType(pc,PCSHELL,ierr)
191: ! (Required) Set the user-defined routine for applying the preconditioner
192: call PCShellSetApply(pc,SampleShellPCApply,PETSC_NULL_OBJECT, &
193: & ierr)
195: ! (Optional) Do any setup required for the preconditioner
196: call SampleShellPCSetUp(A,x,ierr)
198: else
199: call PCSetType(pc,PCJACOBI,ierr)
200: endif
202: ! Set runtime options, e.g.,
203: ! -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
204: ! These options will override those specified above as long as
205: ! KSPSetFromOptions() is called _after_ any other customization
206: ! routines.
208: call KSPSetFromOptions(ksp,ierr)
210: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
211: ! Solve the linear system
212: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
214: call KSPSetRhs(ksp,b,ierr)
215: call KSPSetSolution(ksp,x,ierr)
216: call KSPSolve(ksp,ierr)
218: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219: ! Check solution and clean up
220: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222: ! Check the error
224: call VecAXPY(neg_one,u,x,ierr)
225: call VecNorm(x,NORM_2,norm,ierr)
226: call KSPGetIterationNumber(ksp,its,ierr)
228: if (rank .eq. 0) then
229: if (norm .gt. 1.e-12) then
230: write(6,100) norm,its
231: else
232: write(6,110) its
233: endif
234: endif
235: 100 format('Norm of error ',1pe10.4,' iterations ',i5)
236: 110 format('Norm of error < 1.e-12,iterations ',i5)
238: ! Free work space. All PETSc objects should be destroyed when they
239: ! are no longer needed.
241: call KSPDestroy(ksp,ierr)
242: call VecDestroy(u,ierr)
243: call VecDestroy(x,ierr)
244: call VecDestroy(b,ierr)
245: call MatDestroy(A,ierr)
246: if (user_defined_pc .eq. 1) then
247: call VecDestroy(diag,ierr)
248: endif
251: ! Always call PetscFinalize() before exiting a program.
253: call PetscFinalize(ierr)
254: end
256: !/***********************************************************************/
257: !/* Routines for a user-defined shell preconditioner */
258: !/***********************************************************************/
260: !
261: ! SampleShellPCSetUp - This routine sets up a user-defined
262: ! preconditioner context.
263: !
264: ! Input Parameters:
265: ! pmat - preconditioner matrix
266: ! x - vector
267: !
268: ! Output Parameter:
269: ! ierr - error code (nonzero if error has been detected)
270: !
271: ! Notes:
272: ! In this example, we define the shell preconditioner to be Jacobi
273: ! method. Thus, here we create a work vector for storing the reciprocal
274: ! of the diagonal of the preconditioner matrix; this vector is then
275: ! used within the routine SampleShellPCApply().
276: !
277: subroutine SampleShellPCSetUp(pmat,x,ierr)
279: implicit none
281: #include include/finclude/petsc.h
282: #include include/finclude/petscvec.h
283: #include include/finclude/petscmat.h
285: Vec x
286: Mat pmat
287: integer ierr
289: ! Common block to store data for user-provided preconditioner
290: common /myshellpc/ diag
291: Vec diag
293: call VecDuplicate(x,diag,ierr)
294: call MatGetDiagonal(pmat,diag,ierr)
295: call VecReciprocal(diag,ierr)
297: end
299: ! -------------------------------------------------------------------
300: !
301: ! SampleShellPCApply - This routine demonstrates the use of a
302: ! user-provided preconditioner.
303: !
304: ! Input Parameters:
305: ! dummy - optional user-defined context, not used here
306: ! x - input vector
307: !
308: ! Output Parameters:
309: ! y - preconditioned vector
310: ! ierr - error code (nonzero if error has been detected)
311: !
312: ! Notes:
313: ! This code implements the Jacobi preconditioner, merely as an
314: ! example of working with a PCSHELL. Note that the Jacobi method
315: ! is already provided within PETSc.
316: !
317: subroutine SampleShellPCApply(dummy,x,y,ierr)
319: implicit none
321: #include include/finclude/petsc.h
322: #include include/finclude/petscvec.h
324: Vec x,y
325: integer dummy,ierr
327: ! Common block to store data for user-provided preconditioner
328: common /myshellpc/ diag
329: Vec diag
331: call VecPointwiseMult(x,diag,y,ierr)
333: end