Actual source code: ml.c

petsc-master 2017-08-22
Report Typos and Errors

  2: /*
  3:    Provides an interface to the ML smoothed Aggregation
  4:    Note: Something non-obvious breaks -pc_mg_type ADDITIVE for parallel runs
  5:                                     Jed Brown, see [PETSC #18321, #18449].
  6: */
  7:  #include <petsc/private/pcimpl.h>
  8:  #include <petsc/private/pcmgimpl.h>
  9:  #include <../src/mat/impls/aij/seq/aij.h>
 10:  #include <../src/mat/impls/aij/mpi/mpiaij.h>
 11: #include <petscdm.h>            /* for DMDestroy(&pc->mg) hack */

 13: EXTERN_C_BEGIN
 14: /* HAVE_CONFIG_H flag is required by ML include files */
 15: #if !defined(HAVE_CONFIG_H)
 16: #define HAVE_CONFIG_H
 17: #endif
 18: #include <ml_include.h>
 19: #include <ml_viz_stats.h>
 20: EXTERN_C_END

 22: typedef enum {PCML_NULLSPACE_AUTO,PCML_NULLSPACE_USER,PCML_NULLSPACE_BLOCK,PCML_NULLSPACE_SCALAR} PCMLNullSpaceType;
 23: static const char *const PCMLNullSpaceTypes[] = {"AUTO","USER","BLOCK","SCALAR","PCMLNullSpaceType","PCML_NULLSPACE_",0};

 25: /* The context (data structure) at each grid level */
 26: typedef struct {
 27:   Vec x,b,r;                  /* global vectors */
 28:   Mat A,P,R;
 29:   KSP ksp;
 30:   Vec coords;                 /* projected by ML, if PCSetCoordinates is called; values packed by node */
 31: } GridCtx;

 33: /* The context used to input PETSc matrix into ML at fine grid */
 34: typedef struct {
 35:   Mat         A;       /* Petsc matrix in aij format */
 36:   Mat         Aloc;    /* local portion of A to be used by ML */
 37:   Vec         x,y;
 38:   ML_Operator *mlmat;
 39:   PetscScalar *pwork;  /* tmp array used by PetscML_comm() */
 40: } FineGridCtx;

 42: /* The context associates a ML matrix with a PETSc shell matrix */
 43: typedef struct {
 44:   Mat         A;        /* PETSc shell matrix associated with mlmat */
 45:   ML_Operator *mlmat;   /* ML matrix assorciated with A */
 46:   Vec         y, work;
 47: } Mat_MLShell;

 49: /* Private context for the ML preconditioner */
 50: typedef struct {
 51:   ML                *ml_object;
 52:   ML_Aggregate      *agg_object;
 53:   GridCtx           *gridctx;
 54:   FineGridCtx       *PetscMLdata;
 55:   PetscInt          Nlevels,MaxNlevels,MaxCoarseSize,CoarsenScheme,EnergyMinimization,MinPerProc,PutOnSingleProc,RepartitionType,ZoltanScheme;
 56:   PetscReal         Threshold,DampingFactor,EnergyMinimizationDropTol,MaxMinRatio,AuxThreshold;
 57:   PetscBool         SpectralNormScheme_Anorm,BlockScaling,EnergyMinimizationCheap,Symmetrize,OldHierarchy,KeepAggInfo,Reusable,Repartition,Aux;
 58:   PetscBool         reuse_interpolation;
 59:   PCMLNullSpaceType nulltype;
 60:   PetscMPIInt       size; /* size of communicator for pc->pmat */
 61:   PetscInt          dim;  /* data from PCSetCoordinates(_ML) */
 62:   PetscInt          nloc;
 63:   PetscReal         *coords; /* ML has a grid object for each level: the finest grid will point into coords */
 64: } PC_ML;

 66: static int PetscML_getrow(ML_Operator *ML_data, int N_requested_rows, int requested_rows[],int allocated_space, int columns[], double values[], int row_lengths[])
 67: {
 69:   PetscInt       m,i,j,k=0,row,*aj;
 70:   PetscScalar    *aa;
 71:   FineGridCtx    *ml=(FineGridCtx*)ML_Get_MyGetrowData(ML_data);
 72:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)ml->Aloc->data;

 74:   MatGetSize(ml->Aloc,&m,NULL); if (ierr) return(0);
 75:   for (i = 0; i<N_requested_rows; i++) {
 76:     row            = requested_rows[i];
 77:     row_lengths[i] = a->ilen[row];
 78:     if (allocated_space < k+row_lengths[i]) return(0);
 79:     if ((row >= 0) || (row <= (m-1))) {
 80:       aj = a->j + a->i[row];
 81:       aa = a->a + a->i[row];
 82:       for (j=0; j<row_lengths[i]; j++) {
 83:         columns[k]  = aj[j];
 84:         values[k++] = aa[j];
 85:       }
 86:     }
 87:   }
 88:   return(1);
 89: }

 91: static PetscErrorCode PetscML_comm(double p[],void *ML_data)
 92: {
 93:   PetscErrorCode    ierr;
 94:   FineGridCtx       *ml = (FineGridCtx*)ML_data;
 95:   Mat               A   = ml->A;
 96:   Mat_MPIAIJ        *a  = (Mat_MPIAIJ*)A->data;
 97:   PetscMPIInt       size;
 98:   PetscInt          i,in_length=A->rmap->n,out_length=ml->Aloc->cmap->n;
 99:   const PetscScalar *array;

102:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
103:   if (size == 1) return 0;

105:   VecPlaceArray(ml->y,p);
106:   VecScatterBegin(a->Mvctx,ml->y,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
107:   VecScatterEnd(a->Mvctx,ml->y,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
108:   VecResetArray(ml->y);
109:   VecGetArrayRead(a->lvec,&array);
110:   for (i=in_length; i<out_length; i++) p[i] = array[i-in_length];
111:   VecRestoreArrayRead(a->lvec,&array);
112:   return(0);
113: }

115: static int PetscML_matvec(ML_Operator *ML_data,int in_length,double p[],int out_length,double ap[])
116: {
118:   FineGridCtx    *ml = (FineGridCtx*)ML_Get_MyMatvecData(ML_data);
119:   Mat            A   = ml->A, Aloc=ml->Aloc;
120:   PetscMPIInt    size;
121:   PetscScalar    *pwork=ml->pwork;
122:   PetscInt       i;

125:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
126:   if (size == 1) {
127:     VecPlaceArray(ml->x,p);
128:   } else {
129:     for (i=0; i<in_length; i++) pwork[i] = p[i];
130:     PetscML_comm(pwork,ml);
131:     VecPlaceArray(ml->x,pwork);
132:   }
133:   VecPlaceArray(ml->y,ap);
134:   MatMult(Aloc,ml->x,ml->y);
135:   VecResetArray(ml->x);
136:   VecResetArray(ml->y);
137:   return(0);
138: }

140: static PetscErrorCode MatMult_ML(Mat A,Vec x,Vec y)
141: {
142:   PetscErrorCode    ierr;
143:   Mat_MLShell       *shell;
144:   PetscScalar       *yarray;
145:   const PetscScalar *xarray;
146:   PetscInt          x_length,y_length;

149:   MatShellGetContext(A,(void**)&shell);
150:   VecGetArrayRead(x,&xarray);
151:   VecGetArray(y,&yarray);
152:   x_length = shell->mlmat->invec_leng;
153:   y_length = shell->mlmat->outvec_leng;
154:   PetscStackCall("ML_Operator_Apply",ML_Operator_Apply(shell->mlmat,x_length,(PetscScalar*)xarray,y_length,yarray));
155:   VecRestoreArrayRead(x,&xarray);
156:   VecRestoreArray(y,&yarray);
157:   return(0);
158: }

160: /* Computes y = w + A * x
161:    It is possible that w == y, but not x == y
162: */
163: static PetscErrorCode MatMultAdd_ML(Mat A,Vec x,Vec w,Vec y)
164: {
165:   Mat_MLShell       *shell;
166:   PetscScalar       *yarray;
167:   const PetscScalar *xarray;
168:   PetscInt          x_length,y_length;
169:   PetscErrorCode    ierr;

172:   MatShellGetContext(A, (void**) &shell);
173:   if (y == w) {
174:     if (!shell->work) {
175:       VecDuplicate(y, &shell->work);
176:     }
177:     VecGetArrayRead(x,           &xarray);
178:     VecGetArray(shell->work, &yarray);
179:     x_length = shell->mlmat->invec_leng;
180:     y_length = shell->mlmat->outvec_leng;
181:     PetscStackCall("ML_Operator_Apply",ML_Operator_Apply(shell->mlmat, x_length, (PetscScalar*)xarray, y_length, yarray));
182:     VecRestoreArrayRead(x,           &xarray);
183:     VecRestoreArray(shell->work, &yarray);
184:     VecAXPY(y, 1.0, shell->work);
185:   } else {
186:     VecGetArrayRead(x, &xarray);
187:     VecGetArray(y, &yarray);
188:     x_length = shell->mlmat->invec_leng;
189:     y_length = shell->mlmat->outvec_leng;
190:     PetscStackCall("ML_Operator_Apply",ML_Operator_Apply(shell->mlmat, x_length, (PetscScalar *)xarray, y_length, yarray));
191:     VecRestoreArrayRead(x, &xarray);
192:     VecRestoreArray(y, &yarray);
193:     VecAXPY(y, 1.0, w);
194:   }
195:   return(0);
196: }

198: /* newtype is ignored since only handles one case */
199: static PetscErrorCode MatConvert_MPIAIJ_ML(Mat A,MatType newtype,MatReuse scall,Mat *Aloc)
200: {
202:   Mat_MPIAIJ     *mpimat=(Mat_MPIAIJ*)A->data;
203:   Mat_SeqAIJ     *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
204:   PetscInt       *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
205:   PetscScalar    *aa=a->a,*ba=b->a,*ca;
206:   PetscInt       am =A->rmap->n,an=A->cmap->n,i,j,k;
207:   PetscInt       *ci,*cj,ncols;

210:   if (am != an) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"A must have a square diagonal portion, am: %d != an: %d",am,an);

212:   if (scall == MAT_INITIAL_MATRIX) {
213:     PetscMalloc1(1+am,&ci);
214:     ci[0] = 0;
215:     for (i=0; i<am; i++) ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
216:     PetscMalloc1(1+ci[am],&cj);
217:     PetscMalloc1(1+ci[am],&ca);

219:     k = 0;
220:     for (i=0; i<am; i++) {
221:       /* diagonal portion of A */
222:       ncols = ai[i+1] - ai[i];
223:       for (j=0; j<ncols; j++) {
224:         cj[k]   = *aj++;
225:         ca[k++] = *aa++;
226:       }
227:       /* off-diagonal portion of A */
228:       ncols = bi[i+1] - bi[i];
229:       for (j=0; j<ncols; j++) {
230:         cj[k]   = an + (*bj); bj++;
231:         ca[k++] = *ba++;
232:       }
233:     }
234:     if (k != ci[am]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"k: %d != ci[am]: %d",k,ci[am]);

236:     /* put together the new matrix */
237:     an   = mpimat->A->cmap->n+mpimat->B->cmap->n;
238:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,an,ci,cj,ca,Aloc);

240:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
241:     /* Since these are PETSc arrays, change flags to free them as necessary. */
242:     mat          = (Mat_SeqAIJ*)(*Aloc)->data;
243:     mat->free_a  = PETSC_TRUE;
244:     mat->free_ij = PETSC_TRUE;

246:     mat->nonew = 0;
247:   } else if (scall == MAT_REUSE_MATRIX) {
248:     mat=(Mat_SeqAIJ*)(*Aloc)->data;
249:     ci = mat->i; cj = mat->j; ca = mat->a;
250:     for (i=0; i<am; i++) {
251:       /* diagonal portion of A */
252:       ncols = ai[i+1] - ai[i];
253:       for (j=0; j<ncols; j++) *ca++ = *aa++;
254:       /* off-diagonal portion of A */
255:       ncols = bi[i+1] - bi[i];
256:       for (j=0; j<ncols; j++) *ca++ = *ba++;
257:     }
258:   } else SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
259:   return(0);
260: }

