/[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 2760 - (show annotations)
Thu Nov 19 05:22:45 2009 UTC (10 years, 6 months ago) by artak
File MIME type: text/plain
File size: 17672 byte(s)
The first version of the new AMG preconditioner. It need a lot of polishing for efficiency. Old AMG now called AMLI preconditioner.
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_Jacobi_free(in->GS);
55 Paso_SparseMatrix_free(in->A_FC);
56 Paso_SparseMatrix_free(in->A_FF);
57 Paso_SparseMatrix_free(in->W_FC);
58 Paso_SparseMatrix_free(in->A_CF);
59 Paso_SparseMatrix_free(in->P);
60 Paso_SparseMatrix_free(in->R);
61
62 Paso_SparseMatrix_free(in->A);
63 if(in->coarsest_level==TRUE) {
64 #ifdef MKL
65 Paso_MKL_free1(in->AOffset1);
66 Paso_SparseMatrix_free(in->AOffset1);
67 #else
68 #ifdef UMFPACK
69 Paso_UMFPACK1_free((Paso_UMFPACK_Handler*)(in->solver));
70 #endif
71 #endif
72 }
73 MEMFREE(in->rows_in_F);
74 MEMFREE(in->rows_in_C);
75 MEMFREE(in->mask_F);
76 MEMFREE(in->mask_C);
77 MEMFREE(in->x_F);
78 MEMFREE(in->b_F);
79 MEMFREE(in->x_C);
80 MEMFREE(in->b_C);
81 in->solver=NULL;
82 Paso_Solver_AMG_free(in->AMG_of_Coarse);
83 MEMFREE(in->b_C);
84 MEMFREE(in);
85 }
86 }
87
88 /**************************************************************/
89
90 /* constructs the block-block factorization of
91
92 [ A_FF A_FC ]
93 A_p=
94 [ A_CF A_FF ]
95
96 to
97
98 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ]
99 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ]
100
101
102 where S=A_FF-ACF*invA_FF*A_FC within the shape of S
103
104 then AMG is applied to S again until S becomes empty
105
106 */
107 Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,dim_t level,Paso_Options* options) {
108 Paso_Solver_AMG* out=NULL;
109 /*
110 Paso_Pattern* temp1=NULL;
111 Paso_Pattern* temp2=NULL;
112 */
113 bool_t verbose=options->verbose;
114 dim_t n=A_p->numRows;
115 dim_t n_block=A_p->row_block_size;
116 index_t* mis_marker=NULL;
117 index_t* counter=NULL;
118 /*index_t iPtr,*index, *where_p;*/
119 dim_t i;
120 Paso_SparseMatrix * A_c=NULL;
121
122 /*
123 double *temp,*temp_1;
124 double S;
125 index_t iptr;
126 */
127
128 /*char filename[8];*/
129 /*sprintf(filename,"AMGLevel%d",level);
130
131 Paso_SparseMatrix_saveMM(A_p,filename);
132 */
133
134 /*Make sure we have block sizes 1*/
135 if (A_p->col_block_size>1) {
136 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires column block size 1.");
137 return NULL;
138 }
139 if (A_p->row_block_size>1) {
140 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires row block size 1.");
141 return NULL;
142 }
143 out=MEMALLOC(1,Paso_Solver_AMG);
144 /* identify independend set of rows/columns */
145 mis_marker=TMPMEMALLOC(n,index_t);
146 counter=TMPMEMALLOC(n,index_t);
147 if ( !( Paso_checkPtr(mis_marker) || Paso_checkPtr(counter) || Paso_checkPtr(out)) ) {
148 out->AMG_of_Coarse=NULL;
149 out->A_FF=NULL;
150 out->A_FC=NULL;
151 out->A_CF=NULL;
152 out->W_FC=NULL;
153 out->P=NULL;
154 out->R=NULL;
155 out->rows_in_F=NULL;
156 out->rows_in_C=NULL;
157 out->mask_F=NULL;
158 out->mask_C=NULL;
159 out->x_F=NULL;
160 out->b_F=NULL;
161 out->x_C=NULL;
162 out->b_C=NULL;
163 out->GS=NULL;
164 out->A=Paso_SparseMatrix_getReference(A_p);
165 out->GS=NULL;
166 out->solver=NULL;
167 /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
168 out->level=level;
169 out->n=n;
170 out->n_F=n+1;
171 out->n_block=n_block;
172
173 if (level==0 || n<=options->min_coarse_matrix_size) {
174 out->coarsest_level=TRUE;
175 #ifdef MKL
176 out->AOffset1=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, out->A->pattern,1,1, FALSE);
177 #pragma omp parallel for private(i) schedule(static)
178 for (i=0;i<out->A->len;++i) {
179 out->AOffset1->val[i]=out->A->val[i];
180 }
181 #else
182 #ifdef UMFPACK
183 #else
184 out->GS=Paso_Solver_getJacobi(A_p);
185 #endif
186 #endif
187 } else {
188 out->coarsest_level=FALSE;
189 out->GS=Paso_Solver_getJacobi(A_p);
190
191 /* identify independend set of rows/columns */
192 #pragma omp parallel for private(i) schedule(static)
193 for (i=0;i<n;++i) mis_marker[i]=-1;
194
195 if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
196 Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);
197 }
198 else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
199 Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
200 }
201 else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
202 Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
203 }
204 else {
205 /*Default coarseneing*/
206 Paso_Pattern_RS_MI(A_p,mis_marker,options->coarsening_threshold);
207 /*Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);*/
208 /*Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);*/
209 /*Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);*/
210
211 }
212
213 #pragma omp parallel for private(i) schedule(static)
214 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
215
216 out->n_F=Paso_Util_cumsum(n,counter);
217
218 if (out->n_F==0) {
219 out->coarsest_level=TRUE;
220 level=0;
221 if (verbose) {
222 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
223 printf("AMG coarsening does not eliminate any of the unknowns, switching to Jacobi preconditioner.\n");
224 }
225 }
226 else if (out->n_F==n) {
227 out->coarsest_level=TRUE;
228 level=0;
229 if (verbose) {
230 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
231 printf("AMG coarsening eliminates all of the unknowns, switching to Jacobi preconditioner.\n");
232
233 }
234 } else {
235
236 if (Paso_noError()) {
237
238 /*#pragma omp parallel for private(i) schedule(static)
239 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
240 out->n_F=Paso_Util_cumsum(n,counter);
241 */
242
243 out->mask_F=MEMALLOC(n,index_t);
244 out->rows_in_F=MEMALLOC(out->n_F,index_t);
245 if (! (Paso_checkPtr(out->mask_F) || Paso_checkPtr(out->rows_in_F) ) ) {
246 /* creates an index for F from mask */
247 #pragma omp parallel for private(i) schedule(static)
248 for (i = 0; i < out->n_F; ++i) out->rows_in_F[i]=-1;
249 #pragma omp parallel for private(i) schedule(static)
250 for (i = 0; i < n; ++i) {
251 if (mis_marker[i]) {
252 out->rows_in_F[counter[i]]=i;
253 out->mask_F[i]=counter[i];
254 } else {
255 out->mask_F[i]=-1;
256 }
257 }
258
259 }
260 }
261
262 /*check whether coarsening process actually makes sense to continue.
263 if coarse matrix at least smaller by 30% then continue, otherwise we stop.*/
264 if ((out->n_F*100/n)<30) {
265 level=1;
266 }
267
268 if ( Paso_noError()) {
269 /* if there are no nodes in the coarse level there is no more work to do */
270 out->n_C=n-out->n_F;
271
272 /*if (out->n_F>500) */
273 out->rows_in_C=MEMALLOC(out->n_C,index_t);
274 out->mask_C=MEMALLOC(n,index_t);
275 if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
276 /* creates an index for C from mask */
277 #pragma omp parallel for private(i) schedule(static)
278 for (i = 0; i < n; ++i) counter[i]=! mis_marker[i];
279 Paso_Util_cumsum(n,counter);
280 #pragma omp parallel for private(i) schedule(static)
281 for (i = 0; i < out->n_C; ++i) out->rows_in_C[i]=-1;
282 #pragma omp parallel for private(i) schedule(static)
283 for (i = 0; i < n; ++i) {
284 if (! mis_marker[i]) {
285 out->rows_in_C[counter[i]]=i;
286 out->mask_C[i]=counter[i];
287 } else {
288 out->mask_C[i]=-1;
289 }
290 }
291 }
292 }
293 if ( Paso_noError()) {
294 /* get A_FF block: */
295 /*
296 out->A_FF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_F,out->rows_in_F,out->mask_F);
297 out->A_CF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_F,out->rows_in_C,out->mask_F);
298 out->A_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
299 */
300
301 /*Compute W_FC*/
302 /*initialy W_FC=A_FC*/
303 out->W_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
304
305 /*sprintf(filename,"W_FCbefore_%d",level);
306 Paso_SparseMatrix_saveMM(out->W_FC,filename);
307 */
308 Paso_SparseMatrix_updateWeights(A_p,out->W_FC,mis_marker);
309
310 /*
311 printf("GOT W_FC, but n=%d,n_F=%d,n_C=%d\n",out->n,out->n_F,out->n_C);
312 sprintf(filename,"W_FCafter_%d",level);
313 Paso_SparseMatrix_saveMM(out->W_FC,filename);
314 */
315 /* get Prolongation and Restriction */
316 out->P=Paso_SparseMatrix_getProlongation(out->W_FC,mis_marker);
317
318 /*
319 printf("GOT Prolongation P->nxc %dx%d\n",out->P->numRows,out->P->numCols);
320 sprintf(filename,"P_%d",level);
321 Paso_SparseMatrix_saveMM(out->P,filename);
322 */
323
324 out->R=Paso_SparseMatrix_getRestriction(out->P);
325 /*
326 printf("GOT Restriction->cxn %dx%d\n",out->R->numRows,out->R->numCols);
327 sprintf(filename,"R_%d",level);
328 Paso_SparseMatrix_saveMM(out->R,filename);
329 */
330
331 }
332 if ( Paso_noError()) {
333 A_c=Paso_Solver_getCoarseMatrix(A_p,out->R,out->P);
334 /*Paso_Solver_getCoarseMatrix(A_c, A_p,out->R,out->P);*/
335 /*
336 sprintf(filename,"A_C_%d",level);
337 Paso_SparseMatrix_saveMM(A_c,filename);
338 */
339 out->AMG_of_Coarse=Paso_Solver_getAMG(A_c,level-1,options);
340 }
341
342 /* allocate work arrays for AMG application */
343 if (Paso_noError()) {
344 out->x_F=MEMALLOC(n_block*out->n_F,double);
345 out->b_F=MEMALLOC(n_block*out->n_F,double);
346 out->x_C=MEMALLOC(n_block*out->n_C,double);
347 out->b_C=MEMALLOC(n_block*out->n_C,double);
348
349 if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
350 #pragma omp parallel for private(i) schedule(static)
351 for (i = 0; i < out->n_F; ++i) {
352 out->x_F[i]=0.;
353 out->b_F[i]=0.;
354 }
355 #pragma omp parallel for private(i) schedule(static)
356 for (i = 0; i < out->n_C; ++i) {
357 out->x_C[i]=0.;
358 out->b_C[i]=0.;
359 }
360 }
361 }
362 Paso_SparseMatrix_free(A_c);
363 }
364 }
365 }
366 TMPMEMFREE(mis_marker);
367 TMPMEMFREE(counter);
368
369 if (Paso_noError()) {
370 if (verbose && level>0 && !out->coarsest_level) {
371 printf("AMG: level: %d: %d unknowns eliminated. %d left.\n",level, out->n_F,out->n_C);
372 }
373 return out;
374 } else {
375 Paso_Solver_AMG_free(out);
376 return NULL;
377 }
378 }
379
380 /**************************************************************/
381
382 /* apply AMG precondition b-> x
383
384 in fact it solves
385
386 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FC ] [ x_F ] = [b_F]
387 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
388
389 in the form
390
391 b->[b_F,b_C]
392 x_F=invA_FF*b_F
393 b_C=b_C-A_CF*x_F
394 x_C=AMG(b_C)
395 b_F=b_F-A_FC*x_C
396 x_F=invA_FF*b_F
397 x<-[x_F,x_C]
398
399 should be called within a parallel region
400 barrier synconization should be performed to make sure that the input vector available
401
402 */
403
404 void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
405 dim_t i;
406 double time0=0;
407 double *r=NULL, *x0=NULL;
408 bool_t verbose=0;
409 #ifdef UMFPACK
410 Paso_UMFPACK_Handler * ptr=NULL;
411 #endif
412
413 r=MEMALLOC(amg->n,double);
414 x0=MEMALLOC(amg->n,double);
415
416 if (amg->coarsest_level) {
417
418 time0=Paso_timer();
419 /*If all unknown are eliminated then Jacobi is the best preconditioner*/
420 if (amg->n_F==0 || amg->n_F==amg->n) {
421 Paso_Solver_solveJacobi(amg->GS,x,b);
422 }
423 else {
424 #ifdef MKL
425 Paso_MKL1(amg->AOffset1,x,b,verbose);
426 #else
427 #ifdef UMFPACK
428 ptr=(Paso_UMFPACK_Handler *)(amg->solver);
429 Paso_UMFPACK1(&ptr,amg->A,x,b,verbose);
430 amg->solver=(void*) ptr;
431 #else
432 Paso_Solver_solveJacobi(amg->GS,x,b);
433 #endif
434 #endif
435 }
436 time0=Paso_timer()-time0;
437 if (verbose) fprintf(stderr,"timing: DIRECT SOLVER: %e\n",time0);
438
439 } else {
440 /* presmoothing */
441 time0=Paso_timer();
442 Paso_Solver_solveJacobi(amg->GS,x,b);
443 time0=Paso_timer()-time0;
444 if (verbose) fprintf(stderr,"timing: Presmooting: %e\n",time0);
445 /* end of presmoothing */
446
447
448 time0=Paso_timer();
449 #pragma omp parallel for private(i) schedule(static)
450 for (i=0;i<amg->n;++i) r[i]=b[i];
451
452 /*r=b-Ax*/
453 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
454
455
456 /* b_c <- R*r */
457 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,r,0.,amg->b_C);
458
459 time0=Paso_timer()-time0;
460 if (verbose) fprintf(stderr,"timing: Before next level: %e\n",time0);
461
462 /* x_C=AMG(b_C) */
463 Paso_Solver_solveAMG(amg->AMG_of_Coarse,amg->x_C,amg->b_C);
464
465 time0=Paso_timer();
466
467 /* x_0 <- P*x_c */
468 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,0.,x0);
469
470 /* x=x+x0 */
471 #pragma omp parallel for private(i) schedule(static)
472 for (i=0;i<amg->n;++i) x[i]+=x0[i];
473
474 /*postsmoothing*/
475 time0=Paso_timer();
476 #pragma omp parallel for private(i) schedule(static)
477 for (i=0;i<amg->n;++i) r[i]=b[i];
478
479 /*r=b-Ax */
480 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
481 Paso_Solver_solveJacobi(amg->GS,x0,r);
482
483
484 #pragma omp parallel for private(i) schedule(static)
485 for (i=0;i<amg->n;++i) {
486 x[i]+=x0[i];
487 /*printf("x[%d]=%e \n",i,x[i]);*/
488 }
489
490 time0=Paso_timer()-time0;
491 if (verbose) fprintf(stderr,"timing: Postsmoothing: %e\n",time0);
492
493 /*end of postsmoothing*/
494
495 }
496 MEMFREE(r);
497 MEMFREE(x0);
498
499 return;
500 }

  ViewVC Help
Powered by ViewVC 1.1.26