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

Annotation of /trunk/paso/src/Solver_RILU.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2608 - (hide annotations)
Tue Aug 18 01:25:18 2009 UTC (10 years, 2 months ago) by jfenwick
File MIME type: text/plain
File size: 15878 byte(s)
Updating Lutz' email

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

  ViewVC Help
Powered by ViewVC 1.1.26