/[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 2784 - (show annotations)
Thu Nov 26 05:09:14 2009 UTC (10 years, 3 months ago) by artak
File MIME type: text/plain
File size: 19948 byte(s)
More performance tunning. Sparsity check is added. If Coarse level matrix is denser than 1% then stop.
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 verbose=1;
130 /*char filename[8];*/
131 /*sprintf(filename,"AMGLevel%d",level);
132
133 Paso_SparseMatrix_saveMM(A_p,filename);
134 */
135
136 /*Make sure we have block sizes 1*/
137 if (A_p->col_block_size>1) {
138 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires column block size 1.");
139 return NULL;
140 }
141 if (A_p->row_block_size>1) {
142 Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires row block size 1.");
143 return NULL;
144 }
145 out=MEMALLOC(1,Paso_Solver_AMG);
146 /* identify independend set of rows/columns */
147 mis_marker=TMPMEMALLOC(n,index_t);
148 counter=TMPMEMALLOC(n,index_t);
149 if ( !( Paso_checkPtr(mis_marker) || Paso_checkPtr(counter) || Paso_checkPtr(out)) ) {
150 out->AMG_of_Coarse=NULL;
151 out->A_FF=NULL;
152 out->A_FC=NULL;
153 out->A_CF=NULL;
154 out->W_FC=NULL;
155 out->P=NULL;
156 out->R=NULL;
157 out->rows_in_F=NULL;
158 out->rows_in_C=NULL;
159 out->mask_F=NULL;
160 out->mask_C=NULL;
161 out->x_F=NULL;
162 out->b_F=NULL;
163 out->x_C=NULL;
164 out->b_C=NULL;
165 out->GS=NULL;
166 out->A=Paso_SparseMatrix_getReference(A_p);
167 out->GS=NULL;
168 out->solver=NULL;
169 /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
170 out->level=level;
171 out->n=n;
172 out->n_F=n+1;
173 out->n_block=n_block;
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 if(sparsity>0.01) {
180 level=0;
181 }
182
183
184 if (level==0 || n<=options->min_coarse_matrix_size) {
185 out->coarsest_level=TRUE;
186 /*out->GS=Paso_Solver_getJacobi(A_p);*/
187
188 #ifdef MKL
189 out->AOffset1=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, out->A->pattern,1,1, FALSE);
190 #pragma omp parallel for private(i) schedule(static)
191 for (i=0;i<out->A->len;++i) {
192 out->AOffset1->val[i]=out->A->val[i];
193 }
194 #else
195 #ifdef UMFPACK
196 #else
197 out->GS=Paso_Solver_getJacobi(A_p);
198 #endif
199 #endif
200
201 } else {
202 out->coarsest_level=FALSE;
203 out->GS=Paso_Solver_getJacobi(A_p);
204
205 /* identify independend set of rows/columns */
206 #pragma omp parallel for private(i) schedule(static)
207 for (i=0;i<n;++i) mis_marker[i]=-1;
208
209 /*mesuring coarsening time */
210 time0=Paso_timer();
211
212 if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
213 Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);
214 }
215 else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
216 Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
217 }
218 else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
219 Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
220 }
221 else {
222 /*Default coarseneing*/
223 Paso_Pattern_RS_MI(A_p,mis_marker,options->coarsening_threshold);
224 /*Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);*/
225 /*Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);*/
226 /*Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);*/
227
228 }
229
230 if (verbose) fprintf(stdout,"timing: Profilining for level %d:\n",level);
231
232 time0=Paso_timer()-time0;
233 if (verbose) fprintf(stdout,"timing: Coarsening: %e\n",time0);
234
235 #pragma omp parallel for private(i) schedule(static)
236 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
237
238 out->n_F=Paso_Util_cumsum(n,counter);
239
240 if (out->n_F==0) {
241 out->coarsest_level=TRUE;
242 level=0;
243 if (verbose) {
244 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
245 printf("AMG coarsening does not eliminate any of the unknowns, switching to Jacobi preconditioner.\n");
246 }
247 }
248 else if (out->n_F==n) {
249 out->coarsest_level=TRUE;
250 level=0;
251 if (verbose) {
252 /*printf("AMG coarsening eliminates all unknowns, switching to Jacobi preconditioner.\n");*/
253 printf("AMG coarsening eliminates all of the unknowns, switching to Jacobi preconditioner.\n");
254
255 }
256 } else {
257
258 if (Paso_noError()) {
259
260 /*#pragma omp parallel for private(i) schedule(static)
261 for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
262 out->n_F=Paso_Util_cumsum(n,counter);
263 */
264
265 out->mask_F=MEMALLOC(n,index_t);
266 out->rows_in_F=MEMALLOC(out->n_F,index_t);
267 if (! (Paso_checkPtr(out->mask_F) || Paso_checkPtr(out->rows_in_F) ) ) {
268 /* creates an index for F from mask */
269 #pragma omp parallel for private(i) schedule(static)
270 for (i = 0; i < out->n_F; ++i) out->rows_in_F[i]=-1;
271 #pragma omp parallel for private(i) schedule(static)
272 for (i = 0; i < n; ++i) {
273 if (mis_marker[i]) {
274 out->rows_in_F[counter[i]]=i;
275 out->mask_F[i]=counter[i];
276 } else {
277 out->mask_F[i]=-1;
278 }
279 }
280
281 }
282 }
283
284 /*check whether coarsening process actually makes sense to continue.
285 if coarse matrix at least smaller by 30% then continue, otherwise we stop.*/
286 if ((out->n_F*100/n)<30) {
287 level=1;
288 }
289
290 if ( Paso_noError()) {
291 /* if there are no nodes in the coarse level there is no more work to do */
292 out->n_C=n-out->n_F;
293
294 /*if (out->n_F>500) */
295 out->rows_in_C=MEMALLOC(out->n_C,index_t);
296 out->mask_C=MEMALLOC(n,index_t);
297 if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
298 /* creates an index for C from mask */
299 #pragma omp parallel for private(i) schedule(static)
300 for (i = 0; i < n; ++i) counter[i]=! mis_marker[i];
301 Paso_Util_cumsum(n,counter);
302 #pragma omp parallel for private(i) schedule(static)
303 for (i = 0; i < out->n_C; ++i) out->rows_in_C[i]=-1;
304 #pragma omp parallel for private(i) schedule(static)
305 for (i = 0; i < n; ++i) {
306 if (! mis_marker[i]) {
307 out->rows_in_C[counter[i]]=i;
308 out->mask_C[i]=counter[i];
309 } else {
310 out->mask_C[i]=-1;
311 }
312 }
313 }
314 }
315 if ( Paso_noError()) {
316 /* get A_FF block: */
317 /*
318 out->A_FF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_F,out->rows_in_F,out->mask_F);
319 out->A_CF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_F,out->rows_in_C,out->mask_F);
320 out->A_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
321 */
322
323 /*Compute W_FC*/
324 /*initialy W_FC=A_FC*/
325 out->W_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
326
327 /*sprintf(filename,"W_FCbefore_%d",level);
328 Paso_SparseMatrix_saveMM(out->W_FC,filename);
329 */
330
331 time0=Paso_timer();
332 Paso_SparseMatrix_updateWeights(A_p,out->W_FC,mis_marker);
333 time0=Paso_timer()-time0;
334 if (verbose) fprintf(stdout,"timing: updateWeights: %e\n",time0);
335
336 /*
337 printf("GOT W_FC, but n=%d,n_F=%d,n_C=%d\n",out->n,out->n_F,out->n_C);
338 sprintf(filename,"W_FCafter_%d",level);
339 Paso_SparseMatrix_saveMM(out->W_FC,filename);
340 */
341 /* get Prolongation and Restriction */
342
343 time0=Paso_timer();
344 out->P=Paso_SparseMatrix_getProlongation(out->W_FC,mis_marker);
345 time0=Paso_timer()-time0;
346 if (verbose) fprintf(stdout,"timing: getProlongation: %e\n",time0);
347
348 /*
349 printf("GOT Prolongation P->nxc %dx%d\n",out->P->numRows,out->P->numCols);
350 sprintf(filename,"P_%d",level);
351 Paso_SparseMatrix_saveMM(out->P,filename);
352 */
353
354 time0=Paso_timer();
355 out->R=Paso_SparseMatrix_getRestriction(out->P);
356 time0=Paso_timer()-time0;
357 if (verbose) fprintf(stdout,"timing: getRestriction: %e\n",time0);
358
359 /*
360 printf("GOT Restriction->cxn %dx%d\n",out->R->numRows,out->R->numCols);
361 sprintf(filename,"R_%d",level);
362 Paso_SparseMatrix_saveMM(out->R,filename);
363 */
364
365 }
366 if ( Paso_noError()) {
367
368 time0=Paso_timer();
369
370 Atemp=Paso_SparseMatrix_MatrixMatrix(A_p,out->P);
371 A_c=Paso_SparseMatrix_MatrixMatrix(out->R,Atemp);
372
373 Paso_SparseMatrix_free(Atemp);
374
375 /*A_c=Paso_Solver_getCoarseMatrix(A_p,out->R,out->P);*/
376 time0=Paso_timer()-time0;
377 if (verbose) fprintf(stdout,"timing: getCoarseMatrix: %e\n",time0);
378
379
380 /*Paso_Solver_getCoarseMatrix(A_c, A_p,out->R,out->P);*/
381
382
383 /*sprintf(filename,"A_C_%d",level);
384 Paso_SparseMatrix_saveMM(A_c,filename);
385 */
386
387 out->AMG_of_Coarse=Paso_Solver_getAMG(A_c,level-1,options);
388 }
389
390 /* allocate work arrays for AMG application */
391 if (Paso_noError()) {
392 /*
393 out->x_F=MEMALLOC(n_block*out->n_F,double);
394 out->b_F=MEMALLOC(n_block*out->n_F,double);
395 */
396 out->x_C=MEMALLOC(n_block*out->n_C,double);
397 out->b_C=MEMALLOC(n_block*out->n_C,double);
398
399 /*if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {*/
400 if ( ! ( Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
401
402 /*
403 #pragma omp parallel for private(i) schedule(static)
404 for (i = 0; i < out->n_F; ++i) {
405 out->x_F[i]=0.;
406 out->b_F[i]=0.;
407 }
408 */
409
410 #pragma omp parallel for private(i) schedule(static)
411 for (i = 0; i < out->n_C; ++i) {
412 out->x_C[i]=0.;
413 out->b_C[i]=0.;
414 }
415 }
416 }
417 Paso_SparseMatrix_free(A_c);
418 }
419 }
420 }
421 TMPMEMFREE(mis_marker);
422 TMPMEMFREE(counter);
423
424 if (Paso_noError()) {
425 if (verbose && level>0 && !out->coarsest_level) {
426 printf("AMG: level: %d: %d unknowns eliminated. %d left.\n",level, out->n_F,out->n_C);
427 }
428 return out;
429 } else {
430 Paso_Solver_AMG_free(out);
431 return NULL;
432 }
433 }
434
435 /**************************************************************/
436
437 /* apply AMG precondition b-> x
438
439 in fact it solves
440
441 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FC ] [ x_F ] = [b_F]
442 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
443
444 in the form
445
446 b->[b_F,b_C]
447 x_F=invA_FF*b_F
448 b_C=b_C-A_CF*x_F
449 x_C=AMG(b_C)
450 b_F=b_F-A_FC*x_C
451 x_F=invA_FF*b_F
452 x<-[x_F,x_C]
453
454 should be called within a parallel region
455 barrier synconization should be performed to make sure that the input vector available
456
457 */
458
459 void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
460 dim_t i;
461 double time0=0;
462 double *r=NULL, *x0=NULL;
463 bool_t verbose=0;
464
465 #ifdef UMFPACK
466 Paso_UMFPACK_Handler * ptr=NULL;
467 #endif
468
469
470 r=MEMALLOC(amg->n,double);
471 x0=MEMALLOC(amg->n,double);
472
473 if (amg->coarsest_level) {
474
475 time0=Paso_timer();
476 /*If all unknown are eliminated then Jacobi is the best preconditioner*/
477 /*Paso_Solver_solveJacobi(amg->GS,x,b);*/
478
479 if (amg->n_F==0 || amg->n_F==amg->n) {
480 Paso_Solver_solveJacobi(amg->GS,x,b);
481 }
482 else {
483 #ifdef MKL
484 Paso_MKL1(amg->AOffset1,x,b,verbose);
485 #else
486 #ifdef UMFPACK
487 ptr=(Paso_UMFPACK_Handler *)(amg->solver);
488 Paso_UMFPACK1(&ptr,amg->A,x,b,verbose);
489 amg->solver=(void*) ptr;
490 #else
491 Paso_Solver_solveJacobi(amg->GS,x,b);
492 #endif
493 #endif
494 }
495
496 time0=Paso_timer()-time0;
497 if (verbose) fprintf(stdout,"timing: DIRECT SOLVER: %e\n",time0);
498
499 } else {
500 /* presmoothing */
501 time0=Paso_timer();
502 Paso_Solver_solveJacobi(amg->GS,x,b);
503 time0=Paso_timer()-time0;
504 if (verbose) fprintf(stdout,"timing: Presmooting: %e\n",time0);
505 /* end of presmoothing */
506
507
508 time0=Paso_timer();
509 #pragma omp parallel for private(i) schedule(static)
510 for (i=0;i<amg->n;++i) r[i]=b[i];
511
512 /*r=b-Ax*/
513 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
514
515
516 /* b_c <- R*r */
517 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,r,0.,amg->b_C);
518
519 time0=Paso_timer()-time0;
520 if (verbose) fprintf(stdout,"timing: Before next level: %e\n",time0);
521
522 /* x_C=AMG(b_C) */
523 Paso_Solver_solveAMG(amg->AMG_of_Coarse,amg->x_C,amg->b_C);
524
525 time0=Paso_timer();
526
527 /* x_0 <- P*x_c */
528 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,0.,x0);
529
530 /* x=x+x0 */
531 #pragma omp parallel for private(i) schedule(static)
532 for (i=0;i<amg->n;++i) x[i]+=x0[i];
533
534 /*postsmoothing*/
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 Paso_Solver_solveJacobi(amg->GS,x0,r);
542
543
544 #pragma omp parallel for private(i) schedule(static)
545 for (i=0;i<amg->n;++i) {
546 x[i]+=x0[i];
547 /*printf("x[%d]=%e \n",i,x[i]);*/
548 }
549
550 time0=Paso_timer()-time0;
551 if (verbose) fprintf(stdout,"timing: Postsmoothing: %e\n",time0);
552
553 /*end of postsmoothing*/
554
555 }
556 MEMFREE(r);
557 MEMFREE(x0);
558
559 return;
560 }

  ViewVC Help
Powered by ViewVC 1.1.26