12c733ed4SBarry Smith #include <../src/mat/impls/baij/seq/baij.h> 22c733ed4SBarry Smith 3*9371c9d4SSatish Balay PetscErrorCode MatSolveTranspose_SeqBAIJ_2_NaturalOrdering_inplace(Mat A, Vec bb, Vec xx) { 42c733ed4SBarry Smith Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data; 52c733ed4SBarry Smith PetscInt i, nz, idx, idt, oidx; 62c733ed4SBarry Smith const PetscInt *diag = a->diag, *vi, n = a->mbs, *ai = a->i, *aj = a->j; 72c733ed4SBarry Smith const MatScalar *aa = a->a, *v; 82c733ed4SBarry Smith PetscScalar s1, s2, x1, x2, *x; 92c733ed4SBarry Smith 102c733ed4SBarry Smith PetscFunctionBegin; 119566063dSJacob Faibussowitsch PetscCall(VecCopy(bb, xx)); 129566063dSJacob Faibussowitsch PetscCall(VecGetArray(xx, &x)); 132c733ed4SBarry Smith 142c733ed4SBarry Smith /* forward solve the U^T */ 152c733ed4SBarry Smith idx = 0; 162c733ed4SBarry Smith for (i = 0; i < n; i++) { 172c733ed4SBarry Smith v = aa + 4 * diag[i]; 182c733ed4SBarry Smith /* multiply by the inverse of the block diagonal */ 19*9371c9d4SSatish Balay x1 = x[idx]; 20*9371c9d4SSatish Balay x2 = x[1 + idx]; 212c733ed4SBarry Smith s1 = v[0] * x1 + v[1] * x2; 222c733ed4SBarry Smith s2 = v[2] * x1 + v[3] * x2; 232c733ed4SBarry Smith v += 4; 242c733ed4SBarry Smith 252c733ed4SBarry Smith vi = aj + diag[i] + 1; 262c733ed4SBarry Smith nz = ai[i + 1] - diag[i] - 1; 272c733ed4SBarry Smith while (nz--) { 282c733ed4SBarry Smith oidx = 2 * (*vi++); 292c733ed4SBarry Smith x[oidx] -= v[0] * s1 + v[1] * s2; 302c733ed4SBarry Smith x[oidx + 1] -= v[2] * s1 + v[3] * s2; 312c733ed4SBarry Smith v += 4; 322c733ed4SBarry Smith } 33*9371c9d4SSatish Balay x[idx] = s1; 34*9371c9d4SSatish Balay x[1 + idx] = s2; 352c733ed4SBarry Smith idx += 2; 362c733ed4SBarry Smith } 372c733ed4SBarry Smith /* backward solve the L^T */ 382c733ed4SBarry Smith for (i = n - 1; i >= 0; i--) { 392c733ed4SBarry Smith v = aa + 4 * diag[i] - 4; 402c733ed4SBarry Smith vi = aj + diag[i] - 1; 412c733ed4SBarry Smith nz = diag[i] - ai[i]; 422c733ed4SBarry Smith idt = 2 * i; 43*9371c9d4SSatish Balay s1 = x[idt]; 44*9371c9d4SSatish Balay s2 = x[1 + idt]; 452c733ed4SBarry Smith while (nz--) { 462c733ed4SBarry Smith idx = 2 * (*vi--); 472c733ed4SBarry Smith x[idx] -= v[0] * s1 + v[1] * s2; 482c733ed4SBarry Smith x[idx + 1] -= v[2] * s1 + v[3] * s2; 492c733ed4SBarry Smith v -= 4; 502c733ed4SBarry Smith } 512c733ed4SBarry Smith } 529566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx, &x)); 539566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * 4.0 * (a->nz) - 2.0 * A->cmap->n)); 542c733ed4SBarry Smith PetscFunctionReturn(0); 552c733ed4SBarry Smith } 562c733ed4SBarry Smith 57*9371c9d4SSatish Balay PetscErrorCode MatSolveTranspose_SeqBAIJ_2_NaturalOrdering(Mat A, Vec bb, Vec xx) { 582c733ed4SBarry Smith Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data; 592c733ed4SBarry Smith const PetscInt n = a->mbs, *vi, *ai = a->i, *aj = a->j, *diag = a->diag; 602c733ed4SBarry Smith PetscInt nz, idx, idt, j, i, oidx; 612c733ed4SBarry Smith const PetscInt bs = A->rmap->bs, bs2 = a->bs2; 622c733ed4SBarry Smith const MatScalar *aa = a->a, *v; 632c733ed4SBarry Smith PetscScalar s1, s2, x1, x2, *x; 642c733ed4SBarry Smith 652c733ed4SBarry Smith PetscFunctionBegin; 669566063dSJacob Faibussowitsch PetscCall(VecCopy(bb, xx)); 679566063dSJacob Faibussowitsch PetscCall(VecGetArray(xx, &x)); 682c733ed4SBarry Smith 692c733ed4SBarry Smith /* forward solve the U^T */ 702c733ed4SBarry Smith idx = 0; 712c733ed4SBarry Smith for (i = 0; i < n; i++) { 722c733ed4SBarry Smith v = aa + bs2 * diag[i]; 732c733ed4SBarry Smith /* multiply by the inverse of the block diagonal */ 74*9371c9d4SSatish Balay x1 = x[idx]; 75*9371c9d4SSatish Balay x2 = x[1 + idx]; 762c733ed4SBarry Smith s1 = v[0] * x1 + v[1] * x2; 772c733ed4SBarry Smith s2 = v[2] * x1 + v[3] * x2; 782c733ed4SBarry Smith v -= bs2; 792c733ed4SBarry Smith 802c733ed4SBarry Smith vi = aj + diag[i] - 1; 812c733ed4SBarry Smith nz = diag[i] - diag[i + 1] - 1; 822c733ed4SBarry Smith for (j = 0; j > -nz; j--) { 832c733ed4SBarry Smith oidx = bs * vi[j]; 842c733ed4SBarry Smith x[oidx] -= v[0] * s1 + v[1] * s2; 852c733ed4SBarry Smith x[oidx + 1] -= v[2] * s1 + v[3] * s2; 862c733ed4SBarry Smith v -= bs2; 872c733ed4SBarry Smith } 88*9371c9d4SSatish Balay x[idx] = s1; 89*9371c9d4SSatish Balay x[1 + idx] = s2; 902c733ed4SBarry Smith idx += bs; 912c733ed4SBarry Smith } 922c733ed4SBarry Smith /* backward solve the L^T */ 932c733ed4SBarry Smith for (i = n - 1; i >= 0; i--) { 942c733ed4SBarry Smith v = aa + bs2 * ai[i]; 952c733ed4SBarry Smith vi = aj + ai[i]; 962c733ed4SBarry Smith nz = ai[i + 1] - ai[i]; 972c733ed4SBarry Smith idt = bs * i; 98*9371c9d4SSatish Balay s1 = x[idt]; 99*9371c9d4SSatish Balay s2 = x[1 + idt]; 1002c733ed4SBarry Smith for (j = 0; j < nz; j++) { 1012c733ed4SBarry Smith idx = bs * vi[j]; 1022c733ed4SBarry Smith x[idx] -= v[0] * s1 + v[1] * s2; 1032c733ed4SBarry Smith x[idx + 1] -= v[2] * s1 + v[3] * s2; 1042c733ed4SBarry Smith v += bs2; 1052c733ed4SBarry Smith } 1062c733ed4SBarry Smith } 1079566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx, &x)); 1089566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * bs2 * (a->nz) - bs * A->cmap->n)); 1092c733ed4SBarry Smith PetscFunctionReturn(0); 1102c733ed4SBarry Smith } 111