262: static PetscErrorCode MatDestroy_ML(Mat A)
263: {
265:   Mat_MLShell    *shell;

268:   MatShellGetContext(A,(void**)&shell);
269:   VecDestroy(&shell->y);
270:   if (shell->work) {VecDestroy(&shell->work);}
271:   PetscFree(shell);
272:   return(0);
273: }

275: static PetscErrorCode MatWrapML_SeqAIJ(ML_Operator *mlmat,MatReuse reuse,Mat *newmat)
276: {
277:   struct ML_CSR_MSRdata *matdata = (struct ML_CSR_MSRdata*)mlmat->data;
278:   PetscErrorCode        ierr;
279:   PetscInt              m       =mlmat->outvec_leng,n=mlmat->invec_leng,*nnz = NULL,nz_max;
280:   PetscInt              *ml_cols=matdata->columns,*ml_rowptr=matdata->rowptr,*aj,i;
281:   PetscScalar           *ml_vals=matdata->values,*aa;

284:   if (!mlmat->getrow) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"mlmat->getrow = NULL");
285:   if (m != n) { /* ML Pmat and Rmat are in CSR format. Pass array pointers into SeqAIJ matrix */
286:     if (reuse) {
287:       Mat_SeqAIJ *aij= (Mat_SeqAIJ*)(*newmat)->data;
288:       aij->i = ml_rowptr;
289:       aij->j = ml_cols;
290:       aij->a = ml_vals;
291:     } else {
292:       /* sort ml_cols and ml_vals */
293:       PetscMalloc1(m+1,&nnz);
294:       for (i=0; i<m; i++) nnz[i] = ml_rowptr[i+1] - ml_rowptr[i];
295:       aj = ml_cols; aa = ml_vals;
296:       for (i=0; i<m; i++) {
297:         PetscSortIntWithScalarArray(nnz[i],aj,aa);
298:         aj  += nnz[i]; aa += nnz[i];
299:       }
300:       MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,ml_rowptr,ml_cols,ml_vals,newmat);
301:       PetscFree(nnz);
302:     }
303:     return(0);
304:   }

306:   nz_max = PetscMax(1,mlmat->max_nz_per_row);
307:   PetscMalloc2(nz_max,&aa,nz_max,&aj);
308:   if (!reuse) {
309:     MatCreate(PETSC_COMM_SELF,newmat);
310:     MatSetSizes(*newmat,m,n,PETSC_DECIDE,PETSC_DECIDE);
311:     MatSetType(*newmat,MATSEQAIJ);
312:     /* keep track of block size for A matrices */
313:     MatSetBlockSize (*newmat, mlmat->num_PDEs);

315:     PetscMalloc1(m,&nnz);
316:     for (i=0; i<m; i++) {
317:       PetscStackCall("ML_Operator_Getrow",ML_Operator_Getrow(mlmat,1,&i,nz_max,aj,aa,&nnz[i]));
318:     }
319:     MatSeqAIJSetPreallocation(*newmat,0,nnz);
320:   }
321:   for (i=0; i<m; i++) {
322:     PetscInt ncols;

324:     PetscStackCall("ML_Operator_Getrow",ML_Operator_Getrow(mlmat,1,&i,nz_max,aj,aa,&ncols));
325:     MatSetValues(*newmat,1,&i,ncols,aj,aa,INSERT_VALUES);
326:   }
327:   MatAssemblyBegin(*newmat,MAT_FINAL_ASSEMBLY);
328:   MatAssemblyEnd(*newmat,MAT_FINAL_ASSEMBLY);

330:   PetscFree2(aa,aj);
331:   PetscFree(nnz);
332:   return(0);
333: }

335: static PetscErrorCode MatWrapML_SHELL(ML_Operator *mlmat,MatReuse reuse,Mat *newmat)
336: {
338:   PetscInt       m,n;
339:   ML_Comm        *MLcomm;
340:   Mat_MLShell    *shellctx;

343:   m = mlmat->outvec_leng;
344:   n = mlmat->invec_leng;

346:   if (reuse) {
347:     MatShellGetContext(*newmat,(void**)&shellctx);
348:     shellctx->mlmat = mlmat;
349:     return(0);
350:   }

352:   MLcomm = mlmat->comm;

354:   PetscNew(&shellctx);
355:   MatCreateShell(MLcomm->USR_comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,shellctx,newmat);
356:   MatShellSetOperation(*newmat,MATOP_MULT,(void(*)(void))MatMult_ML);
357:   MatShellSetOperation(*newmat,MATOP_MULT_ADD,(void(*)(void))MatMultAdd_ML);
358:   MatShellSetOperation(*newmat,MATOP_DESTROY,(void(*)(void))MatDestroy_ML);

360:   shellctx->A         = *newmat;
361:   shellctx->mlmat     = mlmat;
362:   shellctx->work      = NULL;

364:   VecCreate(MLcomm->USR_comm,&shellctx->y);
365:   VecSetSizes(shellctx->y,m,PETSC_DECIDE);
366:   VecSetType(shellctx->y,VECSTANDARD);
367:   return(0);
368: }

