/[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 1890 by artak, Fri Oct 17 00:14:22 2008 UTC revision 1940 by artak, Tue Oct 28 03:50:01 2008 UTC
# Line 25  Line 25 
25  #include "Paso.h"  #include "Paso.h"
26  #include "Solver.h"  #include "Solver.h"
27  #include "PasoUtil.h"  #include "PasoUtil.h"
28    #include "UMFPACK.h"
29    #include "Pattern_coupling.h"
30    
31  /**************************************************************/  /**************************************************************/
32    
# Line 33  Line 35 
35  void Paso_Solver_AMG_free(Paso_Solver_AMG * in) {  void Paso_Solver_AMG_free(Paso_Solver_AMG * in) {
36       if (in!=NULL) {       if (in!=NULL) {
37          Paso_Solver_AMG_free(in->AMG_of_Schur);          Paso_Solver_AMG_free(in->AMG_of_Schur);
38            Paso_Solver_GS_free(in->GS);
39          MEMFREE(in->inv_A_FF);          MEMFREE(in->inv_A_FF);
40          MEMFREE(in->A_FF_pivot);          MEMFREE(in->A_FF_pivot);
41          Paso_SparseMatrix_free(in->A_FC);          Paso_SparseMatrix_free(in->A_FC);
# Line 74  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 77  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
77    dim_t n_block=A_p->row_block_size;    dim_t n_block=A_p->row_block_size;
78    index_t* mis_marker=NULL;      index_t* mis_marker=NULL;  
79    index_t* counter=NULL;    index_t* counter=NULL;
   double *rs=NULL;    
80    index_t iPtr,*index, *where_p, iPtr_s;    index_t iPtr,*index, *where_p, iPtr_s;
81    dim_t i,k,j,j0;    dim_t i,k,j,j0;
82    Paso_SparseMatrix * schur=NULL;    Paso_SparseMatrix * schur=NULL;
83    Paso_SparseMatrix * schur_withFillIn=NULL;    Paso_SparseMatrix * schur_withFillIn=NULL;
84    schur_withFillIn=MEMALLOC(1,Paso_SparseMatrix);    double time0,time1,time2,S;
85        
     
   double A11,A12,A13,A21,A22,A23,A31,A32,A33,D,time0,time1,time2;  
     
   
86    /* identify independend set of rows/columns */    /* identify independend set of rows/columns */
87    mis_marker=TMPMEMALLOC(n,index_t);    mis_marker=TMPMEMALLOC(n,index_t);
88    counter=TMPMEMALLOC(n,index_t);    counter=TMPMEMALLOC(n,index_t);
   rs=TMPMEMALLOC(n,double);  
89    out=MEMALLOC(1,Paso_Solver_AMG);    out=MEMALLOC(1,Paso_Solver_AMG);
90    out->AMG_of_Schur=NULL;    out->AMG_of_Schur=NULL;
91    out->inv_A_FF=NULL;    out->inv_A_FF=NULL;
# Line 104  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 101  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
101    out->x_C=NULL;    out->x_C=NULL;
102    out->b_C=NULL;    out->b_C=NULL;
103    out->A=Paso_SparseMatrix_getReference(A_p);    out->A=Paso_SparseMatrix_getReference(A_p);
104      out->GS=Paso_Solver_getGS(A_p,verbose);
105      out->GS->sweeps=2;
106    out->level=level;    out->level=level;
107        
  /* fprintf(stderr,"START OF MATRIX \n\n");  
   for (i = 0; i < A_p->numRows; ++i) {  
      for (iPtr=A_p->pattern->ptr[i];iPtr<A_p->pattern->ptr[i + 1]; ++iPtr) {  
        j=A_p->pattern->index[iPtr];  
        fprintf(stderr,"A[%d,%d]=%.2f ",i,j,A_p->val[iPtr]);  
      }  
      fprintf(stderr,"\n");  
    }  
    fprintf(stderr,"END OF MATRIX \n\n");  
  */  
108    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {
109       /* identify independend set of rows/columns */       /* identify independend set of rows/columns */
110       time0=Paso_timer();       time0=Paso_timer();
111       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
112       for (i=0;i<n;++i) mis_marker[i]=-1;       for (i=0;i<n;++i) mis_marker[i]=-1;
113       Paso_Pattern_RS(A_p,mis_marker,0.25);       Paso_Pattern_RS(A_p,mis_marker,1/n);
114       /*       /*Paso_Pattern_coup(A_p,mis_marker,1/n);*/
      for (i=0;i<n;++i) fprintf(stderr," i=%d mis[i]=%d \n",i,mis_marker[i]);  
      */  
115       time2=Paso_timer()-time0;       time2=Paso_timer()-time0;
116       if (Paso_noError()) {       if (Paso_noError()) {
117          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
# Line 154  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 141  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
141                /* Compute row-sum for getting rs(A_FF)*/                /* Compute row-sum for getting rs(A_FF)*/
142                #pragma omp for private(i,iPtr) schedule(static)                #pragma omp for private(i,iPtr) schedule(static)
143                for (i = 0; i < out->n_F; ++i) {                for (i = 0; i < out->n_F; ++i) {
144                  rs[i]=0;                  out->inv_A_FF[i]=0;
145                  for (iPtr=A_p->pattern->ptr[out->rows_in_F[i]];iPtr<A_p->pattern->ptr[out->rows_in_F[i] + 1]; ++iPtr) {                  for (iPtr=A_p->pattern->ptr[out->rows_in_F[i]];iPtr<A_p->pattern->ptr[out->rows_in_F[i] + 1]; ++iPtr) {
146                   rs[i]+=A_p->val[iPtr];                   out->inv_A_FF[i]+=A_p->val[iPtr];
147                  }                  }
148                }                }
149                
150                #pragma omp for private(i, where_p,iPtr,A11,A12,A13,A21,A22,A23,A31,A32,A33,D,index) schedule(static)                #pragma omp for private(i, where_p,iPtr,index) schedule(static)
151                for (i = 0; i < out->n_F; i++) {                for (i = 0; i < out->n_F; i++) {
152                  /* find main diagonal */                  /* find main diagonal */
153                  iPtr=A_p->pattern->ptr[out->rows_in_F[i]];                  iPtr=A_p->pattern->ptr[out->rows_in_F[i]];
# Line 175  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 162  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
162                  } else {                  } else {
163                      iPtr+=(index_t)(where_p-index);                      iPtr+=(index_t)(where_p-index);
164                      /* get inverse of A_FF block: */                      /* get inverse of A_FF block: */
165                        if (ABS(rs[i])>0.) {                        S=out->inv_A_FF[i];
166                              out->inv_A_FF[i]=1./rs[i];                        if (ABS(A_p->val[iPtr])>0.) {
167                        } else {                              if(ABS(S)>0.)
168                          out->inv_A_FF[i]=0;                                 out->inv_A_FF[i]=1./S;
169                        }  /*                            else
170                                                    {
171                       /* } else {                                 fprintf(stderr,"ROWSUM OF ROW %d is 0\n",out->rows_in_F[i]);
172                                   S=0;
173                                   for (iPtr=A_p->pattern->ptr[out->rows_in_F[i]];iPtr<A_p->pattern->ptr[out->rows_in_F[i] + 1]; ++iPtr) {
174                                   if(A_p->val[iPtr]!=0) {
175                                    fprintf(stderr,"A_p[%d,%d]=%f ",out->rows_in_F[i],A_p->pattern->index[iPtr],A_p->val[iPtr]);
176                                    S+=A_p->val[iPtr];
177                                   }
178                                  }
179                                  fprintf(stderr,"\n SUMMMMMMM %f\n",S);
180                                }
181    */                      } else {
182                              Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver_getAMG: Break-down in AMG decomposition: non-regular main diagonal block.");                              Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver_getAMG: Break-down in AMG decomposition: non-regular main diagonal block.");
183                        }*/                        }
184                  }                  }
185                }                }
186             } /* end parallel region */             } /* end parallel region */
# Line 191  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 188  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
188             if( Paso_noError()) {             if( Paso_noError()) {
189                /* 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 */
190                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
191                 /*if (out->n_C>11) {*/                /*if (level>0) {*/
192                 if (level>0) {                 if (out->n_C>10) {
193                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
194                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
195                     if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {                     if (! (Paso_checkPtr(out->mask_C) || Paso_checkPtr(out->rows_in_C) ) ) {
# Line 225  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 222  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
222                                                            
223                              /*find the pattern of the schur complement with fill in*/                              /*find the pattern of the schur complement with fill in*/
224                              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);
225                                
226                              /* copy values over*/                              /* copy values over*/
227                              #pragma omp for private(i,iPtr,iPtr_s,j,j0) schedule(static)                              #pragma omp for private(i,iPtr,iPtr_s,j,j0) schedule(static)
228                              for (i = 0; i < schur_withFillIn->numRows; ++i) {                              for (i = 0; i < schur_withFillIn->numRows; ++i) {
# Line 241  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 238  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
238                                  }                                  }
239                                }                                }
240                              }                              }
241                                                           time0=Paso_timer()-time0;
242                            /*  for (i = 0; i < schur_withFillIn->numRows; ++i) {                            
                               for (iPtr=schur_withFillIn->pattern->ptr[i];iPtr<schur_withFillIn->pattern->ptr[i + 1]; ++iPtr) {  
                                 j=schur_withFillIn->pattern->index[iPtr];  
                                 fprintf(stderr,"A_CC[%d,%d]=%.2f ",i,j,schur_withFillIn->val[iPtr]);  
                               }  
                               fprintf(stderr,"\n");  
                             }*/  
                             time0=Paso_timer()-time0;  
243                              if (Paso_noError()) {                              if (Paso_noError()) {
244                                  time1=Paso_timer();                                  time1=Paso_timer();
245                                  /* update A_CC block to get Schur complement and then apply AMG to it */                                  /* update A_CC block to get Schur complement and then apply AMG to it */
246                                  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);
247                                  time1=Paso_timer()-time1;                                  time1=Paso_timer()-time1;
248                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1);                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level+1);
249                                                                    
250                                  Paso_SparseMatrix_free(schur);                                  /*Paso_SparseMatrix_free(schur);*/
251                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/
252                              }                              }
253                              /* allocate work arrays for AMG application */                              /* allocate work arrays for AMG application */
# Line 297  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 287  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
287    }    }
288    TMPMEMFREE(mis_marker);    TMPMEMFREE(mis_marker);
289    TMPMEMFREE(counter);    TMPMEMFREE(counter);
   TMPMEMFREE(rs);  
