Actual source code: aijcholmod.c

      1: #include <../src/mat/impls/aij/seq/aij.h>
      2: #include <../src/mat/impls/sbaij/seq/cholmod/cholmodimpl.h>

      4: static PetscErrorCode MatWrapCholmod_seqaij(Mat A, PetscBool values, cholmod_sparse *C, PetscBool *aijalloc, PetscBool *valloc)
      5: {
      6:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ *)A->data;
      7:   const PetscScalar *aa;
      8:   PetscScalar       *ca;
      9:   const PetscInt    *ai = aij->i, *aj = aij->j, *adiag;
     10:   PetscInt           m    = A->rmap->n, i, j, k, nz, *ci, *cj;
     11:   PetscBool          vain = PETSC_FALSE;

     13:   PetscFunctionBegin;
     14:   PetscCall(MatMarkDiagonal_SeqAIJ(A));
     15:   adiag = aij->diag;
     16:   for (i = 0, nz = 0; i < m; i++) nz += ai[i + 1] - adiag[i];
     17:   PetscCall(PetscMalloc2(m + 1, &ci, nz, &cj));
     18:   if (values) {
     19:     vain = PETSC_TRUE;
     20:     PetscCall(PetscMalloc1(nz, &ca));
     21:     PetscCall(MatSeqAIJGetArrayRead(A, &aa));
     22:   }
     23:   for (i = 0, k = 0; i < m; i++) {
     24:     ci[i] = k;
     25:     for (j = adiag[i]; j < ai[i + 1]; j++, k++) {
     26:       cj[k] = aj[j];
     27:       if (values) ca[k] = PetscConj(aa[j]);
     28:     }
     29:   }
     30:   ci[i]     = k;
     31:   *aijalloc = PETSC_TRUE;
     32:   *valloc   = vain;
     33:   if (values) PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));

     35:   PetscCall(PetscMemzero(C, sizeof(*C)));

     37:   C->nrow   = (size_t)A->cmap->n;
     38:   C->ncol   = (size_t)A->rmap->n;
     39:   C->nzmax  = (size_t)nz;
     40:   C->p      = ci;
     41:   C->i      = cj;
     42:   C->x      = values ? ca : 0;
     43:   C->stype  = -1;
     44:   C->itype  = CHOLMOD_INT_TYPE;
     45:   C->xtype  = values ? CHOLMOD_SCALAR_TYPE : CHOLMOD_PATTERN;
     46:   C->dtype  = CHOLMOD_DOUBLE;
     47:   C->sorted = 1;
     48:   C->packed = 1;
     49:   PetscFunctionReturn(PETSC_SUCCESS);
     50: }

     52: static PetscErrorCode MatFactorGetSolverType_seqaij_cholmod(Mat A, MatSolverType *type)
     53: {
     54:   PetscFunctionBegin;
     55:   *type = MATSOLVERCHOLMOD;
     56:   PetscFunctionReturn(PETSC_SUCCESS);
     57: }

     59: /* Almost a copy of MatGetFactor_seqsbaij_cholmod, yuck */
     60: PETSC_INTERN PetscErrorCode MatGetFactor_seqaij_cholmod(Mat A, MatFactorType ftype, Mat *F)
     61: {
     62:   Mat          B;
     63:   Mat_CHOLMOD *chol;
     64:   PetscInt     m = A->rmap->n, n = A->cmap->n;

     66:   PetscFunctionBegin;
     67: #if defined(PETSC_USE_COMPLEX)
     68:   if (A->hermitian != PETSC_BOOL3_TRUE) {
     69:     PetscCall(PetscInfo(A, "Only for Hermitian matrices.\n"));
     70:     *F = NULL;
     71:     PetscFunctionReturn(PETSC_SUCCESS);
     72:   }
     73: #endif
     74:   /* Create the factorization matrix F */
     75:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
     76:   PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, m, n));
     77:   PetscCall(PetscStrallocpy("cholmod", &((PetscObject)B)->type_name));
     78:   PetscCall(MatSetUp(B));
     79:   PetscCall(PetscNew(&chol));

     81:   chol->Wrap = MatWrapCholmod_seqaij;
     82:   B->data    = chol;

     84:   B->ops->getinfo                = MatGetInfo_CHOLMOD;
     85:   B->ops->view                   = MatView_CHOLMOD;
     86:   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_CHOLMOD;
     87:   B->ops->destroy                = MatDestroy_CHOLMOD;

     89:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_seqaij_cholmod));

     91:   B->factortype   = MAT_FACTOR_CHOLESKY;
     92:   B->assembled    = PETSC_TRUE;
     93:   B->preallocated = PETSC_TRUE;

     95:   PetscCall(PetscFree(B->solvertype));
     96:   PetscCall(PetscStrallocpy(MATSOLVERCHOLMOD, &B->solvertype));
     97:   B->canuseordering = PETSC_TRUE;
     98:   PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&B->preferredordering[MAT_FACTOR_CHOLESKY]));
     99:   PetscCall(CholmodStart(B));
    100:   *F = B;
    101:   PetscFunctionReturn(PETSC_SUCCESS);
    102: }