xref: /petsc/src/mat/impls/baij/seq/baijsolvnat5.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_5_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,x1,x2,x3,x4,x5;
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]; x[3] = b[3+idx];x[4] = b[4+idx];
192c733ed4SBarry Smith   for (i=1; i<n; i++) {
202c733ed4SBarry Smith     v   =  aa + 25*ai[i];
212c733ed4SBarry Smith     vi  =  aj + ai[i];
222c733ed4SBarry Smith     nz  =  diag[i] - ai[i];
232c733ed4SBarry Smith     idx =  5*i;
242c733ed4SBarry Smith     s1  =  b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];s5 = b[4+idx];
252c733ed4SBarry Smith     while (nz--) {
262c733ed4SBarry Smith       jdx = 5*(*vi++);
272c733ed4SBarry Smith       x1  = x[jdx];x2 = x[1+jdx];x3 = x[2+jdx];x4 = x[3+jdx];x5 = x[4+jdx];
282c733ed4SBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
292c733ed4SBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
302c733ed4SBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
312c733ed4SBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
322c733ed4SBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
332c733ed4SBarry Smith       v  += 25;
342c733ed4SBarry Smith     }
352c733ed4SBarry Smith     x[idx]   = s1;
362c733ed4SBarry Smith     x[1+idx] = s2;
372c733ed4SBarry Smith     x[2+idx] = s3;
382c733ed4SBarry Smith     x[3+idx] = s4;
392c733ed4SBarry Smith     x[4+idx] = s5;
402c733ed4SBarry Smith   }
412c733ed4SBarry Smith   /* backward solve the upper triangular */
422c733ed4SBarry Smith   for (i=n-1; i>=0; i--) {
432c733ed4SBarry Smith     v   = aa + 25*diag[i] + 25;
442c733ed4SBarry Smith     vi  = aj + diag[i] + 1;
452c733ed4SBarry Smith     nz  = ai[i+1] - diag[i] - 1;
462c733ed4SBarry Smith     idt = 5*i;
472c733ed4SBarry Smith     s1  = x[idt];  s2 = x[1+idt];
482c733ed4SBarry Smith     s3  = x[2+idt];s4 = x[3+idt]; s5 = x[4+idt];
492c733ed4SBarry Smith     while (nz--) {
502c733ed4SBarry Smith       idx = 5*(*vi++);
512c733ed4SBarry Smith       x1  = x[idx];   x2 = x[1+idx];x3    = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
522c733ed4SBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
532c733ed4SBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
542c733ed4SBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
552c733ed4SBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
562c733ed4SBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
572c733ed4SBarry Smith       v  += 25;
582c733ed4SBarry Smith     }
592c733ed4SBarry Smith     v        = aa + 25*diag[i];
602c733ed4SBarry Smith     x[idt]   = v[0]*s1 + v[5]*s2 + v[10]*s3  + v[15]*s4 + v[20]*s5;
612c733ed4SBarry Smith     x[1+idt] = v[1]*s1 + v[6]*s2 + v[11]*s3  + v[16]*s4 + v[21]*s5;
622c733ed4SBarry Smith     x[2+idt] = v[2]*s1 + v[7]*s2 + v[12]*s3  + v[17]*s4 + v[22]*s5;
632c733ed4SBarry Smith     x[3+idt] = v[3]*s1 + v[8]*s2 + v[13]*s3  + v[18]*s4 + v[23]*s5;
642c733ed4SBarry Smith     x[4+idt] = v[4]*s1 + v[9]*s2 + v[14]*s3  + v[19]*s4 + v[24]*s5;
652c733ed4SBarry Smith   }
662c733ed4SBarry Smith 
67*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb,&b));
68*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx,&x));
69*9566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n));
702c733ed4SBarry Smith   PetscFunctionReturn(0);
712c733ed4SBarry Smith }
722c733ed4SBarry Smith 
732c733ed4SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_5_NaturalOrdering(Mat A,Vec bb,Vec xx)
742c733ed4SBarry Smith {
752c733ed4SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
762c733ed4SBarry Smith   const PetscInt    n  = a->mbs,*vi,*ai=a->i,*aj=a->j,*adiag=a->diag;
772c733ed4SBarry Smith   PetscInt          i,k,nz,idx,idt,jdx;
782c733ed4SBarry Smith   const MatScalar   *aa=a->a,*v;
792c733ed4SBarry Smith   PetscScalar       *x,s1,s2,s3,s4,s5,x1,x2,x3,x4,x5;
802c733ed4SBarry Smith   const PetscScalar *b;
812c733ed4SBarry Smith 
822c733ed4SBarry Smith   PetscFunctionBegin;
83*9566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(bb,&b));
84*9566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx,&x));
852c733ed4SBarry Smith   /* forward solve the lower triangular */
862c733ed4SBarry Smith   idx  = 0;
872c733ed4SBarry Smith   x[0] = b[idx]; x[1] = b[1+idx]; x[2] = b[2+idx]; x[3] = b[3+idx];x[4] = b[4+idx];
882c733ed4SBarry Smith   for (i=1; i<n; i++) {
892c733ed4SBarry Smith     v   = aa + 25*ai[i];
902c733ed4SBarry Smith     vi  = aj + ai[i];
912c733ed4SBarry Smith     nz  = ai[i+1] - ai[i];
922c733ed4SBarry Smith     idx = 5*i;
932c733ed4SBarry Smith     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];s5 = b[4+idx];
942c733ed4SBarry Smith     for (k=0; k<nz; k++) {
952c733ed4SBarry Smith       jdx = 5*vi[k];
962c733ed4SBarry Smith       x1  = x[jdx];x2 = x[1+jdx];x3 = x[2+jdx];x4 = x[3+jdx];x5 = x[4+jdx];
972c733ed4SBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
982c733ed4SBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
992c733ed4SBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
1002c733ed4SBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
1012c733ed4SBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
1022c733ed4SBarry Smith       v  += 25;
1032c733ed4SBarry Smith     }
1042c733ed4SBarry Smith     x[idx]   = s1;
1052c733ed4SBarry Smith     x[1+idx] = s2;
1062c733ed4SBarry Smith     x[2+idx] = s3;
1072c733ed4SBarry Smith     x[3+idx] = s4;
1082c733ed4SBarry Smith     x[4+idx] = s5;
1092c733ed4SBarry Smith   }
1102c733ed4SBarry Smith 
1112c733ed4SBarry Smith   /* backward solve the upper triangular */
1122c733ed4SBarry Smith   for (i=n-1; i>=0; i--) {
1132c733ed4SBarry Smith     v   = aa + 25*(adiag[i+1]+1);
1142c733ed4SBarry Smith     vi  = aj + adiag[i+1]+1;
1152c733ed4SBarry Smith     nz  = adiag[i] - adiag[i+1]-1;
1162c733ed4SBarry Smith     idt = 5*i;
1172c733ed4SBarry Smith     s1  = x[idt];  s2 = x[1+idt];
1182c733ed4SBarry Smith     s3  = x[2+idt];s4 = x[3+idt]; s5 = x[4+idt];
1192c733ed4SBarry Smith     for (k=0; k<nz; k++) {
1202c733ed4SBarry Smith       idx = 5*vi[k];
1212c733ed4SBarry Smith       x1  = x[idx];   x2 = x[1+idx];x3    = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
1222c733ed4SBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
1232c733ed4SBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
1242c733ed4SBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
1252c733ed4SBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
1262c733ed4SBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
1272c733ed4SBarry Smith       v  += 25;
1282c733ed4SBarry Smith     }
1292c733ed4SBarry Smith     /* x = inv_diagonal*x */
1302c733ed4SBarry Smith     x[idt]   = v[0]*s1 + v[5]*s2 + v[10]*s3  + v[15]*s4 + v[20]*s5;
1312c733ed4SBarry Smith     x[1+idt] = v[1]*s1 + v[6]*s2 + v[11]*s3  + v[16]*s4 + v[21]*s5;
1322c733ed4SBarry Smith     x[2+idt] = v[2]*s1 + v[7]*s2 + v[12]*s3  + v[17]*s4 + v[22]*s5;
1332c733ed4SBarry Smith     x[3+idt] = v[3]*s1 + v[8]*s2 + v[13]*s3  + v[18]*s4 + v[23]*s5;
1342c733ed4SBarry Smith     x[4+idt] = v[4]*s1 + v[9]*s2 + v[14]*s3  + v[19]*s4 + v[24]*s5;
1352c733ed4SBarry Smith   }
1362c733ed4SBarry Smith 
137*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb,&b));
138*9566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx,&x));
139*9566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n));
1402c733ed4SBarry Smith   PetscFunctionReturn(0);
1412c733ed4SBarry Smith }
142