xref: /petsc/src/mat/impls/aij/seq/umfpack/umfpack.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
11677d0b8SKris Buschelman 
21677d0b8SKris Buschelman /*
3d515b9b4SShri Abhyankar    Provides an interface to the UMFPACK sparse solver available through SuiteSparse version 4.2.1
42d4e2982SHong Zhang 
59e475b0dSSatish Balay    When build with PETSC_USE_64BIT_INDICES this will use Suitesparse_long as the
68592901bSBarry Smith    integer type in UMFPACK, otherwise it will use int. This means
78592901bSBarry Smith    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]
92d4e2982SHong Zhang 
101677d0b8SKris Buschelman */
11c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>
121677d0b8SKris Buschelman 
138592901bSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
148592901bSBarry Smith #if defined(PETSC_USE_COMPLEX)
158592901bSBarry Smith #define umfpack_UMF_free_symbolic umfpack_zl_free_symbolic
168592901bSBarry Smith #define umfpack_UMF_free_numeric  umfpack_zl_free_numeric
17d755ee67SBarry Smith /* the type casts are needed because PetscInt is long long while SuiteSparse_long is long and compilers warn even when they are identical */
18d755ee67SBarry Smith #define umfpack_UMF_wsolve(a, b, c, d, e, f, g, h, i, j, k, l, m, n) umfpack_zl_wsolve(a, (SuiteSparse_long *)b, (SuiteSparse_long *)c, d, e, f, g, h, i, (SuiteSparse_long *)j, k, l, (SuiteSparse_long *)m, n)
19d755ee67SBarry Smith #define umfpack_UMF_numeric(a, b, c, d, e, f, g, h)                  umfpack_zl_numeric((SuiteSparse_long *)a, (SuiteSparse_long *)b, c, d, e, f, g, h)
208592901bSBarry Smith #define umfpack_UMF_report_numeric                                   umfpack_zl_report_numeric
218592901bSBarry Smith #define umfpack_UMF_report_control                                   umfpack_zl_report_control
228592901bSBarry Smith #define umfpack_UMF_report_status                                    umfpack_zl_report_status
238592901bSBarry Smith #define umfpack_UMF_report_info                                      umfpack_zl_report_info
248592901bSBarry Smith #define umfpack_UMF_report_symbolic                                  umfpack_zl_report_symbolic
25d755ee67SBarry Smith #define umfpack_UMF_qsymbolic(a, b, c, d, e, f, g, h, i, j)          umfpack_zl_qsymbolic(a, b, (SuiteSparse_long *)c, (SuiteSparse_long *)d, e, f, (SuiteSparse_long *)g, h, i, j)
26d755ee67SBarry Smith #define umfpack_UMF_symbolic(a, b, c, d, e, f, g, h, i)              umfpack_zl_symbolic(a, b, (SuiteSparse_long *)c, (SuiteSparse_long *)d, e, f, g, h, i)
278592901bSBarry Smith #define umfpack_UMF_defaults                                         umfpack_zl_defaults
288592901bSBarry Smith 
298592901bSBarry Smith #else
308592901bSBarry Smith #define umfpack_UMF_free_symbolic                           umfpack_dl_free_symbolic
318592901bSBarry Smith #define umfpack_UMF_free_numeric                            umfpack_dl_free_numeric
32d755ee67SBarry Smith #define umfpack_UMF_wsolve(a, b, c, d, e, f, g, h, i, j, k) umfpack_dl_wsolve(a, (SuiteSparse_long *)b, (SuiteSparse_long *)c, d, e, f, g, h, i, (SuiteSparse_long *)j, k)
33d755ee67SBarry Smith #define umfpack_UMF_numeric(a, b, c, d, e, f, g)            umfpack_dl_numeric((SuiteSparse_long *)a, (SuiteSparse_long *)b, c, d, e, f, g)
348592901bSBarry Smith #define umfpack_UMF_report_numeric                          umfpack_dl_report_numeric
358592901bSBarry Smith #define umfpack_UMF_report_control                          umfpack_dl_report_control
368592901bSBarry Smith #define umfpack_UMF_report_status                           umfpack_dl_report_status
378592901bSBarry Smith #define umfpack_UMF_report_info                             umfpack_dl_report_info
388592901bSBarry Smith #define umfpack_UMF_report_symbolic                         umfpack_dl_report_symbolic
39d755ee67SBarry Smith #define umfpack_UMF_qsymbolic(a, b, c, d, e, f, g, h, i)    umfpack_dl_qsymbolic(a, b, (SuiteSparse_long *)c, (SuiteSparse_long *)d, e, (SuiteSparse_long *)f, g, h, i)
40d755ee67SBarry Smith #define umfpack_UMF_symbolic(a, b, c, d, e, f, g, h)        umfpack_dl_symbolic(a, b, (SuiteSparse_long *)c, (SuiteSparse_long *)d, e, f, g, h)
418592901bSBarry Smith #define umfpack_UMF_defaults                                umfpack_dl_defaults
428592901bSBarry Smith #endif
438592901bSBarry Smith 
448592901bSBarry Smith #else
458592901bSBarry Smith #if defined(PETSC_USE_COMPLEX)
468592901bSBarry Smith #define umfpack_UMF_free_symbolic   umfpack_zi_free_symbolic
478592901bSBarry Smith #define umfpack_UMF_free_numeric    umfpack_zi_free_numeric
488592901bSBarry Smith #define umfpack_UMF_wsolve          umfpack_zi_wsolve
498592901bSBarry Smith #define umfpack_UMF_numeric         umfpack_zi_numeric
508592901bSBarry Smith #define umfpack_UMF_report_numeric  umfpack_zi_report_numeric
518592901bSBarry Smith #define umfpack_UMF_report_control  umfpack_zi_report_control
528592901bSBarry Smith #define umfpack_UMF_report_status   umfpack_zi_report_status
538592901bSBarry Smith #define umfpack_UMF_report_info     umfpack_zi_report_info
548592901bSBarry Smith #define umfpack_UMF_report_symbolic umfpack_zi_report_symbolic
558592901bSBarry Smith #define umfpack_UMF_qsymbolic       umfpack_zi_qsymbolic
568592901bSBarry Smith #define umfpack_UMF_symbolic        umfpack_zi_symbolic
578592901bSBarry Smith #define umfpack_UMF_defaults        umfpack_zi_defaults
588592901bSBarry Smith 
598592901bSBarry Smith #else
608592901bSBarry Smith #define umfpack_UMF_free_symbolic   umfpack_di_free_symbolic
618592901bSBarry Smith #define umfpack_UMF_free_numeric    umfpack_di_free_numeric
628592901bSBarry Smith #define umfpack_UMF_wsolve          umfpack_di_wsolve
638592901bSBarry Smith #define umfpack_UMF_numeric         umfpack_di_numeric
648592901bSBarry Smith #define umfpack_UMF_report_numeric  umfpack_di_report_numeric
658592901bSBarry Smith #define umfpack_UMF_report_control  umfpack_di_report_control
668592901bSBarry Smith #define umfpack_UMF_report_status   umfpack_di_report_status
678592901bSBarry Smith #define umfpack_UMF_report_info     umfpack_di_report_info
688592901bSBarry Smith #define umfpack_UMF_report_symbolic umfpack_di_report_symbolic
698592901bSBarry Smith #define umfpack_UMF_qsymbolic       umfpack_di_qsymbolic
708592901bSBarry Smith #define umfpack_UMF_symbolic        umfpack_di_symbolic
718592901bSBarry Smith #define umfpack_UMF_defaults        umfpack_di_defaults
728592901bSBarry Smith #endif
738592901bSBarry Smith #endif
748592901bSBarry Smith 
751677d0b8SKris Buschelman EXTERN_C_BEGIN
76c6db04a5SJed Brown #include <umfpack.h>
771677d0b8SKris Buschelman EXTERN_C_END
781677d0b8SKris Buschelman 
79fcfd50ebSBarry Smith static const char *const UmfpackOrderingTypes[] = {"CHOLMOD", "AMD", "GIVEN", "METIS", "BEST", "NONE", "USER", "UmfpackOrderingTypes", "UMFPACK_ORDERING_", 0};
80fcfd50ebSBarry Smith 
811677d0b8SKris Buschelman typedef struct {
821677d0b8SKris Buschelman   void        *Symbolic, *Numeric;
831677d0b8SKris Buschelman   double       Info[UMFPACK_INFO], Control[UMFPACK_CONTROL], *W;
84ae26a541SJed Brown   PetscInt    *Wi, *perm_c;
85ae26a541SJed Brown   Mat          A; /* Matrix used for factorization */
861677d0b8SKris Buschelman   MatStructure flg;
871677d0b8SKris Buschelman 
881677d0b8SKris Buschelman   /* Flag to clean up UMFPACK objects during Destroy */
89ace3abfcSBarry Smith   PetscBool CleanUpUMFPACK;
90f0c56d0fSKris Buschelman } Mat_UMFPACK;
91f0c56d0fSKris Buschelman 
929371c9d4SSatish Balay static PetscErrorCode MatDestroy_UMFPACK(Mat A) {
93b9c12af5SBarry Smith   Mat_UMFPACK *lu = (Mat_UMFPACK *)A->data;
941677d0b8SKris Buschelman 
951677d0b8SKris Buschelman   PetscFunctionBegin;
96b9c12af5SBarry Smith   if (lu->CleanUpUMFPACK) {
978592901bSBarry Smith     umfpack_UMF_free_symbolic(&lu->Symbolic);
988592901bSBarry Smith     umfpack_UMF_free_numeric(&lu->Numeric);
999566063dSJacob Faibussowitsch     PetscCall(PetscFree(lu->Wi));
1009566063dSJacob Faibussowitsch     PetscCall(PetscFree(lu->W));
1019566063dSJacob Faibussowitsch     PetscCall(PetscFree(lu->perm_c));
1021677d0b8SKris Buschelman   }
1039566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&lu->A));
1042e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatFactorGetSolverType_C", NULL));
1059566063dSJacob Faibussowitsch   PetscCall(PetscFree(A->data));
1061677d0b8SKris Buschelman   PetscFunctionReturn(0);
1071677d0b8SKris Buschelman }
1081677d0b8SKris Buschelman 
1099371c9d4SSatish Balay static PetscErrorCode MatSolve_UMFPACK_Private(Mat A, Vec b, Vec x, int uflag) {
110b9c12af5SBarry Smith   Mat_UMFPACK       *lu = (Mat_UMFPACK *)A->data;
111ae26a541SJed Brown   Mat_SeqAIJ        *a  = (Mat_SeqAIJ *)lu->A->data;
112d9ca1df4SBarry Smith   PetscScalar       *av = a->a, *xa;
113d9ca1df4SBarry Smith   const PetscScalar *ba;
114ae26a541SJed Brown   PetscInt          *ai = a->i, *aj = a->j, status;
115fcf6e9abSJed Brown   static PetscBool   cite = PETSC_FALSE;
1161677d0b8SKris Buschelman 
1171677d0b8SKris Buschelman   PetscFunctionBegin;
118a643c07aSBarry Smith   if (!A->rmap->n) PetscFunctionReturn(0);
1199371c9d4SSatish Balay   PetscCall(PetscCitationsRegister("@article{davis2004algorithm,\n  title={Algorithm 832: {UMFPACK} V4.3---An Unsymmetric-Pattern Multifrontal Method},\n  author={Davis, Timothy A},\n  journal={ACM Transactions on Mathematical Software (TOMS)},\n  "
1209371c9d4SSatish Balay                                    "volume={30},\n  number={2},\n  pages={196--199},\n  year={2004},\n  publisher={ACM}\n}\n",
1219371c9d4SSatish Balay                                    &cite));
1222d4e2982SHong Zhang   /* solve Ax = b by umfpack_*_wsolve */
1231677d0b8SKris Buschelman   /* ----------------------------------*/
1242d4e2982SHong Zhang 
125ae26a541SJed Brown   if (!lu->Wi) { /* first time, allocate working space for wsolve */
1269566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(A->rmap->n, &lu->Wi));
1279566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(5 * A->rmap->n, &lu->W));
128ae26a541SJed Brown   }
129ae26a541SJed Brown 
1309566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(b, &ba));
1319566063dSJacob Faibussowitsch   PetscCall(VecGetArray(x, &xa));
13279c34000SHong Zhang #if defined(PETSC_USE_COMPLEX)
133b0043f70SBarry Smith   status = umfpack_UMF_wsolve(uflag, ai, aj, (PetscReal *)av, NULL, (PetscReal *)xa, NULL, (PetscReal *)ba, NULL, lu->Numeric, lu->Control, lu->Info, lu->Wi, lu->W);
13479c34000SHong Zhang #else
1354b019bd2SJed Brown   status = umfpack_UMF_wsolve(uflag, ai, aj, av, xa, ba, lu->Numeric, lu->Control, lu->Info, lu->Wi, lu->W);
13679c34000SHong Zhang #endif
1378592901bSBarry Smith   umfpack_UMF_report_info(lu->Control, lu->Info);
1381677d0b8SKris Buschelman   if (status < 0) {
1398592901bSBarry Smith     umfpack_UMF_report_status(lu->Control, status);
140e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "umfpack_UMF_wsolve failed");
1411677d0b8SKris Buschelman   }
1421677d0b8SKris Buschelman 
1439566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(b, &ba));
1449566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(x, &xa));
1454b019bd2SJed Brown   PetscFunctionReturn(0);
1464b019bd2SJed Brown }
1472a325a84SHong Zhang 
1489371c9d4SSatish Balay static PetscErrorCode MatSolve_UMFPACK(Mat A, Vec b, Vec x) {
1494b019bd2SJed Brown   PetscFunctionBegin;
1504b019bd2SJed Brown   /* We gave UMFPACK the algebraic transpose (because it assumes column alignment) */
1519566063dSJacob Faibussowitsch   PetscCall(MatSolve_UMFPACK_Private(A, b, x, UMFPACK_Aat));
1524b019bd2SJed Brown   PetscFunctionReturn(0);
1534b019bd2SJed Brown }
1544b019bd2SJed Brown 
1559371c9d4SSatish Balay static PetscErrorCode MatSolveTranspose_UMFPACK(Mat A, Vec b, Vec x) {
1564b019bd2SJed Brown   PetscFunctionBegin;
1574b019bd2SJed Brown   /* We gave UMFPACK the algebraic transpose (because it assumes column alignment) */
1589566063dSJacob Faibussowitsch   PetscCall(MatSolve_UMFPACK_Private(A, b, x, UMFPACK_A));
1591677d0b8SKris Buschelman   PetscFunctionReturn(0);
1601677d0b8SKris Buschelman }
1611677d0b8SKris Buschelman 
1629371c9d4SSatish Balay static PetscErrorCode MatLUFactorNumeric_UMFPACK(Mat F, Mat A, const MatFactorInfo *info) {
163b9c12af5SBarry Smith   Mat_UMFPACK *lu = (Mat_UMFPACK *)(F)->data;
164ae26a541SJed Brown   Mat_SeqAIJ  *a  = (Mat_SeqAIJ *)A->data;
165ae26a541SJed Brown   PetscInt    *ai = a->i, *aj = a->j, status;
166ae26a541SJed Brown   PetscScalar *av = a->a;
1671677d0b8SKris Buschelman 
1681677d0b8SKris Buschelman   PetscFunctionBegin;
169a643c07aSBarry Smith   if (!A->rmap->n) PetscFunctionReturn(0);
1701677d0b8SKris Buschelman   /* numeric factorization of A' */
1711677d0b8SKris Buschelman   /* ----------------------------*/
1722d4e2982SHong Zhang 
173ad540459SPierre Jolivet   if (lu->flg == SAME_NONZERO_PATTERN && lu->Numeric) umfpack_UMF_free_numeric(&lu->Numeric);
1742d4e2982SHong Zhang #if defined(PETSC_USE_COMPLEX)
1758592901bSBarry Smith   status = umfpack_UMF_numeric(ai, aj, (double *)av, NULL, lu->Symbolic, &lu->Numeric, lu->Control, lu->Info);
1762d4e2982SHong Zhang #else
1778592901bSBarry Smith   status = umfpack_UMF_numeric(ai, aj, av, lu->Symbolic, &lu->Numeric, lu->Control, lu->Info);
1788592901bSBarry Smith #endif
1799f42a82aSMatthew Knepley   if (status < 0) {
1808592901bSBarry Smith     umfpack_UMF_report_status(lu->Control, status);
181e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "umfpack_UMF_numeric failed");
1829f42a82aSMatthew Knepley   }
1832d4e2982SHong Zhang   /* report numeric factorization of A' when Control[PRL] > 3 */
1848592901bSBarry Smith   (void)umfpack_UMF_report_numeric(lu->Numeric, lu->Control);
1851677d0b8SKris Buschelman 
1869566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)A));
1879566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&lu->A));
1882205254eSKarl Rupp 
189ae26a541SJed Brown   lu->A                  = A;
1902a325a84SHong Zhang   lu->flg                = SAME_NONZERO_PATTERN;
1912a325a84SHong Zhang   lu->CleanUpUMFPACK     = PETSC_TRUE;
1924b019bd2SJed Brown   F->ops->solve          = MatSolve_UMFPACK;
1934b019bd2SJed Brown   F->ops->solvetranspose = MatSolveTranspose_UMFPACK;
1941677d0b8SKris Buschelman   PetscFunctionReturn(0);
1951677d0b8SKris Buschelman }
1961677d0b8SKris Buschelman 
1979371c9d4SSatish Balay static PetscErrorCode MatLUFactorSymbolic_UMFPACK(Mat F, Mat A, IS r, IS c, const MatFactorInfo *info) {
198ae26a541SJed Brown   Mat_SeqAIJ  *a  = (Mat_SeqAIJ *)A->data;
199b9c12af5SBarry Smith   Mat_UMFPACK *lu = (Mat_UMFPACK *)(F->data);
20026cc229bSBarry Smith   PetscInt     i, *ai = a->i, *aj = a->j, m = A->rmap->n, n = A->cmap->n, status, idx;
201989213a4SSatish Balay #if !defined(PETSC_USE_COMPLEX)
202ae26a541SJed Brown   PetscScalar *av = a->a;
203989213a4SSatish Balay #endif
2045d0c19d7SBarry Smith   const PetscInt *ra;
20526cc229bSBarry Smith   const char     *strategy[] = {"AUTO", "UNSYMMETRIC", "SYMMETRIC"};
20626cc229bSBarry Smith   const char     *scale[]    = {"NONE", "SUM", "MAX"};
20726cc229bSBarry Smith   PetscBool       flg;
2081677d0b8SKris Buschelman 
2091677d0b8SKris Buschelman   PetscFunctionBegin;
210a643c07aSBarry Smith   (F)->ops->lufactornumeric = MatLUFactorNumeric_UMFPACK;
211a643c07aSBarry Smith   if (!n) PetscFunctionReturn(0);
21226cc229bSBarry Smith 
21326cc229bSBarry Smith   /* Set options to F */
21426cc229bSBarry Smith   PetscOptionsBegin(PetscObjectComm((PetscObject)F), ((PetscObject)F)->prefix, "UMFPACK Options", "Mat");
21526cc229bSBarry Smith   /* Control parameters used by reporting routiones */
21626cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_prl", "Control[UMFPACK_PRL]", "None", lu->Control[UMFPACK_PRL], &lu->Control[UMFPACK_PRL], NULL));
21726cc229bSBarry Smith 
21826cc229bSBarry Smith   /* Control parameters for symbolic factorization */
21926cc229bSBarry Smith   PetscCall(PetscOptionsEList("-mat_umfpack_strategy", "ordering and pivoting strategy", "None", strategy, 3, strategy[0], &idx, &flg));
22026cc229bSBarry Smith   if (flg) {
22126cc229bSBarry Smith     switch (idx) {
22226cc229bSBarry Smith     case 0: lu->Control[UMFPACK_STRATEGY] = UMFPACK_STRATEGY_AUTO; break;
22326cc229bSBarry Smith     case 1: lu->Control[UMFPACK_STRATEGY] = UMFPACK_STRATEGY_UNSYMMETRIC; break;
22426cc229bSBarry Smith     case 2: lu->Control[UMFPACK_STRATEGY] = UMFPACK_STRATEGY_SYMMETRIC; break;
22526cc229bSBarry Smith     }
22626cc229bSBarry Smith   }
22726cc229bSBarry Smith   PetscCall(PetscOptionsEList("-mat_umfpack_ordering", "Internal ordering method", "None", UmfpackOrderingTypes, PETSC_STATIC_ARRAY_LENGTH(UmfpackOrderingTypes), UmfpackOrderingTypes[(int)lu->Control[UMFPACK_ORDERING]], &idx, &flg));
22826cc229bSBarry Smith   if (flg) lu->Control[UMFPACK_ORDERING] = (int)idx;
22926cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_dense_col", "Control[UMFPACK_DENSE_COL]", "None", lu->Control[UMFPACK_DENSE_COL], &lu->Control[UMFPACK_DENSE_COL], NULL));
23026cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_dense_row", "Control[UMFPACK_DENSE_ROW]", "None", lu->Control[UMFPACK_DENSE_ROW], &lu->Control[UMFPACK_DENSE_ROW], NULL));
23126cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_amd_dense", "Control[UMFPACK_AMD_DENSE]", "None", lu->Control[UMFPACK_AMD_DENSE], &lu->Control[UMFPACK_AMD_DENSE], NULL));
23226cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_block_size", "Control[UMFPACK_BLOCK_SIZE]", "None", lu->Control[UMFPACK_BLOCK_SIZE], &lu->Control[UMFPACK_BLOCK_SIZE], NULL));
23326cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_fixq", "Control[UMFPACK_FIXQ]", "None", lu->Control[UMFPACK_FIXQ], &lu->Control[UMFPACK_FIXQ], NULL));
23426cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_aggressive", "Control[UMFPACK_AGGRESSIVE]", "None", lu->Control[UMFPACK_AGGRESSIVE], &lu->Control[UMFPACK_AGGRESSIVE], NULL));
23526cc229bSBarry Smith 
23626cc229bSBarry Smith   /* Control parameters used by numeric factorization */
23726cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_pivot_tolerance", "Control[UMFPACK_PIVOT_TOLERANCE]", "None", lu->Control[UMFPACK_PIVOT_TOLERANCE], &lu->Control[UMFPACK_PIVOT_TOLERANCE], NULL));
23826cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_sym_pivot_tolerance", "Control[UMFPACK_SYM_PIVOT_TOLERANCE]", "None", lu->Control[UMFPACK_SYM_PIVOT_TOLERANCE], &lu->Control[UMFPACK_SYM_PIVOT_TOLERANCE], NULL));
23926cc229bSBarry Smith   PetscCall(PetscOptionsEList("-mat_umfpack_scale", "Control[UMFPACK_SCALE]", "None", scale, 3, scale[0], &idx, &flg));
24026cc229bSBarry Smith   if (flg) {
24126cc229bSBarry Smith     switch (idx) {
24226cc229bSBarry Smith     case 0: lu->Control[UMFPACK_SCALE] = UMFPACK_SCALE_NONE; break;
24326cc229bSBarry Smith     case 1: lu->Control[UMFPACK_SCALE] = UMFPACK_SCALE_SUM; break;
24426cc229bSBarry Smith     case 2: lu->Control[UMFPACK_SCALE] = UMFPACK_SCALE_MAX; break;
24526cc229bSBarry Smith     }
24626cc229bSBarry Smith   }
24726cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_alloc_init", "Control[UMFPACK_ALLOC_INIT]", "None", lu->Control[UMFPACK_ALLOC_INIT], &lu->Control[UMFPACK_ALLOC_INIT], NULL));
24826cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_front_alloc_init", "Control[UMFPACK_FRONT_ALLOC_INIT]", "None", lu->Control[UMFPACK_FRONT_ALLOC_INIT], &lu->Control[UMFPACK_ALLOC_INIT], NULL));
24926cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_droptol", "Control[UMFPACK_DROPTOL]", "None", lu->Control[UMFPACK_DROPTOL], &lu->Control[UMFPACK_DROPTOL], NULL));
25026cc229bSBarry Smith 
25126cc229bSBarry Smith   /* Control parameters used by solve */
25226cc229bSBarry Smith   PetscCall(PetscOptionsReal("-mat_umfpack_irstep", "Control[UMFPACK_IRSTEP]", "None", lu->Control[UMFPACK_IRSTEP], &lu->Control[UMFPACK_IRSTEP], NULL));
25326cc229bSBarry Smith   PetscOptionsEnd();
25426cc229bSBarry Smith 
2552c7c0729SBarry Smith   if (r) {
2569566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(r, &ra));
2579566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(m, &lu->perm_c));
2582d4e2982SHong Zhang     /* we cannot simply memcpy on 64 bit archs */
2592d4e2982SHong Zhang     for (i = 0; i < m; i++) lu->perm_c[i] = ra[i];
2609566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(r, &ra));
2611677d0b8SKris Buschelman   }
2621677d0b8SKris Buschelman 
2631677d0b8SKris Buschelman   /* print the control parameters */
2648592901bSBarry Smith   if (lu->Control[UMFPACK_PRL] > 1) umfpack_UMF_report_control(lu->Control);
2651677d0b8SKris Buschelman 
2661677d0b8SKris Buschelman   /* symbolic factorization of A' */
2671677d0b8SKris Buschelman   /* ---------------------------------------------------------------------- */
2682c7c0729SBarry Smith   if (r) { /* use Petsc row ordering */
2698592901bSBarry Smith #if !defined(PETSC_USE_COMPLEX)
270ae26a541SJed Brown     status = umfpack_UMF_qsymbolic(n, m, ai, aj, av, lu->perm_c, &lu->Symbolic, lu->Control, lu->Info);
2712d4e2982SHong Zhang #else
272b0043f70SBarry Smith     status = umfpack_UMF_qsymbolic(n, m, ai, aj, NULL, NULL, lu->perm_c, &lu->Symbolic, lu->Control, lu->Info);
2738592901bSBarry Smith #endif
2742d4e2982SHong Zhang   } else { /* use Umfpack col ordering */
2758592901bSBarry Smith #if !defined(PETSC_USE_COMPLEX)
276ae26a541SJed Brown     status = umfpack_UMF_symbolic(n, m, ai, aj, av, &lu->Symbolic, lu->Control, lu->Info);
2778592901bSBarry Smith #else
278ae26a541SJed Brown     status = umfpack_UMF_symbolic(n, m, ai, aj, NULL, NULL, &lu->Symbolic, lu->Control, lu->Info);
2798592901bSBarry Smith #endif
2802d4e2982SHong Zhang   }
2812d4e2982SHong Zhang   if (status < 0) {
2828592901bSBarry Smith     umfpack_UMF_report_info(lu->Control, lu->Info);
2838592901bSBarry Smith     umfpack_UMF_report_status(lu->Control, status);
284e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "umfpack_UMF_symbolic failed");
2852d4e2982SHong Zhang   }
2862d4e2982SHong Zhang   /* report sumbolic factorization of A' when Control[PRL] > 3 */
2878592901bSBarry Smith   (void)umfpack_UMF_report_symbolic(lu->Symbolic, lu->Control);
2881677d0b8SKris Buschelman 
2891677d0b8SKris Buschelman   lu->flg            = DIFFERENT_NONZERO_PATTERN;
290e1b4c3a1SKris Buschelman   lu->CleanUpUMFPACK = PETSC_TRUE;
2911677d0b8SKris Buschelman   PetscFunctionReturn(0);
2921677d0b8SKris Buschelman }
2931677d0b8SKris Buschelman 
2949371c9d4SSatish Balay static PetscErrorCode MatView_Info_UMFPACK(Mat A, PetscViewer viewer) {
295b9c12af5SBarry Smith   Mat_UMFPACK *lu = (Mat_UMFPACK *)A->data;
296f0c56d0fSKris Buschelman 
2971677d0b8SKris Buschelman   PetscFunctionBegin;
2981677d0b8SKris Buschelman   /* check if matrix is UMFPACK type */
299f0c56d0fSKris Buschelman   if (A->ops->solve != MatSolve_UMFPACK) PetscFunctionReturn(0);
3001677d0b8SKris Buschelman 
3019566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "UMFPACK run parameters:\n"));
3021677d0b8SKris Buschelman   /* Control parameters used by reporting routiones */
3039566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_PRL]: %g\n", lu->Control[UMFPACK_PRL]));
3041677d0b8SKris Buschelman 
3051677d0b8SKris Buschelman   /* Control parameters used by symbolic factorization */
3069566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_STRATEGY]: %g\n", lu->Control[UMFPACK_STRATEGY]));
3079566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_DENSE_COL]: %g\n", lu->Control[UMFPACK_DENSE_COL]));
3089566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_DENSE_ROW]: %g\n", lu->Control[UMFPACK_DENSE_ROW]));
3099566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_AMD_DENSE]: %g\n", lu->Control[UMFPACK_AMD_DENSE]));
3109566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_BLOCK_SIZE]: %g\n", lu->Control[UMFPACK_BLOCK_SIZE]));
3119566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_FIXQ]: %g\n", lu->Control[UMFPACK_FIXQ]));
3129566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_AGGRESSIVE]: %g\n", lu->Control[UMFPACK_AGGRESSIVE]));
3131677d0b8SKris Buschelman 
3141677d0b8SKris Buschelman   /* Control parameters used by numeric factorization */
3159566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_PIVOT_TOLERANCE]: %g\n", lu->Control[UMFPACK_PIVOT_TOLERANCE]));
3169566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_SYM_PIVOT_TOLERANCE]: %g\n", lu->Control[UMFPACK_SYM_PIVOT_TOLERANCE]));
3179566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_SCALE]: %g\n", lu->Control[UMFPACK_SCALE]));
3189566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_ALLOC_INIT]: %g\n", lu->Control[UMFPACK_ALLOC_INIT]));
3199566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_DROPTOL]: %g\n", lu->Control[UMFPACK_DROPTOL]));
3201677d0b8SKris Buschelman 
3211677d0b8SKris Buschelman   /* Control parameters used by solve */
3229566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_IRSTEP]: %g\n", lu->Control[UMFPACK_IRSTEP]));
3231677d0b8SKris Buschelman 
3241677d0b8SKris Buschelman   /* mat ordering */
32548a46eb9SPierre Jolivet   if (!lu->perm_c) PetscCall(PetscViewerASCIIPrintf(viewer, "  Control[UMFPACK_ORDERING]: %s (not using the PETSc ordering)\n", UmfpackOrderingTypes[(int)lu->Control[UMFPACK_ORDERING]]));
3261677d0b8SKris Buschelman   PetscFunctionReturn(0);
3271677d0b8SKris Buschelman }
3281677d0b8SKris Buschelman 
3299371c9d4SSatish Balay static PetscErrorCode MatView_UMFPACK(Mat A, PetscViewer viewer) {
330ace3abfcSBarry Smith   PetscBool         iascii;
331d844382dSKris Buschelman   PetscViewerFormat format;
332d844382dSKris Buschelman 
333d844382dSKris Buschelman   PetscFunctionBegin;
3349566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
33532077d6dSBarry Smith   if (iascii) {
3369566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
33748a46eb9SPierre Jolivet     if (format == PETSC_VIEWER_ASCII_INFO) PetscCall(MatView_Info_UMFPACK(A, viewer));
338d844382dSKris Buschelman   }
339d844382dSKris Buschelman   PetscFunctionReturn(0);
340d844382dSKris Buschelman }
341d844382dSKris Buschelman 
3429371c9d4SSatish Balay PetscErrorCode MatFactorGetSolverType_seqaij_umfpack(Mat A, MatSolverType *type) {
34335bd34faSBarry Smith   PetscFunctionBegin;
3442692d6eeSBarry Smith   *type = MATSOLVERUMFPACK;
34535bd34faSBarry Smith   PetscFunctionReturn(0);
34635bd34faSBarry Smith }
34735bd34faSBarry Smith 
3482f71e704SKris Buschelman /*MC
34911a5261eSBarry Smith   MATSOLVERUMFPACK = "umfpack" - A matrix type providing direct solvers, LU, for sequential matrices
3502f71e704SKris Buschelman   via the external package UMFPACK.
3512f71e704SKris Buschelman 
352c2b89b5dSBarry Smith   Use ./configure --download-suitesparse to install PETSc to use UMFPACK
353c2b89b5dSBarry Smith 
3543ca39a21SBarry Smith   Use -pc_type lu -pc_factor_mat_solver_type umfpack to use this direct solver
3552f71e704SKris Buschelman 
3562f71e704SKris Buschelman   Consult UMFPACK documentation for more information about the Control parameters
3572f71e704SKris Buschelman   which correspond to the options database keys below.
3582f71e704SKris Buschelman 
3592f71e704SKris Buschelman   Options Database Keys:
360ba41fbb6SBarry Smith + -mat_umfpack_ordering                - CHOLMOD, AMD, GIVEN, METIS, BEST, NONE
361ba41fbb6SBarry Smith . -mat_umfpack_prl                     - UMFPACK print level: Control[UMFPACK_PRL]
362e08999f5SMatthew G Knepley . -mat_umfpack_strategy <AUTO>         - (choose one of) AUTO UNSYMMETRIC SYMMETRIC 2BY2
3632f71e704SKris Buschelman . -mat_umfpack_dense_col <alpha_c>     - UMFPACK dense column threshold: Control[UMFPACK_DENSE_COL]
364e08999f5SMatthew G Knepley . -mat_umfpack_dense_row <0.2>         - Control[UMFPACK_DENSE_ROW]
365e08999f5SMatthew G Knepley . -mat_umfpack_amd_dense <10>          - Control[UMFPACK_AMD_DENSE]
3662f71e704SKris Buschelman . -mat_umfpack_block_size <bs>         - UMFPACK block size for BLAS-Level 3 calls: Control[UMFPACK_BLOCK_SIZE]
367e08999f5SMatthew G Knepley . -mat_umfpack_2by2_tolerance <0.01>   - Control[UMFPACK_2BY2_TOLERANCE]
368e08999f5SMatthew G Knepley . -mat_umfpack_fixq <0>                - Control[UMFPACK_FIXQ]
369e08999f5SMatthew G Knepley . -mat_umfpack_aggressive <1>          - Control[UMFPACK_AGGRESSIVE]
3702f71e704SKris Buschelman . -mat_umfpack_pivot_tolerance <delta> - UMFPACK partial pivot tolerance: Control[UMFPACK_PIVOT_TOLERANCE]
371e08999f5SMatthew G Knepley . -mat_umfpack_sym_pivot_tolerance <0.001> - Control[UMFPACK_SYM_PIVOT_TOLERANCE]
372e08999f5SMatthew G Knepley . -mat_umfpack_scale <NONE>           - (choose one of) NONE SUM MAX
3732f71e704SKris Buschelman . -mat_umfpack_alloc_init <delta>      - UMFPACK factorized matrix allocation modifier: Control[UMFPACK_ALLOC_INIT]
374e08999f5SMatthew G Knepley . -mat_umfpack_droptol <0>            - Control[UMFPACK_DROPTOL]
3752f71e704SKris Buschelman - -mat_umfpack_irstep <maxit>          - UMFPACK maximum number of iterative refinement steps: Control[UMFPACK_IRSTEP]
3762f71e704SKris Buschelman 
3772f71e704SKris Buschelman    Level: beginner
378a364b7d2SBarry Smith 
379a364b7d2SBarry Smith    Note: UMFPACK is part of SuiteSparse http://faculty.cse.tamu.edu/davis/suitesparse.html
3802f71e704SKris Buschelman 
381db781477SPatrick Sanan .seealso: `PCLU`, `MATSOLVERSUPERLU`, `MATSOLVERMUMPS`, `PCFactorSetMatSolverType()`, `MatSolverType`
3822f71e704SKris Buschelman M*/
383b2573a8aSBarry Smith 
3849371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_umfpack(Mat A, MatFactorType ftype, Mat *F) {
3853519fcdcSHong Zhang   Mat          B;
3863519fcdcSHong Zhang   Mat_UMFPACK *lu;
38726cc229bSBarry Smith   PetscInt     m = A->rmap->n, n = A->cmap->n;
3882d4e2982SHong Zhang 
3893519fcdcSHong Zhang   PetscFunctionBegin;
3903519fcdcSHong Zhang   /* Create the factorization matrix F */
3919566063dSJacob Faibussowitsch   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
3929566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, m, n));
3939566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy("umfpack", &((PetscObject)B)->type_name));
3949566063dSJacob Faibussowitsch   PetscCall(MatSetUp(B));
395b9c12af5SBarry Smith 
396*4dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&lu));
3972205254eSKarl Rupp 
398b9c12af5SBarry Smith   B->data                  = lu;
399b9c12af5SBarry Smith   B->ops->getinfo          = MatGetInfo_External;
4003519fcdcSHong Zhang   B->ops->lufactorsymbolic = MatLUFactorSymbolic_UMFPACK;
4013519fcdcSHong Zhang   B->ops->destroy          = MatDestroy_UMFPACK;
4023519fcdcSHong Zhang   B->ops->view             = MatView_UMFPACK;
4031aef8b4cSStefano Zampini   B->ops->matsolve         = NULL;
4042205254eSKarl Rupp 
4059566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_seqaij_umfpack));
4062205254eSKarl Rupp 
407d5f3da31SBarry Smith   B->factortype   = MAT_FACTOR_LU;
4083519fcdcSHong Zhang   B->assembled    = PETSC_TRUE; /* required by -ksp_view */
4093519fcdcSHong Zhang   B->preallocated = PETSC_TRUE;
4103519fcdcSHong Zhang 
4119566063dSJacob Faibussowitsch   PetscCall(PetscFree(B->solvertype));
4129566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(MATSOLVERUMFPACK, &B->solvertype));
413f73b0415SBarry Smith   B->canuseordering = PETSC_TRUE;
4149566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[MAT_FACTOR_LU]));
41500c67f3bSHong Zhang 
4163519fcdcSHong Zhang   /* initializations */
4173519fcdcSHong Zhang   /* ------------------------------------------------*/
4183519fcdcSHong Zhang   /* get the default control parameters */
4198592901bSBarry Smith   umfpack_UMF_defaults(lu->Control);
4200298fd71SBarry Smith   lu->perm_c                  = NULL; /* use defaul UMFPACK col permutation */
4213519fcdcSHong Zhang   lu->Control[UMFPACK_IRSTEP] = 0;    /* max num of iterative refinement steps to attempt */
4223519fcdcSHong Zhang 
4233519fcdcSHong Zhang   *F = B;
4243519fcdcSHong Zhang   PetscFunctionReturn(0);
4253519fcdcSHong Zhang }
426b2573a8aSBarry Smith 
427db87b0f2SBarry Smith PETSC_INTERN PetscErrorCode MatGetFactor_seqaij_cholmod(Mat, MatFactorType, Mat *);
428db87b0f2SBarry Smith PETSC_INTERN PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat, MatFactorType, Mat *);
429db87b0f2SBarry Smith PETSC_INTERN PetscErrorCode MatGetFactor_seqaij_klu(Mat, MatFactorType, Mat *);
430a2fc1e05SToby Isaac PETSC_INTERN PetscErrorCode MatGetFactor_seqaij_spqr(Mat, MatFactorType, Mat *);
4312d4e2982SHong Zhang 
4329371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_SuiteSparse(void) {
43342c9c57cSBarry Smith   PetscFunctionBegin;
4349566063dSJacob Faibussowitsch   PetscCall(MatSolverTypeRegister(MATSOLVERUMFPACK, MATSEQAIJ, MAT_FACTOR_LU, MatGetFactor_seqaij_umfpack));
4359566063dSJacob Faibussowitsch   PetscCall(MatSolverTypeRegister(MATSOLVERCHOLMOD, MATSEQAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_seqaij_cholmod));
4369566063dSJacob Faibussowitsch   PetscCall(MatSolverTypeRegister(MATSOLVERCHOLMOD, MATSEQSBAIJ, MAT_FACTOR_CHOLESKY, MatGetFactor_seqsbaij_cholmod));
4379566063dSJacob Faibussowitsch   PetscCall(MatSolverTypeRegister(MATSOLVERKLU, MATSEQAIJ, MAT_FACTOR_LU, MatGetFactor_seqaij_klu));
4389566063dSJacob Faibussowitsch   PetscCall(MatSolverTypeRegister(MATSOLVERSPQR, MATSEQAIJ, MAT_FACTOR_QR, MatGetFactor_seqaij_spqr));
43948a46eb9SPierre Jolivet   if (!PetscDefined(USE_COMPLEX)) PetscCall(MatSolverTypeRegister(MATSOLVERSPQR, MATNORMAL, MAT_FACTOR_QR, MatGetFactor_seqaij_spqr));
4409566063dSJacob Faibussowitsch   PetscCall(MatSolverTypeRegister(MATSOLVERSPQR, MATNORMALHERMITIAN, MAT_FACTOR_QR, MatGetFactor_seqaij_spqr));
44142c9c57cSBarry Smith   PetscFunctionReturn(0);
44242c9c57cSBarry Smith }
443