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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3450 - (hide annotations)
Fri Jan 21 06:39:56 2011 UTC (8 years, 9 months ago) by gross
File MIME type: text/plain
File size: 27303 byte(s)
more compilation fixing
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 gross 3441 /* Paso: AMG preconditioner (local version) */
18 gross 3193
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 gross 3441 void Paso_Preconditioner_AMG_free(Paso_Preconditioner_AMG * in) {
39 gross 3193 if (in!=NULL) {
40 gross 3441 Paso_Preconditioner_Smoother_free(in->Smoother);
41     Paso_SystemMatrix_free(in->P);
42     Paso_SystemMatrix_free(in->R);
43     Paso_SystemMatrix_free(in->A_C);
44     Paso_Preconditioner_AMG_free(in->AMG_C);
45 gross 3283 MEMFREE(in->r);
46     MEMFREE(in->x_C);
47     MEMFREE(in->b_C);
48 gross 3193
49 gross 3283 MEMFREE(in);
50 gross 3193 }
51     }
52    
53 gross 3441 index_t Paso_Preconditioner_AMG_getMaxLevel(const Paso_Preconditioner_AMG * in) {
54 gross 3402 if (in->AMG_C == NULL) {
55     return in->level;
56     } else {
57 gross 3441 return Paso_Preconditioner_AMG_getMaxLevel(in->AMG_C);
58 gross 3402 }
59     }
60 gross 3441 double Paso_Preconditioner_AMG_getCoarseLevelSparsity(const Paso_Preconditioner_AMG * in) {
61 gross 3403 if (in->AMG_C == NULL) {
62     if (in->A_C == NULL) {
63     return 1.;
64     } else {
65 gross 3442 return Paso_SystemMatrix_getSparsity(in->A_C);
66 gross 3403 }
67     } else {
68 gross 3441 return Paso_Preconditioner_AMG_getCoarseLevelSparsity(in->AMG_C);
69 gross 3403 }
70 gross 3402 }
71 gross 3441 dim_t Paso_Preconditioner_AMG_getNumCoarseUnknwons(const Paso_Preconditioner_AMG * in) {
72 gross 3402 if (in->AMG_C == NULL) {
73 gross 3403 if (in->A_C == NULL) {
74     return 0;
75     } else {
76 gross 3445 return Paso_SystemMatrix_getTotalNumRows(in->A_C);
77 gross 3403 }
78 gross 3402 } else {
79 gross 3441 return Paso_Preconditioner_AMG_getNumCoarseUnknwons(in->AMG_C);
80 gross 3402 }
81     }
82 gross 3283 /*****************************************************************
83 gross 3193
84 gross 3283 constructs AMG
85    
86     ******************************************************************/
87 gross 3441 Paso_Preconditioner_AMG* Paso_Preconditioner_AMG_alloc(Paso_SystemMatrix *A_p,dim_t level,Paso_Options* options) {
88 gross 3193
89 gross 3441 Paso_Preconditioner_AMG* out=NULL;
90 gross 3193 bool_t verbose=options->verbose;
91 gross 3445
92     const dim_t my_n=Paso_SystemMatrix_getNumRows(A_p);
93     const dim_t overlap_n=Paso_SystemMatrix_getColOverlap(A_p);
94     const dim_t n = my_n + overlap_n;
95    
96 gross 3193 const dim_t n_block=A_p->row_block_size;
97 gross 3450 index_t* F_marker=NULL, *counter=NULL, *mask_C=NULL;
98     dim_t i;
99 gross 3283 double time0=0;
100     const double theta = options->coarsening_threshold;
101     const double tau = options->diagonal_dominance_threshold;
102 gross 3442 const double sparsity=Paso_SystemMatrix_getSparsity(A_p);
103     const dim_t total_n=Paso_SystemMatrix_getGlobalTotalNumRows(A_p);
104 plaub 3436
105 gross 3193
106     /*
107 gross 3283 is the input matrix A suitable for coarsening
108    
109 gross 3193 */
110 gross 3442 if ( (sparsity >= options->min_coarse_sparsity) ||
111     (total_n <= options->min_coarse_matrix_size) ||
112     (level > options->level_max) ) {
113 plaub 3436
114 gross 3445 if (verbose) {
115 plaub 3436 /*
116     print stopping condition:
117     - 'SPAR' = min_coarse_matrix_sparsity exceeded
118     - 'SIZE' = min_coarse_matrix_size exceeded
119     - 'LEVEL' = level_max exceeded
120     */
121 gross 3442 printf("Paso_Preconditioner: AMG: termination of coarsening by ");
122 plaub 3436
123 gross 3442 if (sparsity >= options->min_coarse_sparsity)
124 plaub 3436 printf("SPAR ");
125    
126 gross 3442 if (total_n <= options->min_coarse_matrix_size)
127 plaub 3436 printf("SIZE ");
128    
129     if (level > options->level_max)
130     printf("LEVEL ");
131    
132     printf("\n");
133    
134     printf("Paso_Preconditioner: AMG level %d (limit = %d) stopped. sparsity = %e (limit = %e), unknowns = %d (limit = %d)\n",
135 gross 3442 level, options->level_max, sparsity, options->min_coarse_sparsity, total_n, options->min_coarse_matrix_size);
136 plaub 3436
137 gross 3445 }
138 plaub 3436
139 gross 3445 return NULL;
140     } else {
141     /* Start Coarsening : */
142     const dim_t len_S=A_p->mainBlock->pattern->len+A_p->col_coupleBlock->pattern->len;
143 gross 3193
144 gross 3440 F_marker=TMPMEMALLOC(n,index_t);
145 gross 3283 counter=TMPMEMALLOC(n,index_t);
146 gross 3445
147     dim_t* degree_S=TMPMEMALLOC(my_n, dim_t);
148     index_t *offset_S=TMPMEMALLOC(my_n, index_t);
149     index_t *S=TMPMEMALLOC(len_S, index_t);
150     if ( !( Esys_checkPtr(F_marker) || Esys_checkPtr(counter) || Esys_checkPtr(degree_S) || Esys_checkPtr(offset_S) || Esys_checkPtr(S) ) ) {
151 gross 3283 /*
152     set splitting of unknows:
153    
154     */
155     time0=Esys_timer();
156     if (n_block>1) {
157 gross 3445 Paso_Preconditioner_AMG_setStrongConnections_Block(A_p, degree_S, offset_S, S, theta,tau);
158 gross 3283 } else {
159 gross 3445 Paso_Preconditioner_AMG_setStrongConnections(A_p, degree_S, offset_S, S, theta,tau);
160 gross 3283 }
161 gross 3449 {
162     dim_t p;
163     for (i=0; i< my_n; ++i) {
164     printf("%d: ",i);
165     for (p=0; p<degree_S[i];++p) printf("%d ",S[offset_S[i]+p]);
166     printf("\n");
167     }
168     }
169    
170 gross 3442
171     /*MPI:
172 gross 3440 Paso_Preconditioner_AMG_RungeStuebenSearch(n, A_p->pattern->ptr, degree_S, S, F_marker, options->usePanel);
173 gross 3442 */
174    
175 gross 3440 /* in BoomerAMG interpolation is used FF connectiovity is required :*/
176 gross 3442 /*MPI:
177 gross 3440 if (options->interpolation_method == PASO_CLASSIC_INTERPOLATION_WITH_FF_COUPLING)
178     Paso_Preconditioner_AMG_enforceFFConnectivity(n, A_p->pattern->ptr, degree_S, S, F_marker);
179 gross 3442 */
180    
181 gross 3283 options->coarsening_selection_time=Esys_timer()-time0 + MAX(0, options->coarsening_selection_time);
182 gross 3445
183     #ifdef AAAAA
184 gross 3283 if (Esys_noError() ) {
185     #pragma omp parallel for private(i) schedule(static)
186 gross 3440 for (i = 0; i < n; ++i) F_marker[i]=(F_marker[i] == PASO_AMG_IN_F);
187 gross 3283
188     /*
189     count number of unkowns to be eliminated:
190     */
191 gross 3440 n_F=Paso_Util_cumsum_maskedTrue(n,counter, F_marker);
192 gross 3283 n_C=n-n_F;
193 gross 3402 if (verbose) printf("Paso_Preconditioner: AMG level %d: %d unknowns are flagged for elimination. %d left.\n",level,n_F,n-n_F);
194 gross 3283
195     if ( n_F == 0 ) { /* is a nasty case. a direct solver should be used, return NULL */
196     out = NULL;
197     } else {
198 gross 3441 out=MEMALLOC(1,Paso_Preconditioner_AMG);
199 gross 3323 if (! Esys_checkPtr(out)) {
200 gross 3283 out->level = level;
201     out->n = n;
202     out->n_F = n_F;
203     out->n_block = n_block;
204     out->A_C = NULL;
205     out->P = NULL;
206     out->R = NULL;
207     out->post_sweeps = options->post_sweeps;
208     out->pre_sweeps = options->pre_sweeps;
209     out->r = NULL;
210     out->x_C = NULL;
211     out->b_C = NULL;
212     out->AMG_C = NULL;
213 gross 3323 out->Smoother=NULL;
214     }
215     mask_C=TMPMEMALLOC(n,index_t);
216     rows_in_F=TMPMEMALLOC(n_F,index_t);
217     Esys_checkPtr(mask_C);
218     Esys_checkPtr(rows_in_F);
219     if ( Esys_noError() ) {
220    
221 gross 3442 out->Smoother = Paso_Preconditioner_Smoother_alloc(A_p, (options->smoother == PASO_JACOBI), verbose);
222 gross 3323
223 gross 3402 if (n_C != 0) {
224     /* if nothing is been removed we have a diagonal dominant matrix and we just run a few steps of the smoother */
225 gross 3283
226 gross 3315 /* allocate helpers :*/
227     out->x_C=MEMALLOC(n_block*n_C,double);
228     out->b_C=MEMALLOC(n_block*n_C,double);
229     out->r=MEMALLOC(n_block*n,double);
230    
231     Esys_checkPtr(out->r);
232     Esys_checkPtr(out->x_C);
233     Esys_checkPtr(out->b_C);
234    
235 gross 3323 if ( Esys_noError() ) {
236     /* creates index for F:*/
237     #pragma omp parallel private(i)
238     {
239     #pragma omp for schedule(static)
240     for (i = 0; i < n; ++i) {
241 gross 3440 if (F_marker[i]) rows_in_F[counter[i]]=i;
242 gross 3323 }
243     }
244     /* create mask of C nodes with value >-1 gives new id */
245 gross 3440 i=Paso_Util_cumsum_maskedFalse(n,counter, F_marker);
246 gross 3193
247 gross 3323 #pragma omp parallel for private(i) schedule(static)
248     for (i = 0; i < n; ++i) {
249 gross 3440 if (F_marker[i]) {
250 gross 3323 mask_C[i]=-1;
251     } else {
252     mask_C[i]=counter[i];;
253     }
254 gross 3283 }
255 gross 3323 /*
256 gross 3440 get Prolongation :
257 gross 3323 */
258     time0=Esys_timer();
259 gross 3442 /*MPI:
260 gross 3440 out->P=Paso_Preconditioner_AMG_getProlongation(A_p,A_p->pattern->ptr, degree_S,S,n_C,mask_C, options->interpolation_method);
261 gross 3442 */
262 gross 3323 if (SHOW_TIMING) printf("timing: level %d: getProlongation: %e\n",level, Esys_timer()-time0);
263 gross 3283 }
264 gross 3323 /*
265 gross 3440 construct Restriction operator as transposed of Prolongation operator:
266 gross 3283 */
267 gross 3315 if ( Esys_noError()) {
268     time0=Esys_timer();
269 gross 3442 /*MPI:
270 gross 3441 out->R=Paso_SystemMatrix_getTranspose(out->P);
271 gross 3442 */
272 gross 3441 if (SHOW_TIMING) printf("timing: level %d: Paso_SystemMatrix_getTranspose: %e\n",level,Esys_timer()-time0);
273 gross 3315 }
274 gross 3283 /*
275 gross 3312 construct coarse level matrix:
276 gross 3283 */
277 gross 3315 if ( Esys_noError()) {
278     time0=Esys_timer();
279 gross 3442 /*MPI:
280 gross 3441 Atemp=Paso_SystemMatrix_MatrixMatrix(A_p,out->P);
281     A_C=Paso_SystemMatrix_MatrixMatrix(out->R,Atemp);
282 gross 3442
283 gross 3441 Paso_SystemMatrix_free(Atemp);
284 gross 3442 */
285    
286 gross 3315 if (SHOW_TIMING) printf("timing: level %d : construct coarse matrix: %e\n",level,Esys_timer()-time0);
287     }
288    
289 gross 3283
290     /*
291     constructe courser level:
292    
293     */
294     if ( Esys_noError()) {
295 gross 3441 out->AMG_C=Paso_Preconditioner_AMG_alloc(A_C,level+1,options);
296 gross 3315 }
297     if ( Esys_noError()) {
298 gross 3283 if ( out->AMG_C == NULL ) {
299     out->reordering = options->reordering;
300     out->refinements = options->coarse_matrix_refinements;
301     /* no coarse level matrix has been constructed. use direct solver */
302     #ifdef MKL
303 gross 3441 out->A_C=Paso_SystemMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, A_C);
304     Paso_SystemMatrix_free(A_C);
305 gross 3283 out->A_C->solver_package = PASO_MKL;
306 gross 3402 if (verbose) printf("Paso_Preconditioner: AMG: use MKL direct solver on the coarsest level (number of unknowns = %d).\n",n_C*n_block);
307 gross 3283 #else
308     #ifdef UMFPACK
309 gross 3441 out->A_C=Paso_SystemMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_CSC, A_C);
310     Paso_SystemMatrix_free(A_C);
311 gross 3283 out->A_C->solver_package = PASO_UMFPACK;
312 gross 3402 if (verbose) printf("Paso_Preconditioner: AMG: use UMFPACK direct solver on the coarsest level (number of unknowns = %d).\n",n_C*n_block);
313 gross 3283 #else
314     out->A_C=A_C;
315 gross 3442 out->A_C->solver_p=Paso_Preconditioner_Smoother_alloc(out->A_C, (options->smoother == PASO_JACOBI), verbose);
316 gross 3283 out->A_C->solver_package = PASO_SMOOTHER;
317 gross 3402 if (verbose) printf("Paso_Preconditioner: AMG: use smoother on the coarsest level (number of unknowns = %d).\n",n_C*n_block);
318 gross 3283 #endif
319     #endif
320     } else {
321     /* finally we set some helpers for the solver step */
322     out->A_C=A_C;
323     }
324     }
325     }
326     }
327     TMPMEMFREE(mask_C);
328     TMPMEMFREE(rows_in_F);
329     }
330     }
331 gross 3445 #endif
332    
333 gross 3193 }
334     TMPMEMFREE(counter);
335 gross 3440 TMPMEMFREE(F_marker);
336     TMPMEMFREE(degree_S);
337 gross 3445 TMPMEMFREE(offset_S);
338 gross 3303 TMPMEMFREE(S);
339 gross 3445 }
340 gross 3193
341 jfenwick 3259 if (Esys_noError()) {
342 gross 3193 return out;
343     } else {
344 gross 3441 Paso_Preconditioner_AMG_free(out);
345 gross 3193 return NULL;
346     }
347     }
348    
349    
350 gross 3441 void Paso_Preconditioner_AMG_solve(Paso_SystemMatrix* A, Paso_Preconditioner_AMG * amg, double * x, double * b) {
351 gross 3283 const dim_t n = amg->n * amg->n_block;
352     double time0=0;
353     const dim_t post_sweeps=amg->post_sweeps;
354     const dim_t pre_sweeps=amg->pre_sweeps;
355 gross 3193
356 gross 3283 /* presmoothing */
357     time0=Esys_timer();
358 gross 3442 Paso_Preconditioner_Smoother_solve(A, amg->Smoother, x, b, pre_sweeps, FALSE);
359 gross 3283 time0=Esys_timer()-time0;
360     if (SHOW_TIMING) printf("timing: level %d: Presmooting: %e\n",amg->level, time0);
361     /* end of presmoothing */
362    
363     if (amg->n_F < amg->n) { /* is there work on the coarse level? */
364     time0=Esys_timer();
365     Paso_Copy(n, amg->r, b); /* r <- b */
366 gross 3441 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(-1.,A,x,1.,amg->r); /*r=r-Ax*/
367 gross 3445 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,amg->r,0.,amg->b_C); /* b_c = R*r */
368 gross 3283 time0=Esys_timer()-time0;
369     /* coarse level solve */
370     if ( amg->AMG_C == NULL) {
371     time0=Esys_timer();
372     /* A_C is the coarsest level */
373 gross 3445 #ifdef FIXME
374 gross 3283 switch (amg->A_C->solver_package) {
375     case (PASO_MKL):
376     Paso_MKL(amg->A_C, amg->x_C,amg->b_C, amg->reordering, amg->refinements, SHOW_TIMING);
377     break;
378     case (PASO_UMFPACK):
379     Paso_UMFPACK(amg->A_C, amg->x_C,amg->b_C, amg->refinements, SHOW_TIMING);
380     break;
381     case (PASO_SMOOTHER):
382 gross 3442 Paso_Preconditioner_Smoother_solve(amg->A_C, amg->A_C->solver_p,amg->x_C,amg->b_C,pre_sweeps+post_sweeps, FALSE);
383 gross 3283 break;
384     }
385 gross 3445 #endif
386     Paso_Preconditioner_Smoother_solve(amg->A_C, amg->A_C->solver_p,amg->x_C,amg->b_C,pre_sweeps+post_sweeps, FALSE);
387 gross 3283 if (SHOW_TIMING) printf("timing: level %d: DIRECT SOLVER: %e\n",amg->level,Esys_timer()-time0);
388     } else {
389 gross 3441 Paso_Preconditioner_AMG_solve(amg->A_C, amg->AMG_C,amg->x_C,amg->b_C); /* x_C=AMG(b_C) */
390 gross 3283 }
391     time0=time0+Esys_timer();
392 gross 3445 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,1.,x); /* x = x + P*x_c */
393 gross 3283
394     /*postsmoothing*/
395    
396     /*solve Ax=b with initial guess x */
397     time0=Esys_timer();
398 gross 3442 Paso_Preconditioner_Smoother_solve(A, amg->Smoother, x, b, post_sweeps, TRUE);
399 gross 3283 time0=Esys_timer()-time0;
400     if (SHOW_TIMING) printf("timing: level %d: Postsmoothing: %e\n",amg->level,time0);
401     /*end of postsmoothing*/
402    
403     }
404     return;
405     }
406 gross 3193
407 gross 3283 /* theta = threshold for strong connections */
408     /* tau = threshold for diagonal dominance */
409 gross 3193
410 gross 3303 /*S_i={j \in N_i; i strongly coupled to j}
411    
412     in the sense that |A_{ij}| >= theta * max_k |A_{ik}|
413     */
414    
415 gross 3441 void Paso_Preconditioner_AMG_setStrongConnections(Paso_SystemMatrix* A,
416 gross 3445 dim_t *degree_S, index_t *offset_S, index_t *S,
417 gross 3303 const double theta, const double tau)
418 gross 3283 {
419 gross 3445 const dim_t my_n=Paso_SystemMatrix_getNumRows(A);
420     index_t iptr, i;
421 gross 3193
422 gross 3303
423 gross 3445 #pragma omp parallel for private(i,iptr) schedule(static)
424     for (i=0;i<my_n;++i) {
425     register double max_offdiagonal = 0.;
426     register double sum_row=0;
427     register double main_row=0;
428 gross 3283 #pragma ivdep
429 gross 3445 for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
430     register index_t j=A->mainBlock->pattern->index[iptr];
431     register double fnorm=ABS(A->mainBlock->val[iptr]);
432 gross 3283
433     if( j != i) {
434     max_offdiagonal = MAX(max_offdiagonal,fnorm);
435     sum_row+=fnorm;
436     } else {
437     main_row=fnorm;
438     }
439 gross 3445
440 gross 3283 }
441 gross 3445 #pragma ivdep
442     for (iptr=A->col_coupleBlock->pattern->ptr[i];iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
443     register double fnorm=ABS(A->col_coupleBlock->val[iptr]);
444     max_offdiagonal = MAX(max_offdiagonal,fnorm);
445     sum_row+=fnorm;
446     }
447    
448    
449     const double threshold = theta*max_offdiagonal;
450     register dim_t kdeg=0;
451     register index_t koffset=A->mainBlock->pattern->ptr[i]+A->col_coupleBlock->pattern->ptr[i];
452 gross 3283 if (tau*main_row < sum_row) { /* no diagonal domainance */
453     #pragma ivdep
454 gross 3445 for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
455     register index_t j=A->mainBlock->pattern->index[iptr];
456     if(ABS(A->mainBlock->val[iptr])>threshold && i!=j) {
457     S[koffset+kdeg] = j;
458 gross 3283 kdeg++;
459     }
460     }
461 gross 3445 #pragma ivdep
462     for (iptr=A->col_coupleBlock->pattern->ptr[i];iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
463     register index_t j=A->col_coupleBlock->pattern->index[iptr];
464     if(ABS(A->col_coupleBlock->val[iptr])>threshold) {
465     S[koffset+kdeg] = j + my_n;
466     kdeg++;
467     }
468     }
469 gross 3440 }
470 gross 3445 offset_S[i]=koffset;
471 gross 3440 degree_S[i]=kdeg;
472 gross 3283 }
473     }
474 gross 3193
475 gross 3283 /* theta = threshold for strong connections */
476     /* tau = threshold for diagonal dominance */
477 gross 3303 /*S_i={j \in N_i; i strongly coupled to j}
478 gross 3283
479 gross 3303 in the sense that |A_{ij}|_F >= theta * max_k |A_{ik}|_F
480     */
481 gross 3441 void Paso_Preconditioner_AMG_setStrongConnections_Block(Paso_SystemMatrix* A,
482 gross 3445 dim_t *degree_S, index_t *offset_S, index_t *S,
483 gross 3303 const double theta, const double tau)
484    
485 gross 3283 {
486 gross 3445 const dim_t my_n=Paso_SystemMatrix_getNumRows(A);
487     index_t iptr, i, bi;
488 gross 3283 const dim_t n_block=A->row_block_size;
489    
490    
491 gross 3445 #pragma omp parallel private(i,iptr, bi)
492 gross 3283 {
493 gross 3445 dim_t max_deg=0;
494 gross 3283 #pragma omp for schedule(static)
495 gross 3445 for (i=0;i<my_n;++i) max_deg=MAX(max_deg, A->mainBlock->pattern->ptr[i+1]-A->mainBlock->pattern->ptr[i]
496     +A->col_coupleBlock->pattern->ptr[i+1]-A->col_coupleBlock->pattern->ptr[i]);
497 gross 3193
498 gross 3445 double *rtmp=TMPMEMALLOC(max_deg, double);
499 gross 3283
500     #pragma omp for schedule(static)
501 gross 3445 for (i=0;i<my_n;++i) {
502 gross 3193
503 gross 3445 register double max_offdiagonal = 0.;
504     register double sum_row=0;
505     register double main_row=0;
506     register index_t rtmp_offset=-A->mainBlock->pattern->ptr[i];
507 gross 3440
508 gross 3445 for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
509     register index_t j=A->mainBlock->pattern->index[iptr];
510     register double fnorm=0;
511 gross 3283 #pragma ivdep
512 gross 3445 for(bi=0;bi<n_block*n_block;++bi) {
513     register double rtmp2 = A->mainBlock->val[iptr*n_block*n_block+bi];
514     fnorm+=rtmp2*rtmp2;
515     }
516 gross 3283 fnorm=sqrt(fnorm);
517 gross 3445
518     rtmp[iptr+rtmp_offset]=fnorm;
519 gross 3283 if( j != i) {
520     max_offdiagonal = MAX(max_offdiagonal,fnorm);
521     sum_row+=fnorm;
522     } else {
523     main_row=fnorm;
524     }
525     }
526 gross 3445 rtmp_offset=A->mainBlock->pattern->ptr[i+1]-A->mainBlock->pattern->ptr[i]-A->col_coupleBlock->pattern->ptr[i];
527     for (iptr=A->col_coupleBlock->pattern->ptr[i];iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
528     register double fnorm=0;
529     #pragma ivdep
530     for(bi=0;bi<n_block*n_block;++bi) {
531     register double rtmp2 = A->col_coupleBlock->val[iptr*n_block*n_block+bi];
532     fnorm+=rtmp2*rtmp2;
533     }
534     fnorm=sqrt(fnorm);
535    
536     rtmp[iptr+rtmp_offset]=fnorm;
537     max_offdiagonal = MAX(max_offdiagonal,fnorm);
538     sum_row+=fnorm;
539     }
540    
541     const double threshold = theta*max_offdiagonal;
542     register dim_t kdeg=0;
543     register index_t koffset=A->mainBlock->pattern->ptr[i]+A->col_coupleBlock->pattern->ptr[i];
544    
545 gross 3283 if (tau*main_row < sum_row) { /* no diagonal domainance */
546 gross 3445 rtmp_offset=-A->mainBlock->pattern->ptr[i];
547 gross 3283 #pragma ivdep
548 gross 3445 for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
549     register index_t j=A->mainBlock->pattern->index[iptr];
550     if(rtmp[iptr+rtmp_offset] > threshold && i!=j) {
551     S[koffset+kdeg] = j;
552 gross 3283 kdeg++;
553     }
554     }
555 gross 3445 rtmp_offset=A->mainBlock->pattern->ptr[i+1]-A->mainBlock->pattern->ptr[i]-A->col_coupleBlock->pattern->ptr[i];
556     #pragma ivdep
557 gross 3446 for (iptr=A->col_coupleBlock->pattern->ptr[i]; iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
558 gross 3445 register index_t j=A->col_coupleBlock->pattern->index[iptr];
559     if(rtmp[iptr+rtmp_offset] > threshold) {
560     S[koffset+kdeg] = j + my_n;
561     kdeg++;
562     }
563     }
564    
565 gross 3283 }
566 gross 3440 degree_S[i]=kdeg;
567 gross 3445 offset_S[i]=koffset;
568 gross 3283 }
569     TMPMEMFREE(rtmp);
570     } /* end of parallel region */
571 gross 3303
572 gross 3283 }
573    
574 gross 3445 #ifdef AAAAA
575 gross 3283 /* the runge stueben coarsening algorithm: */
576 gross 3440 void Paso_Preconditioner_AMG_RungeStuebenSearch(const dim_t n, const index_t* offset_S,
577     const dim_t* degree_S, const index_t* S,
578 gross 3352 index_t*split_marker, const bool_t usePanel)
579 gross 3283 {
580    
581 gross 3351 index_t *lambda=NULL, *ST=NULL, *notInPanel=NULL, *panel=NULL, lambda_max, lambda_k;
582 gross 3440 dim_t i,k, p, q, *degree_ST=NULL, len_panel, len_panel_new;
583 gross 3351 register index_t j, itmp;
584    
585 gross 3283 if (n<=0) return; /* make sure that the return of Paso_Util_arg_max is not pointing to nirvana */
586    
587 gross 3351 lambda=TMPMEMALLOC(n, index_t); Esys_checkPtr(lambda);
588 gross 3440 degree_ST=TMPMEMALLOC(n, dim_t); Esys_checkPtr(degree_ST);
589     ST=TMPMEMALLOC(offset_S[n], index_t); Esys_checkPtr(ST);
590 gross 3351 if (usePanel) {
591     notInPanel=TMPMEMALLOC(n, bool_t); Esys_checkPtr(notInPanel);
592     panel=TMPMEMALLOC(n, index_t); Esys_checkPtr(panel);
593     }
594 gross 3283
595 gross 3351
596    
597     if (Esys_noError() ) {
598 gross 3283 /* initialize split_marker and split_marker :*/
599     /* those unknows which are not influenced go into F, the rest is available for F or C */
600     #pragma omp parallel for private(i) schedule(static)
601     for (i=0;i<n;++i) {
602 gross 3440 degree_ST[i]=0;
603     if (degree_S[i]>0) {
604 gross 3283 lambda[i]=0;
605     split_marker[i]=PASO_AMG_UNDECIDED;
606     } else {
607     split_marker[i]=PASO_AMG_IN_F;
608     lambda[i]=-1;
609     }
610     }
611     /* create transpose :*/
612     for (i=0;i<n;++i) {
613 gross 3440 for (p=0; p<degree_S[i]; ++p) {
614     j=S[offset_S[i]+p];
615     ST[offset_S[j]+degree_ST[j]]=i;
616     degree_ST[j]++;
617 gross 3283 }
618     }
619     /* lambda[i] = |undecided k in ST[i]| + 2 * |F-unknown in ST[i]| */
620     #pragma omp parallel for private(i, j, itmp) schedule(static)
621     for (i=0;i<n;++i) {
622     if (split_marker[i]==PASO_AMG_UNDECIDED) {
623     itmp=lambda[i];
624 gross 3440 for (p=0; p<degree_ST[i]; ++p) {
625     j=ST[offset_S[i]+p];
626 gross 3283 if (split_marker[j]==PASO_AMG_UNDECIDED) {
627     itmp++;
628     } else { /* at this point there are no C points */
629     itmp+=2;
630     }
631     }
632     lambda[i]=itmp;
633     }
634     }
635 gross 3351 if (usePanel) {
636     #pragma omp parallel for private(i) schedule(static)
637     for (i=0;i<n;++i) notInPanel[i]=TRUE;
638     }
639 gross 3283 /* start search :*/
640     i=Paso_Util_arg_max(n,lambda);
641 gross 3351 while (lambda[i]>-1) { /* is there any undecided unknown? */
642    
643     if (usePanel) {
644     len_panel=0;
645     do {
646     /* the unknown i is moved to C */
647     split_marker[i]=PASO_AMG_IN_C;
648     lambda[i]=-1; /* lambda from unavailable unknowns is set to -1 */
649    
650     /* all undecided unknown strongly coupled to i are moved to F */
651 gross 3440 for (p=0; p<degree_ST[i]; ++p) {
652     j=ST[offset_S[i]+p];
653 gross 3351
654     if (split_marker[j]==PASO_AMG_UNDECIDED) {
655    
656     split_marker[j]=PASO_AMG_IN_F;
657     lambda[j]=-1;
658    
659 gross 3440 for (q=0; q<degree_ST[j]; ++q) {
660     k=ST[offset_S[j]+q];
661 gross 3351 if (split_marker[k]==PASO_AMG_UNDECIDED) {
662     lambda[k]++;
663     if (notInPanel[k]) {
664     notInPanel[k]=FALSE;
665     panel[len_panel]=k;
666     len_panel++;
667     }
668    
669     } /* the unknown i is moved to C */
670     split_marker[i]=PASO_AMG_IN_C;
671     lambda[i]=-1; /* lambda from unavailable unknowns is set to -1 */
672     }
673    
674     }
675 gross 3283 }
676 gross 3440 for (p=0; p<degree_S[i]; ++p) {
677     j=S[offset_S[i]+p];
678 gross 3351 if (split_marker[j]==PASO_AMG_UNDECIDED) {
679     lambda[j]--;
680     if (notInPanel[j]) {
681     notInPanel[j]=FALSE;
682     panel[len_panel]=j;
683     len_panel++;
684     }
685     }
686     }
687 gross 3193
688 gross 3351 /* consolidate panel */
689     /* remove lambda[q]=-1 */
690     lambda_max=-1;
691     i=-1;
692     len_panel_new=0;
693     for (q=0; q<len_panel; q++) {
694     k=panel[q];
695     lambda_k=lambda[k];
696     if (split_marker[k]==PASO_AMG_UNDECIDED) {
697     panel[len_panel_new]=k;
698     len_panel_new++;
699    
700     if (lambda_max == lambda_k) {
701     if (k<i) i=k;
702     } else if (lambda_max < lambda_k) {
703     lambda_max =lambda_k;
704     i=k;
705     }
706     }
707     }
708     len_panel=len_panel_new;
709     } while (len_panel>0);
710     } else {
711     /* the unknown i is moved to C */
712     split_marker[i]=PASO_AMG_IN_C;
713     lambda[i]=-1; /* lambda from unavailable unknowns is set to -1 */
714    
715     /* all undecided unknown strongly coupled to i are moved to F */
716 gross 3440 for (p=0; p<degree_ST[i]; ++p) {
717     j=ST[offset_S[i]+p];
718 gross 3351 if (split_marker[j]==PASO_AMG_UNDECIDED) {
719    
720     split_marker[j]=PASO_AMG_IN_F;
721     lambda[j]=-1;
722    
723 gross 3440 for (q=0; q<degree_ST[j]; ++q) {
724     k=ST[offset_S[j]+q];
725 gross 3351 if (split_marker[k]==PASO_AMG_UNDECIDED) lambda[k]++;
726     }
727    
728     }
729 gross 3283 }
730 gross 3440 for (p=0; p<degree_S[i]; ++p) {
731     j=S[offset_S[i]+p];
732 gross 3351 if(split_marker[j]==PASO_AMG_UNDECIDED) lambda[j]--;
733     }
734    
735 gross 3283 }
736     i=Paso_Util_arg_max(n,lambda);
737     }
738 gross 3440
739 gross 3283 }
740     TMPMEMFREE(lambda);
741     TMPMEMFREE(ST);
742 gross 3440 TMPMEMFREE(degree_ST);
743 gross 3351 TMPMEMFREE(panel);
744     TMPMEMFREE(notInPanel);
745 gross 3193 }
746 gross 3440 /* ensures that two F nodes are connected via a C node :*/
747     void Paso_Preconditioner_AMG_enforceFFConnectivity(const dim_t n, const index_t* offset_S,
748     const dim_t* degree_S, const index_t* S,
749     index_t*split_marker)
750     {
751     dim_t i, p, q;
752    
753     /* now we make sure that two (strongly) connected F nodes are (strongly) connected via a C node. */
754     for (i=0;i<n;++i) {
755     if ( (split_marker[i]==PASO_AMG_IN_F) && (degree_S[i]>0) ) {
756     for (p=0; p<degree_S[i]; ++p) {
757     register index_t j=S[offset_S[i]+p];
758     if ( (split_marker[j]==PASO_AMG_IN_F) && (degree_S[j]>0) ) {
759     /* i and j are now two F nodes which are strongly connected */
760     /* is there a C node they share ? */
761     register index_t sharing=-1;
762     for (q=0; q<degree_S[i]; ++q) {
763     index_t k=S[offset_S[i]+q];
764     if (split_marker[k]==PASO_AMG_IN_C) {
765     register index_t* where_k=(index_t*)bsearch(&k, &(S[offset_S[j]]), degree_S[j], sizeof(index_t), Paso_comparIndex);
766     if (where_k != NULL) {
767     sharing=k;
768     break;
769     }
770     }
771     }
772     if (sharing<0) {
773     if (i<j) {
774     split_marker[j]=PASO_AMG_IN_C;
775     } else {
776     split_marker[i]=PASO_AMG_IN_C;
777     break; /* no point to look any further as i is now a C node */
778     }
779     }
780     }
781     }
782     }
783     }
784     }
785 gross 3445 #endif
786 gross 3442
787 gross 3445 #ifdef DFG
788 gross 3442 void Paso_Preconditioner_AMG_CIJPCoarsening( )
789     {
790 gross 3445
791    
792 gross 3442 const dim_t my_n;
793     const dim_t overlap_n;
794     const dim_t n= my_n + overlap_n;
795 gross 3449
796    
797     /* initialize split_marker and split_marker :*/
798     /* those unknows which are not influenced go into F, the rest is available for F or C */
799     #pragma omp parallel for private(i) schedule(static)
800     for (i=0;i<n;++i) {
801     degree_ST[i]=0;
802     if (degree_S[i]>0) {
803     lambda[i]=0;
804     split_marker[i]=PASO_AMG_UNDECIDED;
805     } else {
806     split_marker[i]=PASO_AMG_IN_F;
807     lambda[i]=-1;
808     }
809     }
810    
811 gross 3442 /* set local lambda + overlap */
812     #pragma omp parallel for private(i)
813     for (i=0; i<n ++i) {
814     w[i]=degree_ST[i];
815     }
816     for (i=0; i<my_n; i++) {
817     w2[i]=random;
818     }
819    
820    
821 gross 3445 /* add noise to w */
822     Paso_Coupler_add(n, w, 1., w2, col_coupler);
823    
824 gross 3442 /* */
825     global_n_C=0;
826     global_n_F=..;
827    
828     while (global_n_C + global_n_F < global_n) {
829    
830    
831     is_in_D[i]=FALSE;
832     /* test local connectivit*/
833 gross 3445 /* w2[i] = max(w[k] | k in S_i or k in S^T_i */
834     #pragma omp parallel for private(i)
835     for (i=0; i<n; ++i) w2[i]=0;
836    
837     for (i=0; i<my_n; ++i) {
838     for( iPtr =0 ; iPtr < degree_S[i]; ++iPtr) {
839     k=S[offset_S[i]+iPtr];
840 gross 3442 w2[i]=MAX(w2[i],w[k]);
841     w2[k]=MAX(w2[k],w[i]);
842 gross 3445 }
843     }
844 gross 3442 /* adjust overlaps by MAX */
845 gross 3445 Paso_Coupler_max(n, w2, col_coupler);
846    
847 gross 3442 /* points with w[i]>w2[i] become C nodes */
848     }
849    
850     }
851 gross 3445 #endif

  ViewVC Help
Powered by ViewVC 1.1.26