/[escript]/branches/arrexp_2137_win/paso/src/PCG.c
ViewVC logotype

Contents of /branches/arrexp_2137_win/paso/src/PCG.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1556 - (show annotations)
Mon May 12 00:54:58 2008 UTC (11 years, 1 month ago) by gross
Original Path: trunk/paso/src/PCG.c
File MIME type: text/plain
File size: 8022 byte(s)
Modification to allow mixed mode execution. 
In order to keep the code portable accross platform all MPI calls within
parallel regions have been moved. 


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 /* PCG iterations */
17
18 #include "SystemMatrix.h"
19 #include "Paso.h"
20 #include "Solver.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 * PCG solves the linear system A*x = b using the
36 * preconditioned conjugate gradient method plus a smoother
37 * A has to be symmetric.
38 *
39 * Convergence test: norm( b - A*x )< TOL.
40 * For other measures, see the above reference.
41 *
42 * Arguments
43 * =========
44 *
45 * r (input) DOUBLE PRECISION array, dimension N.
46 * On entry, residual of inital guess x
47 *
48 * x (input/output) DOUBLE PRECISION array, dimension N.
49 * On input, the initial guess.
50 *
51 * ITER (input/output) INT
52 * On input, the maximum iterations to be performed.
53 * On output, actual number of iterations performed.
54 *
55 * INFO (output) INT
56 *
57 * = SOLVER_NO_ERROR: Successful exit. Iterated approximate solution returned.
58 * = SOLVEr_MAXITER_REACHED
59 * = SOLVER_INPUT_ERROR Illegal parameter:
60 * = SOLVEr_BREAKDOWN: If parameters rHO or OMEGA become smaller
61 * = SOLVER_MEMORY_ERROR : If parameters rHO or OMEGA become smaller
62 *
63 * ==============================================================
64 */
65
66 err_t Paso_Solver_PCG(
67 Paso_SystemMatrix * A,
68 double * r,
69 double * x,
70 dim_t *iter,
71 double * tolerance,
72 Paso_Performance* pp) {
73
74
75 /* Local variables */
76 dim_t num_iter=0,maxit,num_iter_global;
77 dim_t i0;
78 bool_t breakFlag=FALSE, maxIterFlag=FALSE, convergeFlag=FALSE;
79 err_t status = SOLVER_NO_ERROR;
80 dim_t n = Paso_SystemMatrix_getTotalNumRows(A);
81 double *resid = tolerance, *rs=NULL, *p=NULL, *v=NULL, *x2=NULL ;
82 double tau_old,tau,beta,delta,gamma_1,gamma_2,alpha,sum_1,sum_2,sum_3,sum_4,sum_5,tol;
83 double norm_of_residual,norm_of_residual_global, loc_sum[2], sum[2];
84 register double r_tmp,d,rs_tmp,x2_tmp,x_tmp;
85
86 /* */
87 /*-----------------------------------------------------------------*/
88 /* */
89 /* Start of Calculation : */
90 /* --------------------- */
91 /* */
92 /* */
93 rs=TMPMEMALLOC(n,double);
94 p=TMPMEMALLOC(n,double);
95 v=TMPMEMALLOC(n,double);
96 x2=TMPMEMALLOC(n,double);
97
98 /* Test the input parameters. */
99
100 if (n < 0) {
101 status = SOLVER_INPUT_ERROR;
102 } else if (rs==NULL || p==NULL || v==NULL || x2==NULL) {
103 status = SOLVER_MEMORY_ERROR;
104 } else {
105 maxit = *iter;
106 tol = *resid;
107 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
108 /* initialize data */
109 #pragma omp parallel
110 {
111 #pragma omp for private(i0) schedule(static)
112 for (i0=0;i0<n;i0++) {
113 rs[i0]=r[i0];
114 x2[i0]=x[i0];
115 p[i0]=0;
116 v[i0]=0;
117 }
118 }
119 num_iter=0;
120 /* start of iteration */
121 while (!(convergeFlag || maxIterFlag || breakFlag)) {
122 ++(num_iter);
123 sum_1 = 0;
124 sum_2 = 0;
125 sum_3 = 0;
126 sum_4 = 0;
127 sum_5 = 0;
128 /* v=prec(r) */
129 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
130 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER);
131 Paso_Solver_solvePreconditioner(A,v,r);
132 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER);
133 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
134 /* tau=v*r */
135 #pragma omp parallel for private(i0) reduction(+:sum_1) schedule(static)
136 for (i0=0;i0<n;i0++) sum_1+=v[i0]*r[i0]; /* Limit to local values of v[] and r[] */
137 #ifdef PASO_MPI
138 /* In case we have many MPI processes, each of which may have several OMP threads:
139 OMP master participates in an MPI reduction to get global sum_1 */
140 loc_sum[0] = sum_1;
141 MPI_Allreduce(loc_sum, &sum_1, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
142 #endif
143 tau_old=tau;
144 tau=sum_1;
145 /* p=v+beta*p */
146 if (num_iter==1) {
147 #pragma omp parallel for private(i0) schedule(static)
148 for (i0=0;i0<n;i0++) p[i0]=v[i0];
149 } else {
150 beta=tau/tau_old;
151 #pragma omp parallel for private(i0) schedule(static)
152 for (i0=0;i0<n;i0++) p[i0]=v[i0]+beta*p[i0];
153 }
154 /* v=A*p */
155 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
156 Performance_startMonitor(pp,PERFORMANCE_MVM);
157 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(ONE, A, p,ZERO,v);
158 Performance_stopMonitor(pp,PERFORMANCE_MVM);
159 Performance_startMonitor(pp,PERFORMANCE_SOLVER);
160 /* delta=p*v */
161 #pragma omp parallel for private(i0) reduction(+:sum_2) schedule(static)
162 for (i0=0;i0<n;i0++) sum_2+=v[i0]*p[i0];
163 #ifdef PASO_MPI
164 loc_sum[0] = sum_2;
165 MPI_Allreduce(loc_sum, &sum_2, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
166 #endif
167 delta=sum_2;
168
169
170 if (! (breakFlag = (ABS(delta) <= TOLERANCE_FOR_SCALARS))) {
171 alpha=tau/delta;
172 /* smoother */
173 #pragma omp parallel
174 {
175 #pragma omp for private(i0) schedule(static)
176 for (i0=0;i0<n;i0++) r[i0]-=alpha*v[i0];
177 #pragma omp for private(i0,d) reduction(+:sum_3,sum_4) schedule(static)
178 for (i0=0;i0<n;i0++) {
179 d=r[i0]-rs[i0];
180 sum_3+=d*d;
181 sum_4+=d*rs[i0];
182 }
183 }
184 #ifdef PASO_MPI
185 loc_sum[0] = sum_3;
186 loc_sum[1] = sum_4;
187 MPI_Allreduce(loc_sum, sum, 2, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
188 sum_3=sum[0];
189 sum_4=sum[1];
190 #endif
191 gamma_1= ( (ABS(sum_3)<= ZERO) ? 0 : -sum_4/sum_3) ;
192 gamma_2= ONE-gamma_1;
193 #pragma omp parallel
194 {
195 #pragma omp for private(i0,x2_tmp,x_tmp,rs_tmp) schedule(static)
196 for (i0=0;i0<n;++i0) {
197 rs[i0]=gamma_2*rs[i0]+gamma_1*r[i0];
198 x2[i0]+=alpha*p[i0];
199 x[i0]=gamma_2*x[i0]+gamma_1*x2[i0];
200 }
201 #pragma omp for private(i0) reduction(+:sum_5) schedule(static)
202 for (i0=0;i0<n;++i0) sum_5+=rs[i0]*rs[i0];
203 }
204 #ifdef PASO_MPI
205 loc_sum[0] = sum_5;
206 MPI_Allreduce(loc_sum, &sum_5, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm);
207 #endif
208 norm_of_residual=sqrt(sum_5);
209 convergeFlag = norm_of_residual <= tol;
210 maxIterFlag = num_iter == maxit;
211 breakFlag = (ABS(tau) <= TOLERANCE_FOR_SCALARS);
212 }
213 }
214 /* end of iteration */
215 num_iter_global=num_iter;
216 norm_of_residual_global=norm_of_residual;
217 if (maxIterFlag) {
218 status = SOLVER_MAXITER_REACHED;
219 } else if (breakFlag) {
220 status = SOLVER_BREAKDOWN;
221 }
222 Performance_stopMonitor(pp,PERFORMANCE_SOLVER);
223 TMPMEMFREE(rs);
224 TMPMEMFREE(x2);
225 TMPMEMFREE(v);
226 TMPMEMFREE(p);
227 *iter=num_iter_global;
228 *resid=norm_of_residual_global;
229 }
230 /* End of PCG */
231 return status;
232 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26