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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26