290    if (Paso_noError()) {    if (Paso_noError()) {
291        if (verbose) {        if (verbose) {
292           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,n-out->n_F);
# Line 339  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 328  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
328  */  */
329    
330  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
331       dim_t i,k;       dim_t i,k,oldsweeps;
332       dim_t n_block=amg->n_block;       dim_t n_block=amg->n_block;
333       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
334       double *x0=MEMALLOC(amg->n,double);       /*Paso_Solver_GS* GS=NULL;*/
335             double *bold=MEMALLOC(amg->n*amg->n_block,double);
336       if (amg->level==0) {       double *bnew=MEMALLOC(amg->n*amg->n_block,double);
337          /* x=invA_FF*b  */  
338          Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,x,b);       /*if (amg->level==0) {*/
339         if (amg->n_C<=10) {
340            Paso_UMFPACK1(amg->A,x,b,0);
341       } else {       } else {
342           fprintf(stdout,"LEVEL %d \n",amg->level);          /* presmoothing */
343          /* presmoothing on (Shure, x, b, r) */           Paso_Solver_solveGS(amg->GS,x,b);
344          /****************/           oldsweeps=amg->GS->sweeps;
345           Paso_Solver_GS* GS=MEMALLOC(1,Paso_Solver_GS);           if (amg->GS->sweeps>1) {
346           GS=Paso_Solver_getGS(amg->A,-1);            
347           Paso_Solver_solveGS(GS,x,b);             #pragma omp parallel for private(i) schedule(static)
348               for (i=0;i<amg->GS->n*amg->GS->n_block;++i) bold[i]=b[i];
349              
350               while(amg->GS->sweeps>1) {
351                   #pragma omp parallel for private(i) schedule(static)
352                   for (i=0;i<amg->GS->n*amg->GS->n_block;++i) bnew[i]=bold[i]+b[i];
353                    /* Compute the residual b=b-Ax*/
354                   Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);
355                   /* Go round again*/
356                   Paso_Solver_solveGS(amg->GS,x,bnew);
357                   #pragma omp parallel for private(i) schedule(static)
358                   for (i=0;i<amg->GS->n*amg->GS->n_block;++i) bold[i]=bnew[i];
359                   amg->GS->sweeps=amg->GS->sweeps-1;
360               }
361               }
362               amg->GS->sweeps=oldsweeps;
363            /* end of presmoothing */
364                    
365           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
366           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
367                    
368           /*r=b-Ax*/           /*r=b-Ax*/
369           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
370         /****************/          
371          /* b->[b_F,b_C]     */          /* b->[b_F,b_C]     */
372          if (n_block==1) {          if (n_block==1) {
373             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
374             for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];    /* was b istead of r */             for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];  
375            #pragma omp parallel for private(i) schedule(static)            #pragma omp parallel for private(i) schedule(static)
376             for (i=0;i<amg->n_C;++i) amg->b_C[i]=r[amg->rows_in_C[i]];             for (i=0;i<amg->n_C;++i) amg->b_C[i]=r[amg->rows_in_C[i]];
377          } else {          } else {
# Line 376  void Paso_Solver_solveAMG(Paso_Solver_AM Line 383  void Paso_Solver_solveAMG(Paso_Solver_AM
383                   for (k=0;k<n_block;k++) amg->b_C[amg->n_block*i+k]=r[n_block*amg->rows_in_C[i]+k];                   for (k=0;k<n_block;k++) amg->b_C[amg->n_block*i+k]=r[n_block*amg->rows_in_C[i]+k];
384          }          }
385    
         /****************/  
         /* Coursening part */  
           
         /* r_F=A_FF^-1*r_F  */  
         /*Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->b_F,amg->b_F);  
         fprintf(stderr,"2\n");  
         */  
         /* r_C=r_C-A_CF*r_F */  
         /*Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_CF,amg->b_F,1.,amg->b_C);  
         fprintf(stderr,"3\n");  
         */  
        /****************/  
   
           
386          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
387          Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);          Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);
388                    
# Line 405  void Paso_Solver_solveAMG(Paso_Solver_AM Line 398  void Paso_Solver_solveAMG(Paso_Solver_AM
398          Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);          Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);
399          /* x<-[x_F,x_C]     */          /* x<-[x_F,x_C]     */
400                    
         /* */  
           
           