370: static PetscErrorCode MatWrapML_MPIAIJ(ML_Operator *mlmat,MatReuse reuse,Mat *newmat)
371: {
372:   PetscInt       *aj;
373:   PetscScalar    *aa;
375:   PetscInt       i,j,*gordering;
376:   PetscInt       m=mlmat->outvec_leng,n,nz_max,row;
377:   Mat            A;

380:   if (!mlmat->getrow) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"mlmat->getrow = NULL");
381:   n = mlmat->invec_leng;
382:   if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"m %d must equal to n %d",m,n);

384:   /* create global row numbering for a ML_Operator */
385:   PetscStackCall("ML_build_global_numbering",ML_build_global_numbering(mlmat,&gordering,"rows"));

387:   nz_max = PetscMax(1,mlmat->max_nz_per_row) + 1;
388:   PetscMalloc2(nz_max,&aa,nz_max,&aj);
389:   if (reuse) {
390:     A = *newmat;
391:   } else {
392:     PetscInt *nnzA,*nnzB,*nnz;
393:     PetscInt rstart;
394:     MatCreate(mlmat->comm->USR_comm,&A);
395:     MatSetSizes(A,m,n,PETSC_DECIDE,PETSC_DECIDE);
396:     MatSetType(A,MATMPIAIJ);
397:     /* keep track of block size for A matrices */
398:     MatSetBlockSize (A,mlmat->num_PDEs);
399:     PetscMalloc3(m,&nnzA,m,&nnzB,m,&nnz);
400:     MPI_Scan(&m,&rstart,1,MPIU_INT,MPI_SUM,mlmat->comm->USR_comm);
401:     rstart -= m;

403:     for (i=0; i<m; i++) {
404:       row = gordering[i] - rstart;
405:       PetscStackCall("ML_Operator_Getrow",ML_Operator_Getrow(mlmat,1,&i,nz_max,aj,aa,&nnz[i]));
406:       nnzA[row] = 0;
407:       for (j=0; j<nnz[i]; j++) {
408:         if (aj[j] < m) nnzA[row]++;
409:       }
410:       nnzB[row] = nnz[i] - nnzA[row];
411:     }
412:     MatMPIAIJSetPreallocation(A,0,nnzA,0,nnzB);
413:     PetscFree3(nnzA,nnzB,nnz);
414:   }
415:   for (i=0; i<m; i++) {
416:     PetscInt ncols;
417:     row = gordering[i];

419:     PetscStackCall(",ML_Operator_Getrow",ML_Operator_Getrow(mlmat,1,&i,nz_max,aj,aa,&ncols));
420:     for (j = 0; j < ncols; j++) aj[j] = gordering[aj[j]];
421:     MatSetValues(A,1,&row,ncols,aj,aa,INSERT_VALUES);
422:   }
423:   PetscStackCall("ML_free",ML_free(gordering));
424:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
425:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
426:   *newmat = A;

428:   PetscFree2(aa,aj);
429:   return(0);
430: }

