12c733ed4SBarry Smith #include <../src/mat/impls/baij/seq/baij.h> 22c733ed4SBarry Smith 32c733ed4SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_1_NaturalOrdering(Mat A,Vec bb,Vec xx) 42c733ed4SBarry Smith { 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; 14*9566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(bb,&b)); 15*9566063dSJacob 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 44*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(bb,&b)); 45*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx,&x)); 462c733ed4SBarry Smith 47*9566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0*a->nz-A->cmap->n)); 482c733ed4SBarry Smith PetscFunctionReturn(0); 492c733ed4SBarry Smith } 502c733ed4SBarry Smith 512c733ed4SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_1_NaturalOrdering_inplace(Mat A,Vec bb,Vec xx) 522c733ed4SBarry Smith { 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; 60*9566063dSJacob Faibussowitsch PetscCall(VecCopy(bb,xx)); 61*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(xx,&x)); 622c733ed4SBarry Smith 632c733ed4SBarry Smith /* forward solve the U^T */ 642c733ed4SBarry Smith for (i=0; i<n; i++) { 652c733ed4SBarry Smith 662c733ed4SBarry Smith v = aa + diag[i]; 672c733ed4SBarry Smith /* multiply by the inverse of the block diagonal */ 682c733ed4SBarry Smith s1 = (*v++)*x[i]; 692c733ed4SBarry Smith vi = aj + diag[i] + 1; 702c733ed4SBarry Smith nz = ai[i+1] - diag[i] - 1; 712c733ed4SBarry Smith while (nz--) { 722c733ed4SBarry Smith x[*vi++] -= (*v++)*s1; 732c733ed4SBarry Smith } 742c733ed4SBarry Smith x[i] = s1; 752c733ed4SBarry Smith } 762c733ed4SBarry Smith /* backward solve the L^T */ 772c733ed4SBarry Smith for (i=n-1; i>=0; i--) { 782c733ed4SBarry Smith v = aa + diag[i] - 1; 792c733ed4SBarry Smith vi = aj + diag[i] - 1; 802c733ed4SBarry Smith nz = diag[i] - ai[i]; 812c733ed4SBarry Smith s1 = x[i]; 822c733ed4SBarry Smith while (nz--) { 832c733ed4SBarry Smith x[*vi--] -= (*v--)*s1; 842c733ed4SBarry Smith } 852c733ed4SBarry Smith } 86*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xx,&x)); 87*9566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0*(a->nz) - A->cmap->n)); 882c733ed4SBarry Smith PetscFunctionReturn(0); 892c733ed4SBarry Smith } 90