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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4829 - (show annotations)
Thu Apr 3 04:02:53 2014 UTC (5 years, 5 months ago) by caltinay
File size: 15727 byte(s)
checkpointing some SparseMatrix 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 delete[] in->rows_in_F;
43 delete[] in->rows_in_C;
44 delete[] in->mask_F;
45 delete[] in->mask_C;
46 delete[] in->x_F;
47 delete[] in->b_F;
48 delete[] in->x_C;
49 delete[] in->b_C;
50 delete in;
51 }
52 }
53
54 /************************************************************************************/
55
56 /* constructs the block-block factorization of
57
58 [ A_FF A_FC ]
59 A_p=
60 [ A_CF A_FF ]
61
62 to
63
64 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ]
65 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ]
66
67
68 where S=A_FF-ACF*invA_FF*A_FC within the shape of S
69
70 then RILU is applied to S again until S becomes empty
71
72 */
73 Paso_Solver_RILU* Paso_Solver_getRILU(paso::SparseMatrix_ptr A_p, bool verbose)
74 {
75 Paso_Solver_RILU* out=NULL;
76 dim_t n=A_p->numRows;
77 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 paso::SparseMatrix_ptr schur;
83 double A11,A12,A13,A21,A22,A23,A31,A32,A33,D,time0=0,time1=0;/*,time2=0;*/
84
85
86 mis_marker=new index_t[n];
87 counter=new index_t[n];
88 out=new Paso_Solver_RILU;
89 out->RILU_of_Schur=NULL;
90 out->inv_A_FF=NULL;
91 out->A_FF_pivot=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 ( !(Esys_checkPtr(mis_marker) || Esys_checkPtr(out) || Esys_checkPtr(counter) ) ) {
102 /* identify independent set of rows/columns */
103 time0=Esys_timer();
104 #pragma omp parallel for private(i) schedule(static)
105 for (i=0;i<n;++i) mis_marker[i]=-1;
106 A_p->pattern->mis(mis_marker);
107 /*time2=Esys_timer()-time0;*/
108 if (Esys_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=new index_t[n];
115 out->rows_in_F=new index_t[out->n_F];
116 out->inv_A_FF=new double[n_block*n_block*out->n_F];
117 out->A_FF_pivot=NULL; /* later use for block size>3 */
118 if (! (Esys_checkPtr(out->mask_F) || Esys_checkPtr(out->inv_A_FF) || Esys_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 Esys_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 Esys_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 Esys_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 Esys_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( Esys_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=new index_t[out->n_C];
204 out->mask_C=new index_t[n];
205 if (! (Esys_checkPtr(out->mask_C) || Esys_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 out->A_CF=A_p->getSubmatrix(out->n_C, out->n_F, out->rows_in_C, out->mask_F);
226 if (Esys_noError()) {
227 /* get A_FC block: */
228 out->A_FC=A_p->getSubmatrix(out->n_F, out->n_C, out->rows_in_F, out->mask_C);
229 /* get A_FF block: */
230 if (Esys_noError()) {
231 schur = A_p->getSubmatrix(out->n_C, out->n_C, out->rows_in_C, out->mask_C);
232 time0=Esys_timer()-time0;
233 if (Esys_noError()) {
234 time1=Esys_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=Esys_timer()-time1;
238 out->RILU_of_Schur=Paso_Solver_getRILU(schur,verbose);
239 schur.reset();
240 }
241 /* allocate work arrays for RILU application */
242 if (Esys_noError()) {
243 out->x_F=new double[n_block*out->n_F];
244 out->b_F=new double[n_block*out->n_F];
245 out->x_C=new double[n_block*out->n_C];
246 out->b_C=new double[n_block*out->n_C];
247 #pragma omp parallel
248 {
249 #pragma omp for private(i,k) schedule(static)
250 for (i = 0; i < out->n_F; ++i) {
251 for (k=0; k<n_block;++k) {
252 out->x_F[i*n_block+k]=0.;
253 out->b_F[i*n_block+k]=0.;
254 }
255 }
256 #pragma omp for private(i,k) schedule(static)
257 for (i = 0; i < out->n_C; ++i) {
258 for (k=0; k<n_block;++k) {
259 out->x_C[i*n_block+k]=0.;
260 out->b_C[i*n_block+k]=0.;
261 }
262 }
263 } /* end parallel region */
264 }
265 }
266 }
267 }
268 }
269 }
270 }
271 }
272 }
273 delete[] mis_marker;
274 delete[] counter;
275 if (Esys_noError()) {
276 /*
277 if (verbose) {
278 printf("RILU: %d unknowns eliminated. %d left.\n",out->n_F,n-out->n_F);
279 if (out->n_C>0) {
280 printf("timing: RILU: MIS/reordering/elimination : %e/%e/%e\n",time2,time0,time1);
281 } else {
282 printf("timing: RILU: MIS: %e\n",time2);
283 }
284 }
285 */
286 return out;
287 } else {
288 Paso_Solver_RILU_free(out);
289 return NULL;
290 }
291 }
292
293 /************************************************************************************/
294
295 /* apply RILU precondition b-> x
296
297 in fact it solves
298
299 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ] [ x_F ] = [b_F]
300 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
301
302 in the form
303
304 b->[b_F,b_C]
305 x_F=invA_FF*b_F
306 b_C=b_C-A_CF*x_F
307 x_C=RILU(b_C)
308 b_F=b_F-A_FC*x_C
309 x_F=invA_FF*b_F
310 x<-[x_F,x_C]
311
312 Should be called within a parallel region.
313 Barrier synchronization should be performed to make sure that the input
314 vector is available.
315 */
316
317 void Paso_Solver_solveRILU(Paso_Solver_RILU * rilu, double * x, double * b) {
318 dim_t i,k;
319 dim_t n_block=rilu->n_block;
320
321 if (rilu->n_C==0) {
322 /* x=invA_FF*b */
323 Paso_Copy(n_block*rilu->n_F, x, b);
324 Paso_BlockOps_solveAll(n_block,rilu->n_F,rilu->inv_A_FF,rilu->A_FF_pivot,x);
325 } else {
326 /* b->[b_F,b_C] */
327 if (n_block==1) {
328 #pragma omp parallel for private(i) schedule(static)
329 for (i=0;i<rilu->n_F;++i) rilu->b_F[i]=b[rilu->rows_in_F[i]];
330 #pragma omp parallel for private(i) schedule(static)
331 for (i=0;i<rilu->n_C;++i) rilu->b_C[i]=b[rilu->rows_in_C[i]];
332 } else {
333 #pragma omp parallel for private(i,k) schedule(static)
334 for (i=0;i<rilu->n_F;++i)
335 for (k=0;k<n_block;k++) rilu->b_F[rilu->n_block*i+k]=b[n_block*rilu->rows_in_F[i]+k];
336 #pragma omp parallel for private(i,k) schedule(static)
337 for (i=0;i<rilu->n_C;++i)
338 for (k=0;k<n_block;k++) rilu->b_C[rilu->n_block*i+k]=b[n_block*rilu->rows_in_C[i]+k];
339 }
340 /* x_F=invA_FF*b_F */
341 Paso_Copy(n_block*rilu->n_F, rilu->x_F,rilu->b_F);
342 Paso_BlockOps_solveAll(n_block,rilu->n_F,rilu->inv_A_FF,rilu->A_FF_pivot,rilu->x_F);
343 /* b_C=b_C-A_CF*x_F */
344 paso::SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,rilu->A_CF,rilu->x_F,1.,rilu->b_C);
345 /* 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 paso::SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,rilu->A_FC,rilu->x_C,1.,rilu->b_F);
349 /* x_F=invA_FF*b_F */
350 Paso_Copy(n_block*rilu->n_F, rilu->x_F,rilu->b_F);
351 Paso_BlockOps_solveAll(n_block,rilu->n_F,rilu->inv_A_FF,rilu->A_FF_pivot,rilu->x_F);
352 /* x<-[x_F,x_C] */
353 if (n_block==1) {
354 #pragma omp parallel for private(i) schedule(static)
355 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 #pragma omp parallel for private(i,k) schedule(static)
364 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 */

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