/[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 2816 - (show annotations)
Thu Dec 10 02:09:43 2009 UTC (10 years, 2 months ago) by artak
File MIME type: text/plain
File size: 21516 byte(s)
Standard coarsening is added to options list.
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->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 out->post_sweeps=options->post_sweeps;
173 out->pre_sweeps=options->pre_sweeps;
174
175 sparsity=(A_p->len*1.)/(1.*A_p->numRows*A_p->numCols);
176
177 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);
178
179
180 /*if(sparsity>0.01) {
181 level=0;
182 }
183 */
184
185 if (level==0 || n<=options->min_coarse_matrix_size) {
186 out->coarsest_level=TRUE;
187 /*out->GS=Paso_Solver_getJacobi(A_p);*/
188
189 #ifdef MKL
190 out->AOffset1=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, out->A->pattern,1,1, FALSE);
191 #pragma omp parallel for private(i) schedule(static)
192 for (i=0;i<out->A->len;++i) {
193 out->AOffset1->val[i]=out->A->val[i];
194 }
195 #else
196 #ifdef UMFPACK
197 #else
198 out->GS=Paso_Solver_getJacobi(A_p);
199 #endif
200 #endif
201
202 } else {
203 out->coarsest_level=FALSE;
204 out->GS=Paso_Solver_getJacobi(A_p);
205
206 /* identify independend set of rows/columns */
207 #pragma omp parallel for private(i) schedule(static)
208 for (i=0;i<n;++i) mis_marker[i]=-1;
209
210 /*mesuring coarsening time */
211 time0=Paso_timer();
212
213 if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
214 Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);
215 }
216 else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
217 Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
218 }
219 else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
220 Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
221 }
222 else if (options->coarsening_method == PASO_STANDARD_COARSENING) {
223 Paso_Pattern_Standard(A_p,mis_marker,options->coarsening_threshold);
224 }
225 else {
226 /*Default coarseneing*/
227 /*Paso_Pattern_RS_MI_Aggressive(A_p,mis_marker,options->coarsening_threshold);*/
228 Paso_Pattern_Standard(A_p,mis_marker,options->coarsening_threshold);
229 /*Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);*/
230 /*Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);*/
231 /*Paso_Pattern_greedy(A_p->pattern,mis_marker);*/
232 /*Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);*/
233
234 }
235
236 if (verbose) fprintf(stdout,"timing: Profilining for level %d:\n",level);
237
238 time0=Paso_timer()-time0;
239 if (verbose) fprintf(stdout,"timing: Coarsening: %e\n",time0);
240
241 #pragma omp parallel for private(i) schedule(static)
242 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
243
244 out->n_F=Paso_Util_cumsum(n,counter);
245
246 if (out->n_F==0) {
247 out->coarsest_level=TRUE;
248 level=0;
249 if (verbose) {
250 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
251 printf("AMG coarsening does not eliminate any of the unknowns, switching to Jacobi preconditioner.\n");
252 }
253 }
254 else if (out->n_F==n) {
255 out->coarsest_level=TRUE;
256 level=0;
257 if (verbose) {
258 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
259 printf("AMG coarsening eliminates all of the unknowns, switching to Jacobi preconditioner.\n");
260
261 }
262 } else {
263
264 if (Paso_noError()) {
265
266 /*#pragma omp parallel for private(i) schedule(static)
267 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
268 out->n_F=Paso_Util_cumsum(n,counter);
269 */
270
271 out->mask_F=MEMALLOC(n,index_t);
272 out->rows_in_F=MEMALLOC(out->n_F,index_t);
273 if (! (Paso_checkPtr(out->mask_F) || Paso_checkPtr(out->rows_in_F) ) ) {
274 /* creates an index for F from mask */
275 #pragma omp parallel for private(i) schedule(static)
276 for (i = 0; i < out->n_F; ++i) out->rows_in_F[i]=-1;
277 #pragma omp parallel for private(i) schedule(static)
278 for (i = 0; i < n; ++i) {
279 if (mis_marker[i]) {
280 out->rows_in_F[counter[i]]=i;
281 out->mask_F[i]=counter[i];
282 } else {
283 out->mask_F[i]=-1;
284 }
285 }
286
287 }
288 }
289
290 /* if(level==1) {
291 printf("##TOTAL: %d, ELIMINATED: %d\n",n,out->n_F);
292 for (i = 0; i < n; ++i) {
293 printf("##%d %d\n",i,mis_marker[i]);
294 }
295 }
296 */
297
298 /*check whether coarsening process actually makes sense to continue.
299 if coarse matrix at least smaller by 30% then continue, otherwise we stop.*/
300 if ((out->n_F*100/n)<30) {
301 level=1;
302 }
303
304 if ( Paso_noError()) {
305 out->n_C=n-out->n_F;
306 out->rows_in_C=MEMALLOC(out->n_C,index_t);
307 out->mask_C=MEMALLOC(n,index_t);
308 if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
309 /* creates an index for C from mask */
310 #pragma omp parallel for private(i) schedule(static)
311 for (i = 0; i < n; ++i) counter[i]=! mis_marker[i];
312 Paso_Util_cumsum(n,counter);
313 #pragma omp parallel for private(i) schedule(static)
314 for (i = 0; i < out->n_C; ++i) out->rows_in_C[i]=-1;
315 #pragma omp parallel for private(i) schedule(static)
316 for (i = 0; i < n; ++i) {
317 if (! mis_marker[i]) {
318 out->rows_in_C[counter[i]]=i;
319 out->mask_C[i]=counter[i];
320 } else {
321 out->mask_C[i]=-1;
322 }
323 }
324 }
325 }
326 if ( Paso_noError()) {
327 /* get A_FF block: */
328 /*
329 out->A_FF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_F,out->rows_in_F,out->mask_F);
330 out->A_CF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_F,out->rows_in_C,out->mask_F);
331 out->A_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
332 */
333
334 /*Compute W_FC*/
335 /*initialy W_FC=A_FC*/
336 out->W_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
337
338 /*sprintf(filename,"W_FCbefore_%d",level);
339 Paso_SparseMatrix_saveMM(out->W_FC,filename);
340 */
341 /* for (i = 0; i < n; ++i) {
342 printf("##mis_marker[%d]=%d\n",i,mis_marker[i]);
343 }
344 */
345 time0=Paso_timer();
346 Paso_SparseMatrix_updateWeights(A_p,out->W_FC,mis_marker);
347 time0=Paso_timer()-time0;
348 if (verbose) fprintf(stdout,"timing: updateWeights: %e\n",time0);
349
350
351 /*sprintf(filename,"W_FCafter_%d",level);
352 Paso_SparseMatrix_saveMM(out->W_FC,filename);
353 */
354
355 /* get Prolongation and Restriction */
356 time0=Paso_timer();
357 out->P=Paso_SparseMatrix_getProlongation(out->W_FC,mis_marker);
358 time0=Paso_timer()-time0;
359 if (verbose) fprintf(stdout,"timing: getProlongation: %e\n",time0);
360
361
362
363 /* sprintf(filename,"P_%d",level);
364 Paso_SparseMatrix_saveMM(out->P,filename);
365 */
366
367 time0=Paso_timer();
368 out->R=Paso_SparseMatrix_getRestriction(out->P);
369 time0=Paso_timer()-time0;
370 if (verbose) fprintf(stdout,"timing: getRestriction: %e\n",time0);
371
372
373 /*sprintf(filename,"R_%d",level);
374 Paso_SparseMatrix_saveMM(out->R,filename);
375 */
376
377 }
378 if ( Paso_noError()) {
379
380 time0=Paso_timer();
381
382 Atemp=Paso_SparseMatrix_MatrixMatrix(A_p,out->P);
383 A_c=Paso_SparseMatrix_MatrixMatrix(out->R,Atemp);
384
385 Paso_SparseMatrix_free(Atemp);
386
387 /*A_c=Paso_Solver_getCoarseMatrix(A_p,out->R,out->P);*/
388 time0=Paso_timer()-time0;
389 if (verbose) fprintf(stdout,"timing: getCoarseMatrix: %e\n",time0);
390
391
392 /*Paso_Solver_getCoarseMatrix(A_c, A_p,out->R,out->P);*/
393
394
395 /*sprintf(filename,"A_C_%d",level);
396 Paso_SparseMatrix_saveMM(A_c,filename);
397 */
398
399 out->AMG_of_Coarse=Paso_Solver_getAMG(A_c,level-1,options);
400 }
401
402 /* allocate work arrays for AMG application */
403 if (Paso_noError()) {
404 /*
405 out->x_F=MEMALLOC(n_block*out->n_F,double);
406 out->b_F=MEMALLOC(n_block*out->n_F,double);
407 */
408 out->x_C=MEMALLOC(n_block*out->n_C,double);
409 out->b_C=MEMALLOC(n_block*out->n_C,double);
410
411 /*if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {*/
412 if ( ! ( Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
413
414 /*
415 #pragma omp parallel for private(i) schedule(static)
416 for (i = 0; i < out->n_F; ++i) {
417 out->x_F[i]=0.;
418 out->b_F[i]=0.;
419 }
420 */
421
422 #pragma omp parallel for private(i) schedule(static)
423 for (i = 0; i < out->n_C; ++i) {
424 out->x_C[i]=0.;
425 out->b_C[i]=0.;
426 }
427 }
428 }
429 Paso_SparseMatrix_free(A_c);
430 }
431 }
432 }
433 TMPMEMFREE(mis_marker);
434 TMPMEMFREE(counter);
435
436 if (Paso_noError()) {
437 if (verbose && level>0 && !out->coarsest_level) {
438 printf("AMG: level: %d: %d unknowns eliminated. %d left.\n",level, out->n_F,out->n_C);
439 }
440 return out;
441 } else {
442 Paso_Solver_AMG_free(out);
443 return NULL;
444 }
445 }
446
447 /**************************************************************/
448
449 /* apply AMG precondition b-> x
450
451 in fact it solves
452
453 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FC ] [ x_F ] = [b_F]
454 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
455
456 in the form
457
458 b->[b_F,b_C]
459 x_F=invA_FF*b_F
460 b_C=b_C-A_CF*x_F
461 x_C=AMG(b_C)
462 b_F=b_F-A_FC*x_C
463 x_F=invA_FF*b_F
464 x<-[x_F,x_C]
465
466 should be called within a parallel region
467 barrier synconization should be performed to make sure that the input vector available
468
469 */
470
471 void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
472 dim_t i;
473 double time0=0;
474 double *r=NULL, *x0=NULL;
475 bool_t verbose=0;
476
477 dim_t post_sweeps=amg->post_sweeps;
478 dim_t pre_sweeps=amg->pre_sweeps;
479
480 #ifdef UMFPACK
481 Paso_UMFPACK_Handler * ptr=NULL;
482 #endif
483
484
485 r=MEMALLOC(amg->n,double);
486 x0=MEMALLOC(amg->n,double);
487
488 if (amg->coarsest_level) {
489
490 time0=Paso_timer();
491 /*If all unknown are eliminated then Jacobi is the best preconditioner*/
492 /*Paso_Solver_solveJacobi(amg->GS,x,b);*/
493
494 if (amg->n_F==0 || amg->n_F==amg->n) {
495 Paso_Solver_solveJacobi(amg->GS,x,b);
496 }
497 else {
498 #ifdef MKL
499 Paso_MKL1(amg->AOffset1,x,b,verbose);
500 #else
501 #ifdef UMFPACK
502 ptr=(Paso_UMFPACK_Handler *)(amg->solver);
503 Paso_UMFPACK1(&ptr,amg->A,x,b,verbose);
504 amg->solver=(void*) ptr;
505 #else
506 Paso_Solver_solveJacobi(amg->GS,x,b);
507 #endif
508 #endif
509 }
510
511 time0=Paso_timer()-time0;
512 if (verbose) fprintf(stdout,"timing: DIRECT SOLVER: %e\n",time0);
513
514 } else {
515 /* presmoothing */
516 time0=Paso_timer();
517 Paso_Solver_solveJacobi(amg->GS,x,b);
518
519 /***********/
520 if (pre_sweeps>1) {
521 #pragma omp parallel for private(i) schedule(static)
522 for (i=0;i<amg->n;++i) r[i]=b[i];
523 }
524
525 while(pre_sweeps>1) {
526 #pragma omp parallel for private(i) schedule(static)
527 for (i=0;i<amg->n;++i) r[i]+=b[i];
528
529 /* Compute the residual r=r-Ax*/
530 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
531 /* Go round again*/
532 Paso_Solver_solveJacobi(amg->GS,x,r);
533 pre_sweeps-=1;
534 }
535 /***********/
536
537 time0=Paso_timer()-time0;
538 if (verbose) fprintf(stdout,"timing: Presmooting: %e\n",time0);
539 /* end of presmoothing */
540
541 time0=Paso_timer();
542 #pragma omp parallel for private(i) schedule(static)
543 for (i=0;i<amg->n;++i) r[i]=b[i];
544
545 /*r=b-Ax*/
546 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
547
548 /* b_c <- R*r */
549 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,r,0.,amg->b_C);
550
551 time0=Paso_timer()-time0;
552 if (verbose) fprintf(stdout,"timing: Before next level: %e\n",time0);
553
554 /* x_C=AMG(b_C) */
555 Paso_Solver_solveAMG(amg->AMG_of_Coarse,amg->x_C,amg->b_C);
556
557 time0=Paso_timer();
558
559 /* x_0 <- P*x_c */
560 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,0.,x0);
561
562 /* x=x+x0 */
563 #pragma omp parallel for private(i) schedule(static)
564 for (i=0;i<amg->n;++i) x[i]+=x0[i];
565
566 /*postsmoothing*/
567
568 time0=Paso_timer();
569 #pragma omp parallel for private(i) schedule(static)
570 for (i=0;i<amg->n;++i) r[i]=b[i];
571
572 /*r=b-Ax */
573 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
574 Paso_Solver_solveJacobi(amg->GS,x0,r);
575
576
577 #pragma omp parallel for private(i) schedule(static)
578 for (i=0;i<amg->n;++i) {
579 x[i]+=x0[i];
580 }
581 /***************/
582 while(post_sweeps>1) {
583
584 #pragma omp parallel for private(i) schedule(static)
585 for (i=0;i<amg->n;++i) r[i]=b[i];
586
587 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
588 Paso_Solver_solveJacobi(amg->GS,x0,r);
589 #pragma omp parallel for private(i) schedule(static)
590 for (i=0;i<amg->n;++i) {
591 x[i]+=x0[i];
592 }
593 post_sweeps-=1;
594 }
595 /**************/
596
597 time0=Paso_timer()-time0;
598 if (verbose) fprintf(stdout,"timing: Postsmoothing: %e\n",time0);
599
600 /*end of postsmoothing*/
601
602 }
603 MEMFREE(r);
604 MEMFREE(x0);
605
606 return;
607 }

  ViewVC Help
Powered by ViewVC 1.1.26