12c733ed4SBarry Smith #include <../src/mat/impls/baij/seq/baij.h>
22c733ed4SBarry Smith
MatSolveTranspose_SeqBAIJ_1_NaturalOrdering(Mat A,Vec bb,Vec xx)3d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSolveTranspose_SeqBAIJ_1_NaturalOrdering(Mat A, Vec bb, Vec xx)
4d71ae5a4SJacob Faibussowitsch {
52c733ed4SBarry Smith Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data;
62c733ed4SBarry Smith const PetscInt *adiag = a->diag, *ai = a->i, *aj = a->j, *vi;
72c733ed4SBarry Smith PetscInt i, n = a->mbs, j;
82c733ed4SBarry Smith PetscInt nz;
92c733ed4SBarry Smith PetscScalar *x, *tmp, s1;
102c733ed4SBarry Smith const MatScalar *aa = a->a, *v;
112c733ed4SBarry Smith const PetscScalar *b;
122c733ed4SBarry Smith
132c733ed4SBarry Smith PetscFunctionBegin;
149566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(bb, &b));
159566063dSJacob Faibussowitsch PetscCall(VecGetArray(xx, &x));
162c733ed4SBarry Smith tmp = a->solve_work;
172c733ed4SBarry Smith
182c733ed4SBarry Smith /* copy the b into temp work space according to permutation */
192c733ed4SBarry Smith for (i = 0; i < n; i++) tmp[i] = b[i];
202c733ed4SBarry Smith
212c733ed4SBarry Smith /* forward solve the U^T */
222c733ed4SBarry Smith for (i = 0; i < n; i++) {
232c733ed4SBarry Smith v = aa + adiag[i + 1] + 1;
242c733ed4SBarry Smith vi = aj + adiag[i + 1] + 1;
252c733ed4SBarry Smith nz = adiag[i] - adiag[i + 1] - 1;
262c733ed4SBarry Smith s1 = tmp[i];
272c733ed4SBarry Smith s1 *= v[nz]; /* multiply by inverse of diagonal entry */
282c733ed4SBarry Smith for (j = 0; j < nz; j++) tmp[vi[j]] -= s1 * v[j];
292c733ed4SBarry Smith tmp[i] = s1;
302c733ed4SBarry Smith }
312c733ed4SBarry Smith
322c733ed4SBarry Smith /* backward solve the L^T */
332c733ed4SBarry Smith for (i = n - 1; i >= 0; i--) {
342c733ed4SBarry Smith v = aa + ai[i];
352c733ed4SBarry Smith vi = aj + ai[i];
362c733ed4SBarry Smith nz = ai[i + 1] - ai[i];
372c733ed4SBarry Smith s1 = tmp[i];
382c733ed4SBarry Smith for (j = 0; j < nz; j++) tmp[vi[j]] -= s1 * v[j];
392c733ed4SBarry Smith }
402c733ed4SBarry Smith
412c733ed4SBarry Smith /* copy tmp into x according to permutation */
422c733ed4SBarry Smith for (i = 0; i < n; i++) x[i] = tmp[i];
432c733ed4SBarry Smith
449566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(bb, &b));
459566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx, &x));
462c733ed4SBarry Smith
479566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * a->nz - A->cmap->n));
48*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
492c733ed4SBarry Smith }
502c733ed4SBarry Smith
MatSolveTranspose_SeqBAIJ_1_NaturalOrdering_inplace(Mat A,Vec bb,Vec xx)51d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSolveTranspose_SeqBAIJ_1_NaturalOrdering_inplace(Mat A, Vec bb, Vec xx)
52d71ae5a4SJacob Faibussowitsch {
532c733ed4SBarry Smith Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data;
542c733ed4SBarry Smith PetscInt i, nz;
552c733ed4SBarry Smith const PetscInt *diag = a->diag, n = a->mbs, *vi, *ai = a->i, *aj = a->j;
562c733ed4SBarry Smith const MatScalar *aa = a->a, *v;
572c733ed4SBarry Smith PetscScalar s1, *x;
582c733ed4SBarry Smith
592c733ed4SBarry Smith PetscFunctionBegin;
609566063dSJacob Faibussowitsch PetscCall(VecCopy(bb, xx));
619566063dSJacob Faibussowitsch PetscCall(VecGetArray(xx, &x));
622c733ed4SBarry Smith
632c733ed4SBarry Smith /* forward solve the U^T */
642c733ed4SBarry Smith for (i = 0; i < n; i++) {
652c733ed4SBarry Smith v = aa + diag[i];
662c733ed4SBarry Smith /* multiply by the inverse of the block diagonal */
672c733ed4SBarry Smith s1 = (*v++) * x[i];
682c733ed4SBarry Smith vi = aj + diag[i] + 1;
692c733ed4SBarry Smith nz = ai[i + 1] - diag[i] - 1;
70ad540459SPierre Jolivet while (nz--) x[*vi++] -= (*v++) * s1;
712c733ed4SBarry Smith x[i] = s1;
722c733ed4SBarry Smith }
732c733ed4SBarry Smith /* backward solve the L^T */
742c733ed4SBarry Smith for (i = n - 1; i >= 0; i--) {
752c733ed4SBarry Smith v = aa + diag[i] - 1;
762c733ed4SBarry Smith vi = aj + diag[i] - 1;
772c733ed4SBarry Smith nz = diag[i] - ai[i];
782c733ed4SBarry Smith s1 = x[i];
79ad540459SPierre Jolivet while (nz--) x[*vi--] -= (*v--) * s1;
802c733ed4SBarry Smith }
819566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx, &x));
829566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * (a->nz) - A->cmap->n));
83*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
842c733ed4SBarry Smith }
85