/[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 2686 - (show annotations)
Tue Sep 29 03:39:36 2009 UTC (10 years, 1 month ago) by artak
File MIME type: text/plain
File size: 18111 byte(s)
 changes the default thresold, bug fixes in RS coupling and extra checks added in Solver 
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 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 */
18
19 /**************************************************************/
20
21 /* Author: artak@uq.edu.au */
22
23 /**************************************************************/
24
25 #include "Paso.h"
26 #include "Solver.h"
27 #include "Options.h"
28 #include "PasoUtil.h"
29 #include "UMFPACK.h"
30 #include "MKL.h"
31 #include "SystemMatrix.h"
32 #include "Pattern_coupling.h"
33
34 /**************************************************************/
35
36 /* free all memory used by AMG */
37
38 void Paso_Solver_AMG_System_free(Paso_Solver_AMG_System * in) {
39 dim_t i;
40 if (in!=NULL) {
41 for (i=0;i<in->block_size;++i) {
42 Paso_Solver_AMG_free(in->amgblock[i]);
43 Paso_SparseMatrix_free(in->block[i]);
44 }
45 MEMFREE(in);
46 }
47 }
48
49
50 /* free all memory used by AMG */
51
52 void Paso_Solver_AMG_free(Paso_Solver_AMG * in) {
53 if (in!=NULL) {
54 Paso_Solver_AMG_free(in->AMG_of_Schur);
55 Paso_Solver_Jacobi_free(in->GS);
56 MEMFREE(in->inv_A_FF);
57 MEMFREE(in->A_FF_pivot);
58 Paso_SparseMatrix_free(in->A_FC);
59 Paso_SparseMatrix_free(in->A_CF);
60 Paso_SparseMatrix_free(in->A);
61 MEMFREE(in->rows_in_F);
62 MEMFREE(in->rows_in_C);
63 MEMFREE(in->mask_F);
64 MEMFREE(in->mask_C);
65 MEMFREE(in->x_F);
66 MEMFREE(in->b_F);
67 MEMFREE(in->x_C);
68 MEMFREE(in->b_C);
69 #ifdef MKL
70 #else
71 #ifdef UMFPACK
72 Paso_UMFPACK1_free((Paso_UMFPACK_Handler*)(in->solver));
73 #endif
74 #endif
75 in->solver=NULL;
76 MEMFREE(in->b_C);
77 MEMFREE(in);
78 }
79 }
80
81 /**************************************************************/
82
83 /* constructs the block-block factorization of
84
85 [ A_FF A_FC ]
86 A_p=
87 [ A_CF A_FF ]
88
89 to
90
91 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ]
92 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ]
93
94
95 where S=A_FF-ACF*invA_FF*A_FC within the shape of S
96
97 then AMG is applied to S again until S becomes empty
98
99 */
100 Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,dim_t level,Paso_Options* options) {
101 Paso_Solver_AMG* out=NULL;
102 Paso_Pattern* temp1=NULL;
103 Paso_Pattern* temp2=NULL;
104 bool_t verbose=options->verbose;
105 dim_t n=A_p->numRows;
106 dim_t n_block=A_p->row_block_size;
107 index_t* mis_marker=NULL;
108 index_t* counter=NULL;
109 index_t iPtr,*index, *where_p, iPtr_s;
110 dim_t i,j;
111 Paso_SparseMatrix * schur=NULL;
112 Paso_SparseMatrix * schur_withFillIn=NULL;
113 double S=0;
114
115 /*Make sure we have block sizes 1*/
116 if (A_p->col_block_size>1) {
117 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires column block size 1.");
118 return NULL;
119 }
120 if (A_p->row_block_size>1) {
121 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires row block size 1.");
122 return NULL;
123 }
124 out=MEMALLOC(1,Paso_Solver_AMG);
125 /* identify independend set of rows/columns */
126 mis_marker=TMPMEMALLOC(n,index_t);
127 counter=TMPMEMALLOC(n,index_t);
128 if ( !( Paso_checkPtr(mis_marker) || Paso_checkPtr(counter) || Paso_checkPtr(out)) ) {
129 out->AMG_of_Schur=NULL;
130 out->inv_A_FF=NULL;
131 out->A_FF_pivot=NULL;
132 out->A_FC=NULL;
133 out->A_CF=NULL;
134 out->rows_in_F=NULL;
135 out->rows_in_C=NULL;
136 out->mask_F=NULL;
137 out->mask_C=NULL;
138 out->x_F=NULL;
139 out->b_F=NULL;
140 out->x_C=NULL;
141 out->b_C=NULL;
142 out->GS=NULL;
143 out->A=Paso_SparseMatrix_getReference(A_p);
144 out->GS=NULL;
145 out->solver=NULL;
146 /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
147 out->level=level;
148 out->n=n;
149 out->n_block=n_block;
150
151 if (level==0 || n<=options->min_coarse_matrix_size) {
152 out->coarsest_level=TRUE;
153 #ifdef MKL
154 #else
155 #ifdef UMFPACK
156 #else
157 out->GS=Paso_Solver_getJacobi(A_p);
158 #endif
159 #endif
160 } else {
161 out->coarsest_level=FALSE;
162 out->GS=Paso_Solver_getJacobi(A_p);
163
164 /* identify independend set of rows/columns */
165 #pragma omp parallel for private(i) schedule(static)
166 for (i=0;i<n;++i) mis_marker[i]=-1;
167
168 if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
169 Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);
170 }
171 else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
172 Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
173 }
174 else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
175 Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
176 }
177 else {
178 /*Default coarseneing*/
179 Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
180 /*Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);*/
181 /*Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);*/
182 }
183
184 #pragma omp parallel for private(i) schedule(static)
185 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
186 out->n_F=Paso_Util_cumsum(n,counter);
187
188 if (out->n_F==n) {
189 out->coarsest_level=TRUE;
190 if (verbose) {
191 printf("AMG coarsening eliminates all unknowns, switching to direct solver.\n");
192 }
193 } else {
194
195 if (Paso_noError()) {
196
197 /*#pragma omp parallel for private(i) schedule(static)
198 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
199 out->n_F=Paso_Util_cumsum(n,counter);
200 */
201 out->mask_F=MEMALLOC(n,index_t);
202 out->rows_in_F=MEMALLOC(out->n_F,index_t);
203 out->inv_A_FF=MEMALLOC(n_block*n_block*out->n_F,double);
204 out->A_FF_pivot=NULL; /* later use for block size>3 */
205 if (! (Paso_checkPtr(out->mask_F) || Paso_checkPtr(out->inv_A_FF) || Paso_checkPtr(out->rows_in_F) ) ) {
206 /* creates an index for F from mask */
207 #pragma omp parallel for private(i) schedule(static)
208 for (i = 0; i < out->n_F; ++i) out->rows_in_F[i]=-1;
209 #pragma omp parallel for private(i) schedule(static)
210 for (i = 0; i < n; ++i) {
211 if (mis_marker[i]) {
212 out->rows_in_F[counter[i]]=i;
213 out->mask_F[i]=counter[i];
214 } else {
215 out->mask_F[i]=-1;
216 }
217 }
218
219 /* Compute row-sum for getting rs(A_FF)^-1*/
220 #pragma omp parallel for private(i,iPtr,j,S) schedule(static)
221 for (i = 0; i < out->n_F; ++i) {
222 S=0;
223 /*printf("[%d ]: [%d] -> ",i, out->rows_in_F[i]);*/
224 for (iPtr=A_p->pattern->ptr[out->rows_in_F[i]];iPtr<A_p->pattern->ptr[out->rows_in_F[i] + 1]; ++iPtr) {
225 j=A_p->pattern->index[iPtr];
226 /*if (j==out->rows_in_F[i]) printf("diagonal %e",A_p->val[iPtr]);*/
227 if (mis_marker[j])
228 S+=A_p->val[iPtr];
229 }
230 /*printf("-> %e \n",S);*/
231 out->inv_A_FF[i]=1./S;
232 }
233 }
234 }
235
236 /*check whether coarsening process actually makes sense to continue.
237 if coarse matrix at least smaller by 30% then continue, otherwise we stop.*/
238 if ((out->n_F*100/n)<30) {
239 level=1;
240 }
241
242 if ( Paso_noError()) {
243 /* if there are no nodes in the coarse level there is no more work to do */
244 out->n_C=n-out->n_F;
245
246 /*if (out->n_F>500) */
247 out->rows_in_C=MEMALLOC(out->n_C,index_t);
248 out->mask_C=MEMALLOC(n,index_t);
249 if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
250 /* creates an index for C from mask */
251 #pragma omp parallel for private(i) schedule(static)
252 for (i = 0; i < n; ++i) counter[i]=! mis_marker[i];
253 Paso_Util_cumsum(n,counter);
254 #pragma omp parallel for private(i) schedule(static)
255 for (i = 0; i < out->n_C; ++i) out->rows_in_C[i]=-1;
256 #pragma omp parallel for private(i) schedule(static)
257 for (i = 0; i < n; ++i) {
258 if (! mis_marker[i]) {
259 out->rows_in_C[counter[i]]=i;
260 out->mask_C[i]=counter[i];
261 } else {
262 out->mask_C[i]=-1;
263 }
264 }
265 }
266 }
267 if ( Paso_noError()) {
268 /* get A_CF block: */
269 out->A_CF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_F,out->rows_in_C,out->mask_F);
270 /* get A_FC block: */
271 out->A_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
272 /* get A_CC block: */
273 schur=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_C,out->rows_in_C,out->mask_C);
274
275 }
276 if ( Paso_noError()) {
277 /*find the pattern of the schur complement with fill in*/
278 temp1=Paso_Pattern_multiply(PATTERN_FORMAT_DEFAULT,out->A_CF->pattern,out->A_FC->pattern);
279 temp2=Paso_Pattern_binop(PATTERN_FORMAT_DEFAULT, schur->pattern, temp1);
280 schur_withFillIn=Paso_SparseMatrix_alloc(A_p->type,temp2,1,1, TRUE);
281 Paso_Pattern_free(temp1);
282 Paso_Pattern_free(temp2);
283 }
284 if ( Paso_noError()) {
285 /* copy values over*/
286 #pragma omp parallel for private(i,iPtr,j,iPtr_s,index,where_p) schedule(static)
287 for (i = 0; i < schur_withFillIn->numRows; ++i) {
288 for (iPtr=schur_withFillIn->pattern->ptr[i];iPtr<schur_withFillIn->pattern->ptr[i + 1]; ++iPtr) {
289 j=schur_withFillIn->pattern->index[iPtr];
290 iPtr_s=schur->pattern->ptr[i];
291 index=&(schur->pattern->index[iPtr_s]);
292 where_p=(index_t*)bsearch(&j,
293 index,
294 schur->pattern->ptr[i + 1]-schur->pattern->ptr[i],
295 sizeof(index_t),
296 Paso_comparIndex);
297 if (where_p!=NULL) {
298 schur_withFillIn->val[iPtr]=schur->val[iPtr_s+(index_t)(where_p-index)];
299 }
300 }
301 }
302 Paso_Solver_updateIncompleteSchurComplement(schur_withFillIn,out->A_CF,out->inv_A_FF,out->A_FF_pivot,out->A_FC);
303 out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,level-1,options);
304 }
305 /* allocate work arrays for AMG application */
306 if (Paso_noError()) {
307 out->x_F=MEMALLOC(n_block*out->n_F,double);
308 out->b_F=MEMALLOC(n_block*out->n_F,double);
309 out->x_C=MEMALLOC(n_block*out->n_C,double);
310 out->b_C=MEMALLOC(n_block*out->n_C,double);
311
312 if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
313 #pragma omp parallel for private(i) schedule(static)
314 for (i = 0; i < out->n_F; ++i) {
315 out->x_F[i]=0.;
316 out->b_F[i]=0.;
317 }
318 #pragma omp parallel for private(i) schedule(static)
319 for (i = 0; i < out->n_C; ++i) {
320 out->x_C[i]=0.;
321 out->b_C[i]=0.;
322 }
323 }
324 }
325 Paso_SparseMatrix_free(schur);
326 Paso_SparseMatrix_free(schur_withFillIn);
327 }
328 }
329 }
330 TMPMEMFREE(mis_marker);
331 TMPMEMFREE(counter);
332
333 if (Paso_noError()) {
334 if (verbose && level>0 && !out->coarsest_level) {
335 printf("AMG: level: %d: %d unknowns eliminated. %d left.\n",level, out->n_F,out->n_C);
336 }
337 return out;
338 } else {
339 Paso_Solver_AMG_free(out);
340 return NULL;
341 }
342 }
343
344 /**************************************************************/
345
346 /* apply AMG precondition b-> x
347
348 in fact it solves
349
350 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FC ] [ x_F ] = [b_F]
351 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
352
353 in the form
354
355 b->[b_F,b_C]
356 x_F=invA_FF*b_F
357 b_C=b_C-A_CF*x_F
358 x_C=AMG(b_C)
359 b_F=b_F-A_FC*x_C
360 x_F=invA_FF*b_F
361 x<-[x_F,x_C]
362
363 should be called within a parallel region
364 barrier synconization should be performed to make sure that the input vector available
365
366 */
367
368 void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
369 dim_t i;
370 double time0=0;
371 double *r=NULL, *x0=NULL;
372 bool_t verbose=0;
373 #ifdef MKL
374 Paso_SparseMatrix *temp=NULL;
375 #else
376 #ifdef UMFPACK
377 Paso_UMFPACK_Handler * ptr=NULL;
378 #endif
379 #endif
380 r=MEMALLOC(amg->n,double);
381 x0=MEMALLOC(amg->n,double);
382
383 if (amg->coarsest_level) {
384
385 time0=Paso_timer();
386 #ifdef MKL
387 temp=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, amg->A->pattern,1,1, FALSE);
388 #pragma omp parallel for private(i) schedule(static)
389 for (i=0;i<amg->A->len;++i) {
390 temp->val[i]=amg->A->val[i];
391 }
392 Paso_MKL1(temp,x,b,0);
393 Paso_SparseMatrix_free(temp);
394 #else
395 #ifdef UMFPACK
396 ptr=(Paso_UMFPACK_Handler *)(amg->solver);
397 Paso_UMFPACK1(&ptr,amg->A,x,b,verbose);
398 amg->solver=(void*) ptr;
399 #else
400 Paso_Solver_solveJacobi(amg->GS,x,b);
401 #endif
402 #endif
403 time0=Paso_timer()-time0;
404 if (verbose) fprintf(stderr,"timing: DIRECT SOLVER: %e\n",time0);
405
406 } else {
407 /* presmoothing */
408 time0=Paso_timer();
409 Paso_Solver_solveJacobi(amg->GS,x,b);
410 time0=Paso_timer()-time0;
411 if (verbose) fprintf(stderr,"timing: Presmooting: %e\n",time0);
412 /* end of presmoothing */
413
414
415 time0=Paso_timer();
416 #pragma omp parallel for private(i) schedule(static)
417 for (i=0;i<amg->n;++i) r[i]=b[i];
418
419 /*r=b-Ax*/
420 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
421
422 /* b->[b_F,b_C] */
423 #pragma omp parallel for private(i) schedule(static)
424 for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];
425
426 #pragma omp parallel for private(i) schedule(static)
427 for (i=0;i<amg->n_C;++i) amg->b_C[i]=r[amg->rows_in_C[i]];
428
429 /* x_F=invA_FF*b_F */
430 Paso_Solver_applyBlockDiagonalMatrix(1,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);
431
432 /* b_C=b_C-A_CF*x_F */
433 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_CF,amg->x_F,1.,amg->b_C);
434
435 time0=Paso_timer()-time0;
436 if (verbose) fprintf(stderr,"timing: Before next level: %e\n",time0);
437
438 /* x_C=AMG(b_C) */
439 Paso_Solver_solveAMG(amg->AMG_of_Schur,amg->x_C,amg->b_C);
440
441 time0=Paso_timer();
442 /* b_F=b_F-A_FC*x_C */
443 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_FC,amg->x_C,1.,amg->b_F);
444 /* x_F=invA_FF*b_F */
445 Paso_Solver_applyBlockDiagonalMatrix(1,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);
446 /* x<-[x_F,x_C] */
447
448 #pragma omp parallel for private(i) schedule(static)
449 for (i=0;i<amg->n;++i) {
450 if (amg->mask_C[i]>-1) {
451 x[i]=amg->x_C[amg->mask_C[i]];
452 } else {
453 x[i]=amg->x_F[amg->mask_F[i]];
454 }
455 }
456
457 time0=Paso_timer()-time0;
458 if (verbose) fprintf(stderr,"timing: After next level: %e\n",time0);
459
460 /*postsmoothing*/
461 time0=Paso_timer();
462 #pragma omp parallel for private(i) schedule(static)
463 for (i=0;i<amg->n;++i) r[i]=b[i];
464
465 /*r=b-Ax */
466 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
467 Paso_Solver_solveJacobi(amg->GS,x0,r);
468
469 #pragma omp parallel for private(i) schedule(static)
470 for (i=0;i<amg->n;++i) x[i]+=x0[i];
471
472 time0=Paso_timer()-time0;
473 if (verbose) fprintf(stderr,"timing: Postsmoothing: %e\n",time0);
474
475 /*end of postsmoothing*/
476
477 }
478 MEMFREE(r);
479 MEMFREE(x0);
480 return;
481 }

  ViewVC Help
Powered by ViewVC 1.1.26