/[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 1931 by artak, Mon Oct 27 01:31:28 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 74  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 76  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
76    dim_t n_block=A_p->row_block_size;    dim_t n_block=A_p->row_block_size;
77    index_t* mis_marker=NULL;      index_t* mis_marker=NULL;  
78    index_t* counter=NULL;    index_t* counter=NULL;
   double *rs=NULL;    
79    index_t iPtr,*index, *where_p, iPtr_s;    index_t iPtr,*index, *where_p, iPtr_s;
80    dim_t i,k,j,j0;    dim_t i,k,j,j0;
81    Paso_SparseMatrix * schur=NULL;    Paso_SparseMatrix * schur=NULL;
82    Paso_SparseMatrix * schur_withFillIn=NULL;    Paso_SparseMatrix * schur_withFillIn=NULL;
83    schur_withFillIn=MEMALLOC(1,Paso_SparseMatrix);    double time0,time1,time2,S;
84        
     
   double A11,A12,A13,A21,A22,A23,A31,A32,A33,D,time0,time1,time2;  
     
   
85    /* identify independend set of rows/columns */    /* identify independend set of rows/columns */
86    mis_marker=TMPMEMALLOC(n,index_t);    mis_marker=TMPMEMALLOC(n,index_t);
87    counter=TMPMEMALLOC(n,index_t);    counter=TMPMEMALLOC(n,index_t);
   rs=TMPMEMALLOC(n,double);  
88    out=MEMALLOC(1,Paso_Solver_AMG);    out=MEMALLOC(1,Paso_Solver_AMG);
89    out->AMG_of_Schur=NULL;    out->AMG_of_Schur=NULL;
90    out->inv_A_FF=NULL;    out->inv_A_FF=NULL;
# Line 106  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 102  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
102    out->A=Paso_SparseMatrix_getReference(A_p);    out->A=Paso_SparseMatrix_getReference(A_p);
103    out->level=level;    out->level=level;
104        
  /* 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");  
  */  
105    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {
106       /* identify independend set of rows/columns */       /* identify independend set of rows/columns */
107       time0=Paso_timer();       time0=Paso_timer();
108       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
109       for (i=0;i<n;++i) mis_marker[i]=-1;       for (i=0;i<n;++i) mis_marker[i]=-1;
110       Paso_Pattern_RS(A_p,mis_marker,0.25);       Paso_Pattern_RS(A_p,mis_marker,0.25);
111       /*       /*Paso_Pattern_coup(A_p,mis_marker,0.05);*/
      for (i=0;i<n;++i) fprintf(stderr," i=%d mis[i]=%d \n",i,mis_marker[i]);  
      */  
112       time2=Paso_timer()-time0;       time2=Paso_timer()-time0;
113       if (Paso_noError()) {       if (Paso_noError()) {
114          #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 138  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
138                /* Compute row-sum for getting rs(A_FF)*/                /* Compute row-sum for getting rs(A_FF)*/
139                #pragma omp for private(i,iPtr) schedule(static)                #pragma omp for private(i,iPtr) schedule(static)
140                for (i = 0; i < out->n_F; ++i) {                for (i = 0; i < out->n_F; ++i) {
141                  rs[i]=0;                  out->inv_A_FF[i]=0;
142                  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) {
143                   rs[i]+=A_p->val[iPtr];                   out->inv_A_FF[i]+=A_p->val[iPtr];
144                  }                  }
145                }                }
146                
147                #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)
148                for (i = 0; i < out->n_F; i++) {                for (i = 0; i < out->n_F; i++) {
149                  /* find main diagonal */                  /* find main diagonal */
150                  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 159  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
159                  } else {                  } else {
160                      iPtr+=(index_t)(where_p-index);                      iPtr+=(index_t)(where_p-index);
161                      /* get inverse of A_FF block: */                      /* get inverse of A_FF block: */
162                        if (ABS(rs[i])>0.) {                        S=out->inv_A_FF[i];
163                              out->inv_A_FF[i]=1./rs[i];                        if (ABS(A_p->val[iPtr])>0.) {
164                        } else {                              if(ABS(S)>0.)
165                          out->inv_A_FF[i]=0;                                 out->inv_A_FF[i]=1./S;
166                        }  /*                            else
167                                                    {
168                       /* } else {                                 fprintf(stderr,"ROWSUM OF ROW %d is 0\n",out->rows_in_F[i]);
169                                   S=0;
170                                   for (iPtr=A_p->pattern->ptr[out->rows_in_F[i]];iPtr<A_p->pattern->ptr[out->rows_in_F[i] + 1]; ++iPtr) {
171                                   if(A_p->val[iPtr]!=0) {
172                                    fprintf(stderr,"A_p[%d,%d]=%f ",out->rows_in_F[i],A_p->pattern->index[iPtr],A_p->val[iPtr]);
173                                    S+=A_p->val[iPtr];
174                                   }
175                                  }
176                                  fprintf(stderr,"\n SUMMMMMMM %f\n",S);
177                                }
178    */                      } else {
179                              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.");
180                        }*/                        }
181                  }                  }
182                }                }
183             } /* end parallel region */             } /* end parallel region */
# Line 191  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 185  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
185             if( Paso_noError()) {             if( Paso_noError()) {
186                /* 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 */
187                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
                /*if (out->n_C>11) {*/  
188                 if (level>0) {                 if (level>0) {
189                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
190                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
# Line 225  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 218  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
218                                                            
219                              /*find the pattern of the schur complement with fill in*/                              /*find the pattern of the schur complement with fill in*/
220                              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);
221                                
222                              /* copy values over*/                              /* copy values over*/
223                              #pragma omp for private(i,iPtr,iPtr_s,j,j0) schedule(static)                              #pragma omp for private(i,iPtr,iPtr_s,j,j0) schedule(static)
224                              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 234  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
234                                  }                                  }
235                                }                                }
236                              }                              }
237                                                           time0=Paso_timer()-time0;
238                            /*  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;  
239                              if (Paso_noError()) {                              if (Paso_noError()) {
240                                  time1=Paso_timer();                                  time1=Paso_timer();
241                                  /* 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 */
# Line 257  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 243  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
243                                  time1=Paso_timer()-time1;                                  time1=Paso_timer()-time1;
244                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1);                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1);
245                                                                    
246                                  Paso_SparseMatrix_free(schur);                                  /*Paso_SparseMatrix_free(schur);*/
247                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/
248                              }                              }
249                              /* allocate work arrays for AMG application */                              /* allocate work arrays for AMG application */
# Line 297  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 283  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
283    }    }
284    TMPMEMFREE(mis_marker);    TMPMEMFREE(mis_marker);
285    TMPMEMFREE(counter);    TMPMEMFREE(counter);
   TMPMEMFREE(rs);  
