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

  ViewVC Help
Powered by ViewVC 1.1.26