/[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 2507 by artak, Thu Jul 2 03:14:35 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,bool_t verbose,dim_t level, double coarsening_threshold, dim_t coarsening_method) {
78    Paso_Solver_AMG* out=NULL;    Paso_Solver_AMG* out=NULL;
79    dim_t n=A_p->numRows;    dim_t n=A_p->numRows;
80    dim_t n_block=A_p->row_block_size;    dim_t n_block=A_p->row_block_size;
# Line 81  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 84  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
84    dim_t i,k,j;    dim_t i,k,j;
85    Paso_SparseMatrix * schur=NULL;    Paso_SparseMatrix * schur=NULL;
86    Paso_SparseMatrix * schur_withFillIn=NULL;    Paso_SparseMatrix * schur_withFillIn=NULL;
87    double time0=0,time1=0,time2=0,S=0;    double S=0;
88    /*Paso_Pattern* test;*/    double time0;
89      
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()) {       time0=Paso_timer();
125    
126         if (coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
127          Paso_Pattern_coup(A_p,mis_marker,coarsening_threshold);
128         }
129         else if (coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
130          Paso_Pattern_RS(A_p,mis_marker,coarsening_threshold);
131         }
132         else if (coarsening_method == PASO_AGGREGATION_COARSENING) {
133          Paso_Pattern_Aggregiation(A_p,mis_marker,coarsening_threshold);
134         }
135         else {
136          /*Default coarseneing*/
137          Paso_Pattern_RS(A_p,mis_marker,coarsening_threshold);
138         }
139        
140         time0=Paso_timer()-time0;
141         if (verbose) fprintf(stderr,"timing: Coarseneing: %e\n",time0);
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) {                if (level>0 && out->n_C>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                                time0=Paso_timer();
212                              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);
213                                                            time0=Paso_timer()-time0;
214                              /*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));*/                              if (verbose) fprintf(stderr,"timing: Fill_in pattern computation: %e\n",time0);
215                                    
216                                time0=Paso_timer();
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 204  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 232  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
232                                  }                                  }
233                                }                                }
234                              }                              }
235                                time0=Paso_timer()-time0;
236                                if (verbose) fprintf(stderr,"timing: Copying values for Fill in: %e\n",time0);
237                                                                    
238                              if (Paso_noError()) {                              if (Paso_noError()) {
239                                  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);
240                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1,couplingParam);                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1,coarsening_threshold,coarsening_method);
241                                  Paso_SparseMatrix_free(schur);                                  Paso_SparseMatrix_free(schur);
242                              }                              }
243                              /* allocate work arrays for AMG application */                              /* allocate work arrays for AMG application */
# Line 245  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 275  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
275    TMPMEMFREE(mis_marker);    TMPMEMFREE(mis_marker);
276    TMPMEMFREE(counter);    TMPMEMFREE(counter);
277    if (Paso_noError()) {    if (Paso_noError()) {
278        if (verbose) {        if (verbose && level>0) {
279           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);  
          }  
280       }       }
281       return out;       return out;
282    } else  {    } else  {
# Line 267  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 291  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
291    
292       in fact it solves       in fact it solves
293    
294    [      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]
295    [ 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]
296    
297   in the form   in the form
# Line 288  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 312  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
312  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
313       dim_t i;       dim_t i;
314       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
      /*Paso_Solver_GS* GS=NULL;*/  
315       double *x0=MEMALLOC(amg->n,double);       double *x0=MEMALLOC(amg->n,double);
316       double time0=0;       double time0=0;
317         bool_t verbose=1;
318        
319         #ifdef MKL
320         Paso_SparseMatrix *temp=NULL;
321         #endif
322            
323       if (amg->level==0  || amg->n_F<=500) {      
324         if (amg->level==0  || amg->n_C<=500) {
325          
326       /*if (amg->n_F<=500) {*/       /*if (amg->n_F<=500) {*/
327        time0=Paso_timer();        time0=Paso_timer();
328          
329                    
330          Paso_Solver_solveJacobi(amg->GS,x,b);         /* Paso_Solver_solveJacobi(amg->GS,x,b);*/
331                    
332          /* #pragma omp parallel for private(i) schedule(static)          /* #pragma omp parallel for private(i) schedule(static)
333          for (i=0;i<amg->n;++i) r[i]=b[i];          for (i=0;i<amg->n;++i) r[i]=b[i];
# Line 307  void Paso_Solver_solveAMG(Paso_Solver_AM Line 338  void Paso_Solver_solveAMG(Paso_Solver_AM
338           x[i]+=x0[i];           x[i]+=x0[i];
339          }          }
340          */          */
341          /*Paso_UMFPACK1(amg->A,x,b,0);*/        
342           #ifdef MKL
343            /*dim_t iptr;
344            temp=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, amg->A->pattern,1,1);
345            #pragma omp parallel for private(i,iptr) schedule(static)
346            for (i=0;i<amg->A->numRows;++i) {
347             for (iptr=amg->A->pattern->ptr[i];iptr<amg->A->pattern->ptr[i+1]; ++iptr)
348                 temp->val[iptr]=amg->A->val[iptr];
349            }*/
350            temp=Paso_SparseMatrix_alloc(MATRIX_FORMAT_BLK1 + MATRIX_FORMAT_OFFSET1, amg->A->pattern,1,1);
351            #pragma omp parallel for private(i) schedule(static)
352            for (i=0;i<amg->A->len;++i) {
353                 temp->val[i]=amg->A->val[i];
354            }
355            Paso_MKL1(temp,x,b,0);
356            Paso_SparseMatrix_free(temp);
357           #else
358            #ifdef UMFPACK
359             Paso_UMFPACK1(amg->A,x,b,0);
360            #else
361            Paso_Solver_solveJacobi(amg->GS,x,b);
362            #endif
363           #endif
364          
365         time0=Paso_timer()-time0;         time0=Paso_timer()-time0;
366         /*fprintf(stderr,"timing: DIRECT SOLVER: %e/\n",time0);*/         if (verbose) fprintf(stderr,"timing: DIRECT SOLVER: %e\n\n\n",time0);
367          
368       } else {       } else {
       
369          /* presmoothing */          /* presmoothing */
370             time0=Paso_timer();
371           Paso_Solver_solveJacobi(amg->GS,x,b);           Paso_Solver_solveJacobi(amg->GS,x,b);
372           /*           time0=Paso_timer()-time0;
373           #pragma omp parallel for private(i) schedule(static)           if (verbose) fprintf(stderr,"timing: Presmooting: %e\n",time0);
374             /* #pragma omp parallel for private(i) schedule(static)
375           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
376    
377            Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);            Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
378            Paso_Solver_solveGS(amg->GS,x0,r);            Paso_Solver_solveJacobi(amg->GS,x0,r);
379                        
380            #pragma omp parallel for private(i) schedule(static)            #pragma omp parallel for private(i) schedule(static)
381            for (i=0;i<amg->n;++i) {            for (i=0;i<amg->n;++i) {
382             x[i]+=x0[i];             x[i]+=x0[i];
383            }            }
384            */           */
385          /* end of presmoothing */          /* end of presmoothing */
386                    
387            
388             time0=Paso_timer();
389           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
390           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
391                    
# Line 348  void Paso_Solver_solveAMG(Paso_Solver_AM Line 405  void Paso_Solver_solveAMG(Paso_Solver_AM
405          /* b_C=b_C-A_CF*x_F */          /* b_C=b_C-A_CF*x_F */
406          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);
407                    
408            time0=Paso_timer()-time0;
409            if (verbose) fprintf(stderr,"timing: Before next level: %e\n",time0);
410            
411          /* x_C=AMG(b_C)     */          /* x_C=AMG(b_C)     */
412          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);
413                    
414            time0=Paso_timer();
415          /* b_F=b_F-A_FC*x_C */          /* b_F=b_F-A_FC*x_C */
416          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);
417          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
# Line 366  void Paso_Solver_solveAMG(Paso_Solver_AM Line 427  void Paso_Solver_solveAMG(Paso_Solver_AM
427              }              }
428          }          }
429                    
430       /*postsmoothing*/          time0=Paso_timer()-time0;
431            if (verbose) fprintf(stderr,"timing: After next level: %e\n",time0);
432    
433         /*postsmoothing*/
434         time0=Paso_timer();
435       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
436       for (i=0;i<amg->n;++i) r[i]=b[i];       for (i=0;i<amg->n;++i) r[i]=b[i];
437            
438       /*r=b-Ax*/       /*r=b-Ax */
439       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
440       Paso_Solver_solveJacobi(amg->GS,x0,r);       Paso_Solver_solveJacobi(amg->GS,x0,r);
441            
442       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
443       for (i=0;i<amg->n;++i) x[i]+=x0[i];       for (i=0;i<amg->n;++i) x[i]+=x0[i];
444            
445             time0=Paso_timer()-time0;
446       /*#pragma omp parallel for private(i) schedule(static)       if (verbose) fprintf(stderr,"timing: Postsmoothing: %e\n",time0);
447         /*
448         #pragma omp parallel for private(i) schedule(static)
449       for (i=0;i<amg->n;++i) r[i]=b[i];       for (i=0;i<amg->n;++i) r[i]=b[i];
450        
451       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
452       Paso_Solver_solveGS(amg->GS,x0,r);       Paso_Solver_solveJacobi(amg->GS,x0,r);
453            
454       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
455       for (i=0;i<amg->n;++i) {       for (i=0;i<amg->n;++i) {

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

  ViewVC Help
Powered by ViewVC 1.1.26