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

Contents of /trunk/paso/src/Solver_AMG.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26