Actual source code: sbaijcholmod.c

petsc-master 2020-02-25
Report Typos and Errors

  2: /*
  3:    Provides an interface to the CHOLMOD sparse solver available through SuiteSparse version 4.2.1

  5:    When built with PETSC_USE_64BIT_INDICES this will use Suitesparse_long as the
  6:    integer type in UMFPACK, otherwise it will use int. This means
  7:    all integers in this file as simply declared as PetscInt. Also it means
  8:    that one cannot use 64BIT_INDICES on 32bit machines [as Suitesparse_long is 32bit only]

 10: */

 12:  #include <../src/mat/impls/sbaij/seq/sbaij.h>
 13:  #include <../src/mat/impls/sbaij/seq/cholmod/cholmodimpl.h>

 15: /*
 16:    This is a terrible hack, but it allows the error handler to retain a context.
 17:    Note that this hack really cannot be made both reentrant and concurrent.
 18: */
 19: static Mat static_F;

 21: static void CholmodErrorHandler(int status,const char *file,int line,const char *message)
 22: {

 26:   if (status > CHOLMOD_OK) {
 27:     PetscInfo4(static_F,"CHOLMOD warning %d at %s:%d: %s\n",status,file,line,message);CHKERRV(ierr);
 28:   } else if (status == CHOLMOD_OK) { /* Documentation says this can happen, but why? */
 29:     PetscInfo3(static_F,"CHOLMOD OK at %s:%d: %s\n",file,line,message);CHKERRV(ierr);
 30:   } else {
 31:     PetscErrorPrintf("CHOLMOD error %d at %s:%d: %s\n",status,file,line,message);CHKERRV(ierr);
 32:   }
 33:   PetscFunctionReturnVoid();
 34: }

 36: PetscErrorCode  CholmodStart(Mat F)
 37: {
 39:   Mat_CHOLMOD    *chol=(Mat_CHOLMOD*)F->data;
 40:   cholmod_common *c;
 41:   PetscBool      flg;

 44:   if (chol->common) return(0);
 45:   PetscMalloc1(1,&chol->common);
 46:   !cholmod_X_start(chol->common);

 48:   c                = chol->common;
 49:   c->error_handler = CholmodErrorHandler;

 51: #define CHOLMOD_OPTION_DOUBLE(name,help) do {                            \
 52:     PetscReal tmp = (PetscReal)c->name;                                  \
 53:     PetscOptionsReal("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL); \
 54:     c->name = (double)tmp;                                               \
 55: } while (0)

 57: #define CHOLMOD_OPTION_INT(name,help) do {                               \
 58:     PetscInt tmp = (PetscInt)c->name;                                    \
 59:     PetscOptionsInt("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL); \
 60:     c->name = (int)tmp;                                                  \
 61: } while (0)

 63: #define CHOLMOD_OPTION_SIZE_T(name,help) do {                            \
 64:     PetscReal tmp = (PetscInt)c->name;                                   \
 65:     PetscOptionsReal("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL); \
 66:     if (tmp < 0) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_OUTOFRANGE,"value must be positive"); \
 67:     c->name = (size_t)tmp;                                               \
 68: } while (0)

 70: #define CHOLMOD_OPTION_BOOL(name,help) do {                             \
 71:     PetscBool tmp = (PetscBool) !!c->name;                              \
 72:     PetscOptionsBool("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL); \
 73:     c->name = (int)tmp;                                                  \
 74: } while (0)

 76:   PetscOptionsBegin(PetscObjectComm((PetscObject)F),((PetscObject)F)->prefix,"CHOLMOD Options","Mat");
 77:   CHOLMOD_OPTION_INT(nmethods,"Number of different ordering methods to try");

 79: #if defined(PETSC_USE_SUITESPARSE_GPU)
 80:   c->useGPU = 1;
 81:   CHOLMOD_OPTION_INT(useGPU,"Use GPU for BLAS 1, otherwise 0");
 82:   CHOLMOD_OPTION_SIZE_T(maxGpuMemBytes,"Maximum memory to allocate on the GPU");
 83:   CHOLMOD_OPTION_DOUBLE(maxGpuMemFraction,"Fraction of available GPU memory to allocate");
 84: #endif

 86:   /* CHOLMOD handles first-time packing and refactor-packing separately, but we usually want them to be the same. */
 87:   chol->pack = (PetscBool)c->final_pack;
 88:   PetscOptionsBool("-mat_cholmod_pack","Pack factors after factorization [disable for frequent repeat factorization]","None",chol->pack,&chol->pack,NULL);
 89:   c->final_pack = (int)chol->pack;

 91:   CHOLMOD_OPTION_DOUBLE(dbound,"Minimum absolute value of diagonal entries of D");
 92:   CHOLMOD_OPTION_DOUBLE(grow0,"Global growth ratio when factors are modified");
 93:   CHOLMOD_OPTION_DOUBLE(grow1,"Column growth ratio when factors are modified");
 94:   CHOLMOD_OPTION_SIZE_T(grow2,"Affine column growth constant when factors are modified");
 95:   CHOLMOD_OPTION_SIZE_T(maxrank,"Max rank of update, larger values are faster but use more memory [2,4,8]");
 96:   {
 97:     static const char *const list[] = {"SIMPLICIAL","AUTO","SUPERNODAL","MatCholmodFactorType","MAT_CHOLMOD_FACTOR_",0};
 98:     PetscOptionsEnum("-mat_cholmod_factor","Factorization method","None",list,(PetscEnum)c->supernodal,(PetscEnum*)&c->supernodal,NULL);
 99:   }
100:   if (c->supernodal) CHOLMOD_OPTION_DOUBLE(supernodal_switch,"flop/nnz_L threshold for switching to supernodal factorization");
101:   CHOLMOD_OPTION_BOOL(final_asis,"Leave factors \"as is\"");
102:   CHOLMOD_OPTION_BOOL(final_pack,"Pack the columns when finished (use FALSE if the factors will be updated later)");
103:   if (!c->final_asis) {
104:     CHOLMOD_OPTION_BOOL(final_super,"Leave supernodal factors instead of converting to simplicial");
105:     CHOLMOD_OPTION_BOOL(final_ll,"Turn LDL' factorization into LL'");
106:     CHOLMOD_OPTION_BOOL(final_monotonic,"Ensure columns are monotonic when done");
107:     CHOLMOD_OPTION_BOOL(final_resymbol,"Remove numerically zero values resulting from relaxed supernodal amalgamation");
108:   }
109:   {
110:     PetscReal tmp[] = {(PetscReal)c->zrelax[0],(PetscReal)c->zrelax[1],(PetscReal)c->zrelax[2]};
111:     PetscInt  n     = 3;
112:     PetscOptionsRealArray("-mat_cholmod_zrelax","3 real supernodal relaxed amalgamation parameters","None",tmp,&n,&flg);
113:     if (flg && n != 3) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_OUTOFRANGE,"must provide exactly 3 parameters to -mat_cholmod_zrelax");
114:     if (flg) while (n--) c->zrelax[n] = (double)tmp[n];
115:   }
116:   {
117:     PetscInt n,tmp[] = {(PetscInt)c->nrelax[0],(PetscInt)c->nrelax[1],(PetscInt)c->nrelax[2]};
118:     PetscOptionsIntArray("-mat_cholmod_nrelax","3 size_t supernodal relaxed amalgamation parameters","None",tmp,&n,&flg);
119:     if (flg && n != 3) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_OUTOFRANGE,"must provide exactly 3 parameters to -mat_cholmod_nrelax");
120:     if (flg) while (n--) c->nrelax[n] = (size_t)tmp[n];
121:   }
122:   CHOLMOD_OPTION_BOOL(prefer_upper,"Work with upper triangular form [faster when using fill-reducing ordering, slower in natural ordering]");
123:   CHOLMOD_OPTION_BOOL(default_nesdis,"Use NESDIS instead of METIS for nested dissection");
124:   CHOLMOD_OPTION_INT(print,"Verbosity level");
125:   PetscOptionsEnd();
126:   return(0);
127: }

129: static PetscErrorCode MatWrapCholmod_seqsbaij(Mat A,PetscBool values,cholmod_sparse *C,PetscBool *aijalloc,PetscBool *valloc)
130: {
131:   Mat_SeqSBAIJ   *sbaij = (Mat_SeqSBAIJ*)A->data;
132:   PetscBool      vallocin = PETSC_FALSE;

136:   PetscMemzero(C,sizeof(*C));
137:   /* CHOLMOD uses column alignment, SBAIJ stores the upper factor, so we pass it on as a lower factor, swapping the meaning of row and column */
138:   C->nrow   = (size_t)A->cmap->n;
139:   C->ncol   = (size_t)A->rmap->n;
140:   C->nzmax  = (size_t)sbaij->maxnz;
141:   C->p      = sbaij->i;
142:   C->i      = sbaij->j;
143:   if (values) {
144: #if defined(PETSC_USE_COMPLEX)
145:     /* we need to pass CHOLMOD the conjugate matrix */
146:     PetscScalar *v;
147:     PetscInt    i;

149:     PetscMalloc1(sbaij->maxnz,&v);
150:     for (i = 0; i < sbaij->maxnz; i++) v[i] = PetscConj(sbaij->a[i]);
151:     C->x = v;
152:     vallocin = PETSC_TRUE;
153: #else
154:     C->x = sbaij->a;
155: #endif
156:   }
157:   C->stype  = -1;
158:   C->itype  = CHOLMOD_INT_TYPE;
159:   C->xtype  = values ? CHOLMOD_SCALAR_TYPE : CHOLMOD_PATTERN;
160:   C->dtype  = CHOLMOD_DOUBLE;
161:   C->sorted = 1;
162:   C->packed = 1;
163:   *aijalloc = PETSC_FALSE;
164:   *valloc   = vallocin;
165:   return(0);
166: }

168: #define GET_ARRAY_READ 0
169: #define GET_ARRAY_WRITE 1

171: static PetscErrorCode VecWrapCholmod(Vec X,PetscInt rw,cholmod_dense *Y)
172: {
174:   PetscScalar    *x;
175:   PetscInt       n;

178:   PetscMemzero(Y,sizeof(*Y));
179:   switch (rw) {
180:   case GET_ARRAY_READ:
181:     VecGetArrayRead(X,(const PetscScalar**)&x);
182:     break;
183:   case GET_ARRAY_WRITE:
184:     VecGetArrayWrite(X,&x);
185:     break;
186:   default:
187:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Case %D not handled",rw);
188:     break;
189:   }
190:   VecGetSize(X,&n);

192:   Y->x     = x;
193:   Y->nrow  = n;
194:   Y->ncol  = 1;
195:   Y->nzmax = n;
196:   Y->d     = n;
197:   Y->xtype = CHOLMOD_SCALAR_TYPE;
198:   Y->dtype = CHOLMOD_DOUBLE;
199:   return(0);
200: }

202: static PetscErrorCode VecUnWrapCholmod(Vec X,PetscInt rw,cholmod_dense *Y)
203: {
204:   PetscErrorCode    ierr;

207:   switch (rw) {
208:   case GET_ARRAY_READ:
209:     VecRestoreArrayRead(X,(const PetscScalar**)&Y->x);
210:     break;
211:   case GET_ARRAY_WRITE:
212:     VecRestoreArrayWrite(X,(PetscScalar**)&Y->x);
213:     break;
214:   default:
215:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Case %D not handled",rw);
216:     break;
217:   }
218:   return(0);
219: }

221: static PetscErrorCode MatDenseWrapCholmod(Mat X,PetscInt rw,cholmod_dense *Y)
222: {
224:   PetscScalar    *x;
225:   PetscInt       m,n,lda;

228:   PetscMemzero(Y,sizeof(*Y));
229:   switch (rw) {
230:   case GET_ARRAY_READ:
231:     MatDenseGetArrayRead(X,(const PetscScalar**)&x);
232:     break;
233:   case GET_ARRAY_WRITE:
234:     /* we don't have MatDenseGetArrayWrite */
235:     MatDenseGetArray(X,&x);
236:     break;
237:   default:
238:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Case %D not handled",rw);
239:     break;
240:   }
241:   MatDenseGetLDA(X,&lda);
242:   MatGetLocalSize(X,&m,&n);

244:   Y->x     = x;
245:   Y->nrow  = m;
246:   Y->ncol  = n;
247:   Y->nzmax = lda*n;
248:   Y->d     = lda;
249:   Y->xtype = CHOLMOD_SCALAR_TYPE;
250:   Y->dtype = CHOLMOD_DOUBLE;
251:   return(0);
252: }

254: static PetscErrorCode MatDenseUnWrapCholmod(Mat X,PetscInt rw,cholmod_dense *Y)
255: {
256:   PetscErrorCode    ierr;

259:   switch (rw) {
260:   case GET_ARRAY_READ:
261:     MatDenseRestoreArrayRead(X,(const PetscScalar**)&Y->x);
262:     break;
263:   case GET_ARRAY_WRITE:
264:     /* we don't have MatDenseRestoreArrayWrite */
265:     MatDenseRestoreArray(X,(PetscScalar**)&Y->x);
266:     break;
267:   default:
268:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Case %D not handled",rw);
269:     break;
270:   }
271:   return(0);
272: }

274: PETSC_INTERN PetscErrorCode  MatDestroy_CHOLMOD(Mat F)
275: {
277:   Mat_CHOLMOD    *chol=(Mat_CHOLMOD*)F->data;

280:   !cholmod_X_free_factor(&chol->factor,chol->common);
281:   !cholmod_X_finish(chol->common);
282:   PetscFree(chol->common);
283:   PetscFree(chol->matrix);
284:   PetscObjectComposeFunction((PetscObject)F,"MatFactorGetSolverType_C",NULL);
285:   PetscFree(F->data);
286:   return(0);
287: }

289: static PetscErrorCode MatSolve_CHOLMOD(Mat,Vec,Vec);
290: static PetscErrorCode MatMatSolve_CHOLMOD(Mat,Mat,Mat);

292: /*static const char *const CholmodOrderingMethods[] = {"User","AMD","METIS","NESDIS(default)","Natural","NESDIS(small=20000)","NESDIS(small=4,no constrained)","NESDIS()"};*/

294: static PetscErrorCode MatView_Info_CHOLMOD(Mat F,PetscViewer viewer)
295: {
296:   Mat_CHOLMOD          *chol = (Mat_CHOLMOD*)F->data;
297:   const cholmod_common *c    = chol->common;
298:   PetscErrorCode       ierr;
299:   PetscInt             i;

302:   if (F->ops->solve != MatSolve_CHOLMOD) return(0);
303:   PetscViewerASCIIPrintf(viewer,"CHOLMOD run parameters:\n");
304:   PetscViewerASCIIPushTab(viewer);
305:   PetscViewerASCIIPrintf(viewer,"Pack factors after symbolic factorization: %s\n",chol->pack ? "TRUE" : "FALSE");
306:   PetscViewerASCIIPrintf(viewer,"Common.dbound            %g  (Smallest absolute value of diagonal entries of D)\n",c->dbound);
307:   PetscViewerASCIIPrintf(viewer,"Common.grow0             %g\n",c->grow0);
308:   PetscViewerASCIIPrintf(viewer,"Common.grow1             %g\n",c->grow1);
309:   PetscViewerASCIIPrintf(viewer,"Common.grow2             %u\n",(unsigned)c->grow2);
310:   PetscViewerASCIIPrintf(viewer,"Common.maxrank           %u\n",(unsigned)c->maxrank);
311:   PetscViewerASCIIPrintf(viewer,"Common.supernodal_switch %g\n",c->supernodal_switch);
312:   PetscViewerASCIIPrintf(viewer,"Common.supernodal        %d\n",c->supernodal);
313:   PetscViewerASCIIPrintf(viewer,"Common.final_asis        %d\n",c->final_asis);
314:   PetscViewerASCIIPrintf(viewer,"Common.final_super       %d\n",c->final_super);
315:   PetscViewerASCIIPrintf(viewer,"Common.final_ll          %d\n",c->final_ll);
316:   PetscViewerASCIIPrintf(viewer,"Common.final_pack        %d\n",c->final_pack);
317:   PetscViewerASCIIPrintf(viewer,"Common.final_monotonic   %d\n",c->final_monotonic);
318:   PetscViewerASCIIPrintf(viewer,"Common.final_resymbol    %d\n",c->final_resymbol);
319:   PetscViewerASCIIPrintf(viewer,"Common.zrelax            [%g,%g,%g]\n",c->zrelax[0],c->zrelax[1],c->zrelax[2]);
320:   PetscViewerASCIIPrintf(viewer,"Common.nrelax            [%u,%u,%u]\n",(unsigned)c->nrelax[0],(unsigned)c->nrelax[1],(unsigned)c->nrelax[2]);
321:   PetscViewerASCIIPrintf(viewer,"Common.prefer_upper      %d\n",c->prefer_upper);
322:   PetscViewerASCIIPrintf(viewer,"Common.print             %d\n",c->print);
323:   for (i=0; i<c->nmethods; i++) {
324:     PetscViewerASCIIPrintf(viewer,"Ordering method %D%s:\n",i,i==c->selected ? " [SELECTED]" : "");
325:     PetscViewerASCIIPrintf(viewer,"  lnz %g, fl %g, prune_dense %g, prune_dense2 %g\n",
326:                                   c->method[i].lnz,c->method[i].fl,c->method[i].prune_dense,c->method[i].prune_dense2);
327:   }
328:   PetscViewerASCIIPrintf(viewer,"Common.postorder         %d\n",c->postorder);
329:   PetscViewerASCIIPrintf(viewer,"Common.default_nesdis    %d (use NESDIS instead of METIS for nested dissection)\n",c->default_nesdis);
330:   /* Statistics */
331:   PetscViewerASCIIPrintf(viewer,"Common.fl                %g (flop count from most recent analysis)\n",c->fl);
332:   PetscViewerASCIIPrintf(viewer,"Common.lnz               %g (fundamental nz in L)\n",c->lnz);
333:   PetscViewerASCIIPrintf(viewer,"Common.anz               %g\n",c->anz);
334:   PetscViewerASCIIPrintf(viewer,"Common.modfl             %g (flop count from most recent update)\n",c->modfl);
335:   PetscViewerASCIIPrintf(viewer,"Common.malloc_count      %g (number of live objects)\n",(double)c->malloc_count);
336:   PetscViewerASCIIPrintf(viewer,"Common.memory_usage      %g (peak memory usage in bytes)\n",(double)c->memory_usage);
337:   PetscViewerASCIIPrintf(viewer,"Common.memory_inuse      %g (current memory usage in bytes)\n",(double)c->memory_inuse);
338:   PetscViewerASCIIPrintf(viewer,"Common.nrealloc_col      %g (number of column reallocations)\n",c->nrealloc_col);
339:   PetscViewerASCIIPrintf(viewer,"Common.nrealloc_factor   %g (number of factor reallocations due to column reallocations)\n",c->nrealloc_factor);
340:   PetscViewerASCIIPrintf(viewer,"Common.ndbounds_hit      %g (number of times diagonal was modified by dbound)\n",c->ndbounds_hit);
341:   PetscViewerASCIIPrintf(viewer,"Common.rowfacfl          %g (number of flops in last call to cholmod_rowfac)\n",c->rowfacfl);
342:   PetscViewerASCIIPrintf(viewer,"Common.aatfl             %g (number of flops to compute A(:,f)*A(:,f)')\n",c->aatfl);
343: #if defined(PETSC_USE_SUITESPARSE_GPU)
344:   PetscViewerASCIIPrintf(viewer,"Common.useGPU            %d\n",c->useGPU);
345: #endif
346:   PetscViewerASCIIPopTab(viewer);
347:   return(0);
348: }

350: PETSC_INTERN PetscErrorCode  MatView_CHOLMOD(Mat F,PetscViewer viewer)
351: {
352:   PetscErrorCode    ierr;
353:   PetscBool         iascii;
354:   PetscViewerFormat format;

357:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
358:   if (iascii) {
359:     PetscViewerGetFormat(viewer,&format);
360:     if (format == PETSC_VIEWER_ASCII_INFO) {
361:       MatView_Info_CHOLMOD(F,viewer);
362:     }
363:   }
364:   return(0);
365: }

367: static PetscErrorCode MatSolve_CHOLMOD(Mat F,Vec B,Vec X)
368: {
369:   Mat_CHOLMOD    *chol = (Mat_CHOLMOD*)F->data;
370:   cholmod_dense  cholB,cholX,*X_handle,*Y_handle = NULL,*E_handle = NULL;

374:   static_F = F;
375:   VecWrapCholmod(B,GET_ARRAY_READ,&cholB);
376:   VecWrapCholmod(X,GET_ARRAY_WRITE,&cholX);
377:   X_handle = &cholX;
378:   !cholmod_X_solve2(CHOLMOD_A,chol->factor,&cholB,NULL,&X_handle,NULL,&Y_handle,&E_handle,chol->common);
379:   !cholmod_X_free_dense(&Y_handle,chol->common);
380:   !cholmod_X_free_dense(&E_handle,chol->common);
381:   VecUnWrapCholmod(B,GET_ARRAY_READ,&cholB);
382:   VecUnWrapCholmod(X,GET_ARRAY_WRITE,&cholX);
383:   return(0);
384: }

386: static PetscErrorCode MatMatSolve_CHOLMOD(Mat F,Mat B,Mat X)
387: {
388:   Mat_CHOLMOD    *chol = (Mat_CHOLMOD*)F->data;
389:   cholmod_dense  cholB,cholX,*X_handle,*Y_handle = NULL,*E_handle = NULL;

393:   static_F = F;
394:   MatDenseWrapCholmod(B,GET_ARRAY_READ,&cholB);
395:   MatDenseWrapCholmod(X,GET_ARRAY_WRITE,&cholX);
396:   X_handle = &cholX;
397:   !cholmod_X_solve2(CHOLMOD_A,chol->factor,&cholB,NULL,&X_handle,NULL,&Y_handle,&E_handle,chol->common);
398:   !cholmod_X_free_dense(&Y_handle,chol->common);
399:   !cholmod_X_free_dense(&E_handle,chol->common);
400:   MatDenseUnWrapCholmod(B,GET_ARRAY_READ,&cholB);
401:   MatDenseUnWrapCholmod(X,GET_ARRAY_WRITE,&cholX);
402:   return(0);
403: }

405: static PetscErrorCode MatCholeskyFactorNumeric_CHOLMOD(Mat F,Mat A,const MatFactorInfo *info)
406: {
407:   Mat_CHOLMOD    *chol = (Mat_CHOLMOD*)F->data;
408:   cholmod_sparse cholA;
409:   PetscBool      aijalloc,valloc;

413:   (*chol->Wrap)(A,PETSC_TRUE,&cholA,&aijalloc,&valloc);
414:   static_F = F;
415:   !cholmod_X_factorize(&cholA,chol->factor,chol->common);
416:   if (ierr) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD factorization failed with status %d",chol->common->status);
417:   if (chol->common->status == CHOLMOD_NOT_POSDEF) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_MAT_CH_ZRPVT,"CHOLMOD detected that the matrix is not positive definite, failure at column %u",(unsigned)chol->factor->minor);

419:   if (aijalloc) {PetscFree2(cholA.p,cholA.i);}
420:   if (valloc) {PetscFree(cholA.x);}
421: #if defined(PETSC_USE_SUITESPARSE_GPU)
422:   PetscLogGpuTimeAdd(chol->common->CHOLMOD_GPU_GEMM_TIME + chol->common->CHOLMOD_GPU_SYRK_TIME + chol->common->CHOLMOD_GPU_TRSM_TIME + chol->common->CHOLMOD_GPU_POTRF_TIME);
423: #endif

425:   F->ops->solve             = MatSolve_CHOLMOD;
426:   F->ops->solvetranspose    = MatSolve_CHOLMOD;
427:   F->ops->matsolve          = MatMatSolve_CHOLMOD;
428:   F->ops->matsolvetranspose = MatMatSolve_CHOLMOD;
429:   return(0);
430: }

432: PETSC_INTERN PetscErrorCode  MatCholeskyFactorSymbolic_CHOLMOD(Mat F,Mat A,IS perm,const MatFactorInfo *info)
433: {
434:   Mat_CHOLMOD    *chol = (Mat_CHOLMOD*)F->data;
436:   cholmod_sparse cholA;
437:   PetscBool      aijalloc,valloc;
438:   PetscInt       *fset = 0;
439:   size_t         fsize = 0;

442:   (*chol->Wrap)(A,PETSC_FALSE,&cholA,&aijalloc,&valloc);
443:   static_F = F;
444:   if (chol->factor) {
445:     !cholmod_X_resymbol(&cholA,fset,fsize,(int)chol->pack,chol->factor,chol->common);
446:     if (ierr) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD analysis failed with status %d",chol->common->status);
447:   } else if (perm) {
448:     const PetscInt *ip;
449:     ISGetIndices(perm,&ip);
450:     chol->factor = cholmod_X_analyze_p(&cholA,(PetscInt*)ip,fset,fsize,chol->common);
451:     if (!chol->factor) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD analysis failed with status %d",chol->common->status);
452:     ISRestoreIndices(perm,&ip);
453:   } else {
454:     chol->factor = cholmod_X_analyze(&cholA,chol->common);
455:     if (!chol->factor) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD analysis failed with status %d",chol->common->status);
456:   }

458:   if (aijalloc) {PetscFree2(cholA.p,cholA.i);}
459:   if (valloc) {PetscFree(cholA.x);}

461:   F->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_CHOLMOD;
462:   return(0);
463: }

465: static PetscErrorCode MatFactorGetSolverType_seqsbaij_cholmod(Mat A,MatSolverType *type)
466: {
468:   *type = MATSOLVERCHOLMOD;
469:   return(0);
470: }

472: PETSC_INTERN PetscErrorCode MatGetInfo_CHOLMOD(Mat F,MatInfoType flag,MatInfo *info)
473: {
474:   Mat_CHOLMOD *chol = (Mat_CHOLMOD*)F->data;

477:   info->block_size        = 1.0;
478:   info->nz_allocated      = chol->common->lnz;
479:   info->nz_used           = chol->common->lnz;
480:   info->nz_unneeded       = 0.0;
481:   info->assemblies        = 0.0;
482:   info->mallocs           = 0.0;
483:   info->memory            = chol->common->memory_inuse;
484:   info->fill_ratio_given  = 0;
485:   info->fill_ratio_needed = 0;
486:   info->factor_mallocs    = chol->common->malloc_count;
487:   return(0);
488: }

490: /*MC
491:   MATSOLVERCHOLMOD = "cholmod" - A matrix type providing direct solvers (Cholesky) for sequential matrices
492:   via the external package CHOLMOD.

494:   Use ./configure --download-suitesparse to install PETSc to use CHOLMOD

496:   Use -pc_type cholesky -pc_factor_mat_solver_type cholmod to use this direct solver

498:   Consult CHOLMOD documentation for more information about the Common parameters
499:   which correspond to the options database keys below.

501:   Options Database Keys:
502: + -mat_cholmod_dbound <0>          - Minimum absolute value of diagonal entries of D (None)
503: . -mat_cholmod_grow0 <1.2>         - Global growth ratio when factors are modified (None)
504: . -mat_cholmod_grow1 <1.2>         - Column growth ratio when factors are modified (None)
505: . -mat_cholmod_grow2 <5>           - Affine column growth constant when factors are modified (None)
506: . -mat_cholmod_maxrank <8>         - Max rank of update, larger values are faster but use more memory [2,4,8] (None)
507: . -mat_cholmod_factor <AUTO>       - (choose one of) SIMPLICIAL AUTO SUPERNODAL
508: . -mat_cholmod_supernodal_switch <40> - flop/nnz_L threshold for switching to supernodal factorization (None)
509: . -mat_cholmod_final_asis <TRUE>   - Leave factors "as is" (None)
510: . -mat_cholmod_final_pack <TRUE>   - Pack the columns when finished (use FALSE if the factors will be updated later) (None)
511: . -mat_cholmod_zrelax <0.8>        - 3 real supernodal relaxed amalgamation parameters (None)
512: . -mat_cholmod_nrelax <4>          - 3 size_t supernodal relaxed amalgamation parameters (None)
513: . -mat_cholmod_prefer_upper <TRUE> - Work with upper triangular form (faster when using fill-reducing ordering, slower in natural ordering) (None)
514: - -mat_cholmod_print <3>           - Verbosity level (None)

516:    Level: beginner

518:    Note: CHOLMOD is part of SuiteSparse http://faculty.cse.tamu.edu/davis/suitesparse.html

520: .seealso: PCCHOLESKY, PCFactorSetMatSolverType(), MatSolverType
521: M*/

523: PETSC_INTERN PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat A,MatFactorType ftype,Mat *F)
524: {
525:   Mat            B;
526:   Mat_CHOLMOD    *chol;
528:   PetscInt       m=A->rmap->n,n=A->cmap->n,bs;
529:   const char     *prefix;

532:   MatGetBlockSize(A,&bs);
533:   if (bs != 1) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"CHOLMOD only supports block size=1, given %D",bs);
534: #if defined(PETSC_USE_COMPLEX)
535:   if (!A->hermitian) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Only for hermitian matrices");
536: #endif
537:   /* Create the factorization matrix F */
538:   MatCreate(PetscObjectComm((PetscObject)A),&B);
539:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,m,n);
540:   PetscStrallocpy("cholmod",&((PetscObject)B)->type_name);
541:   MatGetOptionsPrefix(A,&prefix);
542:   MatSetOptionsPrefix(B,prefix);
543:   MatSetUp(B);
544:   PetscNewLog(B,&chol);

546:   chol->Wrap    = MatWrapCholmod_seqsbaij;
547:   B->data       = chol;

549:   B->ops->getinfo                = MatGetInfo_CHOLMOD;
550:   B->ops->view                   = MatView_CHOLMOD;
551:   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_CHOLMOD;
552:   B->ops->destroy                = MatDestroy_CHOLMOD;
553:   PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_seqsbaij_cholmod);
554:   B->factortype                  = MAT_FACTOR_CHOLESKY;
555:   B->assembled                   = PETSC_TRUE;
556:   B->preallocated                = PETSC_TRUE;

558:   CholmodStart(B);

560:   PetscFree(B->solvertype);
561:   PetscStrallocpy(MATSOLVERCHOLMOD,&B->solvertype);

563:   *F   = B;
564:   return(0);
565: }