1
<center><a href="ex12.c">Actual source code: ex12.c</a></center><br>
6
<meta name="generator" content="c2html 0.9.1">
7
<meta name="date" content="2002-05-31T16:28:36+00:00">
10
<body bgcolor="#FFFFFF">
11
<pre width="80"><a name="line1"> 1: </a><font color="#B22222">/*$Id: ex12.c,v 1.24 2001/08/07 21:30:54 bsmith Exp $*/</font>
13
<a name="line3"> 3: </a><font color="#B22222">/* Program usage: <A href="http://www.mcs.anl.gov/mpi/www/www1/mpirun.html#mpirun">mpirun</A> -np <procs> ex12 [-help] [all PETSc options] */</font>
15
<a name="line5"> 5: </a>static char help[] = <font color="#666666">"Solves a linear system in parallel with <A href="../../../../docs/manualpages/SLES/SLES.html#SLES">SLES</A>.n</font>
16
<a name="line6"> 6: </a><font color="#666666">Input parameters include:n</font>
17
<a name="line7"> 7: </a><font color="#666666"> -m <mesh_x> : number of mesh points in x-directionn</font>
18
<a name="line8"> 8: </a><font color="#666666"> -n <mesh_n> : number of mesh points in y-directionnn"</font>;
20
<a name="line10"> 10: </a><font color="#B22222">/*T</font>
21
<a name="line11"> 11: </a><font color="#B22222"> Concepts: <A href="../../../../docs/manualpages/SLES/SLES.html#SLES">SLES</A>^solving a system of linear equations</font>
22
<a name="line12"> 12: </a><font color="#B22222"> Concepts: <A href="../../../../docs/manualpages/SLES/SLES.html#SLES">SLES</A>^Laplacian, 2d</font>
23
<a name="line13"> 13: </a><font color="#B22222"> Concepts: <A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A>^registering preconditioners</font>
24
<a name="line14"> 14: </a><font color="#B22222"> Processors: n</font>
25
<a name="line15"> 15: </a><font color="#B22222">T*/</font>
27
<a name="line17"> 17: </a><font color="#B22222">/*</font>
28
<a name="line18"> 18: </a><font color="#B22222"> Demonstrates registering a new preconditioner (<A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A>) type.</font>
30
<a name="line20"> 20: </a><font color="#B22222"> To register a <A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A> type whose code is linked into the executable,</font>
31
<a name="line21"> 21: </a><font color="#B22222"> use PCRegister(). To register a <A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A> type in a dynamic library use <A href="../../../../docs/manualpages/PC/PCRegisterDynamic.html#PCRegisterDynamic">PCRegisterDynamic</A>()</font>
33
<a name="line23"> 23: </a><font color="#B22222"> Also provide the prototype for your PCCreate_XXX() function. In </font>
34
<a name="line24"> 24: </a><font color="#B22222"> this example we use the PETSc implementation of the Jacobi method,</font>
35
<a name="line25"> 25: </a><font color="#B22222"> PCCreate_Jacobi() just as an example.</font>
37
<a name="line27"> 27: </a><font color="#B22222"> See the file src/sles/pc/impls/jacobi/jacobi.c for details on how to </font>
38
<a name="line28"> 28: </a><font color="#B22222"> write a new <A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A> component.</font>
40
<a name="line30"> 30: </a><font color="#B22222"> See the manual page <A href="../../../../docs/manualpages/PC/PCRegisterDynamic.html#PCRegisterDynamic">PCRegisterDynamic</A>() for details on how to register a method.</font>
41
<a name="line31"> 31: </a><font color="#B22222">*/</font>
43
<a name="line33"> 33: </a><font color="#B22222">/* </font>
44
<a name="line34"> 34: </a><font color="#B22222"> Include "petscsles.h" so that we can use <A href="../../../../docs/manualpages/SLES/SLES.html#SLES">SLES</A> solvers. Note that this file</font>
45
<a name="line35"> 35: </a><font color="#B22222"> automatically includes:</font>
46
<a name="line36"> 36: </a><font color="#B22222"> petsc.h - base PETSc routines petscvec.h - vectors</font>
47
<a name="line37"> 37: </a><font color="#B22222"> petscsys.h - system routines petscmat.h - matrices</font>
48
<a name="line38"> 38: </a><font color="#B22222"> petscis.h - index sets petscksp.h - Krylov subspace methods</font>
49
<a name="line39"> 39: </a><font color="#B22222"> petscviewer.h - viewers petscpc.h - preconditioners</font>
50
<a name="line40"> 40: </a><font color="#B22222">*/</font>
51
<a name="line41"> 41: </a> #include <A href="../../../../include/petscsles.h.html">petscsles.h</A>
53
<a name="line43"> 43: </a><strong><font color="#4169E1">EXTERN_C_BEGIN</font></strong>
54
<a name="line44"> 44: </a><strong><font color="#4169E1">extern int PCCreate_Jacobi(<A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A>)</font></strong>;
55
<a name="line45"> 45: </a><strong><font color="#4169E1"><a name="main"></a>EXTERN_C_END</font></strong>
57
<a name="line47"> 47: </a><strong><font color="#4169E1">int main(int argc,char **args)</font></strong>
58
<a name="line48"> 48: </a>{
59
<a name="line49"> 49: </a> <A href="../../../../docs/manualpages/Vec/Vec.html#Vec">Vec</A> x,b,u; <font color="#B22222">/* approx solution, RHS, exact solution */</font>
60
<a name="line50"> 50: </a> <A href="../../../../docs/manualpages/Mat/Mat.html#Mat">Mat</A> A; <font color="#B22222">/* linear system matrix */</font>
61
<a name="line51"> 51: </a> <A href="../../../../docs/manualpages/SLES/SLES.html#SLES">SLES</A> sles; <font color="#B22222">/* linear solver context */</font>
62
<a name="line52"> 52: </a> PetscReal norm; <font color="#B22222">/* norm of solution error */</font>
63
<a name="line53"> 53: </a> int i,j,I,J,Istart,Iend,ierr,m = 8,n = 7,its;
64
<a name="line54"> 54: </a> <A href="../../../../docs/manualpages/Sys/PetscScalar.html#PetscScalar">PetscScalar</A> v,one = 1.0,neg_one = -1.0;
65
<a name="line55"> 55: </a> <A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A> pc; <font color="#B22222">/* preconditioner context */</font>
67
<a name="line57"> 57: </a> <A href="../../../../docs/manualpages/Sys/PetscInitialize.html#PetscInitialize">PetscInitialize</A>(&argc,&args,(char *)0,help);
68
<a name="line58"> 58: </a> <A href="../../../../docs/manualpages/Sys/PetscOptionsGetInt.html#PetscOptionsGetInt">PetscOptionsGetInt</A>(PETSC_NULL,<font color="#666666">"-m"</font>,&m,PETSC_NULL);
69
<a name="line59"> 59: </a> <A href="../../../../docs/manualpages/Sys/PetscOptionsGetInt.html#PetscOptionsGetInt">PetscOptionsGetInt</A>(PETSC_NULL,<font color="#666666">"-n"</font>,&n,PETSC_NULL);
71
<a name="line61"> 61: </a> <font color="#B22222">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </font>
72
<a name="line62"> 62: </a><font color="#B22222"> Compute the matrix and right-hand-side vector that define</font>
73
<a name="line63"> 63: </a><font color="#B22222"> the linear system, Ax = b.</font>
74
<a name="line64"> 64: </a><font color="#B22222"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</font>
75
<a name="line65"> 65: </a> <font color="#B22222">/* </font>
76
<a name="line66"> 66: </a><font color="#B22222"> Create parallel matrix, specifying only its global dimensions.</font>
77
<a name="line67"> 67: </a><font color="#B22222"> When using <A href="../../../../docs/manualpages/Mat/MatCreate.html#MatCreate">MatCreate</A>(), the matrix format can be specified at</font>
78
<a name="line68"> 68: </a><font color="#B22222"> runtime. Also, the parallel partitioning of the matrix is</font>
79
<a name="line69"> 69: </a><font color="#B22222"> determined by PETSc at runtime.</font>
80
<a name="line70"> 70: </a><font color="#B22222"> */</font>
81
<a name="line71"> 71: </a> <A href="../../../../docs/manualpages/Mat/MatCreate.html#MatCreate">MatCreate</A>(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,&A);
82
<a name="line72"> 72: </a> <A href="../../../../docs/manualpages/Mat/MatSetFromOptions.html#MatSetFromOptions">MatSetFromOptions</A>(A);
84
<a name="line74"> 74: </a> <font color="#B22222">/* </font>
85
<a name="line75"> 75: </a><font color="#B22222"> Currently, all PETSc parallel matrix formats are partitioned by</font>
86
<a name="line76"> 76: </a><font color="#B22222"> contiguous chunks of rows across the processors. Determine which</font>
87
<a name="line77"> 77: </a><font color="#B22222"> rows of the matrix are locally owned. </font>
88
<a name="line78"> 78: </a><font color="#B22222"> */</font>
89
<a name="line79"> 79: </a> <A href="../../../../docs/manualpages/Mat/MatGetOwnershipRange.html#MatGetOwnershipRange">MatGetOwnershipRange</A>(A,&Istart,&Iend);
91
<a name="line81"> 81: </a> <font color="#B22222">/* </font>
92
<a name="line82"> 82: </a><font color="#B22222"> Set matrix elements for the 2-D, five-point stencil in parallel.</font>
93
<a name="line83"> 83: </a><font color="#B22222"> - Each processor needs to insert only elements that it owns</font>
94
<a name="line84"> 84: </a><font color="#B22222"> locally (but any non-local elements will be sent to the</font>
95
<a name="line85"> 85: </a><font color="#B22222"> appropriate processor during matrix assembly). </font>
96
<a name="line86"> 86: </a><font color="#B22222"> - Always specify global rows and columns of matrix entries.</font>
97
<a name="line87"> 87: </a><font color="#B22222"> */</font>
98
<a name="line88"> 88: </a> <font color="#4169E1">for</font> (I=Istart; I<Iend; I++) {
99
<a name="line89"> 89: </a> v = -1.0; i = I/n; j = I - i*n;
100
<a name="line90"> 90: </a> <font color="#4169E1">if</font> (i>0) {J = I - n; <A href="../../../../docs/manualpages/Mat/MatSetValues.html#MatSetValues">MatSetValues</A>(A,1,&I,1,&J,&v,INSERT_VALUES);}
101
<a name="line91"> 91: </a> <font color="#4169E1">if</font> (i<m-1) {J = I + n; <A href="../../../../docs/manualpages/Mat/MatSetValues.html#MatSetValues">MatSetValues</A>(A,1,&I,1,&J,&v,INSERT_VALUES);}
102
<a name="line92"> 92: </a> <font color="#4169E1">if</font> (j>0) {J = I - 1; <A href="../../../../docs/manualpages/Mat/MatSetValues.html#MatSetValues">MatSetValues</A>(A,1,&I,1,&J,&v,INSERT_VALUES);}
103
<a name="line93"> 93: </a> <font color="#4169E1">if</font> (j<n-1) {J = I + 1; <A href="../../../../docs/manualpages/Mat/MatSetValues.html#MatSetValues">MatSetValues</A>(A,1,&I,1,&J,&v,INSERT_VALUES);}
104
<a name="line94"> 94: </a> v = 4.0; <A href="../../../../docs/manualpages/Mat/MatSetValues.html#MatSetValues">MatSetValues</A>(A,1,&I,1,&I,&v,INSERT_VALUES);
105
<a name="line95"> 95: </a> }
107
<a name="line97"> 97: </a> <font color="#B22222">/* </font>
108
<a name="line98"> 98: </a><font color="#B22222"> Assemble matrix, using the 2-step process:</font>
109
<a name="line99"> 99: </a><font color="#B22222"> <A href="../../../../docs/manualpages/Mat/MatAssemblyBegin.html#MatAssemblyBegin">MatAssemblyBegin</A>(), <A href="../../../../docs/manualpages/Mat/MatAssemblyEnd.html#MatAssemblyEnd">MatAssemblyEnd</A>()</font>
110
<a name="line100">100: </a><font color="#B22222"> Computations can be done while messages are in transition</font>
111
<a name="line101">101: </a><font color="#B22222"> by placing code between these two statements.</font>
112
<a name="line102">102: </a><font color="#B22222"> */</font>
113
<a name="line103">103: </a> <A href="../../../../docs/manualpages/Mat/MatAssemblyBegin.html#MatAssemblyBegin">MatAssemblyBegin</A>(A,MAT_FINAL_ASSEMBLY);
114
<a name="line104">104: </a> <A href="../../../../docs/manualpages/Mat/MatAssemblyEnd.html#MatAssemblyEnd">MatAssemblyEnd</A>(A,MAT_FINAL_ASSEMBLY);
116
<a name="line106">106: </a> <font color="#B22222">/* </font>
117
<a name="line107">107: </a><font color="#B22222"> Create parallel vectors.</font>
118
<a name="line108">108: </a><font color="#B22222"> - When using <A href="../../../../docs/manualpages/Vec/VecCreate.html#VecCreate">VecCreate</A>(), <A href="../../../../docs/manualpages/Vec/VecSetSizes.html#VecSetSizes">VecSetSizes</A>() and <A href="../../../../docs/manualpages/Vec/VecSetFromOptions.html#VecSetFromOptions">VecSetFromOptions</A>(),</font>
119
<a name="line109">109: </a><font color="#B22222"> we specify only the vector's global</font>
120
<a name="line110">110: </a><font color="#B22222"> dimension; the parallel partitioning is determined at runtime. </font>
121
<a name="line111">111: </a><font color="#B22222"> - When solving a linear system, the vectors and matrices MUST</font>
122
<a name="line112">112: </a><font color="#B22222"> be partitioned accordingly. PETSc automatically generates</font>
123
<a name="line113">113: </a><font color="#B22222"> appropriately partitioned matrices and vectors when <A href="../../../../docs/manualpages/Mat/MatCreate.html#MatCreate">MatCreate</A>()</font>
124
<a name="line114">114: </a><font color="#B22222"> and <A href="../../../../docs/manualpages/Vec/VecCreate.html#VecCreate">VecCreate</A>() are used with the same communicator. </font>
125
<a name="line115">115: </a><font color="#B22222"> - Note: We form 1 vector from scratch and then duplicate as needed.</font>
126
<a name="line116">116: </a><font color="#B22222"> */</font>
127
<a name="line117">117: </a> <A href="../../../../docs/manualpages/Vec/VecCreate.html#VecCreate">VecCreate</A>(PETSC_COMM_WORLD,&u);
128
<a name="line118">118: </a> <A href="../../../../docs/manualpages/Vec/VecSetSizes.html#VecSetSizes">VecSetSizes</A>(u,PETSC_DECIDE,m*n);
129
<a name="line119">119: </a> <A href="../../../../docs/manualpages/Vec/VecSetFromOptions.html#VecSetFromOptions">VecSetFromOptions</A>(u);
130
<a name="line120">120: </a> <A href="../../../../docs/manualpages/Vec/VecDuplicate.html#VecDuplicate">VecDuplicate</A>(u,&b);
131
<a name="line121">121: </a> <A href="../../../../docs/manualpages/Vec/VecDuplicate.html#VecDuplicate">VecDuplicate</A>(b,&x);
133
<a name="line123">123: </a> <font color="#B22222">/* </font>
134
<a name="line124">124: </a><font color="#B22222"> Set exact solution; then compute right-hand-side vector.</font>
135
<a name="line125">125: </a><font color="#B22222"> Use an exact solution of a vector with all elements of 1.0; </font>
136
<a name="line126">126: </a><font color="#B22222"> */</font>
137
<a name="line127">127: </a> <A href="../../../../docs/manualpages/Vec/VecSet.html#VecSet">VecSet</A>(&one,u);
138
<a name="line128">128: </a> <A href="../../../../docs/manualpages/Mat/MatMult.html#MatMult">MatMult</A>(A,u,b);
140
<a name="line130">130: </a> <font color="#B22222">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </font>
141
<a name="line131">131: </a><font color="#B22222"> Create the linear solver and set various options</font>
142
<a name="line132">132: </a><font color="#B22222"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</font>
144
<a name="line134">134: </a> <font color="#B22222">/* </font>
145
<a name="line135">135: </a><font color="#B22222"> Create linear solver context</font>
146
<a name="line136">136: </a><font color="#B22222"> */</font>
147
<a name="line137">137: </a> <A href="../../../../docs/manualpages/SLES/SLESCreate.html#SLESCreate">SLESCreate</A>(PETSC_COMM_WORLD,&sles);
149
<a name="line139">139: </a> <font color="#B22222">/* </font>
150
<a name="line140">140: </a><font color="#B22222"> Set operators. Here the matrix that defines the linear system</font>
151
<a name="line141">141: </a><font color="#B22222"> also serves as the preconditioning matrix.</font>
152
<a name="line142">142: </a><font color="#B22222"> */</font>
153
<a name="line143">143: </a> <A href="../../../../docs/manualpages/SLES/SLESSetOperators.html#SLESSetOperators">SLESSetOperators</A>(sles,A,A,DIFFERENT_NONZERO_PATTERN);
155
<a name="line145">145: </a> <font color="#B22222">/*</font>
156
<a name="line146">146: </a><font color="#B22222"> First register a new <A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A> type with the command PCRegister()</font>
157
<a name="line147">147: </a><font color="#B22222"> */</font>
158
<a name="line148">148: </a> PCRegister(<font color="#666666">"ourjacobi"</font>,0,<font color="#666666">"PCCreate_Jacobi"</font>,PCCreate_Jacobi);
159
<a name="line149">149: </a>
160
<a name="line150">150: </a> <font color="#B22222">/* </font>
161
<a name="line151">151: </a><font color="#B22222"> Set the <A href="../../../../docs/manualpages/PC/PC.html#PC">PC</A> type to be the new method</font>
162
<a name="line152">152: </a><font color="#B22222"> */</font>
163
<a name="line153">153: </a> <A href="../../../../docs/manualpages/SLES/SLESGetPC.html#SLESGetPC">SLESGetPC</A>(sles,&pc);
164
<a name="line154">154: </a> <A href="../../../../docs/manualpages/PC/PCSetType.html#PCSetType">PCSetType</A>(pc,<font color="#666666">"ourjacobi"</font>);
166
<a name="line156">156: </a> <font color="#B22222">/* </font>
167
<a name="line157">157: </a><font color="#B22222"> Set runtime options, e.g.,</font>
168
<a name="line158">158: </a><font color="#B22222"> -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol></font>
169
<a name="line159">159: </a><font color="#B22222"> These options will override those specified above as long as</font>
170
<a name="line160">160: </a><font color="#B22222"> <A href="../../../../docs/manualpages/SLES/SLESSetFromOptions.html#SLESSetFromOptions">SLESSetFromOptions</A>() is called _after_ any other customization</font>
171
<a name="line161">161: </a><font color="#B22222"> routines.</font>
172
<a name="line162">162: </a><font color="#B22222"> */</font>
173
<a name="line163">163: </a> <A href="../../../../docs/manualpages/SLES/SLESSetFromOptions.html#SLESSetFromOptions">SLESSetFromOptions</A>(sles);
175
<a name="line165">165: </a> <font color="#B22222">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </font>
176
<a name="line166">166: </a><font color="#B22222"> Solve the linear system</font>
177
<a name="line167">167: </a><font color="#B22222"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</font>
179
<a name="line169">169: </a> <A href="../../../../docs/manualpages/SLES/SLESSolve.html#SLESSolve">SLESSolve</A>(sles,b,x,&its);
181
<a name="line171">171: </a> <font color="#B22222">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </font>
182
<a name="line172">172: </a><font color="#B22222"> Check solution and clean up</font>
183
<a name="line173">173: </a><font color="#B22222"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</font>
185
<a name="line175">175: </a> <font color="#B22222">/* </font>
186
<a name="line176">176: </a><font color="#B22222"> Check the error</font>
187
<a name="line177">177: </a><font color="#B22222"> */</font>
188
<a name="line178">178: </a> <A href="../../../../docs/manualpages/Vec/VecAXPY.html#VecAXPY">VecAXPY</A>(&neg_one,u,x);
189
<a name="line179">179: </a> <A href="../../../../docs/manualpages/Vec/VecNorm.html#VecNorm">VecNorm</A>(x,NORM_2,&norm);
191
<a name="line181">181: </a> <font color="#B22222">/* Scale the norm */</font>
192
<a name="line182">182: </a> <font color="#B22222">/* norm *= sqrt(1.0/((m+1)*(n+1))); */</font>
194
<a name="line184">184: </a> <font color="#B22222">/*</font>
195
<a name="line185">185: </a><font color="#B22222"> Print convergence information. <A href="../../../../docs/manualpages/Sys/PetscPrintf.html#PetscPrintf">PetscPrintf</A>() produces a single </font>
196
<a name="line186">186: </a><font color="#B22222"> print statement from all processes that share a communicator.</font>
197
<a name="line187">187: </a><font color="#B22222"> */</font>
198
<a name="line188">188: </a> <A href="../../../../docs/manualpages/Sys/PetscPrintf.html#PetscPrintf">PetscPrintf</A>(PETSC_COMM_WORLD,<font color="#666666">"Norm of error %A iterations %dn"</font>,norm,its);
200
<a name="line190">190: </a> <font color="#B22222">/* </font>
201
<a name="line191">191: </a><font color="#B22222"> Free work space. All PETSc objects should be destroyed when they</font>
202
<a name="line192">192: </a><font color="#B22222"> are no longer needed.</font>
203
<a name="line193">193: </a><font color="#B22222"> */</font>
204
<a name="line194">194: </a> <A href="../../../../docs/manualpages/SLES/SLESDestroy.html#SLESDestroy">SLESDestroy</A>(sles);
205
<a name="line195">195: </a> <A href="../../../../docs/manualpages/Vec/VecDestroy.html#VecDestroy">VecDestroy</A>(u); <A href="../../../../docs/manualpages/Vec/VecDestroy.html#VecDestroy">VecDestroy</A>(x);
206
<a name="line196">196: </a> <A href="../../../../docs/manualpages/Vec/VecDestroy.html#VecDestroy">VecDestroy</A>(b); <A href="../../../../docs/manualpages/Mat/MatDestroy.html#MatDestroy">MatDestroy</A>(A);
208
<a name="line198">198: </a> <font color="#B22222">/*</font>
209
<a name="line199">199: </a><font color="#B22222"> Always call <A href="../../../../docs/manualpages/Sys/PetscFinalize.html#PetscFinalize">PetscFinalize</A>() before exiting a program. This routine</font>
210
<a name="line200">200: </a><font color="#B22222"> - finalizes the PETSc libraries as well as MPI</font>
211
<a name="line201">201: </a><font color="#B22222"> - provides summary and diagnostic information if certain runtime</font>
212
<a name="line202">202: </a><font color="#B22222"> options are chosen (e.g., -log_summary). </font>
213
<a name="line203">203: </a><font color="#B22222"> */</font>
214
<a name="line204">204: </a> <A href="../../../../docs/manualpages/Sys/PetscFinalize.html#PetscFinalize">PetscFinalize</A>();
215
<a name="line205">205: </a> <font color="#4169E1">return</font> 0;
216
<a name="line206">206: </a>}