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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26