12c733ed4SBarry Smith #include <../src/mat/impls/baij/seq/baij.h> 22c733ed4SBarry Smith 3d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSolveTranspose_SeqBAIJ_3_NaturalOrdering_inplace(Mat A, Vec bb, Vec xx) 4d71ae5a4SJacob Faibussowitsch { 52c733ed4SBarry Smith Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data; 62c733ed4SBarry Smith const PetscInt n = a->mbs, *vi, *ai = a->i, *aj = a->j, *diag = a->diag; 72c733ed4SBarry Smith PetscInt i, nz, idx, idt, oidx; 82c733ed4SBarry Smith const MatScalar *aa = a->a, *v; 92c733ed4SBarry Smith PetscScalar s1, s2, s3, x1, x2, x3, *x; 102c733ed4SBarry Smith 112c733ed4SBarry Smith PetscFunctionBegin; 129566063dSJacob Faibussowitsch PetscCall(VecCopy(bb, xx)); 139566063dSJacob Faibussowitsch PetscCall(VecGetArray(xx, &x)); 142c733ed4SBarry Smith 152c733ed4SBarry Smith /* forward solve the U^T */ 162c733ed4SBarry Smith idx = 0; 172c733ed4SBarry Smith for (i = 0; i < n; i++) { 182c733ed4SBarry Smith v = aa + 9 * diag[i]; 192c733ed4SBarry Smith /* multiply by the inverse of the block diagonal */ 209371c9d4SSatish Balay x1 = x[idx]; 219371c9d4SSatish Balay x2 = x[1 + idx]; 229371c9d4SSatish Balay x3 = x[2 + idx]; 232c733ed4SBarry Smith s1 = v[0] * x1 + v[1] * x2 + v[2] * x3; 242c733ed4SBarry Smith s2 = v[3] * x1 + v[4] * x2 + v[5] * x3; 252c733ed4SBarry Smith s3 = v[6] * x1 + v[7] * x2 + v[8] * x3; 262c733ed4SBarry Smith v += 9; 272c733ed4SBarry Smith 282c733ed4SBarry Smith vi = aj + diag[i] + 1; 292c733ed4SBarry Smith nz = ai[i + 1] - diag[i] - 1; 302c733ed4SBarry Smith while (nz--) { 312c733ed4SBarry Smith oidx = 3 * (*vi++); 322c733ed4SBarry Smith x[oidx] -= v[0] * s1 + v[1] * s2 + v[2] * s3; 332c733ed4SBarry Smith x[oidx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3; 342c733ed4SBarry Smith x[oidx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3; 352c733ed4SBarry Smith v += 9; 362c733ed4SBarry Smith } 379371c9d4SSatish Balay x[idx] = s1; 389371c9d4SSatish Balay x[1 + idx] = s2; 399371c9d4SSatish Balay x[2 + idx] = s3; 402c733ed4SBarry Smith idx += 3; 412c733ed4SBarry Smith } 422c733ed4SBarry Smith /* backward solve the L^T */ 432c733ed4SBarry Smith for (i = n - 1; i >= 0; i--) { 442c733ed4SBarry Smith v = aa + 9 * diag[i] - 9; 452c733ed4SBarry Smith vi = aj + diag[i] - 1; 462c733ed4SBarry Smith nz = diag[i] - ai[i]; 472c733ed4SBarry Smith idt = 3 * i; 489371c9d4SSatish Balay s1 = x[idt]; 499371c9d4SSatish Balay s2 = x[1 + idt]; 509371c9d4SSatish Balay s3 = x[2 + idt]; 512c733ed4SBarry Smith while (nz--) { 522c733ed4SBarry Smith idx = 3 * (*vi--); 532c733ed4SBarry Smith x[idx] -= v[0] * s1 + v[1] * s2 + v[2] * s3; 542c733ed4SBarry Smith x[idx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3; 552c733ed4SBarry Smith x[idx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3; 562c733ed4SBarry Smith v -= 9; 572c733ed4SBarry Smith } 582c733ed4SBarry Smith } 599566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx, &x)); 609566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * 9.0 * (a->nz) - 3.0 * A->cmap->n)); 61*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 622c733ed4SBarry Smith } 632c733ed4SBarry Smith 64d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSolveTranspose_SeqBAIJ_3_NaturalOrdering(Mat A, Vec bb, Vec xx) 65d71ae5a4SJacob Faibussowitsch { 662c733ed4SBarry Smith Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data; 672c733ed4SBarry Smith const PetscInt n = a->mbs, *vi, *ai = a->i, *aj = a->j, *diag = a->diag; 682c733ed4SBarry Smith PetscInt nz, idx, idt, j, i, oidx; 692c733ed4SBarry Smith const PetscInt bs = A->rmap->bs, bs2 = a->bs2; 702c733ed4SBarry Smith const MatScalar *aa = a->a, *v; 712c733ed4SBarry Smith PetscScalar s1, s2, s3, x1, x2, x3, *x; 722c733ed4SBarry Smith 732c733ed4SBarry Smith PetscFunctionBegin; 749566063dSJacob Faibussowitsch PetscCall(VecCopy(bb, xx)); 759566063dSJacob Faibussowitsch PetscCall(VecGetArray(xx, &x)); 762c733ed4SBarry Smith 772c733ed4SBarry Smith /* forward solve the U^T */ 782c733ed4SBarry Smith idx = 0; 792c733ed4SBarry Smith for (i = 0; i < n; i++) { 802c733ed4SBarry Smith v = aa + bs2 * diag[i]; 812c733ed4SBarry Smith /* multiply by the inverse of the block diagonal */ 829371c9d4SSatish Balay x1 = x[idx]; 839371c9d4SSatish Balay x2 = x[1 + idx]; 849371c9d4SSatish Balay x3 = x[2 + idx]; 852c733ed4SBarry Smith s1 = v[0] * x1 + v[1] * x2 + v[2] * x3; 862c733ed4SBarry Smith s2 = v[3] * x1 + v[4] * x2 + v[5] * x3; 872c733ed4SBarry Smith s3 = v[6] * x1 + v[7] * x2 + v[8] * x3; 882c733ed4SBarry Smith v -= bs2; 892c733ed4SBarry Smith 902c733ed4SBarry Smith vi = aj + diag[i] - 1; 912c733ed4SBarry Smith nz = diag[i] - diag[i + 1] - 1; 922c733ed4SBarry Smith for (j = 0; j > -nz; j--) { 932c733ed4SBarry Smith oidx = bs * vi[j]; 942c733ed4SBarry Smith x[oidx] -= v[0] * s1 + v[1] * s2 + v[2] * s3; 952c733ed4SBarry Smith x[oidx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3; 962c733ed4SBarry Smith x[oidx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3; 972c733ed4SBarry Smith v -= bs2; 982c733ed4SBarry Smith } 999371c9d4SSatish Balay x[idx] = s1; 1009371c9d4SSatish Balay x[1 + idx] = s2; 1019371c9d4SSatish Balay x[2 + idx] = s3; 1022c733ed4SBarry Smith idx += bs; 1032c733ed4SBarry Smith } 1042c733ed4SBarry Smith /* backward solve the L^T */ 1052c733ed4SBarry Smith for (i = n - 1; i >= 0; i--) { 1062c733ed4SBarry Smith v = aa + bs2 * ai[i]; 1072c733ed4SBarry Smith vi = aj + ai[i]; 1082c733ed4SBarry Smith nz = ai[i + 1] - ai[i]; 1092c733ed4SBarry Smith idt = bs * i; 1109371c9d4SSatish Balay s1 = x[idt]; 1119371c9d4SSatish Balay s2 = x[1 + idt]; 1129371c9d4SSatish Balay s3 = x[2 + idt]; 1132c733ed4SBarry Smith for (j = 0; j < nz; j++) { 1142c733ed4SBarry Smith idx = bs * vi[j]; 1152c733ed4SBarry Smith x[idx] -= v[0] * s1 + v[1] * s2 + v[2] * s3; 1162c733ed4SBarry Smith x[idx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3; 1172c733ed4SBarry Smith x[idx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3; 1182c733ed4SBarry Smith v += bs2; 1192c733ed4SBarry Smith } 1202c733ed4SBarry Smith } 1219566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx, &x)); 1229566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * bs2 * (a->nz) - bs * A->cmap->n)); 123*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1242c733ed4SBarry Smith } 125