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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2562 - (show annotations)
Tue Jul 28 01:25:12 2009 UTC (10 years, 1 month ago) by artak
File MIME type: text/plain
File size: 7498 byte(s)
inplace bugs are fixed
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
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 /* TFQMR iterations */
16
17 #include "SystemMatrix.h"
18 #include "Paso.h"
19 #include "Solver.h"
20 #include "PasoUtil.h"
21
22 #ifdef _OPENMP
23 #include <omp.h>
24 #endif
25
26 #ifdef PASO_MPI
27 #include <mpi.h>
28 #endif
29
30 /*
31 *
32 * Purpose
33 * =======
34 *
35 * TFQMR solves the linear system A*x = b
36 *
37 * Convergence test: norm( b - A*x )< TOL.
38 * For other measures, see the above reference.
39 *
40 * Arguments
41 * =========
42 *
43 * r (input) DOUBLE PRECISION array, dimension N.
44 *
45 *
46 * x (input/output) DOUBLE PRECISION array, dimension N.
47 *
48 *
49 * ITER (input/output) INT
50 * On input, the maximum iterations to be performed.
51 * On output, actual number of iterations performed.
52 *
53 * INFO (output) INT
54 *
55 * = SOLVER_NO_ERROR: Successful exit. Iterated approximate solution returned.
56 * = SOLVEr_MAXITER_REACHED
57 * = SOLVER_INPUT_ERROR Illegal parameter:
58 * = SOLVEr_BREAKDOWN: If parameters rHO or OMEGA become smaller
59 * = SOLVER_MEMORY_ERROR : If parameters rHO or OMEGA become smaller
60 *
61 * ==============================================================
62 */
63
64 /* #define PASO_DYNAMIC_SCHEDULING_MVM */
65
66 #if defined PASO_DYNAMIC_SCHEDULING_MVM && defined __OPENMP
67 #define USE_DYNAMIC_SCHEDULING
68 #endif
69
70 err_t Paso_Solver_TFQMR(
71 Paso_SystemMatrix * A,
72 double * r,
73 double * x,
74 dim_t *iter,
75 double * tolerance,
76 Paso_Performance* pp) {
77
78 /* Local variables */
79
80 int m=1;
81 int j=0;
82
83 dim_t num_iter=0,maxit;
84 bool_t breakFlag=FALSE, maxIterFlag=FALSE, convergeFlag=FALSE;
85 err_t status = SOLVER_NO_ERROR;
86 dim_t n = Paso_SystemMatrix_getTotalNumRows(A);
87 double *u1=NULL, *u2=NULL, *y1=NULL, *y2=NULL, *d=NULL, *w=NULL, *v=NULL, *v_old=NULL, *temp_vector=NULL,*res=NULL;
88
89 double eta,theta,tau,rho,beta,alpha,sigma,rhon,c;
90
91 double norm_of_residual;
92
93 /* */
94 /*-----------------------------------------------------------------*/
95 /* */
96 /* Start of Calculation : */
97 /* --------------------- */
98 /* */
99 /* */
100 u1=TMPMEMALLOC(n,double);
101 u2=TMPMEMALLOC(n,double);
102 y1=TMPMEMALLOC(n,double);
103 y2=TMPMEMALLOC(n,double);
104 d=TMPMEMALLOC(n,double);
105 w=TMPMEMALLOC(n,double);
106 v=TMPMEMALLOC(n,double);
107 v_old=TMPMEMALLOC(n,double);
108 temp_vector=TMPMEMALLOC(n,double);
109 res=TMPMEMALLOC(n,double);
110
111 if (u1 ==NULL || u2== NULL || y1 == NULL || y2== NULL || d==NULL || w==NULL || v==NULL || v_old==NULL) {
112 status=SOLVER_MEMORY_ERROR;
113 }
114
115 maxit = *iter;
116
117 /* Test the input parameters. */
118 if (n < 0 || maxit<=0 ) {
119 status=SOLVER_INPUT_ERROR;
120 }
121
122 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER);
123 Paso_Solver_solvePreconditioner(A,res,r);
124 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER);
125
126 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
127
128 Paso_zeroes(n,u2);
129 Paso_zeroes(n,y2);
130
131 Paso_Copy(n,w,res);
132 Paso_Copy(n,y1,res);
133
134 Paso_zeroes(n,d);
135
136 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
137 Performance_startMonitor(pp,PERFORMANCE_MVM);
138 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(PASO_ONE, A, y1,PASO_ZERO,temp_vector);
139 Performance_stopMonitor(pp,PERFORMANCE_MVM);
140 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
141
142 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
143 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER);
144 Paso_Solver_solvePreconditioner(A,v,temp_vector);
145 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER);
146 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
147
148 Paso_Copy(n,u1,v);
149
150 theta = 0.0;
151 eta = 0.0;
152
153 tau = Paso_l2(n,res,A->mpi_info);
154
155 rho = tau * tau;
156
157 norm_of_residual=tau*sqrt ( m + 1 );
158
159 while (!(convergeFlag || maxIterFlag || breakFlag || (status !=SOLVER_NO_ERROR) ))
160 {
161
162
163 sigma=Paso_InnerProduct(n,res,v,A->mpi_info);
164
165 if (! (breakFlag = (ABS(sigma) == 0.))) {
166
167 alpha = rho / sigma;
168
169 for (j=0; j<=1; j=j+1)
170 {
171 /* Compute y2 and u2 only if you have to */
172 if ( j == 1 ){
173 Paso_LinearCombination(n,y2,1.,y1,-alpha,v);
174
175 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
176 Performance_startMonitor(pp,PERFORMANCE_MVM);
177 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(PASO_ONE, A, y2,PASO_ZERO,temp_vector);
178 Performance_stopMonitor(pp,PERFORMANCE_MVM);
179 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
180
181 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
182 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER);
183 Paso_Solver_solvePreconditioner(A,u2,temp_vector);
184 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER);
185 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
186 }
187 m = 2 * (num_iter+1) - 2 + (j+1);
188 if (j==0) {
189 Paso_Update(n,1.,w,-alpha,u1);
190 Paso_Update(n,( theta * theta * eta / alpha ),d,1.,y1);
191 }
192 if (j==1) {
193 Paso_Update(n,1.,w,-alpha,u2);
194 Paso_Update(n,( theta * theta * eta / alpha ),d,1.,y2);
195 }
196
197 theta =Paso_l2(n,w,A->mpi_info)/tau;
198 c = 1.0 / sqrt ( 1.0 + theta * theta );
199 tau = tau * theta * c;
200 eta = c * c * alpha;
201 Paso_Update(n,1.,x,eta,d);
202 }
203
204 breakFlag = (ABS(rho) == 0);
205
206 rhon = Paso_InnerProduct(n,res,w,A->mpi_info);
207 beta = rhon / rho;
208 rho = rhon;
209
210 Paso_LinearCombination(n,y1,1.,w,beta,y2);
211
212 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
213
214 Performance_startMonitor(pp,PERFORMANCE_MVM);
215 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(PASO_ONE, A, y1,PASO_ZERO,temp_vector);
216 Performance_stopMonitor(pp,PERFORMANCE_MVM);
217
218 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER);
219 Paso_Solver_solvePreconditioner(A,u1,temp_vector);
220 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER);
221
222 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
223
224 Paso_Copy(n,v_old,v);
225
226 Paso_Update(n,beta,v_old,1,u2);
227 Paso_LinearCombination(n,v,1.,u1,beta,v_old);
228 }
229 maxIterFlag = (num_iter > maxit);
230 norm_of_residual=tau*sqrt ( m + 1 );
231 convergeFlag=(norm_of_residual<(*tolerance));
232
233
234 if (maxIterFlag) {
235 status = SOLVER_MAXITER_REACHED;
236 } else if (breakFlag) {
237 status = SOLVER_BREAKDOWN;
238 }
239 ++(num_iter);
240 }
241 /* end of iteration */
242
243 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
244 TMPMEMFREE(u1);
245 TMPMEMFREE(u2);
246 TMPMEMFREE(y1);
247 TMPMEMFREE(y2);
248 TMPMEMFREE(d);
249 TMPMEMFREE(w);
250 TMPMEMFREE(v);
251 TMPMEMFREE(v_old);
252 TMPMEMFREE(temp_vector);
253 TMPMEMFREE(res);
254 *iter=num_iter;
255 *tolerance=norm_of_residual;
256
257 /* End of TFQMR */
258 return status;
259 }

  ViewVC Help
Powered by ViewVC 1.1.26