/[escript]/branches/doubleplusgood/paso/src/RILU.cpp
ViewVC logotype

Contents of /branches/doubleplusgood/paso/src/RILU.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 431 - (show annotations)
Fri Jan 13 05:07:10 2006 UTC (13 years, 4 months ago) by gross
Original Path: trunk/paso/src/Solvers/Solver_RILU.c
File MIME type: text/plain
File size: 15466 byte(s)
new implementation of ILU0
1 /* $Id$ */
2
3 /**************************************************************/
4
5 /* Paso: RILU preconditioner with reordering */
6
7 /**************************************************************/
8
9 /* Copyrights by ACcESS Australia 2003,2004,2005 */
10 /* Author: gross@access.edu.au */
11
12 /**************************************************************/
13
14 #include "Paso.h"
15 #include "Solver.h"
16 #include "Util.h"
17
18 /**************************************************************/
19
20 /* free all memory used by RILU */
21
22 void Paso_Solver_RILU_free(Paso_Solver_RILU * in) {
23 if (in!=NULL) {
24 Paso_Solver_RILU_free(in->RILU_of_Schur);
25 MEMFREE(in->inv_A_FF);
26 MEMFREE(in->A_FF_pivot);
27 Paso_SystemMatrix_dealloc(in->A_FC);
28 Paso_SystemMatrix_dealloc(in->A_CF);
29 MEMFREE(in->rows_in_F);
30 MEMFREE(in->rows_in_C);
31 MEMFREE(in->mask_F);
32 MEMFREE(in->mask_C);
33 MEMFREE(in->x_F);
34 MEMFREE(in->b_F);
35 MEMFREE(in->x_C);
36 MEMFREE(in->b_C);
37 MEMFREE(in);
38 }
39 }
40
41 /**************************************************************/
42
43 /* constructs the block-block factorization of
44
45 [ A_FF A_FC ]
46 A_p=
47 [ A_CF A_FF ]
48
49 to
50
51 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ]
52 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ]
53
54
55 where S=A_FF-ACF*invA_FF*A_FC within the shape of S
56
57 then RILU is applied to S again until S becomes empty
58
59 */
60 Paso_Solver_RILU* Paso_Solver_getRILU(Paso_SystemMatrix * A_p,bool_t verbose) {
61 Paso_Solver_RILU* out=NULL;
62 dim_t n=A_p->num_rows;
63 dim_t n_block=A_p->row_block_size;
64 index_t* mis_marker=NULL;
65 index_t* counter=NULL;
66 index_t iPtr,*index, *where_p;
67 dim_t i,k;
68 Paso_SystemMatrix * schur=NULL;
69 double A11,A12,A13,A21,A22,A23,A31,A32,A33,D,time0,time1,time2;
70
71
72 /* identify independend set of rows/columns */
73 mis_marker=TMPMEMALLOC(n,index_t);
74 counter=TMPMEMALLOC(n,index_t);
75 out=MEMALLOC(1,Paso_Solver_RILU);
76 out->RILU_of_Schur=NULL;
77 out->inv_A_FF=NULL;
78 out->A_FF_pivot=NULL;
79 out->A_FC=NULL;
80 out->A_CF=NULL;
81 out->rows_in_F=NULL;
82 out->rows_in_C=NULL;
83 out->mask_F=NULL;
84 out->mask_C=NULL;
85 out->x_F=NULL;
86 out->b_F=NULL;
87 out->x_C=NULL;
88 out->b_C=NULL;
89
90 if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {
91 /* identify independend set of rows/columns */
92 time0=Paso_timer();
93 #pragma omp parallel for private(i) schedule(static)
94 for (i=0;i<n;++i) mis_marker[i]=-1;
95 Paso_SystemMatrixPattern_mis(A_p->pattern,mis_marker);
96 time2=Paso_timer()-time0;
97 if (Paso_noError()) {
98 #pragma omp parallel for private(i) schedule(static)
99 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
100 out->n=n;
101 out->n_block=n_block;
102 out->n_F=Paso_Util_cumsum(n,counter);
103 out->mask_F=MEMALLOC(n,index_t);
104 out->rows_in_F=MEMALLOC(out->n_F,index_t);
105 out->inv_A_FF=MEMALLOC(n_block*n_block*out->n_F,double);
106 out->A_FF_pivot=NULL; /* later use for block size>3 */
107 if (! (Paso_checkPtr(out->mask_F) || Paso_checkPtr(out->inv_A_FF) || Paso_checkPtr(out->rows_in_F) ) ) {
108 #pragma omp parallel
109 {
110 /* creates an index for F from mask */
111 #pragma omp for private(i) schedule(static)
112 for (i = 0; i < out->n_F; ++i) out->rows_in_F[i]=-1;
113 #pragma omp for private(i) schedule(static)
114 for (i = 0; i < n; ++i) {
115 if (mis_marker[i]) {
116 out->rows_in_F[counter[i]]=i;
117 out->mask_F[i]=counter[i];
118 } else {
119 out->mask_F[i]=-1;
120 }
121 }
122 #pragma omp for private(i, where_p,iPtr,A11,A12,A13,A21,A22,A23,A31,A32,A33,D,index) schedule(static)
123 for (i = 0; i < out->n_F; i++) {
124 /* find main diagonal */
125 iPtr=A_p->pattern->ptr[out->rows_in_F[i]];
126 index=&(A_p->pattern->index[iPtr]);
127 where_p=(index_t*)bsearch(&out->rows_in_F[i],
128 index,
129 A_p->pattern->ptr[out->rows_in_F[i] + 1]-A_p->pattern->ptr[out->rows_in_F[i]],
130 sizeof(index_t),
131 Paso_comparIndex);
132 if (where_p==NULL) {
133 Paso_setError(VALUE_ERROR, "Paso_Solver_getRILU: main diagonal element missing.");
134 } else {
135 iPtr+=(index_t)(where_p-index);
136 /* get inverse of A_FF block: */
137 if (n_block==1) {
138 if (ABS(A_p->val[iPtr])>0.) {
139 out->inv_A_FF[i]=1./A_p->val[iPtr];
140 } else {
141 Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver_getRILU: Break-down in RILU decomposition: non-regular main diagonal block.");
142 }
143 } else if (n_block==2) {
144 A11=A_p->val[iPtr*4];
145 A21=A_p->val[iPtr*4+1];
146 A12=A_p->val[iPtr*4+2];
147 A22=A_p->val[iPtr*4+3];
148 D = A11*A22-A12*A21;
149 if (ABS(D) > 0 ){
150 D=1./D;
151 out->inv_A_FF[i*4]= A22*D;
152 out->inv_A_FF[i*4+1]=-A21*D;
153 out->inv_A_FF[i*4+2]=-A12*D;
154 out->inv_A_FF[i*4+3]= A11*D;
155 } else {
156 Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver_getRILU:Break-down in RILU decomposition: non-regular main diagonal block.");
157 }
158 } else if (n_block==3) {
159 A11=A_p->val[iPtr*9 ];
160 A21=A_p->val[iPtr*9+1];
161 A31=A_p->val[iPtr*9+2];
162 A12=A_p->val[iPtr*9+3];
163 A22=A_p->val[iPtr*9+4];
164 A32=A_p->val[iPtr*9+5];
165 A13=A_p->val[iPtr*9+6];
166 A23=A_p->val[iPtr*9+7];
167 A33=A_p->val[iPtr*9+8];
168 D = A11*(A22*A33-A23*A32)+ A12*(A31*A23-A21*A33)+A13*(A21*A32-A31*A22);
169 if (ABS(D) > 0 ){
170 D=1./D;
171 out->inv_A_FF[i*9 ]=(A22*A33-A23*A32)*D;
172 out->inv_A_FF[i*9+1]=(A31*A23-A21*A33)*D;
173 out->inv_A_FF[i*9+2]=(A21*A32-A31*A22)*D;
174 out->inv_A_FF[i*9+3]=(A13*A32-A12*A33)*D;
175 out->inv_A_FF[i*9+4]=(A11*A33-A31*A13)*D;
176 out->inv_A_FF[i*9+5]=(A12*A31-A11*A32)*D;
177 out->inv_A_FF[i*9+6]=(A12*A23-A13*A22)*D;
178 out->inv_A_FF[i*9+7]=(A13*A21-A11*A23)*D;
179 out->inv_A_FF[i*9+8]=(A11*A22-A12*A21)*D;
180 } else {
181 Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver_getRILU:Break-down in RILU decomposition: non-regular main diagonal block.");
182 }
183 }
184 }
185 }
186 } /* end parallel region */
187
188 if( Paso_noError()) {
189 /* if there are no nodes in the coarse level there is no more work to do */
190 out->n_C=n-out->n_F;
191 if (out->n_C>0) {
192 out->rows_in_C=MEMALLOC(out->n_C,index_t);
193 out->mask_C=MEMALLOC(n,index_t);
194 if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
195 /* creates an index for C from mask */
196 #pragma omp parallel for private(i) schedule(static)
197 for (i = 0; i < n; ++i) counter[i]=! mis_marker[i];
198 Paso_Util_cumsum(n,counter);
199 #pragma omp parallel
200 {
201 #pragma omp for private(i) schedule(static)
202 for (i = 0; i < out->n_C; ++i) out->rows_in_C[i]=-1;
203 #pragma omp for private(i) schedule(static)
204 for (i = 0; i < n; ++i) {
205 if (! mis_marker[i]) {
206 out->rows_in_C[counter[i]]=i;
207 out->mask_C[i]=counter[i];
208 } else {
209 out->mask_C[i]=-1;
210 }
211 }
212 } /* end parallel region */
213 /* get A_CF block: */
214 out->A_CF=Paso_SystemMatrix_getSubmatrix(A_p,out->n_C,out->rows_in_C,out->mask_F);
215 if (Paso_noError()) {
216 /* get A_FC block: */
217 out->A_FC=Paso_SystemMatrix_getSubmatrix(A_p,out->n_F,out->rows_in_F,out->mask_C);
218 /* get A_FF block: */
219 if (Paso_noError()) {
220 schur=Paso_SystemMatrix_getSubmatrix(A_p,out->n_C,out->rows_in_C,out->mask_C);
221 time0=Paso_timer()-time0;
222 if (Paso_noError()) {
223 time1=Paso_timer();
224 /* update A_CC block to get Schur complement and then apply RILU to it */
225 Paso_Solver_updateIncompleteSchurComplement(schur,out->A_CF,out->inv_A_FF,out->A_FF_pivot,out->A_FC);
226 time1=Paso_timer()-time1;
227 out->RILU_of_Schur=Paso_Solver_getRILU(schur,verbose);
228 Paso_SystemMatrix_dealloc(schur);
229 }
230 /* allocate work arrays for RILU application */
231 if (Paso_noError()) {
232 out->x_F=MEMALLOC(n_block*out->n_F,double);
233 out->b_F=MEMALLOC(n_block*out->n_F,double);
234 out->x_C=MEMALLOC(n_block*out->n_C,double);
235 out->b_C=MEMALLOC(n_block*out->n_C,double);
236 if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
237 #pragma omp parallel
238 {
239 #pragma omp for private(i,k) schedule(static)
240 for (i = 0; i < out->n_F; ++i) {
241 for (k=0; k<n_block;++k) {
242 out->x_F[i*n_block+k]=0.;
243 out->b_F[i*n_block+k]=0.;
244 }
245 }
246 #pragma omp for private(i,k) schedule(static)
247 for (i = 0; i < out->n_C; ++i) {
248 for (k=0; k<n_block;++k) {
249 out->x_C[i*n_block+k]=0.;
250 out->b_C[i*n_block+k]=0.;
251 }
252 }
253 } /* end parallel region */
254 }
255 }
256 }
257 }
258 }
259 }
260 }
261 }
262 }
263 }
264 TMPMEMFREE(mis_marker);
265 TMPMEMFREE(counter);
266 if (Paso_noError()) {
267 if (verbose) {
268 printf("RILU: %d unknowns eliminated. %d left.\n",out->n_F,n-out->n_F);
269 if (out->n_C>0) {
270 printf("timing: RILU: MIS/reordering/elemination : %e/%e/%e\n",time2,time0,time1);
271 } else {
272 printf("timing: RILU: MIS: %e\n",time2);
273 }
274 }
275 return out;
276 } else {
277 Paso_Solver_RILU_free(out);
278 return NULL;
279 }
280 }
281
282 /**************************************************************/
283
284 /* apply RILU precondition b-> x
285
286 in fact it solves
287
288 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ] [ x_F ] = [b_F]
289 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
290
291 in the form
292
293 b->[b_F,b_C]
294 x_F=invA_FF*b_F
295 b_C=b_C-A_CF*x_F
296 x_C=RILU(b_C)
297 b_F=b_F-A_FC*x_C
298 x_F=invA_FF*b_F
299 x<-[x_F,x_C]
300
301 should be called within a parallel region
302 barrier synconization should be performed to make sure that the input vector available
303
304 */
305
306 void Paso_Solver_solveRILU(Paso_Solver_RILU * rilu, double * x, double * b) {
307 dim_t i,k;
308 dim_t n_block=rilu->n_block;
309
310 if (rilu->n_C==0) {
311 /* x=invA_FF*b */
312 Paso_Solver_applyBlockDiagonalMatrix(n_block,rilu->n_F,rilu->inv_A_FF,rilu->A_FF_pivot,x,b);
313 } else {
314 /* b->[b_F,b_C] */
315 if (n_block==1) {
316 #pragma omp for private(i) schedule(static)
317 for (i=0;i<rilu->n_F;++i) rilu->b_F[i]=b[rilu->rows_in_F[i]];
318 #pragma omp for private(i) schedule(static)
319 for (i=0;i<rilu->n_C;++i) rilu->b_C[i]=b[rilu->rows_in_C[i]];
320 } else {
321 #pragma omp for private(i,k) schedule(static)
322 for (i=0;i<rilu->n_F;++i)
323 for (k=0;k<n_block;k++) rilu->b_F[rilu->n_block*i+k]=b[n_block*rilu->rows_in_F[i]+k];
324 #pragma omp for private(i,k) schedule(static)
325 for (i=0;i<rilu->n_C;++i)
326 for (k=0;k<n_block;k++) rilu->b_C[rilu->n_block*i+k]=b[n_block*rilu->rows_in_C[i]+k];
327 }
328 /* x_F=invA_FF*b_F */
329 Paso_Solver_applyBlockDiagonalMatrix(n_block,rilu->n_F,rilu->inv_A_FF,rilu->A_FF_pivot,rilu->x_F,rilu->b_F);
330 /* b_C=b_C-A_CF*x_F */
331 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(-1.,rilu->A_CF,rilu->x_F,1.,rilu->b_C);
332 /* x_C=RILU(b_C) */
333 Paso_Solver_solveRILU(rilu->RILU_of_Schur,rilu->x_C,rilu->b_C);
334 /* b_F=b_F-A_FC*x_C */
335 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(-1.,rilu->A_FC,rilu->x_C,1.,rilu->b_F);
336 /* x_F=invA_FF*b_F */
337 Paso_Solver_applyBlockDiagonalMatrix(n_block,rilu->n_F,rilu->inv_A_FF,rilu->A_FF_pivot,rilu->x_F,rilu->b_F);
338 /* x<-[x_F,x_C] */
339 if (n_block==1) {
340 #pragma omp for private(i) schedule(static)
341 for (i=0;i<rilu->n;++i) {
342 if (rilu->mask_C[i]>-1) {
343 x[i]=rilu->x_C[rilu->mask_C[i]];
344 } else {
345 x[i]=rilu->x_F[rilu->mask_F[i]];
346 }
347 }
348 } else {
349 #pragma omp for private(i,k) schedule(static)
350 for (i=0;i<rilu->n;++i) {
351 if (rilu->mask_C[i]>-1) {
352 for (k=0;k<n_block;k++) x[n_block*i+k]=rilu->x_C[n_block*rilu->mask_C[i]+k];
353 } else {
354 for (k=0;k<n_block;k++) x[n_block*i+k]=rilu->x_F[n_block*rilu->mask_F[i]+k];
355 }
356 }
357 }
358 /* all done */
359 }
360 return;
361 }
362
363 /*
364 * $Log$
365 *
366 */

  ViewVC Help
Powered by ViewVC 1.1.26