286    if (Paso_noError()) {    if (Paso_noError()) {
287        if (verbose) {        if (verbose) {
288           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 324  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
324  */  */
325    
326  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
327       dim_t i,k;       dim_t i,k,oldsweeps;
328       dim_t n_block=amg->n_block;       dim_t n_block=amg->n_block;
329       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
330       double *x0=MEMALLOC(amg->n,double);       Paso_Solver_GS* GS=NULL;
331         double *bold=MEMALLOC(amg->n*amg->n_block,double);
332         double *bnew=MEMALLOC(amg->n*amg->n_block,double);
333            
334       if (amg->level==0) {       if (amg->level==0) {
335          /* x=invA_FF*b  */          Paso_UMFPACK1(amg->A,x,b,1);
         Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,x,b);  
336       } else {       } else {
337           fprintf(stdout,"LEVEL %d \n",amg->level);  
338          /* presmoothing on (Shure, x, b, r) */          /* presmoothing */
         /****************/  
          Paso_Solver_GS* GS=MEMALLOC(1,Paso_Solver_GS);  
339           GS=Paso_Solver_getGS(amg->A,-1);           GS=Paso_Solver_getGS(amg->A,-1);
340           Paso_Solver_solveGS(GS,x,b);           Paso_Solver_solveGS(GS,x,b);
341                     oldsweeps=GS->sweeps;
342             if (GS->sweeps>1) {
343              
344               #pragma omp parallel for private(i) schedule(static)
345               for (i=0;i<GS->n*GS->n_block;++i) bold[i]=b[i];
346              
347               while(GS->sweeps>1) {
348                   #pragma omp parallel for private(i) schedule(static)
349                   for (i=0;i<GS->n*GS->n_block;++i) bnew[i]=bold[i]+b[i];
350                    /* Compute the residual b=b-Ax*/
351                   Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);
352                   /* Go round again*/
353                   Paso_Solver_solveGS(GS,x,bnew);
354                   #pragma omp parallel for private(i) schedule(static)
355                   for (i=0;i<GS->n*GS->n_block;++i) bold[i]=bnew[i];
356                   GS->sweeps=GS->sweeps-1;
357               }
358               }
359               GS->sweeps=oldsweeps;
360            /* end of presmoothing */
361    
362           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
363           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
364                    
365           /*r=b-Ax*/           /*r=b-Ax*/
366           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
367         /****************/          
368          /* b->[b_F,b_C]     */          /* b->[b_F,b_C]     */
369          if (n_block==1) {          if (n_block==1) {
370             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
371             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]];  
372            #pragma omp parallel for private(i) schedule(static)            #pragma omp parallel for private(i) schedule(static)
373             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]];
374          } else {          } else {
# Line 376  void Paso_Solver_solveAMG(Paso_Solver_AM Line 380  void Paso_Solver_solveAMG(Paso_Solver_AM
380                   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];
381          }          }
382    
         /****************/  
         /* 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");  
         */  
        /****************/  
   
           
