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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3312 - (hide annotations)
Tue Oct 26 07:54:58 2010 UTC (8 years, 11 months ago) by gross
File MIME type: text/plain
File size: 16072 byte(s)
last step for a clean up version of the AMG
1 gross 3193
2     /*******************************************************
3     *
4     * Copyright (c) 2003-2010 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 gross 3303 /* Author: artak@uq.edu.au, l.gross@uq.edu.au */
22 gross 3193
23     /**************************************************************/
24    
25 gross 3283 #define SHOW_TIMING FALSE
26    
27 gross 3193 #include "Paso.h"
28     #include "Preconditioner.h"
29     #include "Options.h"
30     #include "PasoUtil.h"
31     #include "UMFPACK.h"
32     #include "MKL.h"
33    
34     /**************************************************************/
35    
36     /* free all memory used by AMG */
37    
38     void Paso_Preconditioner_LocalAMG_free(Paso_Preconditioner_LocalAMG * in) {
39     if (in!=NULL) {
40     Paso_Preconditioner_LocalSmoother_free(in->Smoother);
41 gross 3283 Paso_SparseMatrix_free(in->P);
42     Paso_SparseMatrix_free(in->R);
43     Paso_SparseMatrix_free(in->A_C);
44     Paso_Preconditioner_LocalAMG_free(in->AMG_C);
45     MEMFREE(in->r);
46     MEMFREE(in->x_C);
47     MEMFREE(in->b_C);
48 gross 3193
49 gross 3283
50     MEMFREE(in);
51 gross 3193 }
52     }
53    
54 gross 3283 /*****************************************************************
55 gross 3193
56 gross 3283 constructs AMG
57    
58     ******************************************************************/
59     Paso_Preconditioner_LocalAMG* Paso_Preconditioner_LocalAMG_alloc(Paso_SparseMatrix *A_p,dim_t level,Paso_Options* options) {
60 gross 3193
61     Paso_Preconditioner_LocalAMG* out=NULL;
62     bool_t verbose=options->verbose;
63    
64 gross 3303 Paso_SparseMatrix *Atemp=NULL, *A_C=NULL;
65 gross 3193 const dim_t n=A_p->numRows;
66     const dim_t n_block=A_p->row_block_size;
67 gross 3303 index_t* split_marker=NULL, *counter=NULL, *mask_C=NULL, *rows_in_F=NULL, *S=NULL, *degree=NULL;
68 gross 3283 dim_t n_F=0, n_C=0, i;
69     double time0=0;
70     const double theta = options->coarsening_threshold;
71     const double tau = options->diagonal_dominance_threshold;
72 gross 3193
73    
74     /*
75 gross 3283 is the input matrix A suitable for coarsening
76    
77 gross 3193 */
78 gross 3283 if ( (A_p->pattern->len >= options->min_coarse_sparsity * n * n ) || (n <= options->min_coarse_matrix_size) || (level > options->level_max) ) {
79     if (verbose) printf("Paso: AMG level %d (limit = %d) stopped. sparsity = %e (limit = %e), unknowns = %d (limit = %d)\n",
80     level, options->level_max, A_p->pattern->len/(1.*n * n), options->min_coarse_sparsity, n, options->min_coarse_matrix_size );
81 gross 3193 return NULL;
82 gross 3283 }
83     /* Start Coarsening : */
84 gross 3193
85 gross 3283 split_marker=TMPMEMALLOC(n,index_t);
86     counter=TMPMEMALLOC(n,index_t);
87 gross 3303 degree=TMPMEMALLOC(n, dim_t);
88     S=TMPMEMALLOC(A_p->pattern->len, index_t);
89     if ( !( Esys_checkPtr(split_marker) || Esys_checkPtr(counter) || Esys_checkPtr(degree) || Esys_checkPtr(S) ) ) {
90 gross 3283 /*
91     set splitting of unknows:
92    
93     */
94     time0=Esys_timer();
95     if (n_block>1) {
96 gross 3303 Paso_Preconditioner_AMG_setStrongConnections_Block(A_p, degree, S, theta,tau);
97 gross 3283 } else {
98 gross 3303 Paso_Preconditioner_AMG_setStrongConnections(A_p, degree, S, theta,tau);
99 gross 3283 }
100 gross 3303 Paso_Preconditioner_AMG_RungeStuebenSearch(n, A_p->pattern->ptr, degree, S, split_marker);
101 gross 3283 options->coarsening_selection_time=Esys_timer()-time0 + MAX(0, options->coarsening_selection_time);
102    
103     if (Esys_noError() ) {
104     #pragma omp parallel for private(i) schedule(static)
105     for (i = 0; i < n; ++i) split_marker[i]= (split_marker[i] == PASO_AMG_IN_F);
106    
107     /*
108     count number of unkowns to be eliminated:
109     */
110     n_F=Paso_Util_cumsum_maskedTrue(n,counter, split_marker);
111     n_C=n-n_F;
112 gross 3312 if (verbose) printf("Paso: AMG level %d: %d unknowns are flagged for elimination. %d left.\n",level,n_F,n-n_F);
113 gross 3283
114     if ( n_F == 0 ) { /* is a nasty case. a direct solver should be used, return NULL */
115     out = NULL;
116     } else {
117     out=MEMALLOC(1,Paso_Preconditioner_LocalAMG);
118     mask_C=TMPMEMALLOC(n,index_t);
119     rows_in_F=TMPMEMALLOC(n_F,index_t);
120     if ( !( Esys_checkPtr(mask_C) || Esys_checkPtr(rows_in_F) || Esys_checkPtr(out)) ) {
121     out->level = level;
122     out->n = n;
123     out->n_F = n_F;
124     out->n_block = n_block;
125     out->A_C = NULL;
126     out->P = NULL;
127     out->R = NULL;
128     out->post_sweeps = options->post_sweeps;
129     out->pre_sweeps = options->pre_sweeps;
130     out->r = NULL;
131     out->x_C = NULL;
132     out->b_C = NULL;
133     out->AMG_C = NULL;
134     out->Smoother = Paso_Preconditioner_LocalSmoother_alloc(A_p, (options->smoother == PASO_JACOBI), verbose);
135    
136     if ( n_F < n ) { /* if nothing is been removed we have a diagonal dominant matrix and we just run a few steps of the smoother */
137    
138     /* creates index for F:*/
139     #pragma omp parallel for private(i) schedule(static)
140     for (i = 0; i < n; ++i) {
141     if (split_marker[i]) rows_in_F[counter[i]]=i;
142     }
143     /* create mask of C nodes with value >-1 gives new id */
144     i=Paso_Util_cumsum_maskedFalse(n,counter, split_marker);
145 gross 3193
146 gross 3283 #pragma omp parallel for private(i) schedule(static)
147     for (i = 0; i < n; ++i) {
148     if (split_marker[i]) {
149     mask_C[i]=-1;
150     } else {
151     mask_C[i]=counter[i];;
152     }
153     }
154     /*
155 gross 3303 get Restriction :
156     */
157 gross 3283 time0=Esys_timer();
158 gross 3303 out->P=Paso_Preconditioner_AMG_getDirectProlongation(A_p,degree,S,n_C,mask_C);
159 gross 3283 if (SHOW_TIMING) printf("timing: level %d: getProlongation: %e\n",level, Esys_timer()-time0);
160 gross 3303 /*
161 gross 3283 construct Prolongation operator as transposed of restriction operator:
162     */
163     time0=Esys_timer();
164     out->R=Paso_SparseMatrix_getTranspose(out->P);
165     if (SHOW_TIMING) printf("timing: level %d: Paso_SparseMatrix_getTranspose: %e\n",level,Esys_timer()-time0);
166 gross 3303
167 gross 3283 /*
168 gross 3312 construct coarse level matrix:
169 gross 3283 */
170     time0=Esys_timer();
171     Atemp=Paso_SparseMatrix_MatrixMatrix(A_p,out->P);
172     A_C=Paso_SparseMatrix_MatrixMatrix(out->R,Atemp);
173     Paso_SparseMatrix_free(Atemp);
174     if (SHOW_TIMING) printf("timing: level %d : getCoarseMatrix: %e\n",level,Esys_timer()-time0);
175    
176     /* allocate helpers :*/
177     out->x_C=MEMALLOC(n_block*n_C,double);
178     out->b_C=MEMALLOC(n_block*n_C,double);
179     out->r=MEMALLOC(n_block*n,double);
180    
181     Esys_checkPtr(out->r);
182     Esys_checkPtr(out->Smoother);
183     Esys_checkPtr(out->x_C);
184     Esys_checkPtr(out->b_C);
185    
186     /*
187     constructe courser level:
188    
189     */
190     out->AMG_C=Paso_Preconditioner_LocalAMG_alloc(A_C,level+1,options);
191    
192     if ( Esys_noError()) {
193     if ( out->AMG_C == NULL ) {
194     out->reordering = options->reordering;
195     out->refinements = options->coarse_matrix_refinements;
196     /* no coarse level matrix has been constructed. use direct solver */
197     #ifdef MKL
198 gross 3312 out->A_C=Paso_SparseMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, A_C);
199 gross 3283 Paso_SparseMatrix_free(A_C);
200     out->A_C->solver_package = PASO_MKL;
201     #else
202     #ifdef UMFPACK
203 gross 3312 out->A_C=Paso_SparseMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_CSC, A_C);
204 gross 3283 Paso_SparseMatrix_free(A_C);
205     out->A_C->solver_package = PASO_UMFPACK;
206     #else
207     out->A_C=A_C;
208     out->A_C->solver_p=Paso_Preconditioner_LocalSmoother_alloc(out->A_C, (options->smoother == PASO_JACOBI), verbose);
209     out->A_C->solver_package = PASO_SMOOTHER;
210     #endif
211     #endif
212     } else {
213     /* finally we set some helpers for the solver step */
214     out->A_C=A_C;
215     }
216     }
217     }
218     }
219     TMPMEMFREE(mask_C);
220     TMPMEMFREE(rows_in_F);
221     }
222     }
223 gross 3193 }
224     TMPMEMFREE(counter);
225 gross 3283 TMPMEMFREE(split_marker);
226 gross 3303 TMPMEMFREE(degree);
227     TMPMEMFREE(S);
228 gross 3193
229 jfenwick 3259 if (Esys_noError()) {
230 gross 3193 return out;
231     } else {
232     Paso_Preconditioner_LocalAMG_free(out);
233     return NULL;
234     }
235     }
236    
237    
238 gross 3283 void Paso_Preconditioner_LocalAMG_solve(Paso_SparseMatrix* A, Paso_Preconditioner_LocalAMG * amg, double * x, double * b) {
239     const dim_t n = amg->n * amg->n_block;
240     double time0=0;
241     const dim_t post_sweeps=amg->post_sweeps;
242     const dim_t pre_sweeps=amg->pre_sweeps;
243 gross 3193
244 gross 3283 /* presmoothing */
245     time0=Esys_timer();
246     Paso_Preconditioner_LocalSmoother_solve(A, amg->Smoother, x, b, pre_sweeps, FALSE);
247     time0=Esys_timer()-time0;
248     if (SHOW_TIMING) printf("timing: level %d: Presmooting: %e\n",amg->level, time0);
249     /* end of presmoothing */
250    
251     if (amg->n_F < amg->n) { /* is there work on the coarse level? */
252     time0=Esys_timer();
253     Paso_Copy(n, amg->r, b); /* r <- b */
254 gross 3312 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,A,x,1.,amg->r); /*r=r-Ax*/
255     Paso_SparseMatrix_MatrixVector_CSR_OFFSET0_DIAG(1.,amg->R,amg->r,0.,amg->b_C); /* b_c = R*r */
256 gross 3283 time0=Esys_timer()-time0;
257     /* coarse level solve */
258     if ( amg->AMG_C == NULL) {
259     time0=Esys_timer();
260     /* A_C is the coarsest level */
261     switch (amg->A_C->solver_package) {
262     case (PASO_MKL):
263     Paso_MKL(amg->A_C, amg->x_C,amg->b_C, amg->reordering, amg->refinements, SHOW_TIMING);
264     break;
265     case (PASO_UMFPACK):
266     Paso_UMFPACK(amg->A_C, amg->x_C,amg->b_C, amg->refinements, SHOW_TIMING);
267     break;
268     case (PASO_SMOOTHER):
269     Paso_Preconditioner_LocalSmoother_solve(amg->A_C, amg->Smoother,amg->x_C,amg->b_C,pre_sweeps, FALSE);
270     break;
271     }
272     if (SHOW_TIMING) printf("timing: level %d: DIRECT SOLVER: %e\n",amg->level,Esys_timer()-time0);
273     } else {
274     Paso_Preconditioner_LocalAMG_solve(amg->A_C, amg->AMG_C,amg->x_C,amg->b_C); /* x_C=AMG(b_C) */
275     }
276     time0=time0+Esys_timer();
277 gross 3303 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0_DIAG(1.,amg->P,amg->x_C,1.,x); /* x = x + P*x_c */
278 gross 3283
279     /*postsmoothing*/
280    
281     /*solve Ax=b with initial guess x */
282     time0=Esys_timer();
283     Paso_Preconditioner_LocalSmoother_solve(A, amg->Smoother, x, b, post_sweeps, TRUE);
284     time0=Esys_timer()-time0;
285     if (SHOW_TIMING) printf("timing: level %d: Postsmoothing: %e\n",amg->level,time0);
286     /*end of postsmoothing*/
287    
288     }
289     return;
290     }
291 gross 3193
292 gross 3283 /* theta = threshold for strong connections */
293     /* tau = threshold for diagonal dominance */
294 gross 3193
295 gross 3303 /*S_i={j \in N_i; i strongly coupled to j}
296    
297     in the sense that |A_{ij}| >= theta * max_k |A_{ik}|
298     */
299    
300     void Paso_Preconditioner_AMG_setStrongConnections(Paso_SparseMatrix* A,
301     dim_t *degree, index_t *S,
302     const double theta, const double tau)
303 gross 3283 {
304     const dim_t n=A->numRows;
305 gross 3303 index_t iptr, i,j;
306 gross 3283 dim_t kdeg;
307     double max_offdiagonal, threshold, sum_row, main_row, fnorm;
308 gross 3193
309 gross 3303
310 gross 3283 #pragma omp parallel for private(i,iptr,max_offdiagonal, threshold,j, kdeg, sum_row, main_row, fnorm) schedule(static)
311     for (i=0;i<n;++i) {
312 gross 3193
313 gross 3283 max_offdiagonal = 0.;
314     sum_row=0;
315     main_row=0;
316     #pragma ivdep
317     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
318     j=A->pattern->index[iptr];
319     fnorm=ABS(A->val[iptr]);
320    
321     if( j != i) {
322     max_offdiagonal = MAX(max_offdiagonal,fnorm);
323     sum_row+=fnorm;
324     } else {
325     main_row=fnorm;
326     }
327     }
328     threshold = theta*max_offdiagonal;
329     kdeg=0;
330     if (tau*main_row < sum_row) { /* no diagonal domainance */
331     #pragma ivdep
332     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
333     j=A->pattern->index[iptr];
334     if(ABS(A->val[iptr])>threshold && i!=j) {
335     S[A->pattern->ptr[i]+kdeg] = j;
336     kdeg++;
337     }
338     }
339     }
340     degree[i]=kdeg;
341     }
342 gross 3303
343 gross 3283 }
344 gross 3193
345 gross 3283 /* theta = threshold for strong connections */
346     /* tau = threshold for diagonal dominance */
347 gross 3303 /*S_i={j \in N_i; i strongly coupled to j}
348 gross 3283
349 gross 3303 in the sense that |A_{ij}|_F >= theta * max_k |A_{ik}|_F
350     */
351     void Paso_Preconditioner_AMG_setStrongConnections_Block(Paso_SparseMatrix* A,
352     dim_t *degree, index_t *S,
353     const double theta, const double tau)
354    
355 gross 3283 {
356     const dim_t n_block=A->row_block_size;
357     const dim_t n=A->numRows;
358 gross 3303 index_t iptr, i,j, bi;
359 gross 3283 dim_t kdeg, max_deg;
360     register double max_offdiagonal, threshold, fnorm, sum_row, main_row;
361     double *rtmp;
362    
363    
364     #pragma omp parallel private(i,iptr,max_offdiagonal, kdeg, threshold,j, max_deg, fnorm, sum_row, main_row, rtmp)
365     {
366     max_deg=0;
367     #pragma omp for schedule(static)
368     for (i=0;i<n;++i) max_deg=MAX(max_deg, A->pattern->ptr[i+1]-A->pattern->ptr[i]);
369 gross 3193
370 gross 3283 rtmp=TMPMEMALLOC(max_deg, double);
371    
372     #pragma omp for schedule(static)
373     for (i=0;i<n;++i) {
374 gross 3193
375 gross 3283 max_offdiagonal = 0.;
376     sum_row=0;
377     main_row=0;
378     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
379     j=A->pattern->index[iptr];
380     fnorm=0;
381     #pragma ivdep
382     for(bi=0;bi<n_block*n_block;++bi) fnorm+=A->val[iptr*n_block*n_block+bi]*A->val[iptr*n_block*n_block+bi];
383     fnorm=sqrt(fnorm);
384 gross 3287 rtmp[iptr-A->pattern->ptr[i]]=fnorm;
385 gross 3283 if( j != i) {
386     max_offdiagonal = MAX(max_offdiagonal,fnorm);
387     sum_row+=fnorm;
388     } else {
389     main_row=fnorm;
390     }
391     }
392     threshold = theta*max_offdiagonal;
393 gross 3193
394 gross 3283 kdeg=0;
395     if (tau*main_row < sum_row) { /* no diagonal domainance */
396     #pragma ivdep
397     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
398     j=A->pattern->index[iptr];
399     if(rtmp[iptr-A->pattern->ptr[i]] > threshold && i!=j) {
400     S[A->pattern->ptr[i]+kdeg] = j;
401     kdeg++;
402     }
403     }
404     }
405     degree[i]=kdeg;
406     }
407     TMPMEMFREE(rtmp);
408     } /* end of parallel region */
409 gross 3303
410 gross 3283 }
411    
412     /* the runge stueben coarsening algorithm: */
413 gross 3303 void Paso_Preconditioner_AMG_RungeStuebenSearch(const dim_t n, const index_t* offset,
414     const dim_t* degree, const index_t* S,
415 gross 3283 index_t*split_marker)
416     {
417     index_t *lambda=NULL, j, *ST=NULL;
418     dim_t i,k, p, q, *degreeT=NULL, itmp;
419    
420     if (n<=0) return; /* make sure that the return of Paso_Util_arg_max is not pointing to nirvana */
421    
422     lambda=TMPMEMALLOC(n, index_t);
423     degreeT=TMPMEMALLOC(n, dim_t);
424     ST=TMPMEMALLOC(offset[n], index_t);
425    
426     if (! ( Esys_checkPtr(lambda) || Esys_checkPtr(degreeT) || Esys_checkPtr(ST) ) ) {
427     /* initialize split_marker and split_marker :*/
428     /* those unknows which are not influenced go into F, the rest is available for F or C */
429     #pragma omp parallel for private(i) schedule(static)
430     for (i=0;i<n;++i) {
431     degreeT[i]=0;
432     if (degree[i]>0) {
433     lambda[i]=0;
434     split_marker[i]=PASO_AMG_UNDECIDED;
435     } else {
436     split_marker[i]=PASO_AMG_IN_F;
437     lambda[i]=-1;
438     }
439     }
440     /* create transpose :*/
441     for (i=0;i<n;++i) {
442     for (p=0; p<degree[i]; ++p) {
443     j=S[offset[i]+p];
444     ST[offset[j]+degreeT[j]]=i;
445     degreeT[j]++;
446     }
447     }
448     /* lambda[i] = |undecided k in ST[i]| + 2 * |F-unknown in ST[i]| */
449     #pragma omp parallel for private(i, j, itmp) schedule(static)
450     for (i=0;i<n;++i) {
451     if (split_marker[i]==PASO_AMG_UNDECIDED) {
452     itmp=lambda[i];
453     for (p=0; p<degreeT[i]; ++p) {
454     j=ST[offset[i]+p];
455     if (split_marker[j]==PASO_AMG_UNDECIDED) {
456     itmp++;
457     } else { /* at this point there are no C points */
458     itmp+=2;
459     }
460     }
461     lambda[i]=itmp;
462     }
463     }
464    
465     /* start search :*/
466     i=Paso_Util_arg_max(n,lambda);
467     while (lambda[i]>-1) { /* is there any undecided unknowns? */
468 gross 3193
469 gross 3283 /* the unknown i is moved to C */
470     split_marker[i]=PASO_AMG_IN_C;
471     lambda[i]=-1; /* lambda fro unavailable unknowns is set to -1 */
472    
473     /* all undecided unknown strongly coupled to i are moved to F */
474     for (p=0; p<degreeT[i]; ++p) {
475     j=ST[offset[i]+p];
476    
477     if (split_marker[j]==PASO_AMG_UNDECIDED) {
478    
479     split_marker[j]=PASO_AMG_IN_F;
480     lambda[j]=-1;
481    
482     for (q=0; q<degreeT[j]; ++q) {
483     k=ST[offset[j]+q];
484     if (split_marker[k]==PASO_AMG_UNDECIDED) lambda[k]++;
485     }
486 gross 3193
487 gross 3283 }
488     }
489     for (p=0; p<degree[i]; ++p) {
490     j=S[offset[i]+p];
491     if(split_marker[j]==PASO_AMG_UNDECIDED) lambda[j]--;
492     }
493    
494     i=Paso_Util_arg_max(n,lambda);
495     }
496     }
497     TMPMEMFREE(lambda);
498     TMPMEMFREE(ST);
499     TMPMEMFREE(degreeT);
500 gross 3193 }

  ViewVC Help
Powered by ViewVC 1.1.26