432: /* -------------------------------------------------------------------------- */
433: /*
434:    PCSetCoordinates_ML

436:    Input Parameter:
437:    .  pc - the preconditioner context
438: */
439: static PetscErrorCode PCSetCoordinates_ML(PC pc, PetscInt ndm, PetscInt a_nloc, PetscReal *coords)
440: {
441:   PC_MG          *mg    = (PC_MG*)pc->data;
442:   PC_ML          *pc_ml = (PC_ML*)mg->innerctx;
444:   PetscInt       arrsz,oldarrsz,bs,my0,kk,ii,nloc,Iend;
445:   Mat            Amat = pc->pmat;

447:   /* this function copied and modified from PCSetCoordinates_GEO -TGI */
450:   MatGetBlockSize(Amat, &bs);

452:   MatGetOwnershipRange(Amat, &my0, &Iend);
453:   nloc = (Iend-my0)/bs;

455:   if (nloc!=a_nloc) SETERRQ2(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Number of local blocks must locations = %d %d.",a_nloc,nloc);
456:   if ((Iend-my0)%bs!=0) SETERRQ1(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Bad local size %d.",nloc);

458:   oldarrsz    = pc_ml->dim * pc_ml->nloc;
459:   pc_ml->dim  = ndm;
460:   pc_ml->nloc = a_nloc;
461:   arrsz       = ndm * a_nloc;

463:   /* create data - syntactic sugar that should be refactored at some point */
464:   if (pc_ml->coords==0 || (oldarrsz != arrsz)) {
465:     PetscFree(pc_ml->coords);
466:     PetscMalloc1(arrsz, &pc_ml->coords);
467:   }
468:   for (kk=0; kk<arrsz; kk++) pc_ml->coords[kk] = -999.;
469:   /* copy data in - column oriented */
470:   for (kk = 0; kk < nloc; kk++) {
471:     for (ii = 0; ii < ndm; ii++) {
472:       pc_ml->coords[ii*nloc + kk] =  coords[kk*ndm + ii];
473:     }
474:   }
475:   return(0);
476: }

478: /* -----------------------------------------------------------------------------*/
479: extern PetscErrorCode PCReset_MG(PC);
480: PetscErrorCode PCReset_ML(PC pc)
481: {
483:   PC_MG          *mg    = (PC_MG*)pc->data;
484:   PC_ML          *pc_ml = (PC_ML*)mg->innerctx;
485:   PetscInt       level,fine_level=pc_ml->Nlevels-1,dim=pc_ml->dim;

488:   if (dim) {
489:     ML_Aggregate_Viz_Stats * grid_info = (ML_Aggregate_Viz_Stats*) pc_ml->ml_object->Grid[0].Grid;

491:     for (level=0; level<=fine_level; level++) {
492:       VecDestroy(&pc_ml->gridctx[level].coords);
493:     }

495:     grid_info->x = 0; /* do this so ML doesn't try to free coordinates */
496:     grid_info->y = 0;
497:     grid_info->z = 0;

499:     PetscStackCall("ML_Operator_Getrow",ML_Aggregate_VizAndStats_Clean(pc_ml->ml_object));
500:   }
501:   PetscStackCall("ML_Aggregate_Destroy",ML_Aggregate_Destroy(&pc_ml->agg_object));
502:   PetscStackCall("ML_Aggregate_Destroy",ML_Destroy(&pc_ml->ml_object));

504:   if (pc_ml->PetscMLdata) {
505:     PetscFree(pc_ml->PetscMLdata->pwork);
506:     MatDestroy(&pc_ml->PetscMLdata->Aloc);
507:     VecDestroy(&pc_ml->PetscMLdata->x);
508:     VecDestroy(&pc_ml->PetscMLdata->y);
509:   }
510:   PetscFree(pc_ml->PetscMLdata);

512:   if (pc_ml->gridctx) {
513:     for (level=0; level<fine_level; level++) {
514:       if (pc_ml->gridctx[level].A) {MatDestroy(&pc_ml->gridctx[level].A);}
515:       if (pc_ml->gridctx[level].P) {MatDestroy(&pc_ml->gridctx[level].P);}
516:       if (pc_ml->gridctx[level].R) {MatDestroy(&pc_ml->gridctx[level].R);}
517:       if (pc_ml->gridctx[level].x) {VecDestroy(&pc_ml->gridctx[level].x);}
518:       if (pc_ml->gridctx[level].b) {VecDestroy(&pc_ml->gridctx[level].b);}
519:       if (pc_ml->gridctx[level+1].r) {VecDestroy(&pc_ml->gridctx[level+1].r);}
520:     }
521:   }
522:   PetscFree(pc_ml->gridctx);
523:   PetscFree(pc_ml->coords);

525:   pc_ml->dim  = 0;
526:   pc_ml->nloc = 0;
527:   PCReset_MG(pc);
528:   return(0);
529: }
530: /* -------------------------------------------------------------------------- */
531: /*
532:    PCSetUp_ML - Prepares for the use of the ML preconditioner
533:                     by setting data structures and options.

535:    Input Parameter:
536: .  pc - the preconditioner context

538:    Application Interface Routine: PCSetUp()

540:    Notes:
541:    The interface routine PCSetUp() is not usually called directly by
542:    the user, but instead is called by PCApply() if necessary.
543: */
544: extern PetscErrorCode PCSetFromOptions_MG(PetscOptionItems *PetscOptionsObject,PC);
545: extern PetscErrorCode PCReset_MG(PC);

547: PetscErrorCode PCSetUp_ML(PC pc)
548: {
549:   PetscErrorCode   ierr;
550:   PetscMPIInt      size;
551:   FineGridCtx      *PetscMLdata;
552:   ML               *ml_object;
553:   ML_Aggregate     *agg_object;
554:   ML_Operator      *mlmat;
555:   PetscInt         nlocal_allcols,Nlevels,mllevel,level,level1,m,fine_level,bs;
556:   Mat              A,Aloc;
557:   GridCtx          *gridctx;
558:   PC_MG            *mg    = (PC_MG*)pc->data;
559:   PC_ML            *pc_ml = (PC_ML*)mg->innerctx;
560:   PetscBool        isSeq, isMPI;
561:   KSP              smoother;
562:   PC               subpc;
563:   PetscInt         mesh_level, old_mesh_level;
564:   MatInfo          info;
565:   static PetscBool cite = PETSC_FALSE;

568:   PetscCitationsRegister("@TechReport{ml_users_guide,\n  author = {M. Sala and J.J. Hu and R.S. Tuminaro},\n  title = {{ML}3.1 {S}moothed {A}ggregation {U}ser's {G}uide},\n  institution =  {Sandia National Laboratories},\n  number = {SAND2004-4821},\n  year = 2004\n}\n",&cite);
569:   A    = pc->pmat;
570:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);

572:   if (pc->setupcalled) {
573:     if (pc->flag == SAME_NONZERO_PATTERN && pc_ml->reuse_interpolation) {
574:       /*
575:        Reuse interpolaton instead of recomputing aggregates and updating the whole hierarchy. This is less expensive for
576:        multiple solves in which the matrix is not changing too quickly.
577:        */
578:       ml_object             = pc_ml->ml_object;
579:       gridctx               = pc_ml->gridctx;
580:       Nlevels               = pc_ml->Nlevels;
581:       fine_level            = Nlevels - 1;
582:       gridctx[fine_level].A = A;

584:       PetscObjectTypeCompare((PetscObject) A, MATSEQAIJ, &isSeq);
585:       PetscObjectTypeCompare((PetscObject) A, MATMPIAIJ, &isMPI);
586:       if (isMPI) {
587:         MatConvert_MPIAIJ_ML(A,NULL,MAT_INITIAL_MATRIX,&Aloc);
588:       } else if (isSeq) {
589:         Aloc = A;
590:         PetscObjectReference((PetscObject)Aloc);
591:       } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "Matrix type '%s' cannot be used with ML. ML can only handle AIJ matrices.",((PetscObject)A)->type_name);

593:       MatGetSize(Aloc,&m,&nlocal_allcols);
594:       PetscMLdata       = pc_ml->PetscMLdata;
595:       MatDestroy(&PetscMLdata->Aloc);
596:       PetscMLdata->A    = A;
597:       PetscMLdata->Aloc = Aloc;
598:       PetscStackCall("ML_Aggregate_Destroy",ML_Init_Amatrix(ml_object,0,m,m,PetscMLdata));
599:       PetscStackCall("ML_Set_Amatrix_Matvec",ML_Set_Amatrix_Matvec(ml_object,0,PetscML_matvec));

601:       mesh_level = ml_object->ML_finest_level;
602:       while (ml_object->SingleLevel[mesh_level].Rmat->to) {
603:         old_mesh_level = mesh_level;
604:         mesh_level     = ml_object->SingleLevel[mesh_level].Rmat->to->levelnum;

606:         /* clean and regenerate A */
607:         mlmat = &(ml_object->Amat[mesh_level]);
608:         PetscStackCall("ML_Operator_Clean",ML_Operator_Clean(mlmat));
609:         PetscStackCall("ML_Operator_Init",ML_Operator_Init(mlmat,ml_object->comm));
610:         PetscStackCall("ML_Gen_AmatrixRAP",ML_Gen_AmatrixRAP(ml_object, old_mesh_level, mesh_level));
611:       }

613:       level = fine_level - 1;
614:       if (size == 1) { /* convert ML P, R and A into seqaij format */
615:         for (mllevel=1; mllevel<Nlevels; mllevel++) {
616:           mlmat = &(ml_object->Amat[mllevel]);
617:           MatWrapML_SeqAIJ(mlmat,MAT_REUSE_MATRIX,&gridctx[level].A);
618:           level--;
619:         }
620:       } else { /* convert ML P and R into shell format, ML A into mpiaij format */
621:         for (mllevel=1; mllevel<Nlevels; mllevel++) {
622:           mlmat  = &(ml_object->Amat[mllevel]);
623:           MatWrapML_MPIAIJ(mlmat,MAT_REUSE_MATRIX,&gridctx[level].A);
624:           level--;
625:         }
626:       }

628:       for (level=0; level<fine_level; level++) {
629:         if (level > 0) {
630:           PCMGSetResidual(pc,level,PCMGResidualDefault,gridctx[level].A);
631:         }
632:         KSPSetOperators(gridctx[level].ksp,gridctx[level].A,gridctx[level].A);
633:       }
634:       PCMGSetResidual(pc,fine_level,PCMGResidualDefault,gridctx[fine_level].A);
635:       KSPSetOperators(gridctx[fine_level].ksp,gridctx[level].A,gridctx[fine_level].A);

637:       PCSetUp_MG(pc);
638:       return(0);
639:     } else {
640:       /* since ML can change the size of vectors/matrices at any level we must destroy everything */
641:       PCReset_ML(pc);
642:     }
643:   }

645:   /* setup special features of PCML */
646:   /*--------------------------------*/
647:   /* covert A to Aloc to be used by ML at fine grid */
648:   pc_ml->size = size;
649:   PetscObjectTypeCompare((PetscObject) A, MATSEQAIJ, &isSeq);
650:   PetscObjectTypeCompare((PetscObject) A, MATMPIAIJ, &isMPI);
651:   if (isMPI) {
652:     MatConvert_MPIAIJ_ML(A,NULL,MAT_INITIAL_MATRIX,&Aloc);
653:   } else if (isSeq) {
654:     Aloc = A;
655:     PetscObjectReference((PetscObject)Aloc);
656:   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "Matrix type '%s' cannot be used with ML. ML can only handle AIJ matrices.",((PetscObject)A)->type_name);

658:   /* create and initialize struct 'PetscMLdata' */
659:   PetscNewLog(pc,&PetscMLdata);
660:   pc_ml->PetscMLdata = PetscMLdata;
661:   PetscMalloc1(Aloc->cmap->n+1,&PetscMLdata->pwork);

663:   VecCreate(PETSC_COMM_SELF,&PetscMLdata->x);
664:   VecSetSizes(PetscMLdata->x,Aloc->cmap->n,Aloc->cmap->n);
665:   VecSetType(PetscMLdata->x,VECSEQ);

667:   VecCreate(PETSC_COMM_SELF,&PetscMLdata->y);
668:   VecSetSizes(PetscMLdata->y,A->rmap->n,PETSC_DECIDE);
669:   VecSetType(PetscMLdata->y,VECSEQ);
670:   PetscMLdata->A    = A;
671:   PetscMLdata->Aloc = Aloc;
672:   if (pc_ml->dim) { /* create vecs around the coordinate data given */
673:     PetscInt  i,j,dim=pc_ml->dim;
674:     PetscInt  nloc = pc_ml->nloc,nlocghost;
675:     PetscReal *ghostedcoords;

677:     MatGetBlockSize(A,&bs);
678:     nlocghost = Aloc->cmap->n / bs;
679:     PetscMalloc1(dim*nlocghost,&ghostedcoords);
680:     for (i = 0; i < dim; i++) {
681:       /* copy coordinate values into first component of pwork */
682:       for (j = 0; j < nloc; j++) {
683:         PetscMLdata->pwork[bs * j] = pc_ml->coords[nloc * i + j];
684:       }
685:       /* get the ghost values */
686:       PetscML_comm(PetscMLdata->pwork,PetscMLdata);
687:       /* write into the vector */
688:       for (j = 0; j < nlocghost; j++) {
689:         ghostedcoords[i * nlocghost + j] = PetscMLdata->pwork[bs * j];
690:       }
691:     }
692:     /* replace the original coords with the ghosted coords, because these are
693:      * what ML needs */
694:     PetscFree(pc_ml->coords);
695:     pc_ml->coords = ghostedcoords;
696:   }

698:   /* create ML discretization matrix at fine grid */
699:   /* ML requires input of fine-grid matrix. It determines nlevels. */
700:   MatGetSize(Aloc,&m,&nlocal_allcols);
701:   MatGetBlockSize(A,&bs);
702:   PetscStackCall("ML_Create",ML_Create(&ml_object,pc_ml->MaxNlevels));
703:   PetscStackCall("ML_Comm_Set_UsrComm",ML_Comm_Set_UsrComm(ml_object->comm,PetscObjectComm((PetscObject)A)));
704:   pc_ml->ml_object = ml_object;
705:   PetscStackCall("ML_Init_Amatrix",ML_Init_Amatrix(ml_object,0,m,m,PetscMLdata));
706:   PetscStackCall("ML_Set_Amatrix_Getrow",ML_Set_Amatrix_Getrow(ml_object,0,PetscML_getrow,PetscML_comm,nlocal_allcols));
707:   PetscStackCall("ML_Set_Amatrix_Matvec",ML_Set_Amatrix_Matvec(ml_object,0,PetscML_matvec));

709:   PetscStackCall("ML_Set_Symmetrize",ML_Set_Symmetrize(ml_object,pc_ml->Symmetrize ? ML_YES : ML_NO));

711:   /* aggregation */
712:   PetscStackCall("ML_Aggregate_Create",ML_Aggregate_Create(&agg_object));
713:   pc_ml->agg_object = agg_object;

715:   {
716:     MatNullSpace mnull;
717:     MatGetNearNullSpace(A,&mnull);
718:     if (pc_ml->nulltype == PCML_NULLSPACE_AUTO) {
719:       if (mnull) pc_ml->nulltype = PCML_NULLSPACE_USER;
720:       else if (bs > 1) pc_ml->nulltype = PCML_NULLSPACE_BLOCK;
721:       else pc_ml->nulltype = PCML_NULLSPACE_SCALAR;
722:     }
723:     switch (pc_ml->nulltype) {
724:     case PCML_NULLSPACE_USER: {
725:       PetscScalar       *nullvec;
726:       const PetscScalar *v;
727:       PetscBool         has_const;
728:       PetscInt          i,j,mlocal,nvec,M;
729:       const Vec         *vecs;

731:       if (!mnull) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"Must provide explicit null space using MatSetNearNullSpace() to use user-specified null space");
732:       MatGetSize(A,&M,NULL);
733:       MatGetLocalSize(Aloc,&mlocal,NULL);
734:       MatNullSpaceGetVecs(mnull,&has_const,&nvec,&vecs);
735:       PetscMalloc1((nvec+!!has_const)*mlocal,&nullvec);
736:       if (has_const) for (i=0; i<mlocal; i++) nullvec[i] = 1.0/M;
737:       for (i=0; i<nvec; i++) {
738:         VecGetArrayRead(vecs[i],&v);
739:         for (j=0; j<mlocal; j++) nullvec[(i+!!has_const)*mlocal + j] = v[j];
740:         VecRestoreArrayRead(vecs[i],&v);
741:       }
742:       PetscStackCall("ML_Aggregate_Create",ML_Aggregate_Set_NullSpace(agg_object,bs,nvec+!!has_const,nullvec,mlocal);CHKERRQ(ierr));
743:       PetscFree(nullvec);
744:     } break;
745:     case PCML_NULLSPACE_BLOCK:
746:       PetscStackCall("ML_Aggregate_Set_NullSpace",ML_Aggregate_Set_NullSpace(agg_object,bs,bs,0,0);CHKERRQ(ierr));
747:       break;
748:     case PCML_NULLSPACE_SCALAR:
749:       break;
750:     default: SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unknown null space type");
751:     }
752:   }
753:   PetscStackCall("ML_Aggregate_Set_MaxCoarseSize",ML_Aggregate_Set_MaxCoarseSize(agg_object,pc_ml->MaxCoarseSize));
754:   /* set options */
755:   switch (pc_ml->CoarsenScheme) {
756:   case 1:
757:     PetscStackCall("ML_Aggregate_Set_CoarsenScheme_Coupled",ML_Aggregate_Set_CoarsenScheme_Coupled(agg_object));break;
758:   case 2:
759:     PetscStackCall("ML_Aggregate_Set_CoarsenScheme_MIS",ML_Aggregate_Set_CoarsenScheme_MIS(agg_object));break;
760:   case 3:
761:     PetscStackCall("ML_Aggregate_Set_CoarsenScheme_METIS",ML_Aggregate_Set_CoarsenScheme_METIS(agg_object));break;
762:   }
763:   PetscStackCall("ML_Aggregate_Set_Threshold",ML_Aggregate_Set_Threshold(agg_object,pc_ml->Threshold));
764:   PetscStackCall("ML_Aggregate_Set_DampingFactor",ML_Aggregate_Set_DampingFactor(agg_object,pc_ml->DampingFactor));
765:   if (pc_ml->SpectralNormScheme_Anorm) {
766:     PetscStackCall("ML_Set_SpectralNormScheme_Anorm",ML_Set_SpectralNormScheme_Anorm(ml_object));
767:   }
768:   agg_object->keep_agg_information      = (int)pc_ml->KeepAggInfo;
769:   agg_object->keep_P_tentative          = (int)pc_ml->Reusable;
770:   agg_object->block_scaled_SA           = (int)pc_ml->BlockScaling;
771:   agg_object->minimizing_energy         = (int)pc_ml->EnergyMinimization;
772:   agg_object->minimizing_energy_droptol = (double)pc_ml->EnergyMinimizationDropTol;
773:   agg_object->cheap_minimizing_energy   = (int)pc_ml->EnergyMinimizationCheap;

