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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3352 by gross, Tue Nov 16 03:58:09 2010 UTC revision 3911 by jfenwick, Thu Jun 14 01:01:03 2012 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*******************************************************
3  *  *
4  * Copyright (c) 2003-2010 by University of Queensland  * Copyright (c) 2003-2012 by University of Queensland
5  * Earth Systems Science Computational Center (ESSCC)  * Earth Systems Science Computational Center (ESSCC)
6  * http://www.uq.edu.au/esscc  * http://www.uq.edu.au/esscc
7  *  *
# Line 14  Line 14 
14    
15  /**************************************************************/  /**************************************************************/
16    
17  /* Paso: AMG preconditioner                                  */  /* Paso: AMG preconditioner  (local version)                  */
18    
19  /**************************************************************/  /**************************************************************/
20    
21  /* Author: artak@uq.edu.au, l.gross@uq.edu.au                                */  /* Author: artak@uq.edu.au, l.gross@uq.edu.au                 */
22    
23  /**************************************************************/  /**************************************************************/
24    
25  #define SHOW_TIMING FALSE  #define SHOW_TIMING FALSE
26    #define MY_DEBUG 0
27    #define MY_DEBUG1 1
28    
29  #include "Paso.h"  #include "Paso.h"
30  #include "Preconditioner.h"  #include "Preconditioner.h"
# Line 30  Line 32 
32  #include "PasoUtil.h"  #include "PasoUtil.h"
33  #include "UMFPACK.h"  #include "UMFPACK.h"
34  #include "MKL.h"  #include "MKL.h"
35    #include<stdio.h>
36    
37    
38  /**************************************************************/  /**************************************************************/
39    
40  /* free all memory used by AMG                                */  /* free all memory used by AMG                                */
41    
42  void Paso_Preconditioner_LocalAMG_free(Paso_Preconditioner_LocalAMG * in) {  void Paso_Preconditioner_AMG_free(Paso_Preconditioner_AMG * in) {
43       if (in!=NULL) {       if (in!=NULL) {
44      Paso_Preconditioner_LocalSmoother_free(in->Smoother);      Paso_Preconditioner_Smoother_free(in->Smoother);
45      Paso_SparseMatrix_free(in->P);      Paso_SystemMatrix_free(in->P);
46      Paso_SparseMatrix_free(in->R);      Paso_SystemMatrix_free(in->R);
47      Paso_SparseMatrix_free(in->A_C);      Paso_SystemMatrix_free(in->A_C);
48      Paso_Preconditioner_LocalAMG_free(in->AMG_C);      Paso_Preconditioner_AMG_free(in->AMG_C);
49      MEMFREE(in->r);      MEMFREE(in->r);
50      MEMFREE(in->x_C);      MEMFREE(in->x_C);
51      MEMFREE(in->b_C);      MEMFREE(in->b_C);
52            Paso_MergedSolver_free(in->merged_solver);
53    
       
54      MEMFREE(in);      MEMFREE(in);
55       }       }
56  }  }
57    
58    index_t Paso_Preconditioner_AMG_getMaxLevel(const Paso_Preconditioner_AMG * in) {
59       if (in->AMG_C == NULL) {
60          return in->level;
61       } else {
62          return Paso_Preconditioner_AMG_getMaxLevel(in->AMG_C);
63       }
64    }
65    double Paso_Preconditioner_AMG_getCoarseLevelSparsity(const Paso_Preconditioner_AMG * in) {
66          if (in->AMG_C == NULL) {
67         if (in->A_C == NULL) {
68            return 1.;
69         } else {
70            return Paso_SystemMatrix_getSparsity(in->A_C);
71         }
72          } else {
73            return Paso_Preconditioner_AMG_getCoarseLevelSparsity(in->AMG_C);
74          }
75    }
76    dim_t Paso_Preconditioner_AMG_getNumCoarseUnknwons(const Paso_Preconditioner_AMG * in) {
77       if (in->AMG_C == NULL) {
78          if (in->A_C == NULL) {
79         return 0;
80          } else {
81         return Paso_SystemMatrix_getTotalNumRows(in->A_C);
82          }
83       } else {
84         return Paso_Preconditioner_AMG_getNumCoarseUnknwons(in->AMG_C);
85       }
86    }
87  /*****************************************************************  /*****************************************************************
88    
89     constructs AMG     constructs AMG
90        
91  ******************************************************************/  ******************************************************************/
92  Paso_Preconditioner_LocalAMG* Paso_Preconditioner_LocalAMG_alloc(Paso_SparseMatrix *A_p,dim_t level,Paso_Options* options) {  Paso_Preconditioner_AMG* Paso_Preconditioner_AMG_alloc(Paso_SystemMatrix *A_p,dim_t level,Paso_Options* options) {
93    
94    Paso_Preconditioner_LocalAMG* out=NULL;    Paso_Preconditioner_AMG* out=NULL;
95      Paso_SystemMatrix *A_C=NULL;
96    bool_t verbose=options->verbose;    bool_t verbose=options->verbose;
97    
98      const dim_t my_n=A_p->mainBlock->numRows;
99      const dim_t overlap_n=A_p->row_coupleBlock->numRows;
100        
101    Paso_SparseMatrix *Atemp=NULL, *A_C=NULL;    const dim_t n = my_n + overlap_n;
102    const dim_t n=A_p->numRows;  
103    const dim_t n_block=A_p->row_block_size;    const dim_t n_block=A_p->row_block_size;
104    index_t* split_marker=NULL, *counter=NULL, *mask_C=NULL, *rows_in_F=NULL, *S=NULL, *degree=NULL;    index_t* F_marker=NULL, *counter=NULL, *mask_C=NULL, *rows_in_F;
105    dim_t n_F=0, n_C=0, i;    dim_t i, my_n_F, my_n_C, n_C, F_flag, *F_set=NULL, global_n_C=0, global_n_F=0, n_F;
106    double time0=0;    double time0=0;
107    const double theta = options->coarsening_threshold;    const double theta = options->coarsening_threshold;
108    const double tau = options->diagonal_dominance_threshold;    const double tau = options->diagonal_dominance_threshold;
109        const double sparsity=Paso_SystemMatrix_getSparsity(A_p);
110        const dim_t global_n=Paso_SystemMatrix_getGlobalNumRows(A_p);
111    
112    
113    /*    /*
114        is the input matrix A suitable for coarsening        is the input matrix A suitable for coarsening?
115                
116    */    */
117    if ( (A_p->pattern->len >= options->min_coarse_sparsity * n * n ) || (n <= options->min_coarse_matrix_size) || (level > options->level_max) ) {    if ( (sparsity >= options->min_coarse_sparsity) ||
118       if (verbose) printf("Paso_Solver: AMG level %d (limit = %d) stopped. sparsity = %e (limit = %e), unknowns = %d (limit = %d)\n",         (global_n <= options->min_coarse_matrix_size) ||
119      level,  options->level_max, A_p->pattern->len/(1.*n * n), options->min_coarse_sparsity, n, options->min_coarse_matrix_size  );           (level > options->level_max) ) {
120       return NULL;  
121    }          if (verbose) {
122              /*
123                  print stopping condition:
124                          - 'SPAR' = min_coarse_matrix_sparsity exceeded
125                          - 'SIZE' = min_coarse_matrix_size exceeded
126                          - 'LEVEL' = level_max exceeded
127              */
128              printf("Paso_Preconditioner: AMG: termination of coarsening by ");
129    
130              if (sparsity >= options->min_coarse_sparsity)
131                  printf("SPAR");
132    
133              if (global_n <= options->min_coarse_matrix_size)
134                  printf("SIZE");
135    
136              if (level > options->level_max)
137                  printf("LEVEL");
138    
139              printf("\n");
140    
141            printf("Paso_Preconditioner: AMG level %d (limit = %d) stopped. sparsity = %e (limit = %e), unknowns = %d (limit = %d)\n",
142               level,  options->level_max, sparsity, options->min_coarse_sparsity, global_n, options->min_coarse_matrix_size);  
143    
144           }
145    
146           return NULL;
147      }  else {
148       /* Start Coarsening : */       /* Start Coarsening : */
149        
150         /* this is the table for strong connections combining mainBlock, col_coupleBlock and row_coupleBlock */
151         const dim_t len_S=A_p->mainBlock->pattern->len + A_p->col_coupleBlock->pattern->len + A_p->row_coupleBlock->pattern->len  + A_p->row_coupleBlock->numRows * A_p->col_coupleBlock->numCols;
152    
153       split_marker=TMPMEMALLOC(n,index_t);       dim_t* degree_S=TMPMEMALLOC(n, dim_t);
154         index_t *offset_S=TMPMEMALLOC(n, index_t);
155         index_t *S=TMPMEMALLOC(len_S, index_t);
156         dim_t* degree_ST=TMPMEMALLOC(n, dim_t);
157         index_t *offset_ST=TMPMEMALLOC(n, index_t);
158         index_t *ST=TMPMEMALLOC(len_S, index_t);
159        
160        
161         F_marker=TMPMEMALLOC(n,index_t);
162       counter=TMPMEMALLOC(n,index_t);       counter=TMPMEMALLOC(n,index_t);
163       degree=TMPMEMALLOC(n, dim_t);  
164       S=TMPMEMALLOC(A_p->pattern->len, index_t);       if ( !( Esys_checkPtr(F_marker) || Esys_checkPtr(counter) || Esys_checkPtr(degree_S) || Esys_checkPtr(offset_S) || Esys_checkPtr(S)
165       if ( !( Esys_checkPtr(split_marker) || Esys_checkPtr(counter) || Esys_checkPtr(degree) || Esys_checkPtr(S) ) ) {      || Esys_checkPtr(degree_ST) || Esys_checkPtr(offset_ST) || Esys_checkPtr(ST) ) ) {
166       /*      /*
167               make sure that corresponding values in the row_coupleBlock and col_coupleBlock are identical
168        */
169            Paso_SystemMatrix_copyColCoupleBlock(A_p);
170            Paso_SystemMatrix_copyRemoteCoupleBlock(A_p, FALSE);
171    
172        /*
173            set splitting of unknows:            set splitting of unknows:
174                  
175           */           */
176       time0=Esys_timer();       time0=Esys_timer();
177       if (n_block>1) {       if (n_block>1) {
178             Paso_Preconditioner_AMG_setStrongConnections_Block(A_p, degree, S, theta,tau);             Paso_Preconditioner_AMG_setStrongConnections_Block(A_p, degree_S, offset_S, S, theta,tau);
179       } else {       } else {
180             Paso_Preconditioner_AMG_setStrongConnections(A_p, degree, S, theta,tau);             Paso_Preconditioner_AMG_setStrongConnections(A_p, degree_S, offset_S, S, theta,tau);
181       }       }
182       Paso_Preconditioner_AMG_RungeStuebenSearch(n, A_p->pattern->ptr, degree, S, split_marker, options->usePanel);       Paso_Preconditioner_AMG_transposeStrongConnections(n, degree_S, offset_S, S, n, degree_ST, offset_ST, ST);
183    /*   Paso_SystemMatrix_extendedRowsForST(A_p, degree_ST, offset_ST, ST);
184     */
185    
186         Paso_Preconditioner_AMG_CIJPCoarsening(n,my_n,F_marker,
187                                degree_S, offset_S, S, degree_ST, offset_ST, ST,
188                            A_p->col_coupler->connector,A_p->col_distribution);
189          
190    
191             /* in BoomerAMG if interpolation is used FF connectivity is required */
192    /*MPI:
193             if (options->interpolation_method == PASO_CLASSIC_INTERPOLATION_WITH_FF_COUPLING)
194                                 Paso_Preconditioner_AMG_enforceFFConnectivity(n, A_p->pattern->ptr, degree_S, S, F_marker);  
195    */
196    
197       options->coarsening_selection_time=Esys_timer()-time0 + MAX(0, options->coarsening_selection_time);       options->coarsening_selection_time=Esys_timer()-time0 + MAX(0, options->coarsening_selection_time);
       
198       if (Esys_noError() ) {       if (Esys_noError() ) {
199          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
200          for (i = 0; i < n; ++i) split_marker[i]= (split_marker[i] == PASO_AMG_IN_F);          for (i = 0; i < n; ++i) F_marker[i]=(F_marker[i] ==  PASO_AMG_IN_F);
201            
202          /*          /*
203             count number of unkowns to be eliminated:             count number of unkowns to be eliminated:
204          */          */
205          n_F=Paso_Util_cumsum_maskedTrue(n,counter, split_marker);          my_n_F=Paso_Util_cumsum_maskedTrue(my_n,counter, F_marker);
206          n_C=n-n_F;          n_F=Paso_Util_cumsum_maskedTrue(n,counter, F_marker);
207          if (verbose) printf("Paso_Solver: AMG level %d: %d unknowns are flagged for elimination. %d left.\n",level,n_F,n-n_F);              /* collect my_n_F values on all processes, a direct solver should
208                        be used if any my_n_F value is 0 */
209          if ( n_F == 0 ) {  /*  is a nasty case. a direct solver should be used, return NULL */              F_set = TMPMEMALLOC(A_p->mpi_info->size, dim_t);
210            #ifdef ESYS_MPI
211                MPI_Allgather(&my_n_F, 1, MPI_INT, F_set, 1, MPI_INT, A_p->mpi_info->comm);
212            #endif
213                global_n_F=0;
214                F_flag = 1;
215                for (i=0; i<A_p->mpi_info->size; i++) {
216                    global_n_F+=F_set[i];
217                    if (F_set[i] == 0) F_flag = 0;
218                }
219                TMPMEMFREE(F_set);
220    
221            my_n_C=my_n-my_n_F;
222                global_n_C=global_n-global_n_F;
223            if (verbose) printf("Paso_Preconditioner: AMG (non-local) level %d: %d unknowns are flagged for elimination. %d left.\n",level,global_n_F,global_n_C);
224    
225            
226    /*          if ( n_F == 0 ) {  is a nasty case. a direct solver should be used, return NULL */
227                if (F_flag == 0) {
228             out = NULL;             out = NULL;
229          } else {          } else {
230             out=MEMALLOC(1,Paso_Preconditioner_LocalAMG);             out=MEMALLOC(1,Paso_Preconditioner_AMG);
231             if (! Esys_checkPtr(out)) {             if (! Esys_checkPtr(out)) {
232            out->level = level;            out->level = level;
           out->n = n;  
           out->n_F = n_F;  
           out->n_block = n_block;  
233            out->A_C = NULL;            out->A_C = NULL;
234            out->P = NULL;              out->P = NULL;  
235            out->R = NULL;                      out->R = NULL;          
# Line 130  Paso_Preconditioner_LocalAMG* Paso_Preco Line 240  Paso_Preconditioner_LocalAMG* Paso_Preco
240            out->b_C = NULL;            out->b_C = NULL;
241            out->AMG_C = NULL;            out->AMG_C = NULL;
242            out->Smoother=NULL;            out->Smoother=NULL;
243                      out->merged_solver=NULL;
244             }             }
245             mask_C=TMPMEMALLOC(n,index_t);             mask_C=TMPMEMALLOC(n,index_t);
246             rows_in_F=TMPMEMALLOC(n_F,index_t);             rows_in_F=TMPMEMALLOC(n_F,index_t);
# Line 137  Paso_Preconditioner_LocalAMG* Paso_Preco Line 248  Paso_Preconditioner_LocalAMG* Paso_Preco
248             Esys_checkPtr(rows_in_F);             Esys_checkPtr(rows_in_F);
249             if ( Esys_noError() ) {             if ( Esys_noError() ) {
250    
251            out->Smoother = Paso_Preconditioner_LocalSmoother_alloc(A_p, (options->smoother == PASO_JACOBI), verbose);            out->Smoother = Paso_Preconditioner_Smoother_alloc(A_p, (options->smoother == PASO_JACOBI), 0, verbose);
252                
253            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 */            if (global_n_C != 0) {
254                /*  create mask of C nodes with value >-1, gives new id */
255                n_C=Paso_Util_cumsum_maskedFalse(n, mask_C, F_marker);
256                /* if nothing has been removed we have a diagonal dominant matrix and we just run a few steps of the smoother */
257        
258              /* allocate helpers :*/              /* allocate helpers :*/
259              out->x_C=MEMALLOC(n_block*n_C,double);              out->x_C=MEMALLOC(n_block*my_n_C,double);
260              out->b_C=MEMALLOC(n_block*n_C,double);              out->b_C=MEMALLOC(n_block*my_n_C,double);
261              out->r=MEMALLOC(n_block*n,double);              out->r=MEMALLOC(n_block*my_n,double);
262                            
263              Esys_checkPtr(out->r);              Esys_checkPtr(out->r);
264              Esys_checkPtr(out->x_C);              Esys_checkPtr(out->x_C);
# Line 156  Paso_Preconditioner_LocalAMG* Paso_Preco Line 270  Paso_Preconditioner_LocalAMG* Paso_Preco
270                 {                 {
271                    #pragma omp for schedule(static)                    #pragma omp for schedule(static)
272                    for (i = 0; i < n; ++i) {                    for (i = 0; i < n; ++i) {
273                   if  (split_marker[i]) rows_in_F[counter[i]]=i;                   if  (F_marker[i]) rows_in_F[counter[i]]=i;
                   }  
                }  
                /*  create mask of C nodes with value >-1 gives new id */  
                i=Paso_Util_cumsum_maskedFalse(n,counter, split_marker);  
   
                #pragma omp parallel for private(i) schedule(static)  
                for (i = 0; i < n; ++i) {  
                   if  (split_marker[i]) {  
                  mask_C[i]=-1;  
                   } else {  
                  mask_C[i]=counter[i];;  
274                    }                    }
275                 }                 }
276                 /*                 /*
277                    get Restriction :                      get Prolongation :    
278                 */                                   */                  
279    
280                 time0=Esys_timer();                 time0=Esys_timer();
281                 out->P=Paso_Preconditioner_AMG_getDirectProlongation(A_p,degree,S,n_C,mask_C);  
282                 if (SHOW_TIMING) printf("timing: level %d: getProlongation: %e\n",level, Esys_timer()-time0);                 out->P=Paso_Preconditioner_AMG_getProlongation(A_p,offset_S, degree_S,S,n_C,mask_C, options->interpolation_method);
283    
284              }              }
285    
286              /*                    /*      
287                 construct Prolongation operator as transposed of restriction operator:                 construct Restriction operator as transposed of Prolongation operator:
288              */              */
289    
290              if ( Esys_noError()) {              if ( Esys_noError()) {
291                 time0=Esys_timer();                 time0=Esys_timer();
292                 out->R=Paso_SparseMatrix_getTranspose(out->P);  
293                 if (SHOW_TIMING) printf("timing: level %d: Paso_SparseMatrix_getTranspose: %e\n",level,Esys_timer()-time0);                 out->R=Paso_Preconditioner_AMG_getRestriction(out->P);
294    
295                   if (SHOW_TIMING) printf("timing: level %d: Paso_SystemMatrix_getTranspose: %e\n",level,Esys_timer()-time0);
296              }                    }      
297              /*              /*
298              construct coarse level matrix:                          construct coarse level matrix:
299              */                          */
300              if ( Esys_noError()) {                          if ( Esys_noError()) {
301                 time0=Esys_timer();                             time0=Esys_timer();
302                 Atemp=Paso_SparseMatrix_MatrixMatrix(A_p,out->P);  
303                 A_C=Paso_SparseMatrix_MatrixMatrix(out->R,Atemp);                             A_C = Paso_Preconditioner_AMG_buildInterpolationOperator(A_p, out->P, out->R);
304                 Paso_SparseMatrix_free(Atemp);  
305                 if (SHOW_TIMING) printf("timing: level %d : construct coarse matrix: %e\n",level,Esys_timer()-time0);                                         if (SHOW_TIMING) printf("timing: level %d : construct coarse matrix: %e\n",level,Esys_timer()-time0);
306              }                          }
307    
               
308              /*              /*
309                 constructe courser level:                 constructe courser level:
310                                
311              */              */
312              if ( Esys_noError()) {              if ( Esys_noError()) {
313                 out->AMG_C=Paso_Preconditioner_LocalAMG_alloc(A_C,level+1,options);                 out->AMG_C=Paso_Preconditioner_AMG_alloc(A_C,level+1,options);
314              }              }
315    
316              if ( Esys_noError()) {              if ( Esys_noError()) {
317                 if ( out->AMG_C == NULL ) {                out->A_C=A_C;
318                    out->reordering = options->reordering;                if ( out->AMG_C == NULL ) {
319                    out->refinements = options->coarse_matrix_refinements;                    /* merge the system matrix into 1 rank when
320                    /* no coarse level matrix has been constructed. use direct solver */                   it's not suitable coarsening due to the
321                    #ifdef MKL                   total number of unknowns are too small */
322                      out->A_C=Paso_SparseMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, A_C);                                out->merged_solver= Paso_MergedSolver_alloc(A_C, options);
323                      Paso_SparseMatrix_free(A_C);                }
                     out->A_C->solver_package = PASO_MKL;  
                     if (verbose) printf("Paso_Solver: AMG: use MKL direct solver on the coarsest level (number of unknowns = %d).\n",n_C);  
                   #else  
                     #ifdef UMFPACK  
                        out->A_C=Paso_SparseMatrix_unroll(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_CSC, A_C);  
                        Paso_SparseMatrix_free(A_C);  
                        out->A_C->solver_package = PASO_UMFPACK;  
                        if (verbose) printf("Paso_Solver: AMG: use UMFPACK direct solver on the coarsest level (number of unknowns = %d).\n",n_C);  
                     #else  
                        out->A_C=A_C;  
                        out->A_C->solver_p=Paso_Preconditioner_LocalSmoother_alloc(out->A_C, (options->smoother == PASO_JACOBI), verbose);  
                        out->A_C->solver_package = PASO_SMOOTHER;  
                        if (verbose) printf("Paso_Solver: AMG: use smoother on the coarsest level (number of unknowns = %d).\n",n_C);  
                     #endif  
                   #endif  
                } else {  
                   /* finally we set some helpers for the solver step */  
                   out->A_C=A_C;  
                }  
324              }                      }        
325            }            }
326             }             }
# Line 238  Paso_Preconditioner_LocalAMG* Paso_Preco Line 328  Paso_Preconditioner_LocalAMG* Paso_Preco
328             TMPMEMFREE(rows_in_F);             TMPMEMFREE(rows_in_F);
329          }          }
330       }       }
331    
332    }    }
333    TMPMEMFREE(counter);    TMPMEMFREE(counter);
334    TMPMEMFREE(split_marker);    TMPMEMFREE(F_marker);
335    TMPMEMFREE(degree);    TMPMEMFREE(degree_S);
336      TMPMEMFREE(offset_S);
337    TMPMEMFREE(S);    TMPMEMFREE(S);
338      TMPMEMFREE(degree_ST);
339      TMPMEMFREE(offset_ST);
340      TMPMEMFREE(ST);
341      
342      }
343    
344    if (Esys_noError()) {    if (Esys_noError()) {
345       return out;       return out;
346    } else  {    } else  {
347       Paso_Preconditioner_LocalAMG_free(out);       Paso_Preconditioner_AMG_free(out);
348       return NULL;       return NULL;
349    }    }
350  }  }
351    
352    
353  void Paso_Preconditioner_LocalAMG_solve(Paso_SparseMatrix* A, Paso_Preconditioner_LocalAMG * amg, double * x, double * b) {  void Paso_Preconditioner_AMG_solve(Paso_SystemMatrix* A, Paso_Preconditioner_AMG * amg, double * x, double * b) {
354       const dim_t n = amg->n * amg->n_block;       const dim_t n = A->mainBlock->numRows * A->mainBlock->row_block_size;
355       double time0=0;       double time0=0;
356       const dim_t post_sweeps=amg->post_sweeps;       const dim_t post_sweeps=amg->post_sweeps;
357       const dim_t pre_sweeps=amg->pre_sweeps;       const dim_t pre_sweeps=amg->pre_sweeps;
358    
359       /* presmoothing */       /* presmoothing */
360       time0=Esys_timer();       time0=Esys_timer();
361       Paso_Preconditioner_LocalSmoother_solve(A, amg->Smoother, x, b, pre_sweeps, FALSE);       Paso_Preconditioner_Smoother_solve(A, amg->Smoother, x, b, pre_sweeps, FALSE);
362    
363       time0=Esys_timer()-time0;       time0=Esys_timer()-time0;
364       if (SHOW_TIMING) printf("timing: level %d: Presmooting: %e\n",amg->level, time0);       if (SHOW_TIMING) printf("timing: level %d: Presmoothing: %e\n",amg->level, time0);
365       /* end of presmoothing */       /* end of presmoothing */
366            
367       if (amg->n_F < amg->n) { /* is there work on the coarse level? */       time0=Esys_timer();
368           time0=Esys_timer();  
369       Paso_Copy(n, amg->r, b);                            /*  r <- b */       Paso_Copy(n, amg->r, b);                            /*  r <- b */
370       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,A,x,1.,amg->r); /*r=r-Ax*/       Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(-1.,A,x,1.,amg->r); /*r=r-Ax*/
371       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0_DIAG(1.,amg->R,amg->r,0.,amg->b_C);  /* b_c = R*r  */       Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,amg->r,0.,amg->b_C);  /* b_c = R*r  */
372           time0=Esys_timer()-time0;  
373       /* coarse level solve */       time0=Esys_timer()-time0;
374       if ( amg->AMG_C == NULL) {       /* coarse level solve */
375         if ( amg->AMG_C == NULL) {
376          time0=Esys_timer();          time0=Esys_timer();
377          /*  A_C is the coarsest level */          /*  A_C is the coarsest level */
378          switch (amg->A_C->solver_package) {              Paso_MergedSolver_solve(amg->merged_solver,amg->x_C, amg->b_C);
            case (PASO_MKL):  
           Paso_MKL(amg->A_C, amg->x_C,amg->b_C, amg->reordering, amg->refinements, SHOW_TIMING);  
           break;  
            case (PASO_UMFPACK):  
           Paso_UMFPACK(amg->A_C, amg->x_C,amg->b_C, amg->refinements, SHOW_TIMING);  
           break;  
            case (PASO_SMOOTHER):  
           Paso_Preconditioner_LocalSmoother_solve(amg->A_C, amg->Smoother,amg->x_C,amg->b_C,pre_sweeps, FALSE);  
           break;  
         }  
379          if (SHOW_TIMING) printf("timing: level %d: DIRECT SOLVER: %e\n",amg->level,Esys_timer()-time0);          if (SHOW_TIMING) printf("timing: level %d: DIRECT SOLVER: %e\n",amg->level,Esys_timer()-time0);
380       } else {       } else {
381          Paso_Preconditioner_LocalAMG_solve(amg->A_C, amg->AMG_C,amg->x_C,amg->b_C); /* x_C=AMG(b_C)     */          Paso_Preconditioner_AMG_solve(amg->A_C, amg->AMG_C,amg->x_C,amg->b_C); /* x_C=AMG(b_C)     */
      }    
      time0=time0+Esys_timer();  
      Paso_SparseMatrix_MatrixVector_CSR_OFFSET0_DIAG(1.,amg->P,amg->x_C,1.,x); /* x = x + P*x_c */      
       
          /*postsmoothing*/  
         
         /*solve Ax=b with initial guess x */  
         time0=Esys_timer();  
         Paso_Preconditioner_LocalSmoother_solve(A, amg->Smoother, x, b, post_sweeps, TRUE);  
         time0=Esys_timer()-time0;  
         if (SHOW_TIMING) printf("timing: level %d: Postsmoothing: %e\n",amg->level,time0);  
         /*end of postsmoothing*/  
       
382       }       }
383       return;  
384        time0=time0+Esys_timer();
385        Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,1.,x); /* x = x + P*x_c */    
386    
387        /*postsmoothing*/
388          
389        /*solve Ax=b with initial guess x */
390        time0=Esys_timer();
391        Paso_Preconditioner_Smoother_solve(A, amg->Smoother, x, b, post_sweeps, TRUE);
392        time0=Esys_timer()-time0;
393        if (SHOW_TIMING) printf("timing: level %d: Postsmoothing: %e\n",amg->level,time0);
394        return;
395  }  }
396    
397  /* theta = threshold for strong connections */  /* theta = threshold for strong connections */
# Line 315  void Paso_Preconditioner_LocalAMG_solve( Line 402  void Paso_Preconditioner_LocalAMG_solve(
402  in the sense that |A_{ij}| >= theta * max_k |A_{ik}|  in the sense that |A_{ij}| >= theta * max_k |A_{ik}|
403  */  */
404    
405  void Paso_Preconditioner_AMG_setStrongConnections(Paso_SparseMatrix* A,  void Paso_Preconditioner_AMG_setStrongConnections(Paso_SystemMatrix* A,
406                        dim_t *degree, index_t *S,                            dim_t *degree_S, index_t *offset_S, index_t *S,
407                        const double theta, const double tau)                        const double theta, const double tau)
408  {  {
    const dim_t n=A->numRows;  
    index_t iptr, i,j;  
    dim_t kdeg;  
    double max_offdiagonal, threshold, sum_row, main_row, fnorm;  
409    
410       const dim_t my_n=A->mainBlock->numRows;
411       const dim_t overlap_n=A->row_coupleBlock->numRows;
412      
413       index_t iptr, i;
414       double *threshold_p=NULL;
415    
416        #pragma omp parallel for private(i,iptr,max_offdiagonal, threshold,j, kdeg, sum_row, main_row, fnorm) schedule(static)     threshold_p = TMPMEMALLOC(2*my_n, double);
417        for (i=0;i<n;++i) {    
418                 #pragma omp parallel for private(i,iptr) schedule(static)
419       max_offdiagonal = 0.;     for (i=0;i<my_n;++i) {        
420       sum_row=0;      
421       main_row=0;       register double max_offdiagonal = 0.;
422         register double sum_row=0;
423         register double main_row=0;
424         register dim_t kdeg=0;
425             register const index_t koffset=A->mainBlock->pattern->ptr[i]+A->col_coupleBlock->pattern->ptr[i];
426    
427            
428         /* collect information for row i: */
429       #pragma ivdep       #pragma ivdep
430       for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {       for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
431          j=A->pattern->index[iptr];          register index_t j=A->mainBlock->pattern->index[iptr];
432          fnorm=ABS(A->val[iptr]);          register double fnorm=ABS(A->mainBlock->val[iptr]);
           
433          if( j != i) {          if( j != i) {
434             max_offdiagonal = MAX(max_offdiagonal,fnorm);             max_offdiagonal = MAX(max_offdiagonal,fnorm);
435             sum_row+=fnorm;             sum_row+=fnorm;
436          } else {          } else {
437             main_row=fnorm;             main_row=fnorm;
438          }          }
439    
440       }       }
441       threshold = theta*max_offdiagonal;  
442       kdeg=0;       #pragma ivdep
443       if (tau*main_row < sum_row) { /* no diagonal domainance */       for (iptr=A->col_coupleBlock->pattern->ptr[i];iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
444          #pragma ivdep          register double fnorm=ABS(A->col_coupleBlock->val[iptr]);
445          for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {  
446             j=A->pattern->index[iptr];          max_offdiagonal = MAX(max_offdiagonal,fnorm);
447             if(ABS(A->val[iptr])>threshold && i!=j) {          sum_row+=fnorm;
           S[A->pattern->ptr[i]+kdeg] = j;  
           kdeg++;  
            }  
         }  
448       }       }
449       degree[i]=kdeg;  
450             /* inspect row i: */
451             {
452            const double threshold = theta*max_offdiagonal;
453                threshold_p[2*i+1]=threshold;
454            if (tau*main_row < sum_row) { /* no diagonal dominance */
455                   threshold_p[2*i]=1;
456               #pragma ivdep
457               for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
458                  register index_t j=A->mainBlock->pattern->index[iptr];
459                  if(ABS(A->mainBlock->val[iptr])>threshold && i!=j) {
460                 S[koffset+kdeg] = j;
461                 kdeg++;
462                  }
463               }
464               #pragma ivdep
465               for (iptr=A->col_coupleBlock->pattern->ptr[i];iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
466                  register index_t j=A->col_coupleBlock->pattern->index[iptr];
467                  if(ABS(A->col_coupleBlock->val[iptr])>threshold) {
468                 S[koffset+kdeg] = j + my_n;
469                 kdeg++;
470                  }
471               }
472                } else {
473                   threshold_p[2*i]=-1;
474                }
475             }
476             offset_S[i]=koffset;
477         degree_S[i]=kdeg;
478        }        }
479    
480          /* now we need to distribute the threshold and the diagonal dominance indicator */
481          if (A->mpi_info->size > 1) {
482    
483              const index_t koffset_0=A->mainBlock->pattern->ptr[my_n]+A->col_coupleBlock->pattern->ptr[my_n]
484                     -A->mainBlock->pattern->ptr[0]-A->col_coupleBlock->pattern->ptr[0];
485          
486              double *remote_threshold=NULL;
487              
488          Paso_Coupler* threshold_coupler=Paso_Coupler_alloc(A->row_coupler->connector  ,2);
489              Paso_Coupler_startCollect(threshold_coupler,threshold_p);
490              Paso_Coupler_finishCollect(threshold_coupler);
491              remote_threshold=threshold_coupler->recv_buffer;
492    
493              #pragma omp parallel for private(i,iptr) schedule(static)
494              for (i=0; i<overlap_n; i++) {
495              const double threshold = remote_threshold[2*i+1];
496              register dim_t kdeg=0;
497                  register const index_t koffset=koffset_0+A->row_coupleBlock->pattern->ptr[i]+A->remote_coupleBlock->pattern->ptr[i];
498                  if (remote_threshold[2*i]>0) {
499            #pragma ivdep
500            for (iptr=A->row_coupleBlock->pattern->ptr[i];iptr<A->row_coupleBlock->pattern->ptr[i+1]; ++iptr) {
501                  register index_t j=A->row_coupleBlock->pattern->index[iptr];
502                  if(ABS(A->row_coupleBlock->val[iptr])>threshold) {
503                 S[koffset+kdeg] = j ;
504                 kdeg++;
505                  }
506            }
507    
508            #pragma ivdep
509            for (iptr=A->remote_coupleBlock->pattern->ptr[i];iptr<A->remote_coupleBlock->pattern->ptr[i+1]; iptr++) {
510              register index_t j=A->remote_coupleBlock->pattern->index[iptr];
511              if(ABS(A->remote_coupleBlock->val[iptr])>threshold && i!=j) {
512                  S[koffset+kdeg] = j + my_n;
513                  kdeg++;
514              }
515            }
516                  }
517                  offset_S[i+my_n]=koffset;
518              degree_S[i+my_n]=kdeg;
519              }
520    
521              Paso_Coupler_free(threshold_coupler);
522         }
523         TMPMEMFREE(threshold_p);
524  }  }
525    
526  /* theta = threshold for strong connections */  /* theta = threshold for strong connections */
# Line 366  void Paso_Preconditioner_AMG_setStrongCo Line 529  void Paso_Preconditioner_AMG_setStrongCo
529    
530  in the sense that |A_{ij}|_F >= theta * max_k |A_{ik}|_F  in the sense that |A_{ij}|_F >= theta * max_k |A_{ik}|_F
531  */  */
532  void Paso_Preconditioner_AMG_setStrongConnections_Block(Paso_SparseMatrix* A,  void Paso_Preconditioner_AMG_setStrongConnections_Block(Paso_SystemMatrix* A,
533                              dim_t *degree, index_t *S,                              dim_t *degree_S, index_t *offset_S, index_t *S,
534                              const double theta, const double tau)                              const double theta, const double tau)
535    
536  {  {
537     const dim_t n_block=A->row_block_size;     const dim_t block_size=A->block_size;
538     const dim_t n=A->numRows;     const dim_t my_n=A->mainBlock->numRows;
539     index_t iptr, i,j, bi;     const dim_t overlap_n=A->row_coupleBlock->numRows;
    dim_t kdeg, max_deg;  
    register double max_offdiagonal, threshold, fnorm, sum_row, main_row;  
    double *rtmp;  
540        
541       index_t iptr, i, bi;
542       double *threshold_p=NULL;
543        
544        #pragma omp parallel private(i,iptr,max_offdiagonal, kdeg, threshold,j, max_deg, fnorm, sum_row, main_row, rtmp)    
545        {     threshold_p = TMPMEMALLOC(2*my_n, double);
546       max_deg=0;  
547       #pragma omp for schedule(static)     #pragma omp parallel private(i,iptr,bi)
548       for (i=0;i<n;++i) max_deg=MAX(max_deg, A->pattern->ptr[i+1]-A->pattern->ptr[i]);     {
549      
550          dim_t max_deg=0;
551          double *rtmp=NULL;
552    
553          #pragma omp for schedule(static)
554          for (i=0;i<my_n;++i) max_deg=MAX(max_deg, A->mainBlock->pattern->ptr[i+1]-A->mainBlock->pattern->ptr[i]
555                         +A->col_coupleBlock->pattern->ptr[i+1]-A->col_coupleBlock->pattern->ptr[i]);
556                
557       rtmp=TMPMEMALLOC(max_deg, double);        rtmp=TMPMEMALLOC(max_deg, double);
558                
559       #pragma omp for schedule(static)        #pragma omp for schedule(static)
560       for (i=0;i<n;++i) {        for (i=0;i<my_n;++i) {        
561         register double max_offdiagonal = 0.;
562         register double sum_row=0;
563         register double main_row=0;
564         register index_t rtmp_offset=-A->mainBlock->pattern->ptr[i];
565         register dim_t kdeg=0;
566         register const index_t koffset=A->mainBlock->pattern->ptr[i]+A->col_coupleBlock->pattern->ptr[i];
567            
568          max_offdiagonal = 0.;       /* collect information for row i: */
569          sum_row=0;       for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
570          main_row=0;          register index_t j=A->mainBlock->pattern->index[iptr];
571          for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {          register double fnorm=0;
572             j=A->pattern->index[iptr];          #pragma ivdep
573             fnorm=0;          for(bi=0;bi<block_size;++bi) {
574             #pragma ivdep              register double  rtmp2= A->mainBlock->val[iptr*block_size+bi];
575             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];             fnorm+=rtmp2*rtmp2;
            fnorm=sqrt(fnorm);  
            rtmp[iptr-A->pattern->ptr[i]]=fnorm;  
            if( j != i) {  
           max_offdiagonal = MAX(max_offdiagonal,fnorm);  
           sum_row+=fnorm;  
            } else {  
           main_row=fnorm;  
            }  
576          }          }
577          threshold = theta*max_offdiagonal;          fnorm=sqrt(fnorm);
578            rtmp[iptr+rtmp_offset]=fnorm;
579            
580            if( j != i) {
581               max_offdiagonal = MAX(max_offdiagonal,fnorm);
582               sum_row+=fnorm;
583            } else {
584               main_row=fnorm;
585            }
586        
587         }
588                
589          kdeg=0;           rtmp_offset+=A->mainBlock->pattern->ptr[i+1]-A->col_coupleBlock->pattern->ptr[i];
590          if (tau*main_row < sum_row) { /* no diagonal domainance */       for (iptr=A->col_coupleBlock->pattern->ptr[i];iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
591            register double fnorm=0;
592            #pragma ivdep
593            for(bi=0;bi<block_size;++bi) {
594               register double rtmp2 = A->col_coupleBlock->val[iptr*block_size+bi];
595               fnorm+=rtmp2*rtmp2;
596            }
597            fnorm=sqrt(fnorm);
598            
599            rtmp[iptr+rtmp_offset]=fnorm;
600            max_offdiagonal = MAX(max_offdiagonal,fnorm);
601            sum_row+=fnorm;
602         }
603        
604          
605         /* inspect row i: */
606         {
607            const double threshold = theta*max_offdiagonal;
608            rtmp_offset=-A->mainBlock->pattern->ptr[i];
609            
610            threshold_p[2*i+1]=threshold;
611            if (tau*main_row < sum_row) { /* no diagonal dominance */
612               threshold_p[2*i]=1;
613               #pragma ivdep
614               for (iptr=A->mainBlock->pattern->ptr[i];iptr<A->mainBlock->pattern->ptr[i+1]; ++iptr) {
615              register index_t j=A->mainBlock->pattern->index[iptr];
616              if(rtmp[iptr+rtmp_offset] > threshold && i!=j) {
617                 S[koffset+kdeg] = j;
618                 kdeg++;
619              }
620               }
621               rtmp_offset+=A->mainBlock->pattern->ptr[i+1]-A->col_coupleBlock->pattern->ptr[i];
622             #pragma ivdep             #pragma ivdep
623             for (iptr=A->pattern->ptr[i];iptr<A->pattern->ptr[i+1]; ++iptr) {             for (iptr=A->col_coupleBlock->pattern->ptr[i];iptr<A->col_coupleBlock->pattern->ptr[i+1]; ++iptr) {
624            j=A->pattern->index[iptr];            register index_t j=A->col_coupleBlock->pattern->index[iptr];
625            if(rtmp[iptr-A->pattern->ptr[i]] > threshold && i!=j) {            if( rtmp[iptr+rtmp_offset] >threshold) {
626               S[A->pattern->ptr[i]+kdeg] = j;               S[koffset+kdeg] = j + my_n;
627               kdeg++;               kdeg++;
628            }            }
629             }             }
630            } else {
631               threshold_p[2*i]=-1;
632          }          }
633          degree[i]=kdeg;       }
634       }             offset_S[i]=koffset;
635       TMPMEMFREE(rtmp);       degree_S[i]=kdeg;
636        } /* end of parallel region */        }
637          TMPMEMFREE(rtmp);
638  }       }
639       /* now we need to distribute the threshold and the diagonal dominance indicator */
640       if (A->mpi_info->size > 1) {
641          
642          const index_t koffset_0=A->mainBlock->pattern->ptr[my_n]+A->col_coupleBlock->pattern->ptr[my_n]
643                                 -A->mainBlock->pattern->ptr[0]-A->col_coupleBlock->pattern->ptr[0];
644          
645          double *remote_threshold=NULL;
646          
647          Paso_Coupler* threshold_coupler=Paso_Coupler_alloc(A->row_coupler->connector  ,2);
648          Paso_Coupler_startCollect(threshold_coupler,threshold_p);
649          Paso_Coupler_finishCollect(threshold_coupler);
650          remote_threshold=threshold_coupler->recv_buffer;
651          
652          #pragma omp parallel for private(i,iptr) schedule(static)
653          for (i=0; i<overlap_n; i++) {
654        
655         const double threshold2 = remote_threshold[2*i+1]*remote_threshold[2*i+1];
656         register dim_t kdeg=0;
657         register const index_t koffset=koffset_0+A->row_coupleBlock->pattern->ptr[i]+A->remote_coupleBlock->pattern->ptr[i];
658         if (remote_threshold[2*i]>0) {
659            #pragma ivdep
660            for (iptr=A->row_coupleBlock->pattern->ptr[i];iptr<A->row_coupleBlock->pattern->ptr[i+1]; ++iptr) {
661               register index_t j=A->row_coupleBlock->pattern->index[iptr];
662               register double fnorm2=0;
663               #pragma ivdepremote_threshold[2*i]
664               for(bi=0;bi<block_size;++bi) {
665              register double rtmp2 = A->row_coupleBlock->val[iptr*block_size+bi];
666              fnorm2+=rtmp2*rtmp2;
667               }
668              
669               if(fnorm2 > threshold2 ) {
670              S[koffset+kdeg] = j ;
671              kdeg++;
672               }
673            }
674    
675            #pragma ivdep
676                for (iptr=A->remote_coupleBlock->pattern->ptr[i];iptr<A->remote_coupleBlock->pattern->ptr[i+1]; ++iptr) {
677                   register index_t j=A->remote_coupleBlock->pattern->index[iptr];
678                   register double fnorm2=0;
679                   #pragma ivdepremote_threshold[2*i]
680                   for(bi=0;bi<block_size;++bi) {
681                      register double rtmp2 = A->remote_coupleBlock->val[iptr*block_size+bi];
682                      fnorm2+=rtmp2*rtmp2;
683                   }
684                   if(fnorm2 > threshold2 && i != j) {
685                      S[koffset+kdeg] = j + my_n;
686                      kdeg++;
687                   }
688                }
689            
690         }
691         offset_S[i+my_n]=koffset;
692         degree_S[i+my_n]=kdeg;
693          }
694          Paso_Coupler_free(threshold_coupler);
695       }
696       TMPMEMFREE(threshold_p);
697    }
698    
699  /* the runge stueben coarsening algorithm: */  void Paso_Preconditioner_AMG_transposeStrongConnections(const dim_t n, const dim_t* degree_S, const index_t* offset_S, const index_t* S,
700  void Paso_Preconditioner_AMG_RungeStuebenSearch(const dim_t n, const index_t* offset,                              const dim_t nT, dim_t* degree_ST, index_t* offset_ST,index_t* ST)
                         const dim_t* degree, const index_t* S,  
                         index_t*split_marker, const bool_t usePanel)  
