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

  ViewVC Help
Powered by ViewVC 1.1.26