775:   if (pc_ml->Aux) {
776:     if (!pc_ml->dim) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"Auxiliary matrix requires coordinates");
777:     ml_object->Amat[0].aux_data->threshold = pc_ml->AuxThreshold;
778:     ml_object->Amat[0].aux_data->enable    = 1;
779:     ml_object->Amat[0].aux_data->max_level = 10;
780:     ml_object->Amat[0].num_PDEs            = bs;
781:   }

783:   MatGetInfo(A,MAT_LOCAL,&info);
784:   ml_object->Amat[0].N_nonzeros = (int) info.nz_used;

786:   if (pc_ml->dim) {
787:     PetscInt               i,dim = pc_ml->dim;
788:     ML_Aggregate_Viz_Stats *grid_info;
789:     PetscInt               nlocghost;

791:     MatGetBlockSize(A,&bs);
792:     nlocghost = Aloc->cmap->n / bs;

794:     PetscStackCall("ML_Aggregate_VizAndStats_Setup(",ML_Aggregate_VizAndStats_Setup(ml_object)); /* create ml info for coords */
795:     grid_info = (ML_Aggregate_Viz_Stats*) ml_object->Grid[0].Grid;
796:     for (i = 0; i < dim; i++) {
797:       /* set the finest level coordinates to point to the column-order array
798:        * in pc_ml */
799:       /* NOTE: must point away before VizAndStats_Clean so ML doesn't free */
800:       switch (i) {
801:       case 0: grid_info->x = pc_ml->coords + nlocghost * i; break;
802:       case 1: grid_info->y = pc_ml->coords + nlocghost * i; break;
803:       case 2: grid_info->z = pc_ml->coords + nlocghost * i; break;
804:       default: SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_SIZ,"PCML coordinate dimension must be <= 3");
805:       }
806:     }
807:     grid_info->Ndim = dim;
808:   }

