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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2662 - (show annotations)
Tue Sep 15 03:05:23 2009 UTC (10 years ago) by artak
Original Path: trunk/paso/src/Solver_preconditioner.c
File MIME type: text/plain
File size: 8520 byte(s)
Migrating to block_sizes greather than 3 made easy.
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 /**************************************************************/
16
17 /* Paso: SystemMatrix: sets-up the preconditioner */
18
19 /**************************************************************/
20
21 /* Copyrights by ACcESS Australia 2003/04 */
22 /* Author: Lutz Gross, l.gross@uq.edu.au */
23
24 /**************************************************************/
25
26 #include "Paso.h"
27 #include "SystemMatrix.h"
28 #include "Solver.h"
29
30 /***********************************************************************************/
31
32 /* free space */
33
34 void Paso_Preconditioner_free(Paso_Solver_Preconditioner* in) {
35 if (in!=NULL) {
36 Paso_Solver_ILU_free(in->ilu);
37 Paso_Solver_RILU_free(in->rilu);
38 Paso_Solver_Jacobi_free(in->jacobi);
39 Paso_Solver_GS_free(in->gs);
40 Paso_Solver_AMG_free(in->amg);
41 Paso_Solver_AMG_System_free(in->amgSystem);
42 MEMFREE(in);
43 }
44 }
45 /* call the iterative solver: */
46
47 void Paso_Solver_setPreconditioner(Paso_SystemMatrix* A,Paso_Options* options) {
48
49 Paso_Solver_Preconditioner* prec=NULL;
50 dim_t i;
51 if (A->solver==NULL) {
52 /* allocate structure to hold preconditioner */
53 prec=MEMALLOC(1,Paso_Solver_Preconditioner);
54 prec->amgSystem=MEMALLOC(1,Paso_Solver_AMG_System);
55 if (Paso_checkPtr(prec)) return;
56 prec->type=UNKNOWN;
57 prec->rilu=NULL;
58 prec->ilu=NULL;
59 prec->jacobi=NULL;
60 prec->gs=NULL;
61 prec->amg=NULL;
62
63 prec->amgSystem->block_size=A->row_block_size;
64 for (i=0;i<A->row_block_size;++i) {
65 prec->amgSystem->amgblock[i]=NULL;
66 prec->amgSystem->block[i]=NULL;
67 }
68
69
70 A->solver=prec;
71 switch (options->preconditioner) {
72 default:
73 case PASO_JACOBI:
74 if (options->verbose) printf("Jacobi preconditioner is used.\n");
75 prec->jacobi=Paso_Solver_getJacobi(A->mainBlock);
76 prec->type=PASO_JACOBI;
77 break;
78 case PASO_ILU0:
79 if (options->verbose) printf("ILU preconditioner is used.\n");
80 prec->ilu=Paso_Solver_getILU(A->mainBlock,options->verbose);
81 prec->type=PASO_ILU0;
82 break;
83 case PASO_RILU:
84 if (options->verbose) printf("RILU preconditioner is used.\n");
85 prec->rilu=Paso_Solver_getRILU(A->mainBlock,options->verbose);
86 prec->type=PASO_RILU;
87 break;
88 case PASO_GS:
89 if (options->verbose) printf("Gauss-Seidel preconditioner is used.\n");
90 prec->gs=Paso_Solver_getGS(A->mainBlock,options->verbose);
91 prec->gs->sweeps=options->sweeps;
92 prec->type=PASO_GS;
93 break;
94 case PASO_AMG:
95 if (options->verbose) printf("AMG preconditioner is used.\n");
96
97 /*For performace reasons we check if block_size is one. If yes, then we do not need to separate blocks.*/
98 if (A->row_block_size==1) {
99 prec->amg=Paso_Solver_getAMG(A->mainBlock,options->level_max,options);
100 }
101 else {
102 #pragma omp parallel for private(i) schedule(static)
103 for (i=0;i<A->row_block_size;++i) {
104 prec->amgSystem->block[i]=Paso_SparseMatrix_getBlock(A->mainBlock,i+1);
105 prec->amgSystem->amgblock[i]=Paso_Solver_getAMG(prec->amgSystem->block[i],options->level_max,options);
106 }
107 }
108
109 prec->type=PASO_AMG;
110 break;
111
112 }
113 if (! Paso_MPIInfo_noError(A->mpi_info ) ){
114 Paso_Preconditioner_free(prec);
115 A->solver=NULL;
116 }
117 }
118 }
119
120 /* applies the preconditioner */
121 /* has to be called within a parallel reqion */
122 /* barrier synchronization is performed before the evaluation to make sure that the input vector is available */
123 void Paso_Solver_solvePreconditioner(Paso_SystemMatrix* A,double* x,double* b){
124 Paso_Solver_Preconditioner* prec=(Paso_Solver_Preconditioner*) A->solver;
125 dim_t i,j;
126 dim_t n=Paso_SystemMatrix_getGlobalNumRows(A);
127 double **xx;
128 double **bb;
129
130 switch (prec->type) {
131 default:
132 case PASO_JACOBI:
133 Paso_Solver_solveJacobi(prec->jacobi,x,b);
134 break;
135 case PASO_ILU0:
136 Paso_Solver_solveILU(prec->ilu,x,b);
137 break;
138 case PASO_RILU:
139 Paso_Solver_solveRILU(prec->rilu,x,b);
140 break;
141 case PASO_GS:
142 /* Gauss-Seidel preconditioner P=U^{-1}DL^{-1} is used here with sweeps paramenter.
143 We want to solve x_new=x_old+P^{-1}(b-Ax_old). So for fisrt 3 we will have the following:
144 x_0=0
145 x_1=P^{-1}(b)
146
147 b_old=b
148
149 b_new=b_old+b
150 x_2=x_1+P^{-1}(b-Ax_1)=P^{-1}(b)+P^{-1}(b)-P^{-1}AP^{-1}(b))
151 =P^{-1}(2b-AP^{-1}b)=P^{-1}(b_new-AP^{-1}b_old)
152 b_old=b_new
153
154 b_new=b_old+b
155 x_3=....=P^{-1}(b_new-AP^{-1}b_old)
156 b_old=b_new
157
158 So for n- steps we will use loop, but we always make sure that every value calculated only once!
159 */
160
161 Paso_Solver_solveGS(prec->gs,x,b);
162 if (prec->gs->sweeps>1) {
163 double *bold=MEMALLOC(prec->gs->n*prec->gs->n_block,double);
164 double *bnew=MEMALLOC(prec->gs->n*prec->gs->n_block,double);
165
166 #pragma omp parallel for private(i) schedule(static)
167 for (i=0;i<prec->gs->n*prec->gs->n_block;++i) bold[i]=b[i];
168
169 while(prec->gs->sweeps>1) {
170 #pragma omp parallel for private(i) schedule(static)
171 for (i=0;i<prec->gs->n*prec->gs->n_block;++i) bnew[i]=bold[i]+b[i];
172 /* Compute the residual b=b-Ax*/
173 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), A, x, DBLE(1), bnew);
174 /* Go round again*/
175 Paso_Solver_solveGS(prec->gs,x,bnew);
176 #pragma omp parallel for private(i) schedule(static)
177 for (i=0;i<prec->gs->n*prec->gs->n_block;++i) bold[i]=bnew[i];
178 prec->gs->sweeps=prec->gs->sweeps-1;
179 }
180 MEMFREE(bold);
181 MEMFREE(bnew);
182 }
183 break;
184 case PASO_AMG:
185
186 /*For performace reasons we check if block_size is one. If yes, then we do not need to do unnecessary copying.*/
187 if (A->row_block_size==1) {
188 Paso_Solver_solveAMG(prec->amg,x,b);
189 }
190 else {
191
192 xx=MEMALLOC(A->row_block_size,double*);
193 bb=MEMALLOC(A->row_block_size,double*);
194
195 for (i=0;i<A->row_block_size;i++) {
196 xx[i]=MEMALLOC(n,double);
197 bb[i]=MEMALLOC(n,double);
198 }
199
200 #pragma omp parallel for private(i,j) schedule(static)
201 for (i=0;i<n;i++) {
202 for (j=0;j<A->row_block_size;j++) {
203 bb[j][i]=b[A->row_block_size*i+j];
204 }
205 }
206
207 #pragma omp parallel for private(i) schedule(static)
208 for (i=0;i<A->row_block_size;i++) {
209 Paso_Solver_solveAMG(prec->amgSystem->amgblock[i],xx[i],bb[i]);
210 }
211
212 #pragma omp parallel for private(i,j) schedule(static)
213 for (i=0;i<n;i++) {
214 for (j=0;j<A->row_block_size;j++) {
215 x[A->row_block_size*i+j]=xx[j][i];
216 }
217 }
218
219 for (i=0;i<A->row_block_size;i++) {
220 MEMFREE(xx[i]);
221 MEMFREE(bb[i]);
222 }
223
224 MEMFREE(xx);
225 MEMFREE(bb);
226 }
227
228 break;
229 }
230
231 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26