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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26