xref: /petsc/src/mat/impls/baij/seq/baijsolvtran6.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
12c733ed4SBarry Smith #include <../src/mat/impls/baij/seq/baij.h>
22c733ed4SBarry Smith #include <petsc/private/kernels/blockinvert.h>
32c733ed4SBarry Smith 
4*9371c9d4SSatish Balay PetscErrorCode MatSolveTranspose_SeqBAIJ_6_inplace(Mat A, Vec bb, Vec xx) {
52c733ed4SBarry Smith   Mat_SeqBAIJ       *a     = (Mat_SeqBAIJ *)A->data;
62c733ed4SBarry Smith   IS                 iscol = a->col, isrow = a->row;
72c733ed4SBarry Smith   const PetscInt    *r, *c, *rout, *cout;
82c733ed4SBarry Smith   const PetscInt    *diag = a->diag, n = a->mbs, *vi, *ai = a->i, *aj = a->j;
92c733ed4SBarry Smith   PetscInt           i, nz, idx, idt, ii, ic, ir, oidx;
102c733ed4SBarry Smith   const MatScalar   *aa = a->a, *v;
112c733ed4SBarry Smith   PetscScalar        s1, s2, s3, s4, s5, s6, x1, x2, x3, x4, x5, x6, *x, *t;
122c733ed4SBarry Smith   const PetscScalar *b;
132c733ed4SBarry Smith 
142c733ed4SBarry Smith   PetscFunctionBegin;
159566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(bb, &b));
169566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx, &x));
172c733ed4SBarry Smith   t = a->solve_work;
182c733ed4SBarry Smith 
19*9371c9d4SSatish Balay   PetscCall(ISGetIndices(isrow, &rout));
20*9371c9d4SSatish Balay   r = rout;
21*9371c9d4SSatish Balay   PetscCall(ISGetIndices(iscol, &cout));
22*9371c9d4SSatish Balay   c = cout;
232c733ed4SBarry Smith 
242c733ed4SBarry Smith   /* copy the b into temp work space according to permutation */
252c733ed4SBarry Smith   ii = 0;
262c733ed4SBarry Smith   for (i = 0; i < n; i++) {
272c733ed4SBarry Smith     ic        = 6 * c[i];
282c733ed4SBarry Smith     t[ii]     = b[ic];
292c733ed4SBarry Smith     t[ii + 1] = b[ic + 1];
302c733ed4SBarry Smith     t[ii + 2] = b[ic + 2];
312c733ed4SBarry Smith     t[ii + 3] = b[ic + 3];
322c733ed4SBarry Smith     t[ii + 4] = b[ic + 4];
332c733ed4SBarry Smith     t[ii + 5] = b[ic + 5];
342c733ed4SBarry Smith     ii += 6;
352c733ed4SBarry Smith   }
362c733ed4SBarry Smith 
372c733ed4SBarry Smith   /* forward solve the U^T */
382c733ed4SBarry Smith   idx = 0;
392c733ed4SBarry Smith   for (i = 0; i < n; i++) {
402c733ed4SBarry Smith     v  = aa + 36 * diag[i];
412c733ed4SBarry Smith     /* multiply by the inverse of the block diagonal */
42*9371c9d4SSatish Balay     x1 = t[idx];
43*9371c9d4SSatish Balay     x2 = t[1 + idx];
44*9371c9d4SSatish Balay     x3 = t[2 + idx];
45*9371c9d4SSatish Balay     x4 = t[3 + idx];
46*9371c9d4SSatish Balay     x5 = t[4 + idx];
472c733ed4SBarry Smith     x6 = t[5 + idx];
482c733ed4SBarry Smith     s1 = v[0] * x1 + v[1] * x2 + v[2] * x3 + v[3] * x4 + v[4] * x5 + v[5] * x6;
492c733ed4SBarry Smith     s2 = v[6] * x1 + v[7] * x2 + v[8] * x3 + v[9] * x4 + v[10] * x5 + v[11] * x6;
502c733ed4SBarry Smith     s3 = v[12] * x1 + v[13] * x2 + v[14] * x3 + v[15] * x4 + v[16] * x5 + v[17] * x6;
512c733ed4SBarry Smith     s4 = v[18] * x1 + v[19] * x2 + v[20] * x3 + v[21] * x4 + v[22] * x5 + v[23] * x6;
522c733ed4SBarry Smith     s5 = v[24] * x1 + v[25] * x2 + v[26] * x3 + v[27] * x4 + v[28] * x5 + v[29] * x6;
532c733ed4SBarry Smith     s6 = v[30] * x1 + v[31] * x2 + v[32] * x3 + v[33] * x4 + v[34] * x5 + v[35] * x6;
542c733ed4SBarry Smith     v += 36;
552c733ed4SBarry Smith 
562c733ed4SBarry Smith     vi = aj + diag[i] + 1;
572c733ed4SBarry Smith     nz = ai[i + 1] - diag[i] - 1;
582c733ed4SBarry Smith     while (nz--) {
592c733ed4SBarry Smith       oidx = 6 * (*vi++);
602c733ed4SBarry Smith       t[oidx] -= v[0] * s1 + v[1] * s2 + v[2] * s3 + v[3] * s4 + v[4] * s5 + v[5] * s6;
612c733ed4SBarry Smith       t[oidx + 1] -= v[6] * s1 + v[7] * s2 + v[8] * s3 + v[9] * s4 + v[10] * s5 + v[11] * s6;
622c733ed4SBarry Smith       t[oidx + 2] -= v[12] * s1 + v[13] * s2 + v[14] * s3 + v[15] * s4 + v[16] * s5 + v[17] * s6;
632c733ed4SBarry Smith       t[oidx + 3] -= v[18] * s1 + v[19] * s2 + v[20] * s3 + v[21] * s4 + v[22] * s5 + v[23] * s6;
642c733ed4SBarry Smith       t[oidx + 4] -= v[24] * s1 + v[25] * s2 + v[26] * s3 + v[27] * s4 + v[28] * s5 + v[29] * s6;
652c733ed4SBarry Smith       t[oidx + 5] -= v[30] * s1 + v[31] * s2 + v[32] * s3 + v[33] * s4 + v[34] * s5 + v[35] * s6;
662c733ed4SBarry Smith       v += 36;
672c733ed4SBarry Smith     }
68*9371c9d4SSatish Balay     t[idx]     = s1;
69*9371c9d4SSatish Balay     t[1 + idx] = s2;
70*9371c9d4SSatish Balay     t[2 + idx] = s3;
71*9371c9d4SSatish Balay     t[3 + idx] = s4;
72*9371c9d4SSatish Balay     t[4 + idx] = s5;
732c733ed4SBarry Smith     t[5 + idx] = s6;
742c733ed4SBarry Smith     idx += 6;
752c733ed4SBarry Smith   }
762c733ed4SBarry Smith   /* backward solve the L^T */
772c733ed4SBarry Smith   for (i = n - 1; i >= 0; i--) {
782c733ed4SBarry Smith     v   = aa + 36 * diag[i] - 36;
792c733ed4SBarry Smith     vi  = aj + diag[i] - 1;
802c733ed4SBarry Smith     nz  = diag[i] - ai[i];
812c733ed4SBarry Smith     idt = 6 * i;
82*9371c9d4SSatish Balay     s1  = t[idt];
83*9371c9d4SSatish Balay     s2  = t[1 + idt];
84*9371c9d4SSatish Balay     s3  = t[2 + idt];
85*9371c9d4SSatish Balay     s4  = t[3 + idt];
86*9371c9d4SSatish Balay     s5  = t[4 + idt];
872c733ed4SBarry Smith     s6  = t[5 + idt];
882c733ed4SBarry Smith     while (nz--) {
892c733ed4SBarry Smith       idx = 6 * (*vi--);
902c733ed4SBarry Smith       t[idx] -= v[0] * s1 + v[1] * s2 + v[2] * s3 + v[3] * s4 + v[4] * s5 + v[5] * s6;
912c733ed4SBarry Smith       t[idx + 1] -= v[6] * s1 + v[7] * s2 + v[8] * s3 + v[9] * s4 + v[10] * s5 + v[11] * s6;
922c733ed4SBarry Smith       t[idx + 2] -= v[12] * s1 + v[13] * s2 + v[14] * s3 + v[15] * s4 + v[16] * s5 + v[17] * s6;
932c733ed4SBarry Smith       t[idx + 3] -= v[18] * s1 + v[19] * s2 + v[20] * s3 + v[21] * s4 + v[22] * s5 + v[23] * s6;
942c733ed4SBarry Smith       t[idx + 4] -= v[24] * s1 + v[25] * s2 + v[26] * s3 + v[27] * s4 + v[28] * s5 + v[29] * s6;
952c733ed4SBarry Smith       t[idx + 5] -= v[30] * s1 + v[31] * s2 + v[32] * s3 + v[33] * s4 + v[34] * s5 + v[35] * s6;
962c733ed4SBarry Smith       v -= 36;
972c733ed4SBarry Smith     }
982c733ed4SBarry Smith   }
992c733ed4SBarry Smith 
1002c733ed4SBarry Smith   /* copy t into x according to permutation */
1012c733ed4SBarry Smith   ii = 0;
1022c733ed4SBarry Smith   for (i = 0; i < n; i++) {
1032c733ed4SBarry Smith     ir        = 6 * r[i];
1042c733ed4SBarry Smith     x[ir]     = t[ii];
1052c733ed4SBarry Smith     x[ir + 1] = t[ii + 1];
1062c733ed4SBarry Smith     x[ir + 2] = t[ii + 2];
1072c733ed4SBarry Smith     x[ir + 3] = t[ii + 3];
1082c733ed4SBarry Smith     x[ir + 4] = t[ii + 4];
1092c733ed4SBarry Smith     x[ir + 5] = t[ii + 5];
1102c733ed4SBarry Smith     ii += 6;
1112c733ed4SBarry Smith   }
1122c733ed4SBarry Smith 
1139566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(isrow, &rout));
1149566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(iscol, &cout));
1159566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb, &b));
1169566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx, &x));
1179566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0 * 36 * (a->nz) - 6.0 * A->cmap->n));
1182c733ed4SBarry Smith   PetscFunctionReturn(0);
1192c733ed4SBarry Smith }
1202c733ed4SBarry Smith 
121*9371c9d4SSatish Balay PetscErrorCode MatSolveTranspose_SeqBAIJ_6(Mat A, Vec bb, Vec xx) {
1222c733ed4SBarry Smith   Mat_SeqBAIJ       *a     = (Mat_SeqBAIJ *)A->data;
1232c733ed4SBarry Smith   IS                 iscol = a->col, isrow = a->row;
1242c733ed4SBarry Smith   const PetscInt     n = a->mbs, *vi, *ai = a->i, *aj = a->j, *diag = a->diag;
1252c733ed4SBarry Smith   const PetscInt    *r, *c, *rout, *cout;
1262c733ed4SBarry Smith   PetscInt           nz, idx, idt, j, i, oidx, ii, ic, ir;
1272c733ed4SBarry Smith   const PetscInt     bs = A->rmap->bs, bs2 = a->bs2;
1282c733ed4SBarry Smith   const MatScalar   *aa = a->a, *v;
1292c733ed4SBarry Smith   PetscScalar        s1, s2, s3, s4, s5, s6, x1, x2, x3, x4, x5, x6, *x, *t;
1302c733ed4SBarry Smith   const PetscScalar *b;
1312c733ed4SBarry Smith 
1322c733ed4SBarry Smith   PetscFunctionBegin;
1339566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(bb, &b));
1349566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx, &x));
1352c733ed4SBarry Smith   t = a->solve_work;
1362c733ed4SBarry Smith 
137*9371c9d4SSatish Balay   PetscCall(ISGetIndices(isrow, &rout));
138*9371c9d4SSatish Balay   r = rout;
139*9371c9d4SSatish Balay   PetscCall(ISGetIndices(iscol, &cout));
140*9371c9d4SSatish Balay   c = cout;
1412c733ed4SBarry Smith 
1422c733ed4SBarry Smith   /* copy b into temp work space according to permutation */
1432c733ed4SBarry Smith   for (i = 0; i < n; i++) {
144*9371c9d4SSatish Balay     ii        = bs * i;
145*9371c9d4SSatish Balay     ic        = bs * c[i];
146*9371c9d4SSatish Balay     t[ii]     = b[ic];
147*9371c9d4SSatish Balay     t[ii + 1] = b[ic + 1];
148*9371c9d4SSatish Balay     t[ii + 2] = b[ic + 2];
149*9371c9d4SSatish Balay     t[ii + 3] = b[ic + 3];
150*9371c9d4SSatish Balay     t[ii + 4] = b[ic + 4];
151*9371c9d4SSatish Balay     t[ii + 5] = b[ic + 5];
1522c733ed4SBarry Smith   }
1532c733ed4SBarry Smith 
1542c733ed4SBarry Smith   /* forward solve the U^T */
1552c733ed4SBarry Smith   idx = 0;
1562c733ed4SBarry Smith   for (i = 0; i < n; i++) {
1572c733ed4SBarry Smith     v  = aa + bs2 * diag[i];
1582c733ed4SBarry Smith     /* multiply by the inverse of the block diagonal */
159*9371c9d4SSatish Balay     x1 = t[idx];
160*9371c9d4SSatish Balay     x2 = t[1 + idx];
161*9371c9d4SSatish Balay     x3 = t[2 + idx];
162*9371c9d4SSatish Balay     x4 = t[3 + idx];
163*9371c9d4SSatish Balay     x5 = t[4 + idx];
1642c733ed4SBarry Smith     x6 = t[5 + idx];
1652c733ed4SBarry Smith     s1 = v[0] * x1 + v[1] * x2 + v[2] * x3 + v[3] * x4 + v[4] * x5 + v[5] * x6;
1662c733ed4SBarry Smith     s2 = v[6] * x1 + v[7] * x2 + v[8] * x3 + v[9] * x4 + v[10] * x5 + v[11] * x6;
1672c733ed4SBarry Smith     s3 = v[12] * x1 + v[13] * x2 + v[14] * x3 + v[15] * x4 + v[16] * x5 + v[17] * x6;
1682c733ed4SBarry Smith     s4 = v[18] * x1 + v[19] * x2 + v[20] * x3 + v[21] * x4 + v[22] * x5 + v[23] * x6;
1692c733ed4SBarry Smith     s5 = v[24] * x1 + v[25] * x2 + v[26] * x3 + v[27] * x4 + v[28] * x5 + v[29] * x6;
1702c733ed4SBarry Smith     s6 = v[30] * x1 + v[31] * x2 + v[32] * x3 + v[33] * x4 + v[34] * x5 + v[35] * x6;
1712c733ed4SBarry Smith     v -= bs2;
1722c733ed4SBarry Smith 
1732c733ed4SBarry Smith     vi = aj + diag[i] - 1;
1742c733ed4SBarry Smith     nz = diag[i] - diag[i + 1] - 1;
1752c733ed4SBarry Smith     for (j = 0; j > -nz; j--) {
1762c733ed4SBarry Smith       oidx = bs * vi[j];
1772c733ed4SBarry Smith       t[oidx] -= v[0] * s1 + v[1] * s2 + v[2] * s3 + v[3] * s4 + v[4] * s5 + v[5] * s6;
1782c733ed4SBarry Smith       t[oidx + 1] -= v[6] * s1 + v[7] * s2 + v[8] * s3 + v[9] * s4 + v[10] * s5 + v[11] * s6;
1792c733ed4SBarry Smith       t[oidx + 2] -= v[12] * s1 + v[13] * s2 + v[14] * s3 + v[15] * s4 + v[16] * s5 + v[17] * s6;
1802c733ed4SBarry Smith       t[oidx + 3] -= v[18] * s1 + v[19] * s2 + v[20] * s3 + v[21] * s4 + v[22] * s5 + v[23] * s6;
1812c733ed4SBarry Smith       t[oidx + 4] -= v[24] * s1 + v[25] * s2 + v[26] * s3 + v[27] * s4 + v[28] * s5 + v[29] * s6;
1822c733ed4SBarry Smith       t[oidx + 5] -= v[30] * s1 + v[31] * s2 + v[32] * s3 + v[33] * s4 + v[34] * s5 + v[35] * s6;
1832c733ed4SBarry Smith       v -= bs2;
1842c733ed4SBarry Smith     }
185*9371c9d4SSatish Balay     t[idx]     = s1;
186*9371c9d4SSatish Balay     t[1 + idx] = s2;
187*9371c9d4SSatish Balay     t[2 + idx] = s3;
188*9371c9d4SSatish Balay     t[3 + idx] = s4;
189*9371c9d4SSatish Balay     t[4 + idx] = s5;
1902c733ed4SBarry Smith     t[5 + idx] = s6;
1912c733ed4SBarry Smith     idx += bs;
1922c733ed4SBarry Smith   }
1932c733ed4SBarry Smith   /* backward solve the L^T */
1942c733ed4SBarry Smith   for (i = n - 1; i >= 0; i--) {
1952c733ed4SBarry Smith     v   = aa + bs2 * ai[i];
1962c733ed4SBarry Smith     vi  = aj + ai[i];
1972c733ed4SBarry Smith     nz  = ai[i + 1] - ai[i];
1982c733ed4SBarry Smith     idt = bs * i;
199*9371c9d4SSatish Balay     s1  = t[idt];
200*9371c9d4SSatish Balay     s2  = t[1 + idt];
201*9371c9d4SSatish Balay     s3  = t[2 + idt];
202*9371c9d4SSatish Balay     s4  = t[3 + idt];
203*9371c9d4SSatish Balay     s5  = t[4 + idt];
2042c733ed4SBarry Smith     s6  = t[5 + idt];
2052c733ed4SBarry Smith     for (j = 0; j < nz; j++) {
2062c733ed4SBarry Smith       idx = bs * vi[j];
2072c733ed4SBarry Smith       t[idx] -= v[0] * s1 + v[1] * s2 + v[2] * s3 + v[3] * s4 + v[4] * s5 + v[5] * s6;
2082c733ed4SBarry Smith       t[idx + 1] -= v[6] * s1 + v[7] * s2 + v[8] * s3 + v[9] * s4 + v[10] * s5 + v[11] * s6;
2092c733ed4SBarry Smith       t[idx + 2] -= v[12] * s1 + v[13] * s2 + v[14] * s3 + v[15] * s4 + v[16] * s5 + v[17] * s6;
2102c733ed4SBarry Smith       t[idx + 3] -= v[18] * s1 + v[19] * s2 + v[20] * s3 + v[21] * s4 + v[22] * s5 + v[23] * s6;
2112c733ed4SBarry Smith       t[idx + 4] -= v[24] * s1 + v[25] * s2 + v[26] * s3 + v[27] * s4 + v[28] * s5 + v[29] * s6;
2122c733ed4SBarry Smith       t[idx + 5] -= v[30] * s1 + v[31] * s2 + v[32] * s3 + v[33] * s4 + v[34] * s5 + v[35] * s6;
2132c733ed4SBarry Smith       v += bs2;
2142c733ed4SBarry Smith     }
2152c733ed4SBarry Smith   }
2162c733ed4SBarry Smith 
2172c733ed4SBarry Smith   /* copy t into x according to permutation */
2182c733ed4SBarry Smith   for (i = 0; i < n; i++) {
219*9371c9d4SSatish Balay     ii        = bs * i;
220*9371c9d4SSatish Balay     ir        = bs * r[i];
221*9371c9d4SSatish Balay     x[ir]     = t[ii];
222*9371c9d4SSatish Balay     x[ir + 1] = t[ii + 1];
223*9371c9d4SSatish Balay     x[ir + 2] = t[ii + 2];
224*9371c9d4SSatish Balay     x[ir + 3] = t[ii + 3];
225*9371c9d4SSatish Balay     x[ir + 4] = t[ii + 4];
226*9371c9d4SSatish Balay     x[ir + 5] = t[ii + 5];
2272c733ed4SBarry Smith   }
2282c733ed4SBarry Smith 
2299566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(isrow, &rout));
2309566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(iscol, &cout));
2319566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(bb, &b));
2329566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx, &x));
2339566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0 * bs2 * (a->nz) - bs * A->cmap->n));
2342c733ed4SBarry Smith   PetscFunctionReturn(0);
2352c733ed4SBarry Smith }
236