/[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 2520 by artak, Mon Jul 6 03:13:11 2009 UTC
# 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 107  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 115  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
115    out->GS=Paso_Solver_getJacobi(A_p);    out->GS=Paso_Solver_getJacobi(A_p);
116    /*out->GS->sweeps=2;*/    /*out->GS->sweeps=2;*/
117    out->level=level;    out->level=level;
118      
119    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) || level==0 ) ) {
120       /* identify independend set of rows/columns */       /* identify independend set of rows/columns */
121       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
122       for (i=0;i<n;++i) mis_marker[i]=-1;       for (i=0;i<n;++i) mis_marker[i]=-1;
123       Paso_Pattern_coup(A_p,mis_marker,couplingParam);      
124       if (Paso_noError()) {       if (options->coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
125          Paso_Pattern_coup(A_p,mis_marker,options->coarsening_threshold);
126         }
127         else if (options->coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
128          Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
129         }
130         else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
131          Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
132         }
133         else {
134          /*Default coarseneing*/
135          Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);
136         }
137    
138        if (Paso_noError()) {
139          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
140          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
141          out->n=n;          out->n=n;
# Line 146  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 168  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
168                   if (mis_marker[j])                   if (mis_marker[j])
169                       S+=A_p->val[iPtr];                       S+=A_p->val[iPtr];
170                  }                  }
171                     out->inv_A_FF[i]=1./S;                  out->inv_A_FF[i]=1./S;
172                }                }
173    
174             if( Paso_noError()) {             if( Paso_noError()) {
175                /* 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 */
176                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
177                if (level>0 && out->n_F>500) {                if (level>0 && out->n_C>500) {
178                 /*if (out->n_F>500) {*/                 /*if (out->n_F>500) {*/
179                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
180                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
# Line 181  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 203  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
203                           if (Paso_noError()) {                           if (Paso_noError()) {
204                              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);
205                              /*find the pattern of the schur complement with fill in*/                              /*find the pattern of the schur complement with fill in*/
206        
207                              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));*/  
                               
208                              /* copy values over*/                              /* copy values over*/
209                              #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)
210                              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 222  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
222                                      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)];
223                                  }                                  }
224                                }                                }
225                              }                              }                                
                                   
226                              if (Paso_noError()) {                              if (Paso_noError()) {
227                                  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);
228                                  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);
229                                  Paso_SparseMatrix_free(schur);                                  Paso_SparseMatrix_free(schur);
230                              }                              }
231                              /* allocate work arrays for AMG application */                              /* allocate work arrays for AMG application */
# Line 245  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 263  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
263    TMPMEMFREE(mis_marker);    TMPMEMFREE(mis_marker);
264    TMPMEMFREE(counter);    TMPMEMFREE(counter);
265    if (Paso_noError()) {    if (Paso_noError()) {
266        if (verbose) {        if (verbose && level>0) {
267           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);  
          }  
268       }       }
269       return out;       return out;
270    } else  {    } else  {
# Line 267  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 279  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
279    
280       in fact it solves       in fact it solves
281    
282    [      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]
283    [ 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]
284    
285   in the form   in the form
# Line 288  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 300  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
300  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
301       dim_t i;       dim_t i;
302       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
      /*Paso_Solver_GS* GS=NULL;*/  
303       double *x0=MEMALLOC(amg->n,double);       double *x0=MEMALLOC(amg->n,double);
304       double time0=0;       double time0=0;
305         bool_t verbose=0;
306        
307         #ifdef MKL
308         Paso_SparseMatrix *temp=NULL;
309         #endif
310            
311       if (amg->level==0  || amg->n_F<=500) {      
312       /*if (amg->n_F<=500) {*/       if (amg->level==0  || amg->n_C<=500) {
313          
314        time0=Paso_timer();        time0=Paso_timer();
315                   #ifdef MKL
316          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);  
317          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
318          for (i=0;i<amg->n;++i) {          for (i=0;i<amg->A->len;++i) {
319           x[i]+=x0[i];               temp->val[i]=amg->A->val[i];
320          }          }
321          */          Paso_MKL1(temp,x,b,0);
322          /*Paso_UMFPACK1(amg->A,x,b,0);*/          Paso_SparseMatrix_free(temp);
323           #else
324            #ifdef UMFPACK
325             Paso_UMFPACK1(amg->A,x,b,0);
326            #else
327            Paso_Solver_solveJacobi(amg->GS,x,b);
328            #endif
329           #endif
330          
331         time0=Paso_timer()-time0;         time0=Paso_timer()-time0;
332         /*fprintf(stderr,"timing: DIRECT SOLVER: %e/\n",time0);*/         if (verbose) fprintf(stderr,"timing: DIRECT SOLVER: %e\n\n\n",time0);
333          
334       } else {       } else {
       
335          /* presmoothing */          /* presmoothing */
336             time0=Paso_timer();
337           Paso_Solver_solveJacobi(amg->GS,x,b);           Paso_Solver_solveJacobi(amg->GS,x,b);
338           /*           time0=Paso_timer()-time0;
339           #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];  
           }  
           */  
340          /* end of presmoothing */          /* end of presmoothing */
341                    
342            
343             time0=Paso_timer();
344           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
345           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
346                    
# Line 348  void Paso_Solver_solveAMG(Paso_Solver_AM Line 360  void Paso_Solver_solveAMG(Paso_Solver_AM
360          /* b_C=b_C-A_CF*x_F */          /* b_C=b_C-A_CF*x_F */
361          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);
362                    
363            time0=Paso_timer()-time0;
364            if (verbose) fprintf(stderr,"timing: Before next level: %e\n",time0);
365            
366          /* x_C=AMG(b_C)     */          /* x_C=AMG(b_C)     */
367          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);
368                    
369            time0=Paso_timer();
370          /* b_F=b_F-A_FC*x_C */          /* b_F=b_F-A_FC*x_C */
371          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);
372          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
# Line 366  void Paso_Solver_solveAMG(Paso_Solver_AM Line 382  void Paso_Solver_solveAMG(Paso_Solver_AM
382              }              }
383          }          }
384                    
385       /*postsmoothing*/          time0=Paso_timer()-time0;
386            if (verbose) fprintf(stderr,"timing: After next level: %e\n",time0);
387    
388         /*postsmoothing*/
389         time0=Paso_timer();
390       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
391       for (i=0;i<amg->n;++i) r[i]=b[i];       for (i=0;i<amg->n;++i) r[i]=b[i];
392            
393       /*r=b-Ax*/       /*r=b-Ax */
394       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
395       Paso_Solver_solveJacobi(amg->GS,x0,r);       Paso_Solver_solveJacobi(amg->GS,x0,r);
396            
397       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
398       for (i=0;i<amg->n;++i) x[i]+=x0[i];       for (i=0;i<amg->n;++i) x[i]+=x0[i];
399            
400             time0=Paso_timer()-time0;
401       /*#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];  
402    
      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];  
      }  
      */  
403       /*end of postsmoothing*/       /*end of postsmoothing*/
404            
405       }       }

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

  ViewVC Help
Powered by ViewVC 1.1.26