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

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

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

revision 2360 by artak, Thu Apr 2 04:12:12 2009 UTC revision 2548 by jfenwick, Mon Jul 20 06:20:06 2009 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*******************************************************
3  *  *
4  * Copyright (c) 2003-2008 by University of Queensland  * Copyright (c) 2003-2009 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 with reordering                 */  /* Paso: AMG preconditioner                                  */
18    
19  /**************************************************************/  /**************************************************************/
20    
# Line 24  Line 24 
24    
25  #include "Paso.h"  #include "Paso.h"
26  #include "Solver.h"  #include "Solver.h"
27    #include "Options.h"
28  #include "PasoUtil.h"  #include "PasoUtil.h"
29  #include "UMFPACK.h"  #include "UMFPACK.h"
30    #include "MKL.h"
31    #include "SystemMatrix.h"
32  #include "Pattern_coupling.h"  #include "Pattern_coupling.h"
33    
34  /**************************************************************/  /**************************************************************/
# Line 71  to Line 74  to
74     then AMG is applied to S again until S becomes empty     then AMG is applied to S again until S becomes empty
75    
76  */  */
77  Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,bool_t verbose,dim_t level, double couplingParam) {  Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,dim_t level,Paso_Options* options) {
78    Paso_Solver_AMG* out=NULL;    Paso_Solver_AMG* out=NULL;
79      bool_t verbose=options->verbose;
80    dim_t n=A_p->numRows;    dim_t n=A_p->numRows;
81    dim_t n_block=A_p->row_block_size;    dim_t n_block=A_p->row_block_size;
82    index_t* mis_marker=NULL;      index_t* mis_marker=NULL;  
# Line 81  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 85  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
85    dim_t i,k,j;    dim_t i,k,j;
86    Paso_SparseMatrix * schur=NULL;    Paso_SparseMatrix * schur=NULL;
87    Paso_SparseMatrix * schur_withFillIn=NULL;    Paso_SparseMatrix * schur_withFillIn=NULL;
88    double time0=0,time1=0,time2=0,S=0;    double S=0;
89    /*Paso_Pattern* test;*/    
90      /*Make sure we have block sizes 1*/
91      A_p->pattern->input_block_size=A_p->col_block_size;
92      A_p->pattern->output_block_size=A_p->row_block_size;
93      A_p->pattern->block_size=A_p->block_size;
94        
95    /* identify independend set of rows/columns */    /* identify independend set of rows/columns */
96    mis_marker=TMPMEMALLOC(n,index_t);    mis_marker=TMPMEMALLOC(n,index_t);
# Line 105  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 113  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
113    out->A=Paso_SparseMatrix_getReference(A_p);    out->A=Paso_SparseMatrix_getReference(A_p);
114    /*out->GS=Paso_Solver_getGS(A_p,verbose);*/    /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
115    out->GS=Paso_Solver_getJacobi(A_p);    out->GS=Paso_Solver_getJacobi(A_p);
   /*out->GS->sweeps=2;*/  
116    out->level=level;    out->level=level;
117        
118    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {    if (level==0 || n<=options->min_coarse_matrix_size)
119        out->coarsest_level=TRUE;
120      else
121        out->coarsest_level=FALSE;
122    
123      if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) || level==0 || n<=options->min_coarse_matrix_size) ) {
124       /* identify independend set of rows/columns */       /* identify independend set of rows/columns */
125       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
126       for (i=0;i<n;++i) mis_marker[i]=-1;       for (i=0;i<n;++i) mis_marker[i]=-1;
127       Paso_Pattern_coup(A_p,mis_marker,couplingParam);      
128       if (Paso_noError()) {       if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
129          Paso_Pattern_coup(A_p,mis_marker,options->coarsening_threshold);
130         }
131         else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
132          Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
133         }
134         else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
135          Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
136         }
137         else {
138          /*Default coarseneing*/
139          Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
140         }
141    
142        if (Paso_noError()) {
143          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
144          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
145          out->n=n;          out->n=n;
# Line 146  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 172  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
172                   if (mis_marker[j])                   if (mis_marker[j])
173                       S+=A_p->val[iPtr];                       S+=A_p->val[iPtr];
174                  }                  }
175                     out->inv_A_FF[i]=1./S;                  out->inv_A_FF[i]=1./S;
176                }                }
177    
178             if( Paso_noError()) {             if( Paso_noError()) {
179                /* if there are no nodes in the coarse level there is no more work to do */                /* if there are no nodes in the coarse level there is no more work to do */
180                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
181                if (level>0 && out->n_F>500) {                
182                 /*if (out->n_F>500) {*/                 /*if (out->n_F>500) {*/
183                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
184                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
# Line 181  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 207  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
207                           if (Paso_noError()) {                           if (Paso_noError()) {
208                              schur=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_C,out->rows_in_C,out->mask_C);                              schur=Paso_SparseMatrix_getSubmatrix(A_p,out->n_C,out->n_C,out->rows_in_C,out->mask_C);
209                              /*find the pattern of the schur complement with fill in*/                              /*find the pattern of the schur complement with fill in*/
210        
211                              schur_withFillIn=Paso_SparseMatrix_alloc(A_p->type,Paso_Pattern_binop(PATTERN_FORMAT_DEFAULT, schur->pattern, Paso_Pattern_multiply(PATTERN_FORMAT_DEFAULT,out->A_CF->pattern,out->A_FC->pattern)),1,1);                              schur_withFillIn=Paso_SparseMatrix_alloc(A_p->type,Paso_Pattern_binop(PATTERN_FORMAT_DEFAULT, schur->pattern, Paso_Pattern_multiply(PATTERN_FORMAT_DEFAULT,out->A_CF->pattern,out->A_FC->pattern)),1,1);
                               
                             /*fprintf(stderr,"Sparsity of Schure: %dx%d LEN %d Percentage %f\n",schur_withFillIn->pattern->numOutput,schur_withFillIn->pattern->numInput,schur_withFillIn->len,schur_withFillIn->len/(1.*schur_withFillIn->pattern->numOutput*schur_withFillIn->pattern->numInput));*/  
                               
212                              /* copy values over*/                              /* copy values over*/
213                              #pragma omp parallel for private(i,iPtr,j,iPtr_s,index,where_p) schedule(static)                              #pragma omp parallel for private(i,iPtr,j,iPtr_s,index,where_p) schedule(static)
214                              for (i = 0; i < schur_withFillIn->numRows; ++i) {                              for (i = 0; i < schur_withFillIn->numRows; ++i) {
# Line 203  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 226  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
226                                      schur_withFillIn->val[iPtr]=schur->val[iPtr_s+(index_t)(where_p-index)];                                      schur_withFillIn->val[iPtr]=schur->val[iPtr_s+(index_t)(where_p-index)];
227                                  }                                  }
228                                }                                }
229                              }                              }                                
                                   
