xref: /petsc/src/mat/impls/baij/seq/baijsolvtran5.c (revision 9566063d113dddea24716c546802770db7481bc0)
12c733ed4SBarry Smith #include <../src/mat/impls/baij/seq/baij.h>
22c733ed4SBarry Smith #include <petsc/private/kernels/blockinvert.h>
32c733ed4SBarry Smith 
42c733ed4SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_5_inplace(Mat A,Vec bb,Vec xx)
52c733ed4SBarry Smith {
62c733ed4SBarry Smith   Mat_SeqBAIJ       *a   =(Mat_SeqBAIJ*)A->data;
72c733ed4SBarry Smith   IS                iscol=a->col,isrow=a->row;
82c733ed4SBarry Smith   const PetscInt    *r,*c,*rout,*cout;
92c733ed4SBarry Smith   const PetscInt    *diag=a->diag,n=a->mbs,*vi,*ai=a->i,*aj=a->j;
102c733ed4SBarry Smith   PetscInt          i,nz,idx,idt,ii,ic,ir,oidx;
112c733ed4SBarry Smith   const MatScalar   *aa=a->a,*v;
122c733ed4SBarry Smith   PetscScalar       s1,s2,s3,s4,s5,x1,x2,x3,x4,x5,*x,*t;
132c733ed4SBarry Smith   const PetscScalar *b;
142c733ed4SBarry Smith 
152c733ed4SBarry Smith   PetscFunctionBegin;
16*9566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(bb,&b));
17*9566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx,&x));
182c733ed4SBarry Smith   t    = a->solve_work;
192c733ed4SBarry Smith 
20*9566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(isrow,&rout)); r = rout;
21*9566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(iscol,&cout)); c = cout;
222c733ed4SBarry Smith 
232c733ed4SBarry Smith   /* copy the b into temp work space according to permutation */
242c733ed4SBarry Smith   ii = 0;
252c733ed4SBarry Smith   for (i=0; i<n; i++) {
262c733ed4SBarry Smith     ic      = 5*c[i];
272c733ed4SBarry Smith     t[ii]   = b[ic];
282c733ed4SBarry Smith     t[ii+1] = b[ic+1];
292c733ed4SBarry Smith     t[ii+2] = b[ic+2];
302c733ed4SBarry Smith     t[ii+3] = b[ic+3];
312c733ed4SBarry Smith     t[ii+4] = b[ic+4];
322c733ed4SBarry Smith     ii     += 5;
332c733ed4SBarry Smith   }
342c733ed4SBarry Smith 
352c733ed4SBarry Smith   /* forward solve the U^T */
362c733ed4SBarry Smith   idx = 0;
372c733ed4SBarry Smith   for (i=0; i<n; i++) {
382c733ed4SBarry Smith 
392c733ed4SBarry Smith     v = aa + 25*diag[i];
402c733ed4SBarry Smith     /* multiply by the inverse of the block diagonal */
412c733ed4SBarry Smith     x1 = t[idx];   x2 = t[1+idx]; x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
422c733ed4SBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5;
432c733ed4SBarry Smith     s2 = v[5]*x1  +  v[6]*x2 +  v[7]*x3 +  v[8]*x4 +  v[9]*x5;
442c733ed4SBarry Smith     s3 = v[10]*x1 + v[11]*x2 + v[12]*x3 + v[13]*x4 + v[14]*x5;
452c733ed4SBarry Smith     s4 = v[15]*x1 + v[16]*x2 + v[17]*x3 + v[18]*x4 + v[19]*x5;
462c733ed4SBarry Smith     s5 = v[20]*x1 + v[21]*x2 + v[22]*x3 + v[23]*x4 + v[24]*x5;
472c733ed4SBarry Smith     v += 25;
482c733ed4SBarry Smith 
492c733ed4SBarry Smith     vi = aj + diag[i] + 1;
502c733ed4SBarry Smith     nz = ai[i+1] - diag[i] - 1;
512c733ed4SBarry Smith     while (nz--) {
522c733ed4SBarry Smith       oidx       = 5*(*vi++);
532c733ed4SBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
542c733ed4SBarry Smith       t[oidx+1] -= v[5]*s1  +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
552c733ed4SBarry Smith       t[oidx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
562c733ed4SBarry Smith       t[oidx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
572c733ed4SBarry Smith       t[oidx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
582c733ed4SBarry Smith       v         += 25;
592c733ed4SBarry Smith     }
602c733ed4SBarry Smith     t[idx] = s1;t[1+idx] = s2; t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
612c733ed4SBarry Smith     idx   += 5;
622c733ed4SBarry Smith   }
632c733ed4SBarry Smith   /* backward solve the L^T */
642c733ed4SBarry Smith   for (i=n-1; i>=0; i--) {
652c733ed4SBarry Smith     v   = aa + 25*diag[i] - 25;
662c733ed4SBarry Smith     vi  = aj + diag[i] - 1;
672c733ed4SBarry Smith     nz  = diag[i] - ai[i];
682c733ed4SBarry Smith     idt = 5*i;
692c733ed4SBarry Smith     s1  = t[idt];  s2 = t[1+idt]; s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
702c733ed4SBarry Smith     while (nz--) {
712c733ed4SBarry Smith       idx       = 5*(*vi--);
722c733ed4SBarry Smith       t[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
732c733ed4SBarry Smith       t[idx+1] -=  v[5]*s1 +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
742c733ed4SBarry Smith       t[idx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
752c733ed4SBarry Smith       t[idx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
762c733ed4SBarry Smith       t[idx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
772c733ed4SBarry Smith       v        -= 25;
782c733ed4SBarry Smith     }
792c733ed4SBarry Smith   }
802c733ed4SBarry Smith 
812c733ed4SBarry Smith   /* copy t into x according to permutation */
822c733ed4SBarry Smith   ii = 0;
832c733ed4SBarry Smith   for (i=0; i<n; i++) {
842c733ed4SBarry Smith     ir      = 5*r[i];
852c733ed4SBarry Smith     x[ir]   = t[ii];
862c733ed4SBarry Smith     x[ir+1] = t[ii+1];
872c733ed4SBarry Smith     x[ir+2] = t[ii+2];
882c733ed4SBarry Smith     x[ir+3] = t[ii+3];
892c733ed4SBarry Smith     x[ir+4] = t[ii+4];
902c733ed4SBarry Smith     ii     += 5;
912c733ed4SBarry Smith   }
922c733ed4SBarry Smith 
93*9566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(isrow,&rout));
94*9566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(iscol,&cout));
95*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb,&b));
96*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx,&x));
97*9566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n));
982c733ed4SBarry Smith   PetscFunctionReturn(0);
992c733ed4SBarry Smith }
1002c733ed4SBarry Smith 
1012c733ed4SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_5(Mat A,Vec bb,Vec xx)
1022c733ed4SBarry Smith {
1032c733ed4SBarry Smith   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ*)A->data;
1042c733ed4SBarry Smith   IS                iscol=a->col,isrow=a->row;
1052c733ed4SBarry Smith   const PetscInt    n    =a->mbs,*vi,*ai=a->i,*aj=a->j,*diag=a->diag;
1062c733ed4SBarry Smith   const PetscInt    *r,*c,*rout,*cout;
1072c733ed4SBarry Smith   PetscInt          nz,idx,idt,j,i,oidx,ii,ic,ir;
1082c733ed4SBarry Smith   const PetscInt    bs =A->rmap->bs,bs2=a->bs2;
1092c733ed4SBarry Smith   const MatScalar   *aa=a->a,*v;
1102c733ed4SBarry Smith   PetscScalar       s1,s2,s3,s4,s5,x1,x2,x3,x4,x5,*x,*t;
1112c733ed4SBarry Smith   const PetscScalar *b;
1122c733ed4SBarry Smith 
1132c733ed4SBarry Smith   PetscFunctionBegin;
114*9566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(bb,&b));
115*9566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx,&x));
1162c733ed4SBarry Smith   t    = a->solve_work;
1172c733ed4SBarry Smith 
118*9566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(isrow,&rout)); r = rout;
119*9566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(iscol,&cout)); c = cout;
1202c733ed4SBarry Smith 
1212c733ed4SBarry Smith   /* copy b into temp work space according to permutation */
1222c733ed4SBarry Smith   for (i=0; i<n; i++) {
1232c733ed4SBarry Smith     ii      = bs*i; ic = bs*c[i];
1242c733ed4SBarry Smith     t[ii]   = b[ic]; t[ii+1] = b[ic+1]; t[ii+2] = b[ic+2]; t[ii+3] = b[ic+3];
1252c733ed4SBarry Smith     t[ii+4] = b[ic+4];
1262c733ed4SBarry Smith   }
1272c733ed4SBarry Smith 
1282c733ed4SBarry Smith   /* forward solve the U^T */
1292c733ed4SBarry Smith   idx = 0;
1302c733ed4SBarry Smith   for (i=0; i<n; i++) {
1312c733ed4SBarry Smith     v = aa + bs2*diag[i];
1322c733ed4SBarry Smith     /* multiply by the inverse of the block diagonal */
1332c733ed4SBarry Smith     x1 = t[idx];   x2 = t[1+idx]; x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
1342c733ed4SBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5;
1352c733ed4SBarry Smith     s2 = v[5]*x1  +  v[6]*x2 +  v[7]*x3 +  v[8]*x4 +  v[9]*x5;
1362c733ed4SBarry Smith     s3 = v[10]*x1 + v[11]*x2 + v[12]*x3 + v[13]*x4 + v[14]*x5;
1372c733ed4SBarry Smith     s4 = v[15]*x1 + v[16]*x2 + v[17]*x3 + v[18]*x4 + v[19]*x5;
1382c733ed4SBarry Smith     s5 = v[20]*x1 + v[21]*x2 + v[22]*x3 + v[23]*x4 + v[24]*x5;
1392c733ed4SBarry Smith     v -= bs2;
1402c733ed4SBarry Smith 
1412c733ed4SBarry Smith     vi = aj + diag[i] - 1;
1422c733ed4SBarry Smith     nz = diag[i] - diag[i+1] - 1;
1432c733ed4SBarry Smith     for (j=0; j>-nz; j--) {
1442c733ed4SBarry Smith       oidx       = bs*vi[j];
1452c733ed4SBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
1462c733ed4SBarry Smith       t[oidx+1] -= v[5]*s1  +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
1472c733ed4SBarry Smith       t[oidx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
1482c733ed4SBarry Smith       t[oidx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
1492c733ed4SBarry Smith       t[oidx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
1502c733ed4SBarry Smith       v         -= bs2;
1512c733ed4SBarry Smith     }
1522c733ed4SBarry Smith     t[idx] = s1;t[1+idx] = s2;  t[2+idx] = s3;  t[3+idx] = s4; t[4+idx] =s5;
1532c733ed4SBarry Smith     idx   += bs;
1542c733ed4SBarry Smith   }
1552c733ed4SBarry Smith   /* backward solve the L^T */
1562c733ed4SBarry Smith   for (i=n-1; i>=0; i--) {
1572c733ed4SBarry Smith     v   = aa + bs2*ai[i];
1582c733ed4SBarry Smith     vi  = aj + ai[i];
1592c733ed4SBarry Smith     nz  = ai[i+1] - ai[i];
1602c733ed4SBarry Smith     idt = bs*i;
1612c733ed4SBarry Smith     s1  = t[idt];  s2 = t[1+idt];  s3 = t[2+idt];  s4 = t[3+idt]; s5 = t[4+idt];
1622c733ed4SBarry Smith     for (j=0; j<nz; j++) {
1632c733ed4SBarry Smith       idx       = bs*vi[j];
1642c733ed4SBarry Smith       t[idx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
1652c733ed4SBarry Smith       t[idx+1] -= v[5]*s1  +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
1662c733ed4SBarry Smith       t[idx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
1672c733ed4SBarry Smith       t[idx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
1682c733ed4SBarry Smith       t[idx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
1692c733ed4SBarry Smith       v        += bs2;
1702c733ed4SBarry Smith     }
1712c733ed4SBarry Smith   }
1722c733ed4SBarry Smith 
1732c733ed4SBarry Smith   /* copy t into x according to permutation */
1742c733ed4SBarry Smith   for (i=0; i<n; i++) {
1752c733ed4SBarry Smith     ii      = bs*i;  ir = bs*r[i];
1762c733ed4SBarry Smith     x[ir]   = t[ii];  x[ir+1] = t[ii+1]; x[ir+2] = t[ii+2];  x[ir+3] = t[ii+3];
1772c733ed4SBarry Smith     x[ir+4] = t[ii+4];
1782c733ed4SBarry Smith   }
1792c733ed4SBarry Smith 
180*9566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(isrow,&rout));
181*9566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(iscol,&cout));
182*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb,&b));
183*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx,&x));
184*9566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0*bs2*(a->nz) - bs*A->cmap->n));
1852c733ed4SBarry Smith   PetscFunctionReturn(0);
1862c733ed4SBarry Smith }
187