/[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 2550 by gross, Tue Jul 21 03:36:13 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      if (A_p->col_block_size>1) {
92         Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires column block size 1.");
93         return NULL;
94      }
95      if (A_p->row_block_size>1) {
96         Paso_setError(TYPE_ERROR,"Paso_Solver_getAMG: AMG requires row block size 1.");
97         return NULL;
98      }
99        
100    /* identify independend set of rows/columns */    /* identify independend set of rows/columns */
101    mis_marker=TMPMEMALLOC(n,index_t);    mis_marker=TMPMEMALLOC(n,index_t);
# Line 105  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 118  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
118    out->A=Paso_SparseMatrix_getReference(A_p);    out->A=Paso_SparseMatrix_getReference(A_p);
119    /*out->GS=Paso_Solver_getGS(A_p,verbose);*/    /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
120    out->GS=Paso_Solver_getJacobi(A_p);    out->GS=Paso_Solver_getJacobi(A_p);
   /*out->GS->sweeps=2;*/  
121    out->level=level;    out->level=level;
122        
123    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {    if (level==0 || n<=options->min_coarse_matrix_size)
124        out->coarsest_level=TRUE;
125      else
126        out->coarsest_level=FALSE;
127    
128      if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) || level==0 || n<=options->min_coarse_matrix_size) ) {
129       /* identify independend set of rows/columns */       /* identify independend set of rows/columns */
130       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
131       for (i=0;i<n;++i) mis_marker[i]=-1;       for (i=0;i<n;++i) mis_marker[i]=-1;
132       Paso_Pattern_coup(A_p,mis_marker,couplingParam);      
133       if (Paso_noError()) {       if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
134          Paso_Pattern_coup(A_p,mis_marker,options->coarsening_threshold);
135         }
136         else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
137          Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
138         }
139         else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
140          Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
141         }
142         else {
143          /*Default coarseneing*/
144          Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
145         }
146    
147        if (Paso_noError()) {
148          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
149          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
150          out->n=n;          out->n=n;
# Line 146  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 177  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
177                   if (mis_marker[j])                   if (mis_marker[j])
178                       S+=A_p->val[iPtr];                       S+=A_p->val[iPtr];
179                  }                  }
180                     out->inv_A_FF[i]=1./S;                  out->inv_A_FF[i]=1./S;
181                }                }
182    
183             if( Paso_noError()) {             if( Paso_noError()) {
184                /* 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 */
185                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
186                if (level>0 && out->n_F>500) {                
187                 /*if (out->n_F>500) {*/                 /*if (out->n_F>500) {*/
188                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
189                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
# Line 181  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 212  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
212                           if (Paso_noError()) {                           if (Paso_noError()) {
213                              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);
214                              /*find the pattern of the schur complement with fill in*/                              /*find the pattern of the schur complement with fill in*/
215        
216                              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));*/  
                               
217                              /* copy values over*/                              /* copy values over*/
218                              #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)
219                              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 231  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
231                                      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)];
232                                  }                                  }
233                                }                                }
234                              }                              }                                
                                   