810:   /* repartitioning */
811:   if (pc_ml->Repartition) {
812:     PetscStackCall("ML_Repartition_Activate",ML_Repartition_Activate(ml_object));
813:     PetscStackCall("ML_Repartition_Set_LargestMinMaxRatio",ML_Repartition_Set_LargestMinMaxRatio(ml_object,pc_ml->MaxMinRatio));
814:     PetscStackCall("ML_Repartition_Set_MinPerProc",ML_Repartition_Set_MinPerProc(ml_object,pc_ml->MinPerProc));
815:     PetscStackCall("ML_Repartition_Set_PutOnSingleProc",ML_Repartition_Set_PutOnSingleProc(ml_object,pc_ml->PutOnSingleProc));
816: #if 0                           /* Function not yet defined in ml-6.2 */
817:     /* I'm not sure what compatibility issues might crop up if we partitioned
818:      * on the finest level, so to be safe repartition starts on the next
819:      * finest level (reflection default behavior in
820:      * ml_MultiLevelPreconditioner) */
821:     PetscStackCall("ML_Repartition_Set_StartLevel",ML_Repartition_Set_StartLevel(ml_object,1));
822: #endif

824:     if (!pc_ml->RepartitionType) {
825:       PetscInt i;

827:       if (!pc_ml->dim) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"ML Zoltan repartitioning requires coordinates");
828:       PetscStackCall("ML_Repartition_Set_Partitioner",ML_Repartition_Set_Partitioner(ml_object,ML_USEZOLTAN));
829:       PetscStackCall("ML_Aggregate_Set_Dimensions",ML_Aggregate_Set_Dimensions(agg_object, pc_ml->dim));

831:       for (i = 0; i < ml_object->ML_num_levels; i++) {
832:         ML_Aggregate_Viz_Stats *grid_info = (ML_Aggregate_Viz_Stats*)ml_object->Grid[i].Grid;
833:         grid_info->zoltan_type = pc_ml->ZoltanScheme + 1; /* ml numbers options 1, 2, 3 */
834:         /* defaults from ml_agg_info.c */
835:         grid_info->zoltan_estimated_its = 40; /* only relevant to hypergraph / fast hypergraph */
836:         grid_info->zoltan_timers        = 0;
837:         grid_info->smoothing_steps      = 4;  /* only relevant to hypergraph / fast hypergraph */
838:       }
839:     } else {
840:       PetscStackCall("ML_Repartition_Set_Partitioner",ML_Repartition_Set_Partitioner(ml_object,ML_USEPARMETIS));
841:     }
842:   }

844:   if (pc_ml->OldHierarchy) {
845:     PetscStackCall("ML_Gen_MGHierarchy_UsingAggregation",Nlevels = ML_Gen_MGHierarchy_UsingAggregation(ml_object,0,ML_INCREASING,agg_object));
846:   } else {
847:     PetscStackCall("ML_Gen_MultiLevelHierarchy_UsingAggregation",Nlevels = ML_Gen_MultiLevelHierarchy_UsingAggregation(ml_object,0,ML_INCREASING,agg_object));
848:   }
849:   if (Nlevels<=0) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Nlevels %d must > 0",Nlevels);
850:   pc_ml->Nlevels = Nlevels;
851:   fine_level     = Nlevels - 1;

853:   PCMGSetLevels(pc,Nlevels,NULL);
854:   /* set default smoothers */
855:   for (level=1; level<=fine_level; level++) {
856:     PCMGGetSmoother(pc,level,&smoother);
857:     KSPSetType(smoother,KSPRICHARDSON);
858:     KSPGetPC(smoother,&subpc);
859:     PCSetType(subpc,PCSOR);
860:   }
861:   PetscObjectOptionsBegin((PetscObject)pc);
862:   PCSetFromOptions_MG(PetscOptionsObject,pc); /* should be called in PCSetFromOptions_ML(), but cannot be called prior to PCMGSetLevels() */
863:   PetscOptionsEnd();

865:   PetscMalloc1(Nlevels,&gridctx);

867:   pc_ml->gridctx = gridctx;

869:   /* wrap ML matrices by PETSc shell matrices at coarsened grids.
870:      Level 0 is the finest grid for ML, but coarsest for PETSc! */
871:   gridctx[fine_level].A = A;

873:   level = fine_level - 1;
874:   if (size == 1) { /* convert ML P, R and A into seqaij format */
875:     for (mllevel=1; mllevel<Nlevels; mllevel++) {
876:       mlmat = &(ml_object->Pmat[mllevel]);
877:       MatWrapML_SeqAIJ(mlmat,MAT_INITIAL_MATRIX,&gridctx[level].P);
878:       mlmat = &(ml_object->Rmat[mllevel-1]);
879:       MatWrapML_SeqAIJ(mlmat,MAT_INITIAL_MATRIX,&gridctx[level].R);

881:       mlmat = &(ml_object->Amat[mllevel]);
882:       MatWrapML_SeqAIJ(mlmat,MAT_INITIAL_MATRIX,&gridctx[level].A);
883:       level--;
884:     }
885:   } else { /* convert ML P and R into shell format, ML A into mpiaij format */
886:     for (mllevel=1; mllevel<Nlevels; mllevel++) {
887:       mlmat  = &(ml_object->Pmat[mllevel]);
888:       MatWrapML_SHELL(mlmat,MAT_INITIAL_MATRIX,&gridctx[level].P);
889:       mlmat  = &(ml_object->Rmat[mllevel-1]);
890:       MatWrapML_SHELL(mlmat,MAT_INITIAL_MATRIX,&gridctx[level].R);

892:       mlmat  = &(ml_object->Amat[mllevel]);
893:       MatWrapML_MPIAIJ(mlmat,MAT_INITIAL_MATRIX,&gridctx[level].A);
894:       level--;
895:     }
896:   }

898:   /* create vectors and ksp at all levels */
899:   for (level=0; level<fine_level; level++) {
900:     level1 = level + 1;
901:     VecCreate(((PetscObject)gridctx[level].A)->comm,&gridctx[level].x);
902:     VecSetSizes(gridctx[level].x,gridctx[level].A->cmap->n,PETSC_DECIDE);
903:     VecSetType(gridctx[level].x,VECMPI);
904:     PCMGSetX(pc,level,gridctx[level].x);

906:     VecCreate(((PetscObject)gridctx[level].A)->comm,&gridctx[level].b);
907:     VecSetSizes(gridctx[level].b,gridctx[level].A->rmap->n,PETSC_DECIDE);
908:     VecSetType(gridctx[level].b,VECMPI);
909:     PCMGSetRhs(pc,level,gridctx[level].b);

911:     VecCreate(((PetscObject)gridctx[level1].A)->comm,&gridctx[level1].r);
912:     VecSetSizes(gridctx[level1].r,gridctx[level1].A->rmap->n,PETSC_DECIDE);
913:     VecSetType(gridctx[level1].r,VECMPI);
914:     PCMGSetR(pc,level1,gridctx[level1].r);

916:     if (level == 0) {
917:       PCMGGetCoarseSolve(pc,&gridctx[level].ksp);
918:     } else {
919:       PCMGGetSmoother(pc,level,&gridctx[level].ksp);
920:     }
921:   }
922:   PCMGGetSmoother(pc,fine_level,&gridctx[fine_level].ksp);

924:   /* create coarse level and the interpolation between the levels */
925:   for (level=0; level<fine_level; level++) {
926:     level1 = level + 1;
927:     PCMGSetInterpolation(pc,level1,gridctx[level].P);
928:     PCMGSetRestriction(pc,level1,gridctx[level].R);
929:     if (level > 0) {
930:       PCMGSetResidual(pc,level,PCMGResidualDefault,gridctx[level].A);
931:     }
932:     KSPSetOperators(gridctx[level].ksp,gridctx[level].A,gridctx[level].A);
933:   }
934:   PCMGSetResidual(pc,fine_level,PCMGResidualDefault,gridctx[fine_level].A);
935:   KSPSetOperators(gridctx[fine_level].ksp,gridctx[level].A,gridctx[fine_level].A);

937:   /* put coordinate info in levels */
938:   if (pc_ml->dim) {
939:     PetscInt  i,j,dim = pc_ml->dim;
940:     PetscInt  bs, nloc;
941:     PC        subpc;
942:     PetscReal *array;

944:     level = fine_level;
945:     for (mllevel = 0; mllevel < Nlevels; mllevel++) {
946:       ML_Aggregate_Viz_Stats *grid_info = (ML_Aggregate_Viz_Stats*)ml_object->Amat[mllevel].to->Grid->Grid;
947:       MPI_Comm               comm       = ((PetscObject)gridctx[level].A)->comm;

949:       MatGetBlockSize (gridctx[level].A, &bs);
950:       MatGetLocalSize (gridctx[level].A, NULL, &nloc);
951:       nloc /= bs; /* number of local nodes */

953:       VecCreate(comm,&gridctx[level].coords);
954:       VecSetSizes(gridctx[level].coords,dim * nloc,PETSC_DECIDE);
955:       VecSetType(gridctx[level].coords,VECMPI);
956:       VecGetArray(gridctx[level].coords,&array);
957:       for (j = 0; j < nloc; j++) {
958:         for (i = 0; i < dim; i++) {
959:           switch (i) {
960:           case 0: array[dim * j + i] = grid_info->x[j]; break;
961:           case 1: array[dim * j + i] = grid_info->y[j]; break;
962:           case 2: array[dim * j + i] = grid_info->z[j]; break;
963:           default: SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_SIZ,"PCML coordinate dimension must be <= 3");
964:           }
965:         }
966:       }

968:       /* passing coordinates to smoothers/coarse solver, should they need them */
969:       KSPGetPC(gridctx[level].ksp,&subpc);
970:       PCSetCoordinates(subpc,dim,nloc,array);
971:       VecRestoreArray(gridctx[level].coords,&array);
972:       level--;
973:     }
974:   }

