Actual source code: gamg.c

petsc-master 2017-03-27
Report Typos and Errors
  1: /*
  2:  GAMG geometric-algebric multigrid PC - Mark Adams 2011
  3:  */
  4:  #include <petsc/private/matimpl.h>
  5:  #include <../src/ksp/pc/impls/gamg/gamg.h>
  6: #include <../src/ksp/pc/impls/bjacobi/bjacobi.h> /* Hack to access same_local_solves */

  8: #if defined PETSC_GAMG_USE_LOG
  9: PetscLogEvent petsc_gamg_setup_events[NUM_SET];
 10: #endif

 12: #if defined PETSC_USE_LOG
 13: PetscLogEvent PC_GAMGGraph_AGG;
 14: PetscLogEvent PC_GAMGGraph_GEO;
 15: PetscLogEvent PC_GAMGCoarsen_AGG;
 16: PetscLogEvent PC_GAMGCoarsen_GEO;
 17: PetscLogEvent PC_GAMGProlongator_AGG;
 18: PetscLogEvent PC_GAMGProlongator_GEO;
 19: PetscLogEvent PC_GAMGOptProlongator_AGG;
 20: #endif

 22: /* #define GAMG_STAGES */
 23: #if (defined PETSC_GAMG_USE_LOG && defined GAMG_STAGES)
 24: static PetscLogStage gamg_stages[PETSC_GAMG_MAXLEVELS];
 25: #endif

 27: static PetscFunctionList GAMGList = 0;
 28: static PetscBool PCGAMGPackageInitialized;

 30: /* ----------------------------------------------------------------------------- */
 31: PetscErrorCode PCReset_GAMG(PC pc)
 32: {
 34:   PC_MG          *mg      = (PC_MG*)pc->data;
 35:   PC_GAMG        *pc_gamg = (PC_GAMG*)mg->innerctx;

 38:   if (pc_gamg->data) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"This should not happen, cleaned up in SetUp\n");
 39:   pc_gamg->data_sz = 0;
 40:   PetscFree(pc_gamg->orig_data);
 41:   return(0);
 42: }

 44: /* -------------------------------------------------------------------------- */
 45: /*
 46:    PCGAMGCreateLevel_GAMG: create coarse op with RAP.  repartition and/or reduce number
 47:      of active processors.

 49:    Input Parameter:
 50:    . pc - parameters + side effect: coarse data in 'pc_gamg->data' and
 51:           'pc_gamg->data_sz' are changed via repartitioning/reduction.
 52:    . Amat_fine - matrix on this fine (k) level
 53:    . cr_bs - coarse block size
 54:    In/Output Parameter:
 55:    . a_P_inout - prolongation operator to the next level (k-->k-1)
 56:    . a_nactive_proc - number of active procs
 57:    Output Parameter:
 58:    . a_Amat_crs - coarse matrix that is created (k-1)
 59: */

 61: static PetscErrorCode PCGAMGCreateLevel_GAMG(PC pc,Mat Amat_fine,PetscInt cr_bs,Mat *a_P_inout,Mat *a_Amat_crs,PetscMPIInt *a_nactive_proc,IS * Pcolumnperm, PetscBool is_last)
 62: {
 63:   PetscErrorCode  ierr;
 64:   PC_MG           *mg         = (PC_MG*)pc->data;
 65:   PC_GAMG         *pc_gamg    = (PC_GAMG*)mg->innerctx;
 66:   Mat             Cmat,Pold=*a_P_inout;
 67:   MPI_Comm        comm;
 68:   PetscMPIInt     rank,size,new_size,nactive=*a_nactive_proc;
 69:   PetscInt        ncrs_eq,ncrs,f_bs;

 72:   PetscObjectGetComm((PetscObject)Amat_fine,&comm);
 73:   MPI_Comm_rank(comm, &rank);
 74:   MPI_Comm_size(comm, &size);
 75:   MatGetBlockSize(Amat_fine, &f_bs);
 76:   MatPtAP(Amat_fine, Pold, MAT_INITIAL_MATRIX, 2.0, &Cmat);

 78:   /* set 'ncrs' (nodes), 'ncrs_eq' (equations)*/
 79:   MatGetLocalSize(Cmat, &ncrs_eq, NULL);
 80:   if (pc_gamg->data_cell_rows>0) {
 81:     ncrs = pc_gamg->data_sz/pc_gamg->data_cell_cols/pc_gamg->data_cell_rows;
 82:   } else {
 83:     PetscInt  bs;
 84:     MatGetBlockSize(Cmat, &bs);
 85:     ncrs = ncrs_eq/bs;
 86:   }

 88:   /* get number of PEs to make active 'new_size', reduce, can be any integer 1-P */
 89:   if (is_last && !pc_gamg->use_parallel_coarse_grid_solver) new_size = 1;
 90:   else {
 91:     PetscInt ncrs_eq_glob;
 92:     MatGetSize(Cmat, &ncrs_eq_glob, NULL);
 93:     new_size = (PetscMPIInt)((float)ncrs_eq_glob/(float)pc_gamg->min_eq_proc + 0.5); /* hardwire min. number of eq/proc */
 94:     if (!new_size) new_size = 1; /* not likely, posible? */
 95:     else if (new_size >= nactive) new_size = nactive; /* no change, rare */
 96:   }

 98:   if (Pcolumnperm) *Pcolumnperm = NULL;

100:   if (!pc_gamg->repart && new_size==nactive) *a_Amat_crs = Cmat; /* output - no repartitioning or reduction - could bail here */
101:   else {
102:     PetscInt       *counts,*newproc_idx,ii,jj,kk,strideNew,*tidx,ncrs_new,ncrs_eq_new,nloc_old;
103:     IS             is_eq_newproc,is_eq_num,is_eq_num_prim,new_eq_indices;

105:     nloc_old = ncrs_eq/cr_bs;
106:     if (ncrs_eq % cr_bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"ncrs_eq %D not divisible by cr_bs %D",ncrs_eq,cr_bs);
107: #if defined PETSC_GAMG_USE_LOG
108:     PetscLogEventBegin(petsc_gamg_setup_events[SET12],0,0,0,0);
109: #endif
110:     /* make 'is_eq_newproc' */
111:     PetscMalloc1(size, &counts);
112:     if (pc_gamg->repart) {
113:       /* Repartition Cmat_{k} and move colums of P^{k}_{k-1} and coordinates of primal part accordingly */
114:       Mat adj;

116:      PetscInfo3(pc,"Repartition: size (active): %D --> %D, %D local equations\n",*a_nactive_proc,new_size,ncrs_eq);

118:       /* get 'adj' */
119:       if (cr_bs == 1) {
120:         MatConvert(Cmat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj);
121:       } else {
122:         /* make a scalar matrix to partition (no Stokes here) */
123:         Mat               tMat;
124:         PetscInt          Istart_crs,Iend_crs,ncols,jj,Ii;
125:         const PetscScalar *vals;
126:         const PetscInt    *idx;
127:         PetscInt          *d_nnz, *o_nnz, M, N;
128:         static PetscInt   llev = 0;
129:         MatType           mtype;

131:         PetscMalloc2(ncrs, &d_nnz,ncrs, &o_nnz);
132:         MatGetOwnershipRange(Cmat, &Istart_crs, &Iend_crs);
133:         MatGetSize(Cmat, &M, &N);
134:         for (Ii = Istart_crs, jj = 0; Ii < Iend_crs; Ii += cr_bs, jj++) {
135:           MatGetRow(Cmat,Ii,&ncols,0,0);
136:           d_nnz[jj] = ncols/cr_bs;
137:           o_nnz[jj] = ncols/cr_bs;
138:           MatRestoreRow(Cmat,Ii,&ncols,0,0);
139:           if (d_nnz[jj] > ncrs) d_nnz[jj] = ncrs;
140:           if (o_nnz[jj] > (M/cr_bs-ncrs)) o_nnz[jj] = M/cr_bs-ncrs;
141:         }

143:         MatGetType(Amat_fine,&mtype);
144:         MatCreate(comm, &tMat);
145:         MatSetSizes(tMat, ncrs, ncrs,PETSC_DETERMINE, PETSC_DETERMINE);
146:         MatSetType(tMat,mtype);
147:         MatSeqAIJSetPreallocation(tMat,0,d_nnz);
148:         MatMPIAIJSetPreallocation(tMat,0,d_nnz,0,o_nnz);
149:         PetscFree2(d_nnz,o_nnz);

151:         for (ii = Istart_crs; ii < Iend_crs; ii++) {
152:           PetscInt dest_row = ii/cr_bs;
153:           MatGetRow(Cmat,ii,&ncols,&idx,&vals);
154:           for (jj = 0; jj < ncols; jj++) {
155:             PetscInt    dest_col = idx[jj]/cr_bs;
156:             PetscScalar v        = 1.0;
157:             MatSetValues(tMat,1,&dest_row,1,&dest_col,&v,ADD_VALUES);
158:           }
159:           MatRestoreRow(Cmat,ii,&ncols,&idx,&vals);
160:         }
161:         MatAssemblyBegin(tMat,MAT_FINAL_ASSEMBLY);
162:         MatAssemblyEnd(tMat,MAT_FINAL_ASSEMBLY);

164:         if (llev++ == -1) {
165:           PetscViewer viewer; char fname[32];
166:           PetscSNPrintf(fname,sizeof(fname),"part_mat_%D.mat",llev);
167:           PetscViewerBinaryOpen(comm,fname,FILE_MODE_WRITE,&viewer);
168:           MatView(tMat, viewer);
169:           PetscViewerDestroy(&viewer);
170:         }
171:         MatConvert(tMat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj);
172:         MatDestroy(&tMat);
173:       } /* create 'adj' */

175:       { /* partition: get newproc_idx */
176:         char            prefix[256];
177:         const char      *pcpre;
178:         const PetscInt  *is_idx;
179:         MatPartitioning mpart;
180:         IS              proc_is;
181:         PetscInt        targetPE;

183:         MatPartitioningCreate(comm, &mpart);
184:         MatPartitioningSetAdjacency(mpart, adj);
185:         PCGetOptionsPrefix(pc, &pcpre);
186:         PetscSNPrintf(prefix,sizeof(prefix),"%spc_gamg_",pcpre ? pcpre : "");
187:         PetscObjectSetOptionsPrefix((PetscObject)mpart,prefix);
188:         MatPartitioningSetFromOptions(mpart);
189:         MatPartitioningSetNParts(mpart, new_size);
190:         MatPartitioningApply(mpart, &proc_is);
191:         MatPartitioningDestroy(&mpart);

193:         /* collect IS info */
194:         PetscMalloc1(ncrs_eq, &newproc_idx);
195:         ISGetIndices(proc_is, &is_idx);
196:         targetPE = 1; /* bring to "front" of machine */
197:         /*targetPE = size/new_size;*/ /* spread partitioning across machine */
198:         for (kk = jj = 0 ; kk < nloc_old ; kk++) {
199:           for (ii = 0 ; ii < cr_bs ; ii++, jj++) {
200:             newproc_idx[jj] = is_idx[kk] * targetPE; /* distribution */
201:           }
202:         }
203:         ISRestoreIndices(proc_is, &is_idx);
204:         ISDestroy(&proc_is);
205:       }
206:       MatDestroy(&adj);

208:       ISCreateGeneral(comm, ncrs_eq, newproc_idx, PETSC_COPY_VALUES, &is_eq_newproc);
209:       PetscFree(newproc_idx);
210:     } else { /* simple aggreagtion of parts -- 'is_eq_newproc' */
211:       PetscInt rfactor,targetPE;

213:       /* find factor */
214:       if (new_size == 1) rfactor = size; /* easy */
215:       else {
216:         PetscReal best_fact = 0.;
217:         jj = -1;
218:         for (kk = 1 ; kk <= size ; kk++) {
219:           if (!(size%kk)) { /* a candidate */
220:             PetscReal nactpe = (PetscReal)size/(PetscReal)kk, fact = nactpe/(PetscReal)new_size;
221:             if (fact > 1.0) fact = 1./fact; /* keep fact < 1 */
222:             if (fact > best_fact) {
223:               best_fact = fact; jj = kk;
224:             }
225:           }
226:         }
227:         if (jj != -1) rfactor = jj;
228:         else rfactor = 1; /* does this happen .. a prime */
229:       }
230:       new_size = size/rfactor;

232:       if (new_size==nactive) {
233:         *a_Amat_crs = Cmat; /* output - no repartitioning or reduction, bail out because nested here */
234:         PetscFree(counts);
235:         PetscInfo2(pc,"Aggregate processors noop: new_size=%D, neq(loc)=%D\n",new_size,ncrs_eq);
236: #if defined PETSC_GAMG_USE_LOG
237:         PetscLogEventEnd(petsc_gamg_setup_events[SET12],0,0,0,0);
238: #endif
239:         return(0);
240:       }

242:       PetscInfo1(pc,"Number of equations (loc) %D with simple aggregation\n",ncrs_eq);
243:       targetPE = rank/rfactor;
244:       ISCreateStride(comm, ncrs_eq, targetPE, 0, &is_eq_newproc);
245:     } /* end simple 'is_eq_newproc' */

247:     /*
248:      Create an index set from the is_eq_newproc index set to indicate the mapping TO
249:      */
250:     ISPartitioningToNumbering(is_eq_newproc, &is_eq_num);
251:     is_eq_num_prim = is_eq_num;
252:     /*
253:       Determine how many equations/vertices are assigned to each processor
254:      */
255:     ISPartitioningCount(is_eq_newproc, size, counts);
256:     ncrs_eq_new = counts[rank];
257:     ISDestroy(&is_eq_newproc);
258:     ncrs_new = ncrs_eq_new/cr_bs; /* eqs */

260:     PetscFree(counts);
261: #if defined PETSC_GAMG_USE_LOG
262:     PetscLogEventEnd(petsc_gamg_setup_events[SET12],0,0,0,0);
263: #endif
264:     /* data movement scope -- this could be moved to subclasses so that we don't try to cram all auxilary data into some complex abstracted thing */
265:     {
266:     Vec            src_crd, dest_crd;
267:     const PetscInt *idx,ndata_rows=pc_gamg->data_cell_rows,ndata_cols=pc_gamg->data_cell_cols,node_data_sz=ndata_rows*ndata_cols;
268:     VecScatter     vecscat;
269:     PetscScalar    *array;
270:     IS isscat;

272:     /* move data (for primal equations only) */
273:     /* Create a vector to contain the newly ordered element information */
274:     VecCreate(comm, &dest_crd);
275:     VecSetSizes(dest_crd, node_data_sz*ncrs_new, PETSC_DECIDE);
276:     VecSetType(dest_crd,VECSTANDARD); /* this is needed! */
277:     /*
278:      There are 'ndata_rows*ndata_cols' data items per node, (one can think of the vectors of having
279:      a block size of ...).  Note, ISs are expanded into equation space by 'cr_bs'.
280:      */
281:     PetscMalloc1(ncrs*node_data_sz, &tidx);
282:     ISGetIndices(is_eq_num_prim, &idx);
283:     for (ii=0,jj=0; ii<ncrs; ii++) {
284:       PetscInt id = idx[ii*cr_bs]/cr_bs; /* get node back */
285:       for (kk=0; kk<node_data_sz; kk++, jj++) tidx[jj] = id*node_data_sz + kk;
286:     }
287:     ISRestoreIndices(is_eq_num_prim, &idx);
288:     ISCreateGeneral(comm, node_data_sz*ncrs, tidx, PETSC_COPY_VALUES, &isscat);
289:     PetscFree(tidx);
290:     /*
291:      Create a vector to contain the original vertex information for each element
292:      */
293:     VecCreateSeq(PETSC_COMM_SELF, node_data_sz*ncrs, &src_crd);
294:     for (jj=0; jj<ndata_cols; jj++) {
295:       const PetscInt stride0=ncrs*pc_gamg->data_cell_rows;
296:       for (ii=0; ii<ncrs; ii++) {
297:         for (kk=0; kk<ndata_rows; kk++) {
298:           PetscInt    ix = ii*ndata_rows + kk + jj*stride0, jx = ii*node_data_sz + kk*ndata_cols + jj;
299:           PetscScalar tt = (PetscScalar)pc_gamg->data[ix];
300:           VecSetValues(src_crd, 1, &jx, &tt, INSERT_VALUES);
301:         }
302:       }
303:     }
304:     VecAssemblyBegin(src_crd);
305:     VecAssemblyEnd(src_crd);
306:     /*
307:       Scatter the element vertex information (still in the original vertex ordering)
308:       to the correct processor
309:     */
310:     VecScatterCreate(src_crd, NULL, dest_crd, isscat, &vecscat);
311:     ISDestroy(&isscat);
312:     VecScatterBegin(vecscat,src_crd,dest_crd,INSERT_VALUES,SCATTER_FORWARD);
313:     VecScatterEnd(vecscat,src_crd,dest_crd,INSERT_VALUES,SCATTER_FORWARD);
314:     VecScatterDestroy(&vecscat);
315:     VecDestroy(&src_crd);
316:     /*
317:       Put the element vertex data into a new allocation of the gdata->ele
318:     */
319:     PetscFree(pc_gamg->data);
320:     PetscMalloc1(node_data_sz*ncrs_new, &pc_gamg->data);

322:     pc_gamg->data_sz = node_data_sz*ncrs_new;
323:     strideNew        = ncrs_new*ndata_rows;

325:     VecGetArray(dest_crd, &array);
326:     for (jj=0; jj<ndata_cols; jj++) {
327:       for (ii=0; ii<ncrs_new; ii++) {
328:         for (kk=0; kk<ndata_rows; kk++) {
329:           PetscInt ix = ii*ndata_rows + kk + jj*strideNew, jx = ii*node_data_sz + kk*ndata_cols + jj;
330:           pc_gamg->data[ix] = PetscRealPart(array[jx]);
331:         }
332:       }
333:     }
334:     VecRestoreArray(dest_crd, &array);
335:     VecDestroy(&dest_crd);
336:     }
337:     /* move A and P (columns) with new layout */
338: #if defined PETSC_GAMG_USE_LOG
339:     PetscLogEventBegin(petsc_gamg_setup_events[SET13],0,0,0,0);
340: #endif

342:     /*
343:       Invert for MatCreateSubMatrix
344:     */
345:     ISInvertPermutation(is_eq_num, ncrs_eq_new, &new_eq_indices);
346:     ISSort(new_eq_indices); /* is this needed? */
347:     ISSetBlockSize(new_eq_indices, cr_bs);
348:     if (is_eq_num != is_eq_num_prim) {
349:       ISDestroy(&is_eq_num_prim); /* could be same as 'is_eq_num' */
350:     }
351:     if (Pcolumnperm) {
352:       PetscObjectReference((PetscObject)new_eq_indices);
353:       *Pcolumnperm = new_eq_indices;
354:     }
355:     ISDestroy(&is_eq_num);
356: #if defined PETSC_GAMG_USE_LOG
357:     PetscLogEventEnd(petsc_gamg_setup_events[SET13],0,0,0,0);
358:     PetscLogEventBegin(petsc_gamg_setup_events[SET14],0,0,0,0);
359: #endif
360:     /* 'a_Amat_crs' output */
361:     {
362:       Mat mat;
363:       MatCreateSubMatrix(Cmat, new_eq_indices, new_eq_indices, MAT_INITIAL_MATRIX, &mat);
364:       *a_Amat_crs = mat;
365:     }
366:     MatDestroy(&Cmat);

368: #if defined PETSC_GAMG_USE_LOG
369:     PetscLogEventEnd(petsc_gamg_setup_events[SET14],0,0,0,0);
370: #endif
371:     /* prolongator */
372:     {
373:       IS       findices;
374:       PetscInt Istart,Iend;
375:       Mat      Pnew;

377:       MatGetOwnershipRange(Pold, &Istart, &Iend);
378: #if defined PETSC_GAMG_USE_LOG
379:       PetscLogEventBegin(petsc_gamg_setup_events[SET15],0,0,0,0);
380: #endif
381:       ISCreateStride(comm,Iend-Istart,Istart,1,&findices);
382:       ISSetBlockSize(findices,f_bs);
383:       MatCreateSubMatrix(Pold, findices, new_eq_indices, MAT_INITIAL_MATRIX, &Pnew);
384:       ISDestroy(&findices);

386: #if defined PETSC_GAMG_USE_LOG
387:       PetscLogEventEnd(petsc_gamg_setup_events[SET15],0,0,0,0);
388: #endif
389:       MatDestroy(a_P_inout);

391:       /* output - repartitioned */
392:       *a_P_inout = Pnew;
393:     }
394:     ISDestroy(&new_eq_indices);

396:     *a_nactive_proc = new_size; /* output */
397:   }
398:   return(0);
399: }

