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

  ViewVC Help
Powered by ViewVC 1.1.26