976:   /* setupcalled is set to 0 so that MG is setup from scratch */
977:   pc->setupcalled = 0;
978:   PCSetUp_MG(pc);
979:   return(0);
980: }

982: /* -------------------------------------------------------------------------- */
983: /*
984:    PCDestroy_ML - Destroys the private context for the ML preconditioner
985:    that was created with PCCreate_ML().

987:    Input Parameter:
988: .  pc - the preconditioner context

990:    Application Interface Routine: PCDestroy()
991: */
992: PetscErrorCode PCDestroy_ML(PC pc)
993: {
995:   PC_MG          *mg   = (PC_MG*)pc->data;
996:   PC_ML          *pc_ml= (PC_ML*)mg->innerctx;

999:   PCReset_ML(pc);
1000:   PetscFree(pc_ml);
1001:   PCDestroy_MG(pc);
1002:   PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",NULL);
1003:   return(0);
1004: }

1006: PetscErrorCode PCSetFromOptions_ML(PetscOptionItems *PetscOptionsObject,PC pc)
1007: {
1009:   PetscInt       indx,PrintLevel,partindx;
1010:   const char     *scheme[] = {"Uncoupled","Coupled","MIS","METIS"};
1011:   const char     *part[]   = {"Zoltan","ParMETIS"};
1012: #if defined(HAVE_ML_ZOLTAN)
1013:   const char *zscheme[] = {"RCB","hypergraph","fast_hypergraph"};
1014: #endif
1015:   PC_MG       *mg    = (PC_MG*)pc->data;
1016:   PC_ML       *pc_ml = (PC_ML*)mg->innerctx;
1017:   PetscMPIInt size;
1018:   MPI_Comm    comm;

1021:   PetscObjectGetComm((PetscObject)pc,&comm);
1022:   MPI_Comm_size(comm,&size);
1023:   PetscOptionsHead(PetscOptionsObject,"ML options");

1025:   PrintLevel = 0;
1026:   indx       = 0;
1027:   partindx   = 0;

1029:   PetscOptionsInt("-pc_ml_PrintLevel","Print level","ML_Set_PrintLevel",PrintLevel,&PrintLevel,NULL);
1030:   PetscStackCall("ML_Set_PrintLeve",ML_Set_PrintLevel(PrintLevel));
1031:   PetscOptionsInt("-pc_ml_maxNlevels","Maximum number of levels","None",pc_ml->MaxNlevels,&pc_ml->MaxNlevels,NULL);
1032:   PetscOptionsInt("-pc_ml_maxCoarseSize","Maximum coarsest mesh size","ML_Aggregate_Set_MaxCoarseSize",pc_ml->MaxCoarseSize,&pc_ml->MaxCoarseSize,NULL);
1033:   PetscOptionsEList("-pc_ml_CoarsenScheme","Aggregate Coarsen Scheme","ML_Aggregate_Set_CoarsenScheme_*",scheme,4,scheme[0],&indx,NULL);

1035:   pc_ml->CoarsenScheme = indx;

1037:   PetscOptionsReal("-pc_ml_DampingFactor","P damping factor","ML_Aggregate_Set_DampingFactor",pc_ml->DampingFactor,&pc_ml->DampingFactor,NULL);
1038:   PetscOptionsReal("-pc_ml_Threshold","Smoother drop tol","ML_Aggregate_Set_Threshold",pc_ml->Threshold,&pc_ml->Threshold,NULL);
1039:   PetscOptionsBool("-pc_ml_SpectralNormScheme_Anorm","Method used for estimating spectral radius","ML_Set_SpectralNormScheme_Anorm",pc_ml->SpectralNormScheme_Anorm,&pc_ml->SpectralNormScheme_Anorm,NULL);
1040:   PetscOptionsBool("-pc_ml_Symmetrize","Symmetrize aggregation","ML_Set_Symmetrize",pc_ml->Symmetrize,&pc_ml->Symmetrize,NULL);
1041:   PetscOptionsBool("-pc_ml_BlockScaling","Scale all dofs at each node together","None",pc_ml->BlockScaling,&pc_ml->BlockScaling,NULL);
1042:   PetscOptionsEnum("-pc_ml_nullspace","Which type of null space information to use","None",PCMLNullSpaceTypes,(PetscEnum)pc_ml->nulltype,(PetscEnum*)&pc_ml->nulltype,NULL);
1043:   PetscOptionsInt("-pc_ml_EnergyMinimization","Energy minimization norm type (0=no minimization; see ML manual for 1,2,3; -1 and 4 undocumented)","None",pc_ml->EnergyMinimization,&pc_ml->EnergyMinimization,NULL);
1044:   PetscOptionsBool("-pc_ml_reuse_interpolation","Reuse the interpolation operators when possible (cheaper, weaker when matrix entries change a lot)","None",pc_ml->reuse_interpolation,&pc_ml->reuse_interpolation,NULL);
1045:   /*
1046:     The following checks a number of conditions.  If we let this stuff slip by, then ML's error handling will take over.
1047:     This is suboptimal because it amounts to calling exit(1) so we check for the most common conditions.

1049:     We also try to set some sane defaults when energy minimization is activated, otherwise it's hard to find a working
1050:     combination of options and ML's exit(1) explanations don't help matters.
1051:   */
1052:   if (pc_ml->EnergyMinimization < -1 || pc_ml->EnergyMinimization > 4) SETERRQ(comm,PETSC_ERR_ARG_OUTOFRANGE,"EnergyMinimization must be in range -1..4");
1053:   if (pc_ml->EnergyMinimization == 4 && size > 1) SETERRQ(comm,PETSC_ERR_SUP,"Energy minimization type 4 does not work in parallel");
1054:   if (pc_ml->EnergyMinimization == 4) {PetscInfo(pc,"Mandel's energy minimization scheme is experimental and broken in ML-6.2\n");}
1055:   if (pc_ml->EnergyMinimization) {
1056:     PetscOptionsReal("-pc_ml_EnergyMinimizationDropTol","Energy minimization drop tolerance","None",pc_ml->EnergyMinimizationDropTol,&pc_ml->EnergyMinimizationDropTol,NULL);
1057:   }
1058:   if (pc_ml->EnergyMinimization == 2) {
1059:     /* According to ml_MultiLevelPreconditioner.cpp, this option is only meaningful for norm type (2) */
1060:     PetscOptionsBool("-pc_ml_EnergyMinimizationCheap","Use cheaper variant of norm type 2","None",pc_ml->EnergyMinimizationCheap,&pc_ml->EnergyMinimizationCheap,NULL);
1061:   }
1062:   /* energy minimization sometimes breaks if this is turned off, the more classical stuff should be okay without it */
1063:   if (pc_ml->EnergyMinimization) pc_ml->KeepAggInfo = PETSC_TRUE;
1064:   PetscOptionsBool("-pc_ml_KeepAggInfo","Allows the preconditioner to be reused, or auxilliary matrices to be generated","None",pc_ml->KeepAggInfo,&pc_ml->KeepAggInfo,NULL);
1065:   /* Option (-1) doesn't work at all (calls exit(1)) if the tentative restriction operator isn't stored. */
1066:   if (pc_ml->EnergyMinimization == -1) pc_ml->Reusable = PETSC_TRUE;
1067:   PetscOptionsBool("-pc_ml_Reusable","Store intermedaiate data structures so that the multilevel hierarchy is reusable","None",pc_ml->Reusable,&pc_ml->Reusable,NULL);
1068:   /*
1069:     ML's C API is severely underdocumented and lacks significant functionality.  The C++ API calls
1070:     ML_Gen_MultiLevelHierarchy_UsingAggregation() which is a modified copy (!?) of the documented function
1071:     ML_Gen_MGHierarchy_UsingAggregation().  This modification, however, does not provide a strict superset of the
1072:     functionality in the old function, so some users may still want to use it.  Note that many options are ignored in
1073:     this context, but ML doesn't provide a way to find out which ones.
1074:    */
1075:   PetscOptionsBool("-pc_ml_OldHierarchy","Use old routine to generate hierarchy","None",pc_ml->OldHierarchy,&pc_ml->OldHierarchy,NULL);
1076:   PetscOptionsBool("-pc_ml_repartition", "Allow ML to repartition levels of the heirarchy","ML_Repartition_Activate",pc_ml->Repartition,&pc_ml->Repartition,NULL);
1077:   if (pc_ml->Repartition) {
1078:     PetscOptionsReal("-pc_ml_repartitionMaxMinRatio", "Acceptable ratio of repartitioned sizes","ML_Repartition_Set_LargestMinMaxRatio",pc_ml->MaxMinRatio,&pc_ml->MaxMinRatio,NULL);
1079:     PetscOptionsInt("-pc_ml_repartitionMinPerProc", "Smallest repartitioned size","ML_Repartition_Set_MinPerProc",pc_ml->MinPerProc,&pc_ml->MinPerProc,NULL);
1080:     PetscOptionsInt("-pc_ml_repartitionPutOnSingleProc", "Problem size automatically repartitioned to one processor","ML_Repartition_Set_PutOnSingleProc",pc_ml->PutOnSingleProc,&pc_ml->PutOnSingleProc,NULL);
1081: #if defined(HAVE_ML_ZOLTAN)
1082:     partindx = 0;
1083:     PetscOptionsEList("-pc_ml_repartitionType", "Repartitioning library to use","ML_Repartition_Set_Partitioner",part,2,part[0],&partindx,NULL);

1085:     pc_ml->RepartitionType = partindx;
1086:     if (!partindx) {
1087:       PetscInt zindx = 0;

1089:       PetscOptionsEList("-pc_ml_repartitionZoltanScheme", "Repartitioning scheme to use","None",zscheme,3,zscheme[0],&zindx,NULL);

1091:       pc_ml->ZoltanScheme = zindx;
1092:     }
1093: #else
1094:     partindx = 1;
1095:     PetscOptionsEList("-pc_ml_repartitionType", "Repartitioning library to use","ML_Repartition_Set_Partitioner",part,2,part[1],&partindx,NULL);
1096:     pc_ml->RepartitionType = partindx;
1097:     if (!partindx) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP_SYS,"ML not compiled with Zoltan");
1098: #endif
1099:     PetscOptionsBool("-pc_ml_Aux","Aggregate using auxiliary coordinate-based laplacian","None",pc_ml->Aux,&pc_ml->Aux,NULL);
1100:     PetscOptionsReal("-pc_ml_AuxThreshold","Auxiliary smoother drop tol","None",pc_ml->AuxThreshold,&pc_ml->AuxThreshold,NULL);
1101:   }
1102:   PetscOptionsTail();
1103:   return(0);
1104: }