401: /* -------------------------------------------------------------------------- */
402: /*
403:    PCSetUp_GAMG - Prepares for the use of the GAMG preconditioner
404:                     by setting data structures and options.

406:    Input Parameter:
407: .  pc - the preconditioner context

409: */
410: PetscErrorCode PCSetUp_GAMG(PC pc)
411: {
413:   PC_MG          *mg      = (PC_MG*)pc->data;
414:   PC_GAMG        *pc_gamg = (PC_GAMG*)mg->innerctx;
415:   Mat            Pmat     = pc->pmat;
416:   PetscInt       fine_level,level,level1,bs,M,N,qq,lidx,nASMBlocksArr[PETSC_GAMG_MAXLEVELS];
417:   MPI_Comm       comm;
418:   PetscMPIInt    rank,size,nactivepe;
419:   Mat            Aarr[PETSC_GAMG_MAXLEVELS],Parr[PETSC_GAMG_MAXLEVELS];
420:   IS             *ASMLocalIDsArr[PETSC_GAMG_MAXLEVELS];
421:   PetscLogDouble nnz0=0.,nnztot=0.;
422:   MatInfo        info;
423:   PetscBool      is_last = PETSC_FALSE;

426:   PetscObjectGetComm((PetscObject)pc,&comm);
427:   MPI_Comm_rank(comm,&rank);
428:   MPI_Comm_size(comm,&size);

430:   if (pc_gamg->setup_count++ > 0) {
431:     if ((PetscBool)(!pc_gamg->reuse_prol)) {
432:       /* reset everything */
433:       PCReset_MG(pc);
434:       pc->setupcalled = 0;
435:     } else {
436:       PC_MG_Levels **mglevels = mg->levels;
437:       /* just do Galerkin grids */
438:       Mat          B,dA,dB;

440:      if (!pc->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"PCSetUp() has not been called yet");
441:       if (pc_gamg->Nlevels > 1) {
442:         /* currently only handle case where mat and pmat are the same on coarser levels */
443:         KSPGetOperators(mglevels[pc_gamg->Nlevels-1]->smoothd,&dA,&dB);
444:         /* (re)set to get dirty flag */
445:         KSPSetOperators(mglevels[pc_gamg->Nlevels-1]->smoothd,dA,dB);

447:         for (level=pc_gamg->Nlevels-2; level>=0; level--) {
448:           /* the first time through the matrix structure has changed from repartitioning */
449:           if (pc_gamg->setup_count==2) {
450:             MatPtAP(dB,mglevels[level+1]->interpolate,MAT_INITIAL_MATRIX,1.0,&B);
451:             MatDestroy(&mglevels[level]->A);

453:             mglevels[level]->A = B;
454:           } else {
455:             KSPGetOperators(mglevels[level]->smoothd,NULL,&B);
456:             MatPtAP(dB,mglevels[level+1]->interpolate,MAT_REUSE_MATRIX,1.0,&B);
457:           }
458:           KSPSetOperators(mglevels[level]->smoothd,B,B);
459:           dB   = B;
460:         }
461:       }

463:       PCSetUp_MG(pc);
464:       return(0);
465:     }
466:   }

468:   if (!pc_gamg->data) {
469:     if (pc_gamg->orig_data) {
470:       MatGetBlockSize(Pmat, &bs);
471:       MatGetLocalSize(Pmat, &qq, NULL);

473:       pc_gamg->data_sz        = (qq/bs)*pc_gamg->orig_data_cell_rows*pc_gamg->orig_data_cell_cols;
474:       pc_gamg->data_cell_rows = pc_gamg->orig_data_cell_rows;
475:       pc_gamg->data_cell_cols = pc_gamg->orig_data_cell_cols;

477:       PetscMalloc1(pc_gamg->data_sz, &pc_gamg->data);
478:       for (qq=0; qq<pc_gamg->data_sz; qq++) pc_gamg->data[qq] = pc_gamg->orig_data[qq];
479:     } else {
480:       if (!pc_gamg->ops->createdefaultdata) SETERRQ(comm,PETSC_ERR_PLIB,"'createdefaultdata' not set(?) need to support NULL data");
481:       pc_gamg->ops->createdefaultdata(pc,Pmat);
482:     }
483:   }

485:   /* cache original data for reuse */
486:   if (!pc_gamg->orig_data && (PetscBool)(!pc_gamg->reuse_prol)) {
487:     PetscMalloc1(pc_gamg->data_sz, &pc_gamg->orig_data);
488:     for (qq=0; qq<pc_gamg->data_sz; qq++) pc_gamg->orig_data[qq] = pc_gamg->data[qq];
489:     pc_gamg->orig_data_cell_rows = pc_gamg->data_cell_rows;
490:     pc_gamg->orig_data_cell_cols = pc_gamg->data_cell_cols;
491:   }

493:   /* get basic dims */
494:   MatGetBlockSize(Pmat, &bs);
495:   MatGetSize(Pmat, &M, &N);

497:   MatGetInfo(Pmat,MAT_GLOBAL_SUM,&info); /* global reduction */
498:   nnz0   = info.nz_used;
499:   nnztot = info.nz_used;
500:   PetscInfo6(pc,"level %d) N=%D, n data rows=%d, n data cols=%d, nnz/row (ave)=%d, np=%d\n",0,M,pc_gamg->data_cell_rows,pc_gamg->data_cell_cols,(int)(nnz0/(PetscReal)M+0.5),size);

502:   /* Get A_i and R_i */
503:   for (level=0, Aarr[0]=Pmat, nactivepe = size; level < (pc_gamg->Nlevels-1) && (!level || M>pc_gamg->coarse_eq_limit); level++) {
504:     pc_gamg->current_level = level;
505:     level1 = level + 1;
506: #if defined PETSC_GAMG_USE_LOG
507:     PetscLogEventBegin(petsc_gamg_setup_events[SET1],0,0,0,0);
508: #if (defined GAMG_STAGES)
509:     PetscLogStagePush(gamg_stages[level]);
510: #endif
511: #endif
512:     { /* construct prolongator */
513:       Mat              Gmat;
514:       PetscCoarsenData *agg_lists;
515:       Mat              Prol11;

517:       pc_gamg->ops->graph(pc,Aarr[level], &Gmat);
518:       pc_gamg->ops->coarsen(pc, &Gmat, &agg_lists);
519:       pc_gamg->ops->prolongator(pc,Aarr[level],Gmat,agg_lists,&Prol11);

521:       /* could have failed to create new level */
522:       if (Prol11) {
523:         /* get new block size of coarse matrices */
524:         MatGetBlockSizes(Prol11, NULL, &bs);

526:         if (pc_gamg->ops->optprolongator) {
527:           /* smooth */
528:           pc_gamg->ops->optprolongator(pc, Aarr[level], &Prol11);
529:         }

531:         Parr[level1] = Prol11;
532:       } else Parr[level1] = NULL; /* failed to coarsen */

534:       if (pc_gamg->use_aggs_in_asm) {
535:         PetscInt bs;
536:         MatGetBlockSizes(Prol11, &bs, NULL);
537:         PetscCDGetASMBlocks(agg_lists, bs, Gmat, &nASMBlocksArr[level], &ASMLocalIDsArr[level]);
538:       }

540:       MatDestroy(&Gmat);
541:       PetscCDDestroy(agg_lists);
542:     } /* construct prolongator scope */
543: #if defined PETSC_GAMG_USE_LOG
544:     PetscLogEventEnd(petsc_gamg_setup_events[SET1],0,0,0,0);
545: #endif
546:     if (!level) Aarr[0] = Pmat; /* use Pmat for finest level setup */
547:     if (!Parr[level1]) { /* failed to coarsen */
548:        PetscInfo1(pc,"Stop gridding, level %D\n",level);
549: #if defined PETSC_GAMG_USE_LOG && defined GAMG_STAGES
550:       PetscLogStagePop();
551: #endif
552:       break;
553:     }
554: #if defined PETSC_GAMG_USE_LOG
555:     PetscLogEventBegin(petsc_gamg_setup_events[SET2],0,0,0,0);
556: #endif
557:     MatGetSize(Parr[level1], &M, &N); /* N is next M, a loop test variables */
558:     if (is_last) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Is last ????????");
559:     if (N <= pc_gamg->coarse_eq_limit) is_last = PETSC_TRUE;
560:     pc_gamg->ops->createlevel(pc, Aarr[level], bs, &Parr[level1], &Aarr[level1], &nactivepe, NULL, is_last);

562: #if defined PETSC_GAMG_USE_LOG
563:     PetscLogEventEnd(petsc_gamg_setup_events[SET2],0,0,0,0);
564: #endif
565:     MatGetSize(Aarr[level1], &M, &N); /* M is loop test variables */
566:     MatGetInfo(Aarr[level1], MAT_GLOBAL_SUM, &info);
567:     nnztot += info.nz_used;
568:     PetscInfo5(pc,"%d) N=%D, n data cols=%d, nnz/row (ave)=%d, %d active pes\n",level1,M,pc_gamg->data_cell_cols,(int)(info.nz_used/(PetscReal)M),nactivepe);

570: #if (defined PETSC_GAMG_USE_LOG && defined GAMG_STAGES)
571:     PetscLogStagePop();
572: #endif
573:     /* stop if one node or one proc -- could pull back for singular problems */
574:     if ( (pc_gamg->data_cell_cols && M/pc_gamg->data_cell_cols < 2) || (!pc_gamg->data_cell_cols && M/bs < 2) ) {
575:        PetscInfo2(pc,"HARD stop of coarsening on level %D.  Grid too small: %D block nodes\n",level,M/bs);
576:       level++;
577:       break;
578:     }
579:   } /* levels */
580:   PetscFree(pc_gamg->data);

582:   PetscInfo2(pc,"%D levels, grid complexity = %g\n",level+1,nnztot/nnz0);
583:   pc_gamg->Nlevels = level + 1;
584:   fine_level       = level;
585:   PCMGSetLevels(pc,pc_gamg->Nlevels,NULL);

587:   if (pc_gamg->Nlevels > 1) { /* don't setup MG if one level */
588:     /* set default smoothers & set operators */
589:     for (lidx = 1, level = pc_gamg->Nlevels-2; lidx <= fine_level; lidx++, level--) {
590:       KSP smoother;
591:       PC  subpc;

593:       PCMGGetSmoother(pc, lidx, &smoother);
594:       KSPGetPC(smoother, &subpc);

596:       KSPSetNormType(smoother, KSP_NORM_NONE);
597:       /* set ops */
598:       KSPSetOperators(smoother, Aarr[level], Aarr[level]);
599:       PCMGSetInterpolation(pc, lidx, Parr[level+1]);

601:       /* set defaults */
602:       KSPSetType(smoother, KSPCHEBYSHEV);

604:       /* set blocks for ASM smoother that uses the 'aggregates' */
605:       if (pc_gamg->use_aggs_in_asm) {
606:         PetscInt sz;
607:         IS       *iss;

609:         sz   = nASMBlocksArr[level];
610:         iss   = ASMLocalIDsArr[level];
611:         PCSetType(subpc, PCASM);
612:         PCASMSetOverlap(subpc, 0);
613:         PCASMSetType(subpc,PC_ASM_BASIC);
614:         if (!sz) {
615:           IS       is;
616:           ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_COPY_VALUES, &is);
617:           PCASMSetLocalSubdomains(subpc, 1, NULL, &is);
618:           ISDestroy(&is);
619:         } else {
620:           PetscInt kk;
621:           PCASMSetLocalSubdomains(subpc, sz, NULL, iss);
622:           for (kk=0; kk<sz; kk++) {
623:             ISDestroy(&iss[kk]);
624:           }
625:           PetscFree(iss);
626:         }
627:         ASMLocalIDsArr[level] = NULL;
628:         nASMBlocksArr[level]  = 0;
629:       } else {
630:         PCSetType(subpc, PCSOR);
631:       }
632:     }
633:     {
634:       /* coarse grid */
635:       KSP smoother,*k2; PC subpc,pc2; PetscInt ii,first;
636:       Mat Lmat = Aarr[(level=pc_gamg->Nlevels-1)]; lidx = 0;
637:       PCMGGetSmoother(pc, lidx, &smoother);
638:       KSPSetOperators(smoother, Lmat, Lmat);
639:       if (!pc_gamg->use_parallel_coarse_grid_solver) {
640:         KSPSetNormType(smoother, KSP_NORM_NONE);
641:         KSPGetPC(smoother, &subpc);
642:         PCSetType(subpc, PCBJACOBI);
643:         PCSetUp(subpc);
644:         PCBJacobiGetSubKSP(subpc,&ii,&first,&k2);
645:         if (ii != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"ii %D is not one",ii);
646:         KSPGetPC(k2[0],&pc2);
647:         PCSetType(pc2, PCLU);
648:         PCFactorSetShiftType(pc2,MAT_SHIFT_INBLOCKS);
649:         KSPSetTolerances(k2[0],PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,1);
650:         KSPSetType(k2[0], KSPPREONLY);
651:         /* This flag gets reset by PCBJacobiGetSubKSP(), but our BJacobi really does the same algorithm everywhere (and in
652:          * fact, all but one process will have zero dofs), so we reset the flag to avoid having PCView_BJacobi attempt to
653:          * view every subdomain as though they were different. */
654:         ((PC_BJacobi*)subpc->data)->same_local_solves = PETSC_TRUE;
655:       }
656:     }

658:     /* should be called in PCSetFromOptions_GAMG(), but cannot be called prior to PCMGSetLevels() */
659:     PetscObjectOptionsBegin((PetscObject)pc);
660:     PCSetFromOptions_MG(PetscOptionsObject,pc);
661:     PetscOptionsEnd();
662:     PCMGSetGalerkin(pc,PC_MG_GALERKIN_EXTERNAL);

664:     /* clean up */
665:     for (level=1; level<pc_gamg->Nlevels; level++) {
666:       MatDestroy(&Parr[level]);
667:       MatDestroy(&Aarr[level]);
668:     }
669:     PCSetUp_MG(pc);
670:   } else {
671:     KSP smoother;
672:     PetscInfo(pc,"One level solver used (system is seen as DD). Using default solver.\n");
673:     PCMGGetSmoother(pc, 0, &smoother);
674:     KSPSetOperators(smoother, Aarr[0], Aarr[0]);
675:     KSPSetType(smoother, KSPPREONLY);
676:     PCSetUp_MG(pc);
677:   }
678:   return(0);
679: }

