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