230                              if (Paso_noError()) {                              if (Paso_noError()) {
231                                  Paso_Solver_updateIncompleteSchurComplement(schur_withFillIn,out->A_CF,out->inv_A_FF,out->A_FF_pivot,out->A_FC);                                  Paso_Solver_updateIncompleteSchurComplement(schur_withFillIn,out->A_CF,out->inv_A_FF,out->A_FF_pivot,out->A_FC);
232                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1,couplingParam);                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,level-1,options);
233                                  Paso_SparseMatrix_free(schur);                                  Paso_SparseMatrix_free(schur);
234                              }                              }
235                              /* allocate work arrays for AMG application */                              /* allocate work arrays for AMG application */
# Line 237  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 259  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
259                           }                           }
260                       }                       }
261                   }                   }
262                }                
263             }             }
264          }          }
265       }       }
# Line 245  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 267  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
267    TMPMEMFREE(mis_marker);    TMPMEMFREE(mis_marker);
268    TMPMEMFREE(counter);    TMPMEMFREE(counter);
269    if (Paso_noError()) {    if (Paso_noError()) {
270        if (verbose) {        if (verbose && level>0 && !out->coarsest_level) {
271           printf("AMG: %d unknowns eliminated. %d left.\n",out->n_F,n-out->n_F);           printf("AMG: %d unknowns eliminated. %d left.\n",out->n_F,out->n_C);
          if (level>0 && out->n_F>500) {  
         /* if (out->n_F<500) {*/  
             printf("timing: AMG: MIS/reordering/elemination : %e/%e/%e\n",time2,time0,time1);  
          } else {  
             printf("timing: AMG: MIS: %e\n",time2);  
          }  
272       }       }
273       return out;       return out;
274    } else  {    } else  {
# Line 267  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 283  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
283    
284       in fact it solves       in fact it solves
285    
286    [      I         0  ]  [ A_FF 0 ] [ I    invA_FF*A_FF ]  [ x_F ]  = [b_F]    [      I         0  ]  [ A_FF 0 ] [ I    invA_FF*A_FC ]  [ x_F ]  = [b_F]
287    [ A_CF*invA_FF   I  ]  [   0  S ] [ 0          I      ]  [ x_C ]  = [b_C]    [ A_CF*invA_FF   I  ]  [   0  S ] [ 0          I      ]  [ x_C ]  = [b_C]
288    
289   in the form   in the form
# Line 288  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 304  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
304  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
305       dim_t i;       dim_t i;
306       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
      /*Paso_Solver_GS* GS=NULL;*/  
307       double *x0=MEMALLOC(amg->n,double);       double *x0=MEMALLOC(amg->n,double);
308       double time0=0;       double time0=0;
309         bool_t verbose=0;
310        
311         #ifdef MKL
312         Paso_SparseMatrix *temp=NULL;
313         #endif
314            
315       if (amg->level==0  || amg->n_F<=500) {      
316       /*if (amg->n_F<=500) {*/       if (amg->coarsest_level) {
317          
318        time0=Paso_timer();        time0=Paso_timer();
319                   #ifdef MKL
320          Paso_Solver_solveJacobi(amg->GS,x,b);          temp=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, amg->A->pattern,1,1);
           
         /* #pragma omp parallel for private(i) schedule(static)  
         for (i=0;i<amg->n;++i) r[i]=b[i];  
         Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);  
         Paso_Solver_solveGS(amg->GS,x0,r);  
321          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
322          for (i=0;i<amg->n;++i) {          for (i=0;i<amg->A->len;++i) {
323           x[i]+=x0[i];               temp->val[i]=amg->A->val[i];
324          }          }
325          */          Paso_MKL1(temp,x,b,0);
326          /*Paso_UMFPACK1(amg->A,x,b,0);*/          Paso_SparseMatrix_free(temp);
327           #else
328            #ifdef UMFPACK
329             Paso_UMFPACK1(amg->A,x,b,0);
330            #else
331            Paso_Solver_solveJacobi(amg->GS,x,b);
332            #endif
333           #endif
334          
335         time0=Paso_timer()-time0;         time0=Paso_timer()-time0;
336         /*fprintf(stderr,"timing: DIRECT SOLVER: %e/\n",time0);*/         if (verbose) fprintf(stderr,"timing: DIRECT SOLVER: %e\n\n\n",time0);
337          
338       } else {       } else {
       
339          /* presmoothing */          /* presmoothing */
340             time0=Paso_timer();
341           Paso_Solver_solveJacobi(amg->GS,x,b);           Paso_Solver_solveJacobi(amg->GS,x,b);
342           /*           time0=Paso_timer()-time0;
343           #pragma omp parallel for private(i) schedule(static)           if (verbose) fprintf(stderr,"timing: Presmooting: %e\n",time0);
          for (i=0;i<amg->n;++i) r[i]=b[i];  
   
           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);  
           Paso_Solver_solveGS(amg->GS,x0,r);  
             
           #pragma omp parallel for private(i) schedule(static)  
           for (i=0;i<amg->n;++i) {  
            x[i]+=x0[i];  
           }  
           */  
344          /* end of presmoothing */          /* end of presmoothing */
345                    
346            
347             time0=Paso_timer();
348           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
349           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
350                    
# Line 348  void Paso_Solver_solveAMG(Paso_Solver_AM Line 364  void Paso_Solver_solveAMG(Paso_Solver_AM
364          /* b_C=b_C-A_CF*x_F */          /* b_C=b_C-A_CF*x_F */
365          Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_CF,amg->x_F,1.,amg->b_C);          Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_CF,amg->x_F,1.,amg->b_C);
366                    
367            time0=Paso_timer()-time0;
368            if (verbose) fprintf(stderr,"timing: Before next level: %e\n",time0);
369            
370          /* x_C=AMG(b_C)     */          /* x_C=AMG(b_C)     */
371          Paso_Solver_solveAMG(amg->AMG_of_Schur,amg->x_C,amg->b_C);          Paso_Solver_solveAMG(amg->AMG_of_Schur,amg->x_C,amg->b_C);
372                    
373            time0=Paso_timer();
374          /* b_F=b_F-A_FC*x_C */          /* b_F=b_F-A_FC*x_C */
375          Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_FC,amg->x_C,1.,amg->b_F);          Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_FC,amg->x_C,1.,amg->b_F);
376          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
# Line 366  void Paso_Solver_solveAMG(Paso_Solver_AM Line 386  void Paso_Solver_solveAMG(Paso_Solver_AM
386              }              }
387          }          }
388                    
389       /*postsmoothing*/          time0=Paso_timer()-time0;
390            if (verbose) fprintf(stderr,"timing: After next level: %e\n",time0);
391    
392         /*postsmoothing*/
393         time0=Paso_timer();
394       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
395       for (i=0;i<amg->n;++i) r[i]=b[i];       for (i=0;i<amg->n;++i) r[i]=b[i];
396            
397       /*r=b-Ax*/       /*r=b-Ax */
398       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
399       Paso_Solver_solveJacobi(amg->GS,x0,r);       Paso_Solver_solveJacobi(amg->GS,x0,r);
400            
401       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
402       for (i=0;i<amg->n;++i) x[i]+=x0[i];       for (i=0;i<amg->n;++i) x[i]+=x0[i];
403            
404             time0=Paso_timer()-time0;
405       /*#pragma omp parallel for private(i) schedule(static)       if (verbose) fprintf(stderr,"timing: Postsmoothing: %e\n",time0);
      for (i=0;i<amg->n;++i) r[i]=b[i];  
406    
      Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);  
      Paso_Solver_solveGS(amg->GS,x0,r);  
       
      #pragma omp parallel for private(i) schedule(static)  
      for (i=0;i<amg->n;++i) {  
       x[i]+=x0[i];  
      }  
      */  
407       /*end of postsmoothing*/       /*end of postsmoothing*/
408            
409       }       }

Legend:
Removed from v.2360  
changed lines
  Added in v.2548

  ViewVC Help
Powered by ViewVC 1.1.26