/[escript]/branches/windows_from_1456_trunk_1490_merged_in/paso/src/Solver.c
ViewVC logotype

Contents of /branches/windows_from_1456_trunk_1490_merged_in/paso/src/Solver.c

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26