/[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 2475 by artak, Wed Jun 17 01:48:46 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 "Pattern_coupling.h"  #include "Pattern_coupling.h"
# Line 71  to Line 72  to
72     then AMG is applied to S again until S becomes empty     then AMG is applied to S again until S becomes empty
73    
74  */  */
75  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) {
76    Paso_Solver_AMG* out=NULL;    Paso_Solver_AMG* out=NULL;
77    dim_t n=A_p->numRows;    dim_t n=A_p->numRows;
78    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 82  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
82    dim_t i,k,j;    dim_t i,k,j;
83    Paso_SparseMatrix * schur=NULL;    Paso_SparseMatrix * schur=NULL;
84    Paso_SparseMatrix * schur_withFillIn=NULL;    Paso_SparseMatrix * schur_withFillIn=NULL;
85    double time0=0,time1=0,time2=0,S=0;    double S=0;
86    /*Paso_Pattern* test;*/    
87      /*Make sure we have block sizes 1*/
88      A_p->pattern->input_block_size=A_p->col_block_size;
89      A_p->pattern->output_block_size=A_p->row_block_size;
90      A_p->pattern->block_size=A_p->block_size;
91        
92    /* identify independend set of rows/columns */    /* identify independend set of rows/columns */
93    mis_marker=TMPMEMALLOC(n,index_t);    mis_marker=TMPMEMALLOC(n,index_t);
# Line 107  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 112  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
112    out->GS=Paso_Solver_getJacobi(A_p);    out->GS=Paso_Solver_getJacobi(A_p);
113    /*out->GS->sweeps=2;*/    /*out->GS->sweeps=2;*/
114    out->level=level;    out->level=level;
115      
116    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) || level==0 ) ) {
117       /* identify independend set of rows/columns */       /* identify independend set of rows/columns */
118       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
119       for (i=0;i<n;++i) mis_marker[i]=-1;       for (i=0;i<n;++i) mis_marker[i]=-1;
120       Paso_Pattern_coup(A_p,mis_marker,couplingParam);      
121       if (Paso_noError()) {       /*Paso_Pattern_RS(A_p,mis_marker,coarsening_threshold);*/
122         /*fprintf(stderr,"\n Coarsening method == %d, %d \n",coarsening_method,PASO_RUGE_STUEBEN_COARSENING);*/
123         if (coarsening_method == PASO_YAIR_SHAPIRA_COARSENING) {
124          Paso_Pattern_coup(A_p,mis_marker,coarsening_threshold);
125         }
126         else if (coarsening_method == PASO_RUGE_STUEBEN_COARSENING) {
127          Paso_Pattern_RS(A_p,mis_marker,coarsening_threshold);
128         }
129         else if (coarsening_method == PASO_AGGREGATION_COARSENING) {
130          Paso_Pattern_Aggregiation(A_p,mis_marker,coarsening_threshold);
131         }
132         else {
133          /*Default coarseneing*/
134          Paso_Pattern_RS(A_p,mis_marker,coarsening_threshold);
135         }
136        
137        if (Paso_noError()) {
138          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
139          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
140          out->n=n;          out->n=n;
# Line 146  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 167  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
167                   if (mis_marker[j])                   if (mis_marker[j])
168                       S+=A_p->val[iPtr];                       S+=A_p->val[iPtr];
169                  }                  }
170                     out->inv_A_FF[i]=1./S;                  out->inv_A_FF[i]=1./S;
171                }                }
172    
173             if( Paso_noError()) {             if( Paso_noError()) {
174                /* 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 */
175                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
176                if (level>0 && out->n_F>500) {                if (level>0 && out->n_C>500) {
177                 /*if (out->n_F>500) {*/                 /*if (out->n_F>500) {*/
178                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
179                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
# Line 181  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 202  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
202                           if (Paso_noError()) {                           if (Paso_noError()) {
203                              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);
204                              /*find the pattern of the schur complement with fill in*/                              /*find the pattern of the schur complement with fill in*/
   
205                              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);
206                                                          
                             /*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));*/  
                               
207                              /* copy values over*/                              /* copy values over*/
208                              #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)
209                              for (i = 0; i < schur_withFillIn->numRows; ++i) {                              for (i = 0; i < schur_withFillIn->numRows; ++i) {
210                                for (iPtr=schur_withFillIn->pattern->ptr[i];iPtr<schur_withFillIn->pattern->ptr[i + 1]; ++iPtr) {                                for (iPtr=schur_withFillIn->pattern->ptr[i];iPtr<schur_withFillIn->pattern->ptr[i + 1]; ++iPtr) {
211                                  j=schur_withFillIn->pattern->index[iPtr];                                  j=schur_withFillIn->pattern->index[iPtr];
212                                  iPtr_s=schur->pattern->ptr[i];                                  iPtr_s=schur->pattern->ptr[i];
213                                  schur_withFillIn->val[iPtr]=0.;                                  /*schur_withFillIn->val[iPtr]=0.;*/
214                                  index=&(schur->pattern->index[iPtr_s]);                                  index=&(schur->pattern->index[iPtr_s]);
215                                  where_p=(index_t*)bsearch(&j,                                  where_p=(index_t*)bsearch(&j,
216                                          index,                                          index,
# Line 207  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 225  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
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,verbose,level-1,coarsening_threshold,coarsening_method);
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);
268           if (level>0 && out->n_F>500) {           /*if (level>0 && out->n_F>500) {*/
269          /* if (out->n_F<500) {*/          /* if (out->n_F<500) {*/
270              printf("timing: AMG: MIS/reordering/elemination : %e/%e/%e\n",time2,time0,time1);          /*   printf("timing: AMG: MIS/reordering/elemination : %e/%e\n",time0,time1);
271           } else {           } else {
272              printf("timing: AMG: MIS: %e\n",time2);              printf("timing: AMG: MIS: %e\n",time2);
273           }           }*/
274       }       }
275       return out;       return out;
276    } else  {    } else  {
# Line 267  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 285  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
285    
286       in fact it solves       in fact it solves
287    
288    [      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]
289    [ 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]
290    
291   in the form   in the form
# Line 288  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 306  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
306  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
307       dim_t i;       dim_t i;
308       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
      /*Paso_Solver_GS* GS=NULL;*/  
309       double *x0=MEMALLOC(amg->n,double);       double *x0=MEMALLOC(amg->n,double);
310       double time0=0;       /*double time0=0;*/
311            
312       if (amg->level==0  || amg->n_F<=500) {       if (amg->level==0  || amg->n_C<=500) {
313       /*if (amg->n_F<=500) {*/       /*if (amg->n_F<=500) {*/
314        time0=Paso_timer();        /*time0=Paso_timer();*/
315                    
316          Paso_Solver_solveJacobi(amg->GS,x,b);          /*Paso_Solver_solveJacobi(amg->GS,x,b);*/
317                    
318          /* #pragma omp parallel for private(i) schedule(static)          /* #pragma omp parallel for private(i) schedule(static)
319          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 324  void Paso_Solver_solveAMG(Paso_Solver_AM
324           x[i]+=x0[i];           x[i]+=x0[i];
325          }          }
326          */          */
327          /*Paso_UMFPACK1(amg->A,x,b,0);*/          Paso_UMFPACK1(amg->A,x,b,0);
328            
329           /*
330         time0=Paso_timer()-time0;         time0=Paso_timer()-time0;
331         /*fprintf(stderr,"timing: DIRECT SOLVER: %e/\n",time0);*/         fprintf(stderr,"timing: DIRECT SOLVER: %e/\n",time0);
332           */
333       } else {       } else {
       
334          /* presmoothing */          /* presmoothing */
335           Paso_Solver_solveJacobi(amg->GS,x,b);           Paso_Solver_solveJacobi(amg->GS,x,b);
336           /*          
337           #pragma omp parallel for private(i) schedule(static)           /* #pragma omp parallel for private(i) schedule(static)
338           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
339    
340            Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);            Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
341            Paso_Solver_solveGS(amg->GS,x0,r);            Paso_Solver_solveJacobi(amg->GS,x0,r);
342                        
343            #pragma omp parallel for private(i) schedule(static)            #pragma omp parallel for private(i) schedule(static)
344            for (i=0;i<amg->n;++i) {            for (i=0;i<amg->n;++i) {
345             x[i]+=x0[i];             x[i]+=x0[i];
346            }            }
347            */           */
348          /* end of presmoothing */          /* end of presmoothing */
349                    
350           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
# Line 371  void Paso_Solver_solveAMG(Paso_Solver_AM Line 389  void Paso_Solver_solveAMG(Paso_Solver_AM
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            
392       /*r=b-Ax*/       /*r=b-Ax */
393       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
394       Paso_Solver_solveJacobi(amg->GS,x0,r);       Paso_Solver_solveJacobi(amg->GS,x0,r);
395            
396       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
397       for (i=0;i<amg->n;++i) x[i]+=x0[i];       for (i=0;i<amg->n;++i) x[i]+=x0[i];
398            
399             /*
400       /*#pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
401       for (i=0;i<amg->n;++i) r[i]=b[i];       for (i=0;i<amg->n;++i) r[i]=b[i];
402        
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_solveGS(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) {       for (i=0;i<amg->n;++i) {

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

  ViewVC Help
Powered by ViewVC 1.1.26