xref: /petsc/src/mat/impls/baij/seq/baijsolvnat7.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 MatSolve_SeqBAIJ_7_NaturalOrdering_inplace(Mat A,Vec bb,Vec xx)
52c733ed4SBarry Smith {
62c733ed4SBarry Smith   Mat_SeqBAIJ       *a   = (Mat_SeqBAIJ*)A->data;
72c733ed4SBarry Smith   const PetscInt    *diag=a->diag,n=a->mbs,*vi,*ai=a->i,*aj=a->j;
82c733ed4SBarry Smith   PetscInt          i,nz,idx,idt,jdx;
92c733ed4SBarry Smith   const MatScalar   *aa=a->a,*v;
102c733ed4SBarry Smith   PetscScalar       *x,s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
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   /* forward solve the lower triangular */
172c733ed4SBarry Smith   idx  = 0;
182c733ed4SBarry Smith   x[0] = b[idx];   x[1] = b[1+idx]; x[2] = b[2+idx];
192c733ed4SBarry Smith   x[3] = b[3+idx]; x[4] = b[4+idx]; x[5] = b[5+idx];
202c733ed4SBarry Smith   x[6] = b[6+idx];
212c733ed4SBarry Smith   for (i=1; i<n; i++) {
222c733ed4SBarry Smith     v   =  aa + 49*ai[i];
232c733ed4SBarry Smith     vi  =  aj + ai[i];
242c733ed4SBarry Smith     nz  =  diag[i] - ai[i];
252c733ed4SBarry Smith     idx =  7*i;
262c733ed4SBarry Smith     s1  =  b[idx];   s2 = b[1+idx]; s3 = b[2+idx];
272c733ed4SBarry Smith     s4  =  b[3+idx]; s5 = b[4+idx]; s6 = b[5+idx];
282c733ed4SBarry Smith     s7  =  b[6+idx];
292c733ed4SBarry Smith     while (nz--) {
302c733ed4SBarry Smith       jdx = 7*(*vi++);
312c733ed4SBarry Smith       x1  = x[jdx];   x2 = x[1+jdx]; x3 = x[2+jdx];
322c733ed4SBarry Smith       x4  = x[3+jdx]; x5 = x[4+jdx]; x6 = x[5+jdx];
332c733ed4SBarry Smith       x7  = x[6+jdx];
342c733ed4SBarry Smith       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
352c733ed4SBarry Smith       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
362c733ed4SBarry Smith       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
372c733ed4SBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
382c733ed4SBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
392c733ed4SBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
402c733ed4SBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
412c733ed4SBarry Smith       v  += 49;
422c733ed4SBarry Smith     }
432c733ed4SBarry Smith     x[idx]   = s1;
442c733ed4SBarry Smith     x[1+idx] = s2;
452c733ed4SBarry Smith     x[2+idx] = s3;
462c733ed4SBarry Smith     x[3+idx] = s4;
472c733ed4SBarry Smith     x[4+idx] = s5;
482c733ed4SBarry Smith     x[5+idx] = s6;
492c733ed4SBarry Smith     x[6+idx] = s7;
502c733ed4SBarry Smith   }
512c733ed4SBarry Smith   /* backward solve the upper triangular */
522c733ed4SBarry Smith   for (i=n-1; i>=0; i--) {
532c733ed4SBarry Smith     v   = aa + 49*diag[i] + 49;
542c733ed4SBarry Smith     vi  = aj + diag[i] + 1;
552c733ed4SBarry Smith     nz  = ai[i+1] - diag[i] - 1;
562c733ed4SBarry Smith     idt = 7*i;
572c733ed4SBarry Smith     s1  = x[idt];   s2 = x[1+idt];
582c733ed4SBarry Smith     s3  = x[2+idt]; s4 = x[3+idt];
592c733ed4SBarry Smith     s5  = x[4+idt]; s6 = x[5+idt];
602c733ed4SBarry Smith     s7  = x[6+idt];
612c733ed4SBarry Smith     while (nz--) {
622c733ed4SBarry Smith       idx = 7*(*vi++);
632c733ed4SBarry Smith       x1  = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];
642c733ed4SBarry Smith       x4  = x[3+idx]; x5 = x[4+idx]; x6 = x[5+idx];
652c733ed4SBarry Smith       x7  = x[6+idx];
662c733ed4SBarry Smith       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
672c733ed4SBarry Smith       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
682c733ed4SBarry Smith       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
692c733ed4SBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
702c733ed4SBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
712c733ed4SBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
722c733ed4SBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
732c733ed4SBarry Smith       v  += 49;
742c733ed4SBarry Smith     }
752c733ed4SBarry Smith     v      = aa + 49*diag[i];
762c733ed4SBarry Smith     x[idt] = v[0]*s1 + v[7]*s2  + v[14]*s3 + v[21]*s4
772c733ed4SBarry Smith              + v[28]*s5 + v[35]*s6 + v[42]*s7;
782c733ed4SBarry Smith     x[1+idt] = v[1]*s1 + v[8]*s2  + v[15]*s3 + v[22]*s4
792c733ed4SBarry Smith                + v[29]*s5 + v[36]*s6 + v[43]*s7;
802c733ed4SBarry Smith     x[2+idt] = v[2]*s1 + v[9]*s2  + v[16]*s3 + v[23]*s4
812c733ed4SBarry Smith                + v[30]*s5 + v[37]*s6 + v[44]*s7;
822c733ed4SBarry Smith     x[3+idt] = v[3]*s1 + v[10]*s2  + v[17]*s3 + v[24]*s4
832c733ed4SBarry Smith                + v[31]*s5 + v[38]*s6 + v[45]*s7;
842c733ed4SBarry Smith     x[4+idt] = v[4]*s1 + v[11]*s2  + v[18]*s3 + v[25]*s4
852c733ed4SBarry Smith                + v[32]*s5 + v[39]*s6 + v[46]*s7;
862c733ed4SBarry Smith     x[5+idt] = v[5]*s1 + v[12]*s2  + v[19]*s3 + v[26]*s4
872c733ed4SBarry Smith                + v[33]*s5 + v[40]*s6 + v[47]*s7;
882c733ed4SBarry Smith     x[6+idt] = v[6]*s1 + v[13]*s2  + v[20]*s3 + v[27]*s4
892c733ed4SBarry Smith                + v[34]*s5 + v[41]*s6 + v[48]*s7;
902c733ed4SBarry Smith   }
912c733ed4SBarry Smith 
92*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb,&b));
93*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx,&x));
94*9566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0*36*(a->nz) - 6.0*A->cmap->n));
952c733ed4SBarry Smith   PetscFunctionReturn(0);
962c733ed4SBarry Smith }
972c733ed4SBarry Smith 
982c733ed4SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_7_NaturalOrdering(Mat A,Vec bb,Vec xx)
992c733ed4SBarry Smith {
1002c733ed4SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1012c733ed4SBarry Smith   const PetscInt    n  =a->mbs,*vi,*ai=a->i,*aj=a->j,*adiag=a->diag;
1022c733ed4SBarry Smith   PetscInt          i,k,nz,idx,jdx,idt;
1032c733ed4SBarry Smith   const PetscInt    bs = A->rmap->bs,bs2 = a->bs2;
1042c733ed4SBarry Smith   const MatScalar   *aa=a->a,*v;
1052c733ed4SBarry Smith   PetscScalar       *x;
1062c733ed4SBarry Smith   const PetscScalar *b;
1072c733ed4SBarry Smith   PetscScalar       s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
1082c733ed4SBarry Smith 
1092c733ed4SBarry Smith   PetscFunctionBegin;
110*9566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(bb,&b));
111*9566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx,&x));
1122c733ed4SBarry Smith   /* forward solve the lower triangular */
1132c733ed4SBarry Smith   idx  = 0;
1142c733ed4SBarry Smith   x[0] = b[idx]; x[1] = b[1+idx];x[2] = b[2+idx];x[3] = b[3+idx];
1152c733ed4SBarry Smith   x[4] = b[4+idx];x[5] = b[5+idx];x[6] = b[6+idx];
1162c733ed4SBarry Smith   for (i=1; i<n; i++) {
1172c733ed4SBarry Smith     v   = aa + bs2*ai[i];
1182c733ed4SBarry Smith     vi  = aj + ai[i];
1192c733ed4SBarry Smith     nz  = ai[i+1] - ai[i];
1202c733ed4SBarry Smith     idx = bs*i;
1212c733ed4SBarry Smith     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
1222c733ed4SBarry Smith     s5  = b[4+idx];s6 = b[5+idx];s7 = b[6+idx];
1232c733ed4SBarry Smith     for (k=0; k<nz; k++) {
1242c733ed4SBarry Smith       jdx = bs*vi[k];
1252c733ed4SBarry Smith       x1  = x[jdx];x2 = x[1+jdx]; x3 =x[2+jdx];x4 =x[3+jdx];
1262c733ed4SBarry Smith       x5  = x[4+jdx]; x6 = x[5+jdx];x7 = x[6+jdx];
1272c733ed4SBarry Smith       s1 -= v[0]*x1 + v[7]*x2 + v[14]*x3 + v[21]*x4  + v[28]*x5 + v[35]*x6 + v[42]*x7;
1282c733ed4SBarry Smith       s2 -= v[1]*x1 + v[8]*x2 + v[15]*x3 + v[22]*x4  + v[29]*x5 + v[36]*x6 + v[43]*x7;
1292c733ed4SBarry Smith       s3 -= v[2]*x1 + v[9]*x2 + v[16]*x3 + v[23]*x4  + v[30]*x5 + v[37]*x6 + v[44]*x7;
1302c733ed4SBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4  + v[31]*x5 + v[38]*x6 + v[45]*x7;
1312c733ed4SBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4  + v[32]*x5 + v[39]*x6 + v[46]*x7;
1322c733ed4SBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4  + v[33]*x5 + v[40]*x6 + v[47]*x7;
1332c733ed4SBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4  + v[34]*x5 + v[41]*x6 + v[48]*x7;
1342c733ed4SBarry Smith       v  +=  bs2;
1352c733ed4SBarry Smith     }
1362c733ed4SBarry Smith 
1372c733ed4SBarry Smith     x[idx]   = s1;
1382c733ed4SBarry Smith     x[1+idx] = s2;
1392c733ed4SBarry Smith     x[2+idx] = s3;
1402c733ed4SBarry Smith     x[3+idx] = s4;
1412c733ed4SBarry Smith     x[4+idx] = s5;
1422c733ed4SBarry Smith     x[5+idx] = s6;
1432c733ed4SBarry Smith     x[6+idx] = s7;
1442c733ed4SBarry Smith   }
1452c733ed4SBarry Smith 
1462c733ed4SBarry Smith   /* backward solve the upper triangular */
1472c733ed4SBarry Smith   for (i=n-1; i>=0; i--) {
1482c733ed4SBarry Smith     v   = aa + bs2*(adiag[i+1]+1);
1492c733ed4SBarry Smith     vi  = aj + adiag[i+1]+1;
1502c733ed4SBarry Smith     nz  = adiag[i] - adiag[i+1]-1;
1512c733ed4SBarry Smith     idt = bs*i;
1522c733ed4SBarry Smith     s1  = x[idt];  s2 = x[1+idt];s3 = x[2+idt];s4 = x[3+idt];
1532c733ed4SBarry Smith     s5  = x[4+idt];s6 = x[5+idt];s7 = x[6+idt];
1542c733ed4SBarry Smith     for (k=0; k<nz; k++) {
1552c733ed4SBarry Smith       idx = bs*vi[k];
1562c733ed4SBarry Smith       x1  = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];x4 = x[3+idx];
1572c733ed4SBarry Smith       x5  = x[4+idx];x6 = x[5+idx];x7 = x[6+idx];
1582c733ed4SBarry Smith       s1 -= v[0]*x1 + v[7]*x2 + v[14]*x3 + v[21]*x4  + v[28]*x5 + v[35]*x6 + v[42]*x7;
1592c733ed4SBarry Smith       s2 -= v[1]*x1 + v[8]*x2 + v[15]*x3 + v[22]*x4  + v[29]*x5 + v[36]*x6 + v[43]*x7;
1602c733ed4SBarry Smith       s3 -= v[2]*x1 + v[9]*x2 + v[16]*x3 + v[23]*x4  + v[30]*x5 + v[37]*x6 + v[44]*x7;
1612c733ed4SBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4  + v[31]*x5 + v[38]*x6 + v[45]*x7;
1622c733ed4SBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4  + v[32]*x5 + v[39]*x6 + v[46]*x7;
1632c733ed4SBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4  + v[33]*x5 + v[40]*x6 + v[47]*x7;
1642c733ed4SBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4  + v[34]*x5 + v[41]*x6 + v[48]*x7;
1652c733ed4SBarry Smith       v  +=  bs2;
1662c733ed4SBarry Smith     }
1672c733ed4SBarry Smith     /* x = inv_diagonal*x */
1682c733ed4SBarry Smith     x[idt]   = v[0]*s1 + v[7]*s2 + v[14]*s3 + v[21]*s4  + v[28]*s5 + v[35]*s6 + v[42]*s7;
1692c733ed4SBarry Smith     x[1+idt] = v[1]*s1 + v[8]*s2 + v[15]*s3 + v[22]*s4  + v[29]*s5 + v[36]*s6 + v[43]*s7;
1702c733ed4SBarry Smith     x[2+idt] = v[2]*s1 + v[9]*s2 + v[16]*s3 + v[23]*s4  + v[30]*s5 + v[37]*s6 + v[44]*s7;
1712c733ed4SBarry Smith     x[3+idt] = v[3]*s1 + v[10]*s2 + v[17]*s3 + v[24]*s4  + v[31]*s5 + v[38]*s6 + v[45]*s7;
1722c733ed4SBarry Smith     x[4+idt] = v[4]*s1 + v[11]*s2 + v[18]*s3 + v[25]*s4  + v[32]*s5 + v[39]*s6 + v[46]*s7;
1732c733ed4SBarry Smith     x[5+idt] = v[5]*s1 + v[12]*s2 + v[19]*s3 + v[26]*s4  + v[33]*s5 + v[40]*s6 + v[47]*s7;
1742c733ed4SBarry Smith     x[6+idt] = v[6]*s1 + v[13]*s2 + v[20]*s3 + v[27]*s4  + v[34]*s5 + v[41]*s6 + v[48]*s7;
1752c733ed4SBarry Smith   }
1762c733ed4SBarry Smith 
177*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb,&b));
178*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx,&x));
179*9566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0*bs2*(a->nz) - bs*A->cmap->n));
1802c733ed4SBarry Smith   PetscFunctionReturn(0);
1812c733ed4SBarry Smith }
182