681: /* ------------------------------------------------------------------------- */
682: /*
683:  PCDestroy_GAMG - Destroys the private context for the GAMG preconditioner
684:    that was created with PCCreate_GAMG().

686:    Input Parameter:
687: .  pc - the preconditioner context

689:    Application Interface Routine: PCDestroy()
690: */
691: PetscErrorCode PCDestroy_GAMG(PC pc)
692: {
694:   PC_MG          *mg     = (PC_MG*)pc->data;
695:   PC_GAMG        *pc_gamg= (PC_GAMG*)mg->innerctx;

698:   PCReset_GAMG(pc);
699:   if (pc_gamg->ops->destroy) {
700:     (*pc_gamg->ops->destroy)(pc);
701:   }
702:   PetscFree(pc_gamg->ops);
703:   PetscFree(pc_gamg->gamg_type_name);
704:   PetscFree(pc_gamg);
705:   PCDestroy_MG(pc);
706:   return(0);
707: }

709: /*@
710:    PCGAMGSetProcEqLim - Set number of equations to aim for per process on the coarse grids via processor reduction.

712:    Logically Collective on PC

714:    Input Parameters:
715: +  pc - the preconditioner context
716: -  n - the number of equations


719:    Options Database Key:
720: .  -pc_gamg_process_eq_limit <limit>

722:    Notes: GAMG will reduce the number of MPI processes used directly on the coarse grids so that there are around <limit> equations on each process 
723:           that has degrees of freedom

725:    Level: intermediate

727:    Concepts: Unstructured multigrid preconditioner

729: .seealso: PCGAMGSetCoarseEqLim()
730: @*/
731: PetscErrorCode  PCGAMGSetProcEqLim(PC pc, PetscInt n)
732: {

737:   PetscTryMethod(pc,"PCGAMGSetProcEqLim_C",(PC,PetscInt),(pc,n));
738:   return(0);
739: }

