/[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 1960 by artak, Tue Nov 4 05:50:41 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 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=4;
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 143  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 147  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
147                   out->inv_A_FF[i]+=A_p->val[iPtr];                   out->inv_A_FF[i]+=A_p->val[iPtr];
148                  }                  }
149                }                }
150                  
151    /*          for (i=0;i<out->n_F;++i) {
152              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));
153              }
154              fprintf(stderr,"END\n");
155    */
156                #pragma omp for private(i, where_p,iPtr,index) schedule(static)                #pragma omp for private(i, where_p,iPtr,index) schedule(static)
157                for (i = 0; i < out->n_F; i++) {                for (i = 0; i < out->n_F; i++) {
158                  /* find main diagonal */                  /* find main diagonal */
# Line 163  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 172  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
172                        if (ABS(A_p->val[iPtr])>0.) {                        if (ABS(A_p->val[iPtr])>0.) {
173                              if(ABS(S)>0.)                              if(ABS(S)>0.)
174                                 out->inv_A_FF[i]=1./S;                                 out->inv_A_FF[i]=1./S;
175  /*                            else                              else
176                              {                              {
177                                 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]);
178                                 S=0;                                 S=0;
179                                 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) {
180                                 if(A_p->val[iPtr]!=0) {                                 if(A_p->val[iPtr]!=0) {
# Line 175  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 184  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
184                                }                                }
185                                fprintf(stderr,"\n SUMMMMMMM %f\n",S);                                fprintf(stderr,"\n SUMMMMMMM %f\n",S);
186                              }                              }
187  */                      } else {                        } else {
188                              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.");
189                        }                        }
190                  }                  }
# Line 185  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 194  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
194             if( Paso_noError()) {             if( Paso_noError()) {
195                /* 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 */
196                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
197                 if (level>0) {                if (level<2) {
198                  /*if (out->n_C>10) {*/
199                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
200                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
201                     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 251  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
251                                  /* 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 */
252                                  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);
253                                  time1=Paso_timer()-time1;                                  time1=Paso_timer()-time1;
254                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level-1);                                  out->AMG_of_Schur=Paso_Solver_getAMG(schur_withFillIn,verbose,level+1);
255                                                                    
256                                  /*Paso_SparseMatrix_free(schur);*/                                  /*Paso_SparseMatrix_free(schur);*/
257                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/                                  /* Paso_SparseMatrix_free(schur_withFillIn);*/
# Line 286  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 296  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
296    if (Paso_noError()) {    if (Paso_noError()) {
297        if (verbose) {        if (verbose) {
298           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);
299           if (out->n_C>0) {           if (level<2) {
300              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);
301           } else {           } else {
302              printf("timing: AMG: MIS: %e\n",time2);              printf("timing: AMG: MIS: %e\n",time2);
# Line 324  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 334  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
334  */  */
335    
336  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
337       dim_t i,k,oldsweeps;       dim_t i,oldsweeps;
      dim_t n_block=amg->n_block;  
338       double *r=MEMALLOC(amg->n,double);       double *r=MEMALLOC(amg->n,double);
339       Paso_Solver_GS* GS=NULL;       /*Paso_Solver_GS* GS=NULL;*/
340       double *bold=MEMALLOC(amg->n*amg->n_block,double);       double *bold=MEMALLOC(amg->n,double);
341       double *bnew=MEMALLOC(amg->n*amg->n_block,double);       double *bnew=MEMALLOC(amg->n,double);
342             double *x0=MEMALLOC(amg->n,double);
343       if (amg->level==0) {  
344          Paso_UMFPACK1(amg->A,x,b,1);       if (amg->level==2) {
345         /*if (amg->n_C<=10) {*/
346            Paso_UMFPACK1(amg->A,x,b,0);
347       } else {       } else {
348        
349          /* presmoothing */          /* presmoothing */
350           GS=Paso_Solver_getGS(amg->A,-1);           Paso_Solver_solveGS(amg->GS,x,b);
351           Paso_Solver_solveGS(GS,x,b);            oldsweeps=amg->GS->sweeps;
352           oldsweeps=GS->sweeps;           if (amg->GS->sweeps>1) {
          if (GS->sweeps>1) {  
353                        
354             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
355             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];
356                        
357             while(GS->sweeps>1) {             while(amg->GS->sweeps>1) {
358                 #pragma omp parallel for private(i) schedule(static)                 #pragma omp parallel for private(i) schedule(static)
359                 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*/  
360                 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);
361                 /* Go round again*/                 Paso_Solver_solveGS(amg->GS,x,bnew);
                Paso_Solver_solveGS(GS,x,bnew);  
362                 #pragma omp parallel for private(i) schedule(static)                 #pragma omp parallel for private(i) schedule(static)
363                 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];
364                 GS->sweeps=GS->sweeps-1;                 amg->GS->sweeps=amg->GS->sweeps-1;
365             }             }
366             }             }
367             GS->sweeps=oldsweeps;             amg->GS->sweeps=oldsweeps;
368              
369          /* end of presmoothing */          /* end of presmoothing */
370            
371           #pragma omp parallel for private(i) schedule(static)           #pragma omp parallel for private(i) schedule(static)
372           for (i=0;i<amg->n;++i) r[i]=b[i];           for (i=0;i<amg->n;++i) r[i]=b[i];
373                    
374           /*r=b-Ax*/           /*r=b-Ax*/
375           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);           Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
376          
377          /* b->[b_F,b_C]     */          /* b->[b_F,b_C]     */
378          if (n_block==1) {          #pragma omp parallel for private(i) schedule(static)
379             #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]];
380             for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];            
381            #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
382             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];  
         }  
383    
384          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
385          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);
386                    
387          /* b_C=b_C-A_CF*x_F */          /* b_C=b_C-A_CF*x_F */
388          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 393  void Paso_Solver_solveAMG(Paso_Solver_AM
393          /* b_F=b_F-A_FC*x_C */          /* b_F=b_F-A_FC*x_C */
394          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);
395          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
396          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);
397          /* x<-[x_F,x_C]     */          /* x<-[x_F,x_C]     */
398                    
399          if (n_block==1) {          #pragma omp parallel for private(i) schedule(static)
400             #pragma omp parallel for private(i) schedule(static)          for (i=0;i<amg->n;++i) {
401             for (i=0;i<amg->n;++i) {              if (amg->mask_C[i]>-1) {
402                if (amg->mask_C[i]>-1) {                   x[i]+=amg->x_C[amg->mask_C[i]];
403                    x[i]+=amg->x_C[amg->mask_C[i]];              } else {
404                } else {                   x[i]+=amg->x_F[amg->mask_F[i]];
405                    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];  
                  }  
            }  
406          }          }
407            
408    
409       /*postsmoothing*/       /*postsmoothing*/
410       Paso_Solver_solveGS1(GS,x,b);       #pragma omp parallel for private(i) schedule(static)
411       if (GS->sweeps>1) {       for (i=0;i<amg->n;++i) r[i]=b[i];
412             #pragma omp parallel for private(i) schedule(static)      
413             for (i=0;i<GS->n*GS->n_block;++i) bold[i]=b[i];       /*for (i=0;i<amg->n;++i) fprintf(stderr,"%lf %lf %d %d %d BBB\n",r[i],x[i],amg->n,amg->A->numCols, amg->level);*/
414                  
415             while(GS->sweeps>1) {       /*
416                 #pragma omp parallel for private(i) schedule(static)       for (i = 0; i < amg->A->numRows; ++i) {
417                 for (i=0;i<GS->n*GS->n_block;++i) bnew[i]=bold[i]+b[i];            for (iPtr=amg->A->pattern->ptr[i];iPtr<amg->A->pattern->ptr[i + 1]; ++iPtr) {
418                  /* Compute the residual b=b-Ax*/                j=amg->A->pattern->index[iPtr];
419                 Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), amg->A, x, DBLE(1), bnew);                fprintf(stderr,"A[%d,%d]=%lf ",i,j,amg->A->val[iPtr]);
420                 /* Go round again*/            }
421                 Paso_Solver_solveGS(GS,x,bnew);            fprintf(stderr,"\n");
422                 #pragma omp parallel for private(i) schedule(static)       }*/
423                 for (i=0;i<GS->n*GS->n_block;++i) bold[i]=bnew[i];      
424                 GS->sweeps=GS->sweeps-1;       /*r=b-Ax*/
425             }       Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
426             }       Paso_Solver_solveGS(amg->GS,x0,r);
427        
428         #pragma omp parallel for private(i) schedule(static)
429         for (i=0;i<amg->n;++i) {
430          /*fprintf(stderr,"%lf %lf %lf\n",x[i],x0[i],x[i]+x0[i]);*/
431          x[i]+=x0[i];
432         }
433        
434       /*end of postsmoothing*/       /*end of postsmoothing*/
435            
436       Paso_Solver_GS_free(GS);       /*Paso_Solver_GS_free(amg->GS);*/
437       }       }
438       MEMFREE(r);       MEMFREE(r);
439       MEMFREE(bold);       MEMFREE(bold);
440       MEMFREE(bnew);       MEMFREE(bnew);
441         MEMFREE(x0);
442      return;      return;
443  }  }
444    

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

  ViewVC Help
Powered by ViewVC 1.1.26