701  {  {
702         index_t i, j;
703     index_t *lambda=NULL, *ST=NULL, *notInPanel=NULL, *panel=NULL, lambda_max, lambda_k;     dim_t p;
704     dim_t i,k, p, q, *degreeT=NULL, len_panel, len_panel_new;     dim_t len=0;
705     register index_t j, itmp;     #pragma omp parallel for private(i) schedule(static)
706         for (i=0; i<nT ;++i) {
707     if (n<=0) return; /* make sure that the return of Paso_Util_arg_max is not pointing to nirvana */        degree_ST[i]=0;
     
    lambda=TMPMEMALLOC(n, index_t); Esys_checkPtr(lambda);  
    degreeT=TMPMEMALLOC(n, dim_t); Esys_checkPtr(degreeT);  
    ST=TMPMEMALLOC(offset[n], index_t);  Esys_checkPtr(ST);  
    if (usePanel) {  
       notInPanel=TMPMEMALLOC(n, bool_t); Esys_checkPtr(notInPanel);  
       panel=TMPMEMALLOC(n, index_t); Esys_checkPtr(panel);  
708     }     }
709       for (i=0; i<n ;++i) {
710          for (p=0; p<degree_S[i]; ++p) degree_ST[ S[offset_S[i]+p] ]++;
711       }
712       for (i=0; i<nT ;++i) {
713          offset_ST[i]=len;
714          len+=degree_ST[i];
715          degree_ST[i]=0;
716       }
717       for (i=0; i<n ;++i) {
718          for (p=0; p<degree_S[i]; ++p) {
719           j=S[offset_S[i]+p];
720           ST[offset_ST[j]+degree_ST[j]]=i;
721           degree_ST[j]++;
722          }
723       }
724    }
725    
726    int compareindex(const void *a, const void *b)
727    {
728      return (*(int *)a - *(int *)b);
729    }
730    
731    void Paso_Preconditioner_AMG_CIJPCoarsening(const dim_t n, const dim_t my_n, index_t*split_marker,
732                            const dim_t* degree_S, const index_t* offset_S, const index_t* S,
733                            const dim_t* degree_ST, const index_t* offset_ST, const index_t* ST,
734                            Paso_Connector* col_connector, Paso_Distribution* col_dist)
735    {
736       dim_t i, numUndefined,   iter=0;
737      index_t iptr, jptr, kptr;
738      double *random=NULL, *w=NULL, *Status=NULL;
739      index_t * ST_flag=NULL;
740    
741      Paso_Coupler* w_coupler=Paso_Coupler_alloc(col_connector  ,1);
742        
743        w=TMPMEMALLOC(n, double);
744        Status=TMPMEMALLOC(n, double);
745     if (Esys_noError() ) {    random = Paso_Distribution_createRandomVector(col_dist,1);
746        /* initialize  split_marker and split_marker :*/    ST_flag=TMPMEMALLOC(offset_ST[n-1]+ degree_ST[n-1], index_t);
747        /* those unknows which are not influenced go into F, the rest is available for F or C */  
748        #pragma omp parallel for private(i) schedule(static)    #pragma omp parallel for private(i)
749        for (i=0;i<n;++i) {    for (i=0; i< my_n; ++i) {
750       degreeT[i]=0;        w[i]=degree_ST[i]+random[i];
751       if (degree[i]>0) {        if (degree_ST[i] < 1) {
752          lambda[i]=0;         Status[i]=-100; /* F point  */
753          split_marker[i]=PASO_AMG_UNDECIDED;        } else {
754       } else {         Status[i]=1; /* status undefined */
         split_marker[i]=PASO_AMG_IN_F;  
         lambda[i]=-1;  
      }  
755        }        }
756        /* create transpose :*/    }
757        for (i=0;i<n;++i) {  
758          for (p=0; p<degree[i]; ++p) {    #pragma omp parallel for private(i, iptr)
759             j=S[offset[i]+p];    for (i=0; i< n; ++i) {
760             ST[offset[j]+degreeT[j]]=i;        for( iptr =0 ; iptr < degree_ST[i]; ++iptr)  {
761             degreeT[j]++;       ST_flag[offset_ST[i]+iptr]=1;
         }  
762        }        }
763        /* lambda[i] = |undecided k in ST[i]| + 2 * |F-unknown in ST[i]| */    }  
764        #pragma omp parallel for private(i, j, itmp) schedule(static)  
765        for (i=0;i<n;++i) {    
766       if (split_marker[i]==PASO_AMG_UNDECIDED) {    numUndefined = Paso_Distribution_numPositives(Status, col_dist, 1 );
767          itmp=lambda[i];    /* printf(" coarsening loop start: num of undefined rows = %d \n",numUndefined);  */
768          for (p=0; p<degreeT[i]; ++p) {    iter=0;
769             j=ST[offset[i]+p];    while (numUndefined > 0) {
770             if (split_marker[j]==PASO_AMG_UNDECIDED) {       Paso_Coupler_fillOverlap(n, w, w_coupler);
771            itmp++;  
772             } else {  /* at this point there are no C points */        /* calculate the maximum value of neighbours following active strong connections:
773            itmp+=2;          w2[i]=MAX(w[k]) with k in ST[i] or S[i] and (i,k) connection is still active  */      
774          #pragma omp parallel for private(i, iptr)
775          for (i=0; i<my_n; ++i) {
776         if (Status[i]>0) { /* status is still undefined */
777    
778            register bool_t inD=TRUE;
779            const double wi=w[i];
780    
781            for( iptr =0 ; iptr < degree_S[i]; ++iptr) {
782               const index_t k=S[offset_S[i]+iptr];
783               const index_t* start_p = &ST[offset_ST[k]];
784               const index_t* where_p=(index_t*)bsearch(&i, start_p, degree_ST[k], sizeof(index_t), Paso_comparIndex);
785    
786               if (ST_flag[offset_ST[k] + (index_t)(where_p-start_p)]>0) {
787              if (wi <= w[k] ) {
788                 inD=FALSE;
789                 break;
790              }
791             }             }
792            
793            }
794            
795            if (inD) {
796              for( iptr =0 ; iptr < degree_ST[i]; ++iptr) {
797                 const index_t k=ST[offset_ST[i]+iptr];
798                 if ( ST_flag[offset_ST[i]+iptr] > 0 ) {
799                           if (wi <= w[k] ) {
800                   inD=FALSE;
801                   break;
802                }
803                 }
804              }
805            }    
806            if (inD) {
807               Status[i]=0.; /* is in D */
808          }          }
         lambda[i]=itmp;  
809       }       }
810        
811        }        }
       if (usePanel) {  
      #pragma omp parallel for private(i) schedule(static)  
      for (i=0;i<n;++i) notInPanel[i]=TRUE;  
       }  
       /* start search :*/  
       i=Paso_Util_arg_max(n,lambda);  
       while (lambda[i]>-1) { /* is there any undecided unknown? */  
   
      if (usePanel) {  
         len_panel=0;  
         do {  
            /* the unknown i is moved to C */  
            split_marker[i]=PASO_AMG_IN_C;  
            lambda[i]=-1;  /* lambda from unavailable unknowns is set to -1 */  
             
            /* all undecided unknown strongly coupled to i are moved to F */  
            for (p=0; p<degreeT[i]; ++p) {  
           j=ST[offset[i]+p];  
             
           if (split_marker[j]==PASO_AMG_UNDECIDED) {  
               
              split_marker[j]=PASO_AMG_IN_F;  
              lambda[j]=-1;  
               
              for (q=0; q<degreeT[j]; ++q) {  
             k=ST[offset[j]+q];  
             if (split_marker[k]==PASO_AMG_UNDECIDED) {  
                lambda[k]++;  
                if (notInPanel[k]) {  
                   notInPanel[k]=FALSE;  
                   panel[len_panel]=k;  
                   len_panel++;  
                }  
812    
813              }    /* the unknown i is moved to C */        Paso_Coupler_fillOverlap(n, Status, w_coupler);
814              split_marker[i]=PASO_AMG_IN_C;  
815              lambda[i]=-1;  /* lambda from unavailable unknowns is set to -1 */  
816               }       /*   remove connection to D points :
817                    
818               for each i in D:
819              for each j in S_i:
820                 w[j]--
821                 ST_tag[j,i]=-1
822              for each j in ST[i]:
823                 ST_tag[i,j]=-1
824                 for each k in ST[j]:
825                if k in ST[i]:
826                   w[j]--;
827                ST_tag[j,k]=-1
828                
829         */
830         /* w is updated  for local rows only */
831         {
832            #pragma omp parallel for private(i, jptr)
833            for (i=0; i< my_n; ++i) {
834    
835               for (jptr=0; jptr<degree_ST[i]; ++jptr) {
836              const index_t j=ST[offset_ST[i]+jptr];
837              if ( (Status[j] == 0.) && (ST_flag[offset_ST[i]+jptr]>0) ) {
838                 w[i]--;
839                 ST_flag[offset_ST[i]+jptr]=-1;
840            }            }
841             }             }
842             for (p=0; p<degree[i]; ++p) {            
843            j=S[offset[i]+p];          }
844            if (split_marker[j]==PASO_AMG_UNDECIDED) {          #pragma omp parallel for private(i, jptr)
845               lambda[j]--;          for (i=my_n; i< n; ++i) {
846               if (notInPanel[j]) {             for (jptr=0; jptr<degree_ST[i]; ++jptr) {
847              notInPanel[j]=FALSE;            const index_t j = ST[offset_ST[i]+jptr];
848              panel[len_panel]=j;            if ( Status[j] == 0. ) ST_flag[offset_ST[i]+jptr]=-1;
             len_panel++;  
              }  
           }  
849             }             }
850            }
851    
852            
853            for (i=0; i< n; ++i) {
854               if ( Status[i] == 0. ) {
855    
856             /* consolidate panel */                       const index_t* start_p = &ST[offset_ST[i]];
857             /* remove lambda[q]=-1 */  
858             lambda_max=-1;               for (jptr=0; jptr<degree_ST[i]; ++jptr) {
859             i=-1;              const index_t j=ST[offset_ST[i]+jptr];
860             len_panel_new=0;              ST_flag[offset_ST[i]+jptr]=-1;
861             for (q=0; q<len_panel; q++) {              for (kptr=0; kptr<degree_ST[j]; ++kptr) {
862               k=panel[q];                 const index_t k=ST[offset_ST[j]+kptr];
863               lambda_k=lambda[k];                 if (NULL != bsearch(&k, start_p, degree_ST[i], sizeof(index_t), Paso_comparIndex) ) { /* k in ST[i] ? */
864               if (split_marker[k]==PASO_AMG_UNDECIDED) {                    if (ST_flag[offset_ST[j]+kptr] >0) {
865              panel[len_panel_new]=k;                   if (j< my_n ) {
866              len_panel_new++;                      w[j]--;
867                     }
868              if (lambda_max == lambda_k) {                   ST_flag[offset_ST[j]+kptr]=-1;
869                 if (k<i) i=k;                    }
870              } else if (lambda_max < lambda_k) {                 }
                lambda_max =lambda_k;  
                i=k;  
871              }              }
872               }               }
            }  
            len_panel=len_panel_new;  
         } while (len_panel>0);      
      } else {  
         /* the unknown i is moved to C */  
         split_marker[i]=PASO_AMG_IN_C;  
         lambda[i]=-1;  /* lambda from unavailable unknowns is set to -1 */  
           
         /* all undecided unknown strongly coupled to i are moved to F */  
         for (p=0; p<degreeT[i]; ++p) {  
            j=ST[offset[i]+p];  
            if (split_marker[j]==PASO_AMG_UNDECIDED) {  
           
           split_marker[j]=PASO_AMG_IN_F;  
           lambda[j]=-1;  
           
           for (q=0; q<degreeT[j]; ++q) {  
              k=ST[offset[j]+q];  
              if (split_marker[k]==PASO_AMG_UNDECIDED) lambda[k]++;  
873            }            }
   
            }  
874          }          }
875          for (p=0; p<degree[i]; ++p) {       }
876             j=S[offset[i]+p];  
877             if(split_marker[j]==PASO_AMG_UNDECIDED) lambda[j]--;       /* adjust status */
878         #pragma omp parallel for private(i)
879         for (i=0; i< my_n; ++i) {
880            if ( Status[i] == 0. ) {
881               Status[i] = -10;   /* this is now a C point */
882            } else if (Status[i] == 1. && w[i]<1.) {
883               Status[i] = -100;   /* this is now a F point */  
884          }          }
           
885       }       }
886       i=Paso_Util_arg_max(n,lambda);      
887        }       i = numUndefined;
888     }       numUndefined = Paso_Distribution_numPositives(Status, col_dist, 1 );
889     TMPMEMFREE(lambda);       if (numUndefined == i) {
890     TMPMEMFREE(ST);         Esys_setError(SYSTEM_ERROR, "Can NOT reduce numUndefined.");
891     TMPMEMFREE(degreeT);         return;
892     TMPMEMFREE(panel);       }
893     TMPMEMFREE(notInPanel);  
894         iter++;
895         /* printf(" coarsening loop %d: num of undefined rows = %d \n",iter, numUndefined); */
896    
897      } /* end of while loop */
898    
899      /* map to output :*/
900      Paso_Coupler_fillOverlap(n, Status, w_coupler);
901      #pragma omp parallel for private(i)
902      for (i=0; i< n; ++i) {
903         if (Status[i] > -50.) {
904            split_marker[i]=PASO_AMG_IN_C;
905         } else {
906            split_marker[i]=PASO_AMG_IN_F;
907         }
908      }
909      /* clean up : */
910      Paso_Coupler_free(w_coupler);
911      TMPMEMFREE(random);
912      TMPMEMFREE(w);
913      TMPMEMFREE(Status);
914      TMPMEMFREE(ST_flag);
915      
916      return;
917  }  }
918    

Legend:
Removed from v.3352  
changed lines
  Added in v.3911

  ViewVC Help
Powered by ViewVC 1.1.26