/[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 1931 by artak, Mon Oct 27 01:31:28 2008 UTC revision 2100 by gross, Wed Nov 26 08:13:00 2008 UTC
# Line 35  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 80  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 81  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
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    double time0,time1,time2,S;    double time0=0,time1=0,time2=0,S;
85        
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);
# Line 100  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        
108    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {    if ( !(Paso_checkPtr(mis_marker) || Paso_checkPtr(out) || Paso_checkPtr(counter) ) ) {
# Line 107  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 110  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
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,0.25);*/
114       /*Paso_Pattern_coup(A_p,mis_marker,0.05);*/       /*Paso_Pattern_Aggregiation(A_p,mis_marker,0.5);*/
115         Paso_Pattern_coup(A_p,mis_marker,0.05);
116       time2=Paso_timer()-time0;       time2=Paso_timer()-time0;
117       if (Paso_noError()) {       if (Paso_noError()) {
118          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
# Line 140  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 144  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
144                for (i = 0; i < out->n_F; ++i) {                for (i = 0; i < out->n_F; ++i) {
145                  out->inv_A_FF[i]=0;                  out->inv_A_FF[i]=0;
146                  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) {
147                   out->inv_A_FF[i]+=A_p->val[iPtr];                   j=A_p->pattern->index[iPtr];
148                     if (mis_marker[j])
149                         out->inv_A_FF[i]+=A_p->val[iPtr];
150                  }                  }
151                }                }
152                  
153    /*          for (i=0;i<out->n_F;++i) {
154              fprintf(stderr,"GET INV_A_FF %g %d of %d (%d)\n",out->inv_A_FF[i],i,out->rows_in_F[i],(out->inv_A_FF[i]==0));
155              }
156              fprintf(stderr,"END\n");
157    */
158                #pragma omp for private(i, where_p,iPtr,index) schedule(static)                #pragma omp for private(i, where_p,iPtr,index) schedule(static)
159                for (i = 0; i < out->n_F; i++) {                for (i = 0; i < out->n_F; i++) {
160                  /* find main diagonal */                  /* find main diagonal */
# Line 163  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 174  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
174                        if (ABS(A_p->val[iPtr])>0.) {                        if (ABS(A_p->val[iPtr])>0.) {
175                              if(ABS(S)>0.)                              if(ABS(S)>0.)
176                                 out->inv_A_FF[i]=1./S;                                 out->inv_A_FF[i]=1./S;
177  /*                            else                              else
178                              {                              {
179                                 fprintf(stderr,"ROWSUM OF ROW %d is 0\n",out->rows_in_F[i]);                                 fprintf(stderr,"ROWSUM OF ROW %d->%d is 0\n",i,out->rows_in_F[i]);
180                                 S=0;                                 S=0;
181                                 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) {
182                                 if(A_p->val[iPtr]!=0) {                                 if(A_p->val[iPtr]!=0) {
# Line 175  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 186  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
186                                }                                }
187                                fprintf(stderr,"\n SUMMMMMMM %f\n",S);                                fprintf(stderr,"\n SUMMMMMMM %f\n",S);
188                              }                              }
189  */                      } else {                        } else {
190                              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.");
191                        }                        }
192                  }                  }
# Line 185  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 196  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
196             if( Paso_noError()) {             if( Paso_noError()) {
197                /* 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 */
198                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
199                 if (level>0) {                if (level<3) {
200                   /*if (out->n_C>10) {*/
201                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
202                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
203                     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 241  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 253  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
253                                  /* 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 */
254                                  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);
255                                  time1=Paso_timer()-time1;                                  time1=Paso_timer()-time1;
256                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1);                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level+1);
257                                                                    
258                                  /*Paso_SparseMatrix_free(schur);*/                                  /*Paso_SparseMatrix_free(schur);*/
259                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/
# Line 257  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 269  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
269                                    #pragma omp parallel                                    #pragma omp parallel
270                                    {                                    {
271                                      #pragma omp for private(i,k) schedule(static)                                      #pragma omp for private(i,k) schedule(static)
272                                      for (i = 0; i < out->n_F; ++i) {                                     for (i = 0; i < out->n_F; ++i) {
273                                            for (k=0; k<n_block;++k) {                                            for (k=0; k<n_block;++k) {
274                                               out->x_F[i*n_block+k]=0.;                                               out->x_F[i*n_block+k]=0.;
275                                               out->b_F[i*n_block+k]=0.;                                               out->b_F[i*n_block+k]=0.;
# Line 286  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 298  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
298    if (Paso_noError()) {    if (Paso_noError()) {
299        if (verbose) {        if (verbose) {
300           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);
301           if (out->n_C>0) {           if (level<3) {
302              printf("timing: AMG: MIS/reordering/elemination : %e/%e/%e\n",time2,time0,time1);              printf("timing: AMG: MIS/reordering/elemination : %e/%e/%e\n",time2,time0,time1);
303           } else {           } else {
304              printf("timing: AMG: MIS: %e\n",time2);              printf("timing: AMG: MIS: %e\n",time2);
# Line 324  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 336  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
336  */  */
337    
338  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
339       dim_t i,k,oldsweeps;       dim_t i,oldsweeps;
      dim_t n_block=amg->n_block;  
340       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
341       Paso_Solver_GS* GS=NULL;       /*Paso_Solver_GS* GS=NULL;*/
342       double *bold=MEMALLOC(amg->n*amg->n_block,double);       double *bold=MEMALLOC(amg->n,double);
343       double *bnew=MEMALLOC(amg->n*amg->n_block,double);       double *bnew=MEMALLOC(amg->n,double);
344             double *x0=MEMALLOC(amg->n,double);
345       if (amg->level==0) {  
346          Paso_UMFPACK1(amg->A,x,b,1);       if (amg->level==3) {
347         /*if (amg->n_C<=10) {*/
348            Paso_UMFPACK1(amg->A,x,b,0);
349       } else {       } else {
350        
351          /* presmoothing */          /* presmoothing */
352           GS=Paso_Solver_getGS(amg->A,-1);           Paso_Solver_solveGS(amg->GS,x,b);
353           Paso_Solver_solveGS(GS,x,b);            oldsweeps=amg->GS->sweeps;
354           oldsweeps=GS->sweeps;           if (amg->GS->sweeps>1) {
          if (GS->sweeps>1) {  
355                        
356             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
357             for (i=0;i<GS->n*GS->n_block;++i) bold[i]=b[i];             for (i=0;i<amg->GS->n;++i) bold[i]=b[i];
358                        
359             while(GS->sweeps>1) {             while(amg->GS->sweeps>1) {
360                 #pragma omp parallel for private(i) schedule(static)                 #pragma omp parallel for private(i) schedule(static)
361                 for (i=0;i<GS->n*GS->n_block;++i) bnew[i]=bold[i]+b[i];                 for (i=0;i<amg->GS->n;++i) bnew[i]=bold[i]+b[i];
                 /* Compute the residual b=b-Ax*/  
362                 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);                 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);
363                 /* Go round again*/                 Paso_Solver_solveGS(amg->GS,x,bnew);
                Paso_Solver_solveGS(GS,x,bnew);  
364                 #pragma omp parallel for private(i) schedule(static)                 #pragma omp parallel for private(i) schedule(static)
365                 for (i=0;i<GS->n*GS->n_block;++i) bold[i]=bnew[i];                 for (i=0;i<amg->GS->n;++i) bold[i]=bnew[i];
366                 GS->sweeps=GS->sweeps-1;                 amg->GS->sweeps=amg->GS->sweeps-1;
367             }             }
368             }             }
369             GS->sweeps=oldsweeps;             amg->GS->sweeps=oldsweeps;
370              
371          /* end of presmoothing */          /* end of presmoothing */
372            
373           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
374           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
375                    
376           /*r=b-Ax*/           /*r=b-Ax*/
377           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
378          
379          /* b->[b_F,b_C]     */          /* b->[b_F,b_C]     */
380          if (n_block==1) {          #pragma omp parallel for private(i) schedule(static)
381             #pragma omp parallel for private(i) schedule(static)          for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];
382             for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];            
383            #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
384             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]];
         } else {  
            #pragma omp parallel for private(i,k) schedule(static)  
            for (i=0;i<amg->n_F;++i)  
                  for (k=0;k<n_block;k++) amg->b_F[amg->n_block*i+k]=r[n_block*amg->rows_in_F[i]+k];  
            #pragma omp parallel for private(i,k) schedule(static)  
            for (i=0;i<amg->n_C;++i)  
                  for (k=0;k<n_block;k++) amg->b_C[amg->n_block*i+k]=r[n_block*amg->rows_in_C[i]+k];  
         }  
385    
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(1,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->x_F,amg->b_F);
388                    
389          /* b_C=b_C-A_CF*x_F */          /* b_C=b_C-A_CF*x_F */
390          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);
# Line 392  void Paso_Solver_solveAMG(Paso_Solver_AM Line 395  void Paso_Solver_solveAMG(Paso_Solver_AM
395          /* b_F=b_F-A_FC*x_C */          /* b_F=b_F-A_FC*x_C */
396          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);
397          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
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(1,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) {          #pragma omp parallel for private(i) schedule(static)
402             #pragma omp parallel for private(i) schedule(static)          for (i=0;i<amg->n;++i) {
403             for (i=0;i<amg->n;++i) {              if (amg->mask_C[i]>-1) {
404                if (amg->mask_C[i]>-1) {                   x[i]+=amg->x_C[amg->mask_C[i]];
405                    x[i]+=amg->x_C[amg->mask_C[i]];              } else {
406                } else {                   x[i]+=amg->x_F[amg->mask_F[i]];
407                    x[i]+=amg->x_F[amg->mask_F[i]];              }
               }  
            }  
         } else {  
            #pragma omp parallel for private(i,k) schedule(static)  
            for (i=0;i<amg->n;++i) {  
                  if (amg->mask_C[i]>-1) {  
                      for (k=0;k<n_block;k++) x[n_block*i+k]+=amg->x_C[n_block*amg->mask_C[i]+k];  
                  } else {  
                      for (k=0;k<n_block;k++) x[n_block*i+k]+=amg->x_F[n_block*amg->mask_F[i]+k];  
                  }  
            }  
408          }          }
409            
410    
411       /*postsmoothing*/       /*postsmoothing*/
412       Paso_Solver_solveGS1(GS,x,b);       #pragma omp parallel for private(i) schedule(static)
413       if (GS->sweeps>1) {       for (i=0;i<amg->n;++i) r[i]=b[i];
414             #pragma omp parallel for private(i) schedule(static)  
415             for (i=0;i<GS->n*GS->n_block;++i) bold[i]=b[i];       /*r=b-Ax*/
416                   Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
417             while(GS->sweeps>1) {       Paso_Solver_solveGS(amg->GS,x0,r);
418                 #pragma omp parallel for private(i) schedule(static)      
419                 for (i=0;i<GS->n*GS->n_block;++i) bnew[i]=bold[i]+b[i];       #pragma omp parallel for private(i) schedule(static)
420                  /* Compute the residual b=b-Ax*/       for (i=0;i<amg->n;++i) {
421                 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);        x[i]+=x0[i];
422                 /* Go round again*/       }
423                 Paso_Solver_solveGS(GS,x,bnew);      
                #pragma omp parallel for private(i) schedule(static)  
                for (i=0;i<GS->n*GS->n_block;++i) bold[i]=bnew[i];  
                GS->sweeps=GS->sweeps-1;  
            }  
            }  
424       /*end of postsmoothing*/       /*end of postsmoothing*/
425            
426       Paso_Solver_GS_free(GS);       /*Paso_Solver_GS_free(amg->GS);*/
427       }       }
428       MEMFREE(r);       MEMFREE(r);
429       MEMFREE(bold);       MEMFREE(bold);
430       MEMFREE(bnew);       MEMFREE(bnew);
431         MEMFREE(x0);
432      return;      return;
433  }  }
434    

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

  ViewVC Help
Powered by ViewVC 1.1.26