741: static PetscErrorCode PCGAMGSetProcEqLim_GAMG(PC pc, PetscInt n)
742: {
743:   PC_MG   *mg      = (PC_MG*)pc->data;
744:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;

747:   if (n>0) pc_gamg->min_eq_proc = n;
748:   return(0);
749: }

751: /*@
752:    PCGAMGSetCoarseEqLim - Set maximum number of equations on coarsest grid.

754:  Collective on PC

756:    Input Parameters:
757: +  pc - the preconditioner context
758: -  n - maximum number of equations to aim for

760:    Options Database Key:
761: .  -pc_gamg_coarse_eq_limit <limit>

763:    Level: intermediate

765:    Concepts: Unstructured multigrid preconditioner

767: .seealso: PCGAMGSetProcEqLim()
768: @*/
769: PetscErrorCode PCGAMGSetCoarseEqLim(PC pc, PetscInt n)
770: {

775:   PetscTryMethod(pc,"PCGAMGSetCoarseEqLim_C",(PC,PetscInt),(pc,n));
776:   return(0);
777: }

779: static PetscErrorCode PCGAMGSetCoarseEqLim_GAMG(PC pc, PetscInt n)
780: {
781:   PC_MG   *mg      = (PC_MG*)pc->data;
782:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;

785:   if (n>0) pc_gamg->coarse_eq_limit = n;
786:   return(0);
787: }

