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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26