/[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 2803 - (show annotations)
Thu Dec 3 05:35:20 2009 UTC (9 years, 9 months ago) by artak
File MIME type: text/plain
File size: 21244 byte(s)
Parameters are added for post and pre smoothing. Now user can choose number of pre and post smoothing steps in AMG (e.g. setNumPreSweeps(5)). 
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 Paso_SparseMatrix_free(in->A);
62 if(in->coarsest_level==TRUE) {
63 #ifdef MKL
64 Paso_MKL_free1(in->AOffset1);
65 Paso_SparseMatrix_free(in->AOffset1);
66 #else
67 #ifdef UMFPACK
68 Paso_UMFPACK1_free((Paso_UMFPACK_Handler*)(in->solver));
69 #endif
70 #endif
71 }
72 MEMFREE(in->rows_in_F);
73 MEMFREE(in->rows_in_C);
74 MEMFREE(in->mask_F);
75 MEMFREE(in->mask_C);
76 MEMFREE(in->x_F);
77 MEMFREE(in->b_F);
78 MEMFREE(in->x_C);
79 MEMFREE(in->b_C);
80 in->solver=NULL;
81 Paso_Solver_AMG_free(in->AMG_of_Coarse);
82 MEMFREE(in->b_C);
83 MEMFREE(in);
84 }
85 }
86
87 /**************************************************************/
88
89 /* constructs the block-block factorization of
90
91 [ A_FF A_FC ]
92 A_p=
93 [ A_CF A_FF ]
94
95 to
96
97 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ]
98 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ]
99
100
101 where S=A_FF-ACF*invA_FF*A_FC within the shape of S
102
103 then AMG is applied to S again until S becomes empty
104
105 */
106 Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,dim_t level,Paso_Options* options) {
107 Paso_Solver_AMG* out=NULL;
108 /*
109 Paso_Pattern* temp1=NULL;
110 Paso_Pattern* temp2=NULL;
111 */
112 bool_t verbose=options->verbose;
113 dim_t n=A_p->numRows;
114 dim_t n_block=A_p->row_block_size;
115 index_t* mis_marker=NULL;
116 index_t* counter=NULL;
117 /*index_t iPtr,*index, *where_p;*/
118 dim_t i;
119 Paso_SparseMatrix * A_c=NULL;
120 double time0=0;
121 Paso_SparseMatrix * Atemp=NULL;
122 double sparsity=0;
123
124 /*
125 double *temp,*temp_1;
126 double S;
127 index_t iptr;
128 */
129 /*char filename[8];*/
130 /*sprintf(filename,"AMGLevel%d",level);
131
132 Paso_SparseMatrix_saveMM(A_p,filename);
133 */
134
135 /*Make sure we have block sizes 1*/
136 if (A_p->col_block_size>1) {
137 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires column block size 1.");
138 return NULL;
139 }
140 if (A_p->row_block_size>1) {
141 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires row block size 1.");
142 return NULL;
143 }
144 out=MEMALLOC(1,Paso_Solver_AMG);
145 /* identify independend set of rows/columns */
146 mis_marker=TMPMEMALLOC(n,index_t);
147 counter=TMPMEMALLOC(n,index_t);
148 if ( !( Paso_checkPtr(mis_marker) || Paso_checkPtr(counter) || Paso_checkPtr(out)) ) {
149 out->AMG_of_Coarse=NULL;
150 out->A_FF=NULL;
151 out->A_FC=NULL;
152 out->A_CF=NULL;
153 out->W_FC=NULL;
154 out->P=NULL;
155 out->R=NULL;
156 out->rows_in_F=NULL;
157 out->rows_in_C=NULL;
158 out->mask_F=NULL;
159 out->mask_C=NULL;
160 out->x_F=NULL;
161 out->b_F=NULL;
162 out->x_C=NULL;
163 out->b_C=NULL;
164 out->GS=NULL;
165 out->A=Paso_SparseMatrix_getReference(A_p);
166 out->GS=NULL;
167 out->solver=NULL;
168 /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
169 out->level=level;
170 out->n=n;
171 out->n_F=n+1;
172 out->n_block=n_block;
173 out->post_sweeps=options->post_sweeps;
174 out->pre_sweeps=options->pre_sweeps;
175
176 sparsity=(A_p->len*1.)/(1.*A_p->numRows*A_p->numCols);
177
178 if (verbose) fprintf(stdout,"Stats: Sparsity of the Coarse Matrix with %d non-zeros (%d,%d) in level %d is %.6f\n",A_p->len,A_p->numRows,A_p->numCols,level,sparsity);
179
180
181 /*if(sparsity>0.01) {
182 level=0;
183 }
184 */
185
186 if (level==0 || n<=options->min_coarse_matrix_size) {
187 out->coarsest_level=TRUE;
188 /*out->GS=Paso_Solver_getJacobi(A_p);*/
189
190 #ifdef MKL
191 out->AOffset1=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, out->A->pattern,1,1, FALSE);
192 #pragma omp parallel for private(i) schedule(static)
193 for (i=0;i<out->A->len;++i) {
194 out->AOffset1->val[i]=out->A->val[i];
195 }
196 #else
197 #ifdef UMFPACK
198 #else
199 out->GS=Paso_Solver_getJacobi(A_p);
200 #endif
201 #endif
202
203 } else {
204 out->coarsest_level=FALSE;
205 out->GS=Paso_Solver_getJacobi(A_p);
206
207 /* identify independend set of rows/columns */
208 #pragma omp parallel for private(i) schedule(static)
209 for (i=0;i<n;++i) mis_marker[i]=-1;
210
211 /*mesuring coarsening time */
212 time0=Paso_timer();
213
214 if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
215 Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);
216 }
217 else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
218 Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
219 }
220 else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
221 Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
222 }
223 else {
224 /*Default coarseneing*/
225 Paso_Pattern_RS_MI_Aggressive(A_p,mis_marker,options->coarsening_threshold);
226 /*Paso_Pattern_RS_MI(A_p,mis_marker,options->coarsening_threshold);*/
227 /*Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);*/
228 /*Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);*/
229 /*Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);*/
230
231 }
232
233 if (verbose) fprintf(stdout,"timing: Profilining for level %d:\n",level);
234
235 time0=Paso_timer()-time0;
236 if (verbose) fprintf(stdout,"timing: Coarsening: %e\n",time0);
237
238 #pragma omp parallel for private(i) schedule(static)
239 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
240
241 out->n_F=Paso_Util_cumsum(n,counter);
242
243 if (out->n_F==0) {
244 out->coarsest_level=TRUE;
245 level=0;
246 if (verbose) {
247 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
248 printf("AMG coarsening does not eliminate any of the unknowns, switching to Jacobi preconditioner.\n");
249 }
250 }
251 else if (out->n_F==n) {
252 out->coarsest_level=TRUE;
253 level=0;
254 if (verbose) {
255 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
256 printf("AMG coarsening eliminates all of the unknowns, switching to Jacobi preconditioner.\n");
257
258 }
259 } else {
260
261 if (Paso_noError()) {
262
263 /*#pragma omp parallel for private(i) schedule(static)
264 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
265 out->n_F=Paso_Util_cumsum(n,counter);
266 */
267
268 out->mask_F=MEMALLOC(n,index_t);
269 out->rows_in_F=MEMALLOC(out->n_F,index_t);
270 if (! (Paso_checkPtr(out->mask_F) || Paso_checkPtr(out->rows_in_F) ) ) {
271 /* creates an index for F from mask */
272 #pragma omp parallel for private(i) schedule(static)
273 for (i = 0; i < out->n_F; ++i) out->rows_in_F[i]=-1;
274 #pragma omp parallel for private(i) schedule(static)
275 for (i = 0; i < n; ++i) {
276 if (mis_marker[i]) {
277 out->rows_in_F[counter[i]]=i;
278 out->mask_F[i]=counter[i];
279 } else {
280 out->mask_F[i]=-1;
281 }
282 }
283
284 }
285 }
286
287 /*if(level==1) {
288 printf("##TOTAL: %d, ELIMINATED: %d\n",n,out->n_F);
289 for (i = 0; i < n; ++i) {
290 printf("##%d %d\n",i,mis_marker[i]);
291 }
292 }
293 */
294 /*check whether coarsening process actually makes sense to continue.
295 if coarse matrix at least smaller by 30% then continue, otherwise we stop.*/
296 if ((out->n_F*100/n)<30) {
297 level=1;
298 }
299
300 if ( Paso_noError()) {
301 out->n_C=n-out->n_F;
302 out->rows_in_C=MEMALLOC(out->n_C,index_t);
303 out->mask_C=MEMALLOC(n,index_t);
304 if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
305 /* creates an index for C from mask */
306 #pragma omp parallel for private(i) schedule(static)
307 for (i = 0; i < n; ++i) counter[i]=! mis_marker[i];
308 Paso_Util_cumsum(n,counter);
309 #pragma omp parallel for private(i) schedule(static)
310 for (i = 0; i < out->n_C; ++i) out->rows_in_C[i]=-1;
311 #pragma omp parallel for private(i) schedule(static)
312 for (i = 0; i < n; ++i) {
313 if (! mis_marker[i]) {
314 out->rows_in_C[counter[i]]=i;
315 out->mask_C[i]=counter[i];
316 } else {
317 out->mask_C[i]=-1;
318 }
319 }
320 }
321 }
322 if ( Paso_noError()) {
323 /* get A_FF block: */
324 /*
325 out->A_FF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_F,out->rows_in_F,out->mask_F);
326 out->A_CF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_F,out->rows_in_C,out->mask_F);
327 out->A_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
328 */
329
330 /*Compute W_FC*/
331 /*initialy W_FC=A_FC*/
332 out->W_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
333
334 /*sprintf(filename,"W_FCbefore_%d",level);
335 Paso_SparseMatrix_saveMM(out->W_FC,filename);
336 */
337 /* for (i = 0; i < n; ++i) {
338 printf("##mis_marker[%d]=%d\n",i,mis_marker[i]);
339 }
340 */
341 time0=Paso_timer();
342 Paso_SparseMatrix_updateWeights(A_p,out->W_FC,mis_marker);
343 time0=Paso_timer()-time0;
344 if (verbose) fprintf(stdout,"timing: updateWeights: %e\n",time0);
345
346
347 /*sprintf(filename,"W_FCafter_%d",level);
348 Paso_SparseMatrix_saveMM(out->W_FC,filename);
349 */
350
351 /* get Prolongation and Restriction */
352 time0=Paso_timer();
353 out->P=Paso_SparseMatrix_getProlongation(out->W_FC,mis_marker);
354 time0=Paso_timer()-time0;
355 if (verbose) fprintf(stdout,"timing: getProlongation: %e\n",time0);
356
357
358
359 /* sprintf(filename,"P_%d",level);
360 Paso_SparseMatrix_saveMM(out->P,filename);
361 */
362
363 time0=Paso_timer();
364 out->R=Paso_SparseMatrix_getRestriction(out->P);
365 time0=Paso_timer()-time0;
366 if (verbose) fprintf(stdout,"timing: getRestriction: %e\n",time0);
367
368
369 /*sprintf(filename,"R_%d",level);
370 Paso_SparseMatrix_saveMM(out->R,filename);
371 */
372
373 }
374 if ( Paso_noError()) {
375
376 time0=Paso_timer();
377
378 Atemp=Paso_SparseMatrix_MatrixMatrix(A_p,out->P);
379 A_c=Paso_SparseMatrix_MatrixMatrix(out->R,Atemp);
380
381 Paso_SparseMatrix_free(Atemp);
382
383 /*A_c=Paso_Solver_getCoarseMatrix(A_p,out->R,out->P);*/
384 time0=Paso_timer()-time0;
385 if (verbose) fprintf(stdout,"timing: getCoarseMatrix: %e\n",time0);
386
387
388 /*Paso_Solver_getCoarseMatrix(A_c, A_p,out->R,out->P);*/
389
390
391 /*sprintf(filename,"A_C_%d",level);
392 Paso_SparseMatrix_saveMM(A_c,filename);
393 */
394
395 out->AMG_of_Coarse=Paso_Solver_getAMG(A_c,level-1,options);
396 }
397
398 /* allocate work arrays for AMG application */
399 if (Paso_noError()) {
400 /*
401 out->x_F=MEMALLOC(n_block*out->n_F,double);
402 out->b_F=MEMALLOC(n_block*out->n_F,double);
403 */
404 out->x_C=MEMALLOC(n_block*out->n_C,double);
405 out->b_C=MEMALLOC(n_block*out->n_C,double);
406
407 /*if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {*/
408 if ( ! ( Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
409
410 /*
411 #pragma omp parallel for private(i) schedule(static)
412 for (i = 0; i < out->n_F; ++i) {
413 out->x_F[i]=0.;
414 out->b_F[i]=0.;
415 }
416 */
417
418 #pragma omp parallel for private(i) schedule(static)
419 for (i = 0; i < out->n_C; ++i) {
420 out->x_C[i]=0.;
421 out->b_C[i]=0.;
422 }
423 }
424 }
425 Paso_SparseMatrix_free(A_c);
426 }
427 }
428 }
429 TMPMEMFREE(mis_marker);
430 TMPMEMFREE(counter);
431
432 if (Paso_noError()) {
433 if (verbose && level>0 && !out->coarsest_level) {
434 printf("AMG: level: %d: %d unknowns eliminated. %d left.\n",level, out->n_F,out->n_C);
435 }
436 return out;
437 } else {
438 Paso_Solver_AMG_free(out);
439 return NULL;
440 }
441 }
442
443 /**************************************************************/
444
445 /* apply AMG precondition b-> x
446
447 in fact it solves
448
449 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FC ] [ x_F ] = [b_F]
450 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
451
452 in the form
453
454 b->[b_F,b_C]
455 x_F=invA_FF*b_F
456 b_C=b_C-A_CF*x_F
457 x_C=AMG(b_C)
458 b_F=b_F-A_FC*x_C
459 x_F=invA_FF*b_F
460 x<-[x_F,x_C]
461
462 should be called within a parallel region
463 barrier synconization should be performed to make sure that the input vector available
464
465 */
466
467 void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
468 dim_t i;
469 double time0=0;
470 double *r=NULL, *x0=NULL;
471 bool_t verbose=0;
472
473 dim_t post_sweeps=amg->post_sweeps;
474 dim_t pre_sweeps=amg->pre_sweeps;
475
476 #ifdef UMFPACK
477 Paso_UMFPACK_Handler * ptr=NULL;
478 #endif
479
480
481 r=MEMALLOC(amg->n,double);
482 x0=MEMALLOC(amg->n,double);
483
484 if (amg->coarsest_level) {
485
486 time0=Paso_timer();
487 /*If all unknown are eliminated then Jacobi is the best preconditioner*/
488 /*Paso_Solver_solveJacobi(amg->GS,x,b);*/
489
490 if (amg->n_F==0 || amg->n_F==amg->n) {
491 Paso_Solver_solveJacobi(amg->GS,x,b);
492 }
493 else {
494 #ifdef MKL
495 Paso_MKL1(amg->AOffset1,x,b,verbose);
496 #else
497 #ifdef UMFPACK
498 ptr=(Paso_UMFPACK_Handler *)(amg->solver);
499 Paso_UMFPACK1(&ptr,amg->A,x,b,verbose);
500 amg->solver=(void*) ptr;
501 #else
502 Paso_Solver_solveJacobi(amg->GS,x,b);
503 #endif
504 #endif
505 }
506
507 time0=Paso_timer()-time0;
508 if (verbose) fprintf(stdout,"timing: DIRECT SOLVER: %e\n",time0);
509
510 } else {
511 /* presmoothing */
512 time0=Paso_timer();
513 Paso_Solver_solveJacobi(amg->GS,x,b);
514
515 /***********/
516 #pragma omp parallel for private(i) schedule(static)
517 for (i=0;i<amg->n;++i) r[i]=b[i];
518
519 while(pre_sweeps>1) {
520 #pragma omp parallel for private(i) schedule(static)
521 for (i=0;i<amg->n;++i) r[i]+=b[i];
522
523 /* Compute the residual b=b-Ax*/
524 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
525 /* Go round again*/
526 Paso_Solver_solveJacobi(amg->GS,x,r);
527 pre_sweeps-=1;
528 }
529 /***********/
530
531 time0=Paso_timer()-time0;
532 if (verbose) fprintf(stdout,"timing: Presmooting: %e\n",time0);
533 /* end of presmoothing */
534
535 time0=Paso_timer();
536 #pragma omp parallel for private(i) schedule(static)
537 for (i=0;i<amg->n;++i) r[i]=b[i];
538
539 /*r=b-Ax*/
540 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
541
542 /* b_c <- R*r */
543 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,r,0.,amg->b_C);
544
545 time0=Paso_timer()-time0;
546 if (verbose) fprintf(stdout,"timing: Before next level: %e\n",time0);
547
548 /* x_C=AMG(b_C) */
549 Paso_Solver_solveAMG(amg->AMG_of_Coarse,amg->x_C,amg->b_C);
550
551 time0=Paso_timer();
552
553 /* x_0 <- P*x_c */
554 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,0.,x0);
555
556 /* x=x+x0 */
557 #pragma omp parallel for private(i) schedule(static)
558 for (i=0;i<amg->n;++i) x[i]+=x0[i];
559
560 /*postsmoothing*/
561
562 time0=Paso_timer();
563 #pragma omp parallel for private(i) schedule(static)
564 for (i=0;i<amg->n;++i) r[i]=b[i];
565
566 /*r=b-Ax */
567 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
568 Paso_Solver_solveJacobi(amg->GS,x0,r);
569
570
571 #pragma omp parallel for private(i) schedule(static)
572 for (i=0;i<amg->n;++i) {
573 x[i]+=x0[i];
574 }
575 /***************/
576 while(post_sweeps>1) {
577
578 #pragma omp parallel for private(i) schedule(static)
579 for (i=0;i<amg->n;++i) r[i]=b[i];
580
581 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
582 Paso_Solver_solveJacobi(amg->GS,x0,r);
583 #pragma omp parallel for private(i) schedule(static)
584 for (i=0;i<amg->n;++i) {
585 x[i]+=x0[i];
586 }
587 post_sweeps-=1;
588 }
589 /**************/
590
591 time0=Paso_timer()-time0;
592 if (verbose) fprintf(stdout,"timing: Postsmoothing: %e\n",time0);
593
594 /*end of postsmoothing*/
595
596 }
597 MEMFREE(r);
598 MEMFREE(x0);
599
600 return;
601 }

  ViewVC Help
Powered by ViewVC 1.1.26