789: /*@
790:    PCGAMGSetRepartition - Repartition the degrees of freedom across the processors on the coarser grids

792:    Collective on PC

794:    Input Parameters:
795: +  pc - the preconditioner context
796: -  n - PETSC_TRUE or PETSC_FALSE

798:    Options Database Key:
799: .  -pc_gamg_repartition <true,false>

801:    Notes: this will generally improve the loading balancing of the work on each level

803:    Level: intermediate

805:    Concepts: Unstructured multigrid preconditioner

807: .seealso: ()
808: @*/
809: PetscErrorCode PCGAMGSetRepartition(PC pc, PetscBool n)
810: {

815:   PetscTryMethod(pc,"PCGAMGSetRepartition_C",(PC,PetscBool),(pc,n));
816:   return(0);
817: }

819: static PetscErrorCode PCGAMGSetRepartition_GAMG(PC pc, PetscBool n)
820: {
821:   PC_MG   *mg      = (PC_MG*)pc->data;
822:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;

825:   pc_gamg->repart = n;
826:   return(0);
827: }

829: /*@
830:    PCGAMGSetReuseInterpolation - Reuse prolongation when rebuilding algebraic multigrid preconditioner

832:    Collective on PC

834:    Input Parameters:
835: +  pc - the preconditioner context
836: -  n - PETSC_TRUE or PETSC_FALSE

838:    Options Database Key:
839: .  -pc_gamg_reuse_interpolation <true,false>

841:    Level: intermediate

843:    Notes: this may negatively affect the convergence rate of the method on new matrices if the matrix entries change a great deal, but allows
844:           rebuilding the preconditioner quicker.

846:    Concepts: Unstructured multigrid preconditioner

848: .seealso: ()
849: @*/
850: PetscErrorCode PCGAMGSetReuseInterpolation(PC pc, PetscBool n)
851: {

856:   PetscTryMethod(pc,"PCGAMGSetReuseInterpolation_C",(PC,PetscBool),(pc,n));
857:   return(0);
858: }

860: static PetscErrorCode PCGAMGSetReuseInterpolation_GAMG(PC pc, PetscBool n)
861: {
862:   PC_MG   *mg      = (PC_MG*)pc->data;
863:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;

866:   pc_gamg->reuse_prol = n;
867:   return(0);
868: }

870: /*@
871:    PCGAMGASMSetUseAggs - Have the PCGAMG smoother on each level use the aggregates defined by the coarsening process as the subdomains for the additive Schwarz preconditioner.

873:    Collective on PC

875:    Input Parameters:
876: +  pc - the preconditioner context
877: -  flg - PETSC_TRUE to use aggregates, PETSC_FALSE to not

879:    Options Database Key:
880: .  -pc_gamg_asm_use_agg

882:    Level: intermediate

884:    Concepts: Unstructured multigrid preconditioner

886: .seealso: ()
887: @*/
888: PetscErrorCode PCGAMGASMSetUseAggs(PC pc, PetscBool flg)
889: {

894:   PetscTryMethod(pc,"PCGAMGASMSetUseAggs_C",(PC,PetscBool),(pc,flg));
895:   return(0);
896: }

898: static PetscErrorCode PCGAMGASMSetUseAggs_GAMG(PC pc, PetscBool flg)
899: {
900:   PC_MG   *mg      = (PC_MG*)pc->data;
901:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;

904:   pc_gamg->use_aggs_in_asm = flg;
905:   return(0);
906: }

908: /*@
909:    PCGAMGSetUseParallelCoarseGridSolve - allow a parallel coarse grid solver

911:    Collective on PC

913:    Input Parameters:
914: +  pc - the preconditioner context
915: -  flg - PETSC_TRUE to not force coarse grid onto one processor

917:    Options Database Key:
918: .  -pc_gamg_use_parallel_coarse_grid_solver

920:    Level: intermediate

922:    Concepts: Unstructured multigrid preconditioner

924: .seealso: ()
925: @*/
926: PetscErrorCode PCGAMGSetUseParallelCoarseGridSolve(PC pc, PetscBool flg)
927: {

932:   PetscTryMethod(pc,"PCGAMGSetUseParallelCoarseGridSolve_C",(PC,PetscBool),(pc,flg));
933:   return(0);
934: }

936: static PetscErrorCode PCGAMGSetUseParallelCoarseGridSolve_GAMG(PC pc, PetscBool flg)
937: {
938:   PC_MG   *mg      = (PC_MG*)pc->data;
939:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;

942:   pc_gamg->use_parallel_coarse_grid_solver = flg;
943:   return(0);
944: }

