xref: /petsc/src/mat/impls/baij/seq/baijfact4.c (revision d5b43468fb8780a8feea140ccd6fa3e6a50411cc)
1 
2 /*
3     Factorization code for BAIJ format.
4 */
5 #include <../src/mat/impls/baij/seq/baij.h>
6 #include <petsc/private/kernels/blockinvert.h>
7 
8 /* ----------------------------------------------------------- */
9 PetscErrorCode MatLUFactorNumeric_SeqBAIJ_N_inplace(Mat C, Mat A, const MatFactorInfo *info)
10 {
11   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data, *b = (Mat_SeqBAIJ *)C->data;
12   IS              isrow = b->row, isicol = b->icol;
13   const PetscInt *r, *ic;
14   PetscInt        i, j, n = a->mbs, *bi = b->i, *bj = b->j;
15   PetscInt       *ajtmpold, *ajtmp, nz, row, *ai = a->i, *aj = a->j, k, flg;
16   PetscInt       *diag_offset = b->diag, diag, bs = A->rmap->bs, bs2 = a->bs2, *pj, *v_pivots;
17   MatScalar      *ba = b->a, *aa = a->a, *pv, *v, *rtmp, *multiplier, *v_work, *pc, *w;
18   PetscBool       allowzeropivot, zeropivotdetected;
19 
20   PetscFunctionBegin;
21   PetscCall(ISGetIndices(isrow, &r));
22   PetscCall(ISGetIndices(isicol, &ic));
23   allowzeropivot = PetscNot(A->erroriffailure);
24 
25   PetscCall(PetscCalloc1(bs2 * (n + 1), &rtmp));
26   /* generate work space needed by dense LU factorization */
27   PetscCall(PetscMalloc3(bs, &v_work, bs2, &multiplier, bs, &v_pivots));
28 
29   for (i = 0; i < n; i++) {
30     nz    = bi[i + 1] - bi[i];
31     ajtmp = bj + bi[i];
32     for (j = 0; j < nz; j++) PetscCall(PetscArrayzero(rtmp + bs2 * ajtmp[j], bs2));
33     /* load in initial (unfactored row) */
34     nz       = ai[r[i] + 1] - ai[r[i]];
35     ajtmpold = aj + ai[r[i]];
36     v        = aa + bs2 * ai[r[i]];
37     for (j = 0; j < nz; j++) PetscCall(PetscArraycpy(rtmp + bs2 * ic[ajtmpold[j]], v + bs2 * j, bs2));
38     row = *ajtmp++;
39     while (row < i) {
40       pc = rtmp + bs2 * row;
41       /*      if (*pc) { */
42       for (flg = 0, k = 0; k < bs2; k++) {
43         if (pc[k] != 0.0) {
44           flg = 1;
45           break;
46         }
47       }
48       if (flg) {
49         pv = ba + bs2 * diag_offset[row];
50         pj = bj + diag_offset[row] + 1;
51         PetscKernel_A_gets_A_times_B(bs, pc, pv, multiplier);
52         nz = bi[row + 1] - diag_offset[row] - 1;
53         pv += bs2;
54         for (j = 0; j < nz; j++) PetscKernel_A_gets_A_minus_B_times_C(bs, rtmp + bs2 * pj[j], pc, pv + bs2 * j);
55         PetscCall(PetscLogFlops(2.0 * bs * bs2 * (nz + 1.0) - bs));
56       }
57       row = *ajtmp++;
58     }
59     /* finished row so stick it into b->a */
60     pv = ba + bs2 * bi[i];
61     pj = bj + bi[i];
62     nz = bi[i + 1] - bi[i];
63     for (j = 0; j < nz; j++) PetscCall(PetscArraycpy(pv + bs2 * j, rtmp + bs2 * pj[j], bs2));
64     diag = diag_offset[i] - bi[i];
65     /* invert diagonal block */
66     w = pv + bs2 * diag;
67 
68     PetscCall(PetscKernel_A_gets_inverse_A(bs, w, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
69     if (zeropivotdetected) C->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
70   }
71 
72   PetscCall(PetscFree(rtmp));
73   PetscCall(PetscFree3(v_work, multiplier, v_pivots));
74   PetscCall(ISRestoreIndices(isicol, &ic));
75   PetscCall(ISRestoreIndices(isrow, &r));
76 
77   C->ops->solve          = MatSolve_SeqBAIJ_N_inplace;
78   C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_N_inplace;
79   C->assembled           = PETSC_TRUE;
80 
81   PetscCall(PetscLogFlops(1.333333333333 * bs * bs2 * b->mbs)); /* from inverting diagonal blocks */
82   PetscFunctionReturn(0);
83 }
84