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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2484 - (show annotations)
Mon Jun 22 04:22:19 2009 UTC (9 years, 7 months ago) by gross
File MIME type: text/plain
File size: 13552 byte(s)
numarray removed from docu; Locator revised.
1 /*******************************************************
2 *
3 * Copyright (c) 2003-2008 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: gross@access.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;
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 time_iter=Paso_timer();
80 /* this for testing only */
81 if (method==PASO_NONLINEAR_GMRES) {
82 Paso_Function* F=NULL;
83 F=Paso_Function_LinearSystem_alloc(A,b,options);
84 Paso_Solver_solvePreconditioner(A,x,b);
85 errorCode=Paso_Solver_NewtonGMRES(F,x,options,pp);
86 if (errorCode!=NO_ERROR) {
87 Paso_setError(SYSTEM_ERROR,"Paso_Solver_NewtonGMRES: an error has occured.");
88 }
89 Paso_Function_LinearSystem_free(F);
90 return;
91 }
92 /* ========================= */
93 Performance_startMonitor(pp,PERFORMANCE_ALL);
94 if (Paso_noError()) {
95 /* get normalization */
96 scaling=Paso_SystemMatrix_borrowNormalization(A);
97 if (Paso_noError()) {
98 /* get the norm of the right hand side */
99 norm2_of_b=0.;
100 norm_max_of_b=0.;
101 #pragma omp parallel private(norm2_of_b_local,norm_max_of_b_local)
102 {
103 norm2_of_b_local=0.;
104 norm_max_of_b_local=0.;
105 #pragma omp for private(i) schedule(static)
106 for (i = 0; i < numEqua ; ++i) {
107 norm2_of_b_local += b[i] * b[i];
108 norm_max_of_b_local = MAX(ABS(scaling[i]*b[i]),norm_max_of_b_local);
109 }
110 #pragma omp critical
111 {
112 norm2_of_b += norm2_of_b_local;
113 norm_max_of_b = MAX(norm_max_of_b_local,norm_max_of_b);
114 }
115 }
116 /* TODO: use one call */
117 #ifdef PASO_MPI
118 {
119 loc_norm = norm2_of_b;
120 MPI_Allreduce(&loc_norm,&norm2_of_b, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
121 loc_norm = norm_max_of_b;
122 MPI_Allreduce(&loc_norm,&norm_max_of_b, 1, MPI_DOUBLE, MPI_MAX, A->mpi_info->comm);
123 }
124 #endif
125 norm2_of_b=sqrt(norm2_of_b);
126
127 /* if norm2_of_b==0 we are ready: x=0 */
128 if (norm2_of_b <=0.) {
129 #pragma omp parallel for private(i) schedule(static)
130 for (i = 0; i < numSol; i++) x[i]=0.;
131 if (options->verbose) printf("right hand side is identical zero.\n");
132 } else {
133 if (options->verbose) {
134 printf("Paso_Solver: l2/lmax-norm of right hand side is %e/%e.\n",norm2_of_b,norm_max_of_b);
135 printf("Paso_Solver: l2/lmax-stopping criterion is %e/%e.\n",norm2_of_b*tolerance,norm_max_of_b*tolerance);
136 switch (method) {
137 case PASO_BICGSTAB:
138 printf("Paso_Solver: Iterative method is BiCGStab.\n");
139 break;
140 case PASO_PCG:
141 printf("Paso_Solver: Iterative method is PCG.\n");
142 break;
143 case PASO_TFQMR:
144 printf("Paso_Solver: Iterative method is TFQMR.\n");
145 break;
146 case PASO_MINRES:
147 printf("Paso_Solver: Iterative method is MINRES.\n");
148 break;
149 case PASO_PRES20:
150 printf("Paso_Solver: Iterative method is PRES20.\n");
151 break;
152 case PASO_GMRES:
153 if (options->restart>0) {
154 printf("Paso_Solver: Iterative method is GMRES(%d,%d)\n",options->truncation,options->restart);
155 } else {
156 printf("Paso_Solver: Iterative method is GMRES(%d)\n",options->truncation);
157 }
158 break;
159 }
160 }
161 /* construct the preconditioner */
162 blocktimer_precond = blocktimer_time();
163 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
164 Paso_Solver_setPreconditioner(A,options);
165 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
166 blocktimer_increment("Paso_Solver_setPreconditioner()", blocktimer_precond);
167 options->set_up_time=Paso_timer()-time_iter;
168 if (! Paso_noError()) return;
169 /* get an initial guess by evaluating the preconditioner */
170 Paso_Solver_solvePreconditioner(A,x,b);
171 /* start the iteration process :*/
172 r=TMPMEMALLOC(numEqua,double);
173 Paso_checkPtr(r);
174 if (Paso_noError()) {
175 totIter = 1;
176 finalizeIteration = FALSE;
177 last_norm2_of_residual=norm2_of_b;
178 last_norm_max_of_residual=norm_max_of_b;
179 /* Loop */
180 while (! finalizeIteration) {
181 cntIter = options->iter_max - totIter;
182 finalizeIteration = TRUE;
183 /* Set initial residual. */
184 norm2_of_residual = 0;
185 norm_max_of_residual = 0;
186 #pragma omp parallel for private(i) schedule(static)
187 for (i = 0; i < numEqua; i++) r[i]=b[i];
188 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), A, x, DBLE(1), r);
189 #pragma omp parallel private(norm2_of_residual_local,norm_max_of_residual_local)
190 {
191 norm2_of_residual_local = 0;
192 norm_max_of_residual_local = 0;
193 #pragma omp for private(i) schedule(static)
194 for (i = 0; i < numEqua; i++) {
195 norm2_of_residual_local+= r[i] * r[i];
196 norm_max_of_residual_local=MAX(ABS(scaling[i]*r[i]),norm_max_of_residual_local);
197 }
198 #pragma omp critical
199 {
200 norm2_of_residual += norm2_of_residual_local;
201 norm_max_of_residual = MAX(norm_max_of_residual_local,norm_max_of_residual);
202 }
203 }
204 /* TODO: use one call */
205 #ifdef PASO_MPI
206 loc_norm = norm2_of_residual;
207 MPI_Allreduce(&loc_norm,&norm2_of_residual, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
208 loc_norm = norm_max_of_residual;
209 MPI_Allreduce(&loc_norm,&norm_max_of_residual, 1, MPI_DOUBLE, MPI_MAX, A->mpi_info->comm);
210 #endif
211 norm2_of_residual =sqrt(norm2_of_residual);
212 options->residual_norm=norm2_of_residual;
213
214 if (options->verbose) printf("Paso_Solver: Step %5d: l2/lmax-norm of residual is %e/%e",totIter,norm2_of_residual,norm_max_of_residual);
215 if (totIter>1 && norm2_of_residual>=last_norm2_of_residual && norm_max_of_residual>=last_norm_max_of_residual) {
216 if (options->verbose) printf(" divergence!\n");
217 Paso_setError(DIVERGED, "Paso_Solver: No improvement during iteration. Iterative solver gives up.");
218 } else {
219 /* */
220 if (norm2_of_residual>tolerance*norm2_of_b || norm_max_of_residual>tolerance*norm_max_of_b ) {
221 tol=tolerance*MIN(norm2_of_b,0.1*norm2_of_residual/norm_max_of_residual*norm_max_of_b);
222 if (options->verbose) printf(" (new tolerance = %e).\n",tol);
223 last_norm2_of_residual=norm2_of_residual;
224 last_norm_max_of_residual=norm_max_of_residual;
225 /* call the solver */
226 switch (method) {
227 case PASO_BICGSTAB:
228 errorCode = Paso_Solver_BiCGStab(A, r, x, &cntIter, &tol, pp);
229 break;
230 case PASO_PCG:
231 errorCode = Paso_Solver_PCG(A, r, x, &cntIter, &tol, pp);
232 break;
233 case PASO_TFQMR:
234 errorCode = Paso_Solver_TFQMR(A, r, x, &cntIter, &tol, pp);
235 break;
236 case PASO_MINRES:
237 errorCode = Paso_Solver_MINRES(A, r, x, &cntIter, &tol, pp);
238 break;
239 case PASO_PRES20:
240 errorCode = Paso_Solver_GMRES(A, r, x, &cntIter, &tol,5,20, pp);
241 break;
242 case PASO_GMRES:
243 errorCode = Paso_Solver_GMRES(A, r, x, &cntIter, &tol,options->truncation,options->restart, pp);
244 break;
245 }
246 totIter += cntIter;
247 /* error handling */
248 if (errorCode==NO_ERROR) {
249 finalizeIteration = FALSE;
250 } else if (errorCode==SOLVER_MAXITER_REACHED) {
251 Paso_setError(DIVERGED,"Paso_Solver: maximum number of iteration step reached.\nReturned solution does not fulfil stopping criterion.");
252 if (options->verbose) printf("Paso_Solver: Maximum number of iterations reached.!\n");
253 } else if (errorCode == SOLVER_INPUT_ERROR ) {
254 Paso_setError(SYSTEM_ERROR,"Paso_Solver: illegal dimension in iterative solver.");
255 if (options->verbose) printf("Paso_Solver: Internal error!\n");
256 } else if ( errorCode == SOLVER_BREAKDOWN ) {
257 if (cntIter <= 1) {
258 Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver: fatal break down in iterative solver.");
259 if (options->verbose) printf("Paso_Solver: Uncurable break down!\n");
260 } else {
261 if (options->verbose) printf("Paso_Solver: Breakdown at iter %d (residual = %e). Restarting ...\n", cntIter+totIter, tol);
262 finalizeIteration = FALSE;
263 }
264 }
265 } else {
266 if (options->verbose) printf(". convergence! \n");
267 options->converged=TRUE;
268 }
269 }
270 } /* while */
271 }
272 MEMFREE(r);
273 options->num_level=0;
274 options->num_inner_iter=0;
275 options->num_iter=totIter;
276 }
277 }
278 }
279 options->time=Paso_timer()-time_iter;
280 Performance_stopMonitor(pp,PERFORMANCE_ALL);
281 blocktimer_increment("Paso_Solver()", blocktimer_start);
282
283 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26