|
| 1 | + |
| 2 | +/* |
| 3 | + * -- SuperLU routine (version 6.0) -- |
| 4 | + * Univ. of California Berkeley, Xerox Palo Alto Research Center, |
| 5 | + * and Lawrence Berkeley National Lab. |
| 6 | + * October 15, 2003 |
| 7 | + * |
| 8 | + * March 26, 2023 Add 64-bit indexing and METIS ordering |
| 9 | + */ |
| 10 | + |
| 11 | +#include "slu_ddefs.h" |
| 12 | + |
| 13 | +#define HANDLE_SIZE 8 |
| 14 | + |
| 15 | +/* kind of integer to hold a pointer. Use 64-bit. */ |
| 16 | +typedef long long int fptr; |
| 17 | + |
| 18 | +typedef struct { |
| 19 | + SuperMatrix *L; |
| 20 | + SuperMatrix *U; |
| 21 | + int *perm_c; |
| 22 | + int *perm_r; |
| 23 | +} factors_t; |
| 24 | + |
| 25 | +/*! |
| 26 | + * This routine can be called from Fortran. |
| 27 | + * |
| 28 | + * iopt (input) int |
| 29 | + * Specifies the operation: |
| 30 | + * = 1, performs LU decomposition for the first time |
| 31 | + * = 2, performs triangular solve |
| 32 | + * = 3, free all the storage in the end |
| 33 | + * |
| 34 | + * f_factors (input/output) fptr* |
| 35 | + * If iopt == 1, it is an output and contains the pointer pointing to |
| 36 | + * the structure of the factored matrices. |
| 37 | + * Otherwise, it it an input. |
| 38 | + */ |
| 39 | +void c_fortran_dgssv_(int *iopt, int *n, int_t *nnz, int *nrhs, double *values, |
| 40 | + int_t *rowind, int_t *colptr, double *b, int *ldb, |
| 41 | + fptr *f_factors, /* a handle containing the address |
| 42 | + pointing to the factored matrices */ |
| 43 | + int_t *info) { |
| 44 | + SuperMatrix A, AC, B; |
| 45 | + SuperMatrix *L, *U; |
| 46 | + int *perm_r; /* row permutations from partial pivoting */ |
| 47 | + int *perm_c; /* column permutation vector */ |
| 48 | + int *etree; /* column elimination tree */ |
| 49 | + SCformat *Lstore; |
| 50 | + NCformat *Ustore; |
| 51 | + int i, panel_size, permc_spec, relax; |
| 52 | + trans_t trans; |
| 53 | + mem_usage_t mem_usage; |
| 54 | + superlu_options_t options; |
| 55 | + SuperLUStat_t stat; |
| 56 | + factors_t *LUfactors; |
| 57 | + GlobalLU_t Glu; /* Not needed on return. */ |
| 58 | + int_t *rowind0; /* counter 1-based indexing from Fortran arrays. */ |
| 59 | + int_t *colptr0; |
| 60 | + |
| 61 | + trans = NOTRANS; |
| 62 | + |
| 63 | + if (*iopt == 1) { /* LU decomposition */ |
| 64 | + |
| 65 | + /* Set the default input options. */ |
| 66 | + set_default_options(&options); |
| 67 | + |
| 68 | + /* Initialize the statistics variables. */ |
| 69 | + StatInit(&stat); |
| 70 | + |
| 71 | + /* Adjust to 0-based indexing */ |
| 72 | + if (!(rowind0 = intMalloc(*nnz))) |
| 73 | + ABORT("Malloc fails for rowind0[]."); |
| 74 | + if (!(colptr0 = intMalloc(*n + 1))) |
| 75 | + ABORT("Malloc fails for colptr0[]."); |
| 76 | + for (i = 0; i < *nnz; ++i) |
| 77 | + rowind0[i] = rowind[i] - 1; |
| 78 | + for (i = 0; i <= *n; ++i) |
| 79 | + colptr0[i] = colptr[i] - 1; |
| 80 | + |
| 81 | + dCreate_CompCol_Matrix(&A, *n, *n, *nnz, values, rowind0, colptr0, SLU_NC, |
| 82 | + SLU_D, SLU_GE); |
| 83 | + L = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix)); |
| 84 | + U = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix)); |
| 85 | + if (!(perm_r = int32Malloc(*n))) |
| 86 | + ABORT("Malloc fails for perm_r[]."); |
| 87 | + if (!(perm_c = int32Malloc(*n))) |
| 88 | + ABORT("Malloc fails for perm_c[]."); |
| 89 | + if (!(etree = int32Malloc(*n))) |
| 90 | + ABORT("Malloc fails for etree[]."); |
| 91 | +/* |
| 92 | + * Get column permutation vector perm_c[], according to permc_spec: |
| 93 | + * permc_spec = 0: natural ordering |
| 94 | + * permc_spec = 1: minimum degree on structure of A'*A |
| 95 | + * permc_spec = 2: minimum degree on structure of A'+A |
| 96 | + * permc_spec = 3: approximate minimum degree for unsymmetric matrices |
| 97 | + * permc_spec = 6: METIS ordering on structure of A'*A |
| 98 | + */ |
| 99 | +#if (HAVE_METIS) |
| 100 | + printf("USING METIS ORDERING\r\n"); |
| 101 | + permc_spec = 6; |
| 102 | +#else |
| 103 | + permc_spec = options.ColPerm; |
| 104 | +#endif |
| 105 | + // permc_spec = 0; |
| 106 | + // printf("before get_perm_c: permc_spec %d, *n %d\n", permc_spec, *n); |
| 107 | + get_perm_c(permc_spec, &A, perm_c); |
| 108 | + // printf("after get_perm_c: permc_spec %d\n", permc_spec); |
| 109 | + |
| 110 | + sp_preorder(&options, &A, perm_c, etree, &AC); |
| 111 | + |
| 112 | + panel_size = sp_ienv(1); |
| 113 | + relax = sp_ienv(2); |
| 114 | + |
| 115 | + dgstrf(&options, &AC, relax, panel_size, etree, NULL, 0, perm_c, perm_r, L, |
| 116 | + U, &Glu, &stat, info); |
| 117 | + |
| 118 | + if (*info == 0) { |
| 119 | + Lstore = (SCformat *)L->Store; |
| 120 | + Ustore = (NCformat *)U->Store; |
| 121 | + printf("No of nonzeros in factor L = %lld\n", (long long)Lstore->nnz); |
| 122 | + printf("No of nonzeros in factor U = %lld\n", (long long)Ustore->nnz); |
| 123 | + printf("No of nonzeros in L+U = %lld\n", |
| 124 | + (long long)Lstore->nnz + Ustore->nnz); |
| 125 | + dQuerySpace(L, U, &mem_usage); |
| 126 | + printf("L\\U MB %.3f\ttotal MB needed %.3f\n", mem_usage.for_lu / 1e6, |
| 127 | + mem_usage.total_needed / 1e6); |
| 128 | + } else { |
| 129 | + printf("dgstrf() error returns INFO= %lld\n", (long long)*info); |
| 130 | + if (*info <= *n) { /* factorization completes */ |
| 131 | + dQuerySpace(L, U, &mem_usage); |
| 132 | + printf("L\\U MB %.3f\ttotal MB needed %.3f\n", mem_usage.for_lu / 1e6, |
| 133 | + mem_usage.total_needed / 1e6); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /* Save the LU factors in the factors handle */ |
| 138 | + LUfactors = (factors_t *)SUPERLU_MALLOC(sizeof(factors_t)); |
| 139 | + LUfactors->L = L; |
| 140 | + LUfactors->U = U; |
| 141 | + LUfactors->perm_c = perm_c; |
| 142 | + LUfactors->perm_r = perm_r; |
| 143 | + *f_factors = (fptr)LUfactors; |
| 144 | + |
| 145 | + /* Free un-wanted storage */ |
| 146 | + SUPERLU_FREE(etree); |
| 147 | + Destroy_SuperMatrix_Store(&A); |
| 148 | + Destroy_CompCol_Permuted(&AC); |
| 149 | + SUPERLU_FREE(rowind0); |
| 150 | + SUPERLU_FREE(colptr0); |
| 151 | + StatFree(&stat); |
| 152 | + |
| 153 | + } else if (*iopt == 2) { /* Triangular solve */ |
| 154 | + int iinfo; |
| 155 | + |
| 156 | + /* Initialize the statistics variables. */ |
| 157 | + StatInit(&stat); |
| 158 | + |
| 159 | + /* Extract the LU factors in the factors handle */ |
| 160 | + LUfactors = (factors_t *)*f_factors; |
| 161 | + L = LUfactors->L; |
| 162 | + U = LUfactors->U; |
| 163 | + perm_c = LUfactors->perm_c; |
| 164 | + perm_r = LUfactors->perm_r; |
| 165 | + |
| 166 | + dCreate_Dense_Matrix(&B, *n, *nrhs, b, *ldb, SLU_DN, SLU_D, SLU_GE); |
| 167 | + |
| 168 | + /* Solve the system A*X=B, overwriting B with X. */ |
| 169 | + dgstrs(trans, L, U, perm_c, perm_r, &B, &stat, &iinfo); |
| 170 | + *info = iinfo; |
| 171 | + |
| 172 | + Destroy_SuperMatrix_Store(&B); |
| 173 | + StatFree(&stat); |
| 174 | + |
| 175 | + } else if (*iopt == 3) { /* Free storage */ |
| 176 | + /* Free the LU factors in the factors handle */ |
| 177 | + LUfactors = (factors_t *)*f_factors; |
| 178 | + SUPERLU_FREE(LUfactors->perm_r); |
| 179 | + SUPERLU_FREE(LUfactors->perm_c); |
| 180 | + Destroy_SuperNode_Matrix(LUfactors->L); |
| 181 | + Destroy_CompCol_Matrix(LUfactors->U); |
| 182 | + SUPERLU_FREE(LUfactors->L); |
| 183 | + SUPERLU_FREE(LUfactors->U); |
| 184 | + SUPERLU_FREE(LUfactors); |
| 185 | + } else { |
| 186 | + fprintf(stderr, "Invalid iopt=%d passed to c_fortran_dgssv()\n", *iopt); |
| 187 | + exit(-1); |
| 188 | + } |
| 189 | +} |
0 commit comments