383          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
384          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);
385                    
# Line 405  void Paso_Solver_solveAMG(Paso_Solver_AM Line 395  void Paso_Solver_solveAMG(Paso_Solver_AM
395          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);
396          /* x<-[x_F,x_C]     */          /* x<-[x_F,x_C]     */
397                    
         /* */  
           
           
398          if (n_block==1) {          if (n_block==1) {
399             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
400             for (i=0;i<amg->n;++i) {             for (i=0;i<amg->n;++i) {
# Line 427  void Paso_Solver_solveAMG(Paso_Solver_AM Line 414  void Paso_Solver_solveAMG(Paso_Solver_AM
414                   }                   }
415             }             }
416          }          }
417          /* all done */  
418       /*post smoothing*/       /*postsmoothing*/
419  /*     Paso_Solver_solveGS(GS,x0,b);       Paso_Solver_solveGS1(GS,x,b);
420       if (n_block==1) {       if (GS->sweeps>1) {
421             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
422             for (i=0;i<amg->n;++i) x[i]+=x0[i];             for (i=0;i<GS->n*GS->n_block;++i) bold[i]=b[i];
423          } else {            
424             #pragma omp parallel for private(i,k) schedule(static)             while(GS->sweeps>1) {
425             for (i=0;i<amg->n;++i) {                 #pragma omp parallel for private(i) schedule(static)
426                       for (k=0;k<n_block;k++) x[n_block*i+k]+=x0[n_block*i+k];                 for (i=0;i<GS->n*GS->n_block;++i) bnew[i]=bold[i]+b[i];
427                    /* Compute the residual b=b-Ax*/
428                   Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);
429                   /* Go round again*/
430                   Paso_Solver_solveGS(GS,x,bnew);
431                   #pragma omp parallel for private(i) schedule(static)
432                   for (i=0;i<GS->n*GS->n_block;++i) bold[i]=bnew[i];
433                   GS->sweeps=GS->sweeps-1;
434             }             }
435          }             }
436  */           /*end of postsmoothing*/
437        
438       Paso_Solver_GS_free(GS);       Paso_Solver_GS_free(GS);
439       }       }
      MEMFREE(x0);  
440       MEMFREE(r);       MEMFREE(r);
441       return;       MEMFREE(bold);
442         MEMFREE(bnew);
443        return;
444  }  }
445    
446  /*  /*

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

  ViewVC Help
Powered by ViewVC 1.1.26