1106: /* -------------------------------------------------------------------------- */
1107: /*
1108:    PCCreate_ML - Creates a ML preconditioner context, PC_ML,
1109:    and sets this as the private data within the generic preconditioning
1110:    context, PC, that was created within PCCreate().

1112:    Input Parameter:
1113: .  pc - the preconditioner context

1115:    Application Interface Routine: PCCreate()
1116: */

1118: /*MC
1119:      PCML - Use algebraic multigrid preconditioning. This preconditioner requires you provide
1120:        fine grid discretization matrix. The coarser grid matrices and restriction/interpolation
1121:        operators are computed by ML, with the matrices coverted to PETSc matrices in aij format
1122:        and the restriction/interpolation operators wrapped as PETSc shell matrices.

1124:    Options Database Key:
1125:    Multigrid options(inherited):
1126: +  -pc_mg_cycles <1>: 1 for V cycle, 2 for W-cycle (MGSetCycles)
1127: .  -pc_mg_smoothup <1>: Number of post-smoothing steps (MGSetNumberSmoothUp)
1128: .  -pc_mg_smoothdown <1>: Number of pre-smoothing steps (MGSetNumberSmoothDown)
1129: -  -pc_mg_type <multiplicative>: (one of) additive multiplicative full kascade
1130:    ML options:
1131: +  -pc_ml_PrintLevel <0>: Print level (ML_Set_PrintLevel)
1132: .  -pc_ml_maxNlevels <10>: Maximum number of levels (None)
1133: .  -pc_ml_maxCoarseSize <1>: Maximum coarsest mesh size (ML_Aggregate_Set_MaxCoarseSize)
1134: .  -pc_ml_CoarsenScheme <Uncoupled>: (one of) Uncoupled Coupled MIS METIS
1135: .  -pc_ml_DampingFactor <1.33333>: P damping factor (ML_Aggregate_Set_DampingFactor)
1136: .  -pc_ml_Threshold <0>: Smoother drop tol (ML_Aggregate_Set_Threshold)
1137: .  -pc_ml_SpectralNormScheme_Anorm <false>: Method used for estimating spectral radius (ML_Set_SpectralNormScheme_Anorm)
1138: .  -pc_ml_repartition <false>: Allow ML to repartition levels of the heirarchy (ML_Repartition_Activate)
1139: .  -pc_ml_repartitionMaxMinRatio <1.3>: Acceptable ratio of repartitioned sizes (ML_Repartition_Set_LargestMinMaxRatio)
1140: .  -pc_ml_repartitionMinPerProc <512>: Smallest repartitioned size (ML_Repartition_Set_MinPerProc)
1141: .  -pc_ml_repartitionPutOnSingleProc <5000>: Problem size automatically repartitioned to one processor (ML_Repartition_Set_PutOnSingleProc)
1142: .  -pc_ml_repartitionType <Zoltan>: Repartitioning library to use (ML_Repartition_Set_Partitioner)
1143: .  -pc_ml_repartitionZoltanScheme <RCB>: Repartitioning scheme to use (None)
1144: .  -pc_ml_Aux <false>: Aggregate using auxiliary coordinate-based laplacian (None)
1145: -  -pc_ml_AuxThreshold <0.0>: Auxiliary smoother drop tol (None)

1147:    Level: intermediate

1149:   Concepts: multigrid

1151: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType,
1152:            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), MPSetCycles(), PCMGSetNumberSmoothDown(),
1153:            PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
1154:            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
1155:            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
1156: M*/

1158: PETSC_EXTERN PetscErrorCode PCCreate_ML(PC pc)
1159: {
1161:   PC_ML          *pc_ml;
1162:   PC_MG          *mg;

1165:   /* PCML is an inherited class of PCMG. Initialize pc as PCMG */
1166:   PCSetType(pc,PCMG); /* calls PCCreate_MG() and MGCreate_Private() */
1167:   PetscObjectChangeTypeName((PetscObject)pc,PCML);
1168:   /* Since PCMG tries to use DM assocated with PC must delete it */
1169:   DMDestroy(&pc->dm);
1170:   PCMGSetGalerkin(pc,PC_MG_GALERKIN_EXTERNAL);
1171:   mg   = (PC_MG*)pc->data;

1173:   /* create a supporting struct and attach it to pc */
1174:   PetscNewLog(pc,&pc_ml);
1175:   mg->innerctx = pc_ml;

1177:   pc_ml->ml_object                = 0;
1178:   pc_ml->agg_object               = 0;
1179:   pc_ml->gridctx                  = 0;
1180:   pc_ml->PetscMLdata              = 0;
1181:   pc_ml->Nlevels                  = -1;
1182:   pc_ml->MaxNlevels               = 10;
1183:   pc_ml->MaxCoarseSize            = 1;
1184:   pc_ml->CoarsenScheme            = 1;
1185:   pc_ml->Threshold                = 0.0;
1186:   pc_ml->DampingFactor            = 4.0/3.0;
1187:   pc_ml->SpectralNormScheme_Anorm = PETSC_FALSE;
1188:   pc_ml->size                     = 0;
1189:   pc_ml->dim                      = 0;
1190:   pc_ml->nloc                     = 0;
1191:   pc_ml->coords                   = 0;
1192:   pc_ml->Repartition              = PETSC_FALSE;
1193:   pc_ml->MaxMinRatio              = 1.3;
1194:   pc_ml->MinPerProc               = 512;
1195:   pc_ml->PutOnSingleProc          = 5000;
1196:   pc_ml->RepartitionType          = 0;
1197:   pc_ml->ZoltanScheme             = 0;
1198:   pc_ml->Aux                      = PETSC_FALSE;
1199:   pc_ml->AuxThreshold             = 0.0;

1201:   /* allow for coordinates to be passed */
1202:   PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",PCSetCoordinates_ML);

1204:   /* overwrite the pointers of PCMG by the functions of PCML */
1205:   pc->ops->setfromoptions = PCSetFromOptions_ML;
1206:   pc->ops->setup          = PCSetUp_ML;
1207:   pc->ops->reset          = PCReset_ML;
1208:   pc->ops->destroy        = PCDestroy_ML;
1209:   return(0);
1210: }