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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3446 - (show annotations)
Thu Jan 20 06:47:07 2011 UTC (8 years, 9 months ago) by gross
File MIME type: text/plain
File size: 26921 byte(s)
and more work toward AMG MPI
1
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 (local version) */
18
19 /**************************************************************/
20
21 /* Author: artak@uq.edu.au, l.gross@uq.edu.au */
22
23 /**************************************************************/
24
25 #define SHOW_TIMING FALSE
26
27 #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_AMG_free(Paso_Preconditioner_AMG * in) {
39 if (in!=NULL) {
40 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 MEMFREE(in->r);
46 MEMFREE(in->x_C);
47 MEMFREE(in->b_C);
48
49 MEMFREE(in);
50 }
51 }
52
53 index_t Paso_Preconditioner_AMG_getMaxLevel(const Paso_Preconditioner_AMG * in) {
54 if (in->AMG_C == NULL) {
55 return in->level;
56 } else {
57 return Paso_Preconditioner_AMG_getMaxLevel(in->AMG_C);
58 }
59 }
60 double Paso_Preconditioner_AMG_getCoarseLevelSparsity(const Paso_Preconditioner_AMG * in) {
61 if (in->AMG_C == NULL) {
62 if (in->A_C == NULL) {
63 return 1.;
64 } else {
65 return Paso_SystemMatrix_getSparsity(in->A_C);
66 }
67 } else {
68 return Paso_Preconditioner_AMG_getCoarseLevelSparsity(in->AMG_C);
69 }
70 }
71 dim_t Paso_Preconditioner_AMG_getNumCoarseUnknwons(const Paso_Preconditioner_AMG * in) {
72 if (in->AMG_C == NULL) {
73 if (in->A_C == NULL) {
74 return 0;
75 } else {
76 return Paso_SystemMatrix_getTotalNumRows(in->A_C);
77 }
78 } else {
79 return Paso_Preconditioner_AMG_getNumCoarseUnknwons(in->AMG_C);
80 }
81 }
82 /*****************************************************************
83
84 constructs AMG
85
86 ******************************************************************/
87 Paso_Preconditioner_AMG* Paso_Preconditioner_AMG_alloc(Paso_SystemMatrix *A_p,dim_t level,Paso_Options* options) {
88
89 Paso_Preconditioner_AMG* out=NULL;
90 bool_t verbose=options->verbose;
91 Paso_SystemMatrix *Atemp=NULL, *A_C=NULL;
92
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 const dim_t n_block=A_p->row_block_size;
98 index_t* F_marker=NULL, *counter=NULL, *mask_C=NULL, *rows_in_F=NULL;
99 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 const double sparsity=Paso_SystemMatrix_getSparsity(A_p);
104 const dim_t total_n=Paso_SystemMatrix_getGlobalTotalNumRows(A_p);
105
106
107 /*
108 is the input matrix A suitable for coarsening
109
110 */
111 if ( (sparsity >= options->min_coarse_sparsity) ||
112 (total_n <= options->min_coarse_matrix_size) ||
113 (level > options->level_max) ) {
114
115 if (verbose) {
116 /*
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 printf("Paso_Preconditioner: AMG: termination of coarsening by ");
123
124 if (sparsity >= options->min_coarse_sparsity)
125 printf("SPAR ");
126
127 if (total_n <= options->min_coarse_matrix_size)
128 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 level, options->level_max, sparsity, options->min_coarse_sparsity, total_n, options->min_coarse_matrix_size);
137
138 }
139
140 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
145 F_marker=TMPMEMALLOC(n,index_t);
146 counter=TMPMEMALLOC(n,index_t);
147
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 /*
153 set splitting of unknows:
154
155 */
156 time0=Esys_timer();
157 if (n_block>1) {
158 Paso_Preconditioner_AMG_setStrongConnections_Block(A_p, degree_S, offset_S, S, theta,tau);
159 } else {
160 Paso_Preconditioner_AMG_setStrongConnections(A_p, degree_S, offset_S, S, theta,tau);
161 }
162
163 /*MPI:
164 Paso_Preconditioner_AMG_RungeStuebenSearch(n, A_p->pattern->ptr, degree_S, S, F_marker, options->usePanel);
165 */
166
167 /* in BoomerAMG interpolation is used FF connectiovity is required :*/
168 /*MPI:
169 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 */
172
173 options->coarsening_selection_time=Esys_timer()-time0 + MAX(0, options->coarsening_selection_time);
174
175 #ifdef AAAAA
176 if (Esys_noError() ) {
177 #pragma omp parallel for private(i) schedule(static)
178 for (i = 0; i < n; ++i) F_marker[i]=(F_marker[i] == PASO_AMG_IN_F);
179
180 /*
181 count number of unkowns to be eliminated:
182 */
183 n_F=Paso_Util_cumsum_maskedTrue(n,counter, F_marker);
184 n_C=n-n_F;
185 if (verbose) printf("Paso_Preconditioner: AMG level %d: %d unknowns are flagged for elimination. %d left.\n",level,n_F,n-n_F);
186
187 if ( n_F == 0 ) { /* is a nasty case. a direct solver should be used, return NULL */
188 out = NULL;
189 } else {
190 out=MEMALLOC(1,Paso_Preconditioner_AMG);
191 if (! Esys_checkPtr(out)) {
192 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 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 out->Smoother = Paso_Preconditioner_Smoother_alloc(A_p, (options->smoother == PASO_JACOBI), verbose);
214
215 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
218 /* 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 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 if (F_marker[i]) rows_in_F[counter[i]]=i;
234 }
235 }
236 /* create mask of C nodes with value >-1 gives new id */
237 i=Paso_Util_cumsum_maskedFalse(n,counter, F_marker);
238
239 #pragma omp parallel for private(i) schedule(static)
240 for (i = 0; i < n; ++i) {
241 if (F_marker[i]) {
242 mask_C[i]=-1;
243 } else {
244 mask_C[i]=counter[i];;
245 }
246 }
247 /*
248 get Prolongation :
249 */
250 time0=Esys_timer();
251 /*MPI:
252 out->P=Paso_Preconditioner_AMG_getProlongation(A_p,A_p->pattern->ptr, degree_S,S,n_C,mask_C, options->interpolation_method);
253 */
254 if (SHOW_TIMING) printf("timing: level %d: getProlongation: %e\n",level, Esys_timer()-time0);
255 }
256 /*
257 construct Restriction operator as transposed of Prolongation operator:
258 */
259 if ( Esys_noError()) {
260 time0=Esys_timer();
261 /*MPI:
262 out->R=Paso_SystemMatrix_getTranspose(out->P);
263 */
264 if (SHOW_TIMING) printf("timing: level %d: Paso_SystemMatrix_getTranspose: %e\n",level,Esys_timer()-time0);
265 }
266 /*
267 construct coarse level matrix:
268 */
269 if ( Esys_noError()) {
270 time0=Esys_timer();
271 /*MPI:
272 Atemp=Paso_SystemMatrix_MatrixMatrix(A_p,out->P);
273 A_C=Paso_SystemMatrix_MatrixMatrix(out->R,Atemp);
274
275 Paso_SystemMatrix_free(Atemp);
276 */
277
278 if (SHOW_TIMING) printf("timing: level %d : construct coarse matrix: %e\n",level,Esys_timer()-time0);
279 }
280
281
282 /*
283 constructe courser level:
284
285 */
286 if ( Esys_noError()) {
287 out->AMG_C=Paso_Preconditioner_AMG_alloc(A_C,level+1,options);
288 }
289 if ( Esys_noError()) {
290 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 out->A_C=Paso_SystemMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, A_C);
296 Paso_SystemMatrix_free(A_C);
297 out->A_C->solver_package = PASO_MKL;
298 if (verbose) printf("Paso_Preconditioner: AMG: use MKL direct solver on the coarsest level (number of unknowns = %d).\n",n_C*n_block);
299 #else
300 #ifdef UMFPACK
301 out->A_C=Paso_SystemMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_CSC, A_C);
302 Paso_SystemMatrix_free(A_C);
303 out->A_C->solver_package = PASO_UMFPACK;
304 if (verbose) printf("Paso_Preconditioner: AMG: use UMFPACK direct solver on the coarsest level (number of unknowns = %d).\n",n_C*n_block);
305 #else
306 out->A_C=A_C;
307 out->A_C->solver_p=Paso_Preconditioner_Smoother_alloc(out->A_C, (options->smoother == PASO_JACOBI), verbose);
308 out->A_C->solver_package = PASO_SMOOTHER;
309 if (verbose) printf("Paso_Preconditioner: AMG: use smoother on the coarsest level (number of unknowns = %d).\n",n_C*n_block);
310 #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 #endif
324
325 }
326 TMPMEMFREE(counter);
327 TMPMEMFREE(F_marker);
328 TMPMEMFREE(degree_S);
329 TMPMEMFREE(offset_S);
330 TMPMEMFREE(S);
331 }
332
333 if (Esys_noError()) {
334 return out;
335 } else {
336 Paso_Preconditioner_AMG_free(out);
337 return NULL;
338 }
339 }
340
341
342 void Paso_Preconditioner_AMG_solve(Paso_SystemMatrix* A, Paso_Preconditioner_AMG * amg, double * x, double * b) {
343 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
348 /* presmoothing */
349 time0=Esys_timer();
350 Paso_Preconditioner_Smoother_solve(A, amg->Smoother, x, b, pre_sweeps, FALSE);
351 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 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(-1.,A,x,1.,amg->r); /*r=r-Ax*/
359 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,amg->r,0.,amg->b_C); /* b_c = R*r */
360 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 #ifdef FIXME
366 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 Paso_Preconditioner_Smoother_solve(amg->A_C, amg->A_C->solver_p,amg->x_C,amg->b_C,pre_sweeps+post_sweeps, FALSE);
375 break;
376 }
377 #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 if (SHOW_TIMING) printf("timing: level %d: DIRECT SOLVER: %e\n",amg->level,Esys_timer()-time0);
380 } else {
381 Paso_Preconditioner_AMG_solve(amg->A_C, amg->AMG_C,amg->x_C,amg->b_C); /* x_C=AMG(b_C) */
382 }
383 time0=time0+Esys_timer();
384 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,1.,x); /* x = x + P*x_c */
385
386 /*postsmoothing*/
387
388 /*solve Ax=b with initial guess x */
389 time0=Esys_timer();
390 Paso_Preconditioner_Smoother_solve(A, amg->Smoother, x, b, post_sweeps, TRUE);
391 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
399 /* theta = threshold for strong connections */
400 /* tau = threshold for diagonal dominance */
401
402 /*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 void Paso_Preconditioner_AMG_setStrongConnections(Paso_SystemMatrix* A,
408 dim_t *degree_S, index_t *offset_S, index_t *S,
409 const double theta, const double tau)
410 {
411 const dim_t my_n=Paso_SystemMatrix_getNumRows(A);
412 index_t iptr, i;
413
414
415 #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 #pragma ivdep
421 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
425 if( j != i) {
426 max_offdiagonal = MAX(max_offdiagonal,fnorm);
427 sum_row+=fnorm;
428 } else {
429 main_row=fnorm;
430 }
431
432 }
433 #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 if (tau*main_row < sum_row) { /* no diagonal domainance */
446 #pragma ivdep
447 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 kdeg++;
452 }
453 }
454 #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 }
463 offset_S[i]=koffset;
464 degree_S[i]=kdeg;
465 }
466 }
467
468 /* theta = threshold for strong connections */
469 /* tau = threshold for diagonal dominance */
470 /*S_i={j \in N_i; i strongly coupled to j}
471
472 in the sense that |A_{ij}|_F >= theta * max_k |A_{ik}|_F
473 */
474 void Paso_Preconditioner_AMG_setStrongConnections_Block(Paso_SystemMatrix* A,
475 dim_t *degree_S, index_t *offset_S, index_t *S,
476 const double theta, const double tau)
477
478 {
479 const dim_t my_n=Paso_SystemMatrix_getNumRows(A);
480 index_t iptr, i, bi;
481 const dim_t n_block=A->row_block_size;
482
483
484 #pragma omp parallel private(i,iptr, bi)
485 {
486 dim_t max_deg=0;
487 #pragma omp for schedule(static)
488 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
491 double *rtmp=TMPMEMALLOC(max_deg, double);
492
493 #pragma omp for schedule(static)
494 for (i=0;i<my_n;++i) {
495
496 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
501 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 #pragma ivdep
505 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 fnorm=sqrt(fnorm);
510
511 rtmp[iptr+rtmp_offset]=fnorm;
512 if( j != i) {
513 max_offdiagonal = MAX(max_offdiagonal,fnorm);
514 sum_row+=fnorm;
515 } else {
516 main_row=fnorm;
517 }
518 }
519 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 if (tau*main_row < sum_row) { /* no diagonal domainance */
540 rtmp_offset=-A->mainBlock->pattern->ptr[i];
541 #pragma ivdep
542 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 kdeg++;
547 }
548 }
549 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->ptr[i]; 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 }
560 degree_S[i]=kdeg;
561 offset_S[i]=koffset;
562 }
563 TMPMEMFREE(rtmp);
564 } /* end of parallel region */
565
566 }
567
568 #ifdef AAAAA
569 /* the runge stueben coarsening algorithm: */
570 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 index_t*split_marker, const bool_t usePanel)
573 {
574
575 index_t *lambda=NULL, *ST=NULL, *notInPanel=NULL, *panel=NULL, lambda_max, lambda_k;
576 dim_t i,k, p, q, *degree_ST=NULL, len_panel, len_panel_new;
577 register index_t j, itmp;
578
579 if (n<=0) return; /* make sure that the return of Paso_Util_arg_max is not pointing to nirvana */
580
581 lambda=TMPMEMALLOC(n, index_t); Esys_checkPtr(lambda);
582 degree_ST=TMPMEMALLOC(n, dim_t); Esys_checkPtr(degree_ST);
583 ST=TMPMEMALLOC(offset_S[n], index_t); Esys_checkPtr(ST);
584 if (usePanel) {
585 notInPanel=TMPMEMALLOC(n, bool_t); Esys_checkPtr(notInPanel);
586 panel=TMPMEMALLOC(n, index_t); Esys_checkPtr(panel);
587 }
588
589
590
591 if (Esys_noError() ) {
592 /* 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 degree_ST[i]=0;
597 if (degree_S[i]>0) {
598 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 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 }
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 for (p=0; p<degree_ST[i]; ++p) {
619 j=ST[offset_S[i]+p];
620 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 if (usePanel) {
630 #pragma omp parallel for private(i) schedule(static)
631 for (i=0;i<n;++i) notInPanel[i]=TRUE;
632 }
633 /* start search :*/
634 i=Paso_Util_arg_max(n,lambda);
635 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 for (p=0; p<degree_ST[i]; ++p) {
646 j=ST[offset_S[i]+p];
647
648 if (split_marker[j]==PASO_AMG_UNDECIDED) {
649
650 split_marker[j]=PASO_AMG_IN_F;
651 lambda[j]=-1;
652
653 for (q=0; q<degree_ST[j]; ++q) {
654 k=ST[offset_S[j]+q];
655 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 }
670 for (p=0; p<degree_S[i]; ++p) {
671 j=S[offset_S[i]+p];
672 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
682 /* 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 for (p=0; p<degree_ST[i]; ++p) {
711 j=ST[offset_S[i]+p];
712 if (split_marker[j]==PASO_AMG_UNDECIDED) {
713
714 split_marker[j]=PASO_AMG_IN_F;
715 lambda[j]=-1;
716
717 for (q=0; q<degree_ST[j]; ++q) {
718 k=ST[offset_S[j]+q];
719 if (split_marker[k]==PASO_AMG_UNDECIDED) lambda[k]++;
720 }
721
722 }
723 }
724 for (p=0; p<degree_S[i]; ++p) {
725 j=S[offset_S[i]+p];
726 if(split_marker[j]==PASO_AMG_UNDECIDED) lambda[j]--;
727 }
728
729 }
730 i=Paso_Util_arg_max(n,lambda);
731 }
732
733 }
734 TMPMEMFREE(lambda);
735 TMPMEMFREE(ST);
736 TMPMEMFREE(degree_ST);
737 TMPMEMFREE(panel);
738 TMPMEMFREE(notInPanel);
739 }
740 /* 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 #endif
780
781 #ifdef DFG
782 void Paso_Preconditioner_AMG_CIJPCoarsening( )
783 {
784
785
786 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 /* add noise to w */
800 Paso_Coupler_add(n, w, 1., w2, col_coupler);
801
802 /* */
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 /* 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 w2[i]=MAX(w2[i],w[k]);
819 w2[k]=MAX(w2[k],w[i]);
820 }
821 }
822 /* adjust overlaps by MAX */
823 Paso_Coupler_max(n, w2, col_coupler);
824
825 /* points with w[i]>w2[i] become C nodes */
826 }
827
828 }
829 #endif

  ViewVC Help
Powered by ViewVC 1.1.26