946: /*@
947:    PCGAMGSetNlevels -  Sets the maximum number of levels PCGAMG will use

949:    Not collective on PC

951:    Input Parameters:
952: +  pc - the preconditioner
953: -  n - the maximum number of levels to use

955:    Options Database Key:
956: .  -pc_mg_levels

958:    Level: intermediate

960:    Concepts: Unstructured multigrid preconditioner

962: .seealso: ()
963: @*/
964: PetscErrorCode PCGAMGSetNlevels(PC pc, PetscInt n)
965: {

970:   PetscTryMethod(pc,"PCGAMGSetNlevels_C",(PC,PetscInt),(pc,n));
971:   return(0);
972: }

974: static PetscErrorCode PCGAMGSetNlevels_GAMG(PC pc, PetscInt n)
975: {
976:   PC_MG   *mg      = (PC_MG*)pc->data;
977:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;

980:   pc_gamg->Nlevels = n;
981:   return(0);
982: }

984: /*@
985:    PCGAMGSetThreshold - Relative threshold to use for dropping edges in aggregation graph

987:    Not collective on PC

989:    Input Parameters:
990: +  pc - the preconditioner context
991: -  threshold - the threshold value, 0.0 means keep all nonzero entries in the graph; negative means keep even zero entries in the graph

993:    Options Database Key:
994: .  -pc_gamg_threshold <threshold>

996:    Notes: Before aggregating the graph GAMG will remove small values from the graph thus reducing the coupling in the graph and a different
997:     (perhaps better) coarser set of points.

999:    Level: intermediate

1001:    Concepts: Unstructured multigrid preconditioner

1003: .seealso: PCGAMGFilterGraph()
1004: @*/
1005: PetscErrorCode PCGAMGSetThreshold(PC pc, PetscReal v[], PetscInt n)
1006: {

1011:   PetscTryMethod(pc,"PCGAMGSetThreshold_C",(PC,PetscReal[],PetscInt),(pc,v,n));
1012:   return(0);
1013: }

1015: static PetscErrorCode PCGAMGSetThreshold_GAMG(PC pc, PetscReal v[], PetscInt n)
1016: {
1017:   PC_MG   *mg      = (PC_MG*)pc->data;
1018:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;
1019:   PetscInt i;
1021:   for (i=0;i<n;i++) pc_gamg->threshold[i] = v[i];
1022:   do {pc_gamg->threshold[i] = pc_gamg->threshold[i-1]*pc_gamg->threshold_scale;} while (++i<PETSC_GAMG_MAXLEVELS);
1023:   return(0);
1024: }

1026: /*@
1027:    PCGAMGSetThresholdScale - Relative threshold reduction at each level

1029:    Not collective on PC

1031:    Input Parameters:
1032: +  pc - the preconditioner context
1033: -  scale - the threshold value reduction, ussually < 1.0

1035:    Options Database Key:
1036: .  -pc_gamg_threshold_scale <v>

1038:    Level: advanced

1040:    Concepts: Unstructured multigrid preconditioner

1042: .seealso: ()
1043: @*/
1044: PetscErrorCode PCGAMGSetThresholdScale(PC pc, PetscReal v)
1045: {

1050:   PetscTryMethod(pc,"PCGAMGSetThresholdScale_C",(PC,PetscReal),(pc,v));
1051:   return(0);
1052: }

1054: static PetscErrorCode PCGAMGSetThresholdScale_GAMG(PC pc, PetscReal v)
1055: {
1056:   PC_MG   *mg      = (PC_MG*)pc->data;
1057:   PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;
1059:   pc_gamg->threshold_scale = v;
1060:   return(0);
1061: }

1063: /*@C
1064:    PCGAMGSetType - Set solution method

1066:    Collective on PC

1068:    Input Parameters:
1069: +  pc - the preconditioner context
1070: -  type - PCGAMGAGG, PCGAMGGEO, or PCGAMGCLASSICAL

1072:    Options Database Key:
1073: .  -pc_gamg_type <agg,geo,classical> - type of algebraic multigrid to apply

1075:    Level: intermediate

1077:    Concepts: Unstructured multigrid preconditioner

1079: .seealso: PCGAMGGetType(), PCGAMG, PCGAMGType
1080: @*/
1081: PetscErrorCode PCGAMGSetType(PC pc, PCGAMGType type)
1082: {

1087:   PetscTryMethod(pc,"PCGAMGSetType_C",(PC,PCGAMGType),(pc,type));
1088:   return(0);
1089: }

1091: /*@C
1092:    PCGAMGGetType - Get solution method

1094:    Collective on PC

1096:    Input Parameter:
1097: .  pc - the preconditioner context

1099:    Output Parameter:
1100: .  type - the type of algorithm used

1102:    Level: intermediate

1104:    Concepts: Unstructured multigrid preconditioner

1106: .seealso: PCGAMGSetType(), PCGAMGType
1107: @*/
1108: PetscErrorCode PCGAMGGetType(PC pc, PCGAMGType *type)
1109: {

1114:   PetscUseMethod(pc,"PCGAMGGetType_C",(PC,PCGAMGType*),(pc,type));
1115:   return(0);
1116: }

1118: static PetscErrorCode PCGAMGGetType_GAMG(PC pc, PCGAMGType *type)
1119: {
1120:   PC_MG          *mg      = (PC_MG*)pc->data;
1121:   PC_GAMG        *pc_gamg = (PC_GAMG*)mg->innerctx;

1124:   *type = pc_gamg->type;
1125:   return(0);
1126: }

1128: static PetscErrorCode PCGAMGSetType_GAMG(PC pc, PCGAMGType type)
1129: {
1130:   PetscErrorCode ierr,(*r)(PC);
1131:   PC_MG          *mg      = (PC_MG*)pc->data;
1132:   PC_GAMG        *pc_gamg = (PC_GAMG*)mg->innerctx;

1135:   pc_gamg->type = type;
1136:   PetscFunctionListFind(GAMGList,type,&r);
1137:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown GAMG type %s given",type);
1138:   if (pc_gamg->ops->destroy) {
1139:     (*pc_gamg->ops->destroy)(pc);
1140:     PetscMemzero(pc_gamg->ops,sizeof(struct _PCGAMGOps));
1141:     pc_gamg->ops->createlevel = PCGAMGCreateLevel_GAMG;
1142:     /* cleaning up common data in pc_gamg - this should disapear someday */
1143:     pc_gamg->data_cell_cols = 0;
1144:     pc_gamg->data_cell_rows = 0;
1145:     pc_gamg->orig_data_cell_cols = 0;
1146:     pc_gamg->orig_data_cell_rows = 0;
1147:     PetscFree(pc_gamg->data);
1148:     pc_gamg->data_sz = 0;
1149:   }
1150:   PetscFree(pc_gamg->gamg_type_name);
1151:   PetscStrallocpy(type,&pc_gamg->gamg_type_name);
1152:   (*r)(pc);
1153:   return(0);
1154: }

1156: static PetscErrorCode PCView_GAMG(PC pc,PetscViewer viewer)
1157: {
1158:   PetscErrorCode ierr,i;
1159:   PC_MG          *mg      = (PC_MG*)pc->data;
1160:   PC_GAMG        *pc_gamg = (PC_GAMG*)mg->innerctx;

1163:   PetscViewerASCIIPrintf(viewer,"    GAMG specific options\n");
1164:   PetscViewerASCIIPrintf(viewer,"      Threshold for dropping small values in graph on each level =");
1165:   for (i=0;i<pc_gamg->current_level;i++) {
1166:     PetscViewerASCIIPrintf(viewer," %g",(double)pc_gamg->threshold[i]);
1167:   }
1168:   PetscViewerASCIIPrintf(viewer,"\n");
1169:   PetscViewerASCIIPrintf(viewer,"      Threshold scaling factor for each level not specified = %g\n",(double)pc_gamg->threshold_scale);
1170:   if (pc_gamg->use_aggs_in_asm) {
1171:     PetscViewerASCIIPrintf(viewer,"      Using aggregates from coarsening process to define subdomains for PCASM\n");
1172:   }
1173:   if (pc_gamg->use_parallel_coarse_grid_solver) {
1174:     PetscViewerASCIIPrintf(viewer,"      Using parallel coarse grid solver (all coarse grid equations not put on one process)\n");
1175:   }
1176:   if (pc_gamg->ops->view) {
1177:     (*pc_gamg->ops->view)(pc,viewer);
1178:   }
1179:   return(0);
1180: }

