/[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 2803 by artak, Thu Dec 3 05:35:20 2009 UTC revision 2832 by artak, Thu Jan 7 05:16:50 2010 UTC
# Line 51  void Paso_Solver_AMG_System_free(Paso_So Line 51  void Paso_Solver_AMG_System_free(Paso_So
51    
52  void Paso_Solver_AMG_free(Paso_Solver_AMG * in) {  void Paso_Solver_AMG_free(Paso_Solver_AMG * in) {
53       if (in!=NULL) {       if (in!=NULL) {
54          Paso_Solver_Jacobi_free(in->GS);          
55            if(in->Smoother->ID==PASO_JACOBI)
56                Paso_Solver_Jacobi_free(in->Smoother->Jacobi);
57            else if (in->Smoother->ID==PASO_GS)    
58                Paso_Solver_GS_free(in->Smoother->GS);
59            MEMFREE(in->Smoother);
60                
61          Paso_SparseMatrix_free(in->A_FC);          Paso_SparseMatrix_free(in->A_FC);
62          Paso_SparseMatrix_free(in->A_FF);          Paso_SparseMatrix_free(in->A_FF);
63          Paso_SparseMatrix_free(in->W_FC);          Paso_SparseMatrix_free(in->W_FC);
# Line 110  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 116  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
116    Paso_Pattern* temp2=NULL;    Paso_Pattern* temp2=NULL;
117    */    */
118    bool_t verbose=options->verbose;    bool_t verbose=options->verbose;
119      bool_t timing=0;
120      
121    dim_t n=A_p->numRows;    dim_t n=A_p->numRows;
122    dim_t n_block=A_p->row_block_size;    dim_t n_block=A_p->row_block_size;
123    index_t* mis_marker=NULL;      index_t* mis_marker=NULL;  
# Line 142  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 150  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
150       return NULL;       return NULL;
151    }    }
152    out=MEMALLOC(1,Paso_Solver_AMG);    out=MEMALLOC(1,Paso_Solver_AMG);
153      out->Smoother=MEMALLOC(1,Paso_Solver_Smoother);
154    /* identify independend set of rows/columns */    /* identify independend set of rows/columns */
155    mis_marker=TMPMEMALLOC(n,index_t);    mis_marker=TMPMEMALLOC(n,index_t);
156    counter=TMPMEMALLOC(n,index_t);    counter=TMPMEMALLOC(n,index_t);
# Line 161  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 170  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
170       out->b_F=NULL;       out->b_F=NULL;
171       out->x_C=NULL;       out->x_C=NULL;
172       out->b_C=NULL;       out->b_C=NULL;
      out->GS=NULL;  
173       out->A=Paso_SparseMatrix_getReference(A_p);       out->A=Paso_SparseMatrix_getReference(A_p);
      out->GS=NULL;  
174       out->solver=NULL;       out->solver=NULL;
175         out->Smoother->ID=options->smoother;
176         out->Smoother->Jacobi=NULL;
177         out->Smoother->GS=NULL;
178       /*out->GS=Paso_Solver_getGS(A_p,verbose);*/       /*out->GS=Paso_Solver_getGS(A_p,verbose);*/
179       out->level=level;       out->level=level;
180       out->n=n;       out->n=n;
# Line 196  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 206  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
206           #else           #else
207              #ifdef UMFPACK              #ifdef UMFPACK
208              #else              #else
209                  out->GS=Paso_Solver_getJacobi(A_p);                if (options->smoother == PASO_JACOBI)
210                    out->Smoother->Jacobi=Paso_Solver_getJacobi(A_p);
211                  else if (options->smoother == PASO_GS)
212                    out->Smoother->GS=Paso_Solver_getGS(A_p,verbose);
213              #endif              #endif
214           #endif           #endif
215                    
216       } else {       } else {
217           out->coarsest_level=FALSE;           out->coarsest_level=FALSE;
218           out->GS=Paso_Solver_getJacobi(A_p);          
219            if (options->smoother == PASO_JACOBI)
220                    out->Smoother->Jacobi=Paso_Solver_getJacobi(A_p);
221            else if (options->smoother == PASO_GS)
222                    out->Smoother->GS=Paso_Solver_getGS(A_p,verbose);
223    
224           /* identify independend set of rows/columns */           /* identify independend set of rows/columns */
225           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
# Line 220  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 237  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
237           else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {           else if (options->coarsening_method == PASO_AGGREGATION_COARSENING) {
238               Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);               Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);
239          }          }
240            else if (options->coarsening_method == PASO_STANDARD_COARSENING) {
241                 Paso_Pattern_Standard(A_p,mis_marker,options->coarsening_threshold);
242            }
243          else {          else {
244             /*Default coarseneing*/             /*Default coarseneing*/
245              Paso_Pattern_RS_MI_Aggressive(A_p,mis_marker,options->coarsening_threshold);              Paso_Pattern_Standard(A_p,mis_marker,options->coarsening_threshold);
246              /*Paso_Pattern_RS_MI(A_p,mis_marker,options->coarsening_threshold);*/              /*Paso_Pattern_Read("RS.spl",n,mis_marker);*/
247              /*Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);*/              /*Paso_Pattern_YS(A_p,mis_marker,options->coarsening_threshold);*/
248              /*Paso_Pattern_RS(A_p,mis_marker,options->coarsening_threshold);*/              /*Paso_Pattern_greedy_Agg(A_p,mis_marker,options->coarsening_threshold);*/
249                /*Paso_Pattern_greedy(A_p->pattern,mis_marker);*/
250              /*Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);*/              /*Paso_Pattern_Aggregiation(A_p,mis_marker,options->coarsening_threshold);*/
251                            
252          }          }
253                    
254          if (verbose) fprintf(stdout,"timing: Profilining for level %d:\n",level);          if (timing) fprintf(stdout,"timing: Profilining for level %d:\n",level);
255                    
256          time0=Paso_timer()-time0;          time0=Paso_timer()-time0;
257          if (verbose) fprintf(stdout,"timing: Coarsening: %e\n",time0);          if (timing) fprintf(stdout,"timing: Coarsening: %e\n",time0);
258    
259          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
260          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];          for (i = 0; i < n; ++i) counter[i]=mis_marker[i];
# Line 284  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 305  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
305                   }                   }
306                }                }
307                                
308                 /*if(level==1) {                /* if(level==1) {
309                     printf("##TOTAL: %d, ELIMINATED: %d\n",n,out->n_F);                     printf("##TOTAL: %d, ELIMINATED: %d\n",n,out->n_F);
310                     for (i = 0; i < n; ++i) {                     for (i = 0; i < n; ++i) {
311                      printf("##%d %d\n",i,mis_marker[i]);                      printf("##%d %d\n",i,!mis_marker[i]);
312                     }                     }
313                   }                  }
314                 */                */
315                  
316                /*check whether coarsening process actually makes sense to continue.                /*check whether coarsening process actually makes sense to continue.
317                if coarse matrix at least smaller by 30% then continue, otherwise we stop.*/                if coarse matrix at least smaller by 30% then continue, otherwise we stop.*/
318                if ((out->n_F*100/n)<30) {                if ((out->n_F*100/n)<30) {
# Line 341  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 363  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
363                        time0=Paso_timer();                        time0=Paso_timer();
364                        Paso_SparseMatrix_updateWeights(A_p,out->W_FC,mis_marker);                        Paso_SparseMatrix_updateWeights(A_p,out->W_FC,mis_marker);
365                        time0=Paso_timer()-time0;                        time0=Paso_timer()-time0;
366                        if (verbose) fprintf(stdout,"timing: updateWeights: %e\n",time0);                        if (timing) fprintf(stdout,"timing: updateWeights: %e\n",time0);
367                    
368                                                
369                        /*sprintf(filename,"W_FCafter_%d",level);                        /*sprintf(filename,"W_FCafter_%d",level);
# Line 352  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 374  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
374                        time0=Paso_timer();                        time0=Paso_timer();
375                        out->P=Paso_SparseMatrix_getProlongation(out->W_FC,mis_marker);                        out->P=Paso_SparseMatrix_getProlongation(out->W_FC,mis_marker);
376                        time0=Paso_timer()-time0;                        time0=Paso_timer()-time0;
377                        if (verbose) fprintf(stdout,"timing: getProlongation: %e\n",time0);                        if (timing) fprintf(stdout,"timing: getProlongation: %e\n",time0);
378                                                /*out->P=Paso_SparseMatrix_loadMM_toCSR("P1.mtx");*/
379                                                
380                                                
381                        /* sprintf(filename,"P_%d",level);                        /*sprintf(filename,"P_%d",level);
382                        Paso_SparseMatrix_saveMM(out->P,filename);                        Paso_SparseMatrix_saveMM(out->P,filename);
383                        */                        */
384                                                
385                        time0=Paso_timer();                        time0=Paso_timer();
386                        out->R=Paso_SparseMatrix_getRestriction(out->P);                        out->R=Paso_SparseMatrix_getRestriction(out->P);
387                        time0=Paso_timer()-time0;                        time0=Paso_timer()-time0;
388                        if (verbose) fprintf(stdout,"timing: getRestriction: %e\n",time0);                        if (timing) fprintf(stdout,"timing: getRestriction: %e\n",time0);
389                                                /*out->R=Paso_SparseMatrix_loadMM_toCSR("R1.mtx");*/
390                                                
391                        /*sprintf(filename,"R_%d",level);                        /*
392                          sprintf(filename,"R_%d",level);
393                        Paso_SparseMatrix_saveMM(out->R,filename);                        Paso_SparseMatrix_saveMM(out->R,filename);
394                        */                        */
395                                            
# Line 376  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 399  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
399                      time0=Paso_timer();                      time0=Paso_timer();
400                                            
401                      Atemp=Paso_SparseMatrix_MatrixMatrix(A_p,out->P);                      Atemp=Paso_SparseMatrix_MatrixMatrix(A_p,out->P);
402                        
403                      A_c=Paso_SparseMatrix_MatrixMatrix(out->R,Atemp);                      A_c=Paso_SparseMatrix_MatrixMatrix(out->R,Atemp);
404                                            
405                        /*A_c=Paso_SparseMatrix_loadMM_toCSR("A_C1.mtx");*/
406                        
407                      Paso_SparseMatrix_free(Atemp);                      Paso_SparseMatrix_free(Atemp);
408                                            
409                      /*A_c=Paso_Solver_getCoarseMatrix(A_p,out->R,out->P);*/                      /*A_c=Paso_Solver_getCoarseMatrix(A_p,out->R,out->P);*/
410                      time0=Paso_timer()-time0;                      time0=Paso_timer()-time0;
411                      if (verbose) fprintf(stdout,"timing: getCoarseMatrix: %e\n",time0);                      if (timing) fprintf(stdout,"timing: getCoarseMatrix: %e\n",time0);
412                                            
413                                                                                    
414                      /*Paso_Solver_getCoarseMatrix(A_c, A_p,out->R,out->P);*/                      /*Paso_Solver_getCoarseMatrix(A_c, A_p,out->R,out->P);*/
415                                            
416                                            /*
417                      /*sprintf(filename,"A_C_%d",level);                      sprintf(filename,"A_C_%d",level);
418                      Paso_SparseMatrix_saveMM(A_c,filename);                      Paso_SparseMatrix_saveMM(A_c,filename);
419                      */                      */
420                                            
# Line 468  void Paso_Solver_solveAMG(Paso_Solver_AM Line 494  void Paso_Solver_solveAMG(Paso_Solver_AM
494       dim_t i;       dim_t i;
495       double time0=0;       double time0=0;
496       double *r=NULL, *x0=NULL;       double *r=NULL, *x0=NULL;
497       bool_t verbose=0;       bool_t timing=0;
498            
499       dim_t post_sweeps=amg->post_sweeps;       dim_t post_sweeps=amg->post_sweeps;
500       dim_t pre_sweeps=amg->pre_sweeps;       dim_t pre_sweeps=amg->pre_sweeps;
# Line 476  void Paso_Solver_solveAMG(Paso_Solver_AM Line 502  void Paso_Solver_solveAMG(Paso_Solver_AM
502       #ifdef UMFPACK       #ifdef UMFPACK
503            Paso_UMFPACK_Handler * ptr=NULL;            Paso_UMFPACK_Handler * ptr=NULL;
504       #endif       #endif
       
505                        
506       r=MEMALLOC(amg->n,double);       r=MEMALLOC(amg->n,double);
507       x0=MEMALLOC(amg->n,double);       x0=MEMALLOC(amg->n,double);
# Line 485  void Paso_Solver_solveAMG(Paso_Solver_AM Line 510  void Paso_Solver_solveAMG(Paso_Solver_AM
510                
511        time0=Paso_timer();        time0=Paso_timer();
512        /*If all unknown are eliminated then Jacobi is the best preconditioner*/        /*If all unknown are eliminated then Jacobi is the best preconditioner*/
513        /*Paso_Solver_solveJacobi(amg->GS,x,b);*/  
         
514        if (amg->n_F==0 || amg->n_F==amg->n) {        if (amg->n_F==0 || amg->n_F==amg->n) {
515          Paso_Solver_solveJacobi(amg->GS,x,b);          if(amg->Smoother->ID==PASO_JACOBI)
516                Paso_Solver_solveJacobi(amg->Smoother->Jacobi,x,b);
517            else if (amg->Smoother->ID==PASO_GS)    
518                Paso_Solver_solveGS(amg->Smoother->GS,x,b);
519        }        }
520         else {         else {
521         #ifdef MKL         #ifdef MKL
# Line 496  void Paso_Solver_solveAMG(Paso_Solver_AM Line 523  void Paso_Solver_solveAMG(Paso_Solver_AM
523         #else         #else
524            #ifdef UMFPACK            #ifdef UMFPACK
525               ptr=(Paso_UMFPACK_Handler *)(amg->solver);               ptr=(Paso_UMFPACK_Handler *)(amg->solver);
526               Paso_UMFPACK1(&ptr,amg->A,x,b,verbose);               Paso_UMFPACK1(&ptr,amg->A,x,b,timing);
527               amg->solver=(void*) ptr;               amg->solver=(void*) ptr;
528            #else                  #else      
529               Paso_Solver_solveJacobi(amg->GS,x,b);             if(amg->Smoother->ID==PASO_JACOBI)
530                 Paso_Solver_solveJacobi(amg->Smoother->Jacobi,x,b);
531              else if (amg->Smoother->ID==PASO_GS)    
532                Paso_Solver_solveGS(amg->Smoother->GS,x,b);
533           #endif           #endif
534         #endif         #endif
535         }         }
536                
537         time0=Paso_timer()-time0;         time0=Paso_timer()-time0;
538         if (verbose) fprintf(stdout,"timing: DIRECT SOLVER: %e\n",time0);         if (timing) fprintf(stdout,"timing: DIRECT SOLVER: %e\n",time0);
539                
540       } else {       } else {
541          /* presmoothing */          /* presmoothing */
542           time0=Paso_timer();           time0=Paso_timer();
543           Paso_Solver_solveJacobi(amg->GS,x,b);           if(amg->Smoother->ID==PASO_JACOBI)
544                Paso_Solver_solveJacobi(amg->Smoother->Jacobi,x,b);
545            else if (amg->Smoother->ID==PASO_GS)    
546                Paso_Solver_solveGS(amg->Smoother->GS,x,b);
547                    
548          /***********/          /***********/
549          #pragma omp parallel for private(i) schedule(static)          if (pre_sweeps>1) {
550          for (i=0;i<amg->n;++i) r[i]=b[i];              #pragma omp parallel for private(i) schedule(static)
551                for (i=0;i<amg->n;++i) r[i]=b[i];
552            }
553        
554          while(pre_sweeps>1) {          while(pre_sweeps>1) {
555             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
556             for (i=0;i<amg->n;++i) r[i]+=b[i];             for (i=0;i<amg->n;++i) r[i]+=b[i];
557                        
558              /* Compute the residual b=b-Ax*/              /* Compute the residual r=r-Ax*/
559             Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);             Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
560             /* Go round again*/             /* Go round again*/
561             Paso_Solver_solveJacobi(amg->GS,x,r);            
562               if(amg->Smoother->ID==PASO_JACOBI)
563                Paso_Solver_solveJacobi(amg->Smoother->Jacobi,x,r);
564               else if (amg->Smoother->ID==PASO_GS)    
565                Paso_Solver_solveGS(amg->Smoother->GS,x,r);
566              
567             pre_sweeps-=1;             pre_sweeps-=1;
568          }          }
569           /***********/           /***********/
570                    
571           time0=Paso_timer()-time0;           time0=Paso_timer()-time0;
572           if (verbose) fprintf(stdout,"timing: Presmooting: %e\n",time0);           if (timing) fprintf(stdout,"timing: Presmooting: %e\n",time0);
573           /* end of presmoothing */           /* end of presmoothing */
574                    
575           time0=Paso_timer();           time0=Paso_timer();
# Line 539  void Paso_Solver_solveAMG(Paso_Solver_AM Line 579  void Paso_Solver_solveAMG(Paso_Solver_AM
579           /*r=b-Ax*/           /*r=b-Ax*/
580           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
581                    
582          /* b_c <- R*r  */          /* b_c = R*r  */
583           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,r,0.,amg->b_C);           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->R,r,0.,amg->b_C);
584                    
585          time0=Paso_timer()-time0;          time0=Paso_timer()-time0;
586          if (verbose) fprintf(stdout,"timing: Before next level: %e\n",time0);          if (timing) fprintf(stdout,"timing: Before next level: %e\n",time0);
587                    
588          /* x_C=AMG(b_C)     */          /* x_C=AMG(b_C)     */
589          Paso_Solver_solveAMG(amg->AMG_of_Coarse,amg->x_C,amg->b_C);          Paso_Solver_solveAMG(amg->AMG_of_Coarse,amg->x_C,amg->b_C);
590                    
591          time0=Paso_timer();          time0=Paso_timer();
592                    
593          /* x_0 <- P*x_c */          /* x_0 = P*x_c */
594          Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,0.,x0);          Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(1.,amg->P,amg->x_C,0.,x0);
595                    
596          /* x=x+x0 */          /* x=x+x0 */
# Line 565  void Paso_Solver_solveAMG(Paso_Solver_AM Line 605  void Paso_Solver_solveAMG(Paso_Solver_AM
605                
606        /*r=b-Ax */        /*r=b-Ax */
607        Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);        Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
608        Paso_Solver_solveJacobi(amg->GS,x0,r);        if(amg->Smoother->ID==PASO_JACOBI)
609                      Paso_Solver_solveJacobi(amg->Smoother->Jacobi,x0,r);
610          else if (amg->Smoother->ID==PASO_GS)    
611                Paso_Solver_solveGS(amg->Smoother->GS,x0,r);
612                
613        #pragma omp parallel for private(i) schedule(static)        #pragma omp parallel for private(i) schedule(static)
614        for (i=0;i<amg->n;++i)  {        for (i=0;i<amg->n;++i)  {
615         x[i]+=x0[i];         x[i]+=x0[i];
616        }        }
617          
618          /***************/          /***************/
619          while(post_sweeps>1) {          while(post_sweeps>1) {
620                        
# Line 579  void Paso_Solver_solveAMG(Paso_Solver_AM Line 622  void Paso_Solver_solveAMG(Paso_Solver_AM
622             for (i=0;i<amg->n;++i) r[i]=b[i];             for (i=0;i<amg->n;++i) r[i]=b[i];
623                        
624             Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);             Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
625             Paso_Solver_solveJacobi(amg->GS,x0,r);            
626               if(amg->Smoother->ID==PASO_JACOBI)
627                  Paso_Solver_solveJacobi(amg->Smoother->Jacobi,x0,r);
628               else if (amg->Smoother->ID==PASO_GS)    
629                  Paso_Solver_solveGS(amg->Smoother->GS,x0,r);
630                  
631             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
632              for (i=0;i<amg->n;++i)  {              for (i=0;i<amg->n;++i)  {
633               x[i]+=x0[i];               x[i]+=x0[i];
# Line 589  void Paso_Solver_solveAMG(Paso_Solver_AM Line 637  void Paso_Solver_solveAMG(Paso_Solver_AM
637          /**************/          /**************/
638                
639        time0=Paso_timer()-time0;        time0=Paso_timer()-time0;
640        if (verbose) fprintf(stdout,"timing: Postsmoothing: %e\n",time0);        if (timing) fprintf(stdout,"timing: Postsmoothing: %e\n",time0);
641    
642        /*end of postsmoothing*/        /*end of postsmoothing*/
643            

Legend:
Removed from v.2803  
changed lines
  Added in v.2832

  ViewVC Help
Powered by ViewVC 1.1.26