235                              if (Paso_noError()) {                              if (Paso_noError()) {
236                                  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);
237                                  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);
238                                  Paso_SparseMatrix_free(schur);                                  Paso_SparseMatrix_free(schur);
239                              }                              }
240                              /* allocate work arrays for AMG application */                              /* allocate work arrays for AMG application */
# Line 237  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 264  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
264                           }                           }
265                       }                       }
266                   }                   }
267                }                
268             }             }
269          }          }
270       }       }
# Line 245  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 272  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
272    TMPMEMFREE(mis_marker);    TMPMEMFREE(mis_marker);
273    TMPMEMFREE(counter);    TMPMEMFREE(counter);
274    if (Paso_noError()) {    if (Paso_noError()) {
275        if (verbose) {        if (verbose && level>0 && !out->coarsest_level) {
276           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);  
          }  
277       }       }
278       return out;       return out;
279    } else  {    } else  {
# Line 267  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 288  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
288    
289       in fact it solves       in fact it solves
290    
291    [      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]
292    [ 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]
293    
294   in the form   in the form
# Line 288  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 309  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
309  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
310       dim_t i;       dim_t i;
311       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
      /*Paso_Solver_GS* GS=NULL;*/  
312       double *x0=MEMALLOC(amg->n,double);       double *x0=MEMALLOC(amg->n,double);
313       double time0=0;       double time0=0;
314         bool_t verbose=0;
315        
316         #ifdef MKL
317         Paso_SparseMatrix *temp=NULL;
318         #endif
319            
320       if (amg->level==0  || amg->n_F<=500) {      
321       /*if (amg->n_F<=500) {*/       if (amg->coarsest_level) {
322          
323        time0=Paso_timer();        time0=Paso_timer();
324                   #ifdef MKL
325          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);  
326          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
327          for (i=0;i<amg->n;++i) {          for (i=0;i<amg->A->len;++i) {
328           x[i]+=x0[i];               temp->val[i]=amg->A->val[i];
329          }          }
330          */          Paso_MKL1(temp,x,b,0);
331          /*Paso_UMFPACK1(amg->A,x,b,0);*/          Paso_SparseMatrix_free(temp);
332           #else
333            #ifdef UMFPACK
334             Paso_UMFPACK1(amg->A,x,b,0);
335            #else
336            Paso_Solver_solveJacobi(amg->GS,x,b);
337            #endif
338           #endif
339          
340         time0=Paso_timer()-time0;         time0=Paso_timer()-time0;
341         /*fprintf(stderr,"timing: DIRECT SOLVER: %e/\n",time0);*/         if (verbose) fprintf(stderr,"timing: DIRECT SOLVER: %e\n\n\n",time0);
342          
343       } else {       } else {
       
344          /* presmoothing */          /* presmoothing */
345             time0=Paso_timer();
346           Paso_Solver_solveJacobi(amg->GS,x,b);           Paso_Solver_solveJacobi(amg->GS,x,b);
347           /*           time0=Paso_timer()-time0;
348           #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];  
           }  
           */  
349          /* end of presmoothing */          /* end of presmoothing */
350                    
351            
352             time0=Paso_timer();
353           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
354           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
355                    
# Line 348  void Paso_Solver_solveAMG(Paso_Solver_AM Line 369  void Paso_Solver_solveAMG(Paso_Solver_AM
369          /* b_C=b_C-A_CF*x_F */          /* b_C=b_C-A_CF*x_F */
370          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);
371                    
372            time0=Paso_timer()-time0;
373            if (verbose) fprintf(stderr,"timing: Before next level: %e\n",time0);
374            
375          /* x_C=AMG(b_C)     */          /* x_C=AMG(b_C)     */
376          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);
377                    
378            time0=Paso_timer();
379          /* b_F=b_F-A_FC*x_C */          /* b_F=b_F-A_FC*x_C */
380          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);
381          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
# Line 366  void Paso_Solver_solveAMG(Paso_Solver_AM Line 391  void Paso_Solver_solveAMG(Paso_Solver_AM
391              }              }
392          }          }
393                    
394       /*postsmoothing*/          time0=Paso_timer()-time0;
395            if (verbose) fprintf(stderr,"timing: After next level: %e\n",time0);
396    
397         /*postsmoothing*/
398         time0=Paso_timer();
399       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
400       for (i=0;i<amg->n;++i) r[i]=b[i];       for (i=0;i<amg->n;++i) r[i]=b[i];
401            
402       /*r=b-Ax*/       /*r=b-Ax */
403       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
404       Paso_Solver_solveJacobi(amg->GS,x0,r);       Paso_Solver_solveJacobi(amg->GS,x0,r);
405            
406       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
407       for (i=0;i<amg->n;++i) x[i]+=x0[i];       for (i=0;i<amg->n;++i) x[i]+=x0[i];
408            
409             time0=Paso_timer()-time0;
410       /*#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];  
411    
      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];  
      }  
      */  
412       /*end of postsmoothing*/       /*end of postsmoothing*/
413            
414       }       }

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

  ViewVC Help
Powered by ViewVC 1.1.26