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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 929 - (show annotations)
Wed Jan 17 07:41:13 2007 UTC (13 years, 2 months ago) by gross
File MIME type: text/plain
File size: 8441 byte(s)
reverse orientation added but does not work for 2D yet.
1 /* $Id$ */
2
3 /*
4 ********************************************************************************
5 * Copyright 2006 by ACcESS MNRF *
6 * *
7 * http://www.access.edu.au *
8 * Primary Business: Queensland, Australia *
9 * Licensed under the Open Software License version 3.0 *
10 * http://www.opensource.org/licenses/osl-3.0.php *
11 ********************************************************************************
12 */
13
14 /*
15 Crude modifications and translations for Paso by Matt Davies and Lutz Gross
16 */
17
18 #include "Paso.h"
19 #include "SystemMatrix.h"
20 #include "Solver.h"
21 #ifdef _OPENMP
22 #include <omp.h>
23 #endif
24
25 /* -- Iterative template routine --
26 * Univ. of Tennessee and Oak Ridge National Laboratory
27 * October 1, 1993
28 * Details of this algorithm are described in "Templates for the
29 * Solution of Linear Systems: Building Blocks for Iterative
30 * Methods", Barrett, Berry, Chan, Demmel, Donato, Dongarra,
31 * Eijkhout, Pozo, Romine, and van der Vorst, SIAM Publications,
32 * 1993. (ftp netlib2.cs.utk.edu; cd linalg; get templates.ps).
33 *
34 * Purpose
35 * =======
36 *
37 * BICGSTAB solves the linear system A*x = b using the
38 * BiConjugate Gradient Stabilized iterative method with
39 * preconditioning.
40 *
41 * Convergence test: norm( b - A*x )< TOL.
42 * For other measures, see the above reference.
43 *
44 * Arguments
45 * =========
46 *
47 * A (input)
48 *
49 * R (input) DOUBLE PRECISION array, dimension N.
50 * On entry, residual of inital guess X
51 *
52 * X (input/output) DOUBLE PRECISION array, dimension N.
53 * On input, the initial guess.
54 *
55 * ITER (input/output) INT
56 * On input, the maximum iterations to be performed.
57 * On output, actual number of iterations performed.
58 *
59 * RESID (input/output) DOUBLE PRECISION
60 * On input, the allowable convergence measure for
61 * norm( b - A*x )
62 * On output, the final value of this measure.
63 *
64 * return value
65 *
66 * = SOLVER_NO_ERROR: Successful exit. Iterated approximate solution returned.
67 * = SOLVER_MAXITER_REACHED
68 * = SOLVER_INPUT_ERROR Illegal parameter:
69 * = SOLVER_BREAKDOWN: If parameters RHO or OMEGA become smaller
70 * = SOLVER_MEMORY_ERROR : If parameters RHO or OMEGA become smaller
71 *
72 * ==============================================================
73 */
74
75 err_t Paso_Solver_BiCGStab(
76 Paso_SystemMatrix * A,
77 double * r,
78 double * x,
79 dim_t *iter,
80 double * tolerance,
81 Paso_Performance* pp) {
82
83
84 /* Local variables */
85 double *rtld=NULL,*p=NULL,*v=NULL,*t=NULL,*phat=NULL,*shat=NULL,*s=NULL;
86 double beta,norm_of_residual,sum_1,sum_2,sum_3,sum_4,norm_of_residual_global;
87 double alpha, omega, omegaNumtr, omegaDenumtr, rho, tol, rho1;
88 dim_t num_iter=0,maxit,num_iter_global;
89 dim_t i0;
90 bool_t breakFlag=FALSE, maxIterFlag=FALSE, convergeFlag=FALSE;
91 dim_t status = SOLVER_NO_ERROR;
92
93 /* adapt original routine parameters */
94 dim_t n = A->num_cols * A-> col_block_size;;
95 double * resid = tolerance;
96
97 /* Executable Statements */
98
99 /* allocate memory: */
100 rtld=TMPMEMALLOC(n,double);
101 p=TMPMEMALLOC(n,double);
102 v=TMPMEMALLOC(n,double);
103 t=TMPMEMALLOC(n,double);
104 phat=TMPMEMALLOC(n,double);
105 shat=TMPMEMALLOC(n,double);
106 s=TMPMEMALLOC(n,double);
107
108 /* Test the input parameters. */
109
110 if (n < 0) {
111 status = SOLVER_INPUT_ERROR;
112 } else if (rtld==NULL || p==NULL || v==NULL || t==NULL || phat==NULL || shat==NULL || s==NULL) {
113 status = SOLVER_MEMORY_ERROR;
114 } else {
115
116 /* now bicgstab starts : */
117 maxit = *iter;
118 tol = *resid;
119
120 #pragma omp parallel firstprivate(maxit,tol) \
121 private(rho,omega,num_iter,norm_of_residual,beta,alpha,rho1, convergeFlag,maxIterFlag,breakFlag)
122 {
123 num_iter =0;
124 convergeFlag=FALSE;
125 maxIterFlag=FALSE;
126 breakFlag=FALSE;
127
128 /* initialize arrays */
129
130 #pragma omp for private(i0) schedule(static)
131 for (i0 = 0; i0 < n; i0++) {
132 rtld[i0]=0;
133 p[i0]=0;
134 v[i0]=0;
135 t[i0]=0;
136 phat[i0]=0;
137 shat[i0]=0;
138 }
139 #pragma omp for private(i0) schedule(static)
140 for (i0 = 0; i0 < n; i0++) rtld[i0] = r[i0];
141
142 /* Perform BiConjugate Gradient Stabilized iteration. */
143
144 L10:
145 ++(num_iter);
146 #pragma omp barrier
147 #pragma omp master
148 {
149 sum_1 = 0;
150 sum_2 = 0;
151 sum_3 = 0;
152 sum_4 = 0;
153 omegaNumtr = 0.0;
154 omegaDenumtr = 0.0;
155 }
156 #pragma omp barrier
157 #pragma ivdep
158 #pragma omp for private(i0) reduction(+:sum_1) schedule(static)
159 for (i0 = 0; i0 < n; i0++) sum_1 += rtld[i0] * r[i0];
160 rho = sum_1;
161
162 if (! (breakFlag = (ABS(rho) <= TOLERANCE_FOR_SCALARS))) {
163 /* Compute vector P. */
164
165 if (num_iter > 1) {
166 beta = rho / rho1 * (alpha / omega);
167 #pragma ivdep
168 #pragma omp for private(i0) schedule(static)
169 for (i0 = 0; i0 < n; i0++) p[i0] = r[i0] + beta * (p[i0] - omega * v[i0]);
170 } else {
171 #pragma ivdep
172 #pragma omp for private(i0) schedule(static)
173 for (i0 = 0; i0 < n; i0++) p[i0] = r[i0];
174 }
175
176 /* Compute direction adjusting vector PHAT and scalar ALPHA. */
177
178 Paso_Solver_solvePreconditioner(A,&phat[0], &p[0]);
179 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(ONE, A, &phat[0],ZERO, &v[0]);
180
181 // #pragma ivdep
182 #pragma omp for private(i0) reduction(+:sum_2) schedule(static)
183 for (i0 = 0; i0 < n; i0++) sum_2 += rtld[i0] * v[i0];
184 if (! (breakFlag = (ABS(sum_2) <= TOLERANCE_FOR_SCALARS))) {
185 alpha = rho / sum_2;
186
187 // #pragma ivdep
188 #pragma omp for private(i0) reduction(+:sum_3) schedule(static)
189 for (i0 = 0; i0 < n; i0++) {
190 r[i0] -= alpha * v[i0];
191 s[i0] = r[i0];
192 sum_3 += s[i0] * s[i0];
193 }
194 norm_of_residual = sqrt(sum_3);
195
196 /* Early check for tolerance. */
197 if ( (convergeFlag = (norm_of_residual <= tol)) ) {
198 // #pragma ivdep
199 #pragma omp for private(i0) schedule(static)
200 for (i0 = 0; i0 < n; i0++) x[i0] += alpha * phat[i0];
201 maxIterFlag = FALSE;
202 breakFlag = FALSE;
203 } else {
204 /* Compute stabilizer vector SHAT and scalar OMEGA. */
205 Paso_Solver_solvePreconditioner(A,&shat[0], &s[0]);
206 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(ONE, A, &shat[0],ZERO,&t[0]);
207
208 // #pragma ivdep
209 #pragma omp for private(i0) reduction(+:omegaNumtr,omegaDenumtr) schedule(static)
210 for (i0 = 0; i0 < n; i0++) {
211 omegaNumtr +=t[i0] * s[i0];
212 omegaDenumtr += t[i0] * t[i0];
213 }
214 if (! (breakFlag = (ABS(omegaDenumtr) <= TOLERANCE_FOR_SCALARS))) {
215 omega = omegaNumtr / omegaDenumtr;
216
217 // #pragma ivdep
218 #pragma omp for private(i0) reduction(+:sum_4) schedule(static)
219 for (i0 = 0; i0 < n; i0++) {
220 x[i0] += alpha * phat[i0] + omega * shat[i0];
221 r[i0] -= omega * t[i0];
222 sum_4 += r[i0] * r[i0];
223 }
224 norm_of_residual = sqrt(sum_4);
225 convergeFlag = norm_of_residual <= tol;
226 maxIterFlag = num_iter == maxit;
227 breakFlag = (ABS(omega) <= TOLERANCE_FOR_SCALARS);
228 }
229 }
230 }
231 if (!(convergeFlag || maxIterFlag || breakFlag)) {
232 rho1 = rho;
233 goto L10;
234 }
235 }
236 /* end of iteration */
237 #pragma omp master
238 {
239 num_iter_global=num_iter;
240 norm_of_residual_global=norm_of_residual;
241 if (maxIterFlag) {
242 status = SOLVER_MAXITER_REACHED;
243 } else if (breakFlag) {
244 status = SOLVER_BREAKDOWN;
245 }
246 }
247 } /* end of parallel region */
248 }
249 TMPMEMFREE(rtld);
250 TMPMEMFREE(p);
251 TMPMEMFREE(v);
252 TMPMEMFREE(t);
253 TMPMEMFREE(phat);
254 TMPMEMFREE(shat);
255 TMPMEMFREE(s);
256 *iter=num_iter_global;
257 *resid=norm_of_residual_global;
258
259 /* End of BICGSTAB */
260 return status;
261 }
262 /*
263 * $Log$
264 * Revision 1.2 2005/09/15 03:44:40 jgs
265 * Merge of development branch dev-02 back to main trunk on 2005-09-15
266 *
267 * Revision 1.1.2.1 2005/09/05 06:29:49 gross
268 * These files have been extracted from finley to define a stand alone libray for iterative
269 * linear solvers on the ALTIX. main entry through Paso_solve. this version compiles but
270 * has not been tested yet.
271 *
272 *
273 */

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26