1182: PetscErrorCode PCSetFromOptions_GAMG(PetscOptionItems *PetscOptionsObject,PC pc)
1183: {
1185:   PC_MG          *mg      = (PC_MG*)pc->data;
1186:   PC_GAMG        *pc_gamg = (PC_GAMG*)mg->innerctx;
1187:   PetscBool      flag;
1188:   MPI_Comm       comm;
1189:   char           prefix[256];
1190:   PetscInt       i,n;
1191:   const char     *pcpre;

1194:   PetscObjectGetComm((PetscObject)pc,&comm);
1195:   PetscOptionsHead(PetscOptionsObject,"GAMG options");
1196:   {
1197:     char tname[256];
1198:     PetscOptionsFList("-pc_gamg_type","Type of AMG method","PCGAMGSetType",GAMGList, pc_gamg->gamg_type_name, tname, sizeof(tname), &flag);
1199:     if (flag) {
1200:       PCGAMGSetType(pc,tname);
1201:     }
1202:     PetscOptionsBool("-pc_gamg_repartition","Repartion coarse grids","PCGAMGSetRepartition",pc_gamg->repart,&pc_gamg->repart,NULL);
1203:     PetscOptionsBool("-pc_gamg_reuse_interpolation","Reuse prolongation operator","PCGAMGReuseInterpolation",pc_gamg->reuse_prol,&pc_gamg->reuse_prol,NULL);
1204:     PetscOptionsBool("-pc_gamg_asm_use_agg","Use aggregation agragates for ASM smoother","PCGAMGASMSetUseAggs",pc_gamg->use_aggs_in_asm,&pc_gamg->use_aggs_in_asm,NULL);
1205:     PetscOptionsBool("-pc_gamg_use_parallel_coarse_grid_solver","Use parallel coarse grid solver (otherwise put last grid on one process)","PCGAMGSetUseParallelCoarseGridSolve",pc_gamg->use_parallel_coarse_grid_solver,&pc_gamg->use_parallel_coarse_grid_solver,NULL);
1206:     PetscOptionsInt("-pc_gamg_process_eq_limit","Limit (goal) on number of equations per process on coarse grids","PCGAMGSetProcEqLim",pc_gamg->min_eq_proc,&pc_gamg->min_eq_proc,NULL);
1207:     PetscOptionsInt("-pc_gamg_coarse_eq_limit","Limit on number of equations for the coarse grid","PCGAMGSetCoarseEqLim",pc_gamg->coarse_eq_limit,&pc_gamg->coarse_eq_limit,NULL);
1208:     PetscOptionsReal("-pc_gamg_threshold_scale","scaleing of threshold for each level not specified","PCGAMGSetThresholdScale",pc_gamg->threshold_scale,&pc_gamg->threshold_scale,NULL);
1209:     n = PETSC_GAMG_MAXLEVELS;
1210:     PetscOptionsRealArray("-pc_gamg_threshold","Relative threshold to use for dropping edges in aggregation graph","PCGAMGSetThreshold",pc_gamg->threshold,&n,&flag);
1211:     if (!flag || n < PETSC_GAMG_MAXLEVELS) {
1212:       if (!flag) n = 1;
1213:       i = n;
1214:       do {pc_gamg->threshold[i] = pc_gamg->threshold[i-1]*pc_gamg->threshold_scale;} while (++i<PETSC_GAMG_MAXLEVELS);
1215:     }
1216:     PetscOptionsInt("-pc_mg_levels","Set number of MG levels","PCGAMGSetNlevels",pc_gamg->Nlevels,&pc_gamg->Nlevels,NULL);

1218:     /* set options for subtype */
1219:     if (pc_gamg->ops->setfromoptions) {(*pc_gamg->ops->setfromoptions)(PetscOptionsObject,pc);}
1220:   }
1221:   PCGetOptionsPrefix(pc, &pcpre);
1222:   PetscSNPrintf(prefix,sizeof(prefix),"%spc_gamg_",pcpre ? pcpre : "");
1223:   PetscOptionsTail();
1224:   return(0);
1225: }

1227: /* -------------------------------------------------------------------------- */
1228: /*MC
1229:      PCGAMG - Geometric algebraic multigrid (AMG) preconditioner

1231:    Options Database Keys:
1232: +   -pc_gamg_type <type> - one of agg, geo, or classical
1233: .   -pc_gamg_repartition  <true,default=false> - repartition the degrees of freedom accross the coarse grids as they are determined
1234: .   -pc_gamg_reuse_interpolation <true,default=false> - when rebuilding the algebraic multigrid preconditioner reuse the previously computed interpolations
1235: .   -pc_gamg_asm_use_agg <true,default=false> - use the aggregates from the coasening process to defined the subdomains on each level for the PCASM smoother
1236: .   -pc_gamg_process_eq_limit <limit, default=50> - GAMG will reduce the number of MPI processes used directly on the coarse grids so that there are around <limit>
1237:                                         equations on each process that has degrees of freedom
1238: .   -pc_gamg_coarse_eq_limit <limit, default=50> - Set maximum number of equations on coarsest grid to aim for.
1239: -   -pc_gamg_threshold[] <thresh,default=0> - Before aggregating the graph GAMG will remove small values from the graph on each level
1240: -   -pc_gamg_threshold_scale <scale,default=1> - Scaling of threshold on each coarser grid if not specified

1242:    Options Database Keys for default Aggregation:
1243: +  -pc_gamg_agg_nsmooths <nsmooth, default=1> - number of smoothing steps to use with smooth aggregation
1244: .  -pc_gamg_sym_graph <true,default=false> - symmetrize the graph before computing the aggregation
1245: -  -pc_gamg_square_graph <n,default=1> - number of levels to square the graph before aggregating it

1247:    Multigrid options(inherited):
1248: +  -pc_mg_cycles <v>: v or w (PCMGSetCycleType())
1249: .  -pc_mg_smoothup <1>: Number of post-smoothing steps (PCMGSetNumberSmoothUp)
1250: .  -pc_mg_smoothdown <1>: Number of pre-smoothing steps (PCMGSetNumberSmoothDown)
1251: .  -pc_mg_type <multiplicative>: (one of) additive multiplicative full kascade
1252: -  -pc_mg_levels <levels> - Number of levels of multigrid to use.


1255:   Notes: In order to obtain good performance for PCGAMG for vector valued problems you must
1256: $       Call MatSetBlockSize() to indicate the number of degrees of freedom per grid point
1257: $       Call MatSetNearNullSpace() (or PCSetCoordinates() if solving the equations of elasticity) to indicate the near null space of the operator
1258: $       See the Users Manual Chapter 4 for more details

1260:   Level: intermediate

1262:   Concepts: algebraic multigrid

1264: .seealso:  PCCreate(), PCSetType(), MatSetBlockSize(), PCMGType, PCSetCoordinates(), MatSetNearNullSpace(), PCGAMGSetType(), PCGAMGAGG, PCGAMGGEO, PCGAMGCLASSICAL, PCGAMGSetProcEqLim(),
1265:            PCGAMGSetCoarseEqLim(), PCGAMGSetRepartition(), PCGAMGRegister(), PCGAMGSetReuseInterpolation(), PCGAMGASMSetUseAggs(), PCGAMGSetUseParallelCoarseGridSolve(), PCGAMGSetNlevels(), PCGAMGSetThreshold(), PCGAMGGetType(), PCGAMGSetReuseInterpolation()
1266: M*/

