xref: /petsc/src/ksp/pc/impls/gamg/classical.c (revision c634539dcd3d55ea13370dd1a0331d70a764cfa5)
1 #include <../src/ksp/pc/impls/gamg/gamg.h>        /*I "petscpc.h" I*/
2 #include <petsc-private/kspimpl.h>
3 #include <petscsf.h>
4 
5 PetscFunctionList PCGAMGClassicalProlongatorList    = NULL;
6 PetscBool         PCGAMGClassicalPackageInitialized = PETSC_FALSE;
7 
8 typedef struct {
9   PetscReal interp_threshold; /* interpolation threshold */
10   char      prolongtype[256];
11   PetscInt  nsmooths;         /* number of jacobi smoothings on the prolongator */
12 } PC_GAMG_Classical;
13 
14 #undef __FUNCT__
15 #define __FUNCT__ "PCGAMGClassicalSetType"
16 /*@C
17    PCGAMGClassicalSetType - Sets the type of classical interpolation to use
18 
19    Collective on PC
20 
21    Input Parameters:
22 .  pc - the preconditioner context
23 
24    Options Database Key:
25 .  -pc_gamg_classical_type
26 
27    Level: intermediate
28 
29 .seealso: ()
30 @*/
31 PetscErrorCode PCGAMGClassicalSetType(PC pc, PCGAMGClassicalType type)
32 {
33   PetscErrorCode ierr;
34 
35   PetscFunctionBegin;
36   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
37   ierr = PetscTryMethod(pc,"PCGAMGClassicalSetType_C",(PC,PCGAMGType),(pc,type));CHKERRQ(ierr);
38   PetscFunctionReturn(0);
39 }
40 
41 #undef __FUNCT__
42 #define __FUNCT__ "PCGAMGClassicalSetType_GAMG"
43 static PetscErrorCode PCGAMGClassicalSetType_GAMG(PC pc, PCGAMGClassicalType type)
44 {
45   PetscErrorCode    ierr;
46   PC_MG             *mg          = (PC_MG*)pc->data;
47   PC_GAMG           *pc_gamg     = (PC_GAMG*)mg->innerctx;
48   PC_GAMG_Classical *cls         = (PC_GAMG_Classical*)pc_gamg->subctx;
49 
50   PetscFunctionBegin;
51   ierr = PetscStrcpy(cls->prolongtype,type);CHKERRQ(ierr);
52   PetscFunctionReturn(0);
53 }
54 
55 #undef __FUNCT__
56 #define __FUNCT__ "PCGAMGGraph_Classical"
57 PetscErrorCode PCGAMGGraph_Classical(PC pc,const Mat A,Mat *G)
58 {
59   PetscInt          s,f,n,idx,lidx,gidx;
60   PetscInt          r,c,ncols;
61   const PetscInt    *rcol;
62   const PetscScalar *rval;
63   PetscInt          *gcol;
64   PetscScalar       *gval;
65   PetscReal         rmax;
66   PetscInt          cmax = 0;
67   PC_MG             *mg;
68   PC_GAMG           *gamg;
69   PetscErrorCode    ierr;
70   PetscInt          *gsparse,*lsparse;
71   PetscScalar       *Amax;
72   MatType           mtype;
73 
74   PetscFunctionBegin;
75   mg   = (PC_MG *)pc->data;
76   gamg = (PC_GAMG *)mg->innerctx;
77 
78   ierr = MatGetOwnershipRange(A,&s,&f);CHKERRQ(ierr);
79   n=f-s;
80   ierr = PetscMalloc1(n,&lsparse);CHKERRQ(ierr);
81   ierr = PetscMalloc1(n,&gsparse);CHKERRQ(ierr);
82   ierr = PetscMalloc1(n,&Amax);CHKERRQ(ierr);
83 
84   for (r = 0;r < n;r++) {
85     lsparse[r] = 0;
86     gsparse[r] = 0;
87   }
88 
89   for (r = s;r < f;r++) {
90     /* determine the maximum off-diagonal in each row */
91     rmax = 0.;
92     ierr = MatGetRow(A,r,&ncols,&rcol,&rval);CHKERRQ(ierr);
93     for (c = 0; c < ncols; c++) {
94       if (PetscRealPart(-rval[c]) > rmax && rcol[c] != r) {
95         rmax = PetscRealPart(-rval[c]);
96       }
97     }
98     Amax[r-s] = rmax;
99     if (ncols > cmax) cmax = ncols;
100     lidx = 0;
101     gidx = 0;
102     /* create the local and global sparsity patterns */
103     for (c = 0; c < ncols; c++) {
104       if (PetscRealPart(-rval[c]) > gamg->threshold*PetscRealPart(Amax[r-s]) || rcol[c] == r) {
105         if (rcol[c] < f && rcol[c] >= s) {
106           lidx++;
107         } else {
108           gidx++;
109         }
110       }
111     }
112     ierr = MatRestoreRow(A,r,&ncols,&rcol,&rval);CHKERRQ(ierr);
113     lsparse[r-s] = lidx;
114     gsparse[r-s] = gidx;
115   }
116   ierr = PetscMalloc1(cmax,&gval);CHKERRQ(ierr);
117   ierr = PetscMalloc1(cmax,&gcol);CHKERRQ(ierr);
118 
119   ierr = MatCreate(PetscObjectComm((PetscObject)A),G); CHKERRQ(ierr);
120   ierr = MatGetType(A,&mtype);CHKERRQ(ierr);
121   ierr = MatSetType(*G,mtype);CHKERRQ(ierr);
122   ierr = MatSetSizes(*G,n,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
123   ierr = MatMPIAIJSetPreallocation(*G,0,lsparse,0,gsparse);CHKERRQ(ierr);
124   ierr = MatSeqAIJSetPreallocation(*G,0,lsparse);CHKERRQ(ierr);
125   for (r = s;r < f;r++) {
126     ierr = MatGetRow(A,r,&ncols,&rcol,&rval);CHKERRQ(ierr);
127     idx = 0;
128     for (c = 0; c < ncols; c++) {
129       /* classical strength of connection */
130       if (PetscRealPart(-rval[c]) > gamg->threshold*PetscRealPart(Amax[r-s]) || rcol[c] == r) {
131         gcol[idx] = rcol[c];
132         gval[idx] = rval[c];
133         idx++;
134       }
135     }
136     ierr = MatSetValues(*G,1,&r,idx,gcol,gval,INSERT_VALUES);CHKERRQ(ierr);
137     ierr = MatRestoreRow(A,r,&ncols,&rcol,&rval);CHKERRQ(ierr);
138   }
139   ierr = MatAssemblyBegin(*G, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
140   ierr = MatAssemblyEnd(*G, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
141 
142   ierr = PetscFree(gval);CHKERRQ(ierr);
143   ierr = PetscFree(gcol);CHKERRQ(ierr);
144   ierr = PetscFree(lsparse);CHKERRQ(ierr);
145   ierr = PetscFree(gsparse);CHKERRQ(ierr);
146   ierr = PetscFree(Amax);CHKERRQ(ierr);
147   PetscFunctionReturn(0);
148 }
149 
150 
151 #undef __FUNCT__
152 #define __FUNCT__ "PCGAMGCoarsen_Classical"
153 PetscErrorCode PCGAMGCoarsen_Classical(PC pc,Mat *G,PetscCoarsenData **agg_lists)
154 {
155   PetscErrorCode   ierr;
156   MatCoarsen       crs;
157   MPI_Comm         fcomm = ((PetscObject)pc)->comm;
158 
159   PetscFunctionBegin;
160 
161 
162   /* construct the graph if necessary */
163   if (!G) {
164     SETERRQ(fcomm,PETSC_ERR_ARG_WRONGSTATE,"Must set Graph in PC in PCGAMG before coarsening");
165   }
166 
167   ierr = MatCoarsenCreate(fcomm,&crs);CHKERRQ(ierr);
168   ierr = MatCoarsenSetFromOptions(crs);CHKERRQ(ierr);
169   ierr = MatCoarsenSetAdjacency(crs,*G);CHKERRQ(ierr);
170   ierr = MatCoarsenSetStrictAggs(crs,PETSC_TRUE);CHKERRQ(ierr);
171   ierr = MatCoarsenApply(crs);CHKERRQ(ierr);
172   ierr = MatCoarsenGetData(crs,agg_lists);CHKERRQ(ierr);
173   ierr = MatCoarsenDestroy(&crs);CHKERRQ(ierr);
174 
175   PetscFunctionReturn(0);
176 }
177 
178 #undef __FUNCT__
179 #define __FUNCT__ "PCGAMGProlongator_Classical_Direct"
180 PetscErrorCode PCGAMGProlongator_Classical_Direct(PC pc, const Mat A, const Mat G, PetscCoarsenData *agg_lists,Mat *P)
181 {
182   PetscErrorCode    ierr;
183   PetscReal         *Amax_pos,*Amax_neg;
184   Mat               lA,gA=NULL;                /* on and off diagonal matrices */
185   PetscInt          fn;                        /* fine local blocked sizes */
186   PetscInt          cn;                        /* coarse local blocked sizes */
187   PetscInt          fs,fe;                     /* fine (row) ownership range*/
188   PetscInt          cs,ce;                     /* coarse (column) ownership range */
189   PetscInt          i,j;                       /* indices! */
190   PetscBool         iscoarse;                  /* flag for determining if a node is coarse */
191   PetscInt          *lcid,*gcid;               /* on and off-processor coarse unknown IDs */
192   PetscInt          *lsparse,*gsparse;         /* on and off-processor sparsity patterns for prolongator */
193   PetscScalar       pij;
194   const PetscScalar *rval;
195   const PetscInt    *rcol;
196   PetscScalar       g_pos,g_neg,a_pos,a_neg,diag,invdiag,alpha,beta;
197   Vec               C;   /* vec of fine size */
198   MatType           mtype;
199   PetscInt          ncols,col;
200   PetscInt          row_f,row_c;
201   PetscInt          cmax=0,idx;
202   PetscScalar       *pvals;
203   PetscInt          *pcols;
204   PC_MG             *mg          = (PC_MG*)pc->data;
205   PC_GAMG           *gamg        = (PC_GAMG*)mg->innerctx;
206   PetscLayout       clayout;
207   PetscSF           sf;
208   Vec               lvec;
209   PetscInt          *colmap,noff;
210   PetscBool         isMPIAIJ,isSEQAIJ;
211   Mat_MPIAIJ        *mpiaij;
212 
213   PetscFunctionBegin;
214   ierr = MatGetOwnershipRange(A,&fs,&fe);CHKERRQ(ierr);
215   fn = fe-fs;
216   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&isMPIAIJ);CHKERRQ(ierr);
217   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJ,&isSEQAIJ);CHKERRQ(ierr);
218   if (!isMPIAIJ && !isSEQAIJ) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Classical AMG requires MPIAIJ matrix");
219   if (isMPIAIJ) {
220     mpiaij = (Mat_MPIAIJ*)A->data;
221     lA = mpiaij->A;
222     gA = mpiaij->B;
223     lvec = mpiaij->lvec;
224     ierr = VecGetSize(lvec,&noff);CHKERRQ(ierr);
225     colmap = mpiaij->garray;
226     ierr = MatGetLayouts(A,NULL,&clayout);CHKERRQ(ierr);
227     ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
228     ierr = PetscSFSetGraphLayout(sf,clayout,noff,NULL,PETSC_COPY_VALUES,colmap);CHKERRQ(ierr);
229     ierr = PetscMalloc1(noff,&gcid);CHKERRQ(ierr);
230   } else {
231     lA = A;
232   }
233   ierr = PetscMalloc1(fn,&lsparse);CHKERRQ(ierr);
234   ierr = PetscMalloc1(fn,&gsparse);CHKERRQ(ierr);
235   ierr = PetscMalloc1(fn,&lcid);CHKERRQ(ierr);
236   ierr = PetscMalloc1(fn,&Amax_pos);CHKERRQ(ierr);
237   ierr = PetscMalloc1(fn,&Amax_neg);CHKERRQ(ierr);
238 
239   /* count the number of coarse unknowns */
240   cn = 0;
241   for (i=0;i<fn;i++) {
242     /* filter out singletons */
243     ierr = PetscCDEmptyAt(agg_lists,i,&iscoarse); CHKERRQ(ierr);
244     lcid[i] = -1;
245     if (!iscoarse) {
246       cn++;
247     }
248   }
249 
250    /* create the coarse vector */
251   ierr = VecCreateMPI(PetscObjectComm((PetscObject)A),cn,PETSC_DECIDE,&C);CHKERRQ(ierr);
252   ierr = VecGetOwnershipRange(C,&cs,&ce);CHKERRQ(ierr);
253 
254   cn = 0;
255   for (i=0;i<fn;i++) {
256     ierr = PetscCDEmptyAt(agg_lists,i,&iscoarse); CHKERRQ(ierr);
257     if (!iscoarse) {
258       lcid[i] = cs+cn;
259       cn++;
260     } else {
261       lcid[i] = -1;
262     }
263   }
264 
265   if (gA) {
266     ierr = PetscSFBcastBegin(sf,MPIU_INT,lcid,gcid);CHKERRQ(ierr);
267     ierr = PetscSFBcastEnd(sf,MPIU_INT,lcid,gcid);CHKERRQ(ierr);
268   }
269 
270   /* determine the biggest off-diagonal entries in each row */
271   for (i=fs;i<fe;i++) {
272     Amax_pos[i-fs] = 0.;
273     Amax_neg[i-fs] = 0.;
274     ierr = MatGetRow(A,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
275     for(j=0;j<ncols;j++){
276       if ((PetscRealPart(-rval[j]) > Amax_neg[i-fs]) && i != rcol[j]) Amax_neg[i-fs] = PetscAbsScalar(rval[j]);
277       if ((PetscRealPart(rval[j])  > Amax_pos[i-fs]) && i != rcol[j]) Amax_pos[i-fs] = PetscAbsScalar(rval[j]);
278     }
279     if (ncols > cmax) cmax = ncols;
280     ierr = MatRestoreRow(A,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
281   }
282   ierr = PetscMalloc1(cmax,&pcols);CHKERRQ(ierr);
283   ierr = PetscMalloc1(cmax,&pvals);CHKERRQ(ierr);
284   ierr = VecDestroy(&C);CHKERRQ(ierr);
285 
286   /* count the on and off processor sparsity patterns for the prolongator */
287   for (i=0;i<fn;i++) {
288     /* on */
289     lsparse[i] = 0;
290     gsparse[i] = 0;
291     if (lcid[i] >= 0) {
292       lsparse[i] = 1;
293       gsparse[i] = 0;
294     } else {
295       ierr = MatGetRow(lA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
296       for (j = 0;j < ncols;j++) {
297         col = rcol[j];
298         if (lcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold*Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold*Amax_neg[i])) {
299           lsparse[i] += 1;
300         }
301       }
302       ierr = MatRestoreRow(lA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
303       /* off */
304       if (gA) {
305         ierr = MatGetRow(gA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
306         for (j = 0; j < ncols; j++) {
307           col = rcol[j];
308           if (gcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold*Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold*Amax_neg[i])) {
309             gsparse[i] += 1;
310           }
311         }
312         ierr = MatRestoreRow(gA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
313       }
314     }
315   }
316 
317   /* preallocate and create the prolongator */
318   ierr = MatCreate(PetscObjectComm((PetscObject)A),P); CHKERRQ(ierr);
319   ierr = MatGetType(G,&mtype);CHKERRQ(ierr);
320   ierr = MatSetType(*P,mtype);CHKERRQ(ierr);
321 
322   ierr = MatSetSizes(*P,fn,cn,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
323   ierr = MatMPIAIJSetPreallocation(*P,0,lsparse,0,gsparse);CHKERRQ(ierr);
324   ierr = MatSeqAIJSetPreallocation(*P,0,lsparse);CHKERRQ(ierr);
325 
326   /* loop over local fine nodes -- get the diagonal, the sum of positive and negative strong and weak weights, and set up the row */
327   for (i = 0;i < fn;i++) {
328     /* determine on or off */
329     row_f = i + fs;
330     row_c = lcid[i];
331     if (row_c >= 0) {
332       pij = 1.;
333       ierr = MatSetValues(*P,1,&row_f,1,&row_c,&pij,INSERT_VALUES);CHKERRQ(ierr);
334     } else {
335       g_pos = 0.;
336       g_neg = 0.;
337       a_pos = 0.;
338       a_neg = 0.;
339       diag = 0.;
340 
341       /* local connections */
342       ierr = MatGetRow(lA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
343       for (j = 0; j < ncols; j++) {
344         col = rcol[j];
345         if (lcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold*Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold*Amax_neg[i])) {
346           if (PetscRealPart(rval[j]) > 0.) {
347             g_pos += rval[j];
348           } else {
349             g_neg += rval[j];
350           }
351         }
352         if (col != i) {
353           if (PetscRealPart(rval[j]) > 0.) {
354             a_pos += rval[j];
355           } else {
356             a_neg += rval[j];
357           }
358         } else {
359           diag = rval[j];
360         }
361       }
362       ierr = MatRestoreRow(lA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
363 
364       /* ghosted connections */
365       if (gA) {
366         ierr = MatGetRow(gA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
367         for (j = 0; j < ncols; j++) {
368           col = rcol[j];
369           if (gcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold*Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold*Amax_neg[i])) {
370             if (PetscRealPart(rval[j]) > 0.) {
371               g_pos += rval[j];
372             } else {
373               g_neg += rval[j];
374             }
375           }
376           if (PetscRealPart(rval[j]) > 0.) {
377             a_pos += rval[j];
378           } else {
379             a_neg += rval[j];
380           }
381         }
382         ierr = MatRestoreRow(gA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
383       }
384 
385       if (g_neg == 0.) {
386         alpha = 0.;
387       } else {
388         alpha = -a_neg/g_neg;
389       }
390 
391       if (g_pos == 0.) {
392         diag += a_pos;
393         beta = 0.;
394       } else {
395         beta = -a_pos/g_pos;
396       }
397       if (diag == 0.) {
398         invdiag = 0.;
399       } else invdiag = 1. / diag;
400       /* on */
401       ierr = MatGetRow(lA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
402       idx = 0;
403       for (j = 0;j < ncols;j++) {
404         col = rcol[j];
405         if (lcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold*Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold*Amax_neg[i])) {
406           row_f = i + fs;
407           row_c = lcid[col];
408           /* set the values for on-processor ones */
409           if (PetscRealPart(rval[j]) < 0.) {
410             pij = rval[j]*alpha*invdiag;
411           } else {
412             pij = rval[j]*beta*invdiag;
413           }
414           if (PetscAbsScalar(pij) != 0.) {
415             pvals[idx] = pij;
416             pcols[idx] = row_c;
417             idx++;
418           }
419         }
420       }
421       ierr = MatRestoreRow(lA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
422       /* off */
423       if (gA) {
424         ierr = MatGetRow(gA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
425         for (j = 0; j < ncols; j++) {
426           col = rcol[j];
427           if (gcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold*Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold*Amax_neg[i])) {
428             row_f = i + fs;
429             row_c = gcid[col];
430             /* set the values for on-processor ones */
431             if (PetscRealPart(rval[j]) < 0.) {
432               pij = rval[j]*alpha*invdiag;
433             } else {
434               pij = rval[j]*beta*invdiag;
435             }
436             if (PetscAbsScalar(pij) != 0.) {
437               pvals[idx] = pij;
438               pcols[idx] = row_c;
439               idx++;
440             }
441           }
442         }
443         ierr = MatRestoreRow(gA,i,&ncols,&rcol,&rval);CHKERRQ(ierr);
444       }
445       ierr = MatSetValues(*P,1,&row_f,idx,pcols,pvals,INSERT_VALUES);CHKERRQ(ierr);
446     }
447   }
448 
449   ierr = MatAssemblyBegin(*P, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
450   ierr = MatAssemblyEnd(*P, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
451 
452   ierr = PetscFree(lsparse);CHKERRQ(ierr);
453   ierr = PetscFree(gsparse);CHKERRQ(ierr);
454   ierr = PetscFree(pcols);CHKERRQ(ierr);
455   ierr = PetscFree(pvals);CHKERRQ(ierr);
456   ierr = PetscFree(Amax_pos);CHKERRQ(ierr);
457   ierr = PetscFree(Amax_neg);CHKERRQ(ierr);
458   ierr = PetscFree(lcid);CHKERRQ(ierr);
459   if (gA) {
460     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
461     ierr = PetscFree(gcid);CHKERRQ(ierr);
462   }
463 
464   PetscFunctionReturn(0);
465 }
466 
467 #undef __FUNCT__
468 #define __FUNCT__ "PCGAMGTruncateProlongator_Private"
469 PetscErrorCode PCGAMGTruncateProlongator_Private(PC pc,Mat *P)
470 {
471   PetscInt          j,i,ps,pf,pn,pcs,pcf,pcn,idx,cmax;
472   PetscErrorCode    ierr;
473   const PetscScalar *pval;
474   const PetscInt    *pcol;
475   PetscScalar       *pnval;
476   PetscInt          *pncol;
477   PetscInt          ncols;
478   Mat               Pnew;
479   PetscInt          *lsparse,*gsparse;
480   PetscReal         pmax_pos,pmax_neg,ptot_pos,ptot_neg,pthresh_pos,pthresh_neg;
481   PC_MG             *mg          = (PC_MG*)pc->data;
482   PC_GAMG           *pc_gamg     = (PC_GAMG*)mg->innerctx;
483   PC_GAMG_Classical *cls         = (PC_GAMG_Classical*)pc_gamg->subctx;
484 
485   PetscFunctionBegin;
486   /* trim and rescale with reallocation */
487   ierr = MatGetOwnershipRange(*P,&ps,&pf);CHKERRQ(ierr);
488   ierr = MatGetOwnershipRangeColumn(*P,&pcs,&pcf);CHKERRQ(ierr);
489   pn = pf-ps;
490   pcn = pcf-pcs;
491   ierr = PetscMalloc1(pn,&lsparse);CHKERRQ(ierr);
492   ierr = PetscMalloc1(pn,&gsparse);CHKERRQ(ierr);
493   /* allocate */
494   cmax = 0;
495   for (i=ps;i<pf;i++) {
496     lsparse[i-ps] = 0;
497     gsparse[i-ps] = 0;
498     ierr = MatGetRow(*P,i,&ncols,&pcol,&pval);CHKERRQ(ierr);
499     if (ncols > cmax) {
500       cmax = ncols;
501     }
502     pmax_pos = 0.;
503     pmax_neg = 0.;
504     for (j=0;j<ncols;j++) {
505       if (PetscRealPart(pval[j]) > pmax_pos) {
506         pmax_pos = PetscRealPart(pval[j]);
507       } else if (PetscRealPart(pval[j]) < pmax_neg) {
508         pmax_neg = PetscRealPart(pval[j]);
509       }
510     }
511     for (j=0;j<ncols;j++) {
512       if (PetscRealPart(pval[j]) >= pmax_pos*cls->interp_threshold || PetscRealPart(pval[j]) <= pmax_neg*cls->interp_threshold) {
513         if (pcol[j] >= pcs && pcol[j] < pcf) {
514           lsparse[i-ps]++;
515         } else {
516           gsparse[i-ps]++;
517         }
518       }
519     }
520     ierr = MatRestoreRow(*P,i,&ncols,&pcol,&pval);CHKERRQ(ierr);
521   }
522 
523   ierr = PetscMalloc1(cmax,&pnval);CHKERRQ(ierr);
524   ierr = PetscMalloc1(cmax,&pncol);CHKERRQ(ierr);
525 
526   ierr = MatCreate(PetscObjectComm((PetscObject)*P),&Pnew);CHKERRQ(ierr);
527   ierr = MatSetType(Pnew, MATAIJ);CHKERRQ(ierr);
528   ierr = MatSetSizes(Pnew,pn,pcn,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
529   ierr = MatSeqAIJSetPreallocation(Pnew,0,lsparse);CHKERRQ(ierr);
530   ierr = MatMPIAIJSetPreallocation(Pnew,0,lsparse,0,gsparse);CHKERRQ(ierr);
531 
532   for (i=ps;i<pf;i++) {
533     ierr = MatGetRow(*P,i,&ncols,&pcol,&pval);CHKERRQ(ierr);
534     pmax_pos = 0.;
535     pmax_neg = 0.;
536     for (j=0;j<ncols;j++) {
537       if (PetscRealPart(pval[j]) > pmax_pos) {
538         pmax_pos = PetscRealPart(pval[j]);
539       } else if (PetscRealPart(pval[j]) < pmax_neg) {
540         pmax_neg = PetscRealPart(pval[j]);
541       }
542     }
543     pthresh_pos = 0.;
544     pthresh_neg = 0.;
545     ptot_pos = 0.;
546     ptot_neg = 0.;
547     for (j=0;j<ncols;j++) {
548       if (PetscRealPart(pval[j]) >= cls->interp_threshold*pmax_pos) {
549         pthresh_pos += PetscRealPart(pval[j]);
550       } else if (PetscRealPart(pval[j]) <= cls->interp_threshold*pmax_neg) {
551         pthresh_neg += PetscRealPart(pval[j]);
552       }
553       if (PetscRealPart(pval[j]) > 0.) {
554         ptot_pos += PetscRealPart(pval[j]);
555       } else {
556         ptot_neg += PetscRealPart(pval[j]);
557       }
558     }
559     if (PetscAbsReal(pthresh_pos) > 0.) ptot_pos /= pthresh_pos;
560     if (PetscAbsReal(pthresh_neg) > 0.) ptot_neg /= pthresh_neg;
561     idx=0;
562     for (j=0;j<ncols;j++) {
563       if (PetscRealPart(pval[j]) >= pmax_pos*cls->interp_threshold) {
564         pnval[idx] = ptot_pos*pval[j];
565         pncol[idx] = pcol[j];
566         idx++;
567       } else if (PetscRealPart(pval[j]) <= pmax_neg*cls->interp_threshold) {
568         pnval[idx] = ptot_neg*pval[j];
569         pncol[idx] = pcol[j];
570         idx++;
571       }
572     }
573     ierr = MatRestoreRow(*P,i,&ncols,&pcol,&pval);CHKERRQ(ierr);
574     ierr = MatSetValues(Pnew,1,&i,idx,pncol,pnval,INSERT_VALUES);CHKERRQ(ierr);
575   }
576 
577   ierr = MatAssemblyBegin(Pnew, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
578   ierr = MatAssemblyEnd(Pnew, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
579   ierr = MatDestroy(P);CHKERRQ(ierr);
580 
581   *P = Pnew;
582   ierr = PetscFree(lsparse);CHKERRQ(ierr);
583   ierr = PetscFree(gsparse);CHKERRQ(ierr);
584   ierr = PetscFree(pncol);CHKERRQ(ierr);
585   ierr = PetscFree(pnval);CHKERRQ(ierr);
586   PetscFunctionReturn(0);
587 }
588 
589 #undef __FUNCT__
590 #define __FUNCT__ "PCGAMGProlongator_Classical_Standard"
591 PetscErrorCode PCGAMGProlongator_Classical_Standard(PC pc, const Mat A, const Mat G, PetscCoarsenData *agg_lists,Mat *P)
592 {
593   PetscErrorCode    ierr;
594   Mat               lA,*lAs;
595   Vec               cv;
596   PetscInt          *gcid,*lcid;
597   IS                lis;
598   PetscInt          fs,fe,cs,ce,nl,i,j,k,li,lni,ci;
599   PetscInt          fn,cn,cid;
600   PetscBool         iscoarse;
601   const PetscScalar *vcol;
602   const PetscInt    *icol;
603   const PetscInt    *gidx;
604   PetscInt          ncols;
605   PetscInt          *lsparse,*gsparse;
606   MatType           mtype;
607   PetscInt          maxcols;
608   PetscReal         diag,jdiag,jwttotal;
609   PetscScalar       *pvcol,vi;
610   PetscInt          *picol;
611   PetscInt          pncols;
612   PetscScalar       *pcontrib,pentry,pjentry;
613   PetscSF           sf;
614   PetscInt          size;
615   const PetscInt    *lidx;
616   PetscLayout       clayout;
617 
618   PetscFunctionBegin;
619   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr);
620   ierr = MatGetOwnershipRange(A,&fs,&fe);CHKERRQ(ierr);
621   fn = fe-fs;
622   ierr = ISCreateStride(PETSC_COMM_SELF,fe-fs,fs,1,&lis);CHKERRQ(ierr);
623   if (size > 1) {
624     ierr = MatGetLayouts(A,NULL,&clayout);CHKERRQ(ierr);
625     /* increase the overlap by two to get neighbors of neighbors */
626     ierr = MatIncreaseOverlap(A,1,&lis,2);CHKERRQ(ierr);
627     ierr = ISSort(lis);CHKERRQ(ierr);
628     /* get the local part of A */
629     ierr = MatGetSubMatrices(A,1,&lis,&lis,MAT_INITIAL_MATRIX,&lAs);CHKERRQ(ierr);
630     lA = lAs[0];
631     /* build an SF out of it */
632     ierr = ISGetLocalSize(lis,&nl);CHKERRQ(ierr);
633     ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
634     ierr = ISGetIndices(lis,&lidx);CHKERRQ(ierr);
635     ierr = PetscSFSetGraphLayout(sf,clayout,nl,NULL,PETSC_COPY_VALUES,lidx);CHKERRQ(ierr);
636     ierr = ISRestoreIndices(lis,&lidx);CHKERRQ(ierr);
637   } else {
638     lA = A;
639     nl = fn;
640   }
641   /* create a communication structure for the overlapped portion and transmit coarse indices */
642   ierr = PetscMalloc1(fn,&lsparse);CHKERRQ(ierr);
643   ierr = PetscMalloc1(fn,&gsparse);CHKERRQ(ierr);
644   ierr = PetscMalloc1(nl,&pcontrib);CHKERRQ(ierr);
645   /* create coarse vector */
646   cn = 0;
647   for (i=0;i<fn;i++) {
648     ierr = PetscCDEmptyAt(agg_lists,i,&iscoarse);CHKERRQ(ierr);
649     if (!iscoarse) {
650       cn++;
651     }
652   }
653   ierr = PetscMalloc1(fn,&gcid);CHKERRQ(ierr);
654   ierr = VecCreateMPI(PetscObjectComm((PetscObject)A),cn,PETSC_DECIDE,&cv);CHKERRQ(ierr);
655   ierr = VecGetOwnershipRange(cv,&cs,&ce);CHKERRQ(ierr);
656   cn = 0;
657   for (i=0;i<fn;i++) {
658     ierr = PetscCDEmptyAt(agg_lists,i,&iscoarse); CHKERRQ(ierr);
659     if (!iscoarse) {
660       gcid[i] = cs+cn;
661       cn++;
662     } else {
663       gcid[i] = -1;
664     }
665   }
666   if (size > 1) {
667     ierr = PetscMalloc1(nl,&lcid);CHKERRQ(ierr);
668     ierr = PetscSFBcastBegin(sf,MPIU_INT,gcid,lcid);CHKERRQ(ierr);
669     ierr = PetscSFBcastEnd(sf,MPIU_INT,gcid,lcid);CHKERRQ(ierr);
670   } else {
671     lcid = gcid;
672   }
673   /* count to preallocate the prolongator */
674   ierr = ISGetIndices(lis,&gidx);CHKERRQ(ierr);
675   maxcols = 0;
676   /* count the number of unique contributing coarse cells for each fine */
677   for (i=0;i<nl;i++) {
678     pcontrib[i] = 0.;
679     ierr = MatGetRow(lA,i,&ncols,&icol,NULL);CHKERRQ(ierr);
680     if (gidx[i] >= fs && gidx[i] < fe) {
681       li = gidx[i] - fs;
682       lsparse[li] = 0;
683       gsparse[li] = 0;
684       cid = lcid[i];
685       if (cid >= 0) {
686         lsparse[li] = 1;
687       } else {
688         for (j=0;j<ncols;j++) {
689           if (lcid[icol[j]] >= 0) {
690             pcontrib[icol[j]] = 1.;
691           } else {
692             ci = icol[j];
693             ierr = MatRestoreRow(lA,i,&ncols,&icol,NULL);CHKERRQ(ierr);
694             ierr = MatGetRow(lA,ci,&ncols,&icol,NULL);CHKERRQ(ierr);
695             for (k=0;k<ncols;k++) {
696               if (lcid[icol[k]] >= 0) {
697                 pcontrib[icol[k]] = 1.;
698               }
699             }
700             ierr = MatRestoreRow(lA,ci,&ncols,&icol,NULL);CHKERRQ(ierr);
701             ierr = MatGetRow(lA,i,&ncols,&icol,NULL);CHKERRQ(ierr);
702           }
703         }
704         for (j=0;j<ncols;j++) {
705           if (lcid[icol[j]] >= 0 && pcontrib[icol[j]] != 0.) {
706             lni = lcid[icol[j]];
707             if (lni >= cs && lni < ce) {
708               lsparse[li]++;
709             } else {
710               gsparse[li]++;
711             }
712             pcontrib[icol[j]] = 0.;
713           } else {
714             ci = icol[j];
715             ierr = MatRestoreRow(lA,i,&ncols,&icol,NULL);CHKERRQ(ierr);
716             ierr = MatGetRow(lA,ci,&ncols,&icol,NULL);CHKERRQ(ierr);
717             for (k=0;k<ncols;k++) {
718               if (lcid[icol[k]] >= 0 && pcontrib[icol[k]] != 0.) {
719                 lni = lcid[icol[k]];
720                 if (lni >= cs && lni < ce) {
721                   lsparse[li]++;
722                 } else {
723                   gsparse[li]++;
724                 }
725                 pcontrib[icol[k]] = 0.;
726               }
727             }
728             ierr = MatRestoreRow(lA,ci,&ncols,&icol,NULL);CHKERRQ(ierr);
729             ierr = MatGetRow(lA,i,&ncols,&icol,NULL);CHKERRQ(ierr);
730           }
731         }
732       }
733       if (lsparse[li] + gsparse[li] > maxcols) maxcols = lsparse[li]+gsparse[li];
734     }
735     ierr = MatRestoreRow(lA,i,&ncols,&icol,&vcol);CHKERRQ(ierr);
736   }
737   ierr = PetscMalloc1(maxcols,&picol);CHKERRQ(ierr);
738   ierr = PetscMalloc1(maxcols,&pvcol);CHKERRQ(ierr);
739   ierr = MatCreate(PetscObjectComm((PetscObject)A),P);CHKERRQ(ierr);
740   ierr = MatGetType(A,&mtype);CHKERRQ(ierr);
741   ierr = MatSetType(*P,mtype);CHKERRQ(ierr);
742   ierr = MatSetSizes(*P,fn,cn,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
743   ierr = MatMPIAIJSetPreallocation(*P,0,lsparse,0,gsparse);CHKERRQ(ierr);
744   ierr = MatSeqAIJSetPreallocation(*P,0,lsparse);CHKERRQ(ierr);
745   for (i=0;i<nl;i++) {
746     diag = 0.;
747     if (gidx[i] >= fs && gidx[i] < fe) {
748       li = gidx[i] - fs;
749       pncols=0;
750       cid = lcid[i];
751       if (cid >= 0) {
752         pncols = 1;
753         picol[0] = cid;
754         pvcol[0] = 1.;
755       } else {
756         ierr = MatGetRow(lA,i,&ncols,&icol,&vcol);CHKERRQ(ierr);
757         for (j=0;j<ncols;j++) {
758           pentry = vcol[j];
759           if (lcid[icol[j]] >= 0) {
760             /* coarse neighbor */
761             pcontrib[icol[j]] += pentry;
762           } else if (icol[j] != i) {
763             /* the neighbor is a strongly connected fine node */
764             ci = icol[j];
765             vi = vcol[j];
766             ierr = MatRestoreRow(lA,i,&ncols,&icol,&vcol);CHKERRQ(ierr);
767             ierr = MatGetRow(lA,ci,&ncols,&icol,&vcol);CHKERRQ(ierr);
768             jwttotal=0.;
769             jdiag = 0.;
770             for (k=0;k<ncols;k++) {
771               if (ci == icol[k]) {
772                 jdiag = PetscRealPart(vcol[k]);
773               }
774             }
775             for (k=0;k<ncols;k++) {
776               if (lcid[icol[k]] >= 0 && jdiag*PetscRealPart(vcol[k]) < 0.) {
777                 pjentry = vcol[k];
778                 jwttotal += PetscRealPart(pjentry);
779               }
780             }
781             if (jwttotal != 0.) {
782               jwttotal = PetscRealPart(vi)/jwttotal;
783               for (k=0;k<ncols;k++) {
784                 if (lcid[icol[k]] >= 0 && jdiag*PetscRealPart(vcol[k]) < 0.) {
785                   pjentry = vcol[k]*jwttotal;
786                   pcontrib[icol[k]] += pjentry;
787                 }
788               }
789             } else {
790               diag += PetscRealPart(vi);
791             }
792             ierr = MatRestoreRow(lA,ci,&ncols,&icol,&vcol);CHKERRQ(ierr);
793             ierr = MatGetRow(lA,i,&ncols,&icol,&vcol);CHKERRQ(ierr);
794           } else {
795             diag += PetscRealPart(vcol[j]);
796           }
797         }
798         if (diag != 0.) {
799           diag = 1./diag;
800           for (j=0;j<ncols;j++) {
801             if (lcid[icol[j]] >= 0 && pcontrib[icol[j]] != 0.) {
802               /* the neighbor is a coarse node */
803               if (PetscAbsScalar(pcontrib[icol[j]]) > 0.0) {
804                 lni = lcid[icol[j]];
805                 pvcol[pncols] = -pcontrib[icol[j]]*diag;
806                 picol[pncols] = lni;
807                 pncols++;
808               }
809               pcontrib[icol[j]] = 0.;
810             } else {
811               /* the neighbor is a strongly connected fine node */
812               ci = icol[j];
813               ierr = MatRestoreRow(lA,i,&ncols,&icol,&vcol);CHKERRQ(ierr);
814               ierr = MatGetRow(lA,ci,&ncols,&icol,&vcol);CHKERRQ(ierr);
815               for (k=0;k<ncols;k++) {
816                 if (lcid[icol[k]] >= 0 && pcontrib[icol[k]] != 0.) {
817                   if (PetscAbsScalar(pcontrib[icol[k]]) > 0.0) {
818                     lni = lcid[icol[k]];
819                     pvcol[pncols] = -pcontrib[icol[k]]*diag;
820                     picol[pncols] = lni;
821                     pncols++;
822                   }
823                   pcontrib[icol[k]] = 0.;
824                 }
825               }
826               ierr = MatRestoreRow(lA,ci,&ncols,&icol,&vcol);CHKERRQ(ierr);
827               ierr = MatGetRow(lA,i,&ncols,&icol,&vcol);CHKERRQ(ierr);
828             }
829             pcontrib[icol[j]] = 0.;
830           }
831           ierr = MatRestoreRow(lA,i,&ncols,&icol,&vcol);CHKERRQ(ierr);
832         }
833       }
834       ci = gidx[i];
835       li = gidx[i] - fs;
836       if (pncols > 0) {
837         ierr = MatSetValues(*P,1,&ci,pncols,picol,pvcol,INSERT_VALUES);CHKERRQ(ierr);
838       }
839     }
840   }
841   ierr = ISRestoreIndices(lis,&gidx);CHKERRQ(ierr);
842   ierr = PetscFree(pcontrib);CHKERRQ(ierr);
843   ierr = PetscFree(picol);CHKERRQ(ierr);
844   ierr = PetscFree(pvcol);CHKERRQ(ierr);
845   ierr = PetscFree(lsparse);CHKERRQ(ierr);
846   ierr = PetscFree(gsparse);CHKERRQ(ierr);
847   ierr = ISDestroy(&lis);CHKERRQ(ierr);
848   ierr = PetscFree(gcid);CHKERRQ(ierr);
849   if (size > 1) {
850     ierr = PetscFree(lcid);CHKERRQ(ierr);
851     ierr = MatDestroyMatrices(1,&lAs);CHKERRQ(ierr);
852     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
853   }
854   ierr = VecDestroy(&cv);CHKERRQ(ierr);
855   ierr = MatAssemblyBegin(*P, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
856   ierr = MatAssemblyEnd(*P, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
857   PetscFunctionReturn(0);
858 }
859 
860 #undef __FUNCT__
861 #define __FUNCT__ "PCGAMGOptProl_Classical_Jacobi"
862 PetscErrorCode PCGAMGOptProl_Classical_Jacobi(PC pc,const Mat A,Mat *P)
863 {
864 
865   PetscErrorCode    ierr;
866   PetscInt          f,s,n,cf,cs,i,idx;
867   PetscInt          *coarserows;
868   PetscInt          ncols;
869   const PetscInt    *pcols;
870   const PetscScalar *pvals;
871   Mat               Pnew;
872   Vec               diag;
873   PC_MG             *mg          = (PC_MG*)pc->data;
874   PC_GAMG           *pc_gamg     = (PC_GAMG*)mg->innerctx;
875   PC_GAMG_Classical *cls         = (PC_GAMG_Classical*)pc_gamg->subctx;
876 
877   PetscFunctionBegin;
878   if (cls->nsmooths == 0) {
879     ierr = PCGAMGTruncateProlongator_Private(pc,P);CHKERRQ(ierr);
880     PetscFunctionReturn(0);
881   }
882   ierr = MatGetOwnershipRange(*P,&s,&f);CHKERRQ(ierr);
883   n = f-s;
884   ierr = MatGetOwnershipRangeColumn(*P,&cs,&cf);CHKERRQ(ierr);
885   ierr = PetscMalloc1(n,&coarserows);CHKERRQ(ierr);
886   /* identify the rows corresponding to coarse unknowns */
887   idx = 0;
888   for (i=s;i<f;i++) {
889     ierr = MatGetRow(*P,i,&ncols,&pcols,&pvals);CHKERRQ(ierr);
890     /* assume, for now, that it's a coarse unknown if it has a single unit entry */
891     if (ncols == 1) {
892       if (pvals[0] == 1.) {
893         coarserows[idx] = i;
894         idx++;
895       }
896     }
897     ierr = MatRestoreRow(*P,i,&ncols,&pcols,&pvals);CHKERRQ(ierr);
898   }
899   ierr = MatGetVecs(A,&diag,0);CHKERRQ(ierr);
900   ierr = MatGetDiagonal(A,diag);CHKERRQ(ierr);
901   ierr = VecReciprocal(diag);CHKERRQ(ierr);
902   for (i=0;i<cls->nsmooths;i++) {
903     ierr = MatMatMult(A,*P,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&Pnew);CHKERRQ(ierr);
904     ierr = MatZeroRows(Pnew,idx,coarserows,0.,NULL,NULL);CHKERRQ(ierr);
905     ierr = MatDiagonalScale(Pnew,diag,0);CHKERRQ(ierr);
906     ierr = MatAYPX(Pnew,-1.0,*P,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
907     ierr = MatDestroy(P);CHKERRQ(ierr);
908     *P  = Pnew;
909     Pnew = NULL;
910   }
911   ierr = VecDestroy(&diag);CHKERRQ(ierr);
912   ierr = PetscFree(coarserows);CHKERRQ(ierr);
913   ierr = PCGAMGTruncateProlongator_Private(pc,P);CHKERRQ(ierr);
914   PetscFunctionReturn(0);
915 }
916 
917 #undef __FUNCT__
918 #define __FUNCT__ "PCGAMGProlongator_Classical"
919 PetscErrorCode PCGAMGProlongator_Classical(PC pc, const Mat A, const Mat G, PetscCoarsenData *agg_lists,Mat *P)
920 {
921   PetscErrorCode    ierr;
922   PetscErrorCode    (*f)(PC,Mat,Mat,PetscCoarsenData*,Mat*);
923   PC_MG             *mg          = (PC_MG*)pc->data;
924   PC_GAMG           *pc_gamg     = (PC_GAMG*)mg->innerctx;
925   PC_GAMG_Classical *cls         = (PC_GAMG_Classical*)pc_gamg->subctx;
926 
927   PetscFunctionBegin;
928   ierr = PetscFunctionListFind(PCGAMGClassicalProlongatorList,cls->prolongtype,&f);CHKERRQ(ierr);
929   if (!f)SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Cannot find PCGAMG Classical prolongator type");
930   ierr = (*f)(pc,A,G,agg_lists,P);CHKERRQ(ierr);
931   PetscFunctionReturn(0);
932 }
933 
934 #undef __FUNCT__
935 #define __FUNCT__ "PCGAMGDestroy_Classical"
936 PetscErrorCode PCGAMGDestroy_Classical(PC pc)
937 {
938   PetscErrorCode ierr;
939   PC_MG          *mg          = (PC_MG*)pc->data;
940   PC_GAMG        *pc_gamg     = (PC_GAMG*)mg->innerctx;
941 
942   PetscFunctionBegin;
943   ierr = PetscFree(pc_gamg->subctx);CHKERRQ(ierr);
944   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCGAMGClassicalSetType_C",NULL);CHKERRQ(ierr);
945   PetscFunctionReturn(0);
946 }
947 
948 #undef __FUNCT__
949 #define __FUNCT__ "PCGAMGSetFromOptions_Classical"
950 PetscErrorCode PCGAMGSetFromOptions_Classical(PC pc)
951 {
952   PC_MG             *mg          = (PC_MG*)pc->data;
953   PC_GAMG           *pc_gamg     = (PC_GAMG*)mg->innerctx;
954   PC_GAMG_Classical *cls         = (PC_GAMG_Classical*)pc_gamg->subctx;
955   char              tname[256];
956   PetscErrorCode    ierr;
957   PetscBool         flg;
958 
959   PetscFunctionBegin;
960   ierr = PetscOptionsHead("GAMG-Classical options");CHKERRQ(ierr);
961   ierr = PetscOptionsFList("-pc_gamg_classical_type","Type of Classical AMG prolongation",
962                           "PCGAMGClassicalSetType",PCGAMGClassicalProlongatorList,cls->prolongtype, tname, sizeof(tname), &flg);CHKERRQ(ierr);
963   if (flg) {
964     ierr = PCGAMGClassicalSetType(pc,tname);CHKERRQ(ierr);
965   }
966   ierr = PetscOptionsReal("-pc_gamg_classical_interp_threshold","Threshold for classical interpolator entries","",cls->interp_threshold,&cls->interp_threshold,NULL);CHKERRQ(ierr);
967   ierr = PetscOptionsInt("-pc_gamg_classical_nsmooths","Threshold for classical interpolator entries","",cls->nsmooths,&cls->nsmooths,NULL);CHKERRQ(ierr);
968   ierr = PetscOptionsTail();CHKERRQ(ierr);
969   PetscFunctionReturn(0);
970 }
971 
972 #undef __FUNCT__
973 #define __FUNCT__ "PCGAMGSetData_Classical"
974 PetscErrorCode PCGAMGSetData_Classical(PC pc, Mat A)
975 {
976   PC_MG          *mg      = (PC_MG*)pc->data;
977   PC_GAMG        *pc_gamg = (PC_GAMG*)mg->innerctx;
978 
979   PetscFunctionBegin;
980   /* no data for classical AMG */
981   pc_gamg->data = NULL;
982   pc_gamg->data_cell_cols = 0;
983   pc_gamg->data_cell_rows = 0;
984   pc_gamg->data_sz        = 0;
985   PetscFunctionReturn(0);
986 }
987 
988 
989 #undef __FUNCT__
990 #define __FUNCT__ "PCGAMGClassicalFinalizePackage"
991 PetscErrorCode PCGAMGClassicalFinalizePackage(void)
992 {
993   PetscErrorCode ierr;
994 
995   PetscFunctionBegin;
996   PCGAMGClassicalPackageInitialized = PETSC_FALSE;
997   ierr = PetscFunctionListDestroy(&PCGAMGClassicalProlongatorList);CHKERRQ(ierr);
998   PetscFunctionReturn(0);
999 }
1000 
1001 #undef __FUNCT__
1002 #define __FUNCT__ "PCGAMGClassicalInitializePackage"
1003 PetscErrorCode PCGAMGClassicalInitializePackage(void)
1004 {
1005   PetscErrorCode ierr;
1006 
1007   PetscFunctionBegin;
1008   if (PCGAMGClassicalPackageInitialized) PetscFunctionReturn(0);
1009   ierr = PetscFunctionListAdd(&PCGAMGClassicalProlongatorList,PCGAMGCLASSICALDIRECT,PCGAMGProlongator_Classical_Direct);CHKERRQ(ierr);
1010   ierr = PetscFunctionListAdd(&PCGAMGClassicalProlongatorList,PCGAMGCLASSICALSTANDARD,PCGAMGProlongator_Classical_Standard);CHKERRQ(ierr);
1011   ierr = PetscRegisterFinalize(PCGAMGClassicalFinalizePackage);CHKERRQ(ierr);
1012   PetscFunctionReturn(0);
1013 }
1014 
1015 /* -------------------------------------------------------------------------- */
1016 /*
1017    PCCreateGAMG_Classical
1018 
1019 */
1020 #undef __FUNCT__
1021 #define __FUNCT__ "PCCreateGAMG_Classical"
1022 PetscErrorCode  PCCreateGAMG_Classical(PC pc)
1023 {
1024   PetscErrorCode ierr;
1025   PC_MG             *mg      = (PC_MG*)pc->data;
1026   PC_GAMG           *pc_gamg = (PC_GAMG*)mg->innerctx;
1027   PC_GAMG_Classical *pc_gamg_classical;
1028 
1029   PetscFunctionBegin;
1030   ierr = PCGAMGClassicalInitializePackage();
1031   if (pc_gamg->subctx) {
1032     /* call base class */
1033     ierr = PCDestroy_GAMG(pc);CHKERRQ(ierr);
1034   }
1035 
1036   /* create sub context for SA */
1037   ierr = PetscNewLog(pc,&pc_gamg_classical);CHKERRQ(ierr);
1038   pc_gamg->subctx = pc_gamg_classical;
1039   pc->ops->setfromoptions = PCGAMGSetFromOptions_Classical;
1040   /* reset does not do anything; setup not virtual */
1041 
1042   /* set internal function pointers */
1043   pc_gamg->ops->destroy        = PCGAMGDestroy_Classical;
1044   pc_gamg->ops->graph          = PCGAMGGraph_Classical;
1045   pc_gamg->ops->coarsen        = PCGAMGCoarsen_Classical;
1046   pc_gamg->ops->prolongator    = PCGAMGProlongator_Classical;
1047   pc_gamg->ops->optprol        = PCGAMGOptProl_Classical_Jacobi;
1048   pc_gamg->ops->setfromoptions = PCGAMGSetFromOptions_Classical;
1049 
1050   pc_gamg->ops->createdefaultdata = PCGAMGSetData_Classical;
1051   pc_gamg_classical->interp_threshold = 0.2;
1052   pc_gamg_classical->nsmooths         = 0;
1053   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCGAMGClassicalSetType_C",PCGAMGClassicalSetType_GAMG);CHKERRQ(ierr);
1054   ierr = PCGAMGClassicalSetType(pc,PCGAMGCLASSICALSTANDARD);CHKERRQ(ierr);
1055   PetscFunctionReturn(0);
1056 }
1057