/[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 1887 by ksteube, Wed Oct 15 03:26:25 2008 UTC revision 1892 by artak, Fri Oct 17 00:55:59 2008 UTC
# Line 68  to Line 68  to
68     then AMG is applied to S again until S becomes empty     then AMG is applied to S again until S becomes empty
69    
70  */  */
71  Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,bool_t verbose) {  Paso_Solver_AMG* Paso_Solver_getAMG(Paso_SparseMatrix *A_p,bool_t verbose,dim_t level) {
72    Paso_Solver_AMG* out=NULL;    Paso_Solver_AMG* out=NULL;
73    dim_t n=A_p->numRows;    dim_t n=A_p->numRows;
74    dim_t n_block=A_p->row_block_size;    dim_t n_block=A_p->row_block_size;
# Line 80  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 80  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
80    Paso_SparseMatrix * schur=NULL;    Paso_SparseMatrix * schur=NULL;
81    Paso_SparseMatrix * schur_withFillIn=NULL;    Paso_SparseMatrix * schur_withFillIn=NULL;
82    double A11,A12,A13,A21,A22,A23,A31,A32,A33,D,time0,time1,time2;    double A11,A12,A13,A21,A22,A23,A31,A32,A33,D,time0,time1,time2;
83    schur_withFillIn=MEMALLOC(1,Paso_SparseMatrix);    
     
   
84    /* identify independend set of rows/columns */    /* identify independend set of rows/columns */
85    mis_marker=TMPMEMALLOC(n,index_t);    mis_marker=TMPMEMALLOC(n,index_t);
86    counter=TMPMEMALLOC(n,index_t);    counter=TMPMEMALLOC(n,index_t);
# Line 101  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 99  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
99    out->b_F=NULL;    out->b_F=NULL;
100    out->x_C=NULL;    out->x_C=NULL;
101    out->b_C=NULL;    out->b_C=NULL;
102      out->A=Paso_SparseMatrix_getReference(A_p);
103      out->level=level;
104        
105   /* fprintf(stderr,"START OF MATRIX \n\n");   /* fprintf(stderr,"START OF MATRIX \n\n");
106    for (i = 0; i < A_p->numRows; ++i) {    for (i = 0; i < A_p->numRows; ++i) {
# Line 117  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 117  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
117       time0=Paso_timer();       time0=Paso_timer();
118       #pragma omp parallel for private(i) schedule(static)       #pragma omp parallel for private(i) schedule(static)
119       for (i=0;i<n;++i) mis_marker[i]=-1;       for (i=0;i<n;++i) mis_marker[i]=-1;
120       Paso_Pattern_coup(A_p,mis_marker);       Paso_Pattern_RS(A_p,mis_marker,0.25);
121         /*
122         for (i=0;i<n;++i) fprintf(stderr," i=%d mis[i]=%d \n",i,mis_marker[i]);
123         */
124       time2=Paso_timer()-time0;       time2=Paso_timer()-time0;
125       if (Paso_noError()) {       if (Paso_noError()) {
126          #pragma omp parallel for private(i) schedule(static)          #pragma omp parallel for private(i) schedule(static)
# Line 171  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 174  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
174                        if (ABS(rs[i])>0.) {                        if (ABS(rs[i])>0.) {
175                              out->inv_A_FF[i]=1./rs[i];                              out->inv_A_FF[i]=1./rs[i];
176                        } else {                        } else {
177                              Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver_getAMG: Break-down in AMG decomposition: non-regular main diagonal block.");                          out->inv_A_FF[i]=0;
178                        }                        }
179                        
180                         /* } else {
181                                Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver_getAMG: Break-down in AMG decomposition: non-regular main diagonal block.");
182                          }*/
183                  }                  }
184                }                }
185             } /* end parallel region */             } /* end parallel region */
# Line 180  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 187  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
187             if( Paso_noError()) {             if( Paso_noError()) {
188                /* 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 */
189                out->n_C=n-out->n_F;                out->n_C=n-out->n_F;
190                if (out->n_C>0) {                 /*if (out->n_C>11) {*/
191                   if (level>0) {
192                     out->rows_in_C=MEMALLOC(out->n_C,index_t);                     out->rows_in_C=MEMALLOC(out->n_C,index_t);
193                     out->mask_C=MEMALLOC(n,index_t);                     out->mask_C=MEMALLOC(n,index_t);
194                     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 243  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);                                  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);*/
258                              }                              }
259                              /* allocate work arrays for AMG application */                              /* allocate work arrays for AMG application */
260                              if (Paso_noError()) {                              if (Paso_noError()) {
# Line 254  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 262  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
262                                out->b_F=MEMALLOC(n_block*out->n_F,double);                                out->b_F=MEMALLOC(n_block*out->n_F,double);
263                                out->x_C=MEMALLOC(n_block*out->n_C,double);                                out->x_C=MEMALLOC(n_block*out->n_C,double);
264                                out->b_C=MEMALLOC(n_block*out->n_C,double);                                out->b_C=MEMALLOC(n_block*out->n_C,double);
265    
266                                if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {                                if (! (Paso_checkPtr(out->x_F) || Paso_checkPtr(out->b_F) || Paso_checkPtr(out->x_C) || Paso_checkPtr(out->b_C) ) ) {
267                                    #pragma omp parallel                                    #pragma omp parallel
268                                    {                                    {
# Line 328  Paso_Solver_AMG* Paso_Solver_getAMG(Paso Line 337  Paso_Solver_AMG* Paso_Solver_getAMG(Paso
337  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {  void Paso_Solver_solveAMG(Paso_Solver_AMG * amg, double * x, double * b) {
338       dim_t i,k;       dim_t i,k;
339       dim_t n_block=amg->n_block;       dim_t n_block=amg->n_block;
340         double *r=MEMALLOC(amg->n,double);
341       if (amg->n_C==0) {       double *x0=MEMALLOC(amg->n,double);
342         Paso_Solver_GS* GS=NULL;
343        
344         if (amg->level==0) {
345          /* x=invA_FF*b  */          /* x=invA_FF*b  */
346          Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,x,b);          Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,x,b);
347       } else {       } else {
348             fprintf(stdout,"LEVEL %d \n",amg->level);
349          /* presmoothing on (Shure, x, b, r) */          /* presmoothing on (Shure, x, b, r) */
350            /****************/
351             GS=Paso_Solver_getGS(amg->A,-1);
352             Paso_Solver_solveGS(GS,x,b);
353            
354             #pragma omp parallel for private(i) schedule(static)
355             for (i=0;i<amg->n;++i) r[i]=b[i];
356            
357             /*r=b-Ax*/
358             Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A,x,1.,r);
359           /****************/        
360          /* b->[b_F,b_C]     */          /* b->[b_F,b_C]     */
361          if (n_block==1) {          if (n_block==1) {
362             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
363             for (i=0;i<amg->n_F;++i) amg->b_F[i]=b[amg->rows_in_F[i]];             for (i=0;i<amg->n_F;++i) amg->b_F[i]=r[amg->rows_in_F[i]];    /* was b istead of r */
364             #pragma omp parallel for private(i) schedule(static)            #pragma omp parallel for private(i) schedule(static)
365             for (i=0;i<amg->n_C;++i) amg->b_C[i]=b[amg->rows_in_C[i]];             for (i=0;i<amg->n_C;++i) amg->b_C[i]=r[amg->rows_in_C[i]];
366          } else {          } else {
367             #pragma omp parallel for private(i,k) schedule(static)             #pragma omp parallel for private(i,k) schedule(static)
368             for (i=0;i<amg->n_F;++i)             for (i=0;i<amg->n_F;++i)
369                   for (k=0;k<n_block;k++) amg->b_F[amg->n_block*i+k]=b[n_block*amg->rows_in_F[i]+k];                   for (k=0;k<n_block;k++) amg->b_F[amg->n_block*i+k]=r[n_block*amg->rows_in_F[i]+k];
370             #pragma omp parallel for private(i,k) schedule(static)             #pragma omp parallel for private(i,k) schedule(static)
371             for (i=0;i<amg->n_C;++i)             for (i=0;i<amg->n_C;++i)
372                   for (k=0;k<n_block;k++) amg->b_C[amg->n_block*i+k]=b[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];
373          }          }
374    
375            /****************/
376            /* Corsening part */
377            
378            /* r_F=A_FF^-1*r_F  */
379            /*Paso_Solver_applyBlockDiagonalMatrix(n_block,amg->n_F,amg->inv_A_FF,amg->A_FF_pivot,amg->b_F,amg->b_F);
380            fprintf(stderr,"2\n");
381            */
382            /* r_C=r_C-A_CF*r_F */
383            /*Paso_SparseMatrix_MatrixVector_CSR_OFFSET0(-1.,amg->A_CF,amg->b_F,1.,amg->b_C);
384            fprintf(stderr,"3\n");
385            */
386           /****************/
387    
388            
389          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
390          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);
391            
392          /* b_C=b_C-A_CF*x_F */          /* b_C=b_C-A_CF*x_F */
393          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);
394            
395          /* x_C=AMG(b_C)     */          /* x_C=AMG(b_C)     */
396          Paso_Solver_solveAMG(amg->AMG_of_Schur,amg->x_C,amg->b_C);          Paso_Solver_solveAMG(amg->AMG_of_Schur,amg->x_C,amg->b_C);
397            
398          /* b_F=b_F-A_FC*x_C */          /* b_F=b_F-A_FC*x_C */
399          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);
400          /* x_F=invA_FF*b_F  */          /* x_F=invA_FF*b_F  */
401          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);
402          /* x<-[x_F,x_C]     */          /* x<-[x_F,x_C]     */
403            
404            /* */
405            
406            
407          if (n_block==1) {          if (n_block==1) {
408             #pragma omp parallel for private(i) schedule(static)             #pragma omp parallel for private(i) schedule(static)
409             for (i=0;i<amg->n;++i) {             for (i=0;i<amg->n;++i) {
410                if (amg->mask_C[i]>-1) {                if (amg->mask_C[i]>-1) {
411                    x[i]=amg->x_C[amg->mask_C[i]];                    x[i]+=amg->x_C[amg->mask_C[i]];
412                } else {                } else {
413                    x[i]=amg->x_F[amg->mask_F[i]];                    x[i]+=amg->x_F[amg->mask_F[i]];
414                }                }
415             }             }
416          } else {          } else {
417             #pragma omp parallel for private(i,k) schedule(static)             #pragma omp parallel for private(i,k) schedule(static)
418             for (i=0;i<amg->n;++i) {             for (i=0;i<amg->n;++i) {
419                   if (amg->mask_C[i]>-1) {                   if (amg->mask_C[i]>-1) {
420                       for (k=0;k<n_block;k++) x[n_block*i+k]=amg->x_C[n_block*amg->mask_C[i]+k];                       for (k=0;k<n_block;k++) x[n_block*i+k]+=amg->x_C[n_block*amg->mask_C[i]+k];
421                   } else {                   } else {
422                       for (k=0;k<n_block;k++) x[n_block*i+k]=amg->x_F[n_block*amg->mask_F[i]+k];                       for (k=0;k<n_block;k++) x[n_block*i+k]+=amg->x_F[n_block*amg->mask_F[i]+k];
423                   }                   }
424             }             }
425          }          }
426          /* all done */          /* all done */
427         /*post smoothing*/
428    /*     Paso_Solver_solveGS(GS,x0,b);
429         if (n_block==1) {
430               #pragma omp parallel for private(i) schedule(static)
431               for (i=0;i<amg->n;++i) x[i]+=x0[i];
432            } else {
433               #pragma omp parallel for private(i,k) schedule(static)
434               for (i=0;i<amg->n;++i) {
435                         for (k=0;k<n_block;k++) x[n_block*i+k]+=x0[n_block*i+k];
436               }
437            }
438    */    
439         Paso_Solver_GS_free(GS);
440       }       }
441         MEMFREE(x0);
442         MEMFREE(r);
443       return;       return;
444  }  }
445    

Legend:
Removed from v.1887  
changed lines
  Added in v.1892

  ViewVC Help
Powered by ViewVC 1.1.26