401          if (n_block==1) {          if (n_block==1) {
402             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
403             for (i=0;i<amg->n;++i) {             for (i=0;i<amg->n;++i) {
# Line 427  void Paso_Solver_solveAMG(Paso_Solver_AM Line 417  void Paso_Solver_solveAMG(Paso_Solver_AM
417                   }                   }
418             }             }
419          }          }
420          /* all done */  
421       /*post smoothing*/       /*postsmoothing*/
422  /*     Paso_Solver_solveGS(GS,x0,b);       Paso_Solver_solveGS1(amg->GS,x,b);
423       if (n_block==1) {       if (amg->GS->sweeps>1) {
424             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
425             for (i=0;i<amg->n;++i) x[i]+=x0[i];             for (i=0;i<amg->GS->n*amg->GS->n_block;++i) bold[i]=b[i];
426          } else {            
427             #pragma omp parallel for private(i,k) schedule(static)             while(amg->GS->sweeps>1) {
428             for (i=0;i<amg->n;++i) {                 #pragma omp parallel for private(i) schedule(static)
429                       for (k=0;k<n_block;k++) x[n_block*i+k]+=x0[n_block*i+k];                 for (i=0;i<amg->GS->n*amg->GS->n_block;++i) bnew[i]=bold[i]+b[i];
430                    /* Compute the residual b=b-Ax*/
431                   Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);
432                   /* Go round again*/
433                   Paso_Solver_solveGS1(amg->GS,x,bnew);
434                   #pragma omp parallel for private(i) schedule(static)
435                   for (i=0;i<amg->GS->n*amg->GS->n_block;++i) bold[i]=bnew[i];
436                   amg->GS->sweeps=amg->GS->sweeps-1;
437             }             }
438          }             }
439  */           /*end of postsmoothing*/
440       Paso_Solver_GS_free(GS);      
441         /*Paso_Solver_GS_free(GS);*/
442       }       }
      MEMFREE(x0);  
443       MEMFREE(r);       MEMFREE(r);
444       return;       MEMFREE(bold);
445         MEMFREE(bnew);
446        return;
447  }  }
448    
449  /*  /*

Legend:
Removed from v.1890  
changed lines
  Added in v.1940

  ViewVC Help
Powered by ViewVC 1.1.26