/[escript]/trunk/paso/src/Solver.c
ViewVC logotype

Contents of /trunk/paso/src/Solver.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2748 - (show annotations)
Tue Nov 17 07:32:59 2009 UTC (9 years, 11 months ago) by gross
File MIME type: text/plain
File size: 14001 byte(s)
Macro elements are implemented now. VTK writer for macro elements still needs testing.
1 /*******************************************************
2 *
3 * Copyright (c) 2003-2009 by University of Queensland
4 * Earth Systems Science Computational Center (ESSCC)
5 * http://www.uq.edu.au/esscc
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 *******************************************************/
12
13
14 /**************************************************************/
15
16 /* Paso: SystemMatrix: controls iterative linear system solvers */
17
18 /**************************************************************/
19
20 /* Copyrights by ACcESS Australia 2003/04 */
21 /* Author: Lutz Gross, l.gross@uq.edu.au */
22
23 /**************************************************************/
24
25 #include "Paso.h"
26 #include "SystemMatrix.h"
27 #include "Solver.h"
28 #include "esysUtils/blocktimer.h"
29
30 /***********************************************************************************/
31
32 /* free space */
33
34 void Paso_Solver_free(Paso_SystemMatrix* A) {
35 Paso_Preconditioner_free(A->solver);
36 A->solver=NULL;
37 }
38 /* call the iterative solver: */
39
40 void Paso_Solver(Paso_SystemMatrix* A,double* x,double* b,
41 Paso_Options* options,Paso_Performance* pp) {
42
43 double norm2_of_b,tol,tolerance,time_iter,net_time_start;
44 double *r=NULL,norm2_of_residual,last_norm2_of_residual,norm_max_of_b;
45 double norm2_of_b_local,norm_max_of_b_local,norm2_of_residual_local;
46 double norm_max_of_residual_local,norm_max_of_residual;
47 double last_norm_max_of_residual,*scaling;
48 #ifdef PASO_MPI
49 double loc_norm;
50 #endif
51 dim_t i,totIter=0,cntIter,method;
52 bool_t finalizeIteration;
53 err_t errorCode=NO_ERROR;
54 dim_t numSol = Paso_SystemMatrix_getTotalNumCols(A);
55 dim_t numEqua = Paso_SystemMatrix_getTotalNumRows(A);
56 double blocktimer_precond, blocktimer_start = blocktimer_time();
57
58
59 Paso_resetError();
60 tolerance=options->tolerance;
61 if (tolerance < 100.* EPSILON) {
62 Paso_setError(VALUE_ERROR,"Paso_Solver: Tolerance is too small.");
63 }
64 if (tolerance >1.) {
65 Paso_setError(VALUE_ERROR,"Paso_Solver: Tolerance mut be less than one.");
66 }
67 method=Paso_Options_getSolver(options->method,PASO_PASO,options->symmetric,A->mpi_info);
68 /* check matrix type */
69 if ((A->type & MATRIX_FORMAT_CSC) || (A->type & MATRIX_FORMAT_OFFSET1) || (A->type & MATRIX_FORMAT_SYM) ) {
70 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires CSR format with unsymmetric storage scheme and index offset 0.");
71 }
72 if (A->col_block_size != A->row_block_size) {
73 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires row and column block sizes to be equal.");
74 }
75 if (Paso_SystemMatrix_getGlobalNumCols(A) != Paso_SystemMatrix_getGlobalNumRows(A)) {
76 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires a square matrix.");
77 return;
78 }
79 /*if (A->block_size != 1 && options->preconditioner==PASO_AMG) {
80 Paso_setError(TYPE_ERROR,"Paso_Solver: AMG on systems not supported yet.");
81 }
82 */
83 time_iter=Paso_timer();
84 /* this for testing only */
85 if (method==PASO_NONLINEAR_GMRES) {
86 Paso_Function* F=NULL;
87 F=Paso_Function_LinearSystem_alloc(A,b,options);
88 Paso_Solver_solvePreconditioner(A,x,b);
89 errorCode=Paso_Solver_NewtonGMRES(F,x,options,pp);
90 if (errorCode!=NO_ERROR) {
91 Paso_setError(SYSTEM_ERROR,"Paso_Solver_NewtonGMRES: an error has occured.");
92 }
93 Paso_Function_LinearSystem_free(F);
94 return;
95 }
96 /* ========================= */
97 Performance_startMonitor(pp,PERFORMANCE_ALL);
98 if (Paso_noError()) {
99 /* get normalization */
100 scaling=Paso_SystemMatrix_borrowNormalization(A);
101 if (Paso_noError()) {
102 /* get the norm of the right hand side */
103 norm2_of_b=0.;
104 norm_max_of_b=0.;
105 #pragma omp parallel private(norm2_of_b_local,norm_max_of_b_local)
106 {
107 norm2_of_b_local=0.;
108 norm_max_of_b_local=0.;
109 #pragma omp for private(i) schedule(static)
110 for (i = 0; i < numEqua ; ++i) {
111 norm2_of_b_local += b[i] * b[i];
112 norm_max_of_b_local = MAX(ABS(scaling[i]*b[i]),norm_max_of_b_local);
113 }
114 #pragma omp critical
115 {
116 norm2_of_b += norm2_of_b_local;
117 norm_max_of_b = MAX(norm_max_of_b_local,norm_max_of_b);
118 }
119 }
120 /* TODO: use one call */
121 #ifdef PASO_MPI
122 {
123 loc_norm = norm2_of_b;
124 MPI_Allreduce(&loc_norm,&norm2_of_b, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
125 loc_norm = norm_max_of_b;
126 MPI_Allreduce(&loc_norm,&norm_max_of_b, 1, MPI_DOUBLE, MPI_MAX, A->mpi_info->comm);
127 }
128 #endif
129 norm2_of_b=sqrt(norm2_of_b);
130 /* if norm2_of_b==0 we are ready: x=0 */
131 if ( IS_NAN(norm2_of_b) || IS_NAN(norm_max_of_b) ) {
132 Paso_setError(VALUE_ERROR,"Paso_Solver: Matrix or right hand side contains undefined values.");
133 } else if (norm2_of_b <=0.) {
134 #pragma omp parallel for private(i) schedule(static)
135 for (i = 0; i < numSol; i++) x[i]=0.;
136 if (options->verbose) printf("right hand side is identical zero.\n");
137 } else {
138 if (options->verbose) {
139 printf("Paso_Solver: l2/lmax-norm of right hand side is %e/%e.\n",norm2_of_b,norm_max_of_b);
140 printf("Paso_Solver: l2/lmax-stopping criterion is %e/%e.\n",norm2_of_b*tolerance,norm_max_of_b*tolerance);
141 switch (method) {
142 case PASO_BICGSTAB:
143 printf("Paso_Solver: Iterative method is BiCGStab.\n");
144 break;
145 case PASO_PCG:
146 printf("Paso_Solver: Iterative method is PCG.\n");
147 break;
148 case PASO_TFQMR:
149 printf("Paso_Solver: Iterative method is TFQMR.\n");
150 break;
151 case PASO_MINRES:
152 printf("Paso_Solver: Iterative method is MINRES.\n");
153 break;
154 case PASO_PRES20:
155 printf("Paso_Solver: Iterative method is PRES20.\n");
156 break;
157 case PASO_GMRES:
158 if (options->restart>0) {
159 printf("Paso_Solver: Iterative method is GMRES(%d,%d)\n",options->truncation,options->restart);
160 } else {
161 printf("Paso_Solver: Iterative method is GMRES(%d)\n",options->truncation);
162 }
163 break;
164 }
165 }
166 /* construct the preconditioner */
167 blocktimer_precond = blocktimer_time();
168 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
169 Paso_Solver_setPreconditioner(A,options);
170 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
171 blocktimer_increment("Paso_Solver_setPreconditioner()", blocktimer_precond);
172 options->set_up_time=Paso_timer()-time_iter;
173 if (! Paso_noError()) return;
174 /* get an initial guess by evaluating the preconditioner */
175 Paso_Solver_solvePreconditioner(A,x,b);
176 /* start the iteration process :*/
177 r=TMPMEMALLOC(numEqua,double);
178 Paso_checkPtr(r);
179 if (Paso_noError()) {
180 totIter = 1;
181 finalizeIteration = FALSE;
182 last_norm2_of_residual=norm2_of_b;
183 last_norm_max_of_residual=norm_max_of_b;
184 net_time_start=Paso_timer();
185 /* Loop */
186 while (! finalizeIteration) {
187 cntIter = options->iter_max - totIter;
188 finalizeIteration = TRUE;
189 /* Set initial residual. */
190 norm2_of_residual = 0;
191 norm_max_of_residual = 0;
192 #pragma omp parallel for private(i) schedule(static)
193 for (i = 0; i < numEqua; i++) r[i]=b[i];
194 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), A, x, DBLE(1), r);
195 #pragma omp parallel private(norm2_of_residual_local,norm_max_of_residual_local)
196 {
197 norm2_of_residual_local = 0;
198 norm_max_of_residual_local = 0;
199 #pragma omp for private(i) schedule(static)
200 for (i = 0; i < numEqua; i++) {
201 norm2_of_residual_local+= r[i] * r[i];
202 norm_max_of_residual_local=MAX(ABS(scaling[i]*r[i]),norm_max_of_residual_local);
203 }
204 #pragma omp critical
205 {
206 norm2_of_residual += norm2_of_residual_local;
207 norm_max_of_residual = MAX(norm_max_of_residual_local,norm_max_of_residual);
208 }
209 }
210 /* TODO: use one call */
211 #ifdef PASO_MPI
212 loc_norm = norm2_of_residual;
213 MPI_Allreduce(&loc_norm,&norm2_of_residual, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
214 loc_norm = norm_max_of_residual;
215 MPI_Allreduce(&loc_norm,&norm_max_of_residual, 1, MPI_DOUBLE, MPI_MAX, A->mpi_info->comm);
216 #endif
217 norm2_of_residual =sqrt(norm2_of_residual);
218 options->residual_norm=norm2_of_residual;
219
220 if (options->verbose) printf("Paso_Solver: Step %5d: l2/lmax-norm of residual is %e/%e",totIter,norm2_of_residual,norm_max_of_residual);
221 if (totIter>1 && norm2_of_residual>=last_norm2_of_residual && norm_max_of_residual>=last_norm_max_of_residual) {
222 if (options->verbose) printf(" divergence!\n");
223 Paso_setError(DIVERGED, "Paso_Solver: No improvement during iteration. Iterative solver gives up.");
224 } else {
225 /* */
226 if (norm2_of_residual>tolerance*norm2_of_b || norm_max_of_residual>tolerance*norm_max_of_b ) {
227 tol=tolerance*MIN(norm2_of_b,0.1*norm2_of_residual/norm_max_of_residual*norm_max_of_b);
228 if (options->verbose) printf(" (new tolerance = %e).\n",tol);
229 last_norm2_of_residual=norm2_of_residual;
230 last_norm_max_of_residual=norm_max_of_residual;
231 /* call the solver */
232 switch (method) {
233 case PASO_BICGSTAB:
234 errorCode = Paso_Solver_BiCGStab(A, r, x, &cntIter, &tol, pp);
235 break;
236 case PASO_PCG:
237 errorCode = Paso_Solver_PCG(A, r, x, &cntIter, &tol, pp);
238 break;
239 case PASO_TFQMR:
240 errorCode = Paso_Solver_TFQMR(A, r, x, &cntIter, &tol, pp);
241 break;
242 case PASO_MINRES:
243 errorCode = Paso_Solver_MINRES(A, r, x, &cntIter, &tol, pp);
244 break;
245 case PASO_PRES20:
246 errorCode = Paso_Solver_GMRES(A, r, x, &cntIter, &tol,5,20, pp);
247 break;
248 case PASO_GMRES:
249 errorCode = Paso_Solver_GMRES(A, r, x, &cntIter, &tol,options->truncation,options->restart, pp);
250 break;
251 }
252 totIter += cntIter;
253 /* error handling */
254 if (errorCode==NO_ERROR) {
255 finalizeIteration = FALSE;
256 } else if (errorCode==SOLVER_MAXITER_REACHED) {
257 Paso_setError(DIVERGED,"Paso_Solver: maximum number of iteration step reached.\nReturned solution does not fulfil stopping criterion.");
258 if (options->verbose) printf("Paso_Solver: Maximum number of iterations reached.!\n");
259 } else if (errorCode == SOLVER_INPUT_ERROR ) {
260 Paso_setError(SYSTEM_ERROR,"Paso_Solver: illegal dimension in iterative solver.");
261 if (options->verbose) printf("Paso_Solver: Internal error!\n");
262 } else if ( errorCode == SOLVER_BREAKDOWN ) {
263 if (cntIter <= 1) {
264 Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver: fatal break down in iterative solver.");
265 if (options->verbose) printf("Paso_Solver: Uncurable break down!\n");
266 } else {
267 if (options->verbose) printf("Paso_Solver: Breakdown at iter %d (residual = %e). Restarting ...\n", cntIter+totIter, tol);
268 finalizeIteration = FALSE;
269 }
270 }
271 } else {
272 if (options->verbose) printf(". convergence! \n");
273 options->converged=TRUE;
274 }
275 }
276 } /* while */
277 options->net_time=Paso_timer()-net_time_start;
278 }
279 MEMFREE(r);
280 options->num_level=0;
281 options->num_inner_iter=0;
282 options->num_iter=totIter;
283 }
284 }
285 }
286 options->time=Paso_timer()-time_iter;
287 Performance_stopMonitor(pp,PERFORMANCE_ALL);
288 blocktimer_increment("Paso_Solver()", blocktimer_start);
289
290 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26