/[escript]/trunk/paso/src/RILU.cpp
ViewVC logotype

Contents of /trunk/paso/src/RILU.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4797 - (show annotations)
Mon Mar 24 05:42:42 2014 UTC (5 years, 11 months ago) by caltinay
File size: 16125 byte(s)
put SparseMatrix into paso namespace, accompanied by minor code cleanup.

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

Properties

Name Value
svn:mergeinfo /branches/amg_from_3530/paso/src/RILU.cpp:3531-3826 /branches/lapack2681/paso/src/RILU.cpp:2682-2741 /branches/pasowrap/paso/src/RILU.cpp:3661-3674 /branches/py3_attempt2/paso/src/RILU.cpp:3871-3891 /branches/restext/paso/src/RILU.cpp:2610-2624 /branches/ripleygmg_from_3668/paso/src/RILU.cpp:3669-3791 /branches/stage3.0/paso/src/RILU.cpp:2569-2590 /branches/symbolic_from_3470/paso/src/RILU.cpp:3471-3974 /branches/symbolic_from_3470/ripley/test/python/paso/src/RILU.cpp:3517-3974 /release/3.0/paso/src/RILU.cpp:2591-2601 /trunk/paso/src/RILU.cpp:4257-4344 /trunk/ripley/test/python/paso/src/RILU.cpp:3480-3515

  ViewVC Help
Powered by ViewVC 1.1.26