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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2484 - (hide annotations)
Mon Jun 22 04:22:19 2009 UTC (10 years, 1 month ago) by gross
File MIME type: text/plain
File size: 13552 byte(s)
numarray removed from docu; Locator revised.
1 ksteube 1312 /*******************************************************
2 ksteube 1811 *
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 dhawcroft 631
13 ksteube 1811
14 jgs 150 /**************************************************************/
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 gross 700 #include "Paso.h"
26     #include "SystemMatrix.h"
27 jgs 150 #include "Solver.h"
28 phornby 2078 #include "esysUtils/blocktimer.h"
29 jgs 150
30     /***********************************************************************************/
31    
32     /* free space */
33    
34     void Paso_Solver_free(Paso_SystemMatrix* A) {
35 phornby 1628 Paso_Preconditioner_free(A->solver);
36     A->solver=NULL;
37 jgs 150 }
38     /* call the iterative solver: */
39    
40 phornby 1628 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 artak 2381 dim_t i,totIter=0,cntIter,method;
52 phornby 1628 bool_t finalizeIteration;
53 jfenwick 1981 err_t errorCode=NO_ERROR;
54 phornby 1628 dim_t numSol = Paso_SystemMatrix_getTotalNumCols(A);
55     dim_t numEqua = Paso_SystemMatrix_getTotalNumRows(A);
56 ksteube 1707 double blocktimer_precond, blocktimer_start = blocktimer_time();
57 gross 2479
58 gross 415
59 gross 1639 Paso_resetError();
60 gross 2156 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 gross 2315 method=Paso_Options_getSolver(options->method,PASO_PASO,options->symmetric,A->mpi_info);
68 gross 1639 /* check matrix type */
69     if ((A->type & MATRIX_FORMAT_CSC) || (A->type & MATRIX_FORMAT_OFFSET1) || (A->type & MATRIX_FORMAT_SYM) ) {
70 gross 2156 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires CSR format with unsymmetric storage scheme and index offset 0.");
71 gross 1639 }
72     if (A->col_block_size != A->row_block_size) {
73 gross 2156 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires row and column block sizes to be equal.");
74 gross 1639 }
75     if (Paso_SystemMatrix_getGlobalNumCols(A) != Paso_SystemMatrix_getGlobalNumRows(A)) {
76 gross 2156 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires a square matrix.");
77 gross 1639 return;
78     }
79 gross 2479 time_iter=Paso_timer();
80 gross 1639 /* 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 gross 1798 Paso_Solver_solvePreconditioner(A,x,b);
85 gross 1639 errorCode=Paso_Solver_NewtonGMRES(F,x,options,pp);
86 gross 1798 if (errorCode!=NO_ERROR) {
87 gross 1639 Paso_setError(SYSTEM_ERROR,"Paso_Solver_NewtonGMRES: an error has occured.");
88     }
89     Paso_Function_LinearSystem_free(F);
90 gross 1798 return;
91 gross 1639 }
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 jgs 150
127 phornby 1628 /* if norm2_of_b==0 we are ready: x=0 */
128     if (norm2_of_b <=0.) {
129 gross 1798 #pragma omp parallel for private(i) schedule(static)
130 phornby 1628 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 gross 2156 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 phornby 1628 switch (method) {
137     case PASO_BICGSTAB:
138 gross 2156 printf("Paso_Solver: Iterative method is BiCGStab.\n");
139 phornby 1628 break;
140     case PASO_PCG:
141 gross 2156 printf("Paso_Solver: Iterative method is PCG.\n");
142 phornby 1628 break;
143 artak 1703 case PASO_TFQMR:
144 gross 2156 printf("Paso_Solver: Iterative method is TFQMR.\n");
145 artak 1703 break;
146 artak 1787 case PASO_MINRES:
147 gross 2156 printf("Paso_Solver: Iterative method is MINRES.\n");
148 artak 1787 break;
149 phornby 1628 case PASO_PRES20:
150 gross 2156 printf("Paso_Solver: Iterative method is PRES20.\n");
151 phornby 1628 break;
152     case PASO_GMRES:
153     if (options->restart>0) {
154 gross 2156 printf("Paso_Solver: Iterative method is GMRES(%d,%d)\n",options->truncation,options->restart);
155 phornby 1628 } else {
156 gross 2156 printf("Paso_Solver: Iterative method is GMRES(%d)\n",options->truncation);
157 gross 584 }
158 phornby 1628 break;
159     }
160     }
161     /* construct the preconditioner */
162 ksteube 1707 blocktimer_precond = blocktimer_time();
163 phornby 1628 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
164     Paso_Solver_setPreconditioner(A,options);
165     Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT);
166 ksteube 1707 blocktimer_increment("Paso_Solver_setPreconditioner()", blocktimer_precond);
167 gross 2479 options->set_up_time=Paso_timer()-time_iter;
168 phornby 1628 if (! Paso_noError()) return;
169 gross 584 /* get an initial guess by evaluating the preconditioner */
170 gross 1556 Paso_Solver_solvePreconditioner(A,x,b);
171 gross 584 /* start the iteration process :*/
172 ksteube 1312 r=TMPMEMALLOC(numEqua,double);
173     Paso_checkPtr(r);
174     if (Paso_noError()) {
175 artak 2280 totIter = 1;
176 ksteube 1312 finalizeIteration = FALSE;
177     last_norm2_of_residual=norm2_of_b;
178     last_norm_max_of_residual=norm_max_of_b;
179 artak 1787 /* Loop */
180 ksteube 1312 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 gross 1556 #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 ksteube 1312 #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 gross 584 }
204 ksteube 1312 /* 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 gross 2479 options->residual_norm=norm2_of_residual;
213 ksteube 1312
214 gross 2156 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 artak 2280 if (totIter>1 && norm2_of_residual>=last_norm2_of_residual && norm_max_of_residual>=last_norm_max_of_residual) {
216 phornby 1628 if (options->verbose) printf(" divergence!\n");
217 gross 2484 Paso_setError(DIVERGED, "Paso_Solver: No improvement during iteration. Iterative solver gives up.");
218 phornby 1628 } 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 artak 1819 errorCode = Paso_Solver_PCG(A, r, x, &cntIter, &tol, pp);
232 phornby 1628 break;
233 artak 1703 case PASO_TFQMR:
234 artak 1787 errorCode = Paso_Solver_TFQMR(A, r, x, &cntIter, &tol, pp);
235 artak 1703 break;
236 artak 1787 case PASO_MINRES:
237 artak 1862 errorCode = Paso_Solver_MINRES(A, r, x, &cntIter, &tol, pp);
238 artak 1787 break;
239 phornby 1628 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 gross 2484 Paso_setError(DIVERGED,"Paso_Solver: maximum number of iteration step reached.\nReturned solution does not fulfil stopping criterion.");
252 gross 2156 if (options->verbose) printf("Paso_Solver: Maximum number of iterations reached.!\n");
253 phornby 1628 } else if (errorCode == SOLVER_INPUT_ERROR ) {
254 gross 2156 Paso_setError(SYSTEM_ERROR,"Paso_Solver: illegal dimension in iterative solver.");
255     if (options->verbose) printf("Paso_Solver: Internal error!\n");
256 phornby 1628 } else if ( errorCode == SOLVER_BREAKDOWN ) {
257     if (cntIter <= 1) {
258 gross 2156 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 phornby 1628 } else {
261 gross 2156 if (options->verbose) printf("Paso_Solver: Breakdown at iter %d (residual = %e). Restarting ...\n", cntIter+totIter, tol);
262 phornby 1628 finalizeIteration = FALSE;
263 ksteube 1312 }
264 artak 2381 }
265 ksteube 1312 } else {
266     if (options->verbose) printf(". convergence! \n");
267 gross 2479 options->converged=TRUE;
268 ksteube 1312 }
269 jgs 150 }
270 ksteube 1312 } /* while */
271     }
272 gross 584 MEMFREE(r);
273 gross 2479 options->num_level=0;
274     options->num_inner_iter=0;
275     options->num_iter=totIter;
276 jgs 150 }
277     }
278 gross 584 }
279 gross 2479 options->time=Paso_timer()-time_iter;
280 phornby 1628 Performance_stopMonitor(pp,PERFORMANCE_ALL);
281     blocktimer_increment("Paso_Solver()", blocktimer_start);
282 gross 2479
283 jgs 150 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26