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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3303 - (hide annotations)
Mon Oct 25 04:33:31 2010 UTC (8 years, 11 months ago) by gross
File MIME type: text/plain
File size: 16416 byte(s)
more clean up work on 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     if (verbose) printf("Paso AMG level %d: %d unknowns are flagged for elimination. %d left.\n",level,n_F,n-n_F);
113    
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 3303 construct coarse level matrix (can we do this in one call?)
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     Atemp=Paso_SparseMatrix_unroll(A_C);
199     Paso_SparseMatrix_free(A_C);
200     out->A_C=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, Atemp->pattern,1,1, FALSE);
201     #pragma omp parallel for private(i) schedule(static)
202     for (i=0;i<out->A->len;++i) {
203     out->A_C->val[i]=Atemp->val[i];
204     }
205     Paso_SparseMatrix_free(Atemp);
206     out->A_C->solver_package = PASO_MKL;
207     #else
208     #ifdef UMFPACK
209     out->A_C=Paso_SparseMatrix_unroll(A_C);
210     Paso_SparseMatrix_free(A_C);
211     out->A_C->solver_package = PASO_UMFPACK;
212     #else
213     out->A_C=A_C;
214     out->A_C->solver_p=Paso_Preconditioner_LocalSmoother_alloc(out->A_C, (options->smoother == PASO_JACOBI), verbose);
215     out->A_C->solver_package = PASO_SMOOTHER;
216     #endif
217     #endif
218     } else {
219     /* finally we set some helpers for the solver step */
220     out->A_C=A_C;
221     }
222     }
223     }
224     }
225     TMPMEMFREE(mask_C);
226     TMPMEMFREE(rows_in_F);
227     }
228     }
229 gross 3193 }
230     TMPMEMFREE(counter);
231 gross 3283 TMPMEMFREE(split_marker);
232 gross 3303 TMPMEMFREE(degree);
233     TMPMEMFREE(S);
234 gross 3193
235 jfenwick 3259 if (Esys_noError()) {
236 gross 3283 if (verbose) printf("AMG: level %d: %d unknowns eliminated.\n",level, n_F);
237 gross 3193 return out;
238     } else {
239     Paso_Preconditioner_LocalAMG_free(out);
240     return NULL;
241     }
242     }
243    
244    
245 gross 3283 void Paso_Preconditioner_LocalAMG_solve(Paso_SparseMatrix* A, Paso_Preconditioner_LocalAMG * amg, double * x, double * b) {
246     const dim_t n = amg->n * amg->n_block;
247     double time0=0;
248     const dim_t post_sweeps=amg->post_sweeps;
249     const dim_t pre_sweeps=amg->pre_sweeps;
250 gross 3193
251 gross 3283 /* presmoothing */
252     time0=Esys_timer();
253     Paso_Preconditioner_LocalSmoother_solve(A, amg->Smoother, x, b, pre_sweeps, FALSE);
254     time0=Esys_timer()-time0;
255     if (SHOW_TIMING) printf("timing: level %d: Presmooting: %e\n",amg->level, time0);
256     /* end of presmoothing */
257    
258     if (amg->n_F < amg->n) { /* is there work on the coarse level? */
259     time0=Esys_timer();
260     Paso_Copy(n, amg->r, b); /* r <- b */
261 gross 3303 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0_DIAG(1.,amg->R,amg->r,0.,amg->b_C); /*r=r-Ax*/
262 gross 3283 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,amg->r,0.,amg->b_C); /* b_c = R*r */
263     time0=Esys_timer()-time0;
264     /* coarse level solve */
265     if ( amg->AMG_C == NULL) {
266     time0=Esys_timer();
267     /* A_C is the coarsest level */
268     switch (amg->A_C->solver_package) {
269     case (PASO_MKL):
270     Paso_MKL(amg->A_C, amg->x_C,amg->b_C, amg->reordering, amg->refinements, SHOW_TIMING);
271     break;
272     case (PASO_UMFPACK):
273     Paso_UMFPACK(amg->A_C, amg->x_C,amg->b_C, amg->refinements, SHOW_TIMING);
274     break;
275     case (PASO_SMOOTHER):
276     Paso_Preconditioner_LocalSmoother_solve(amg->A_C, amg->Smoother,amg->x_C,amg->b_C,pre_sweeps, FALSE);
277     break;
278     }
279     if (SHOW_TIMING) printf("timing: level %d: DIRECT SOLVER: %e\n",amg->level,Esys_timer()-time0);
280     } else {
281     Paso_Preconditioner_LocalAMG_solve(amg->A_C, amg->AMG_C,amg->x_C,amg->b_C); /* x_C=AMG(b_C) */
282     }
283     time0=time0+Esys_timer();
284 gross 3303 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0_DIAG(1.,amg->P,amg->x_C,1.,x); /* x = x + P*x_c */
285 gross 3283
286     /*postsmoothing*/
287    
288     /*solve Ax=b with initial guess x */
289     time0=Esys_timer();
290     Paso_Preconditioner_LocalSmoother_solve(A, amg->Smoother, x, b, post_sweeps, TRUE);
291     time0=Esys_timer()-time0;
292     if (SHOW_TIMING) printf("timing: level %d: Postsmoothing: %e\n",amg->level,time0);
293     /*end of postsmoothing*/
294    
295     }
296     return;
297     }
298 gross 3193
299 gross 3283 /* theta = threshold for strong connections */
300     /* tau = threshold for diagonal dominance */
301 gross 3193
302 gross 3303 /*S_i={j \in N_i; i strongly coupled to j}
303    
304     in the sense that |A_{ij}| >= theta * max_k |A_{ik}|
305     */
306    
307     void Paso_Preconditioner_AMG_setStrongConnections(Paso_SparseMatrix* A,
308     dim_t *degree, index_t *S,
309     const double theta, const double tau)
310 gross 3283 {
311     const dim_t n=A->numRows;
312 gross 3303 index_t iptr, i,j;
313 gross 3283 dim_t kdeg;
314     double max_offdiagonal, threshold, sum_row, main_row, fnorm;
315 gross 3193
316 gross 3303
317 gross 3283 #pragma omp parallel for private(i,iptr,max_offdiagonal, threshold,j, kdeg, sum_row, main_row, fnorm) schedule(static)
318     for (i=0;i<n;++i) {
319 gross 3193
320 gross 3283 max_offdiagonal = 0.;
321     sum_row=0;
322     main_row=0;
323     #pragma ivdep
324     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
325     j=A->pattern->index[iptr];
326     fnorm=ABS(A->val[iptr]);
327    
328     if( j != i) {
329     max_offdiagonal = MAX(max_offdiagonal,fnorm);
330     sum_row+=fnorm;
331     } else {
332     main_row=fnorm;
333     }
334     }
335     threshold = theta*max_offdiagonal;
336     kdeg=0;
337     if (tau*main_row < sum_row) { /* no diagonal domainance */
338     #pragma ivdep
339     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
340     j=A->pattern->index[iptr];
341     if(ABS(A->val[iptr])>threshold && i!=j) {
342     S[A->pattern->ptr[i]+kdeg] = j;
343     kdeg++;
344     }
345     }
346     }
347     degree[i]=kdeg;
348     }
349 gross 3303
350 gross 3283 }
351 gross 3193
352 gross 3283 /* theta = threshold for strong connections */
353     /* tau = threshold for diagonal dominance */
354 gross 3303 /*S_i={j \in N_i; i strongly coupled to j}
355 gross 3283
356 gross 3303 in the sense that |A_{ij}|_F >= theta * max_k |A_{ik}|_F
357     */
358     void Paso_Preconditioner_AMG_setStrongConnections_Block(Paso_SparseMatrix* A,
359     dim_t *degree, index_t *S,
360     const double theta, const double tau)
361    
362 gross 3283 {
363     const dim_t n_block=A->row_block_size;
364     const dim_t n=A->numRows;
365 gross 3303 index_t iptr, i,j, bi;
366 gross 3283 dim_t kdeg, max_deg;
367     register double max_offdiagonal, threshold, fnorm, sum_row, main_row;
368     double *rtmp;
369    
370    
371     #pragma omp parallel private(i,iptr,max_offdiagonal, kdeg, threshold,j, max_deg, fnorm, sum_row, main_row, rtmp)
372     {
373     max_deg=0;
374     #pragma omp for schedule(static)
375     for (i=0;i<n;++i) max_deg=MAX(max_deg, A->pattern->ptr[i+1]-A->pattern->ptr[i]);
376 gross 3193
377 gross 3283 rtmp=TMPMEMALLOC(max_deg, double);
378    
379     #pragma omp for schedule(static)
380     for (i=0;i<n;++i) {
381 gross 3193
382 gross 3283 max_offdiagonal = 0.;
383     sum_row=0;
384     main_row=0;
385     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
386     j=A->pattern->index[iptr];
387     fnorm=0;
388     #pragma ivdep
389     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];
390     fnorm=sqrt(fnorm);
391 gross 3287 rtmp[iptr-A->pattern->ptr[i]]=fnorm;
392 gross 3283 if( j != i) {
393     max_offdiagonal = MAX(max_offdiagonal,fnorm);
394     sum_row+=fnorm;
395     } else {
396     main_row=fnorm;
397     }
398     }
399     threshold = theta*max_offdiagonal;
400 gross 3193
401 gross 3283 kdeg=0;
402     if (tau*main_row < sum_row) { /* no diagonal domainance */
403     #pragma ivdep
404     for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {
405     j=A->pattern->index[iptr];
406     if(rtmp[iptr-A->pattern->ptr[i]] > threshold && i!=j) {
407     S[A->pattern->ptr[i]+kdeg] = j;
408     kdeg++;
409     }
410     }
411     }
412     degree[i]=kdeg;
413     }
414     TMPMEMFREE(rtmp);
415     } /* end of parallel region */
416 gross 3303
417 gross 3283 }
418    
419     /* the runge stueben coarsening algorithm: */
420 gross 3303 void Paso_Preconditioner_AMG_RungeStuebenSearch(const dim_t n, const index_t* offset,
421     const dim_t* degree, const index_t* S,
422 gross 3283 index_t*split_marker)
423     {
424     index_t *lambda=NULL, j, *ST=NULL;
425     dim_t i,k, p, q, *degreeT=NULL, itmp;
426    
427     if (n<=0) return; /* make sure that the return of Paso_Util_arg_max is not pointing to nirvana */
428    
429     lambda=TMPMEMALLOC(n, index_t);
430     degreeT=TMPMEMALLOC(n, dim_t);
431     ST=TMPMEMALLOC(offset[n], index_t);
432    
433     if (! ( Esys_checkPtr(lambda) || Esys_checkPtr(degreeT) || Esys_checkPtr(ST) ) ) {
434     /* initialize split_marker and split_marker :*/
435     /* those unknows which are not influenced go into F, the rest is available for F or C */
436     #pragma omp parallel for private(i) schedule(static)
437     for (i=0;i<n;++i) {
438     degreeT[i]=0;
439     if (degree[i]>0) {
440     lambda[i]=0;
441     split_marker[i]=PASO_AMG_UNDECIDED;
442     } else {
443     split_marker[i]=PASO_AMG_IN_F;
444     lambda[i]=-1;
445     }
446     }
447     /* create transpose :*/
448     for (i=0;i<n;++i) {
449     for (p=0; p<degree[i]; ++p) {
450     j=S[offset[i]+p];
451     ST[offset[j]+degreeT[j]]=i;
452     degreeT[j]++;
453     }
454     }
455     /* lambda[i] = |undecided k in ST[i]| + 2 * |F-unknown in ST[i]| */
456     #pragma omp parallel for private(i, j, itmp) schedule(static)
457     for (i=0;i<n;++i) {
458     if (split_marker[i]==PASO_AMG_UNDECIDED) {
459     itmp=lambda[i];
460     for (p=0; p<degreeT[i]; ++p) {
461     j=ST[offset[i]+p];
462     if (split_marker[j]==PASO_AMG_UNDECIDED) {
463     itmp++;
464     } else { /* at this point there are no C points */
465     itmp+=2;
466     }
467     }
468     lambda[i]=itmp;
469     }
470     }
471    
472     /* start search :*/
473     i=Paso_Util_arg_max(n,lambda);
474     while (lambda[i]>-1) { /* is there any undecided unknowns? */
475 gross 3193
476 gross 3283 /* the unknown i is moved to C */
477     split_marker[i]=PASO_AMG_IN_C;
478     lambda[i]=-1; /* lambda fro unavailable unknowns is set to -1 */
479    
480     /* all undecided unknown strongly coupled to i are moved to F */
481     for (p=0; p<degreeT[i]; ++p) {
482     j=ST[offset[i]+p];
483    
484     if (split_marker[j]==PASO_AMG_UNDECIDED) {
485    
486     split_marker[j]=PASO_AMG_IN_F;
487     lambda[j]=-1;
488    
489     for (q=0; q<degreeT[j]; ++q) {
490     k=ST[offset[j]+q];
491     if (split_marker[k]==PASO_AMG_UNDECIDED) lambda[k]++;
492     }
493 gross 3193
494 gross 3283 }
495     }
496     for (p=0; p<degree[i]; ++p) {
497     j=S[offset[i]+p];
498     if(split_marker[j]==PASO_AMG_UNDECIDED) lambda[j]--;
499     }
500    
501     i=Paso_Util_arg_max(n,lambda);
502     }
503     }
504     TMPMEMFREE(lambda);
505     TMPMEMFREE(ST);
506     TMPMEMFREE(degreeT);
507 gross 3193 }

  ViewVC Help
Powered by ViewVC 1.1.26