1268: PETSC_EXTERN PetscErrorCode PCCreate_GAMG(PC pc)
1269: {
1270:   PetscErrorCode ierr,i;
1271:   PC_GAMG        *pc_gamg;
1272:   PC_MG          *mg;

1275:    /* register AMG type */
1276:   PCGAMGInitializePackage();

1278:   /* PCGAMG is an inherited class of PCMG. Initialize pc as PCMG */
1279:   PCSetType(pc, PCMG);
1280:   PetscObjectChangeTypeName((PetscObject)pc, PCGAMG);

1282:   /* create a supporting struct and attach it to pc */
1283:   PetscNewLog(pc,&pc_gamg);
1284:   PCMGSetGalerkin(pc,PC_MG_GALERKIN_EXTERNAL);
1285:   mg           = (PC_MG*)pc->data;
1286:   mg->innerctx = pc_gamg;

1288:   PetscNewLog(pc,&pc_gamg->ops);

1290:   pc_gamg->setup_count = 0;
1291:   /* these should be in subctx but repartitioning needs simple arrays */
1292:   pc_gamg->data_sz = 0;
1293:   pc_gamg->data    = 0;

1295:   /* overwrite the pointers of PCMG by the functions of base class PCGAMG */
1296:   pc->ops->setfromoptions = PCSetFromOptions_GAMG;
1297:   pc->ops->setup          = PCSetUp_GAMG;
1298:   pc->ops->reset          = PCReset_GAMG;
1299:   pc->ops->destroy        = PCDestroy_GAMG;
1300:   mg->view                = PCView_GAMG;

1302:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetProcEqLim_C",PCGAMGSetProcEqLim_GAMG);
1303:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetCoarseEqLim_C",PCGAMGSetCoarseEqLim_GAMG);
1304:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetRepartition_C",PCGAMGSetRepartition_GAMG);
1305:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetReuseInterpolation_C",PCGAMGSetReuseInterpolation_GAMG);
1306:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGASMSetUseAggs_C",PCGAMGASMSetUseAggs_GAMG);
1307:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetUseParallelCoarseGridSolve_C",PCGAMGSetUseParallelCoarseGridSolve_GAMG);
1308:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetThreshold_C",PCGAMGSetThreshold_GAMG);
1309:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetThresholdScale_C",PCGAMGSetThresholdScale_GAMG);
1310:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetType_C",PCGAMGSetType_GAMG);
1311:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGGetType_C",PCGAMGGetType_GAMG);
1312:   PetscObjectComposeFunction((PetscObject)pc,"PCGAMGSetNlevels_C",PCGAMGSetNlevels_GAMG);
1313:   pc_gamg->repart           = PETSC_FALSE;
1314:   pc_gamg->reuse_prol       = PETSC_FALSE;
1315:   pc_gamg->use_aggs_in_asm  = PETSC_FALSE;
1316:   pc_gamg->use_parallel_coarse_grid_solver = PETSC_FALSE;
1317:   pc_gamg->min_eq_proc      = 50;
1318:   pc_gamg->coarse_eq_limit  = 50;
1319:   for (i=0;i<PETSC_GAMG_MAXLEVELS;i++) pc_gamg->threshold[i] = 0.;
1320:   pc_gamg->threshold_scale = 1.;
1321:   pc_gamg->Nlevels          = PETSC_GAMG_MAXLEVELS;
1322:   pc_gamg->current_level    = 0; /* don't need to init really */
1323:   pc_gamg->ops->createlevel = PCGAMGCreateLevel_GAMG;

1325:   /* PCSetUp_GAMG assumes that the type has been set, so set it to the default now */
1326:   PCGAMGSetType(pc,PCGAMGAGG);
1327:   return(0);
1328: }

1330: /*@C
1331:  PCGAMGInitializePackage - This function initializes everything in the PCGAMG package. It is called
1332:     from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to PCCreate_GAMG()
1333:     when using static libraries.

1335:  Level: developer

1337:  .keywords: PC, PCGAMG, initialize, package
1338:  .seealso: PetscInitialize()
1339: @*/
1340: PetscErrorCode PCGAMGInitializePackage(void)
1341: {

1345:   if (PCGAMGPackageInitialized) return(0);
1346:   PCGAMGPackageInitialized = PETSC_TRUE;
1347:   PetscFunctionListAdd(&GAMGList,PCGAMGGEO,PCCreateGAMG_GEO);
1348:   PetscFunctionListAdd(&GAMGList,PCGAMGAGG,PCCreateGAMG_AGG);
1349:   PetscFunctionListAdd(&GAMGList,PCGAMGCLASSICAL,PCCreateGAMG_Classical);
1350:   PetscRegisterFinalize(PCGAMGFinalizePackage);

1352:   /* general events */
1353:   PetscLogEventRegister("PCGAMGGraph_AGG", 0, &PC_GAMGGraph_AGG);
1354:   PetscLogEventRegister("PCGAMGGraph_GEO", PC_CLASSID, &PC_GAMGGraph_GEO);
1355:   PetscLogEventRegister("PCGAMGCoarse_AGG", PC_CLASSID, &PC_GAMGCoarsen_AGG);
1356:   PetscLogEventRegister("PCGAMGCoarse_GEO", PC_CLASSID, &PC_GAMGCoarsen_GEO);
1357:   PetscLogEventRegister("PCGAMGProl_AGG", PC_CLASSID, &PC_GAMGProlongator_AGG);
1358:   PetscLogEventRegister("PCGAMGProl_GEO", PC_CLASSID, &PC_GAMGProlongator_GEO);
1359:   PetscLogEventRegister("PCGAMGPOpt_AGG", PC_CLASSID, &PC_GAMGOptProlongator_AGG);

1361: #if defined PETSC_GAMG_USE_LOG
1362:   PetscLogEventRegister("GAMG: createProl", PC_CLASSID, &petsc_gamg_setup_events[SET1]);
1363:   PetscLogEventRegister("  Graph", PC_CLASSID, &petsc_gamg_setup_events[GRAPH]);
1364:   /* PetscLogEventRegister("    G.Mat", PC_CLASSID, &petsc_gamg_setup_events[GRAPH_MAT]); */
1365:   /* PetscLogEventRegister("    G.Filter", PC_CLASSID, &petsc_gamg_setup_events[GRAPH_FILTER]); */
1366:   /* PetscLogEventRegister("    G.Square", PC_CLASSID, &petsc_gamg_setup_events[GRAPH_SQR]); */
1367:   PetscLogEventRegister("  MIS/Agg", PC_CLASSID, &petsc_gamg_setup_events[SET4]);
1368:   PetscLogEventRegister("  geo: growSupp", PC_CLASSID, &petsc_gamg_setup_events[SET5]);
1369:   PetscLogEventRegister("  geo: triangle", PC_CLASSID, &petsc_gamg_setup_events[SET6]);
1370:   PetscLogEventRegister("    search-set", PC_CLASSID, &petsc_gamg_setup_events[FIND_V]);
1371:   PetscLogEventRegister("  SA: col data", PC_CLASSID, &petsc_gamg_setup_events[SET7]);
1372:   PetscLogEventRegister("  SA: frmProl0", PC_CLASSID, &petsc_gamg_setup_events[SET8]);
1373:   PetscLogEventRegister("  SA: smooth", PC_CLASSID, &petsc_gamg_setup_events[SET9]);
1374:   PetscLogEventRegister("GAMG: partLevel", PC_CLASSID, &petsc_gamg_setup_events[SET2]);
1375:   PetscLogEventRegister("  repartition", PC_CLASSID, &petsc_gamg_setup_events[SET12]);
1376:   PetscLogEventRegister("  Invert-Sort", PC_CLASSID, &petsc_gamg_setup_events[SET13]);
1377:   PetscLogEventRegister("  Move A", PC_CLASSID, &petsc_gamg_setup_events[SET14]);
1378:   PetscLogEventRegister("  Move P", PC_CLASSID, &petsc_gamg_setup_events[SET15]);

1380:   /* PetscLogEventRegister(" PL move data", PC_CLASSID, &petsc_gamg_setup_events[SET13]); */
1381:   /* PetscLogEventRegister("GAMG: fix", PC_CLASSID, &petsc_gamg_setup_events[SET10]); */
1382:   /* PetscLogEventRegister("GAMG: set levels", PC_CLASSID, &petsc_gamg_setup_events[SET11]); */
1383:   /* create timer stages */
1384: #if defined GAMG_STAGES
1385:   {
1386:     char     str[32];
1387:     PetscInt lidx;
1388:     sprintf(str,"MG Level %d (finest)",0);
1389:     PetscLogStageRegister(str, &gamg_stages[0]);
1390:     for (lidx=1; lidx<9; lidx++) {
1391:       sprintf(str,"MG Level %d",lidx);
1392:       PetscLogStageRegister(str, &gamg_stages[lidx]);
1393:     }
1394:   }
1395: #endif
1396: #endif
1397:   return(0);
1398: }

1400: /*@C
1401:  PCGAMGFinalizePackage - This function frees everything from the PCGAMG package. It is
1402:     called from PetscFinalize() automatically.

1404:  Level: developer

1406:  .keywords: Petsc, destroy, package
1407:  .seealso: PetscFinalize()
1408: @*/
1409: PetscErrorCode PCGAMGFinalizePackage(void)
1410: {

1414:   PCGAMGPackageInitialized = PETSC_FALSE;
1415:   PetscFunctionListDestroy(&GAMGList);
1416:   return(0);
1417: }

1419: /*@C
1420:  PCGAMGRegister - Register a PCGAMG implementation.

1422:  Input Parameters:
1423:  + type - string that will be used as the name of the GAMG type.
1424:  - create - function for creating the gamg context.

1426:   Level: advanced

1428:  .seealso: PCGAMGType, PCGAMG, PCGAMGSetType()
1429: @*/
1430: PetscErrorCode PCGAMGRegister(PCGAMGType type, PetscErrorCode (*create)(PC))
1431: {

1435:   PCGAMGInitializePackage();
1436:   PetscFunctionListAdd(&GAMGList,type,create);
1437:   return(0);
1438: }