xref: /petsc/src/mat/impls/sbaij/seq/cholmod/sbaijcholmod.c (revision eb9872f6c2c8c7fb7c9ab41097db5b45d6de5e6c)
1641875f9SMatthew G Knepley 
2641875f9SMatthew G Knepley /*
3d515b9b4SShri Abhyankar    Provides an interface to the CHOLMOD sparse solver available through SuiteSparse version 4.2.1
4641875f9SMatthew G Knepley 
59e475b0dSSatish Balay    When build with PETSC_USE_64BIT_INDICES this will use Suitesparse_long as the
6641875f9SMatthew G Knepley    integer type in UMFPACK, otherwise it will use int. This means
7641875f9SMatthew G Knepley    all integers in this file as simply declared as PetscInt. Also it means
89e475b0dSSatish Balay    that one cannot use 64BIT_INDICES on 32bit machines [as Suitesparse_long is 32bit only]
9641875f9SMatthew G Knepley 
10641875f9SMatthew G Knepley */
11641875f9SMatthew G Knepley 
12c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/sbaij.h>
13c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/cholmod/cholmodimpl.h>
14641875f9SMatthew G Knepley 
15641875f9SMatthew G Knepley /*
16641875f9SMatthew G Knepley    This is a terrible hack, but it allows the error handler to retain a context.
17641875f9SMatthew G Knepley    Note that this hack really cannot be made both reentrant and concurrent.
18641875f9SMatthew G Knepley */
19641875f9SMatthew G Knepley static Mat static_F;
20641875f9SMatthew G Knepley 
21641875f9SMatthew G Knepley #undef __FUNCT__
22641875f9SMatthew G Knepley #define __FUNCT__ "CholmodErrorHandler"
23641875f9SMatthew G Knepley static void CholmodErrorHandler(int status,const char *file,int line,const char *message)
24641875f9SMatthew G Knepley {
25ec55ff42SBarry Smith   PetscErrorCode ierr;
26641875f9SMatthew G Knepley 
27641875f9SMatthew G Knepley   PetscFunctionBegin;
28641875f9SMatthew G Knepley   if (status > CHOLMOD_OK) {
29e49b6e0cSMatthew G. Knepley     ierr = PetscInfo4(static_F,"CHOLMOD warning %d at %s:%d: %s\n",status,file,line,message);CHKERRV(ierr);
30641875f9SMatthew G Knepley   } else if (status == CHOLMOD_OK) { /* Documentation says this can happen, but why? */
31e49b6e0cSMatthew G. Knepley     ierr = PetscInfo3(static_F,"CHOLMOD OK at %s:%d: %s\n",file,line,message);CHKERRV(ierr);
32641875f9SMatthew G Knepley   } else {
33e49b6e0cSMatthew G. Knepley     ierr = PetscErrorPrintf("CHOLMOD error %d at %s:%d: %s\n",status,file,line,message);CHKERRV(ierr);
34641875f9SMatthew G Knepley   }
35641875f9SMatthew G Knepley   PetscFunctionReturnVoid();
36641875f9SMatthew G Knepley }
37641875f9SMatthew G Knepley 
38641875f9SMatthew G Knepley #undef __FUNCT__
39641875f9SMatthew G Knepley #define __FUNCT__ "CholmodStart"
407087cfbeSBarry Smith PetscErrorCode  CholmodStart(Mat F)
41641875f9SMatthew G Knepley {
42641875f9SMatthew G Knepley   PetscErrorCode ierr;
43641875f9SMatthew G Knepley   Mat_CHOLMOD    *chol=(Mat_CHOLMOD*)F->spptr;
44641875f9SMatthew G Knepley   cholmod_common *c;
45ace3abfcSBarry Smith   PetscBool      flg;
46641875f9SMatthew G Knepley 
47641875f9SMatthew G Knepley   PetscFunctionBegin;
48641875f9SMatthew G Knepley   if (chol->common) PetscFunctionReturn(0);
49854ce69bSBarry Smith   ierr = PetscMalloc1(1,&chol->common);CHKERRQ(ierr);
50641875f9SMatthew G Knepley   ierr = !cholmod_X_start(chol->common);CHKERRQ(ierr);
5126fbe8dcSKarl Rupp 
52641875f9SMatthew G Knepley   c                = chol->common;
53641875f9SMatthew G Knepley   c->error_handler = CholmodErrorHandler;
54641875f9SMatthew G Knepley 
55641875f9SMatthew G Knepley #define CHOLMOD_OPTION_DOUBLE(name,help) do {                            \
56641875f9SMatthew G Knepley     PetscReal tmp = (PetscReal)c->name;                                  \
578afaa268SBarry Smith     ierr    = PetscOptionsReal("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL);CHKERRQ(ierr); \
58641875f9SMatthew G Knepley     c->name = (double)tmp;                                               \
59641875f9SMatthew G Knepley } while (0)
6026fbe8dcSKarl Rupp 
61641875f9SMatthew G Knepley #define CHOLMOD_OPTION_INT(name,help) do {                               \
62641875f9SMatthew G Knepley     PetscInt tmp = (PetscInt)c->name;                                    \
638afaa268SBarry Smith     ierr    = PetscOptionsInt("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL);CHKERRQ(ierr); \
64641875f9SMatthew G Knepley     c->name = (int)tmp;                                                  \
65641875f9SMatthew G Knepley } while (0)
6626fbe8dcSKarl Rupp 
67641875f9SMatthew G Knepley #define CHOLMOD_OPTION_SIZE_T(name,help) do {                            \
68641875f9SMatthew G Knepley     PetscInt tmp = (PetscInt)c->name;                                    \
698afaa268SBarry Smith     ierr = PetscOptionsInt("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL);CHKERRQ(ierr); \
70ce94432eSBarry Smith     if (tmp < 0) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_OUTOFRANGE,"value must be positive"); \
71641875f9SMatthew G Knepley     c->name = (size_t)tmp;                                               \
72641875f9SMatthew G Knepley } while (0)
7326fbe8dcSKarl Rupp 
74b9eaa5e8SBarry Smith #define CHOLMOD_OPTION_BOOL(name,help) do {                             \
75ace3abfcSBarry Smith     PetscBool tmp = (PetscBool) !!c->name;                              \
768afaa268SBarry Smith     ierr    = PetscOptionsBool("-mat_cholmod_" #name,help,"None",tmp,&tmp,NULL);CHKERRQ(ierr); \
77641875f9SMatthew G Knepley     c->name = (int)tmp;                                                  \
78641875f9SMatthew G Knepley } while (0)
79641875f9SMatthew G Knepley 
80ce94432eSBarry Smith   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)F),((PetscObject)F)->prefix,"CHOLMOD Options","Mat");CHKERRQ(ierr);
81641875f9SMatthew G Knepley   /* CHOLMOD handles first-time packing and refactor-packing separately, but we usually want them to be the same. */
82ace3abfcSBarry Smith   chol->pack = (PetscBool)c->final_pack;
8326fbe8dcSKarl Rupp 
84b9eaa5e8SBarry Smith #if defined(PETSC_USE_SUITESPARSE_GPU)
85b9eaa5e8SBarry Smith   c->useGPU = 1;
86b9eaa5e8SBarry Smith   CHOLMOD_OPTION_INT(useGPU,"Use GPU for BLAS 1, otherwise 0");
87b9eaa5e8SBarry Smith #endif
88b9eaa5e8SBarry Smith 
898afaa268SBarry Smith   ierr = PetscOptionsBool("-mat_cholmod_pack","Pack factors after factorization [disable for frequent repeat factorization]","None",chol->pack,&chol->pack,NULL);CHKERRQ(ierr);
90641875f9SMatthew G Knepley   c->final_pack = (int)chol->pack;
91641875f9SMatthew G Knepley 
92641875f9SMatthew G Knepley   CHOLMOD_OPTION_DOUBLE(dbound,"Minimum absolute value of diagonal entries of D");
93641875f9SMatthew G Knepley   CHOLMOD_OPTION_DOUBLE(grow0,"Global growth ratio when factors are modified");
94641875f9SMatthew G Knepley   CHOLMOD_OPTION_DOUBLE(grow1,"Column growth ratio when factors are modified");
95641875f9SMatthew G Knepley   CHOLMOD_OPTION_SIZE_T(grow2,"Affine column growth constant when factors are modified");
96641875f9SMatthew G Knepley   CHOLMOD_OPTION_SIZE_T(maxrank,"Max rank of update, larger values are faster but use more memory [2,4,8]");
97641875f9SMatthew G Knepley   {
98641875f9SMatthew G Knepley     static const char *const list[] = {"SIMPLICIAL","AUTO","SUPERNODAL","MatCholmodFactorType","MAT_CHOLMOD_FACTOR_",0};
998afaa268SBarry Smith     ierr = PetscOptionsEnum("-mat_cholmod_factor","Factorization method","None",list,(PetscEnum)c->supernodal,(PetscEnum*)&c->supernodal,NULL);CHKERRQ(ierr);
100641875f9SMatthew G Knepley   }
101641875f9SMatthew G Knepley   if (c->supernodal) CHOLMOD_OPTION_DOUBLE(supernodal_switch,"flop/nnz_L threshold for switching to supernodal factorization");
102b9eaa5e8SBarry Smith   CHOLMOD_OPTION_BOOL(final_asis,"Leave factors \"as is\"");
103b9eaa5e8SBarry Smith   CHOLMOD_OPTION_BOOL(final_pack,"Pack the columns when finished (use FALSE if the factors will be updated later)");
104641875f9SMatthew G Knepley   if (!c->final_asis) {
105b9eaa5e8SBarry Smith     CHOLMOD_OPTION_BOOL(final_super,"Leave supernodal factors instead of converting to simplicial");
106b9eaa5e8SBarry Smith     CHOLMOD_OPTION_BOOL(final_ll,"Turn LDL' factorization into LL'");
107b9eaa5e8SBarry Smith     CHOLMOD_OPTION_BOOL(final_monotonic,"Ensure columns are monotonic when done");
108b9eaa5e8SBarry Smith     CHOLMOD_OPTION_BOOL(final_resymbol,"Remove numerically zero values resulting from relaxed supernodal amalgamation");
109641875f9SMatthew G Knepley   }
110641875f9SMatthew G Knepley   {
111641875f9SMatthew G Knepley     PetscReal tmp[] = {(PetscReal)c->zrelax[0],(PetscReal)c->zrelax[1],(PetscReal)c->zrelax[2]};
112641875f9SMatthew G Knepley     PetscInt  n     = 3;
113641875f9SMatthew G Knepley     ierr = PetscOptionsRealArray("-mat_cholmod_zrelax","3 real supernodal relaxed amalgamation parameters","None",tmp,&n,&flg);CHKERRQ(ierr);
114ce94432eSBarry Smith     if (flg && n != 3) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_OUTOFRANGE,"must provide exactly 3 parameters to -mat_cholmod_zrelax");
115641875f9SMatthew G Knepley     if (flg) while (n--) c->zrelax[n] = (double)tmp[n];
116641875f9SMatthew G Knepley   }
117641875f9SMatthew G Knepley   {
118641875f9SMatthew G Knepley     PetscInt n,tmp[] = {(PetscInt)c->nrelax[0],(PetscInt)c->nrelax[1],(PetscInt)c->nrelax[2]};
119641875f9SMatthew G Knepley     ierr = PetscOptionsIntArray("-mat_cholmod_nrelax","3 size_t supernodal relaxed amalgamation parameters","None",tmp,&n,&flg);CHKERRQ(ierr);
120ce94432eSBarry Smith     if (flg && n != 3) SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_ARG_OUTOFRANGE,"must provide exactly 3 parameters to -mat_cholmod_nrelax");
121641875f9SMatthew G Knepley     if (flg) while (n--) c->nrelax[n] = (size_t)tmp[n];
122641875f9SMatthew G Knepley   }
123b9eaa5e8SBarry Smith   CHOLMOD_OPTION_BOOL(prefer_upper,"Work with upper triangular form [faster when using fill-reducing ordering, slower in natural ordering]");
124b9eaa5e8SBarry Smith   CHOLMOD_OPTION_BOOL(default_nesdis,"Use NESDIS instead of METIS for nested dissection");
125641875f9SMatthew G Knepley   CHOLMOD_OPTION_INT(print,"Verbosity level");
126641875f9SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
127641875f9SMatthew G Knepley   PetscFunctionReturn(0);
128641875f9SMatthew G Knepley }
129641875f9SMatthew G Knepley 
130641875f9SMatthew G Knepley #undef __FUNCT__
131641875f9SMatthew G Knepley #define __FUNCT__ "MatWrapCholmod_seqsbaij"
132ace3abfcSBarry Smith static PetscErrorCode MatWrapCholmod_seqsbaij(Mat A,PetscBool values,cholmod_sparse *C,PetscBool  *aijalloc)
133641875f9SMatthew G Knepley {
134641875f9SMatthew G Knepley   Mat_SeqSBAIJ   *sbaij = (Mat_SeqSBAIJ*)A->data;
135641875f9SMatthew G Knepley   PetscErrorCode ierr;
136641875f9SMatthew G Knepley 
137641875f9SMatthew G Knepley   PetscFunctionBegin;
138641875f9SMatthew G Knepley   ierr = PetscMemzero(C,sizeof(*C));CHKERRQ(ierr);
139641875f9SMatthew G Knepley   /* 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 */
140641875f9SMatthew G Knepley   C->nrow   = (size_t)A->cmap->n;
141641875f9SMatthew G Knepley   C->ncol   = (size_t)A->rmap->n;
142641875f9SMatthew G Knepley   C->nzmax  = (size_t)sbaij->maxnz;
143641875f9SMatthew G Knepley   C->p      = sbaij->i;
144641875f9SMatthew G Knepley   C->i      = sbaij->j;
145641875f9SMatthew G Knepley   C->x      = sbaij->a;
146641875f9SMatthew G Knepley   C->stype  = -1;
147641875f9SMatthew G Knepley   C->itype  = CHOLMOD_INT_TYPE;
148641875f9SMatthew G Knepley   C->xtype  = CHOLMOD_SCALAR_TYPE;
149641875f9SMatthew G Knepley   C->dtype  = CHOLMOD_DOUBLE;
150641875f9SMatthew G Knepley   C->sorted = 1;
151641875f9SMatthew G Knepley   C->packed = 1;
152641875f9SMatthew G Knepley   *aijalloc = PETSC_FALSE;
153641875f9SMatthew G Knepley   PetscFunctionReturn(0);
154641875f9SMatthew G Knepley }
155641875f9SMatthew G Knepley 
156641875f9SMatthew G Knepley #undef __FUNCT__
157d9ca1df4SBarry Smith #define __FUNCT__ "VecWrapCholmodRead"
158d9ca1df4SBarry Smith static PetscErrorCode VecWrapCholmodRead(Vec X,cholmod_dense *Y)
159641875f9SMatthew G Knepley {
160641875f9SMatthew G Knepley   PetscErrorCode    ierr;
161d9ca1df4SBarry Smith   const PetscScalar *x;
162641875f9SMatthew G Knepley   PetscInt          n;
163641875f9SMatthew G Knepley 
164641875f9SMatthew G Knepley   PetscFunctionBegin;
165641875f9SMatthew G Knepley   ierr = PetscMemzero(Y,sizeof(*Y));CHKERRQ(ierr);
166d9ca1df4SBarry Smith   ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
167641875f9SMatthew G Knepley   ierr = VecGetSize(X,&n);CHKERRQ(ierr);
16826fbe8dcSKarl Rupp 
169641875f9SMatthew G Knepley   Y->x     = (double*)x;
170641875f9SMatthew G Knepley   Y->nrow  = n;
171641875f9SMatthew G Knepley   Y->ncol  = 1;
172641875f9SMatthew G Knepley   Y->nzmax = n;
173641875f9SMatthew G Knepley   Y->d     = n;
174641875f9SMatthew G Knepley   Y->x     = (double*)x;
175641875f9SMatthew G Knepley   Y->xtype = CHOLMOD_SCALAR_TYPE;
176641875f9SMatthew G Knepley   Y->dtype = CHOLMOD_DOUBLE;
177641875f9SMatthew G Knepley   PetscFunctionReturn(0);
178641875f9SMatthew G Knepley }
179641875f9SMatthew G Knepley 
180641875f9SMatthew G Knepley #undef __FUNCT__
181d9ca1df4SBarry Smith #define __FUNCT__ "VecUnWrapCholmodRead"
182d9ca1df4SBarry Smith static PetscErrorCode VecUnWrapCholmodRead(Vec X,cholmod_dense *Y)
183d9ca1df4SBarry Smith {
184d9ca1df4SBarry Smith   PetscErrorCode    ierr;
185d9ca1df4SBarry Smith 
186d9ca1df4SBarry Smith   PetscFunctionBegin;
187d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(X,PETSC_NULL);CHKERRQ(ierr);
188d9ca1df4SBarry Smith   PetscFunctionReturn(0);
189d9ca1df4SBarry Smith }
190d9ca1df4SBarry Smith 
191d9ca1df4SBarry Smith #undef __FUNCT__
192641875f9SMatthew G Knepley #define __FUNCT__ "MatDestroy_CHOLMOD"
193*eb9872f6SBarry Smith PETSC_INTERN PetscErrorCode  MatDestroy_CHOLMOD(Mat F)
194641875f9SMatthew G Knepley {
195641875f9SMatthew G Knepley   PetscErrorCode ierr;
196641875f9SMatthew G Knepley   Mat_CHOLMOD    *chol=(Mat_CHOLMOD*)F->spptr;
197641875f9SMatthew G Knepley 
198641875f9SMatthew G Knepley   PetscFunctionBegin;
199bf0cc555SLisandro Dalcin   if (chol) {
200641875f9SMatthew G Knepley     ierr = !cholmod_X_free_factor(&chol->factor,chol->common);CHKERRQ(ierr);
201641875f9SMatthew G Knepley     ierr = !cholmod_X_finish(chol->common);CHKERRQ(ierr);
202641875f9SMatthew G Knepley     ierr = PetscFree(chol->common);CHKERRQ(ierr);
203641875f9SMatthew G Knepley     ierr = PetscFree(chol->matrix);CHKERRQ(ierr);
204641875f9SMatthew G Knepley     ierr = (*chol->Destroy)(F);CHKERRQ(ierr);
205bf0cc555SLisandro Dalcin   }
206bf0cc555SLisandro Dalcin   ierr = PetscFree(F->spptr);CHKERRQ(ierr);
207641875f9SMatthew G Knepley   PetscFunctionReturn(0);
208641875f9SMatthew G Knepley }
209641875f9SMatthew G Knepley 
210641875f9SMatthew G Knepley static PetscErrorCode MatSolve_CHOLMOD(Mat,Vec,Vec);
211641875f9SMatthew G Knepley 
212fcd503bcSBarry Smith /*static const char *const CholmodOrderingMethods[] = {"User","AMD","METIS","NESDIS(default)","Natural","NESDIS(small=20000)","NESDIS(small=4,no constrained)","NESDIS()"};*/
213641875f9SMatthew G Knepley 
214641875f9SMatthew G Knepley #undef __FUNCT__
215641875f9SMatthew G Knepley #define __FUNCT__ "MatFactorInfo_CHOLMOD"
216641875f9SMatthew G Knepley static PetscErrorCode MatFactorInfo_CHOLMOD(Mat F,PetscViewer viewer)
217641875f9SMatthew G Knepley {
218641875f9SMatthew G Knepley   Mat_CHOLMOD          *chol = (Mat_CHOLMOD*)F->spptr;
219641875f9SMatthew G Knepley   const cholmod_common *c    = chol->common;
220641875f9SMatthew G Knepley   PetscErrorCode       ierr;
221641875f9SMatthew G Knepley   PetscInt             i;
222641875f9SMatthew G Knepley 
223641875f9SMatthew G Knepley   PetscFunctionBegin;
224641875f9SMatthew G Knepley   if (F->ops->solve != MatSolve_CHOLMOD) PetscFunctionReturn(0);
225641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"CHOLMOD run parameters:\n");CHKERRQ(ierr);
226641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
227641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Pack factors after symbolic factorization: %s\n",chol->pack ? "TRUE" : "FALSE");CHKERRQ(ierr);
228641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.dbound            %g  (Smallest absolute value of diagonal entries of D)\n",c->dbound);CHKERRQ(ierr);
229641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.grow0             %g\n",c->grow0);CHKERRQ(ierr);
230641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.grow1             %g\n",c->grow1);CHKERRQ(ierr);
231641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.grow2             %u\n",(unsigned)c->grow2);CHKERRQ(ierr);
232641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.maxrank           %u\n",(unsigned)c->maxrank);CHKERRQ(ierr);
233641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.supernodal_switch %g\n",c->supernodal_switch);CHKERRQ(ierr);
234641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.supernodal        %d\n",c->supernodal);CHKERRQ(ierr);
235641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.final_asis        %d\n",c->final_asis);CHKERRQ(ierr);
236641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.final_super       %d\n",c->final_super);CHKERRQ(ierr);
237641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.final_ll          %d\n",c->final_ll);CHKERRQ(ierr);
238641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.final_pack        %d\n",c->final_pack);CHKERRQ(ierr);
239641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.final_monotonic   %d\n",c->final_monotonic);CHKERRQ(ierr);
240641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.final_resymbol    %d\n",c->final_resymbol);CHKERRQ(ierr);
241641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.zrelax            [%g,%g,%g]\n",c->zrelax[0],c->zrelax[1],c->zrelax[2]);CHKERRQ(ierr);
242641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.nrelax            [%u,%u,%u]\n",(unsigned)c->nrelax[0],(unsigned)c->nrelax[1],(unsigned)c->nrelax[2]);CHKERRQ(ierr);
243641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.prefer_upper      %d\n",c->prefer_upper);CHKERRQ(ierr);
244641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.print             %d\n",c->print);CHKERRQ(ierr);
245641875f9SMatthew G Knepley   for (i=0; i<c->nmethods; i++) {
246641875f9SMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer,"Ordering method %D%s:\n",i,i==c->selected ? " [SELECTED]" : "");CHKERRQ(ierr);
247641875f9SMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer,"  lnz %g, fl %g, prune_dense %g, prune_dense2 %g\n",
248641875f9SMatthew G Knepley                                   c->method[i].lnz,c->method[i].fl,c->method[i].prune_dense,c->method[i].prune_dense2);CHKERRQ(ierr);
249641875f9SMatthew G Knepley   }
250641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.postorder         %d\n",c->postorder);CHKERRQ(ierr);
251641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.default_nesdis    %d (use NESDIS instead of METIS for nested dissection)\n",c->default_nesdis);CHKERRQ(ierr);
252641875f9SMatthew G Knepley   /* Statistics */
253641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.fl                %g (flop count from most recent analysis)\n",c->fl);CHKERRQ(ierr);
254641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.lnz               %g (fundamental nz in L)\n",c->lnz);CHKERRQ(ierr);
255641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.anz               %g\n",c->anz);CHKERRQ(ierr);
256641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.modfl             %g (flop count from most recent update)\n",c->modfl);CHKERRQ(ierr);
257641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.malloc_count      %g (number of live objects)\n",(double)c->malloc_count);CHKERRQ(ierr);CHKERRQ(ierr);
258641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.memory_usage      %g (peak memory usage in bytes)\n",(double)c->memory_usage);CHKERRQ(ierr);CHKERRQ(ierr);
259641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.memory_inuse      %g (current memory usage in bytes)\n",(double)c->memory_inuse);CHKERRQ(ierr);CHKERRQ(ierr);
260641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.nrealloc_col      %g (number of column reallocations)\n",c->nrealloc_col);CHKERRQ(ierr);CHKERRQ(ierr);
261641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.nrealloc_factor   %g (number of factor reallocations due to column reallocations)\n",c->nrealloc_factor);CHKERRQ(ierr);CHKERRQ(ierr);
262641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.ndbounds_hit      %g (number of times diagonal was modified by dbound)\n",c->ndbounds_hit);CHKERRQ(ierr);CHKERRQ(ierr);
263641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.rowfacfl          %g (number of flops in last call to cholmod_rowfac)\n",c->rowfacfl);CHKERRQ(ierr);CHKERRQ(ierr);
264641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer,"Common.aatfl             %g (number of flops to compute A(:,f)*A(:,f)')\n",c->aatfl);CHKERRQ(ierr);CHKERRQ(ierr);
265b9eaa5e8SBarry Smith #if defined(PETSC_USE_SUITESPARSE_GPU)
266b9eaa5e8SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"Common.useGPU            %d\n",c->useGPU);CHKERRQ(ierr);CHKERRQ(ierr);
267b9eaa5e8SBarry Smith #endif
268641875f9SMatthew G Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
269641875f9SMatthew G Knepley   PetscFunctionReturn(0);
270641875f9SMatthew G Knepley }
271641875f9SMatthew G Knepley 
272641875f9SMatthew G Knepley #undef __FUNCT__
273641875f9SMatthew G Knepley #define __FUNCT__ "MatView_CHOLMOD"
274*eb9872f6SBarry Smith PETSC_INTERN PetscErrorCode  MatView_CHOLMOD(Mat F,PetscViewer viewer)
275641875f9SMatthew G Knepley {
276641875f9SMatthew G Knepley   PetscErrorCode    ierr;
277ace3abfcSBarry Smith   PetscBool         iascii;
278641875f9SMatthew G Knepley   PetscViewerFormat format;
279641875f9SMatthew G Knepley 
280641875f9SMatthew G Knepley   PetscFunctionBegin;
281641875f9SMatthew G Knepley   ierr = MatView_SeqSBAIJ(F,viewer);CHKERRQ(ierr);
282251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
283641875f9SMatthew G Knepley   if (iascii) {
284641875f9SMatthew G Knepley     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
285641875f9SMatthew G Knepley     if (format == PETSC_VIEWER_ASCII_INFO) {
286641875f9SMatthew G Knepley       ierr = MatFactorInfo_CHOLMOD(F,viewer);CHKERRQ(ierr);
287641875f9SMatthew G Knepley     }
288641875f9SMatthew G Knepley   }
289641875f9SMatthew G Knepley   PetscFunctionReturn(0);
290641875f9SMatthew G Knepley }
291641875f9SMatthew G Knepley 
292641875f9SMatthew G Knepley #undef __FUNCT__
293641875f9SMatthew G Knepley #define __FUNCT__ "MatSolve_CHOLMOD"
294641875f9SMatthew G Knepley static PetscErrorCode MatSolve_CHOLMOD(Mat F,Vec B,Vec X)
295641875f9SMatthew G Knepley {
296641875f9SMatthew G Knepley   Mat_CHOLMOD    *chol = (Mat_CHOLMOD*)F->spptr;
297641875f9SMatthew G Knepley   cholmod_dense  cholB,*cholX;
298641875f9SMatthew G Knepley   PetscScalar    *x;
299641875f9SMatthew G Knepley   PetscErrorCode ierr;
300641875f9SMatthew G Knepley 
301641875f9SMatthew G Knepley   PetscFunctionBegin;
302d9ca1df4SBarry Smith   ierr     = VecWrapCholmodRead(B,&cholB);CHKERRQ(ierr);
303641875f9SMatthew G Knepley   static_F = F;
304641875f9SMatthew G Knepley   cholX    = cholmod_X_solve(CHOLMOD_A,chol->factor,&cholB,chol->common);
305641875f9SMatthew G Knepley   if (!cholX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CHOLMOD failed");
306d9ca1df4SBarry Smith   ierr = VecUnWrapCholmodRead(B,&cholB);CHKERRQ(ierr);
307641875f9SMatthew G Knepley   ierr = VecGetArray(X,&x);CHKERRQ(ierr);
308641875f9SMatthew G Knepley   ierr = PetscMemcpy(x,cholX->x,cholX->nrow*sizeof(*x));CHKERRQ(ierr);
309641875f9SMatthew G Knepley   ierr = !cholmod_X_free_dense(&cholX,chol->common);CHKERRQ(ierr);
310641875f9SMatthew G Knepley   ierr = VecRestoreArray(X,&x);CHKERRQ(ierr);
311641875f9SMatthew G Knepley   PetscFunctionReturn(0);
312641875f9SMatthew G Knepley }
313641875f9SMatthew G Knepley 
314641875f9SMatthew G Knepley #undef __FUNCT__
315641875f9SMatthew G Knepley #define __FUNCT__ "MatCholeskyFactorNumeric_CHOLMOD"
316641875f9SMatthew G Knepley static PetscErrorCode MatCholeskyFactorNumeric_CHOLMOD(Mat F,Mat A,const MatFactorInfo *info)
317641875f9SMatthew G Knepley {
318641875f9SMatthew G Knepley   Mat_CHOLMOD    *chol = (Mat_CHOLMOD*)F->spptr;
319641875f9SMatthew G Knepley   cholmod_sparse cholA;
320ace3abfcSBarry Smith   PetscBool      aijalloc;
321641875f9SMatthew G Knepley   PetscErrorCode ierr;
322641875f9SMatthew G Knepley 
323641875f9SMatthew G Knepley   PetscFunctionBegin;
324641875f9SMatthew G Knepley   ierr     = (*chol->Wrap)(A,PETSC_TRUE,&cholA,&aijalloc);CHKERRQ(ierr);
325641875f9SMatthew G Knepley   static_F = F;
326641875f9SMatthew G Knepley   ierr     = !cholmod_X_factorize(&cholA,chol->factor,chol->common);
327ce94432eSBarry Smith   if (ierr) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD factorization failed with status %d",chol->common->status);
328ce94432eSBarry Smith   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);
329641875f9SMatthew G Knepley 
330641875f9SMatthew G Knepley   if (aijalloc) {ierr = PetscFree3(cholA.p,cholA.i,cholA.x);CHKERRQ(ierr);}
331641875f9SMatthew G Knepley 
332641875f9SMatthew G Knepley   F->ops->solve          = MatSolve_CHOLMOD;
333641875f9SMatthew G Knepley   F->ops->solvetranspose = MatSolve_CHOLMOD;
334641875f9SMatthew G Knepley   PetscFunctionReturn(0);
335641875f9SMatthew G Knepley }
336641875f9SMatthew G Knepley 
337641875f9SMatthew G Knepley #undef __FUNCT__
338641875f9SMatthew G Knepley #define __FUNCT__ "MatCholeskyFactorSymbolic_CHOLMOD"
339*eb9872f6SBarry Smith PETSC_INTERN PetscErrorCode  MatCholeskyFactorSymbolic_CHOLMOD(Mat F,Mat A,IS perm,const MatFactorInfo *info)
340641875f9SMatthew G Knepley {
341641875f9SMatthew G Knepley   Mat_CHOLMOD    *chol = (Mat_CHOLMOD*)F->spptr;
342641875f9SMatthew G Knepley   PetscErrorCode ierr;
343641875f9SMatthew G Knepley   cholmod_sparse cholA;
344ace3abfcSBarry Smith   PetscBool      aijalloc;
345641875f9SMatthew G Knepley   PetscInt       *fset = 0;
346641875f9SMatthew G Knepley   size_t         fsize = 0;
347641875f9SMatthew G Knepley 
348641875f9SMatthew G Knepley   PetscFunctionBegin;
349641875f9SMatthew G Knepley   ierr     = (*chol->Wrap)(A,PETSC_FALSE,&cholA,&aijalloc);CHKERRQ(ierr);
350641875f9SMatthew G Knepley   static_F = F;
351641875f9SMatthew G Knepley   if (chol->factor) {
352641875f9SMatthew G Knepley     ierr = !cholmod_X_resymbol(&cholA,fset,fsize,(int)chol->pack,chol->factor,chol->common);
353ce94432eSBarry Smith     if (ierr) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD analysis failed with status %d",chol->common->status);
354641875f9SMatthew G Knepley   } else if (perm) {
355641875f9SMatthew G Knepley     const PetscInt *ip;
356641875f9SMatthew G Knepley     ierr         = ISGetIndices(perm,&ip);CHKERRQ(ierr);
357641875f9SMatthew G Knepley     chol->factor = cholmod_X_analyze_p(&cholA,(PetscInt*)ip,fset,fsize,chol->common);
358ce94432eSBarry Smith     if (!chol->factor) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD analysis failed with status %d",chol->common->status);
359641875f9SMatthew G Knepley     ierr = ISRestoreIndices(perm,&ip);CHKERRQ(ierr);
360641875f9SMatthew G Knepley   } else {
361641875f9SMatthew G Knepley     chol->factor = cholmod_X_analyze(&cholA,chol->common);
362ce94432eSBarry Smith     if (!chol->factor) SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_LIB,"CHOLMOD analysis failed with status %d",chol->common->status);
363641875f9SMatthew G Knepley   }
364641875f9SMatthew G Knepley 
365641875f9SMatthew G Knepley   if (aijalloc) {ierr = PetscFree3(cholA.p,cholA.i,cholA.x);CHKERRQ(ierr);}
366641875f9SMatthew G Knepley 
367641875f9SMatthew G Knepley   F->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_CHOLMOD;
368641875f9SMatthew G Knepley   PetscFunctionReturn(0);
369641875f9SMatthew G Knepley }
370641875f9SMatthew G Knepley 
371641875f9SMatthew G Knepley #undef __FUNCT__
372641875f9SMatthew G Knepley #define __FUNCT__ "MatFactorGetSolverPackage_seqsbaij_cholmod"
373db87b0f2SBarry Smith static PetscErrorCode MatFactorGetSolverPackage_seqsbaij_cholmod(Mat A,const MatSolverPackage *type)
374641875f9SMatthew G Knepley {
375641875f9SMatthew G Knepley   PetscFunctionBegin;
376641875f9SMatthew G Knepley   *type = MATSOLVERCHOLMOD;
377641875f9SMatthew G Knepley   PetscFunctionReturn(0);
378641875f9SMatthew G Knepley }
379641875f9SMatthew G Knepley 
380641875f9SMatthew G Knepley /*MC
381641875f9SMatthew G Knepley   MATSOLVERCHOLMOD = "cholmod" - A matrix type providing direct solvers (Cholesky) for sequential matrices
382641875f9SMatthew G Knepley   via the external package CHOLMOD.
383641875f9SMatthew G Knepley 
384c2b89b5dSBarry Smith   Use ./configure --download-suitesparse to install PETSc to use CHOLMOD
385c2b89b5dSBarry Smith 
386c2b89b5dSBarry Smith   Use -pc_type lu -pc_factor_mat_solver_package cholmod to use this direct solver
387641875f9SMatthew G Knepley 
388641875f9SMatthew G Knepley   Consult CHOLMOD documentation for more information about the Common parameters
389641875f9SMatthew G Knepley   which correspond to the options database keys below.
390641875f9SMatthew G Knepley 
391641875f9SMatthew G Knepley   Options Database Keys:
392e08999f5SMatthew G Knepley + -mat_cholmod_dbound <0>          - Minimum absolute value of diagonal entries of D (None)
393e08999f5SMatthew G Knepley . -mat_cholmod_grow0 <1.2>         - Global growth ratio when factors are modified (None)
394e08999f5SMatthew G Knepley . -mat_cholmod_grow1 <1.2>         - Column growth ratio when factors are modified (None)
395e08999f5SMatthew G Knepley . -mat_cholmod_grow2 <5>           - Affine column growth constant when factors are modified (None)
396e08999f5SMatthew G Knepley . -mat_cholmod_maxrank <8>         - Max rank of update, larger values are faster but use more memory [2,4,8] (None)
397e08999f5SMatthew G Knepley . -mat_cholmod_factor <AUTO>       - (choose one of) SIMPLICIAL AUTO SUPERNODAL
398e08999f5SMatthew G Knepley . -mat_cholmod_supernodal_switch <40> - flop/nnz_L threshold for switching to supernodal factorization (None)
399e08999f5SMatthew G Knepley . -mat_cholmod_final_asis <TRUE>   - Leave factors "as is" (None)
400e08999f5SMatthew G Knepley . -mat_cholmod_final_pack <TRUE>   - Pack the columns when finished (use FALSE if the factors will be updated later) (None)
401e08999f5SMatthew G Knepley . -mat_cholmod_zrelax <0.8>        - 3 real supernodal relaxed amalgamation parameters (None)
402e08999f5SMatthew G Knepley . -mat_cholmod_nrelax <4>          - 3 size_t supernodal relaxed amalgamation parameters (None)
403e08999f5SMatthew G Knepley . -mat_cholmod_prefer_upper <TRUE> - Work with upper triangular form (faster when using fill-reducing ordering, slower in natural ordering) (None)
404e08999f5SMatthew G Knepley - -mat_cholmod_print <3>           - Verbosity level (None)
405641875f9SMatthew G Knepley 
406641875f9SMatthew G Knepley    Level: beginner
407641875f9SMatthew G Knepley 
408a364b7d2SBarry Smith    Note: CHOLMOD is part of SuiteSparse http://faculty.cse.tamu.edu/davis/suitesparse.html
409a364b7d2SBarry Smith 
410641875f9SMatthew G Knepley .seealso: PCCHOLESKY, PCFactorSetMatSolverPackage(), MatSolverPackage
411641875f9SMatthew G Knepley M*/
412b2573a8aSBarry Smith 
413641875f9SMatthew G Knepley #undef __FUNCT__
414641875f9SMatthew G Knepley #define __FUNCT__ "MatGetFactor_seqsbaij_cholmod"
415db87b0f2SBarry Smith PETSC_INTERN PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat A,MatFactorType ftype,Mat *F)
416641875f9SMatthew G Knepley {
417641875f9SMatthew G Knepley   Mat            B;
418641875f9SMatthew G Knepley   Mat_CHOLMOD    *chol;
419641875f9SMatthew G Knepley   PetscErrorCode ierr;
420641875f9SMatthew G Knepley   PetscInt       m=A->rmap->n,n=A->cmap->n,bs;
421641875f9SMatthew G Knepley 
422641875f9SMatthew G Knepley   PetscFunctionBegin;
423641875f9SMatthew G Knepley   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"CHOLMOD cannot do %s factorization with SBAIJ, only %s",
424641875f9SMatthew G Knepley                                              MatFactorTypes[ftype],MatFactorTypes[MAT_FACTOR_CHOLESKY]);
425641875f9SMatthew G Knepley   ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr);
426ce94432eSBarry Smith   if (bs != 1) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"CHOLMOD only supports block size=1, given %D",bs);
427641875f9SMatthew G Knepley   /* Create the factorization matrix F */
428ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
429641875f9SMatthew G Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,m,n);CHKERRQ(ierr);
430641875f9SMatthew G Knepley   ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4310298fd71SBarry Smith   ierr = MatSeqSBAIJSetPreallocation(B,1,0,NULL);CHKERRQ(ierr);
432b00a9115SJed Brown   ierr = PetscNewLog(B,&chol);CHKERRQ(ierr);
43326fbe8dcSKarl Rupp 
434641875f9SMatthew G Knepley   chol->Wrap    = MatWrapCholmod_seqsbaij;
435641875f9SMatthew G Knepley   chol->Destroy = MatDestroy_SeqSBAIJ;
436641875f9SMatthew G Knepley   B->spptr      = chol;
437641875f9SMatthew G Knepley 
438641875f9SMatthew G Knepley   B->ops->view                   = MatView_CHOLMOD;
439641875f9SMatthew G Knepley   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_CHOLMOD;
440641875f9SMatthew G Knepley   B->ops->destroy                = MatDestroy_CHOLMOD;
441bdf89e91SBarry Smith   ierr                           = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_seqsbaij_cholmod);CHKERRQ(ierr);
442641875f9SMatthew G Knepley   B->factortype                  = MAT_FACTOR_CHOLESKY;
443641875f9SMatthew G Knepley   B->assembled                   = PETSC_TRUE; /* required by -ksp_view */
444641875f9SMatthew G Knepley   B->preallocated                = PETSC_TRUE;
445641875f9SMatthew G Knepley 
446641875f9SMatthew G Knepley   ierr = CholmodStart(B);CHKERRQ(ierr);
447641875f9SMatthew G Knepley   *F   = B;
448641875f9SMatthew G Knepley   PetscFunctionReturn(0);
449641875f9SMatthew G Knepley }
450