xref: /petsc/src/mat/impls/baij/seq/baijfact4.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
1be1d678aSKris Buschelman 
283287d42SBarry Smith /*
383287d42SBarry Smith     Factorization code for BAIJ format.
483287d42SBarry Smith */
5c6db04a5SJed Brown #include <../src/mat/impls/baij/seq/baij.h>
6af0996ceSBarry Smith #include <petsc/private/kernels/blockinvert.h>
783287d42SBarry Smith 
883287d42SBarry Smith /* ----------------------------------------------------------- */
99371c9d4SSatish Balay PetscErrorCode MatLUFactorNumeric_SeqBAIJ_N_inplace(Mat C, Mat A, const MatFactorInfo *info) {
1083287d42SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data, *b = (Mat_SeqBAIJ *)C->data;
1183287d42SBarry Smith   IS              isrow = b->row, isicol = b->icol;
125d0c19d7SBarry Smith   const PetscInt *r, *ic;
135d0c19d7SBarry Smith   PetscInt        i, j, n = a->mbs, *bi = b->i, *bj = b->j;
143bc0b13bSBarry Smith   PetscInt       *ajtmpold, *ajtmp, nz, row, *ai = a->i, *aj = a->j, k, flg;
15d0f46423SBarry Smith   PetscInt       *diag_offset = b->diag, diag, bs = A->rmap->bs, bs2 = a->bs2, *pj, *v_pivots;
1683287d42SBarry Smith   MatScalar      *ba = b->a, *aa = a->a, *pv, *v, *rtmp, *multiplier, *v_work, *pc, *w;
175f8bbccaSHong Zhang   PetscBool       allowzeropivot, zeropivotdetected;
1883287d42SBarry Smith 
1983287d42SBarry Smith   PetscFunctionBegin;
209566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(isrow, &r));
219566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(isicol, &ic));
225f8bbccaSHong Zhang   allowzeropivot = PetscNot(A->erroriffailure);
235f8bbccaSHong Zhang 
249566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(bs2 * (n + 1), &rtmp));
2583287d42SBarry Smith   /* generate work space needed by dense LU factorization */
269566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(bs, &v_work, bs2, &multiplier, bs, &v_pivots));
2783287d42SBarry Smith 
2883287d42SBarry Smith   for (i = 0; i < n; i++) {
2983287d42SBarry Smith     nz    = bi[i + 1] - bi[i];
3083287d42SBarry Smith     ajtmp = bj + bi[i];
31*48a46eb9SPierre Jolivet     for (j = 0; j < nz; j++) PetscCall(PetscArrayzero(rtmp + bs2 * ajtmp[j], bs2));
3283287d42SBarry Smith     /* load in initial (unfactored row) */
3383287d42SBarry Smith     nz       = ai[r[i] + 1] - ai[r[i]];
3483287d42SBarry Smith     ajtmpold = aj + ai[r[i]];
3583287d42SBarry Smith     v        = aa + bs2 * ai[r[i]];
36*48a46eb9SPierre Jolivet     for (j = 0; j < nz; j++) PetscCall(PetscArraycpy(rtmp + bs2 * ic[ajtmpold[j]], v + bs2 * j, bs2));
3783287d42SBarry Smith     row = *ajtmp++;
3883287d42SBarry Smith     while (row < i) {
3983287d42SBarry Smith       pc = rtmp + bs2 * row;
4083287d42SBarry Smith       /*      if (*pc) { */
41c35f09e5SBarry Smith       for (flg = 0, k = 0; k < bs2; k++) {
42c35f09e5SBarry Smith         if (pc[k] != 0.0) {
43c35f09e5SBarry Smith           flg = 1;
44c35f09e5SBarry Smith           break;
45c35f09e5SBarry Smith         }
46c35f09e5SBarry Smith       }
4783287d42SBarry Smith       if (flg) {
4883287d42SBarry Smith         pv = ba + bs2 * diag_offset[row];
4983287d42SBarry Smith         pj = bj + diag_offset[row] + 1;
5096b95a6bSBarry Smith         PetscKernel_A_gets_A_times_B(bs, pc, pv, multiplier);
5183287d42SBarry Smith         nz = bi[row + 1] - diag_offset[row] - 1;
5283287d42SBarry Smith         pv += bs2;
539371c9d4SSatish Balay         for (j = 0; j < nz; j++) { PetscKernel_A_gets_A_minus_B_times_C(bs, rtmp + bs2 * pj[j], pc, pv + bs2 * j); }
549566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * bs * bs2 * (nz + 1.0) - bs));
5583287d42SBarry Smith       }
5683287d42SBarry Smith       row = *ajtmp++;
5783287d42SBarry Smith     }
5883287d42SBarry Smith     /* finished row so stick it into b->a */
5983287d42SBarry Smith     pv = ba + bs2 * bi[i];
6083287d42SBarry Smith     pj = bj + bi[i];
6183287d42SBarry Smith     nz = bi[i + 1] - bi[i];
62*48a46eb9SPierre Jolivet     for (j = 0; j < nz; j++) PetscCall(PetscArraycpy(pv + bs2 * j, rtmp + bs2 * pj[j], bs2));
6383287d42SBarry Smith     diag = diag_offset[i] - bi[i];
6483287d42SBarry Smith     /* invert diagonal block */
6583287d42SBarry Smith     w    = pv + bs2 * diag;
665f8bbccaSHong Zhang 
679566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A(bs, w, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
687b6c816cSBarry Smith     if (zeropivotdetected) C->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
6983287d42SBarry Smith   }
7083287d42SBarry Smith 
719566063dSJacob Faibussowitsch   PetscCall(PetscFree(rtmp));
729566063dSJacob Faibussowitsch   PetscCall(PetscFree3(v_work, multiplier, v_pivots));
739566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(isicol, &ic));
749566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(isrow, &r));
7526fbe8dcSKarl Rupp 
7606e38f1dSHong Zhang   C->ops->solve          = MatSolve_SeqBAIJ_N_inplace;
7706e38f1dSHong Zhang   C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_N_inplace;
7883287d42SBarry Smith   C->assembled           = PETSC_TRUE;
7926fbe8dcSKarl Rupp 
809566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(1.333333333333 * bs * bs2 * b->mbs)); /* from inverting diagonal blocks */
8183287d42SBarry Smith   PetscFunctionReturn(0);
8283287d42SBarry Smith }
83