/[escript]/trunk/paso/src/Solver_AMG.c
ViewVC logotype

Annotation of /trunk/paso/src/Solver_AMG.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2524 - (hide annotations)
Wed Jul 8 00:39:26 2009 UTC (10 years, 7 months ago) by artak
File MIME type: text/plain
File size: 15911 byte(s)
Minimum size of the coarsest level matrix option added to solver options
1 jfenwick 1851
2     /*******************************************************
3     *
4     * Copyright (c) 2003-2008 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 artak 2475 /* Paso: AMG preconditioner */
18 jfenwick 1851
19     /**************************************************************/
20    
21     /* Author: artak@access.edu.au */
22    
23     /**************************************************************/
24    
25     #include "Paso.h"
26     #include "Solver.h"
27 artak 2475 #include "Options.h"
28 jfenwick 1851 #include "PasoUtil.h"
29 artak 1931 #include "UMFPACK.h"
30 artak 2507 #include "MKL.h"
31     #include "SystemMatrix.h"
32 phornby 1917 #include "Pattern_coupling.h"
33 jfenwick 1851
34     /**************************************************************/
35    
36     /* free all memory used by AMG */
37    
38     void Paso_Solver_AMG_free(Paso_Solver_AMG * in) {
39     if (in!=NULL) {
40     Paso_Solver_AMG_free(in->AMG_of_Schur);
41 artak 2112 Paso_Solver_Jacobi_free(in->GS);
42 jfenwick 1851 MEMFREE(in->inv_A_FF);
43     MEMFREE(in->A_FF_pivot);
44     Paso_SparseMatrix_free(in->A_FC);
45     Paso_SparseMatrix_free(in->A_CF);
46     MEMFREE(in->rows_in_F);
47     MEMFREE(in->rows_in_C);
48     MEMFREE(in->mask_F);
49     MEMFREE(in->mask_C);
50     MEMFREE(in->x_F);
51     MEMFREE(in->b_F);
52     MEMFREE(in->x_C);
53     MEMFREE(in->b_C);
54     MEMFREE(in);
55     }
56     }
57    
58     /**************************************************************/
59    
60     /* constructs the block-block factorization of
61    
62     [ A_FF A_FC ]
63     A_p=
64     [ A_CF A_FF ]
65    
66     to
67    
68     [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FF ]
69     [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ]
70    
71    
72     where S=A_FF-ACF*invA_FF*A_FC within the shape of S
73    
74     then AMG is applied to S again until S becomes empty
75    
76     */
77 artak 2520 Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,dim_t level,Paso_Options* options) {
78 jfenwick 1851 Paso_Solver_AMG* out=NULL;
79 artak 2520 bool_t verbose=options->verbose;
80 jfenwick 1851 dim_t n=A_p->numRows;
81     dim_t n_block=A_p->row_block_size;
82     index_t* mis_marker=NULL;
83 artak 1881 index_t* counter=NULL;
84     index_t iPtr,*index, *where_p, iPtr_s;
85 artak 2107 dim_t i,k,j;
86 jfenwick 1851 Paso_SparseMatrix * schur=NULL;
87 artak 1881 Paso_SparseMatrix * schur_withFillIn=NULL;
88 artak 2381 double S=0;
89 artak 2475
90 artak 2439 /*Make sure we have block sizes 1*/
91     A_p->pattern->input_block_size=A_p->col_block_size;
92     A_p->pattern->output_block_size=A_p->row_block_size;
93     A_p->pattern->block_size=A_p->block_size;
94    
95 jfenwick 1851 /* identify independend set of rows/columns */
96     mis_marker=TMPMEMALLOC(n,index_t);
97     counter=TMPMEMALLOC(n,index_t);
98     out=MEMALLOC(1,Paso_Solver_AMG);
99     out->AMG_of_Schur=NULL;
100     out->inv_A_FF=NULL;
101     out->A_FF_pivot=NULL;
102     out->A_FC=NULL;
103     out->A_CF=NULL;
104     out->rows_in_F=NULL;
105     out->rows_in_C=NULL;
106     out->mask_F=NULL;
107     out->mask_C=NULL;
108     out->x_F=NULL;
109     out->b_F=NULL;
110     out->x_C=NULL;
111     out->b_C=NULL;
112 artak 2107 out->GS=NULL;
113 artak 1890 out->A=Paso_SparseMatrix_getReference(A_p);
114 artak 2112 /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
115     out->GS=Paso_Solver_getJacobi(A_p);
116 artak 1890 out->level=level;
117 artak 2524
118     if (level==0 || n<=options->min_coarse_matrix_size)
119     out->coarsest_level=TRUE;
120     else
121     out->coarsest_level=FALSE;
122 artak 2442
123 artak 2524 if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) || level==0 || n<=options->min_coarse_matrix_size) ) {
124 jfenwick 1851 /* identify independend set of rows/columns */
125     #pragma omp parallel for private(i) schedule(static)
126     for (i=0;i<n;++i) mis_marker[i]=-1;
127 artak 2475
128 artak 2520 if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
129     Paso_Pattern_coup(A_p,mis_marker,options->coarsening_threshold);
130 artak 2475 }
131 artak 2520 else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
132     Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
133 artak 2475 }
134 artak 2520 else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
135     Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
136 artak 2475 }
137     else {
138     /*Default coarseneing*/
139 artak 2520 Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
140 artak 2475 }
141 artak 2509
142 artak 2475 if (Paso_noError()) {
143 jfenwick 1851 #pragma omp parallel for private(i) schedule(static)
144     for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
145     out->n=n;
146     out->n_block=n_block;
147     out->n_F=Paso_Util_cumsum(n,counter);
148     out->mask_F=MEMALLOC(n,index_t);
149     out->rows_in_F=MEMALLOC(out->n_F,index_t);
150     out->inv_A_FF=MEMALLOC(n_block*n_block*out->n_F,double);
151     out->A_FF_pivot=NULL; /* later use for block size>3 */
152     if (! (Paso_checkPtr(out->mask_F) || Paso_checkPtr(out->inv_A_FF) || Paso_checkPtr(out->rows_in_F) ) ) {
153     /* creates an index for F from mask */
154 artak 2107 #pragma omp parallel for private(i) schedule(static)
155 jfenwick 1851 for (i = 0; i < out->n_F; ++i) out->rows_in_F[i]=-1;
156 artak 2107 #pragma omp parallel for private(i) schedule(static)
157 jfenwick 1851 for (i = 0; i < n; ++i) {
158     if (mis_marker[i]) {
159     out->rows_in_F[counter[i]]=i;
160     out->mask_F[i]=counter[i];
161     } else {
162     out->mask_F[i]=-1;
163     }
164     }
165 artak 2107
166     /* Compute row-sum for getting rs(A_FF)^-1*/
167 artak 2131 #pragma omp parallel for private(i,iPtr,j,S) schedule(static)
168 artak 1881 for (i = 0; i < out->n_F; ++i) {
169 artak 2107 S=0;
170 artak 1881 for (iPtr=A_p->pattern->ptr[out->rows_in_F[i]];iPtr<A_p->pattern->ptr[out->rows_in_F[i] + 1]; ++iPtr) {
171 artak 1975 j=A_p->pattern->index[iPtr];
172     if (mis_marker[j])
173 artak 2107 S+=A_p->val[iPtr];
174 artak 1881 }
175 artak 2442 out->inv_A_FF[i]=1./S;
176 artak 1881 }
177 jfenwick 1851
178     if( Paso_noError()) {
179     /* if there are no nodes in the coarse level there is no more work to do */
180     out->n_C=n-out->n_F;
181 artak 2524
182 artak 2280 /*if (out->n_F>500) {*/
183 jfenwick 1851 out->rows_in_C=MEMALLOC(out->n_C,index_t);
184     out->mask_C=MEMALLOC(n,index_t);
185     if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
186     /* creates an index for C from mask */
187     #pragma omp parallel for private(i) schedule(static)
188     for (i = 0; i < n; ++i) counter[i]=! mis_marker[i];
189     Paso_Util_cumsum(n,counter);
190 artak 2107 #pragma omp parallel for private(i) schedule(static)
191 jfenwick 1851 for (i = 0; i < out->n_C; ++i) out->rows_in_C[i]=-1;
192 artak 2107 #pragma omp parallel for private(i) schedule(static)
193 jfenwick 1851 for (i = 0; i < n; ++i) {
194     if (! mis_marker[i]) {
195     out->rows_in_C[counter[i]]=i;
196     out->mask_C[i]=counter[i];
197     } else {
198     out->mask_C[i]=-1;
199     }
200     }
201     /* get A_CF block: */
202     out->A_CF=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_F,out->rows_in_C,out->mask_F);
203     if (Paso_noError()) {
204     /* get A_FC block: */
205     out->A_FC=Paso_SparseMatrix_getSubmatrix(A_p,out->n_F,out->n_C,out->rows_in_F,out->mask_C);
206 artak 1881 /* get A_CC block: */
207 jfenwick 1851 if (Paso_noError()) {
208     schur=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_C,out->rows_in_C,out->mask_C);
209 artak 2107 /*find the pattern of the schur complement with fill in*/
210 artak 2507
211 artak 1881 schur_withFillIn=Paso_SparseMatrix_alloc(A_p->type,Paso_Pattern_binop(PATTERN_FORMAT_DEFAULT, schur->pattern, Paso_Pattern_multiply(PATTERN_FORMAT_DEFAULT,out->A_CF->pattern,out->A_FC->pattern)),1,1);
212     /* copy values over*/
213 artak 2107 #pragma omp parallel for private(i,iPtr,j,iPtr_s,index,where_p) schedule(static)
214 artak 1881 for (i = 0; i < schur_withFillIn->numRows; ++i) {
215     for (iPtr=schur_withFillIn->pattern->ptr[i];iPtr<schur_withFillIn->pattern->ptr[i + 1]; ++iPtr) {
216     j=schur_withFillIn->pattern->index[iPtr];
217 artak 2107 iPtr_s=schur->pattern->ptr[i];
218 artak 2507 schur_withFillIn->val[iPtr]=0.;
219 artak 2107 index=&(schur->pattern->index[iPtr_s]);
220     where_p=(index_t*)bsearch(&j,
221     index,
222     schur->pattern->ptr[i + 1]-schur->pattern->ptr[i],
223     sizeof(index_t),
224     Paso_comparIndex);
225     if (where_p!=NULL) {
226     schur_withFillIn->val[iPtr]=schur->val[iPtr_s+(index_t)(where_p-index)];
227 artak 1881 }
228     }
229 artak 2509 }
230 jfenwick 1851 if (Paso_noError()) {
231 artak 1881 Paso_Solver_updateIncompleteSchurComplement(schur_withFillIn,out->A_CF,out->inv_A_FF,out->A_FF_pivot,out->A_FC);
232 artak 2520 out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,level-1,options);
233 artak 2107 Paso_SparseMatrix_free(schur);
234 jfenwick 1851 }
235     /* allocate work arrays for AMG application */
236     if (Paso_noError()) {
237     out->x_F=MEMALLOC(n_block*out->n_F,double);
238     out->b_F=MEMALLOC(n_block*out->n_F,double);
239     out->x_C=MEMALLOC(n_block*out->n_C,double);
240     out->b_C=MEMALLOC(n_block*out->n_C,double);
241 artak 1890
242 jfenwick 1851 if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
243 artak 2107 #pragma omp parallel for private(i,k) schedule(static)
244     for (i = 0; i < out->n_F; ++i) {
245 jfenwick 1851 for (k=0; k<n_block;++k) {
246     out->x_F[i*n_block+k]=0.;
247     out->b_F[i*n_block+k]=0.;
248     }
249     }
250 artak 2107 #pragma omp parallel for private(i,k) schedule(static)
251 jfenwick 1851 for (i = 0; i < out->n_C; ++i) {
252     for (k=0; k<n_block;++k) {
253     out->x_C[i*n_block+k]=0.;
254     out->b_C[i*n_block+k]=0.;
255     }
256     }
257     }
258     }
259     }
260     }
261     }
262 artak 2524
263 jfenwick 1851 }
264     }
265     }
266     }
267     TMPMEMFREE(mis_marker);
268     TMPMEMFREE(counter);
269     if (Paso_noError()) {
270 artak 2524 if (verbose && level>0 && !out->coarsest_level) {
271 artak 2442 printf("AMG: %d unknowns eliminated. %d left.\n",out->n_F,out->n_C);
272 jfenwick 1851 }
273     return out;
274     } else {
275     Paso_Solver_AMG_free(out);
276     return NULL;
277     }
278     }
279    
280     /**************************************************************/
281    
282     /* apply AMG precondition b-> x
283    
284     in fact it solves
285    
286 artak 2381 [ I 0 ] [ A_FF 0 ] [ I invA_FF*A_FC ] [ x_F ] = [b_F]
287 jfenwick 1851 [ A_CF*invA_FF I ] [ 0 S ] [ 0 I ] [ x_C ] = [b_C]
288    
289     in the form
290    
291     b->[b_F,b_C]
292     x_F=invA_FF*b_F
293     b_C=b_C-A_CF*x_F
294     x_C=AMG(b_C)
295     b_F=b_F-A_FC*x_C
296     x_F=invA_FF*b_F
297     x<-[x_F,x_C]
298    
299     should be called within a parallel region
300     barrier synconization should be performed to make sure that the input vector available
301    
302     */
303    
304     void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
305 artak 2107 dim_t i;
306 artak 1890 double *r=MEMALLOC(amg->n,double);
307 artak 1954 double *x0=MEMALLOC(amg->n,double);
308 artak 2507 double time0=0;
309 artak 2509 bool_t verbose=0;
310 artak 2107
311 artak 2507 #ifdef MKL
312     Paso_SparseMatrix *temp=NULL;
313     #endif
314    
315    
316 artak 2524 if (amg->coarsest_level) {
317 artak 2507
318     time0=Paso_timer();
319     #ifdef MKL
320     temp=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, amg->A->pattern,1,1);
321     #pragma omp parallel for private(i) schedule(static)
322     for (i=0;i<amg->A->len;++i) {
323     temp->val[i]=amg->A->val[i];
324     }
325     Paso_MKL1(temp,x,b,0);
326     Paso_SparseMatrix_free(temp);
327     #else
328     #ifdef UMFPACK
329     Paso_UMFPACK1(amg->A,x,b,0);
330     #else
331     Paso_Solver_solveJacobi(amg->GS,x,b);
332     #endif
333     #endif
334    
335 artak 2475 time0=Paso_timer()-time0;
336 artak 2507 if (verbose) fprintf(stderr,"timing: DIRECT SOLVER: %e\n\n\n",time0);
337    
338 jfenwick 1851 } else {
339 artak 1902 /* presmoothing */
340 artak 2507 time0=Paso_timer();
341 artak 2112 Paso_Solver_solveJacobi(amg->GS,x,b);
342 artak 2507 time0=Paso_timer()-time0;
343     if (verbose) fprintf(stderr,"timing: Presmooting: %e\n",time0);
344 artak 1931 /* end of presmoothing */
345 artak 1939
346 artak 2507
347     time0=Paso_timer();
348 artak 1890 #pragma omp parallel for private(i) schedule(static)
349     for (i=0;i<amg->n;++i) r[i]=b[i];
350    
351     /*r=b-Ax*/
352     Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
353 artak 1954
354 jfenwick 1851 /* b->[b_F,b_C] */
355 artak 1954 #pragma omp parallel for private(i) schedule(static)
356     for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];
357    
358     #pragma omp parallel for private(i) schedule(static)
359     for (i=0;i<amg->n_C;++i) amg->b_C[i]=r[amg->rows_in_C[i]];
360 artak 1890
361 jfenwick 1851 /* x_F=invA_FF*b_F */
362 artak 1954 Paso_Solver_applyBlockDiagonalMatrix(1,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);
363 artak 1890
364 jfenwick 1851 /* b_C=b_C-A_CF*x_F */
365     Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_CF,amg->x_F,1.,amg->b_C);
366 artak 1890
367 artak 2507 time0=Paso_timer()-time0;
368     if (verbose) fprintf(stderr,"timing: Before next level: %e\n",time0);
369    
370 jfenwick 1851 /* x_C=AMG(b_C) */
371     Paso_Solver_solveAMG(amg->AMG_of_Schur,amg->x_C,amg->b_C);
372 artak 1890
373 artak 2507 time0=Paso_timer();
374 jfenwick 1851 /* b_F=b_F-A_FC*x_C */
375     Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_FC,amg->x_C,1.,amg->b_F);
376     /* x_F=invA_FF*b_F */
377 artak 1954 Paso_Solver_applyBlockDiagonalMatrix(1,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);
378 jfenwick 1851 /* x<-[x_F,x_C] */
379 artak 2113
380 artak 1954 #pragma omp parallel for private(i) schedule(static)
381     for (i=0;i<amg->n;++i) {
382     if (amg->mask_C[i]>-1) {
383 artak 2107 x[i]=amg->x_C[amg->mask_C[i]];
384 artak 1954 } else {
385 artak 2107 x[i]=amg->x_F[amg->mask_F[i]];
386 artak 1954 }
387 jfenwick 1851 }
388 artak 1954
389 artak 2507 time0=Paso_timer()-time0;
390     if (verbose) fprintf(stderr,"timing: After next level: %e\n",time0);
391    
392 artak 1931 /*postsmoothing*/
393 artak 2507 time0=Paso_timer();
394 artak 1954 #pragma omp parallel for private(i) schedule(static)
395     for (i=0;i<amg->n;++i) r[i]=b[i];
396 artak 2107
397 artak 2442 /*r=b-Ax */
398 artak 1954 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
399 artak 2112 Paso_Solver_solveJacobi(amg->GS,x0,r);
400 artak 2107
401     #pragma omp parallel for private(i) schedule(static)
402     for (i=0;i<amg->n;++i) x[i]+=x0[i];
403    
404 artak 2507 time0=Paso_timer()-time0;
405     if (verbose) fprintf(stderr,"timing: Postsmoothing: %e\n",time0);
406 artak 2509
407 artak 1931 /*end of postsmoothing*/
408    
409 jfenwick 1851 }
410 artak 1890 MEMFREE(r);
411 artak 1954 MEMFREE(x0);
412 artak 1931 return;
413 jfenwick 1851 }
414    
415     /*
416     * $Log$
417     *
418     */

  ViewVC Help
Powered by ViewVC 1.1.26