xref: /petsc/src/mat/impls/baij/seq/baijsolvtrannat3.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
12c733ed4SBarry Smith #include <../src/mat/impls/baij/seq/baij.h>
22c733ed4SBarry Smith 
3*9371c9d4SSatish Balay PetscErrorCode MatSolveTranspose_SeqBAIJ_3_NaturalOrdering_inplace(Mat A, Vec bb, Vec xx) {
42c733ed4SBarry Smith   Mat_SeqBAIJ     *a = (Mat_SeqBAIJ *)A->data;
52c733ed4SBarry Smith   const PetscInt   n = a->mbs, *vi, *ai = a->i, *aj = a->j, *diag = a->diag;
62c733ed4SBarry Smith   PetscInt         i, nz, idx, idt, oidx;
72c733ed4SBarry Smith   const MatScalar *aa = a->a, *v;
82c733ed4SBarry Smith   PetscScalar      s1, s2, s3, x1, x2, x3, *x;
92c733ed4SBarry Smith 
102c733ed4SBarry Smith   PetscFunctionBegin;
119566063dSJacob Faibussowitsch   PetscCall(VecCopy(bb, xx));
129566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx, &x));
132c733ed4SBarry Smith 
142c733ed4SBarry Smith   /* forward solve the U^T */
152c733ed4SBarry Smith   idx = 0;
162c733ed4SBarry Smith   for (i = 0; i < n; i++) {
172c733ed4SBarry Smith     v  = aa + 9 * diag[i];
182c733ed4SBarry Smith     /* multiply by the inverse of the block diagonal */
19*9371c9d4SSatish Balay     x1 = x[idx];
20*9371c9d4SSatish Balay     x2 = x[1 + idx];
21*9371c9d4SSatish Balay     x3 = x[2 + idx];
222c733ed4SBarry Smith     s1 = v[0] * x1 + v[1] * x2 + v[2] * x3;
232c733ed4SBarry Smith     s2 = v[3] * x1 + v[4] * x2 + v[5] * x3;
242c733ed4SBarry Smith     s3 = v[6] * x1 + v[7] * x2 + v[8] * x3;
252c733ed4SBarry Smith     v += 9;
262c733ed4SBarry Smith 
272c733ed4SBarry Smith     vi = aj + diag[i] + 1;
282c733ed4SBarry Smith     nz = ai[i + 1] - diag[i] - 1;
292c733ed4SBarry Smith     while (nz--) {
302c733ed4SBarry Smith       oidx = 3 * (*vi++);
312c733ed4SBarry Smith       x[oidx] -= v[0] * s1 + v[1] * s2 + v[2] * s3;
322c733ed4SBarry Smith       x[oidx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3;
332c733ed4SBarry Smith       x[oidx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3;
342c733ed4SBarry Smith       v += 9;
352c733ed4SBarry Smith     }
36*9371c9d4SSatish Balay     x[idx]     = s1;
37*9371c9d4SSatish Balay     x[1 + idx] = s2;
38*9371c9d4SSatish Balay     x[2 + idx] = s3;
392c733ed4SBarry Smith     idx += 3;
402c733ed4SBarry Smith   }
412c733ed4SBarry Smith   /* backward solve the L^T */
422c733ed4SBarry Smith   for (i = n - 1; i >= 0; i--) {
432c733ed4SBarry Smith     v   = aa + 9 * diag[i] - 9;
442c733ed4SBarry Smith     vi  = aj + diag[i] - 1;
452c733ed4SBarry Smith     nz  = diag[i] - ai[i];
462c733ed4SBarry Smith     idt = 3 * i;
47*9371c9d4SSatish Balay     s1  = x[idt];
48*9371c9d4SSatish Balay     s2  = x[1 + idt];
49*9371c9d4SSatish Balay     s3  = x[2 + idt];
502c733ed4SBarry Smith     while (nz--) {
512c733ed4SBarry Smith       idx = 3 * (*vi--);
522c733ed4SBarry Smith       x[idx] -= v[0] * s1 + v[1] * s2 + v[2] * s3;
532c733ed4SBarry Smith       x[idx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3;
542c733ed4SBarry Smith       x[idx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3;
552c733ed4SBarry Smith       v -= 9;
562c733ed4SBarry Smith     }
572c733ed4SBarry Smith   }
589566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx, &x));
599566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0 * 9.0 * (a->nz) - 3.0 * A->cmap->n));
602c733ed4SBarry Smith   PetscFunctionReturn(0);
612c733ed4SBarry Smith }
622c733ed4SBarry Smith 
63*9371c9d4SSatish Balay PetscErrorCode MatSolveTranspose_SeqBAIJ_3_NaturalOrdering(Mat A, Vec bb, Vec xx) {
642c733ed4SBarry Smith   Mat_SeqBAIJ     *a = (Mat_SeqBAIJ *)A->data;
652c733ed4SBarry Smith   const PetscInt   n = a->mbs, *vi, *ai = a->i, *aj = a->j, *diag = a->diag;
662c733ed4SBarry Smith   PetscInt         nz, idx, idt, j, i, oidx;
672c733ed4SBarry Smith   const PetscInt   bs = A->rmap->bs, bs2 = a->bs2;
682c733ed4SBarry Smith   const MatScalar *aa = a->a, *v;
692c733ed4SBarry Smith   PetscScalar      s1, s2, s3, x1, x2, x3, *x;
702c733ed4SBarry Smith 
712c733ed4SBarry Smith   PetscFunctionBegin;
729566063dSJacob Faibussowitsch   PetscCall(VecCopy(bb, xx));
739566063dSJacob Faibussowitsch   PetscCall(VecGetArray(xx, &x));
742c733ed4SBarry Smith 
752c733ed4SBarry Smith   /* forward solve the U^T */
762c733ed4SBarry Smith   idx = 0;
772c733ed4SBarry Smith   for (i = 0; i < n; i++) {
782c733ed4SBarry Smith     v  = aa + bs2 * diag[i];
792c733ed4SBarry Smith     /* multiply by the inverse of the block diagonal */
80*9371c9d4SSatish Balay     x1 = x[idx];
81*9371c9d4SSatish Balay     x2 = x[1 + idx];
82*9371c9d4SSatish Balay     x3 = x[2 + idx];
832c733ed4SBarry Smith     s1 = v[0] * x1 + v[1] * x2 + v[2] * x3;
842c733ed4SBarry Smith     s2 = v[3] * x1 + v[4] * x2 + v[5] * x3;
852c733ed4SBarry Smith     s3 = v[6] * x1 + v[7] * x2 + v[8] * x3;
862c733ed4SBarry Smith     v -= bs2;
872c733ed4SBarry Smith 
882c733ed4SBarry Smith     vi = aj + diag[i] - 1;
892c733ed4SBarry Smith     nz = diag[i] - diag[i + 1] - 1;
902c733ed4SBarry Smith     for (j = 0; j > -nz; j--) {
912c733ed4SBarry Smith       oidx = bs * vi[j];
922c733ed4SBarry Smith       x[oidx] -= v[0] * s1 + v[1] * s2 + v[2] * s3;
932c733ed4SBarry Smith       x[oidx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3;
942c733ed4SBarry Smith       x[oidx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3;
952c733ed4SBarry Smith       v -= bs2;
962c733ed4SBarry Smith     }
97*9371c9d4SSatish Balay     x[idx]     = s1;
98*9371c9d4SSatish Balay     x[1 + idx] = s2;
99*9371c9d4SSatish Balay     x[2 + idx] = s3;
1002c733ed4SBarry Smith     idx += bs;
1012c733ed4SBarry Smith   }
1022c733ed4SBarry Smith   /* backward solve the L^T */
1032c733ed4SBarry Smith   for (i = n - 1; i >= 0; i--) {
1042c733ed4SBarry Smith     v   = aa + bs2 * ai[i];
1052c733ed4SBarry Smith     vi  = aj + ai[i];
1062c733ed4SBarry Smith     nz  = ai[i + 1] - ai[i];
1072c733ed4SBarry Smith     idt = bs * i;
108*9371c9d4SSatish Balay     s1  = x[idt];
109*9371c9d4SSatish Balay     s2  = x[1 + idt];
110*9371c9d4SSatish Balay     s3  = x[2 + idt];
1112c733ed4SBarry Smith     for (j = 0; j < nz; j++) {
1122c733ed4SBarry Smith       idx = bs * vi[j];
1132c733ed4SBarry Smith       x[idx] -= v[0] * s1 + v[1] * s2 + v[2] * s3;
1142c733ed4SBarry Smith       x[idx + 1] -= v[3] * s1 + v[4] * s2 + v[5] * s3;
1152c733ed4SBarry Smith       x[idx + 2] -= v[6] * s1 + v[7] * s2 + v[8] * s3;
1162c733ed4SBarry Smith       v += bs2;
1172c733ed4SBarry Smith     }
1182c733ed4SBarry Smith   }
1199566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(xx, &x));
1209566063dSJacob Faibussowitsch   PetscCall(PetscLogFlops(2.0 * bs2 * (a->nz) - bs * A->cmap->n));
1212c733ed4SBarry Smith   PetscFunctionReturn(0);
1222c733ed4SBarry Smith }
123