xref: /petsc/src/mat/impls/baij/seq/baijfact2.c (revision b588c5a2f37da9e74d7d91cdaec80d3d0decb4d9)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2be1d678aSKris Buschelman 
3a4005a5dSBarry Smith 
44e2b4712SSatish Balay /*
54e2b4712SSatish Balay     Factorization code for BAIJ format.
64e2b4712SSatish Balay */
74e2b4712SSatish Balay 
87c4f633dSBarry Smith #include "../src/mat/impls/baij/seq/baij.h"
9c60f0209SBarry Smith #include "../src/mat/blockinvert.h"
1016a2bf60SHong Zhang #include "petscbt.h"
1116a2bf60SHong Zhang #include "../src/mat/utils/freespace.h"
124e2b4712SSatish Balay 
134a2ae208SSatish Balay #undef __FUNCT__
144a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_1_NaturalOrdering"
15dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_1_NaturalOrdering(Mat A,Vec bb,Vec xx)
16f1af5d2fSBarry Smith {
17f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
18dfbe8321SBarry Smith   PetscErrorCode ierr;
19690b6cddSBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz;
20690b6cddSBarry Smith   PetscInt       *diag = a->diag;
21f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
2287828ca2SBarry Smith   PetscScalar    s1,*x,*b;
23f1af5d2fSBarry Smith 
24f1af5d2fSBarry Smith   PetscFunctionBegin;
25ef66eb69SBarry Smith   ierr = VecCopy(bb,xx);CHKERRQ(ierr);
261ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
271ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
28f1af5d2fSBarry Smith 
29f1af5d2fSBarry Smith   /* forward solve the U^T */
30f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
31f1af5d2fSBarry Smith 
32f1af5d2fSBarry Smith     v     = aa + diag[i];
33f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
34ef66eb69SBarry Smith     s1    = (*v++)*x[i];
35f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
36f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
37f1af5d2fSBarry Smith     while (nz--) {
38f1af5d2fSBarry Smith       x[*vi++]  -= (*v++)*s1;
39f1af5d2fSBarry Smith     }
40f1af5d2fSBarry Smith     x[i]   = s1;
41f1af5d2fSBarry Smith   }
42f1af5d2fSBarry Smith   /* backward solve the L^T */
43f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
44f1af5d2fSBarry Smith     v    = aa + diag[i] - 1;
45f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
46f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
47f1af5d2fSBarry Smith     s1   = x[i];
48f1af5d2fSBarry Smith     while (nz--) {
49f1af5d2fSBarry Smith       x[*vi--]   -=  (*v--)*s1;
50f1af5d2fSBarry Smith     }
51f1af5d2fSBarry Smith   }
521ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
531ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
54dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*(a->nz) - A->cmap->n);CHKERRQ(ierr);
55f1af5d2fSBarry Smith   PetscFunctionReturn(0);
56f1af5d2fSBarry Smith }
57f1af5d2fSBarry Smith 
584a2ae208SSatish Balay #undef __FUNCT__
594a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_2_NaturalOrdering"
60dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_2_NaturalOrdering(Mat A,Vec bb,Vec xx)
61f1af5d2fSBarry Smith {
62f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
63dfbe8321SBarry Smith   PetscErrorCode ierr;
64690b6cddSBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
65690b6cddSBarry Smith   PetscInt       *diag = a->diag,oidx;
66f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
6787828ca2SBarry Smith   PetscScalar    s1,s2,x1,x2;
6887828ca2SBarry Smith   PetscScalar    *x,*b;
69f1af5d2fSBarry Smith 
70f1af5d2fSBarry Smith   PetscFunctionBegin;
71ef66eb69SBarry Smith   ierr = VecCopy(bb,xx);CHKERRQ(ierr);
721ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
731ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
74f1af5d2fSBarry Smith 
75f1af5d2fSBarry Smith   /* forward solve the U^T */
76f1af5d2fSBarry Smith   idx = 0;
77f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
78f1af5d2fSBarry Smith 
79f1af5d2fSBarry Smith     v     = aa + 4*diag[i];
80f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
81ef66eb69SBarry Smith     x1 = x[idx];   x2 = x[1+idx];
82f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2;
83f1af5d2fSBarry Smith     s2 = v[2]*x1  +  v[3]*x2;
84f1af5d2fSBarry Smith     v += 4;
85f1af5d2fSBarry Smith 
86f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
87f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
88f1af5d2fSBarry Smith     while (nz--) {
89f1af5d2fSBarry Smith       oidx = 2*(*vi++);
90f1af5d2fSBarry Smith       x[oidx]   -= v[0]*s1  +  v[1]*s2;
91f1af5d2fSBarry Smith       x[oidx+1] -= v[2]*s1  +  v[3]*s2;
92f1af5d2fSBarry Smith       v  += 4;
93f1af5d2fSBarry Smith     }
94f1af5d2fSBarry Smith     x[idx]   = s1;x[1+idx] = s2;
95f1af5d2fSBarry Smith     idx += 2;
96f1af5d2fSBarry Smith   }
97f1af5d2fSBarry Smith   /* backward solve the L^T */
98f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
99f1af5d2fSBarry Smith     v    = aa + 4*diag[i] - 4;
100f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
101f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
102f1af5d2fSBarry Smith     idt  = 2*i;
103f1af5d2fSBarry Smith     s1   = x[idt];  s2 = x[1+idt];
104f1af5d2fSBarry Smith     while (nz--) {
105f1af5d2fSBarry Smith       idx   = 2*(*vi--);
106f1af5d2fSBarry Smith       x[idx]   -=  v[0]*s1 +  v[1]*s2;
107f1af5d2fSBarry Smith       x[idx+1] -=  v[2]*s1 +  v[3]*s2;
108f1af5d2fSBarry Smith       v -= 4;
109f1af5d2fSBarry Smith     }
110f1af5d2fSBarry Smith   }
1111ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
1121ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
113dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*4.0*(a->nz) - 2.0*A->cmap->n);CHKERRQ(ierr);
114f1af5d2fSBarry Smith   PetscFunctionReturn(0);
115f1af5d2fSBarry Smith }
116f1af5d2fSBarry Smith 
1174a2ae208SSatish Balay #undef __FUNCT__
1184a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_3_NaturalOrdering"
119dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_3_NaturalOrdering(Mat A,Vec bb,Vec xx)
120f1af5d2fSBarry Smith {
121f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
122dfbe8321SBarry Smith   PetscErrorCode ierr;
123690b6cddSBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
124690b6cddSBarry Smith   PetscInt       *diag = a->diag,oidx;
125f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
12687828ca2SBarry Smith   PetscScalar    s1,s2,s3,x1,x2,x3;
12787828ca2SBarry Smith   PetscScalar    *x,*b;
128f1af5d2fSBarry Smith 
129f1af5d2fSBarry Smith   PetscFunctionBegin;
130ef66eb69SBarry Smith   ierr = VecCopy(bb,xx);CHKERRQ(ierr);
1311ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
1321ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
133f1af5d2fSBarry Smith 
134f1af5d2fSBarry Smith   /* forward solve the U^T */
135f1af5d2fSBarry Smith   idx = 0;
136f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
137f1af5d2fSBarry Smith 
138f1af5d2fSBarry Smith     v     = aa + 9*diag[i];
139f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
140ef66eb69SBarry Smith     x1 = x[idx];   x2 = x[1+idx]; x3    = x[2+idx];
141f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3;
142f1af5d2fSBarry Smith     s2 = v[3]*x1  +  v[4]*x2 +  v[5]*x3;
143f1af5d2fSBarry Smith     s3 = v[6]*x1  +  v[7]*x2 + v[8]*x3;
144f1af5d2fSBarry Smith     v += 9;
145f1af5d2fSBarry Smith 
146f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
147f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
148f1af5d2fSBarry Smith     while (nz--) {
149f1af5d2fSBarry Smith       oidx = 3*(*vi++);
150f1af5d2fSBarry Smith       x[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3;
151f1af5d2fSBarry Smith       x[oidx+1] -= v[3]*s1  +  v[4]*s2 +  v[5]*s3;
152f1af5d2fSBarry Smith       x[oidx+2] -= v[6]*s1 + v[7]*s2 + v[8]*s3;
153f1af5d2fSBarry Smith       v  += 9;
154f1af5d2fSBarry Smith     }
155f1af5d2fSBarry Smith     x[idx]   = s1;x[1+idx] = s2; x[2+idx] = s3;
156f1af5d2fSBarry Smith     idx += 3;
157f1af5d2fSBarry Smith   }
158f1af5d2fSBarry Smith   /* backward solve the L^T */
159f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
160f1af5d2fSBarry Smith     v    = aa + 9*diag[i] - 9;
161f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
162f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
163f1af5d2fSBarry Smith     idt  = 3*i;
164f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt]; s3 = x[2+idt];
165f1af5d2fSBarry Smith     while (nz--) {
166f1af5d2fSBarry Smith       idx   = 3*(*vi--);
167f1af5d2fSBarry Smith       x[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3;
168f1af5d2fSBarry Smith       x[idx+1] -=  v[3]*s1 +  v[4]*s2 +  v[5]*s3;
169f1af5d2fSBarry Smith       x[idx+2] -= v[6]*s1 + v[7]*s2 + v[8]*s3;
170f1af5d2fSBarry Smith       v -= 9;
171f1af5d2fSBarry Smith     }
172f1af5d2fSBarry Smith   }
1731ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
1741ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
175dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*9.0*(a->nz) - 3.0*A->cmap->n);CHKERRQ(ierr);
176f1af5d2fSBarry Smith   PetscFunctionReturn(0);
177f1af5d2fSBarry Smith }
178f1af5d2fSBarry Smith 
1794a2ae208SSatish Balay #undef __FUNCT__
1804a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_4_NaturalOrdering"
181dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_4_NaturalOrdering(Mat A,Vec bb,Vec xx)
182f1af5d2fSBarry Smith {
183f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
184dfbe8321SBarry Smith   PetscErrorCode ierr;
185690b6cddSBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
186690b6cddSBarry Smith   PetscInt       *diag = a->diag,oidx;
187f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
18887828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,x1,x2,x3,x4;
18987828ca2SBarry Smith   PetscScalar    *x,*b;
190f1af5d2fSBarry Smith 
191f1af5d2fSBarry Smith   PetscFunctionBegin;
192ef66eb69SBarry Smith   ierr = VecCopy(bb,xx);CHKERRQ(ierr);
1931ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
1941ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
195f1af5d2fSBarry Smith 
196f1af5d2fSBarry Smith   /* forward solve the U^T */
197f1af5d2fSBarry Smith   idx = 0;
198f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
199f1af5d2fSBarry Smith 
200f1af5d2fSBarry Smith     v     = aa + 16*diag[i];
201f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
202ef66eb69SBarry Smith     x1    = x[idx];   x2 = x[1+idx]; x3    = x[2+idx]; x4 = x[3+idx];
203f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4;
204f1af5d2fSBarry Smith     s2 = v[4]*x1  +  v[5]*x2 +  v[6]*x3 +  v[7]*x4;
205f1af5d2fSBarry Smith     s3 = v[8]*x1  +  v[9]*x2 + v[10]*x3 + v[11]*x4;
206f1af5d2fSBarry Smith     s4 = v[12]*x1 + v[13]*x2 + v[14]*x3 + v[15]*x4;
207f1af5d2fSBarry Smith     v += 16;
208f1af5d2fSBarry Smith 
209f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
210f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
211f1af5d2fSBarry Smith     while (nz--) {
212f1af5d2fSBarry Smith       oidx = 4*(*vi++);
213f1af5d2fSBarry Smith       x[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4;
214f1af5d2fSBarry Smith       x[oidx+1] -= v[4]*s1  +  v[5]*s2 +  v[6]*s3 +  v[7]*s4;
215f1af5d2fSBarry Smith       x[oidx+2] -= v[8]*s1 + v[9]*s2 + v[10]*s3 + v[11]*s4;
216f1af5d2fSBarry Smith       x[oidx+3] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4;
217f1af5d2fSBarry Smith       v  += 16;
218f1af5d2fSBarry Smith     }
219f1af5d2fSBarry Smith     x[idx]   = s1;x[1+idx] = s2; x[2+idx] = s3;x[3+idx] = s4;
220f1af5d2fSBarry Smith     idx += 4;
221f1af5d2fSBarry Smith   }
222f1af5d2fSBarry Smith   /* backward solve the L^T */
223f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
224f1af5d2fSBarry Smith     v    = aa + 16*diag[i] - 16;
225f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
226f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
227f1af5d2fSBarry Smith     idt  = 4*i;
228f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt]; s3 = x[2+idt];s4 = x[3+idt];
229f1af5d2fSBarry Smith     while (nz--) {
230f1af5d2fSBarry Smith       idx   = 4*(*vi--);
231f1af5d2fSBarry Smith       x[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4;
232f1af5d2fSBarry Smith       x[idx+1] -=  v[4]*s1 +  v[5]*s2 +  v[6]*s3 +  v[7]*s4;
233f1af5d2fSBarry Smith       x[idx+2] -= v[8]*s1 + v[9]*s2 + v[10]*s3 + v[11]*s4;
234f1af5d2fSBarry Smith       x[idx+3] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4;
235f1af5d2fSBarry Smith       v -= 16;
236f1af5d2fSBarry Smith     }
237f1af5d2fSBarry Smith   }
2381ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
2391ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
240dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
241f1af5d2fSBarry Smith   PetscFunctionReturn(0);
242f1af5d2fSBarry Smith }
243f1af5d2fSBarry Smith 
2444a2ae208SSatish Balay #undef __FUNCT__
2454a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_5_NaturalOrdering"
246dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_5_NaturalOrdering(Mat A,Vec bb,Vec xx)
247f1af5d2fSBarry Smith {
248f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
249dfbe8321SBarry Smith   PetscErrorCode ierr;
250690b6cddSBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
251690b6cddSBarry Smith   PetscInt       *diag = a->diag,oidx;
252f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
25387828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,s5,x1,x2,x3,x4,x5;
25487828ca2SBarry Smith   PetscScalar    *x,*b;
255f1af5d2fSBarry Smith 
256f1af5d2fSBarry Smith   PetscFunctionBegin;
257ef66eb69SBarry Smith   ierr = VecCopy(bb,xx);CHKERRQ(ierr);
2581ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
2591ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
260f1af5d2fSBarry Smith 
261f1af5d2fSBarry Smith   /* forward solve the U^T */
262f1af5d2fSBarry Smith   idx = 0;
263f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
264f1af5d2fSBarry Smith 
265f1af5d2fSBarry Smith     v     = aa + 25*diag[i];
266f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
267ef66eb69SBarry Smith     x1    = x[idx];   x2 = x[1+idx]; x3    = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
268f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5;
269f1af5d2fSBarry Smith     s2 = v[5]*x1  +  v[6]*x2 +  v[7]*x3 +  v[8]*x4 +  v[9]*x5;
270f1af5d2fSBarry Smith     s3 = v[10]*x1 + v[11]*x2 + v[12]*x3 + v[13]*x4 + v[14]*x5;
271f1af5d2fSBarry Smith     s4 = v[15]*x1 + v[16]*x2 + v[17]*x3 + v[18]*x4 + v[19]*x5;
272f1af5d2fSBarry Smith     s5 = v[20]*x1 + v[21]*x2 + v[22]*x3 + v[23]*x4 + v[24]*x5;
273f1af5d2fSBarry Smith     v += 25;
274f1af5d2fSBarry Smith 
275f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
276f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
277f1af5d2fSBarry Smith     while (nz--) {
278f1af5d2fSBarry Smith       oidx = 5*(*vi++);
279f1af5d2fSBarry Smith       x[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
280f1af5d2fSBarry Smith       x[oidx+1] -= v[5]*s1  +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
281f1af5d2fSBarry Smith       x[oidx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
282f1af5d2fSBarry Smith       x[oidx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
283f1af5d2fSBarry Smith       x[oidx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
284f1af5d2fSBarry Smith       v  += 25;
285f1af5d2fSBarry Smith     }
286f1af5d2fSBarry Smith     x[idx]   = s1;x[1+idx] = s2; x[2+idx] = s3;x[3+idx] = s4; x[4+idx] = s5;
287f1af5d2fSBarry Smith     idx += 5;
288f1af5d2fSBarry Smith   }
289f1af5d2fSBarry Smith   /* backward solve the L^T */
290f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
291f1af5d2fSBarry Smith     v    = aa + 25*diag[i] - 25;
292f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
293f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
294f1af5d2fSBarry Smith     idt  = 5*i;
295f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt]; s3 = x[2+idt];s4 = x[3+idt]; s5 = x[4+idt];
296f1af5d2fSBarry Smith     while (nz--) {
297f1af5d2fSBarry Smith       idx   = 5*(*vi--);
298f1af5d2fSBarry Smith       x[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
299f1af5d2fSBarry Smith       x[idx+1] -=  v[5]*s1 +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
300f1af5d2fSBarry Smith       x[idx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
301f1af5d2fSBarry Smith       x[idx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
302f1af5d2fSBarry Smith       x[idx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
303f1af5d2fSBarry Smith       v -= 25;
304f1af5d2fSBarry Smith     }
305f1af5d2fSBarry Smith   }
3061ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
3071ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
308dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n);CHKERRQ(ierr);
309f1af5d2fSBarry Smith   PetscFunctionReturn(0);
310f1af5d2fSBarry Smith }
311f1af5d2fSBarry Smith 
3124a2ae208SSatish Balay #undef __FUNCT__
3134a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_6_NaturalOrdering"
314dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_6_NaturalOrdering(Mat A,Vec bb,Vec xx)
315f1af5d2fSBarry Smith {
316f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
317dfbe8321SBarry Smith   PetscErrorCode ierr;
318690b6cddSBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
319690b6cddSBarry Smith   PetscInt       *diag = a->diag,oidx;
320f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
32187828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,s5,s6,x1,x2,x3,x4,x5,x6;
32287828ca2SBarry Smith   PetscScalar    *x,*b;
323f1af5d2fSBarry Smith 
324f1af5d2fSBarry Smith   PetscFunctionBegin;
325ef66eb69SBarry Smith   ierr = VecCopy(bb,xx);CHKERRQ(ierr);
3261ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
3271ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
328f1af5d2fSBarry Smith 
329f1af5d2fSBarry Smith   /* forward solve the U^T */
330f1af5d2fSBarry Smith   idx = 0;
331f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
332f1af5d2fSBarry Smith 
333f1af5d2fSBarry Smith     v     = aa + 36*diag[i];
334f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
335ef66eb69SBarry Smith     x1    = x[idx];   x2 = x[1+idx]; x3    = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
336ef66eb69SBarry Smith     x6    = x[5+idx];
337f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5 +  v[5]*x6;
338f1af5d2fSBarry Smith     s2 = v[6]*x1  +  v[7]*x2 +  v[8]*x3 +  v[9]*x4 + v[10]*x5 + v[11]*x6;
339f1af5d2fSBarry Smith     s3 = v[12]*x1 + v[13]*x2 + v[14]*x3 + v[15]*x4 + v[16]*x5 + v[17]*x6;
340f1af5d2fSBarry Smith     s4 = v[18]*x1 + v[19]*x2 + v[20]*x3 + v[21]*x4 + v[22]*x5 + v[23]*x6;
341f1af5d2fSBarry Smith     s5 = v[24]*x1 + v[25]*x2 + v[26]*x3 + v[27]*x4 + v[28]*x5 + v[29]*x6;
342f1af5d2fSBarry Smith     s6 = v[30]*x1 + v[31]*x2 + v[32]*x3 + v[33]*x4 + v[34]*x5 + v[35]*x6;
343f1af5d2fSBarry Smith     v += 36;
344f1af5d2fSBarry Smith 
345f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
346f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
347f1af5d2fSBarry Smith     while (nz--) {
348f1af5d2fSBarry Smith       oidx = 6*(*vi++);
349f1af5d2fSBarry Smith       x[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6;
350f1af5d2fSBarry Smith       x[oidx+1] -= v[6]*s1  +  v[7]*s2 +  v[8]*s3 +  v[9]*s4 + v[10]*s5 + v[11]*s6;
351f1af5d2fSBarry Smith       x[oidx+2] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4 + v[16]*s5 + v[17]*s6;
352f1af5d2fSBarry Smith       x[oidx+3] -= v[18]*s1 + v[19]*s2 + v[20]*s3 + v[21]*s4 + v[22]*s5 + v[23]*s6;
353f1af5d2fSBarry Smith       x[oidx+4] -= v[24]*s1 + v[25]*s2 + v[26]*s3 + v[27]*s4 + v[28]*s5 + v[29]*s6;
354f1af5d2fSBarry Smith       x[oidx+5] -= v[30]*s1 + v[31]*s2 + v[32]*s3 + v[33]*s4 + v[34]*s5 + v[35]*s6;
355f1af5d2fSBarry Smith       v  += 36;
356f1af5d2fSBarry Smith     }
357f1af5d2fSBarry Smith     x[idx]   = s1;x[1+idx] = s2; x[2+idx] = s3;x[3+idx] = s4; x[4+idx] = s5;
358f1af5d2fSBarry Smith     x[5+idx] = s6;
359f1af5d2fSBarry Smith     idx += 6;
360f1af5d2fSBarry Smith   }
361f1af5d2fSBarry Smith   /* backward solve the L^T */
362f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
363f1af5d2fSBarry Smith     v    = aa + 36*diag[i] - 36;
364f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
365f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
366f1af5d2fSBarry Smith     idt  = 6*i;
367f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt]; s3 = x[2+idt];s4 = x[3+idt]; s5 = x[4+idt];
368f1af5d2fSBarry Smith     s6 = x[5+idt];
369f1af5d2fSBarry Smith     while (nz--) {
370f1af5d2fSBarry Smith       idx   = 6*(*vi--);
371f1af5d2fSBarry Smith       x[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6;
372f1af5d2fSBarry Smith       x[idx+1] -=  v[6]*s1 +  v[7]*s2 +  v[8]*s3 +  v[9]*s4 + v[10]*s5 + v[11]*s6;
373f1af5d2fSBarry Smith       x[idx+2] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4 + v[16]*s5 + v[17]*s6;
374f1af5d2fSBarry Smith       x[idx+3] -= v[18]*s1 + v[19]*s2 + v[20]*s3 + v[21]*s4 + v[22]*s5 + v[23]*s6;
375f1af5d2fSBarry Smith       x[idx+4] -= v[24]*s1 + v[25]*s2 + v[26]*s3 + v[27]*s4 + v[28]*s5 + v[29]*s6;
376f1af5d2fSBarry Smith       x[idx+5] -= v[30]*s1 + v[31]*s2 + v[32]*s3 + v[33]*s4 + v[34]*s5 + v[35]*s6;
377f1af5d2fSBarry Smith       v -= 36;
378f1af5d2fSBarry Smith     }
379f1af5d2fSBarry Smith   }
3801ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
3811ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
382dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*36*(a->nz) - 6.0*A->cmap->n);CHKERRQ(ierr);
383f1af5d2fSBarry Smith   PetscFunctionReturn(0);
384f1af5d2fSBarry Smith }
385f1af5d2fSBarry Smith 
3864a2ae208SSatish Balay #undef __FUNCT__
3874a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_7_NaturalOrdering"
388dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_7_NaturalOrdering(Mat A,Vec bb,Vec xx)
389f1af5d2fSBarry Smith {
390f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
391dfbe8321SBarry Smith   PetscErrorCode ierr;
392690b6cddSBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
393690b6cddSBarry Smith   PetscInt       *diag = a->diag,oidx;
394f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
39587828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
39687828ca2SBarry Smith   PetscScalar    *x,*b;
397f1af5d2fSBarry Smith 
398f1af5d2fSBarry Smith   PetscFunctionBegin;
399ef66eb69SBarry Smith   ierr = VecCopy(bb,xx);CHKERRQ(ierr);
4001ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
4011ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
402f1af5d2fSBarry Smith 
403f1af5d2fSBarry Smith   /* forward solve the U^T */
404f1af5d2fSBarry Smith   idx = 0;
405f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
406f1af5d2fSBarry Smith 
407f1af5d2fSBarry Smith     v     = aa + 49*diag[i];
408f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
409ef66eb69SBarry Smith     x1    = x[idx];   x2 = x[1+idx]; x3    = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
410ef66eb69SBarry Smith     x6    = x[5+idx]; x7 = x[6+idx];
411f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5 +  v[5]*x6 +  v[6]*x7;
412f1af5d2fSBarry Smith     s2 = v[7]*x1  +  v[8]*x2 +  v[9]*x3 + v[10]*x4 + v[11]*x5 + v[12]*x6 + v[13]*x7;
413f1af5d2fSBarry Smith     s3 = v[14]*x1 + v[15]*x2 + v[16]*x3 + v[17]*x4 + v[18]*x5 + v[19]*x6 + v[20]*x7;
414f1af5d2fSBarry Smith     s4 = v[21]*x1 + v[22]*x2 + v[23]*x3 + v[24]*x4 + v[25]*x5 + v[26]*x6 + v[27]*x7;
415f1af5d2fSBarry Smith     s5 = v[28]*x1 + v[29]*x2 + v[30]*x3 + v[31]*x4 + v[32]*x5 + v[33]*x6 + v[34]*x7;
416f1af5d2fSBarry Smith     s6 = v[35]*x1 + v[36]*x2 + v[37]*x3 + v[38]*x4 + v[39]*x5 + v[40]*x6 + v[41]*x7;
417f1af5d2fSBarry Smith     s7 = v[42]*x1 + v[43]*x2 + v[44]*x3 + v[45]*x4 + v[46]*x5 + v[47]*x6 + v[48]*x7;
418f1af5d2fSBarry Smith     v += 49;
419f1af5d2fSBarry Smith 
420f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
421f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
422f1af5d2fSBarry Smith     while (nz--) {
423f1af5d2fSBarry Smith       oidx = 7*(*vi++);
424f1af5d2fSBarry Smith       x[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6 +  v[6]*s7;
425f1af5d2fSBarry Smith       x[oidx+1] -= v[7]*s1  +  v[8]*s2 +  v[9]*s3 + v[10]*s4 + v[11]*s5 + v[12]*s6 + v[13]*s7;
426f1af5d2fSBarry Smith       x[oidx+2] -= v[14]*s1 + v[15]*s2 + v[16]*s3 + v[17]*s4 + v[18]*s5 + v[19]*s6 + v[20]*s7;
427f1af5d2fSBarry Smith       x[oidx+3] -= v[21]*s1 + v[22]*s2 + v[23]*s3 + v[24]*s4 + v[25]*s5 + v[26]*s6 + v[27]*s7;
428f1af5d2fSBarry Smith       x[oidx+4] -= v[28]*s1 + v[29]*s2 + v[30]*s3 + v[31]*s4 + v[32]*s5 + v[33]*s6 + v[34]*s7;
429f1af5d2fSBarry Smith       x[oidx+5] -= v[35]*s1 + v[36]*s2 + v[37]*s3 + v[38]*s4 + v[39]*s5 + v[40]*s6 + v[41]*s7;
430f1af5d2fSBarry Smith       x[oidx+6] -= v[42]*s1 + v[43]*s2 + v[44]*s3 + v[45]*s4 + v[46]*s5 + v[47]*s6 + v[48]*s7;
431f1af5d2fSBarry Smith       v  += 49;
432f1af5d2fSBarry Smith     }
433f1af5d2fSBarry Smith     x[idx]   = s1;x[1+idx] = s2; x[2+idx] = s3;x[3+idx] = s4; x[4+idx] = s5;
434f1af5d2fSBarry Smith     x[5+idx] = s6;x[6+idx] = s7;
435f1af5d2fSBarry Smith     idx += 7;
436f1af5d2fSBarry Smith   }
437f1af5d2fSBarry Smith   /* backward solve the L^T */
438f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
439f1af5d2fSBarry Smith     v    = aa + 49*diag[i] - 49;
440f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
441f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
442f1af5d2fSBarry Smith     idt  = 7*i;
443f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt]; s3 = x[2+idt];s4 = x[3+idt]; s5 = x[4+idt];
444f1af5d2fSBarry Smith     s6 = x[5+idt];s7 = x[6+idt];
445f1af5d2fSBarry Smith     while (nz--) {
446f1af5d2fSBarry Smith       idx   = 7*(*vi--);
447f1af5d2fSBarry Smith       x[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6 +  v[6]*s7;
448f1af5d2fSBarry Smith       x[idx+1] -=  v[7]*s1 +  v[8]*s2 +  v[9]*s3 + v[10]*s4 + v[11]*s5 + v[12]*s6 + v[13]*s7;
449f1af5d2fSBarry Smith       x[idx+2] -= v[14]*s1 + v[15]*s2 + v[16]*s3 + v[17]*s4 + v[18]*s5 + v[19]*s6 + v[20]*s7;
450f1af5d2fSBarry Smith       x[idx+3] -= v[21]*s1 + v[22]*s2 + v[23]*s3 + v[24]*s4 + v[25]*s5 + v[26]*s6 + v[27]*s7;
451f1af5d2fSBarry Smith       x[idx+4] -= v[28]*s1 + v[29]*s2 + v[30]*s3 + v[31]*s4 + v[32]*s5 + v[33]*s6 + v[34]*s7;
452f1af5d2fSBarry Smith       x[idx+5] -= v[35]*s1 + v[36]*s2 + v[37]*s3 + v[38]*s4 + v[39]*s5 + v[40]*s6 + v[41]*s7;
453f1af5d2fSBarry Smith       x[idx+6] -= v[42]*s1 + v[43]*s2 + v[44]*s3 + v[45]*s4 + v[46]*s5 + v[47]*s6 + v[48]*s7;
454f1af5d2fSBarry Smith       v -= 49;
455f1af5d2fSBarry Smith     }
456f1af5d2fSBarry Smith   }
4571ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
4581ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
459dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*49*(a->nz) - 7.0*A->cmap->n);CHKERRQ(ierr);
460f1af5d2fSBarry Smith   PetscFunctionReturn(0);
461f1af5d2fSBarry Smith }
462f1af5d2fSBarry Smith 
463f1af5d2fSBarry Smith /*---------------------------------------------------------------------------------------------*/
4644a2ae208SSatish Balay #undef __FUNCT__
4654a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_1"
466dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_1(Mat A,Vec bb,Vec xx)
467f1af5d2fSBarry Smith {
468f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
469f1af5d2fSBarry Smith   IS             iscol=a->col,isrow=a->row;
4706849ba73SBarry Smith   PetscErrorCode ierr;
4715d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout;
4725d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz;
473690b6cddSBarry Smith   PetscInt       *diag = a->diag;
474f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
47587828ca2SBarry Smith   PetscScalar    s1,*x,*b,*t;
476f1af5d2fSBarry Smith 
477f1af5d2fSBarry Smith   PetscFunctionBegin;
4781ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
4791ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
480f1af5d2fSBarry Smith   t  = a->solve_work;
481f1af5d2fSBarry Smith 
482f1af5d2fSBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
483f1af5d2fSBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
484f1af5d2fSBarry Smith 
485f1af5d2fSBarry Smith   /* copy the b into temp work space according to permutation */
486f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
487f1af5d2fSBarry Smith     t[i] = b[c[i]];
488f1af5d2fSBarry Smith   }
489f1af5d2fSBarry Smith 
490f1af5d2fSBarry Smith   /* forward solve the U^T */
491f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
492f1af5d2fSBarry Smith 
493f1af5d2fSBarry Smith     v     = aa + diag[i];
494f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
495f1af5d2fSBarry Smith     s1    = (*v++)*t[i];
496f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
497f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
498f1af5d2fSBarry Smith     while (nz--) {
499f1af5d2fSBarry Smith       t[*vi++]  -= (*v++)*s1;
500f1af5d2fSBarry Smith     }
501f1af5d2fSBarry Smith     t[i]   = s1;
502f1af5d2fSBarry Smith   }
503f1af5d2fSBarry Smith   /* backward solve the L^T */
504f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
505f1af5d2fSBarry Smith     v    = aa + diag[i] - 1;
506f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
507f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
508f1af5d2fSBarry Smith     s1   = t[i];
509f1af5d2fSBarry Smith     while (nz--) {
510f1af5d2fSBarry Smith       t[*vi--]   -=  (*v--)*s1;
511f1af5d2fSBarry Smith     }
512f1af5d2fSBarry Smith   }
513f1af5d2fSBarry Smith 
514f1af5d2fSBarry Smith   /* copy t into x according to permutation */
515f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
516f1af5d2fSBarry Smith     x[r[i]]   = t[i];
517f1af5d2fSBarry Smith   }
518f1af5d2fSBarry Smith 
519f1af5d2fSBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
520f1af5d2fSBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
5211ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
5221ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
523dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*(a->nz) - A->cmap->n);CHKERRQ(ierr);
524f1af5d2fSBarry Smith   PetscFunctionReturn(0);
525f1af5d2fSBarry Smith }
526f1af5d2fSBarry Smith 
5274a2ae208SSatish Balay #undef __FUNCT__
5284a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_2"
529dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_2(Mat A,Vec bb,Vec xx)
530f1af5d2fSBarry Smith {
531f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
532f1af5d2fSBarry Smith   IS             iscol=a->col,isrow=a->row;
5336849ba73SBarry Smith   PetscErrorCode ierr;
5345d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout;
5355d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
536690b6cddSBarry Smith   PetscInt       *diag = a->diag,ii,ic,ir,oidx;
537f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
53887828ca2SBarry Smith   PetscScalar    s1,s2,x1,x2;
53987828ca2SBarry Smith   PetscScalar    *x,*b,*t;
540f1af5d2fSBarry Smith 
541f1af5d2fSBarry Smith   PetscFunctionBegin;
5421ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
5431ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
544f1af5d2fSBarry Smith   t  = a->solve_work;
545f1af5d2fSBarry Smith 
546f1af5d2fSBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
547f1af5d2fSBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
548f1af5d2fSBarry Smith 
549f1af5d2fSBarry Smith   /* copy the b into temp work space according to permutation */
550f1af5d2fSBarry Smith   ii = 0;
551f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
552f1af5d2fSBarry Smith     ic      = 2*c[i];
553f1af5d2fSBarry Smith     t[ii]   = b[ic];
554f1af5d2fSBarry Smith     t[ii+1] = b[ic+1];
555f1af5d2fSBarry Smith     ii += 2;
556f1af5d2fSBarry Smith   }
557f1af5d2fSBarry Smith 
558f1af5d2fSBarry Smith   /* forward solve the U^T */
559f1af5d2fSBarry Smith   idx = 0;
560f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
561f1af5d2fSBarry Smith 
562f1af5d2fSBarry Smith     v     = aa + 4*diag[i];
563f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
564f1af5d2fSBarry Smith     x1    = t[idx];   x2 = t[1+idx];
565f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2;
566f1af5d2fSBarry Smith     s2 = v[2]*x1  +  v[3]*x2;
567f1af5d2fSBarry Smith     v += 4;
568f1af5d2fSBarry Smith 
569f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
570f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
571f1af5d2fSBarry Smith     while (nz--) {
572f1af5d2fSBarry Smith       oidx = 2*(*vi++);
573f1af5d2fSBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2;
574f1af5d2fSBarry Smith       t[oidx+1] -= v[2]*s1  +  v[3]*s2;
575f1af5d2fSBarry Smith       v  += 4;
576f1af5d2fSBarry Smith     }
577f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2;
578f1af5d2fSBarry Smith     idx += 2;
579f1af5d2fSBarry Smith   }
580f1af5d2fSBarry Smith   /* backward solve the L^T */
581f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
582f1af5d2fSBarry Smith     v    = aa + 4*diag[i] - 4;
583f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
584f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
585f1af5d2fSBarry Smith     idt  = 2*i;
586f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt];
587f1af5d2fSBarry Smith     while (nz--) {
588f1af5d2fSBarry Smith       idx   = 2*(*vi--);
589f1af5d2fSBarry Smith       t[idx]   -=  v[0]*s1 +  v[1]*s2;
590f1af5d2fSBarry Smith       t[idx+1] -=  v[2]*s1 +  v[3]*s2;
591f1af5d2fSBarry Smith       v -= 4;
592f1af5d2fSBarry Smith     }
593f1af5d2fSBarry Smith   }
594f1af5d2fSBarry Smith 
595f1af5d2fSBarry Smith   /* copy t into x according to permutation */
596f1af5d2fSBarry Smith   ii = 0;
597f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
598f1af5d2fSBarry Smith     ir      = 2*r[i];
599f1af5d2fSBarry Smith     x[ir]   = t[ii];
600f1af5d2fSBarry Smith     x[ir+1] = t[ii+1];
601f1af5d2fSBarry Smith     ii += 2;
602f1af5d2fSBarry Smith   }
603f1af5d2fSBarry Smith 
604f1af5d2fSBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
605f1af5d2fSBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
6061ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
6071ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
608dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*4*(a->nz) - 2.0*A->cmap->n);CHKERRQ(ierr);
609f1af5d2fSBarry Smith   PetscFunctionReturn(0);
610f1af5d2fSBarry Smith }
611f1af5d2fSBarry Smith 
6124a2ae208SSatish Balay #undef __FUNCT__
6134a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_3"
614dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_3(Mat A,Vec bb,Vec xx)
615f1af5d2fSBarry Smith {
616f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
617f1af5d2fSBarry Smith   IS             iscol=a->col,isrow=a->row;
6186849ba73SBarry Smith   PetscErrorCode ierr;
6195d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout;
6205d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
621690b6cddSBarry Smith   PetscInt       *diag = a->diag,ii,ic,ir,oidx;
622f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
62387828ca2SBarry Smith   PetscScalar    s1,s2,s3,x1,x2,x3;
62487828ca2SBarry Smith   PetscScalar    *x,*b,*t;
625f1af5d2fSBarry Smith 
626f1af5d2fSBarry Smith   PetscFunctionBegin;
6271ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
6281ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
629f1af5d2fSBarry Smith   t  = a->solve_work;
630f1af5d2fSBarry Smith 
631f1af5d2fSBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
632f1af5d2fSBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
633f1af5d2fSBarry Smith 
634f1af5d2fSBarry Smith   /* copy the b into temp work space according to permutation */
635f1af5d2fSBarry Smith   ii = 0;
636f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
637f1af5d2fSBarry Smith     ic      = 3*c[i];
638f1af5d2fSBarry Smith     t[ii]   = b[ic];
639f1af5d2fSBarry Smith     t[ii+1] = b[ic+1];
640f1af5d2fSBarry Smith     t[ii+2] = b[ic+2];
641f1af5d2fSBarry Smith     ii += 3;
642f1af5d2fSBarry Smith   }
643f1af5d2fSBarry Smith 
644f1af5d2fSBarry Smith   /* forward solve the U^T */
645f1af5d2fSBarry Smith   idx = 0;
646f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
647f1af5d2fSBarry Smith 
648f1af5d2fSBarry Smith     v     = aa + 9*diag[i];
649f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
650f1af5d2fSBarry Smith     x1    = t[idx];   x2 = t[1+idx]; x3    = t[2+idx];
651f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3;
652f1af5d2fSBarry Smith     s2 = v[3]*x1  +  v[4]*x2 +  v[5]*x3;
653f1af5d2fSBarry Smith     s3 = v[6]*x1  +  v[7]*x2 + v[8]*x3;
654f1af5d2fSBarry Smith     v += 9;
655f1af5d2fSBarry Smith 
656f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
657f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
658f1af5d2fSBarry Smith     while (nz--) {
659f1af5d2fSBarry Smith       oidx = 3*(*vi++);
660f1af5d2fSBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3;
661f1af5d2fSBarry Smith       t[oidx+1] -= v[3]*s1  +  v[4]*s2 +  v[5]*s3;
662f1af5d2fSBarry Smith       t[oidx+2] -= v[6]*s1 + v[7]*s2 + v[8]*s3;
663f1af5d2fSBarry Smith       v  += 9;
664f1af5d2fSBarry Smith     }
665f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2; t[2+idx] = s3;
666f1af5d2fSBarry Smith     idx += 3;
667f1af5d2fSBarry Smith   }
668f1af5d2fSBarry Smith   /* backward solve the L^T */
669f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
670f1af5d2fSBarry Smith     v    = aa + 9*diag[i] - 9;
671f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
672f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
673f1af5d2fSBarry Smith     idt  = 3*i;
674f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt]; s3 = t[2+idt];
675f1af5d2fSBarry Smith     while (nz--) {
676f1af5d2fSBarry Smith       idx   = 3*(*vi--);
677f1af5d2fSBarry Smith       t[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3;
678f1af5d2fSBarry Smith       t[idx+1] -=  v[3]*s1 +  v[4]*s2 +  v[5]*s3;
679f1af5d2fSBarry Smith       t[idx+2] -= v[6]*s1 + v[7]*s2 + v[8]*s3;
680f1af5d2fSBarry Smith       v -= 9;
681f1af5d2fSBarry Smith     }
682f1af5d2fSBarry Smith   }
683f1af5d2fSBarry Smith 
684f1af5d2fSBarry Smith   /* copy t into x according to permutation */
685f1af5d2fSBarry Smith   ii = 0;
686f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
687f1af5d2fSBarry Smith     ir      = 3*r[i];
688f1af5d2fSBarry Smith     x[ir]   = t[ii];
689f1af5d2fSBarry Smith     x[ir+1] = t[ii+1];
690f1af5d2fSBarry Smith     x[ir+2] = t[ii+2];
691f1af5d2fSBarry Smith     ii += 3;
692f1af5d2fSBarry Smith   }
693f1af5d2fSBarry Smith 
694f1af5d2fSBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
695f1af5d2fSBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
6961ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
6971ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
698dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*9*(a->nz) - 3.0*A->cmap->n);CHKERRQ(ierr);
699f1af5d2fSBarry Smith   PetscFunctionReturn(0);
700f1af5d2fSBarry Smith }
701f1af5d2fSBarry Smith 
7024a2ae208SSatish Balay #undef __FUNCT__
7034a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_4"
704dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_4(Mat A,Vec bb,Vec xx)
705f1af5d2fSBarry Smith {
706f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
707f1af5d2fSBarry Smith   IS             iscol=a->col,isrow=a->row;
7086849ba73SBarry Smith   PetscErrorCode ierr;
7095d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout;
7105d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
711690b6cddSBarry Smith   PetscInt       *diag = a->diag,ii,ic,ir,oidx;
712f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
71387828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,x1,x2,x3,x4;
71487828ca2SBarry Smith   PetscScalar    *x,*b,*t;
715f1af5d2fSBarry Smith 
716f1af5d2fSBarry Smith   PetscFunctionBegin;
7171ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
7181ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
719f1af5d2fSBarry Smith   t  = a->solve_work;
720f1af5d2fSBarry Smith 
721f1af5d2fSBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
722f1af5d2fSBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
723f1af5d2fSBarry Smith 
724f1af5d2fSBarry Smith   /* copy the b into temp work space according to permutation */
725f1af5d2fSBarry Smith   ii = 0;
726f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
727f1af5d2fSBarry Smith     ic      = 4*c[i];
728f1af5d2fSBarry Smith     t[ii]   = b[ic];
729f1af5d2fSBarry Smith     t[ii+1] = b[ic+1];
730f1af5d2fSBarry Smith     t[ii+2] = b[ic+2];
731f1af5d2fSBarry Smith     t[ii+3] = b[ic+3];
732f1af5d2fSBarry Smith     ii += 4;
733f1af5d2fSBarry Smith   }
734f1af5d2fSBarry Smith 
735f1af5d2fSBarry Smith   /* forward solve the U^T */
736f1af5d2fSBarry Smith   idx = 0;
737f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
738f1af5d2fSBarry Smith 
739f1af5d2fSBarry Smith     v     = aa + 16*diag[i];
740f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
741f1af5d2fSBarry Smith     x1    = t[idx];   x2 = t[1+idx]; x3    = t[2+idx]; x4 = t[3+idx];
742f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4;
743f1af5d2fSBarry Smith     s2 = v[4]*x1  +  v[5]*x2 +  v[6]*x3 +  v[7]*x4;
744f1af5d2fSBarry Smith     s3 = v[8]*x1  +  v[9]*x2 + v[10]*x3 + v[11]*x4;
745f1af5d2fSBarry Smith     s4 = v[12]*x1 + v[13]*x2 + v[14]*x3 + v[15]*x4;
746f1af5d2fSBarry Smith     v += 16;
747f1af5d2fSBarry Smith 
748f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
749f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
750f1af5d2fSBarry Smith     while (nz--) {
751f1af5d2fSBarry Smith       oidx = 4*(*vi++);
752f1af5d2fSBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4;
753f1af5d2fSBarry Smith       t[oidx+1] -= v[4]*s1  +  v[5]*s2 +  v[6]*s3 +  v[7]*s4;
754f1af5d2fSBarry Smith       t[oidx+2] -= v[8]*s1 + v[9]*s2 + v[10]*s3 + v[11]*s4;
755f1af5d2fSBarry Smith       t[oidx+3] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4;
756f1af5d2fSBarry Smith       v  += 16;
757f1af5d2fSBarry Smith     }
758f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2; t[2+idx] = s3;t[3+idx] = s4;
759f1af5d2fSBarry Smith     idx += 4;
760f1af5d2fSBarry Smith   }
761f1af5d2fSBarry Smith   /* backward solve the L^T */
762f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
763f1af5d2fSBarry Smith     v    = aa + 16*diag[i] - 16;
764f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
765f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
766f1af5d2fSBarry Smith     idt  = 4*i;
767f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt]; s3 = t[2+idt];s4 = t[3+idt];
768f1af5d2fSBarry Smith     while (nz--) {
769f1af5d2fSBarry Smith       idx   = 4*(*vi--);
770f1af5d2fSBarry Smith       t[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4;
771f1af5d2fSBarry Smith       t[idx+1] -=  v[4]*s1 +  v[5]*s2 +  v[6]*s3 +  v[7]*s4;
772f1af5d2fSBarry Smith       t[idx+2] -= v[8]*s1 + v[9]*s2 + v[10]*s3 + v[11]*s4;
773f1af5d2fSBarry Smith       t[idx+3] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4;
774f1af5d2fSBarry Smith       v -= 16;
775f1af5d2fSBarry Smith     }
776f1af5d2fSBarry Smith   }
777f1af5d2fSBarry Smith 
778f1af5d2fSBarry Smith   /* copy t into x according to permutation */
779f1af5d2fSBarry Smith   ii = 0;
780f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
781f1af5d2fSBarry Smith     ir      = 4*r[i];
782f1af5d2fSBarry Smith     x[ir]   = t[ii];
783f1af5d2fSBarry Smith     x[ir+1] = t[ii+1];
784f1af5d2fSBarry Smith     x[ir+2] = t[ii+2];
785f1af5d2fSBarry Smith     x[ir+3] = t[ii+3];
786f1af5d2fSBarry Smith     ii += 4;
787f1af5d2fSBarry Smith   }
788f1af5d2fSBarry Smith 
789f1af5d2fSBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
790f1af5d2fSBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
7911ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
7921ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
793dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
794f1af5d2fSBarry Smith   PetscFunctionReturn(0);
795f1af5d2fSBarry Smith }
796f1af5d2fSBarry Smith 
7974a2ae208SSatish Balay #undef __FUNCT__
7984a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_5"
799dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_5(Mat A,Vec bb,Vec xx)
800f1af5d2fSBarry Smith {
801f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
802f1af5d2fSBarry Smith   IS             iscol=a->col,isrow=a->row;
8036849ba73SBarry Smith   PetscErrorCode ierr;
8045d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout;
8055d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
806690b6cddSBarry Smith   PetscInt       *diag = a->diag,ii,ic,ir,oidx;
807f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
80887828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,s5,x1,x2,x3,x4,x5;
80987828ca2SBarry Smith   PetscScalar    *x,*b,*t;
810f1af5d2fSBarry Smith 
811f1af5d2fSBarry Smith   PetscFunctionBegin;
8121ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
8131ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
814f1af5d2fSBarry Smith   t  = a->solve_work;
815f1af5d2fSBarry Smith 
816f1af5d2fSBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
817f1af5d2fSBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
818f1af5d2fSBarry Smith 
819f1af5d2fSBarry Smith   /* copy the b into temp work space according to permutation */
820f1af5d2fSBarry Smith   ii = 0;
821f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
822f1af5d2fSBarry Smith     ic      = 5*c[i];
823f1af5d2fSBarry Smith     t[ii]   = b[ic];
824f1af5d2fSBarry Smith     t[ii+1] = b[ic+1];
825f1af5d2fSBarry Smith     t[ii+2] = b[ic+2];
826f1af5d2fSBarry Smith     t[ii+3] = b[ic+3];
827f1af5d2fSBarry Smith     t[ii+4] = b[ic+4];
828f1af5d2fSBarry Smith     ii += 5;
829f1af5d2fSBarry Smith   }
830f1af5d2fSBarry Smith 
831f1af5d2fSBarry Smith   /* forward solve the U^T */
832f1af5d2fSBarry Smith   idx = 0;
833f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
834f1af5d2fSBarry Smith 
835f1af5d2fSBarry Smith     v     = aa + 25*diag[i];
836f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
837f1af5d2fSBarry Smith     x1    = t[idx];   x2 = t[1+idx]; x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
838f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5;
839f1af5d2fSBarry Smith     s2 = v[5]*x1  +  v[6]*x2 +  v[7]*x3 +  v[8]*x4 +  v[9]*x5;
840f1af5d2fSBarry Smith     s3 = v[10]*x1 + v[11]*x2 + v[12]*x3 + v[13]*x4 + v[14]*x5;
841f1af5d2fSBarry Smith     s4 = v[15]*x1 + v[16]*x2 + v[17]*x3 + v[18]*x4 + v[19]*x5;
842f1af5d2fSBarry Smith     s5 = v[20]*x1 + v[21]*x2 + v[22]*x3 + v[23]*x4 + v[24]*x5;
843f1af5d2fSBarry Smith     v += 25;
844f1af5d2fSBarry Smith 
845f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
846f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
847f1af5d2fSBarry Smith     while (nz--) {
848f1af5d2fSBarry Smith       oidx = 5*(*vi++);
849f1af5d2fSBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
850f1af5d2fSBarry Smith       t[oidx+1] -= v[5]*s1  +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
851f1af5d2fSBarry Smith       t[oidx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
852f1af5d2fSBarry Smith       t[oidx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
853f1af5d2fSBarry Smith       t[oidx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
854f1af5d2fSBarry Smith       v  += 25;
855f1af5d2fSBarry Smith     }
856f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2; t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
857f1af5d2fSBarry Smith     idx += 5;
858f1af5d2fSBarry Smith   }
859f1af5d2fSBarry Smith   /* backward solve the L^T */
860f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
861f1af5d2fSBarry Smith     v    = aa + 25*diag[i] - 25;
862f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
863f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
864f1af5d2fSBarry Smith     idt  = 5*i;
865f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt]; s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
866f1af5d2fSBarry Smith     while (nz--) {
867f1af5d2fSBarry Smith       idx   = 5*(*vi--);
868f1af5d2fSBarry Smith       t[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5;
869f1af5d2fSBarry Smith       t[idx+1] -=  v[5]*s1 +  v[6]*s2 +  v[7]*s3 +  v[8]*s4 +  v[9]*s5;
870f1af5d2fSBarry Smith       t[idx+2] -= v[10]*s1 + v[11]*s2 + v[12]*s3 + v[13]*s4 + v[14]*s5;
871f1af5d2fSBarry Smith       t[idx+3] -= v[15]*s1 + v[16]*s2 + v[17]*s3 + v[18]*s4 + v[19]*s5;
872f1af5d2fSBarry Smith       t[idx+4] -= v[20]*s1 + v[21]*s2 + v[22]*s3 + v[23]*s4 + v[24]*s5;
873f1af5d2fSBarry Smith       v -= 25;
874f1af5d2fSBarry Smith     }
875f1af5d2fSBarry Smith   }
876f1af5d2fSBarry Smith 
877f1af5d2fSBarry Smith   /* copy t into x according to permutation */
878f1af5d2fSBarry Smith   ii = 0;
879f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
880f1af5d2fSBarry Smith     ir      = 5*r[i];
881f1af5d2fSBarry Smith     x[ir]   = t[ii];
882f1af5d2fSBarry Smith     x[ir+1] = t[ii+1];
883f1af5d2fSBarry Smith     x[ir+2] = t[ii+2];
884f1af5d2fSBarry Smith     x[ir+3] = t[ii+3];
885f1af5d2fSBarry Smith     x[ir+4] = t[ii+4];
886f1af5d2fSBarry Smith     ii += 5;
887f1af5d2fSBarry Smith   }
888f1af5d2fSBarry Smith 
889f1af5d2fSBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
890f1af5d2fSBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
8911ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
8921ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
893dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n);CHKERRQ(ierr);
894f1af5d2fSBarry Smith   PetscFunctionReturn(0);
895f1af5d2fSBarry Smith }
896f1af5d2fSBarry Smith 
8974a2ae208SSatish Balay #undef __FUNCT__
8984a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_6"
899dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_6(Mat A,Vec bb,Vec xx)
900f1af5d2fSBarry Smith {
901f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
902f1af5d2fSBarry Smith   IS             iscol=a->col,isrow=a->row;
9036849ba73SBarry Smith   PetscErrorCode ierr;
9045d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout;
9055d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
906690b6cddSBarry Smith   PetscInt       *diag = a->diag,ii,ic,ir,oidx;
907f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
90887828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,s5,s6,x1,x2,x3,x4,x5,x6;
90987828ca2SBarry Smith   PetscScalar    *x,*b,*t;
910f1af5d2fSBarry Smith 
911f1af5d2fSBarry Smith   PetscFunctionBegin;
9121ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
9131ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
914f1af5d2fSBarry Smith   t  = a->solve_work;
915f1af5d2fSBarry Smith 
916f1af5d2fSBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
917f1af5d2fSBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
918f1af5d2fSBarry Smith 
919f1af5d2fSBarry Smith   /* copy the b into temp work space according to permutation */
920f1af5d2fSBarry Smith   ii = 0;
921f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
922f1af5d2fSBarry Smith     ic      = 6*c[i];
923f1af5d2fSBarry Smith     t[ii]   = b[ic];
924f1af5d2fSBarry Smith     t[ii+1] = b[ic+1];
925f1af5d2fSBarry Smith     t[ii+2] = b[ic+2];
926f1af5d2fSBarry Smith     t[ii+3] = b[ic+3];
927f1af5d2fSBarry Smith     t[ii+4] = b[ic+4];
928f1af5d2fSBarry Smith     t[ii+5] = b[ic+5];
929f1af5d2fSBarry Smith     ii += 6;
930f1af5d2fSBarry Smith   }
931f1af5d2fSBarry Smith 
932f1af5d2fSBarry Smith   /* forward solve the U^T */
933f1af5d2fSBarry Smith   idx = 0;
934f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
935f1af5d2fSBarry Smith 
936f1af5d2fSBarry Smith     v     = aa + 36*diag[i];
937f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
938f1af5d2fSBarry Smith     x1    = t[idx];   x2 = t[1+idx]; x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
939f1af5d2fSBarry Smith     x6    = t[5+idx];
940f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5 +  v[5]*x6;
941f1af5d2fSBarry Smith     s2 = v[6]*x1  +  v[7]*x2 +  v[8]*x3 +  v[9]*x4 + v[10]*x5 + v[11]*x6;
942f1af5d2fSBarry Smith     s3 = v[12]*x1 + v[13]*x2 + v[14]*x3 + v[15]*x4 + v[16]*x5 + v[17]*x6;
943f1af5d2fSBarry Smith     s4 = v[18]*x1 + v[19]*x2 + v[20]*x3 + v[21]*x4 + v[22]*x5 + v[23]*x6;
944f1af5d2fSBarry Smith     s5 = v[24]*x1 + v[25]*x2 + v[26]*x3 + v[27]*x4 + v[28]*x5 + v[29]*x6;
945f1af5d2fSBarry Smith     s6 = v[30]*x1 + v[31]*x2 + v[32]*x3 + v[33]*x4 + v[34]*x5 + v[35]*x6;
946f1af5d2fSBarry Smith     v += 36;
947f1af5d2fSBarry Smith 
948f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
949f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
950f1af5d2fSBarry Smith     while (nz--) {
951f1af5d2fSBarry Smith       oidx = 6*(*vi++);
952f1af5d2fSBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6;
953f1af5d2fSBarry Smith       t[oidx+1] -= v[6]*s1  +  v[7]*s2 +  v[8]*s3 +  v[9]*s4 + v[10]*s5 + v[11]*s6;
954f1af5d2fSBarry Smith       t[oidx+2] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4 + v[16]*s5 + v[17]*s6;
955f1af5d2fSBarry Smith       t[oidx+3] -= v[18]*s1 + v[19]*s2 + v[20]*s3 + v[21]*s4 + v[22]*s5 + v[23]*s6;
956f1af5d2fSBarry Smith       t[oidx+4] -= v[24]*s1 + v[25]*s2 + v[26]*s3 + v[27]*s4 + v[28]*s5 + v[29]*s6;
957f1af5d2fSBarry Smith       t[oidx+5] -= v[30]*s1 + v[31]*s2 + v[32]*s3 + v[33]*s4 + v[34]*s5 + v[35]*s6;
958f1af5d2fSBarry Smith       v  += 36;
959f1af5d2fSBarry Smith     }
960f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2; t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
961f1af5d2fSBarry Smith     t[5+idx] = s6;
962f1af5d2fSBarry Smith     idx += 6;
963f1af5d2fSBarry Smith   }
964f1af5d2fSBarry Smith   /* backward solve the L^T */
965f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
966f1af5d2fSBarry Smith     v    = aa + 36*diag[i] - 36;
967f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
968f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
969f1af5d2fSBarry Smith     idt  = 6*i;
970f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt]; s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
971f1af5d2fSBarry Smith     s6 = t[5+idt];
972f1af5d2fSBarry Smith     while (nz--) {
973f1af5d2fSBarry Smith       idx   = 6*(*vi--);
974f1af5d2fSBarry Smith       t[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6;
975f1af5d2fSBarry Smith       t[idx+1] -=  v[6]*s1 +  v[7]*s2 +  v[8]*s3 +  v[9]*s4 + v[10]*s5 + v[11]*s6;
976f1af5d2fSBarry Smith       t[idx+2] -= v[12]*s1 + v[13]*s2 + v[14]*s3 + v[15]*s4 + v[16]*s5 + v[17]*s6;
977f1af5d2fSBarry Smith       t[idx+3] -= v[18]*s1 + v[19]*s2 + v[20]*s3 + v[21]*s4 + v[22]*s5 + v[23]*s6;
978f1af5d2fSBarry Smith       t[idx+4] -= v[24]*s1 + v[25]*s2 + v[26]*s3 + v[27]*s4 + v[28]*s5 + v[29]*s6;
979f1af5d2fSBarry Smith       t[idx+5] -= v[30]*s1 + v[31]*s2 + v[32]*s3 + v[33]*s4 + v[34]*s5 + v[35]*s6;
980f1af5d2fSBarry Smith       v -= 36;
981f1af5d2fSBarry Smith     }
982f1af5d2fSBarry Smith   }
983f1af5d2fSBarry Smith 
984f1af5d2fSBarry Smith   /* copy t into x according to permutation */
985f1af5d2fSBarry Smith   ii = 0;
986f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
987f1af5d2fSBarry Smith     ir      = 6*r[i];
988f1af5d2fSBarry Smith     x[ir]   = t[ii];
989f1af5d2fSBarry Smith     x[ir+1] = t[ii+1];
990f1af5d2fSBarry Smith     x[ir+2] = t[ii+2];
991f1af5d2fSBarry Smith     x[ir+3] = t[ii+3];
992f1af5d2fSBarry Smith     x[ir+4] = t[ii+4];
993f1af5d2fSBarry Smith     x[ir+5] = t[ii+5];
994f1af5d2fSBarry Smith     ii += 6;
995f1af5d2fSBarry Smith   }
996f1af5d2fSBarry Smith 
997f1af5d2fSBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
998f1af5d2fSBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
9991ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
10001ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1001dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*36*(a->nz) - 6.0*A->cmap->n);CHKERRQ(ierr);
1002f1af5d2fSBarry Smith   PetscFunctionReturn(0);
1003f1af5d2fSBarry Smith }
1004f1af5d2fSBarry Smith 
10054a2ae208SSatish Balay #undef __FUNCT__
10064a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqBAIJ_7"
1007dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqBAIJ_7(Mat A,Vec bb,Vec xx)
1008f1af5d2fSBarry Smith {
1009f1af5d2fSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
1010f1af5d2fSBarry Smith   IS             iscol=a->col,isrow=a->row;
10116849ba73SBarry Smith   PetscErrorCode ierr;
10125d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout;
10135d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
1014690b6cddSBarry Smith   PetscInt       *diag = a->diag,ii,ic,ir,oidx;
1015f1af5d2fSBarry Smith   MatScalar      *aa=a->a,*v;
101687828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
101787828ca2SBarry Smith   PetscScalar    *x,*b,*t;
1018f1af5d2fSBarry Smith 
1019f1af5d2fSBarry Smith   PetscFunctionBegin;
10201ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
10211ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1022f1af5d2fSBarry Smith   t  = a->solve_work;
1023f1af5d2fSBarry Smith 
1024f1af5d2fSBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1025f1af5d2fSBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1026f1af5d2fSBarry Smith 
1027f1af5d2fSBarry Smith   /* copy the b into temp work space according to permutation */
1028f1af5d2fSBarry Smith   ii = 0;
1029f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
1030f1af5d2fSBarry Smith     ic      = 7*c[i];
1031f1af5d2fSBarry Smith     t[ii]   = b[ic];
1032f1af5d2fSBarry Smith     t[ii+1] = b[ic+1];
1033f1af5d2fSBarry Smith     t[ii+2] = b[ic+2];
1034f1af5d2fSBarry Smith     t[ii+3] = b[ic+3];
1035f1af5d2fSBarry Smith     t[ii+4] = b[ic+4];
1036f1af5d2fSBarry Smith     t[ii+5] = b[ic+5];
1037f1af5d2fSBarry Smith     t[ii+6] = b[ic+6];
1038f1af5d2fSBarry Smith     ii += 7;
1039f1af5d2fSBarry Smith   }
1040f1af5d2fSBarry Smith 
1041f1af5d2fSBarry Smith   /* forward solve the U^T */
1042f1af5d2fSBarry Smith   idx = 0;
1043f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
1044f1af5d2fSBarry Smith 
1045f1af5d2fSBarry Smith     v     = aa + 49*diag[i];
1046f1af5d2fSBarry Smith     /* multiply by the inverse of the block diagonal */
1047f1af5d2fSBarry Smith     x1    = t[idx];   x2 = t[1+idx]; x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
1048f1af5d2fSBarry Smith     x6    = t[5+idx]; x7 = t[6+idx];
1049f1af5d2fSBarry Smith     s1 = v[0]*x1  +  v[1]*x2 +  v[2]*x3 +  v[3]*x4 +  v[4]*x5 +  v[5]*x6 +  v[6]*x7;
1050f1af5d2fSBarry Smith     s2 = v[7]*x1  +  v[8]*x2 +  v[9]*x3 + v[10]*x4 + v[11]*x5 + v[12]*x6 + v[13]*x7;
1051f1af5d2fSBarry Smith     s3 = v[14]*x1 + v[15]*x2 + v[16]*x3 + v[17]*x4 + v[18]*x5 + v[19]*x6 + v[20]*x7;
1052f1af5d2fSBarry Smith     s4 = v[21]*x1 + v[22]*x2 + v[23]*x3 + v[24]*x4 + v[25]*x5 + v[26]*x6 + v[27]*x7;
1053f1af5d2fSBarry Smith     s5 = v[28]*x1 + v[29]*x2 + v[30]*x3 + v[31]*x4 + v[32]*x5 + v[33]*x6 + v[34]*x7;
1054f1af5d2fSBarry Smith     s6 = v[35]*x1 + v[36]*x2 + v[37]*x3 + v[38]*x4 + v[39]*x5 + v[40]*x6 + v[41]*x7;
1055f1af5d2fSBarry Smith     s7 = v[42]*x1 + v[43]*x2 + v[44]*x3 + v[45]*x4 + v[46]*x5 + v[47]*x6 + v[48]*x7;
1056f1af5d2fSBarry Smith     v += 49;
1057f1af5d2fSBarry Smith 
1058f1af5d2fSBarry Smith     vi    = aj + diag[i] + 1;
1059f1af5d2fSBarry Smith     nz    = ai[i+1] - diag[i] - 1;
1060f1af5d2fSBarry Smith     while (nz--) {
1061f1af5d2fSBarry Smith       oidx = 7*(*vi++);
1062f1af5d2fSBarry Smith       t[oidx]   -= v[0]*s1  +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6 +  v[6]*s7;
1063f1af5d2fSBarry Smith       t[oidx+1] -= v[7]*s1  +  v[8]*s2 +  v[9]*s3 + v[10]*s4 + v[11]*s5 + v[12]*s6 + v[13]*s7;
1064f1af5d2fSBarry Smith       t[oidx+2] -= v[14]*s1 + v[15]*s2 + v[16]*s3 + v[17]*s4 + v[18]*s5 + v[19]*s6 + v[20]*s7;
1065f1af5d2fSBarry Smith       t[oidx+3] -= v[21]*s1 + v[22]*s2 + v[23]*s3 + v[24]*s4 + v[25]*s5 + v[26]*s6 + v[27]*s7;
1066f1af5d2fSBarry Smith       t[oidx+4] -= v[28]*s1 + v[29]*s2 + v[30]*s3 + v[31]*s4 + v[32]*s5 + v[33]*s6 + v[34]*s7;
1067f1af5d2fSBarry Smith       t[oidx+5] -= v[35]*s1 + v[36]*s2 + v[37]*s3 + v[38]*s4 + v[39]*s5 + v[40]*s6 + v[41]*s7;
1068f1af5d2fSBarry Smith       t[oidx+6] -= v[42]*s1 + v[43]*s2 + v[44]*s3 + v[45]*s4 + v[46]*s5 + v[47]*s6 + v[48]*s7;
1069f1af5d2fSBarry Smith       v  += 49;
1070f1af5d2fSBarry Smith     }
1071f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2; t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
1072f1af5d2fSBarry Smith     t[5+idx] = s6;t[6+idx] = s7;
1073f1af5d2fSBarry Smith     idx += 7;
1074f1af5d2fSBarry Smith   }
1075f1af5d2fSBarry Smith   /* backward solve the L^T */
1076f1af5d2fSBarry Smith   for (i=n-1; i>=0; i--){
1077f1af5d2fSBarry Smith     v    = aa + 49*diag[i] - 49;
1078f1af5d2fSBarry Smith     vi   = aj + diag[i] - 1;
1079f1af5d2fSBarry Smith     nz   = diag[i] - ai[i];
1080f1af5d2fSBarry Smith     idt  = 7*i;
1081f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt]; s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
1082f1af5d2fSBarry Smith     s6 = t[5+idt];s7 = t[6+idt];
1083f1af5d2fSBarry Smith     while (nz--) {
1084f1af5d2fSBarry Smith       idx   = 7*(*vi--);
1085f1af5d2fSBarry Smith       t[idx]   -=  v[0]*s1 +  v[1]*s2 +  v[2]*s3 +  v[3]*s4 +  v[4]*s5 +  v[5]*s6 +  v[6]*s7;
1086f1af5d2fSBarry Smith       t[idx+1] -=  v[7]*s1 +  v[8]*s2 +  v[9]*s3 + v[10]*s4 + v[11]*s5 + v[12]*s6 + v[13]*s7;
1087f1af5d2fSBarry Smith       t[idx+2] -= v[14]*s1 + v[15]*s2 + v[16]*s3 + v[17]*s4 + v[18]*s5 + v[19]*s6 + v[20]*s7;
1088f1af5d2fSBarry Smith       t[idx+3] -= v[21]*s1 + v[22]*s2 + v[23]*s3 + v[24]*s4 + v[25]*s5 + v[26]*s6 + v[27]*s7;
1089f1af5d2fSBarry Smith       t[idx+4] -= v[28]*s1 + v[29]*s2 + v[30]*s3 + v[31]*s4 + v[32]*s5 + v[33]*s6 + v[34]*s7;
1090f1af5d2fSBarry Smith       t[idx+5] -= v[35]*s1 + v[36]*s2 + v[37]*s3 + v[38]*s4 + v[39]*s5 + v[40]*s6 + v[41]*s7;
1091f1af5d2fSBarry Smith       t[idx+6] -= v[42]*s1 + v[43]*s2 + v[44]*s3 + v[45]*s4 + v[46]*s5 + v[47]*s6 + v[48]*s7;
1092f1af5d2fSBarry Smith       v -= 49;
1093f1af5d2fSBarry Smith     }
1094f1af5d2fSBarry Smith   }
1095f1af5d2fSBarry Smith 
1096f1af5d2fSBarry Smith   /* copy t into x according to permutation */
1097f1af5d2fSBarry Smith   ii = 0;
1098f1af5d2fSBarry Smith   for (i=0; i<n; i++) {
1099f1af5d2fSBarry Smith     ir      = 7*r[i];
1100f1af5d2fSBarry Smith     x[ir]   = t[ii];
1101f1af5d2fSBarry Smith     x[ir+1] = t[ii+1];
1102f1af5d2fSBarry Smith     x[ir+2] = t[ii+2];
1103f1af5d2fSBarry Smith     x[ir+3] = t[ii+3];
1104f1af5d2fSBarry Smith     x[ir+4] = t[ii+4];
1105f1af5d2fSBarry Smith     x[ir+5] = t[ii+5];
1106f1af5d2fSBarry Smith     x[ir+6] = t[ii+6];
1107f1af5d2fSBarry Smith     ii += 7;
1108f1af5d2fSBarry Smith   }
1109f1af5d2fSBarry Smith 
1110f1af5d2fSBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1111f1af5d2fSBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
11121ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
11131ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1114dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*49*(a->nz) - 7.0*A->cmap->n);CHKERRQ(ierr);
1115f1af5d2fSBarry Smith   PetscFunctionReturn(0);
1116f1af5d2fSBarry Smith }
1117f1af5d2fSBarry Smith 
11184e2b4712SSatish Balay /* ----------------------------------------------------------- */
11194a2ae208SSatish Balay #undef __FUNCT__
11204a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_N"
1121dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_N(Mat A,Vec bb,Vec xx)
11224e2b4712SSatish Balay {
11234e2b4712SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
11244e2b4712SSatish Balay   IS             iscol=a->col,isrow=a->row;
11256849ba73SBarry Smith   PetscErrorCode ierr;
11265d0c19d7SBarry Smith   const PetscInt *r,*c,*rout,*cout,*ai=a->i,*aj=a->j,*vi;
11275d0c19d7SBarry Smith   PetscInt       i,n=a->mbs;
11285d0c19d7SBarry Smith   PetscInt       nz,bs=A->rmap->bs,bs2=a->bs2;
11293f1db9ecSBarry Smith   MatScalar      *aa=a->a,*v;
113087828ca2SBarry Smith   PetscScalar    *x,*b,*s,*t,*ls;
11314e2b4712SSatish Balay 
11324e2b4712SSatish Balay   PetscFunctionBegin;
11331ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
11341ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1135f1af5d2fSBarry Smith   t  = a->solve_work;
11364e2b4712SSatish Balay 
11374e2b4712SSatish Balay   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
11384e2b4712SSatish Balay   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
11394e2b4712SSatish Balay 
11404e2b4712SSatish Balay   /* forward solve the lower triangular */
114187828ca2SBarry Smith   ierr = PetscMemcpy(t,b+bs*(*r++),bs*sizeof(PetscScalar));CHKERRQ(ierr);
11424e2b4712SSatish Balay   for (i=1; i<n; i++) {
11434e2b4712SSatish Balay     v   = aa + bs2*ai[i];
11444e2b4712SSatish Balay     vi  = aj + ai[i];
11454e2b4712SSatish Balay     nz  = a->diag[i] - ai[i];
1146f1af5d2fSBarry Smith     s = t + bs*i;
114787828ca2SBarry Smith     ierr = PetscMemcpy(s,b+bs*(*r++),bs*sizeof(PetscScalar));CHKERRQ(ierr);
11484e2b4712SSatish Balay     while (nz--) {
1149f1af5d2fSBarry Smith       Kernel_v_gets_v_minus_A_times_w(bs,s,v,t+bs*(*vi++));
11504e2b4712SSatish Balay       v += bs2;
11514e2b4712SSatish Balay     }
11524e2b4712SSatish Balay   }
11534e2b4712SSatish Balay   /* backward solve the upper triangular */
1154d0f46423SBarry Smith   ls = a->solve_work + A->cmap->n;
11554e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
11564e2b4712SSatish Balay     v   = aa + bs2*(a->diag[i] + 1);
11574e2b4712SSatish Balay     vi  = aj + a->diag[i] + 1;
11584e2b4712SSatish Balay     nz  = ai[i+1] - a->diag[i] - 1;
115987828ca2SBarry Smith     ierr = PetscMemcpy(ls,t+i*bs,bs*sizeof(PetscScalar));CHKERRQ(ierr);
11604e2b4712SSatish Balay     while (nz--) {
1161f1af5d2fSBarry Smith       Kernel_v_gets_v_minus_A_times_w(bs,ls,v,t+bs*(*vi++));
11624e2b4712SSatish Balay       v += bs2;
11634e2b4712SSatish Balay     }
1164f1af5d2fSBarry Smith     Kernel_w_gets_A_times_v(bs,ls,aa+bs2*a->diag[i],t+i*bs);
116587828ca2SBarry Smith     ierr = PetscMemcpy(x + bs*(*c--),t+i*bs,bs*sizeof(PetscScalar));CHKERRQ(ierr);
11664e2b4712SSatish Balay   }
11674e2b4712SSatish Balay 
11684e2b4712SSatish Balay   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
11694e2b4712SSatish Balay   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
11701ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
11711ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1172dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*(a->bs2)*(a->nz) - A->rmap->bs*A->cmap->n);CHKERRQ(ierr);
11734e2b4712SSatish Balay   PetscFunctionReturn(0);
11744e2b4712SSatish Balay }
11754e2b4712SSatish Balay 
11764a2ae208SSatish Balay #undef __FUNCT__
11774a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_7"
1178dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_7(Mat A,Vec bb,Vec xx)
11794e2b4712SSatish Balay {
11804e2b4712SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
11814e2b4712SSatish Balay   IS             iscol=a->col,isrow=a->row;
11826849ba73SBarry Smith   PetscErrorCode ierr;
11835d0c19d7SBarry Smith   const PetscInt *r,*c,*ai=a->i,*aj=a->j,*rout,*cout,*diag = a->diag,*vi;
11845d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,nz,idx,idt,idc;
11853f1db9ecSBarry Smith   MatScalar      *aa=a->a,*v;
118687828ca2SBarry Smith   PetscScalar    s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
118787828ca2SBarry Smith   PetscScalar    *x,*b,*t;
11884e2b4712SSatish Balay 
11894e2b4712SSatish Balay   PetscFunctionBegin;
11901ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
11911ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1192f1af5d2fSBarry Smith   t  = a->solve_work;
11934e2b4712SSatish Balay 
11944e2b4712SSatish Balay   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
11954e2b4712SSatish Balay   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
11964e2b4712SSatish Balay 
11974e2b4712SSatish Balay   /* forward solve the lower triangular */
11984e2b4712SSatish Balay   idx    = 7*(*r++);
1199f1af5d2fSBarry Smith   t[0] = b[idx];   t[1] = b[1+idx];
1200f1af5d2fSBarry Smith   t[2] = b[2+idx]; t[3] = b[3+idx]; t[4] = b[4+idx];
1201f1af5d2fSBarry Smith   t[5] = b[5+idx]; t[6] = b[6+idx];
12024e2b4712SSatish Balay 
12034e2b4712SSatish Balay   for (i=1; i<n; i++) {
12044e2b4712SSatish Balay     v     = aa + 49*ai[i];
12054e2b4712SSatish Balay     vi    = aj + ai[i];
12064e2b4712SSatish Balay     nz    = diag[i] - ai[i];
12074e2b4712SSatish Balay     idx   = 7*(*r++);
1208f1af5d2fSBarry Smith     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
1209f1af5d2fSBarry Smith     s5  = b[4+idx];s6 = b[5+idx];s7 = b[6+idx];
12104e2b4712SSatish Balay     while (nz--) {
12114e2b4712SSatish Balay       idx   = 7*(*vi++);
1212f1af5d2fSBarry Smith       x1    = t[idx];  x2 = t[1+idx];x3 = t[2+idx];
1213f1af5d2fSBarry Smith       x4    = t[3+idx];x5 = t[4+idx];
1214f1af5d2fSBarry Smith       x6    = t[5+idx];x7 = t[6+idx];
1215f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
1216f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
1217f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
1218f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
1219f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
1220f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
1221f1af5d2fSBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
12224e2b4712SSatish Balay       v += 49;
12234e2b4712SSatish Balay     }
12244e2b4712SSatish Balay     idx = 7*i;
1225f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2;
1226f1af5d2fSBarry Smith     t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
1227f1af5d2fSBarry Smith     t[5+idx] = s6;t[6+idx] = s7;
12284e2b4712SSatish Balay   }
12294e2b4712SSatish Balay   /* backward solve the upper triangular */
12304e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
12314e2b4712SSatish Balay     v    = aa + 49*diag[i] + 49;
12324e2b4712SSatish Balay     vi   = aj + diag[i] + 1;
12334e2b4712SSatish Balay     nz   = ai[i+1] - diag[i] - 1;
12344e2b4712SSatish Balay     idt  = 7*i;
1235f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt];
1236f1af5d2fSBarry Smith     s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
1237f1af5d2fSBarry Smith     s6 = t[5+idt];s7 = t[6+idt];
12384e2b4712SSatish Balay     while (nz--) {
12394e2b4712SSatish Balay       idx   = 7*(*vi++);
1240f1af5d2fSBarry Smith       x1    = t[idx];   x2 = t[1+idx];
1241f1af5d2fSBarry Smith       x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
1242f1af5d2fSBarry Smith       x6    = t[5+idx]; x7 = t[6+idx];
1243f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
1244f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
1245f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
1246f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
1247f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
1248f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
1249f1af5d2fSBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
12504e2b4712SSatish Balay       v += 49;
12514e2b4712SSatish Balay     }
12524e2b4712SSatish Balay     idc = 7*(*c--);
12534e2b4712SSatish Balay     v   = aa + 49*diag[i];
1254f1af5d2fSBarry Smith     x[idc]   = t[idt]   = v[0]*s1+v[7]*s2+v[14]*s3+
1255f1af5d2fSBarry Smith                                  v[21]*s4+v[28]*s5+v[35]*s6+v[42]*s7;
1256f1af5d2fSBarry Smith     x[1+idc] = t[1+idt] = v[1]*s1+v[8]*s2+v[15]*s3+
1257f1af5d2fSBarry Smith                                  v[22]*s4+v[29]*s5+v[36]*s6+v[43]*s7;
1258f1af5d2fSBarry Smith     x[2+idc] = t[2+idt] = v[2]*s1+v[9]*s2+v[16]*s3+
1259f1af5d2fSBarry Smith                                  v[23]*s4+v[30]*s5+v[37]*s6+v[44]*s7;
1260f1af5d2fSBarry Smith     x[3+idc] = t[3+idt] = v[3]*s1+v[10]*s2+v[17]*s3+
1261f1af5d2fSBarry Smith                                  v[24]*s4+v[31]*s5+v[38]*s6+v[45]*s7;
1262f1af5d2fSBarry Smith     x[4+idc] = t[4+idt] = v[4]*s1+v[11]*s2+v[18]*s3+
1263f1af5d2fSBarry Smith                                  v[25]*s4+v[32]*s5+v[39]*s6+v[46]*s7;
1264f1af5d2fSBarry Smith     x[5+idc] = t[5+idt] = v[5]*s1+v[12]*s2+v[19]*s3+
1265f1af5d2fSBarry Smith                                  v[26]*s4+v[33]*s5+v[40]*s6+v[47]*s7;
1266f1af5d2fSBarry Smith     x[6+idc] = t[6+idt] = v[6]*s1+v[13]*s2+v[20]*s3+
1267f1af5d2fSBarry Smith                                  v[27]*s4+v[34]*s5+v[41]*s6+v[48]*s7;
12684e2b4712SSatish Balay   }
12694e2b4712SSatish Balay 
12704e2b4712SSatish Balay   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
12714e2b4712SSatish Balay   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
12721ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
12731ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1274dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*49*(a->nz) - 7.0*A->cmap->n);CHKERRQ(ierr);
12754e2b4712SSatish Balay   PetscFunctionReturn(0);
12764e2b4712SSatish Balay }
12774e2b4712SSatish Balay 
12784a2ae208SSatish Balay #undef __FUNCT__
12798f690400SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_7_newdatastruct"
12808f690400SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_7_newdatastruct(Mat A,Vec bb,Vec xx)
12818f690400SShri Abhyankar {
12828f690400SShri Abhyankar   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
12838f690400SShri Abhyankar   IS             iscol=a->col,isrow=a->row;
12848f690400SShri Abhyankar   PetscErrorCode ierr;
12858f690400SShri Abhyankar   const PetscInt *r,*c,*ai=a->i,*aj=a->j,*rout,*cout,*vi;
128629b92fc1SShri Abhyankar   PetscInt       i,n=a->mbs,nz,idx,idt,idc,k,m;
12878f690400SShri Abhyankar   MatScalar      *aa=a->a,*v;
12888f690400SShri Abhyankar   PetscScalar    s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
12898f690400SShri Abhyankar   PetscScalar    *x,*b,*t;
12908f690400SShri Abhyankar 
12918f690400SShri Abhyankar   PetscFunctionBegin;
12928f690400SShri Abhyankar   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
12938f690400SShri Abhyankar   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
12948f690400SShri Abhyankar   t  = a->solve_work;
12958f690400SShri Abhyankar 
12968f690400SShri Abhyankar   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
129729b92fc1SShri Abhyankar   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
12988f690400SShri Abhyankar 
12998f690400SShri Abhyankar   /* forward solve the lower triangular */
130029b92fc1SShri Abhyankar   idx    = 7*r[0];
13018f690400SShri Abhyankar   t[0] = b[idx];   t[1] = b[1+idx];
13028f690400SShri Abhyankar   t[2] = b[2+idx]; t[3] = b[3+idx]; t[4] = b[4+idx];
13038f690400SShri Abhyankar   t[5] = b[5+idx]; t[6] = b[6+idx];
13048f690400SShri Abhyankar 
13058f690400SShri Abhyankar   for (i=1; i<n; i++) {
13068f690400SShri Abhyankar     v     = aa + 49*ai[i];
13078f690400SShri Abhyankar     vi    = aj + ai[i];
13088f690400SShri Abhyankar     nz    = ai[i+1] - ai[i];
130929b92fc1SShri Abhyankar     idx   = 7*r[i];
13108f690400SShri Abhyankar     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
13118f690400SShri Abhyankar     s5  = b[4+idx];s6 = b[5+idx];s7 = b[6+idx];
131229b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
131329b92fc1SShri Abhyankar       idx   = 7*vi[m];
13148f690400SShri Abhyankar       x1    = t[idx];  x2 = t[1+idx];x3 = t[2+idx];
13158f690400SShri Abhyankar       x4    = t[3+idx];x5 = t[4+idx];
13168f690400SShri Abhyankar       x6    = t[5+idx];x7 = t[6+idx];
13178f690400SShri Abhyankar       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
13188f690400SShri Abhyankar       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
13198f690400SShri Abhyankar       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
13208f690400SShri Abhyankar       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
13218f690400SShri Abhyankar       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
13228f690400SShri Abhyankar       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
13238f690400SShri Abhyankar       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
13248f690400SShri Abhyankar       v += 49;
13258f690400SShri Abhyankar     }
13268f690400SShri Abhyankar     idx = 7*i;
13278f690400SShri Abhyankar     t[idx]   = s1;t[1+idx] = s2;
13288f690400SShri Abhyankar     t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
13298f690400SShri Abhyankar     t[5+idx] = s6;t[6+idx] = s7;
13308f690400SShri Abhyankar   }
13318f690400SShri Abhyankar   /* backward solve the upper triangular */
13328f690400SShri Abhyankar   for (i=n-1; i>=0; i--){
13338f690400SShri Abhyankar     k    = 2*n-i;
13348f690400SShri Abhyankar     v    = aa + 49*ai[k];
13358f690400SShri Abhyankar     vi   = aj + ai[k];
13368f690400SShri Abhyankar     nz   = ai[k+1] - ai[k] - 1;
13378f690400SShri Abhyankar     idt  = 7*i;
13388f690400SShri Abhyankar     s1 = t[idt];  s2 = t[1+idt];
13398f690400SShri Abhyankar     s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
13408f690400SShri Abhyankar     s6 = t[5+idt];s7 = t[6+idt];
134129b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
134229b92fc1SShri Abhyankar       idx   = 7*vi[m];
13438f690400SShri Abhyankar       x1    = t[idx];   x2 = t[1+idx];
13448f690400SShri Abhyankar       x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
13458f690400SShri Abhyankar       x6    = t[5+idx]; x7 = t[6+idx];
13468f690400SShri Abhyankar       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
13478f690400SShri Abhyankar       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
13488f690400SShri Abhyankar       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
13498f690400SShri Abhyankar       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
13508f690400SShri Abhyankar       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
13518f690400SShri Abhyankar       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
13528f690400SShri Abhyankar       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
13538f690400SShri Abhyankar       v += 49;
13548f690400SShri Abhyankar     }
135529b92fc1SShri Abhyankar     idc = 7*c[i];
13568f690400SShri Abhyankar     x[idc]   = t[idt]   = v[0]*s1+v[7]*s2+v[14]*s3+
13578f690400SShri Abhyankar                                  v[21]*s4+v[28]*s5+v[35]*s6+v[42]*s7;
13588f690400SShri Abhyankar     x[1+idc] = t[1+idt] = v[1]*s1+v[8]*s2+v[15]*s3+
13598f690400SShri Abhyankar                                  v[22]*s4+v[29]*s5+v[36]*s6+v[43]*s7;
13608f690400SShri Abhyankar     x[2+idc] = t[2+idt] = v[2]*s1+v[9]*s2+v[16]*s3+
13618f690400SShri Abhyankar                                  v[23]*s4+v[30]*s5+v[37]*s6+v[44]*s7;
13628f690400SShri Abhyankar     x[3+idc] = t[3+idt] = v[3]*s1+v[10]*s2+v[17]*s3+
13638f690400SShri Abhyankar                                  v[24]*s4+v[31]*s5+v[38]*s6+v[45]*s7;
13648f690400SShri Abhyankar     x[4+idc] = t[4+idt] = v[4]*s1+v[11]*s2+v[18]*s3+
13658f690400SShri Abhyankar                                  v[25]*s4+v[32]*s5+v[39]*s6+v[46]*s7;
13668f690400SShri Abhyankar     x[5+idc] = t[5+idt] = v[5]*s1+v[12]*s2+v[19]*s3+
13678f690400SShri Abhyankar                                  v[26]*s4+v[33]*s5+v[40]*s6+v[47]*s7;
13688f690400SShri Abhyankar     x[6+idc] = t[6+idt] = v[6]*s1+v[13]*s2+v[20]*s3+
13698f690400SShri Abhyankar                                  v[27]*s4+v[34]*s5+v[41]*s6+v[48]*s7;
13708f690400SShri Abhyankar   }
13718f690400SShri Abhyankar 
13728f690400SShri Abhyankar   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
13738f690400SShri Abhyankar   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
13748f690400SShri Abhyankar   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
13758f690400SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13768f690400SShri Abhyankar   ierr = PetscLogFlops(2.0*49*(a->nz) - 7.0*A->cmap->n);CHKERRQ(ierr);
13778f690400SShri Abhyankar   PetscFunctionReturn(0);
13788f690400SShri Abhyankar }
13798f690400SShri Abhyankar 
13808f690400SShri Abhyankar #undef __FUNCT__
13814a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_7_NaturalOrdering"
1382dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_7_NaturalOrdering(Mat A,Vec bb,Vec xx)
138315091d37SBarry Smith {
138415091d37SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
1385690b6cddSBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
1386dfbe8321SBarry Smith   PetscErrorCode    ierr;
1387690b6cddSBarry Smith   PetscInt          *diag = a->diag,jdx;
1388d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
1389d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
1390d9fead3dSBarry Smith   const PetscScalar *b;
139115091d37SBarry Smith 
139215091d37SBarry Smith   PetscFunctionBegin;
1393d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
13941ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
139515091d37SBarry Smith   /* forward solve the lower triangular */
139615091d37SBarry Smith   idx    = 0;
139715091d37SBarry Smith   x[0] = b[idx];   x[1] = b[1+idx]; x[2] = b[2+idx];
139815091d37SBarry Smith   x[3] = b[3+idx]; x[4] = b[4+idx]; x[5] = b[5+idx];
139915091d37SBarry Smith   x[6] = b[6+idx];
140015091d37SBarry Smith   for (i=1; i<n; i++) {
140115091d37SBarry Smith     v     =  aa + 49*ai[i];
140215091d37SBarry Smith     vi    =  aj + ai[i];
140315091d37SBarry Smith     nz    =  diag[i] - ai[i];
140415091d37SBarry Smith     idx   =  7*i;
1405f1af5d2fSBarry Smith     s1  =  b[idx];   s2 = b[1+idx]; s3 = b[2+idx];
1406f1af5d2fSBarry Smith     s4  =  b[3+idx]; s5 = b[4+idx]; s6 = b[5+idx];
1407f1af5d2fSBarry Smith     s7  =  b[6+idx];
140815091d37SBarry Smith     while (nz--) {
140915091d37SBarry Smith       jdx   = 7*(*vi++);
141015091d37SBarry Smith       x1    = x[jdx];   x2 = x[1+jdx]; x3 = x[2+jdx];
141115091d37SBarry Smith       x4    = x[3+jdx]; x5 = x[4+jdx]; x6 = x[5+jdx];
141215091d37SBarry Smith       x7    = x[6+jdx];
1413f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
1414f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
1415f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
1416f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
1417f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
1418f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
1419f1af5d2fSBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
142015091d37SBarry Smith       v += 49;
142115091d37SBarry Smith      }
1422f1af5d2fSBarry Smith     x[idx]   = s1;
1423f1af5d2fSBarry Smith     x[1+idx] = s2;
1424f1af5d2fSBarry Smith     x[2+idx] = s3;
1425f1af5d2fSBarry Smith     x[3+idx] = s4;
1426f1af5d2fSBarry Smith     x[4+idx] = s5;
1427f1af5d2fSBarry Smith     x[5+idx] = s6;
1428f1af5d2fSBarry Smith     x[6+idx] = s7;
142915091d37SBarry Smith   }
143015091d37SBarry Smith   /* backward solve the upper triangular */
143115091d37SBarry Smith   for (i=n-1; i>=0; i--){
143215091d37SBarry Smith     v    = aa + 49*diag[i] + 49;
143315091d37SBarry Smith     vi   = aj + diag[i] + 1;
143415091d37SBarry Smith     nz   = ai[i+1] - diag[i] - 1;
143515091d37SBarry Smith     idt  = 7*i;
1436f1af5d2fSBarry Smith     s1 = x[idt];   s2 = x[1+idt];
1437f1af5d2fSBarry Smith     s3 = x[2+idt]; s4 = x[3+idt];
1438f1af5d2fSBarry Smith     s5 = x[4+idt]; s6 = x[5+idt];
1439f1af5d2fSBarry Smith     s7 = x[6+idt];
144015091d37SBarry Smith     while (nz--) {
144115091d37SBarry Smith       idx   = 7*(*vi++);
144215091d37SBarry Smith       x1    = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];
144315091d37SBarry Smith       x4    = x[3+idx]; x5 = x[4+idx]; x6 = x[5+idx];
144415091d37SBarry Smith       x7    = x[6+idx];
1445f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
1446f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
1447f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
1448f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
1449f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
1450f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
1451f1af5d2fSBarry Smith       s7 -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
145215091d37SBarry Smith       v += 49;
145315091d37SBarry Smith     }
145415091d37SBarry Smith     v        = aa + 49*diag[i];
1455f1af5d2fSBarry Smith     x[idt]   = v[0]*s1 + v[7]*s2  + v[14]*s3 + v[21]*s4
1456f1af5d2fSBarry Smith                          + v[28]*s5 + v[35]*s6 + v[42]*s7;
1457f1af5d2fSBarry Smith     x[1+idt] = v[1]*s1 + v[8]*s2  + v[15]*s3 + v[22]*s4
1458f1af5d2fSBarry Smith                          + v[29]*s5 + v[36]*s6 + v[43]*s7;
1459f1af5d2fSBarry Smith     x[2+idt] = v[2]*s1 + v[9]*s2  + v[16]*s3 + v[23]*s4
1460f1af5d2fSBarry Smith                          + v[30]*s5 + v[37]*s6 + v[44]*s7;
1461f1af5d2fSBarry Smith     x[3+idt] = v[3]*s1 + v[10]*s2  + v[17]*s3 + v[24]*s4
1462f1af5d2fSBarry Smith                          + v[31]*s5 + v[38]*s6 + v[45]*s7;
1463f1af5d2fSBarry Smith     x[4+idt] = v[4]*s1 + v[11]*s2  + v[18]*s3 + v[25]*s4
1464f1af5d2fSBarry Smith                          + v[32]*s5 + v[39]*s6 + v[46]*s7;
1465f1af5d2fSBarry Smith     x[5+idt] = v[5]*s1 + v[12]*s2  + v[19]*s3 + v[26]*s4
1466f1af5d2fSBarry Smith                          + v[33]*s5 + v[40]*s6 + v[47]*s7;
1467f1af5d2fSBarry Smith     x[6+idt] = v[6]*s1 + v[13]*s2  + v[20]*s3 + v[27]*s4
1468f1af5d2fSBarry Smith                          + v[34]*s5 + v[41]*s6 + v[48]*s7;
146915091d37SBarry Smith   }
147015091d37SBarry Smith 
1471d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
14721ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1473dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*36*(a->nz) - 6.0*A->cmap->n);CHKERRQ(ierr);
147415091d37SBarry Smith   PetscFunctionReturn(0);
147515091d37SBarry Smith }
147615091d37SBarry Smith 
14774a2ae208SSatish Balay #undef __FUNCT__
1478cee9d6f2SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_7_NaturalOrdering_newdatastruct"
1479cee9d6f2SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_7_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx)
1480cee9d6f2SShri Abhyankar {
1481cee9d6f2SShri Abhyankar     Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
14826464896eSShri Abhyankar     PetscInt          i,k,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz;
1483cee9d6f2SShri Abhyankar     PetscErrorCode    ierr;
1484cee9d6f2SShri Abhyankar     PetscInt          idx,jdx,idt;
1485cee9d6f2SShri Abhyankar     PetscInt          bs = A->rmap->bs,bs2 = a->bs2;
1486cee9d6f2SShri Abhyankar     const MatScalar   *aa=a->a,*v;
1487cee9d6f2SShri Abhyankar     PetscScalar       *x;
1488cee9d6f2SShri Abhyankar     const PetscScalar *b;
1489cee9d6f2SShri Abhyankar     PetscScalar        s1,s2,s3,s4,s5,s6,s7,x1,x2,x3,x4,x5,x6,x7;
1490cee9d6f2SShri Abhyankar 
1491cee9d6f2SShri Abhyankar     PetscFunctionBegin;
1492cee9d6f2SShri Abhyankar     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1493cee9d6f2SShri Abhyankar     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1494cee9d6f2SShri Abhyankar     /* forward solve the lower triangular */
1495cee9d6f2SShri Abhyankar     idx    = 0;
1496cee9d6f2SShri Abhyankar     x[0] = b[idx]; x[1] = b[1+idx];x[2] = b[2+idx];x[3] = b[3+idx];
1497cee9d6f2SShri Abhyankar     x[4] = b[4+idx];x[5] = b[5+idx];x[6] = b[6+idx];
1498cee9d6f2SShri Abhyankar     for (i=1; i<n; i++) {
1499cee9d6f2SShri Abhyankar        v    = aa + bs2*ai[i];
1500cee9d6f2SShri Abhyankar        vi   = aj + ai[i];
1501cee9d6f2SShri Abhyankar        nz   = ai[i+1] - ai[i];
1502cee9d6f2SShri Abhyankar       idx   = bs*i;
1503cee9d6f2SShri Abhyankar        s1   = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
1504cee9d6f2SShri Abhyankar        s5   = b[4+idx];s6 = b[5+idx];s7 = b[6+idx];
15056464896eSShri Abhyankar        for(k=0;k<nz;k++) {
15066464896eSShri Abhyankar           jdx   = bs*vi[k];
1507cee9d6f2SShri Abhyankar           x1    = x[jdx];x2 = x[1+jdx]; x3 =x[2+jdx];x4 =x[3+jdx];
1508cee9d6f2SShri Abhyankar 	  x5    = x[4+jdx]; x6 = x[5+jdx];x7 = x[6+jdx];
1509cee9d6f2SShri Abhyankar           s1   -= v[0]*x1 + v[7]*x2 + v[14]*x3 + v[21]*x4  + v[28]*x5 + v[35]*x6 + v[42]*x7;
1510cee9d6f2SShri Abhyankar           s2   -= v[1]*x1 + v[8]*x2 + v[15]*x3 + v[22]*x4  + v[29]*x5 + v[36]*x6 + v[43]*x7;
1511cee9d6f2SShri Abhyankar           s3   -= v[2]*x1 + v[9]*x2 + v[16]*x3 + v[23]*x4  + v[30]*x5 + v[37]*x6 + v[44]*x7;
1512cee9d6f2SShri Abhyankar 	  s4   -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4  + v[31]*x5 + v[38]*x6 + v[45]*x7;
1513cee9d6f2SShri Abhyankar           s5   -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4  + v[32]*x5 + v[39]*x6 + v[46]*x7;
1514cee9d6f2SShri Abhyankar 	  s6   -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4  + v[33]*x5 + v[40]*x6 + v[47]*x7;
1515cee9d6f2SShri Abhyankar 	  s7   -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4  + v[34]*x5 + v[41]*x6 + v[48]*x7;
1516cee9d6f2SShri Abhyankar           v   +=  bs2;
1517cee9d6f2SShri Abhyankar         }
1518cee9d6f2SShri Abhyankar 
1519cee9d6f2SShri Abhyankar        x[idx]   = s1;
1520cee9d6f2SShri Abhyankar        x[1+idx] = s2;
1521cee9d6f2SShri Abhyankar        x[2+idx] = s3;
1522cee9d6f2SShri Abhyankar        x[3+idx] = s4;
1523cee9d6f2SShri Abhyankar        x[4+idx] = s5;
1524cee9d6f2SShri Abhyankar        x[5+idx] = s6;
1525cee9d6f2SShri Abhyankar        x[6+idx] = s7;
1526cee9d6f2SShri Abhyankar     }
1527cee9d6f2SShri Abhyankar 
1528cee9d6f2SShri Abhyankar    /* backward solve the upper triangular */
1529cee9d6f2SShri Abhyankar   for (i=n-1; i>=0; i--){
1530cee9d6f2SShri Abhyankar      v   = aa + bs2*ai[2*n-i];
1531cee9d6f2SShri Abhyankar      vi  = aj + ai[2*n-i];
1532cee9d6f2SShri Abhyankar      nz  = ai[2*n-i +1] - ai[2*n-i]-1;
1533cee9d6f2SShri Abhyankar      idt = bs*i;
1534cee9d6f2SShri Abhyankar      s1 = x[idt];  s2 = x[1+idt];s3 = x[2+idt];s4 = x[3+idt];
1535cee9d6f2SShri Abhyankar      s5 = x[4+idt];s6 = x[5+idt];s7 = x[6+idt];
15366464896eSShri Abhyankar     for(k=0;k<nz;k++) {
15376464896eSShri Abhyankar       idx   = bs*vi[k];
1538cee9d6f2SShri Abhyankar        x1    = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];x4 = x[3+idx];
1539cee9d6f2SShri Abhyankar        x5    = x[4+idx];x6 = x[5+idx];x7 = x[6+idx];
1540cee9d6f2SShri Abhyankar        s1   -= v[0]*x1 + v[7]*x2 + v[14]*x3 + v[21]*x4  + v[28]*x5 + v[35]*x6 + v[42]*x7;
1541cee9d6f2SShri Abhyankar        s2   -= v[1]*x1 + v[8]*x2 + v[15]*x3 + v[22]*x4  + v[29]*x5 + v[36]*x6 + v[43]*x7;
1542cee9d6f2SShri Abhyankar        s3   -= v[2]*x1 + v[9]*x2 + v[16]*x3 + v[23]*x4  + v[30]*x5 + v[37]*x6 + v[44]*x7;
1543cee9d6f2SShri Abhyankar        s4   -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4  + v[31]*x5 + v[38]*x6 + v[45]*x7;
1544cee9d6f2SShri Abhyankar        s5   -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4  + v[32]*x5 + v[39]*x6 + v[46]*x7;
1545cee9d6f2SShri Abhyankar        s6   -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4  + v[33]*x5 + v[40]*x6 + v[47]*x7;
1546cee9d6f2SShri Abhyankar        s7   -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4  + v[34]*x5 + v[41]*x6 + v[48]*x7;
1547cee9d6f2SShri Abhyankar         v   +=  bs2;
1548cee9d6f2SShri Abhyankar     }
1549cee9d6f2SShri Abhyankar     /* x = inv_diagonal*x */
1550cee9d6f2SShri Abhyankar     x[idt]   = v[0]*s1 + v[7]*s2 + v[14]*s3 + v[21]*s4  + v[28]*s5 + v[35]*s6 + v[42]*s7;
1551cee9d6f2SShri Abhyankar     x[1+idt] = v[1]*s1 + v[8]*s2 + v[15]*s3 + v[22]*s4  + v[29]*s5 + v[36]*s6 + v[43]*s7;
1552cee9d6f2SShri Abhyankar     x[2+idt] = v[2]*s1 + v[9]*s2 + v[16]*s3 + v[23]*s4  + v[30]*s5 + v[37]*s6 + v[44]*s7;
1553cee9d6f2SShri Abhyankar     x[3+idt] = v[3]*s1 + v[10]*s2 + v[17]*s3 + v[24]*s4  + v[31]*s5 + v[38]*s6 + v[45]*s7;
1554cee9d6f2SShri Abhyankar     x[4+idt] = v[4]*s1 + v[11]*s2 + v[18]*s3 + v[25]*s4  + v[32]*s5 + v[39]*s6 + v[46]*s7;
1555cee9d6f2SShri Abhyankar     x[5+idt] = v[5]*s1 + v[12]*s2 + v[19]*s3 + v[26]*s4  + v[33]*s5 + v[40]*s6 + v[47]*s7;
1556cee9d6f2SShri Abhyankar     x[6+idt] = v[6]*s1 + v[13]*s2 + v[20]*s3 + v[27]*s4  + v[34]*s5 + v[41]*s6 + v[48]*s7;
1557cee9d6f2SShri Abhyankar   }
1558cee9d6f2SShri Abhyankar 
1559cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1560cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1561cee9d6f2SShri Abhyankar   ierr = PetscLogFlops(2.0*bs2*(a->nz) - bs*A->cmap->n);CHKERRQ(ierr);
1562cee9d6f2SShri Abhyankar   PetscFunctionReturn(0);
1563cee9d6f2SShri Abhyankar }
1564cee9d6f2SShri Abhyankar 
1565cee9d6f2SShri Abhyankar #undef __FUNCT__
15664a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_6"
1567dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_6(Mat A,Vec bb,Vec xx)
156815091d37SBarry Smith {
156915091d37SBarry Smith   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
157015091d37SBarry Smith   IS                iscol=a->col,isrow=a->row;
15716849ba73SBarry Smith   PetscErrorCode    ierr;
15725d0c19d7SBarry Smith   const PetscInt    *r,*c,*rout,*cout;
15735d0c19d7SBarry Smith   PetscInt          *diag = a->diag,i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc;
1574d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
1575d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,s4,s5,s6,x1,x2,x3,x4,x5,x6,*t;
1576d9fead3dSBarry Smith   const PetscScalar *b;
157715091d37SBarry Smith   PetscFunctionBegin;
1578d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
15791ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1580f1af5d2fSBarry Smith   t  = a->solve_work;
158115091d37SBarry Smith 
158215091d37SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
158315091d37SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
158415091d37SBarry Smith 
158515091d37SBarry Smith   /* forward solve the lower triangular */
158615091d37SBarry Smith   idx    = 6*(*r++);
1587f1af5d2fSBarry Smith   t[0] = b[idx];   t[1] = b[1+idx];
1588f1af5d2fSBarry Smith   t[2] = b[2+idx]; t[3] = b[3+idx];
1589f1af5d2fSBarry Smith   t[4] = b[4+idx]; t[5] = b[5+idx];
159015091d37SBarry Smith   for (i=1; i<n; i++) {
159115091d37SBarry Smith     v     = aa + 36*ai[i];
159215091d37SBarry Smith     vi    = aj + ai[i];
159315091d37SBarry Smith     nz    = diag[i] - ai[i];
159415091d37SBarry Smith     idx   = 6*(*r++);
1595f1af5d2fSBarry Smith     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
1596f1af5d2fSBarry Smith     s5  = b[4+idx]; s6 = b[5+idx];
159715091d37SBarry Smith     while (nz--) {
159815091d37SBarry Smith       idx   = 6*(*vi++);
1599f1af5d2fSBarry Smith       x1    = t[idx];   x2 = t[1+idx]; x3 = t[2+idx];
1600f1af5d2fSBarry Smith       x4    = t[3+idx]; x5 = t[4+idx]; x6 = t[5+idx];
1601f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
1602f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
1603f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
1604f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
1605f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
1606f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
160715091d37SBarry Smith       v += 36;
160815091d37SBarry Smith     }
160915091d37SBarry Smith     idx = 6*i;
1610f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2;
1611f1af5d2fSBarry Smith     t[2+idx] = s3;t[3+idx] = s4;
1612f1af5d2fSBarry Smith     t[4+idx] = s5;t[5+idx] = s6;
161315091d37SBarry Smith   }
161415091d37SBarry Smith   /* backward solve the upper triangular */
161515091d37SBarry Smith   for (i=n-1; i>=0; i--){
161615091d37SBarry Smith     v    = aa + 36*diag[i] + 36;
161715091d37SBarry Smith     vi   = aj + diag[i] + 1;
161815091d37SBarry Smith     nz   = ai[i+1] - diag[i] - 1;
161915091d37SBarry Smith     idt  = 6*i;
1620f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt];
1621f1af5d2fSBarry Smith     s3 = t[2+idt];s4 = t[3+idt];
1622f1af5d2fSBarry Smith     s5 = t[4+idt];s6 = t[5+idt];
162315091d37SBarry Smith     while (nz--) {
162415091d37SBarry Smith       idx   = 6*(*vi++);
1625f1af5d2fSBarry Smith       x1    = t[idx];   x2 = t[1+idx];
1626f1af5d2fSBarry Smith       x3    = t[2+idx]; x4 = t[3+idx];
1627f1af5d2fSBarry Smith       x5    = t[4+idx]; x6 = t[5+idx];
1628f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
1629f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
1630f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
1631f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
1632f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
1633f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
163415091d37SBarry Smith       v += 36;
163515091d37SBarry Smith     }
163615091d37SBarry Smith     idc = 6*(*c--);
163715091d37SBarry Smith     v   = aa + 36*diag[i];
1638f1af5d2fSBarry Smith     x[idc]   = t[idt]   = v[0]*s1+v[6]*s2+v[12]*s3+
1639f1af5d2fSBarry Smith                                  v[18]*s4+v[24]*s5+v[30]*s6;
1640f1af5d2fSBarry Smith     x[1+idc] = t[1+idt] = v[1]*s1+v[7]*s2+v[13]*s3+
1641f1af5d2fSBarry Smith                                  v[19]*s4+v[25]*s5+v[31]*s6;
1642f1af5d2fSBarry Smith     x[2+idc] = t[2+idt] = v[2]*s1+v[8]*s2+v[14]*s3+
1643f1af5d2fSBarry Smith                                  v[20]*s4+v[26]*s5+v[32]*s6;
1644f1af5d2fSBarry Smith     x[3+idc] = t[3+idt] = v[3]*s1+v[9]*s2+v[15]*s3+
1645f1af5d2fSBarry Smith                                  v[21]*s4+v[27]*s5+v[33]*s6;
1646f1af5d2fSBarry Smith     x[4+idc] = t[4+idt] = v[4]*s1+v[10]*s2+v[16]*s3+
1647f1af5d2fSBarry Smith                                  v[22]*s4+v[28]*s5+v[34]*s6;
1648f1af5d2fSBarry Smith     x[5+idc] = t[5+idt] = v[5]*s1+v[11]*s2+v[17]*s3+
1649f1af5d2fSBarry Smith                                  v[23]*s4+v[29]*s5+v[35]*s6;
165015091d37SBarry Smith   }
165115091d37SBarry Smith 
165215091d37SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
165315091d37SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1654d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
16551ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1656dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*36*(a->nz) - 6.0*A->cmap->n);CHKERRQ(ierr);
165715091d37SBarry Smith   PetscFunctionReturn(0);
165815091d37SBarry Smith }
165915091d37SBarry Smith 
16604a2ae208SSatish Balay #undef __FUNCT__
16618f690400SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_6_newdatastruct"
16628f690400SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_6_newdatastruct(Mat A,Vec bb,Vec xx)
16638f690400SShri Abhyankar {
16648f690400SShri Abhyankar   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
16658f690400SShri Abhyankar   IS                iscol=a->col,isrow=a->row;
16668f690400SShri Abhyankar   PetscErrorCode    ierr;
16678f690400SShri Abhyankar   const PetscInt    *r,*c,*rout,*cout;
166829b92fc1SShri Abhyankar   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc,k,m;
16698f690400SShri Abhyankar   const MatScalar   *aa=a->a,*v;
16708f690400SShri Abhyankar   PetscScalar       *x,s1,s2,s3,s4,s5,s6,x1,x2,x3,x4,x5,x6,*t;
16718f690400SShri Abhyankar   const PetscScalar *b;
16728f690400SShri Abhyankar   PetscFunctionBegin;
16738f690400SShri Abhyankar   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
16748f690400SShri Abhyankar   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
16758f690400SShri Abhyankar   t  = a->solve_work;
16768f690400SShri Abhyankar 
16778f690400SShri Abhyankar   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
167829b92fc1SShri Abhyankar   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
16798f690400SShri Abhyankar 
16808f690400SShri Abhyankar   /* forward solve the lower triangular */
168129b92fc1SShri Abhyankar   idx    = 6*r[0];
16828f690400SShri Abhyankar   t[0] = b[idx];   t[1] = b[1+idx];
16838f690400SShri Abhyankar   t[2] = b[2+idx]; t[3] = b[3+idx];
16848f690400SShri Abhyankar   t[4] = b[4+idx]; t[5] = b[5+idx];
16858f690400SShri Abhyankar   for (i=1; i<n; i++) {
16868f690400SShri Abhyankar     v     = aa + 36*ai[i];
16878f690400SShri Abhyankar     vi    = aj + ai[i];
16888f690400SShri Abhyankar     nz    = ai[i+1] - ai[i];
168929b92fc1SShri Abhyankar     idx   = 6*r[i];
16908f690400SShri Abhyankar     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
16918f690400SShri Abhyankar     s5  = b[4+idx]; s6 = b[5+idx];
169229b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
169329b92fc1SShri Abhyankar       idx   = 6*vi[m];
16948f690400SShri Abhyankar       x1    = t[idx];   x2 = t[1+idx]; x3 = t[2+idx];
16958f690400SShri Abhyankar       x4    = t[3+idx]; x5 = t[4+idx]; x6 = t[5+idx];
16968f690400SShri Abhyankar       s1 -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
16978f690400SShri Abhyankar       s2 -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
16988f690400SShri Abhyankar       s3 -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
16998f690400SShri Abhyankar       s4 -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
17008f690400SShri Abhyankar       s5 -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
17018f690400SShri Abhyankar       s6 -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
17028f690400SShri Abhyankar       v += 36;
17038f690400SShri Abhyankar     }
17048f690400SShri Abhyankar     idx = 6*i;
17058f690400SShri Abhyankar     t[idx]   = s1;t[1+idx] = s2;
17068f690400SShri Abhyankar     t[2+idx] = s3;t[3+idx] = s4;
17078f690400SShri Abhyankar     t[4+idx] = s5;t[5+idx] = s6;
17088f690400SShri Abhyankar   }
17098f690400SShri Abhyankar   /* backward solve the upper triangular */
17108f690400SShri Abhyankar   for (i=n-1; i>=0; i--){
17118f690400SShri Abhyankar     k    = 2*n-i;
17128f690400SShri Abhyankar     v    = aa + 36*ai[k];
17138f690400SShri Abhyankar     vi   = aj + ai[k];
17148f690400SShri Abhyankar     nz   = ai[k+1] - ai[k] - 1;
17158f690400SShri Abhyankar     idt  = 6*i;
17168f690400SShri Abhyankar     s1 = t[idt];  s2 = t[1+idt];
17178f690400SShri Abhyankar     s3 = t[2+idt];s4 = t[3+idt];
17188f690400SShri Abhyankar     s5 = t[4+idt];s6 = t[5+idt];
171929b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
172029b92fc1SShri Abhyankar       idx   = 6*vi[m];
17218f690400SShri Abhyankar       x1    = t[idx];   x2 = t[1+idx];
17228f690400SShri Abhyankar       x3    = t[2+idx]; x4 = t[3+idx];
17238f690400SShri Abhyankar       x5    = t[4+idx]; x6 = t[5+idx];
17248f690400SShri Abhyankar       s1 -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
17258f690400SShri Abhyankar       s2 -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
17268f690400SShri Abhyankar       s3 -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
17278f690400SShri Abhyankar       s4 -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
17288f690400SShri Abhyankar       s5 -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
17298f690400SShri Abhyankar       s6 -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
17308f690400SShri Abhyankar       v += 36;
17318f690400SShri Abhyankar     }
173229b92fc1SShri Abhyankar     idc = 6*c[i];
17338f690400SShri Abhyankar     x[idc]   = t[idt]   = v[0]*s1+v[6]*s2+v[12]*s3+
17348f690400SShri Abhyankar                                  v[18]*s4+v[24]*s5+v[30]*s6;
17358f690400SShri Abhyankar     x[1+idc] = t[1+idt] = v[1]*s1+v[7]*s2+v[13]*s3+
17368f690400SShri Abhyankar                                  v[19]*s4+v[25]*s5+v[31]*s6;
17378f690400SShri Abhyankar     x[2+idc] = t[2+idt] = v[2]*s1+v[8]*s2+v[14]*s3+
17388f690400SShri Abhyankar                                  v[20]*s4+v[26]*s5+v[32]*s6;
17398f690400SShri Abhyankar     x[3+idc] = t[3+idt] = v[3]*s1+v[9]*s2+v[15]*s3+
17408f690400SShri Abhyankar                                  v[21]*s4+v[27]*s5+v[33]*s6;
17418f690400SShri Abhyankar     x[4+idc] = t[4+idt] = v[4]*s1+v[10]*s2+v[16]*s3+
17428f690400SShri Abhyankar                                  v[22]*s4+v[28]*s5+v[34]*s6;
17438f690400SShri Abhyankar     x[5+idc] = t[5+idt] = v[5]*s1+v[11]*s2+v[17]*s3+
17448f690400SShri Abhyankar                                  v[23]*s4+v[29]*s5+v[35]*s6;
17458f690400SShri Abhyankar   }
17468f690400SShri Abhyankar 
17478f690400SShri Abhyankar   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
17488f690400SShri Abhyankar   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
17498f690400SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
17508f690400SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17518f690400SShri Abhyankar   ierr = PetscLogFlops(2.0*36*(a->nz) - 6.0*A->cmap->n);CHKERRQ(ierr);
17528f690400SShri Abhyankar   PetscFunctionReturn(0);
17538f690400SShri Abhyankar }
17548f690400SShri Abhyankar 
17558f690400SShri Abhyankar 
17568f690400SShri Abhyankar #undef __FUNCT__
17574a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_6_NaturalOrdering"
1758dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_6_NaturalOrdering(Mat A,Vec bb,Vec xx)
175915091d37SBarry Smith {
176015091d37SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
1761690b6cddSBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
1762dfbe8321SBarry Smith   PetscErrorCode    ierr;
1763690b6cddSBarry Smith   PetscInt          *diag = a->diag,jdx;
1764d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
1765d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,s4,s5,s6,x1,x2,x3,x4,x5,x6;
1766d9fead3dSBarry Smith   const PetscScalar *b;
176715091d37SBarry Smith 
176815091d37SBarry Smith   PetscFunctionBegin;
1769d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
17701ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
177115091d37SBarry Smith   /* forward solve the lower triangular */
177215091d37SBarry Smith   idx    = 0;
177315091d37SBarry Smith   x[0] = b[idx];   x[1] = b[1+idx]; x[2] = b[2+idx];
177415091d37SBarry Smith   x[3] = b[3+idx]; x[4] = b[4+idx]; x[5] = b[5+idx];
177515091d37SBarry Smith   for (i=1; i<n; i++) {
177615091d37SBarry Smith     v     =  aa + 36*ai[i];
177715091d37SBarry Smith     vi    =  aj + ai[i];
177815091d37SBarry Smith     nz    =  diag[i] - ai[i];
177915091d37SBarry Smith     idx   =  6*i;
1780f1af5d2fSBarry Smith     s1  =  b[idx];   s2 = b[1+idx]; s3 = b[2+idx];
1781f1af5d2fSBarry Smith     s4  =  b[3+idx]; s5 = b[4+idx]; s6 = b[5+idx];
178215091d37SBarry Smith     while (nz--) {
178315091d37SBarry Smith       jdx   = 6*(*vi++);
178415091d37SBarry Smith       x1    = x[jdx];   x2 = x[1+jdx]; x3 = x[2+jdx];
178515091d37SBarry Smith       x4    = x[3+jdx]; x5 = x[4+jdx]; x6 = x[5+jdx];
1786f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
1787f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
1788f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
1789f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
1790f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
1791f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
179215091d37SBarry Smith       v += 36;
179315091d37SBarry Smith      }
1794f1af5d2fSBarry Smith     x[idx]   = s1;
1795f1af5d2fSBarry Smith     x[1+idx] = s2;
1796f1af5d2fSBarry Smith     x[2+idx] = s3;
1797f1af5d2fSBarry Smith     x[3+idx] = s4;
1798f1af5d2fSBarry Smith     x[4+idx] = s5;
1799f1af5d2fSBarry Smith     x[5+idx] = s6;
180015091d37SBarry Smith   }
180115091d37SBarry Smith   /* backward solve the upper triangular */
180215091d37SBarry Smith   for (i=n-1; i>=0; i--){
180315091d37SBarry Smith     v    = aa + 36*diag[i] + 36;
180415091d37SBarry Smith     vi   = aj + diag[i] + 1;
180515091d37SBarry Smith     nz   = ai[i+1] - diag[i] - 1;
180615091d37SBarry Smith     idt  = 6*i;
1807f1af5d2fSBarry Smith     s1 = x[idt];   s2 = x[1+idt];
1808f1af5d2fSBarry Smith     s3 = x[2+idt]; s4 = x[3+idt];
1809f1af5d2fSBarry Smith     s5 = x[4+idt]; s6 = x[5+idt];
181015091d37SBarry Smith     while (nz--) {
181115091d37SBarry Smith       idx   = 6*(*vi++);
181215091d37SBarry Smith       x1    = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];
181315091d37SBarry Smith       x4    = x[3+idx]; x5 = x[4+idx]; x6 = x[5+idx];
1814f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
1815f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
1816f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
1817f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
1818f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
1819f1af5d2fSBarry Smith       s6 -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
182015091d37SBarry Smith       v += 36;
182115091d37SBarry Smith     }
182215091d37SBarry Smith     v        = aa + 36*diag[i];
1823f1af5d2fSBarry Smith     x[idt]   = v[0]*s1 + v[6]*s2  + v[12]*s3 + v[18]*s4 + v[24]*s5 + v[30]*s6;
1824f1af5d2fSBarry Smith     x[1+idt] = v[1]*s1 + v[7]*s2  + v[13]*s3 + v[19]*s4 + v[25]*s5 + v[31]*s6;
1825f1af5d2fSBarry Smith     x[2+idt] = v[2]*s1 + v[8]*s2  + v[14]*s3 + v[20]*s4 + v[26]*s5 + v[32]*s6;
1826f1af5d2fSBarry Smith     x[3+idt] = v[3]*s1 + v[9]*s2  + v[15]*s3 + v[21]*s4 + v[27]*s5 + v[33]*s6;
1827f1af5d2fSBarry Smith     x[4+idt] = v[4]*s1 + v[10]*s2 + v[16]*s3 + v[22]*s4 + v[28]*s5 + v[34]*s6;
1828f1af5d2fSBarry Smith     x[5+idt] = v[5]*s1 + v[11]*s2 + v[17]*s3 + v[23]*s4 + v[29]*s5 + v[35]*s6;
182915091d37SBarry Smith   }
183015091d37SBarry Smith 
1831d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
18321ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1833dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*36*(a->nz) - 6.0*A->cmap->n);CHKERRQ(ierr);
183415091d37SBarry Smith   PetscFunctionReturn(0);
183515091d37SBarry Smith }
183615091d37SBarry Smith 
18374a2ae208SSatish Balay #undef __FUNCT__
1838cee9d6f2SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_6_NaturalOrdering_newdatastruct"
1839cee9d6f2SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_6_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx)
1840cee9d6f2SShri Abhyankar {
1841cee9d6f2SShri Abhyankar     Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
18426464896eSShri Abhyankar     PetscInt          i,k,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz;
1843cee9d6f2SShri Abhyankar     PetscErrorCode    ierr;
1844cee9d6f2SShri Abhyankar     PetscInt          idx,jdx,idt;
1845cee9d6f2SShri Abhyankar     PetscInt          bs = A->rmap->bs,bs2 = a->bs2;
1846cee9d6f2SShri Abhyankar     const MatScalar   *aa=a->a,*v;
1847cee9d6f2SShri Abhyankar     PetscScalar       *x;
1848cee9d6f2SShri Abhyankar     const PetscScalar *b;
1849cee9d6f2SShri Abhyankar     PetscScalar        s1,s2,s3,s4,s5,s6,x1,x2,x3,x4,x5,x6;
1850cee9d6f2SShri Abhyankar 
1851cee9d6f2SShri Abhyankar     PetscFunctionBegin;
1852cee9d6f2SShri Abhyankar     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1853cee9d6f2SShri Abhyankar     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1854cee9d6f2SShri Abhyankar     /* forward solve the lower triangular */
1855cee9d6f2SShri Abhyankar     idx    = 0;
1856cee9d6f2SShri Abhyankar     x[0] = b[idx]; x[1] = b[1+idx];x[2] = b[2+idx];x[3] = b[3+idx];
1857cee9d6f2SShri Abhyankar     x[4] = b[4+idx];x[5] = b[5+idx];
1858cee9d6f2SShri Abhyankar     for (i=1; i<n; i++) {
1859cee9d6f2SShri Abhyankar        v    = aa + bs2*ai[i];
1860cee9d6f2SShri Abhyankar        vi   = aj + ai[i];
1861cee9d6f2SShri Abhyankar        nz   = ai[i+1] - ai[i];
1862cee9d6f2SShri Abhyankar       idx   = bs*i;
1863cee9d6f2SShri Abhyankar        s1   = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
1864cee9d6f2SShri Abhyankar        s5   = b[4+idx];s6 = b[5+idx];
18656464896eSShri Abhyankar        for(k=0;k<nz;k++){
18666464896eSShri Abhyankar           jdx   = bs*vi[k];
1867cee9d6f2SShri Abhyankar           x1    = x[jdx];x2 = x[1+jdx]; x3 =x[2+jdx];x4 =x[3+jdx];
1868cee9d6f2SShri Abhyankar 	  x5    = x[4+jdx]; x6 = x[5+jdx];
1869cee9d6f2SShri Abhyankar           s1   -= v[0]*x1 + v[6]*x2 + v[12]*x3 + v[18]*x4  + v[24]*x5 + v[30]*x6;
1870cee9d6f2SShri Abhyankar           s2   -= v[1]*x1 + v[7]*x2 + v[13]*x3 + v[19]*x4  + v[25]*x5 + v[31]*x6;;
1871cee9d6f2SShri Abhyankar           s3   -= v[2]*x1 + v[8]*x2 + v[14]*x3 + v[20]*x4  + v[26]*x5 + v[32]*x6;
1872cee9d6f2SShri Abhyankar 	  s4   -= v[3]*x1 + v[9]*x2 + v[15]*x3 + v[21]*x4  + v[27]*x5 + v[33]*x6;
1873cee9d6f2SShri Abhyankar           s5   -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4  + v[28]*x5 + v[34]*x6;
1874cee9d6f2SShri Abhyankar 	  s6   -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4  + v[29]*x5 + v[35]*x6;
1875cee9d6f2SShri Abhyankar           v   +=  bs2;
1876cee9d6f2SShri Abhyankar         }
1877cee9d6f2SShri Abhyankar 
1878cee9d6f2SShri Abhyankar        x[idx]   = s1;
1879cee9d6f2SShri Abhyankar        x[1+idx] = s2;
1880cee9d6f2SShri Abhyankar        x[2+idx] = s3;
1881cee9d6f2SShri Abhyankar        x[3+idx] = s4;
1882cee9d6f2SShri Abhyankar        x[4+idx] = s5;
1883cee9d6f2SShri Abhyankar        x[5+idx] = s6;
1884cee9d6f2SShri Abhyankar     }
1885cee9d6f2SShri Abhyankar 
1886cee9d6f2SShri Abhyankar    /* backward solve the upper triangular */
1887cee9d6f2SShri Abhyankar   for (i=n-1; i>=0; i--){
1888cee9d6f2SShri Abhyankar      v   = aa + bs2*ai[2*n-i];
1889cee9d6f2SShri Abhyankar      vi  = aj + ai[2*n-i];
1890cee9d6f2SShri Abhyankar      nz  = ai[2*n-i +1] - ai[2*n-i]-1;
1891cee9d6f2SShri Abhyankar      idt = bs*i;
1892cee9d6f2SShri Abhyankar      s1 = x[idt];  s2 = x[1+idt];s3 = x[2+idt];s4 = x[3+idt];
1893cee9d6f2SShri Abhyankar      s5 = x[4+idt];s6 = x[5+idt];
18946464896eSShri Abhyankar      for(k=0;k<nz;k++){
18956464896eSShri Abhyankar       idx   = bs*vi[k];
1896cee9d6f2SShri Abhyankar        x1    = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];x4 = x[3+idx];
1897cee9d6f2SShri Abhyankar        x5    = x[4+idx];x6 = x[5+idx];
1898cee9d6f2SShri Abhyankar        s1   -= v[0]*x1 + v[6]*x2 + v[12]*x3 + v[18]*x4  + v[24]*x5 + v[30]*x6;
1899cee9d6f2SShri Abhyankar        s2   -= v[1]*x1 + v[7]*x2 + v[13]*x3 + v[19]*x4  + v[25]*x5 + v[31]*x6;;
1900cee9d6f2SShri Abhyankar        s3   -= v[2]*x1 + v[8]*x2 + v[14]*x3 + v[20]*x4  + v[26]*x5 + v[32]*x6;
1901cee9d6f2SShri Abhyankar        s4   -= v[3]*x1 + v[9]*x2 + v[15]*x3 + v[21]*x4  + v[27]*x5 + v[33]*x6;
1902cee9d6f2SShri Abhyankar        s5   -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4  + v[28]*x5 + v[34]*x6;
1903cee9d6f2SShri Abhyankar        s6   -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4  + v[29]*x5 + v[35]*x6;
1904cee9d6f2SShri Abhyankar         v   +=  bs2;
1905cee9d6f2SShri Abhyankar     }
1906cee9d6f2SShri Abhyankar     /* x = inv_diagonal*x */
1907cee9d6f2SShri Abhyankar    x[idt]   = v[0]*s1 + v[6]*s2 + v[12]*s3 + v[18]*s4 + v[24]*s5 + v[30]*s6;
1908cee9d6f2SShri Abhyankar    x[1+idt] = v[1]*s1 + v[7]*s2 + v[13]*s3 + v[19]*s4 + v[25]*s5 + v[31]*s6;
1909cee9d6f2SShri Abhyankar    x[2+idt] = v[2]*s1 + v[8]*s2 + v[14]*s3 + v[20]*s4 + v[26]*s5 + v[32]*s6;
1910cee9d6f2SShri Abhyankar    x[3+idt] = v[3]*s1 + v[9]*s2 + v[15]*s3 + v[21]*s4 + v[27]*s5 + v[33]*s6;
1911cee9d6f2SShri Abhyankar    x[4+idt] = v[4]*s1 + v[10]*s2 + v[16]*s3 + v[22]*s4 + v[28]*s5 + v[34]*s6;
1912cee9d6f2SShri Abhyankar    x[5+idt] = v[5]*s1 + v[11]*s2 + v[17]*s3 + v[23]*s4 + v[29]*s5 + v[35]*s6;
1913cee9d6f2SShri Abhyankar   }
1914cee9d6f2SShri Abhyankar 
1915cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1916cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1917cee9d6f2SShri Abhyankar   ierr = PetscLogFlops(2.0*bs2*(a->nz) - bs*A->cmap->n);CHKERRQ(ierr);
1918cee9d6f2SShri Abhyankar   PetscFunctionReturn(0);
1919cee9d6f2SShri Abhyankar }
19208f690400SShri Abhyankar 
1921cee9d6f2SShri Abhyankar #undef __FUNCT__
19224a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_5"
1923dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_5(Mat A,Vec bb,Vec xx)
19244e2b4712SSatish Balay {
19254e2b4712SSatish Balay   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
19264e2b4712SSatish Balay   IS                iscol=a->col,isrow=a->row;
19276849ba73SBarry Smith   PetscErrorCode    ierr;
19285d0c19d7SBarry Smith   const PetscInt    *r,*c,*rout,*cout,*diag = a->diag;
19295d0c19d7SBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc;
1930d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
1931d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,s4,s5,x1,x2,x3,x4,x5,*t;
1932d9fead3dSBarry Smith   const PetscScalar *b;
19334e2b4712SSatish Balay 
19344e2b4712SSatish Balay   PetscFunctionBegin;
1935d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
19361ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1937f1af5d2fSBarry Smith   t  = a->solve_work;
19384e2b4712SSatish Balay 
19394e2b4712SSatish Balay   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
19404e2b4712SSatish Balay   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
19414e2b4712SSatish Balay 
19424e2b4712SSatish Balay   /* forward solve the lower triangular */
19434e2b4712SSatish Balay   idx    = 5*(*r++);
1944f1af5d2fSBarry Smith   t[0] = b[idx];   t[1] = b[1+idx];
1945f1af5d2fSBarry Smith   t[2] = b[2+idx]; t[3] = b[3+idx]; t[4] = b[4+idx];
19464e2b4712SSatish Balay   for (i=1; i<n; i++) {
19474e2b4712SSatish Balay     v     = aa + 25*ai[i];
19484e2b4712SSatish Balay     vi    = aj + ai[i];
19494e2b4712SSatish Balay     nz    = diag[i] - ai[i];
19504e2b4712SSatish Balay     idx   = 5*(*r++);
1951f1af5d2fSBarry Smith     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
1952f1af5d2fSBarry Smith     s5  = b[4+idx];
19534e2b4712SSatish Balay     while (nz--) {
19544e2b4712SSatish Balay       idx   = 5*(*vi++);
1955f1af5d2fSBarry Smith       x1    = t[idx];  x2 = t[1+idx];x3 = t[2+idx];
1956f1af5d2fSBarry Smith       x4    = t[3+idx];x5 = t[4+idx];
1957f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3 + v[15]*x4 + v[20]*x5;
1958f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3 + v[16]*x4 + v[21]*x5;
1959f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3 + v[17]*x4 + v[22]*x5;
1960f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3 + v[18]*x4 + v[23]*x5;
1961f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3 + v[19]*x4 + v[24]*x5;
19624e2b4712SSatish Balay       v += 25;
19634e2b4712SSatish Balay     }
19644e2b4712SSatish Balay     idx = 5*i;
1965f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2;
1966f1af5d2fSBarry Smith     t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
19674e2b4712SSatish Balay   }
19684e2b4712SSatish Balay   /* backward solve the upper triangular */
19694e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
19704e2b4712SSatish Balay     v    = aa + 25*diag[i] + 25;
19714e2b4712SSatish Balay     vi   = aj + diag[i] + 1;
19724e2b4712SSatish Balay     nz   = ai[i+1] - diag[i] - 1;
19734e2b4712SSatish Balay     idt  = 5*i;
1974f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt];
1975f1af5d2fSBarry Smith     s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
19764e2b4712SSatish Balay     while (nz--) {
19774e2b4712SSatish Balay       idx   = 5*(*vi++);
1978f1af5d2fSBarry Smith       x1    = t[idx];   x2 = t[1+idx];
1979f1af5d2fSBarry Smith       x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
1980f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3 + v[15]*x4 + v[20]*x5;
1981f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3 + v[16]*x4 + v[21]*x5;
1982f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3 + v[17]*x4 + v[22]*x5;
1983f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3 + v[18]*x4 + v[23]*x5;
1984f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3 + v[19]*x4 + v[24]*x5;
19854e2b4712SSatish Balay       v += 25;
19864e2b4712SSatish Balay     }
19874e2b4712SSatish Balay     idc = 5*(*c--);
19884e2b4712SSatish Balay     v   = aa + 25*diag[i];
1989f1af5d2fSBarry Smith     x[idc]   = t[idt]   = v[0]*s1+v[5]*s2+v[10]*s3+
1990f1af5d2fSBarry Smith                                  v[15]*s4+v[20]*s5;
1991f1af5d2fSBarry Smith     x[1+idc] = t[1+idt] = v[1]*s1+v[6]*s2+v[11]*s3+
1992f1af5d2fSBarry Smith                                  v[16]*s4+v[21]*s5;
1993f1af5d2fSBarry Smith     x[2+idc] = t[2+idt] = v[2]*s1+v[7]*s2+v[12]*s3+
1994f1af5d2fSBarry Smith                                  v[17]*s4+v[22]*s5;
1995f1af5d2fSBarry Smith     x[3+idc] = t[3+idt] = v[3]*s1+v[8]*s2+v[13]*s3+
1996f1af5d2fSBarry Smith                                  v[18]*s4+v[23]*s5;
1997f1af5d2fSBarry Smith     x[4+idc] = t[4+idt] = v[4]*s1+v[9]*s2+v[14]*s3+
1998f1af5d2fSBarry Smith                                  v[19]*s4+v[24]*s5;
19994e2b4712SSatish Balay   }
20004e2b4712SSatish Balay 
20014e2b4712SSatish Balay   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
20024e2b4712SSatish Balay   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
2003d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
20041ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2005dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n);CHKERRQ(ierr);
20064e2b4712SSatish Balay   PetscFunctionReturn(0);
20074e2b4712SSatish Balay }
20084e2b4712SSatish Balay 
20094a2ae208SSatish Balay #undef __FUNCT__
20108f690400SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_5_newdatastruct"
20118f690400SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_5_newdatastruct(Mat A,Vec bb,Vec xx)
20128f690400SShri Abhyankar {
20138f690400SShri Abhyankar   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
20148f690400SShri Abhyankar   IS                iscol=a->col,isrow=a->row;
20158f690400SShri Abhyankar   PetscErrorCode    ierr;
20168f690400SShri Abhyankar   const PetscInt    *r,*c,*rout,*cout;
201729b92fc1SShri Abhyankar   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc,k,m;
20188f690400SShri Abhyankar   const MatScalar   *aa=a->a,*v;
20198f690400SShri Abhyankar   PetscScalar       *x,s1,s2,s3,s4,s5,x1,x2,x3,x4,x5,*t;
20208f690400SShri Abhyankar   const PetscScalar *b;
20218f690400SShri Abhyankar 
20228f690400SShri Abhyankar   PetscFunctionBegin;
20238f690400SShri Abhyankar   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
20248f690400SShri Abhyankar   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
20258f690400SShri Abhyankar   t  = a->solve_work;
20268f690400SShri Abhyankar 
20278f690400SShri Abhyankar   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
202829b92fc1SShri Abhyankar   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
20298f690400SShri Abhyankar 
20308f690400SShri Abhyankar   /* forward solve the lower triangular */
203129b92fc1SShri Abhyankar   idx    = 5*r[0];
20328f690400SShri Abhyankar   t[0] = b[idx];   t[1] = b[1+idx];
20338f690400SShri Abhyankar   t[2] = b[2+idx]; t[3] = b[3+idx]; t[4] = b[4+idx];
20348f690400SShri Abhyankar   for (i=1; i<n; i++) {
20358f690400SShri Abhyankar     v     = aa + 25*ai[i];
20368f690400SShri Abhyankar     vi    = aj + ai[i];
20378f690400SShri Abhyankar     nz    = ai[i+1] - ai[i];
203829b92fc1SShri Abhyankar     idx   = 5*r[i];
20398f690400SShri Abhyankar     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
20408f690400SShri Abhyankar     s5  = b[4+idx];
204129b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
204229b92fc1SShri Abhyankar       idx   = 5*vi[m];
20438f690400SShri Abhyankar       x1    = t[idx];  x2 = t[1+idx];x3 = t[2+idx];
20448f690400SShri Abhyankar       x4    = t[3+idx];x5 = t[4+idx];
20458f690400SShri Abhyankar       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3 + v[15]*x4 + v[20]*x5;
20468f690400SShri Abhyankar       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3 + v[16]*x4 + v[21]*x5;
20478f690400SShri Abhyankar       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3 + v[17]*x4 + v[22]*x5;
20488f690400SShri Abhyankar       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3 + v[18]*x4 + v[23]*x5;
20498f690400SShri Abhyankar       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3 + v[19]*x4 + v[24]*x5;
20508f690400SShri Abhyankar       v += 25;
20518f690400SShri Abhyankar     }
20528f690400SShri Abhyankar     idx = 5*i;
20538f690400SShri Abhyankar     t[idx]   = s1;t[1+idx] = s2;
20548f690400SShri Abhyankar     t[2+idx] = s3;t[3+idx] = s4; t[4+idx] = s5;
20558f690400SShri Abhyankar   }
20568f690400SShri Abhyankar   /* backward solve the upper triangular */
20578f690400SShri Abhyankar   for (i=n-1; i>=0; i--){
20588f690400SShri Abhyankar     k    = 2*n-i;
20598f690400SShri Abhyankar     v    = aa + 25*ai[k];
20608f690400SShri Abhyankar     vi   = aj + ai[k];
20618f690400SShri Abhyankar     nz   = ai[k+1] - ai[k] - 1;
20628f690400SShri Abhyankar     idt  = 5*i;
20638f690400SShri Abhyankar     s1 = t[idt];  s2 = t[1+idt];
20648f690400SShri Abhyankar     s3 = t[2+idt];s4 = t[3+idt]; s5 = t[4+idt];
206529b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
206629b92fc1SShri Abhyankar       idx   = 5*vi[m];
20678f690400SShri Abhyankar       x1    = t[idx];   x2 = t[1+idx];
20688f690400SShri Abhyankar       x3    = t[2+idx]; x4 = t[3+idx]; x5 = t[4+idx];
20698f690400SShri Abhyankar       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3 + v[15]*x4 + v[20]*x5;
20708f690400SShri Abhyankar       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3 + v[16]*x4 + v[21]*x5;
20718f690400SShri Abhyankar       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3 + v[17]*x4 + v[22]*x5;
20728f690400SShri Abhyankar       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3 + v[18]*x4 + v[23]*x5;
20738f690400SShri Abhyankar       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3 + v[19]*x4 + v[24]*x5;
20748f690400SShri Abhyankar       v += 25;
20758f690400SShri Abhyankar     }
207629b92fc1SShri Abhyankar     idc = 5*c[i];
20778f690400SShri Abhyankar     x[idc]   = t[idt]   = v[0]*s1+v[5]*s2+v[10]*s3+
20788f690400SShri Abhyankar                                  v[15]*s4+v[20]*s5;
20798f690400SShri Abhyankar     x[1+idc] = t[1+idt] = v[1]*s1+v[6]*s2+v[11]*s3+
20808f690400SShri Abhyankar                                  v[16]*s4+v[21]*s5;
20818f690400SShri Abhyankar     x[2+idc] = t[2+idt] = v[2]*s1+v[7]*s2+v[12]*s3+
20828f690400SShri Abhyankar                                  v[17]*s4+v[22]*s5;
20838f690400SShri Abhyankar     x[3+idc] = t[3+idt] = v[3]*s1+v[8]*s2+v[13]*s3+
20848f690400SShri Abhyankar                                  v[18]*s4+v[23]*s5;
20858f690400SShri Abhyankar     x[4+idc] = t[4+idt] = v[4]*s1+v[9]*s2+v[14]*s3+
20868f690400SShri Abhyankar                                  v[19]*s4+v[24]*s5;
20878f690400SShri Abhyankar   }
20888f690400SShri Abhyankar 
20898f690400SShri Abhyankar   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
20908f690400SShri Abhyankar   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
20918f690400SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
20928f690400SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
20938f690400SShri Abhyankar   ierr = PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n);CHKERRQ(ierr);
20948f690400SShri Abhyankar   PetscFunctionReturn(0);
20958f690400SShri Abhyankar }
20968f690400SShri Abhyankar #undef __FUNCT__
20974a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_5_NaturalOrdering"
2098dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_5_NaturalOrdering(Mat A,Vec bb,Vec xx)
209915091d37SBarry Smith {
210015091d37SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
2101690b6cddSBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
2102dfbe8321SBarry Smith   PetscErrorCode    ierr;
2103690b6cddSBarry Smith   PetscInt          *diag = a->diag,jdx;
2104d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
2105d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,s4,s5,x1,x2,x3,x4,x5;
2106d9fead3dSBarry Smith   const PetscScalar *b;
210715091d37SBarry Smith 
210815091d37SBarry Smith   PetscFunctionBegin;
2109d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
21101ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
211115091d37SBarry Smith   /* forward solve the lower triangular */
211215091d37SBarry Smith   idx    = 0;
211315091d37SBarry 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];
211415091d37SBarry Smith   for (i=1; i<n; i++) {
211515091d37SBarry Smith     v     =  aa + 25*ai[i];
211615091d37SBarry Smith     vi    =  aj + ai[i];
211715091d37SBarry Smith     nz    =  diag[i] - ai[i];
211815091d37SBarry Smith     idx   =  5*i;
2119f1af5d2fSBarry Smith     s1  =  b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];s5 = b[4+idx];
212015091d37SBarry Smith     while (nz--) {
212115091d37SBarry Smith       jdx   = 5*(*vi++);
212215091d37SBarry Smith       x1    = x[jdx];x2 = x[1+jdx];x3 = x[2+jdx];x4 = x[3+jdx];x5 = x[4+jdx];
2123f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
2124f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
2125f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
2126f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
2127f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
212815091d37SBarry Smith       v    += 25;
212915091d37SBarry Smith     }
2130f1af5d2fSBarry Smith     x[idx]   = s1;
2131f1af5d2fSBarry Smith     x[1+idx] = s2;
2132f1af5d2fSBarry Smith     x[2+idx] = s3;
2133f1af5d2fSBarry Smith     x[3+idx] = s4;
2134f1af5d2fSBarry Smith     x[4+idx] = s5;
213515091d37SBarry Smith   }
213615091d37SBarry Smith   /* backward solve the upper triangular */
213715091d37SBarry Smith   for (i=n-1; i>=0; i--){
213815091d37SBarry Smith     v    = aa + 25*diag[i] + 25;
213915091d37SBarry Smith     vi   = aj + diag[i] + 1;
214015091d37SBarry Smith     nz   = ai[i+1] - diag[i] - 1;
214115091d37SBarry Smith     idt  = 5*i;
2142f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt];
2143f1af5d2fSBarry Smith     s3 = x[2+idt];s4 = x[3+idt]; s5 = x[4+idt];
214415091d37SBarry Smith     while (nz--) {
214515091d37SBarry Smith       idx   = 5*(*vi++);
214615091d37SBarry Smith       x1    = x[idx];   x2 = x[1+idx];x3    = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
2147f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
2148f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
2149f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
2150f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
2151f1af5d2fSBarry Smith       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
215215091d37SBarry Smith       v    += 25;
215315091d37SBarry Smith     }
215415091d37SBarry Smith     v        = aa + 25*diag[i];
2155f1af5d2fSBarry Smith     x[idt]   = v[0]*s1 + v[5]*s2 + v[10]*s3  + v[15]*s4 + v[20]*s5;
2156f1af5d2fSBarry Smith     x[1+idt] = v[1]*s1 + v[6]*s2 + v[11]*s3  + v[16]*s4 + v[21]*s5;
2157f1af5d2fSBarry Smith     x[2+idt] = v[2]*s1 + v[7]*s2 + v[12]*s3  + v[17]*s4 + v[22]*s5;
2158f1af5d2fSBarry Smith     x[3+idt] = v[3]*s1 + v[8]*s2 + v[13]*s3  + v[18]*s4 + v[23]*s5;
2159f1af5d2fSBarry Smith     x[4+idt] = v[4]*s1 + v[9]*s2 + v[14]*s3  + v[19]*s4 + v[24]*s5;
216015091d37SBarry Smith   }
216115091d37SBarry Smith 
2162d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
21631ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2164dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n);CHKERRQ(ierr);
216515091d37SBarry Smith   PetscFunctionReturn(0);
216615091d37SBarry Smith }
216715091d37SBarry Smith 
21684a2ae208SSatish Balay #undef __FUNCT__
2169cee9d6f2SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_5_NaturalOrdering_newdatastruct"
2170cee9d6f2SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_5_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx)
2171cee9d6f2SShri Abhyankar {
2172cee9d6f2SShri Abhyankar   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
21736464896eSShri Abhyankar   PetscInt          i,k,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
2174cee9d6f2SShri Abhyankar   PetscErrorCode    ierr;
2175cee9d6f2SShri Abhyankar   PetscInt          jdx;
2176cee9d6f2SShri Abhyankar   const MatScalar   *aa=a->a,*v;
2177cee9d6f2SShri Abhyankar   PetscScalar       *x,s1,s2,s3,s4,s5,x1,x2,x3,x4,x5;
2178cee9d6f2SShri Abhyankar   const PetscScalar *b;
2179cee9d6f2SShri Abhyankar 
2180cee9d6f2SShri Abhyankar   PetscFunctionBegin;
2181cee9d6f2SShri Abhyankar   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
2182cee9d6f2SShri Abhyankar   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
2183cee9d6f2SShri Abhyankar   /* forward solve the lower triangular */
2184cee9d6f2SShri Abhyankar   idx    = 0;
2185cee9d6f2SShri Abhyankar   x[0] = b[idx]; x[1] = b[1+idx]; x[2] = b[2+idx]; x[3] = b[3+idx];x[4] = b[4+idx];
2186cee9d6f2SShri Abhyankar   for (i=1; i<n; i++) {
2187cee9d6f2SShri Abhyankar     v   = aa + 25*ai[i];
2188cee9d6f2SShri Abhyankar     vi  = aj + ai[i];
2189cee9d6f2SShri Abhyankar     nz  = ai[i+1] - ai[i];
2190cee9d6f2SShri Abhyankar     idx = 5*i;
2191cee9d6f2SShri Abhyankar     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];s5 = b[4+idx];
21926464896eSShri Abhyankar     for(k=0;k<nz;k++) {
21936464896eSShri Abhyankar       jdx   = 5*vi[k];
2194cee9d6f2SShri Abhyankar       x1    = x[jdx];x2 = x[1+jdx];x3 = x[2+jdx];x4 = x[3+jdx];x5 = x[4+jdx];
2195cee9d6f2SShri Abhyankar       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
2196cee9d6f2SShri Abhyankar       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
2197cee9d6f2SShri Abhyankar       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
2198cee9d6f2SShri Abhyankar       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
2199cee9d6f2SShri Abhyankar       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
2200cee9d6f2SShri Abhyankar       v    += 25;
2201cee9d6f2SShri Abhyankar     }
2202cee9d6f2SShri Abhyankar     x[idx]   = s1;
2203cee9d6f2SShri Abhyankar     x[1+idx] = s2;
2204cee9d6f2SShri Abhyankar     x[2+idx] = s3;
2205cee9d6f2SShri Abhyankar     x[3+idx] = s4;
2206cee9d6f2SShri Abhyankar     x[4+idx] = s5;
2207cee9d6f2SShri Abhyankar   }
2208cee9d6f2SShri Abhyankar 
2209cee9d6f2SShri Abhyankar   /* backward solve the upper triangular */
2210cee9d6f2SShri Abhyankar   for (i=n-1; i>=0; i--){
2211cee9d6f2SShri Abhyankar     v   = aa + 25*ai[2*n-i];
2212cee9d6f2SShri Abhyankar     vi  = aj + ai[2*n-i];
2213cee9d6f2SShri Abhyankar     nz  = ai[2*n-i +1] - ai[2*n-i]-1;
2214cee9d6f2SShri Abhyankar     idt = 5*i;
2215cee9d6f2SShri Abhyankar     s1 = x[idt];  s2 = x[1+idt];
2216cee9d6f2SShri Abhyankar     s3 = x[2+idt];s4 = x[3+idt]; s5 = x[4+idt];
22176464896eSShri Abhyankar     for(k=0;k<nz;k++){
22186464896eSShri Abhyankar       idx   = 5*vi[k];
2219cee9d6f2SShri Abhyankar       x1    = x[idx];   x2 = x[1+idx];x3    = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
2220cee9d6f2SShri Abhyankar       s1 -= v[0]*x1 + v[5]*x2 + v[10]*x3  + v[15]*x4 + v[20]*x5;
2221cee9d6f2SShri Abhyankar       s2 -= v[1]*x1 + v[6]*x2 + v[11]*x3  + v[16]*x4 + v[21]*x5;
2222cee9d6f2SShri Abhyankar       s3 -= v[2]*x1 + v[7]*x2 + v[12]*x3  + v[17]*x4 + v[22]*x5;
2223cee9d6f2SShri Abhyankar       s4 -= v[3]*x1 + v[8]*x2 + v[13]*x3  + v[18]*x4 + v[23]*x5;
2224cee9d6f2SShri Abhyankar       s5 -= v[4]*x1 + v[9]*x2 + v[14]*x3  + v[19]*x4 + v[24]*x5;
2225cee9d6f2SShri Abhyankar       v    += 25;
2226cee9d6f2SShri Abhyankar     }
2227cee9d6f2SShri Abhyankar     /* x = inv_diagonal*x */
2228cee9d6f2SShri Abhyankar     x[idt]   = v[0]*s1 + v[5]*s2 + v[10]*s3  + v[15]*s4 + v[20]*s5;
2229cee9d6f2SShri Abhyankar     x[1+idt] = v[1]*s1 + v[6]*s2 + v[11]*s3  + v[16]*s4 + v[21]*s5;
2230cee9d6f2SShri Abhyankar     x[2+idt] = v[2]*s1 + v[7]*s2 + v[12]*s3  + v[17]*s4 + v[22]*s5;
2231cee9d6f2SShri Abhyankar     x[3+idt] = v[3]*s1 + v[8]*s2 + v[13]*s3  + v[18]*s4 + v[23]*s5;
2232cee9d6f2SShri Abhyankar     x[4+idt] = v[4]*s1 + v[9]*s2 + v[14]*s3  + v[19]*s4 + v[24]*s5;
2233cee9d6f2SShri Abhyankar   }
2234cee9d6f2SShri Abhyankar 
2235cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
2236cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2237cee9d6f2SShri Abhyankar   ierr = PetscLogFlops(2.0*25*(a->nz) - 5.0*A->cmap->n);CHKERRQ(ierr);
2238cee9d6f2SShri Abhyankar   PetscFunctionReturn(0);
2239cee9d6f2SShri Abhyankar }
2240cee9d6f2SShri Abhyankar 
2241cee9d6f2SShri Abhyankar #undef __FUNCT__
22424a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_4"
2243dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_4(Mat A,Vec bb,Vec xx)
22444e2b4712SSatish Balay {
22454e2b4712SSatish Balay   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
22464e2b4712SSatish Balay   IS                iscol=a->col,isrow=a->row;
22476849ba73SBarry Smith   PetscErrorCode    ierr;
22485d0c19d7SBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc;
22495d0c19d7SBarry Smith   const PetscInt    *r,*c,*diag = a->diag,*rout,*cout;
2250d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
2251d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,s4,x1,x2,x3,x4,*t;
2252d9fead3dSBarry Smith   const PetscScalar *b;
22534e2b4712SSatish Balay 
22544e2b4712SSatish Balay   PetscFunctionBegin;
2255d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
22561ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
2257f1af5d2fSBarry Smith   t  = a->solve_work;
22584e2b4712SSatish Balay 
22594e2b4712SSatish Balay   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
22604e2b4712SSatish Balay   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
22614e2b4712SSatish Balay 
22624e2b4712SSatish Balay   /* forward solve the lower triangular */
22634e2b4712SSatish Balay   idx    = 4*(*r++);
2264f1af5d2fSBarry Smith   t[0] = b[idx];   t[1] = b[1+idx];
2265f1af5d2fSBarry Smith   t[2] = b[2+idx]; t[3] = b[3+idx];
22664e2b4712SSatish Balay   for (i=1; i<n; i++) {
22674e2b4712SSatish Balay     v     = aa + 16*ai[i];
22684e2b4712SSatish Balay     vi    = aj + ai[i];
22694e2b4712SSatish Balay     nz    = diag[i] - ai[i];
22704e2b4712SSatish Balay     idx   = 4*(*r++);
2271f1af5d2fSBarry Smith     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
22724e2b4712SSatish Balay     while (nz--) {
22734e2b4712SSatish Balay       idx   = 4*(*vi++);
2274f1af5d2fSBarry Smith       x1    = t[idx];x2 = t[1+idx];x3 = t[2+idx];x4 = t[3+idx];
2275f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
2276f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
2277f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
2278f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
22794e2b4712SSatish Balay       v    += 16;
22804e2b4712SSatish Balay     }
22814e2b4712SSatish Balay     idx        = 4*i;
2282f1af5d2fSBarry Smith     t[idx]   = s1;t[1+idx] = s2;
2283f1af5d2fSBarry Smith     t[2+idx] = s3;t[3+idx] = s4;
22844e2b4712SSatish Balay   }
22854e2b4712SSatish Balay   /* backward solve the upper triangular */
22864e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
22874e2b4712SSatish Balay     v    = aa + 16*diag[i] + 16;
22884e2b4712SSatish Balay     vi   = aj + diag[i] + 1;
22894e2b4712SSatish Balay     nz   = ai[i+1] - diag[i] - 1;
22904e2b4712SSatish Balay     idt  = 4*i;
2291f1af5d2fSBarry Smith     s1 = t[idt];  s2 = t[1+idt];
2292f1af5d2fSBarry Smith     s3 = t[2+idt];s4 = t[3+idt];
22934e2b4712SSatish Balay     while (nz--) {
22944e2b4712SSatish Balay       idx   = 4*(*vi++);
2295f1af5d2fSBarry Smith       x1    = t[idx];   x2 = t[1+idx];
2296f1af5d2fSBarry Smith       x3    = t[2+idx]; x4 = t[3+idx];
2297f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3   + v[12]*x4;
2298f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3   + v[13]*x4;
2299f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3  + v[14]*x4;
2300f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3  + v[15]*x4;
23014e2b4712SSatish Balay       v += 16;
23024e2b4712SSatish Balay     }
23034e2b4712SSatish Balay     idc      = 4*(*c--);
23044e2b4712SSatish Balay     v        = aa + 16*diag[i];
2305f1af5d2fSBarry Smith     x[idc]   = t[idt]   = v[0]*s1+v[4]*s2+v[8]*s3+v[12]*s4;
2306f1af5d2fSBarry Smith     x[1+idc] = t[1+idt] = v[1]*s1+v[5]*s2+v[9]*s3+v[13]*s4;
2307f1af5d2fSBarry Smith     x[2+idc] = t[2+idt] = v[2]*s1+v[6]*s2+v[10]*s3+v[14]*s4;
2308f1af5d2fSBarry Smith     x[3+idc] = t[3+idt] = v[3]*s1+v[7]*s2+v[11]*s3+v[15]*s4;
23094e2b4712SSatish Balay   }
23104e2b4712SSatish Balay 
23114e2b4712SSatish Balay   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
23124e2b4712SSatish Balay   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
2313d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
23141ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2315dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
23164e2b4712SSatish Balay   PetscFunctionReturn(0);
23174e2b4712SSatish Balay }
2318f26ec98cSKris Buschelman 
2319f26ec98cSKris Buschelman #undef __FUNCT__
23208f690400SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_4_newdatastruct"
23218f690400SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_4_newdatastruct(Mat A,Vec bb,Vec xx)
23228f690400SShri Abhyankar {
23238f690400SShri Abhyankar   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
23248f690400SShri Abhyankar   IS                iscol=a->col,isrow=a->row;
23258f690400SShri Abhyankar   PetscErrorCode    ierr;
232629b92fc1SShri Abhyankar   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc,k,m;
23278f690400SShri Abhyankar   const PetscInt    *r,*c,*rout,*cout;
23288f690400SShri Abhyankar   const MatScalar   *aa=a->a,*v;
23298f690400SShri Abhyankar   PetscScalar       *x,s1,s2,s3,s4,x1,x2,x3,x4,*t;
23308f690400SShri Abhyankar   const PetscScalar *b;
23318f690400SShri Abhyankar 
23328f690400SShri Abhyankar   PetscFunctionBegin;
23338f690400SShri Abhyankar   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
23348f690400SShri Abhyankar   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
23358f690400SShri Abhyankar   t  = a->solve_work;
23368f690400SShri Abhyankar 
23378f690400SShri Abhyankar   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
233829b92fc1SShri Abhyankar   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
23398f690400SShri Abhyankar 
23408f690400SShri Abhyankar   /* forward solve the lower triangular */
234129b92fc1SShri Abhyankar   idx    = 4*r[0];
23428f690400SShri Abhyankar   t[0] = b[idx];   t[1] = b[1+idx];
23438f690400SShri Abhyankar   t[2] = b[2+idx]; t[3] = b[3+idx];
23448f690400SShri Abhyankar   for (i=1; i<n; i++) {
23458f690400SShri Abhyankar     v     = aa + 16*ai[i];
23468f690400SShri Abhyankar     vi    = aj + ai[i];
23478f690400SShri Abhyankar     nz    = ai[i+1] - ai[i];
234829b92fc1SShri Abhyankar     idx   = 4*r[i];
23498f690400SShri Abhyankar     s1  = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
235029b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
235129b92fc1SShri Abhyankar       idx   = 4*vi[m];
23528f690400SShri Abhyankar       x1    = t[idx];x2 = t[1+idx];x3 = t[2+idx];x4 = t[3+idx];
23538f690400SShri Abhyankar       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
23548f690400SShri Abhyankar       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
23558f690400SShri Abhyankar       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
23568f690400SShri Abhyankar       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
23578f690400SShri Abhyankar       v    += 16;
23588f690400SShri Abhyankar     }
23598f690400SShri Abhyankar     idx        = 4*i;
23608f690400SShri Abhyankar     t[idx]   = s1;t[1+idx] = s2;
23618f690400SShri Abhyankar     t[2+idx] = s3;t[3+idx] = s4;
23628f690400SShri Abhyankar   }
23638f690400SShri Abhyankar   /* backward solve the upper triangular */
23648f690400SShri Abhyankar   for (i=n-1; i>=0; i--){
23658f690400SShri Abhyankar     k    = 2*n-i;
23668f690400SShri Abhyankar     v    = aa + 16*ai[k];
23678f690400SShri Abhyankar     vi   = aj + ai[k];
23688f690400SShri Abhyankar     nz   = ai[k+1] - ai[k] - 1;
23698f690400SShri Abhyankar     idt  = 4*i;
23708f690400SShri Abhyankar     s1 = t[idt];  s2 = t[1+idt];
23718f690400SShri Abhyankar     s3 = t[2+idt];s4 = t[3+idt];
237229b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
237329b92fc1SShri Abhyankar       idx   = 4*vi[m];
23748f690400SShri Abhyankar       x1    = t[idx];   x2 = t[1+idx];
23758f690400SShri Abhyankar       x3    = t[2+idx]; x4 = t[3+idx];
23768f690400SShri Abhyankar       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3   + v[12]*x4;
23778f690400SShri Abhyankar       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3   + v[13]*x4;
23788f690400SShri Abhyankar       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3  + v[14]*x4;
23798f690400SShri Abhyankar       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3  + v[15]*x4;
23808f690400SShri Abhyankar       v += 16;
23818f690400SShri Abhyankar     }
238229b92fc1SShri Abhyankar     idc      = 4*c[i];
23838f690400SShri Abhyankar     x[idc]   = t[idt]   = v[0]*s1+v[4]*s2+v[8]*s3+v[12]*s4;
23848f690400SShri Abhyankar     x[1+idc] = t[1+idt] = v[1]*s1+v[5]*s2+v[9]*s3+v[13]*s4;
23858f690400SShri Abhyankar     x[2+idc] = t[2+idt] = v[2]*s1+v[6]*s2+v[10]*s3+v[14]*s4;
23868f690400SShri Abhyankar     x[3+idc] = t[3+idt] = v[3]*s1+v[7]*s2+v[11]*s3+v[15]*s4;
23878f690400SShri Abhyankar   }
23888f690400SShri Abhyankar 
23898f690400SShri Abhyankar   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
23908f690400SShri Abhyankar   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
23918f690400SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
23928f690400SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
23938f690400SShri Abhyankar   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
23948f690400SShri Abhyankar   PetscFunctionReturn(0);
23958f690400SShri Abhyankar }
23968f690400SShri Abhyankar 
23978f690400SShri Abhyankar #undef __FUNCT__
2398f26ec98cSKris Buschelman #define __FUNCT__ "MatSolve_SeqBAIJ_4_Demotion"
2399dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_4_Demotion(Mat A,Vec bb,Vec xx)
2400f26ec98cSKris Buschelman {
2401f26ec98cSKris Buschelman   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
2402f26ec98cSKris Buschelman   IS                iscol=a->col,isrow=a->row;
24036849ba73SBarry Smith   PetscErrorCode    ierr;
24045d0c19d7SBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc;
24055d0c19d7SBarry Smith   const PetscInt    *r,*c,*diag = a->diag,*rout,*cout;
2406d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
2407d9fead3dSBarry Smith   MatScalar         s1,s2,s3,s4,x1,x2,x3,x4,*t;
2408d9fead3dSBarry Smith   PetscScalar       *x;
2409d9fead3dSBarry Smith   const PetscScalar *b;
2410f26ec98cSKris Buschelman 
2411f26ec98cSKris Buschelman   PetscFunctionBegin;
2412d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
24131ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
2414f26ec98cSKris Buschelman   t  = (MatScalar *)a->solve_work;
2415f26ec98cSKris Buschelman 
2416f26ec98cSKris Buschelman   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
2417f26ec98cSKris Buschelman   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
2418f26ec98cSKris Buschelman 
2419f26ec98cSKris Buschelman   /* forward solve the lower triangular */
2420f26ec98cSKris Buschelman   idx    = 4*(*r++);
2421f26ec98cSKris Buschelman   t[0] = (MatScalar)b[idx];
2422f26ec98cSKris Buschelman   t[1] = (MatScalar)b[1+idx];
2423f26ec98cSKris Buschelman   t[2] = (MatScalar)b[2+idx];
2424f26ec98cSKris Buschelman   t[3] = (MatScalar)b[3+idx];
2425f26ec98cSKris Buschelman   for (i=1; i<n; i++) {
2426f26ec98cSKris Buschelman     v     = aa + 16*ai[i];
2427f26ec98cSKris Buschelman     vi    = aj + ai[i];
2428f26ec98cSKris Buschelman     nz    = diag[i] - ai[i];
2429f26ec98cSKris Buschelman     idx   = 4*(*r++);
2430f26ec98cSKris Buschelman     s1 = (MatScalar)b[idx];
2431f26ec98cSKris Buschelman     s2 = (MatScalar)b[1+idx];
2432f26ec98cSKris Buschelman     s3 = (MatScalar)b[2+idx];
2433f26ec98cSKris Buschelman     s4 = (MatScalar)b[3+idx];
2434f26ec98cSKris Buschelman     while (nz--) {
2435f26ec98cSKris Buschelman       idx   = 4*(*vi++);
2436f26ec98cSKris Buschelman       x1  = t[idx];
2437f26ec98cSKris Buschelman       x2  = t[1+idx];
2438f26ec98cSKris Buschelman       x3  = t[2+idx];
2439f26ec98cSKris Buschelman       x4  = t[3+idx];
2440f26ec98cSKris Buschelman       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
2441f26ec98cSKris Buschelman       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
2442f26ec98cSKris Buschelman       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
2443f26ec98cSKris Buschelman       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
2444f26ec98cSKris Buschelman       v    += 16;
2445f26ec98cSKris Buschelman     }
2446f26ec98cSKris Buschelman     idx        = 4*i;
2447f26ec98cSKris Buschelman     t[idx]   = s1;
2448f26ec98cSKris Buschelman     t[1+idx] = s2;
2449f26ec98cSKris Buschelman     t[2+idx] = s3;
2450f26ec98cSKris Buschelman     t[3+idx] = s4;
2451f26ec98cSKris Buschelman   }
2452f26ec98cSKris Buschelman   /* backward solve the upper triangular */
2453f26ec98cSKris Buschelman   for (i=n-1; i>=0; i--){
2454f26ec98cSKris Buschelman     v    = aa + 16*diag[i] + 16;
2455f26ec98cSKris Buschelman     vi   = aj + diag[i] + 1;
2456f26ec98cSKris Buschelman     nz   = ai[i+1] - diag[i] - 1;
2457f26ec98cSKris Buschelman     idt  = 4*i;
2458f26ec98cSKris Buschelman     s1 = t[idt];
2459f26ec98cSKris Buschelman     s2 = t[1+idt];
2460f26ec98cSKris Buschelman     s3 = t[2+idt];
2461f26ec98cSKris Buschelman     s4 = t[3+idt];
2462f26ec98cSKris Buschelman     while (nz--) {
2463f26ec98cSKris Buschelman       idx   = 4*(*vi++);
2464f26ec98cSKris Buschelman       x1  = t[idx];
2465f26ec98cSKris Buschelman       x2  = t[1+idx];
2466f26ec98cSKris Buschelman       x3  = t[2+idx];
2467f26ec98cSKris Buschelman       x4  = t[3+idx];
2468f26ec98cSKris Buschelman       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3   + v[12]*x4;
2469f26ec98cSKris Buschelman       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3   + v[13]*x4;
2470f26ec98cSKris Buschelman       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3  + v[14]*x4;
2471f26ec98cSKris Buschelman       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3  + v[15]*x4;
2472f26ec98cSKris Buschelman       v += 16;
2473f26ec98cSKris Buschelman     }
2474f26ec98cSKris Buschelman     idc      = 4*(*c--);
2475f26ec98cSKris Buschelman     v        = aa + 16*diag[i];
2476f26ec98cSKris Buschelman     t[idt]   = v[0]*s1+v[4]*s2+v[8]*s3+v[12]*s4;
2477f26ec98cSKris Buschelman     t[1+idt] = v[1]*s1+v[5]*s2+v[9]*s3+v[13]*s4;
2478f26ec98cSKris Buschelman     t[2+idt] = v[2]*s1+v[6]*s2+v[10]*s3+v[14]*s4;
2479f26ec98cSKris Buschelman     t[3+idt] = v[3]*s1+v[7]*s2+v[11]*s3+v[15]*s4;
2480f26ec98cSKris Buschelman     x[idc]   = (PetscScalar)t[idt];
2481f26ec98cSKris Buschelman     x[1+idc] = (PetscScalar)t[1+idt];
2482f26ec98cSKris Buschelman     x[2+idc] = (PetscScalar)t[2+idt];
2483f26ec98cSKris Buschelman     x[3+idc] = (PetscScalar)t[3+idt];
2484f26ec98cSKris Buschelman  }
2485f26ec98cSKris Buschelman 
2486f26ec98cSKris Buschelman   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
2487f26ec98cSKris Buschelman   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
2488d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
24891ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2490dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
2491f26ec98cSKris Buschelman   PetscFunctionReturn(0);
2492f26ec98cSKris Buschelman }
2493f26ec98cSKris Buschelman 
249424c233c2SKris Buschelman #if defined (PETSC_HAVE_SSE)
249524c233c2SKris Buschelman 
249624c233c2SKris Buschelman #include PETSC_HAVE_SSE
249724c233c2SKris Buschelman 
249824c233c2SKris Buschelman #undef __FUNCT__
249924c233c2SKris Buschelman #define __FUNCT__ "MatSolve_SeqBAIJ_4_SSE_Demotion"
2500dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_4_SSE_Demotion(Mat A,Vec bb,Vec xx)
250124c233c2SKris Buschelman {
250224c233c2SKris Buschelman   /*
250324c233c2SKris Buschelman      Note: This code uses demotion of double
250424c233c2SKris Buschelman      to float when performing the mixed-mode computation.
250524c233c2SKris Buschelman      This may not be numerically reasonable for all applications.
250624c233c2SKris Buschelman   */
250724c233c2SKris Buschelman   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data;
250824c233c2SKris Buschelman   IS             iscol=a->col,isrow=a->row;
25096849ba73SBarry Smith   PetscErrorCode ierr;
25105d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc,ai16;
25115d0c19d7SBarry Smith   const PetscInt *r,*c,*diag = a->diag,*rout,*cout;
251224c233c2SKris Buschelman   MatScalar      *aa=a->a,*v;
251387828ca2SBarry Smith   PetscScalar    *x,*b,*t;
251424c233c2SKris Buschelman 
251524c233c2SKris Buschelman   /* Make space in temp stack for 16 Byte Aligned arrays */
251624c233c2SKris Buschelman   float           ssealignedspace[11],*tmps,*tmpx;
251724c233c2SKris Buschelman   unsigned long   offset;
251824c233c2SKris Buschelman 
251924c233c2SKris Buschelman   PetscFunctionBegin;
252024c233c2SKris Buschelman   SSE_SCOPE_BEGIN;
252124c233c2SKris Buschelman 
252224c233c2SKris Buschelman     offset = (unsigned long)ssealignedspace % 16;
252324c233c2SKris Buschelman     if (offset) offset = (16 - offset)/4;
252424c233c2SKris Buschelman     tmps = &ssealignedspace[offset];
252524c233c2SKris Buschelman     tmpx = &ssealignedspace[offset+4];
252624c233c2SKris Buschelman     PREFETCH_NTA(aa+16*ai[1]);
252724c233c2SKris Buschelman 
25281ebc52fbSHong Zhang     ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
25291ebc52fbSHong Zhang     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
253024c233c2SKris Buschelman     t  = a->solve_work;
253124c233c2SKris Buschelman 
253224c233c2SKris Buschelman     ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
253324c233c2SKris Buschelman     ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
253424c233c2SKris Buschelman 
253524c233c2SKris Buschelman     /* forward solve the lower triangular */
253624c233c2SKris Buschelman     idx  = 4*(*r++);
253724c233c2SKris Buschelman     t[0] = b[idx];   t[1] = b[1+idx];
253824c233c2SKris Buschelman     t[2] = b[2+idx]; t[3] = b[3+idx];
253924c233c2SKris Buschelman     v    =  aa + 16*ai[1];
254024c233c2SKris Buschelman 
254124c233c2SKris Buschelman     for (i=1; i<n;) {
254224c233c2SKris Buschelman       PREFETCH_NTA(&v[8]);
254324c233c2SKris Buschelman       vi   =  aj      + ai[i];
254424c233c2SKris Buschelman       nz   =  diag[i] - ai[i];
254524c233c2SKris Buschelman       idx  =  4*(*r++);
254624c233c2SKris Buschelman 
254724c233c2SKris Buschelman       /* Demote sum from double to float */
254824c233c2SKris Buschelman       CONVERT_DOUBLE4_FLOAT4(tmps,&b[idx]);
254924c233c2SKris Buschelman       LOAD_PS(tmps,XMM7);
255024c233c2SKris Buschelman 
255124c233c2SKris Buschelman       while (nz--) {
255224c233c2SKris Buschelman         PREFETCH_NTA(&v[16]);
255324c233c2SKris Buschelman         idx = 4*(*vi++);
255424c233c2SKris Buschelman 
255524c233c2SKris Buschelman         /* Demote solution (so far) from double to float */
255624c233c2SKris Buschelman         CONVERT_DOUBLE4_FLOAT4(tmpx,&x[idx]);
255724c233c2SKris Buschelman 
255824c233c2SKris Buschelman         /* 4x4 Matrix-Vector product with negative accumulation: */
255924c233c2SKris Buschelman         SSE_INLINE_BEGIN_2(tmpx,v)
256024c233c2SKris Buschelman           SSE_LOAD_PS(SSE_ARG_1,FLOAT_0,XMM6)
256124c233c2SKris Buschelman 
256224c233c2SKris Buschelman           /* First Column */
256324c233c2SKris Buschelman           SSE_COPY_PS(XMM0,XMM6)
256424c233c2SKris Buschelman           SSE_SHUFFLE(XMM0,XMM0,0x00)
256524c233c2SKris Buschelman           SSE_MULT_PS_M(XMM0,SSE_ARG_2,FLOAT_0)
256624c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM0)
256724c233c2SKris Buschelman 
256824c233c2SKris Buschelman           /* Second Column */
256924c233c2SKris Buschelman           SSE_COPY_PS(XMM1,XMM6)
257024c233c2SKris Buschelman           SSE_SHUFFLE(XMM1,XMM1,0x55)
257124c233c2SKris Buschelman           SSE_MULT_PS_M(XMM1,SSE_ARG_2,FLOAT_4)
257224c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM1)
257324c233c2SKris Buschelman 
257424c233c2SKris Buschelman           SSE_PREFETCH_NTA(SSE_ARG_2,FLOAT_24)
257524c233c2SKris Buschelman 
257624c233c2SKris Buschelman           /* Third Column */
257724c233c2SKris Buschelman           SSE_COPY_PS(XMM2,XMM6)
257824c233c2SKris Buschelman           SSE_SHUFFLE(XMM2,XMM2,0xAA)
257924c233c2SKris Buschelman           SSE_MULT_PS_M(XMM2,SSE_ARG_2,FLOAT_8)
258024c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM2)
258124c233c2SKris Buschelman 
258224c233c2SKris Buschelman           /* Fourth Column */
258324c233c2SKris Buschelman           SSE_COPY_PS(XMM3,XMM6)
258424c233c2SKris Buschelman           SSE_SHUFFLE(XMM3,XMM3,0xFF)
258524c233c2SKris Buschelman           SSE_MULT_PS_M(XMM3,SSE_ARG_2,FLOAT_12)
258624c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM3)
258724c233c2SKris Buschelman         SSE_INLINE_END_2
258824c233c2SKris Buschelman 
258924c233c2SKris Buschelman         v  += 16;
259024c233c2SKris Buschelman       }
259124c233c2SKris Buschelman       idx = 4*i;
259224c233c2SKris Buschelman       v   = aa + 16*ai[++i];
259324c233c2SKris Buschelman       PREFETCH_NTA(v);
259424c233c2SKris Buschelman       STORE_PS(tmps,XMM7);
259524c233c2SKris Buschelman 
259624c233c2SKris Buschelman       /* Promote result from float to double */
259724c233c2SKris Buschelman       CONVERT_FLOAT4_DOUBLE4(&t[idx],tmps);
259824c233c2SKris Buschelman     }
259924c233c2SKris Buschelman     /* backward solve the upper triangular */
260024c233c2SKris Buschelman     idt  = 4*(n-1);
260124c233c2SKris Buschelman     ai16 = 16*diag[n-1];
260224c233c2SKris Buschelman     v    = aa + ai16 + 16;
260324c233c2SKris Buschelman     for (i=n-1; i>=0;){
260424c233c2SKris Buschelman       PREFETCH_NTA(&v[8]);
260524c233c2SKris Buschelman       vi = aj + diag[i] + 1;
260624c233c2SKris Buschelman       nz = ai[i+1] - diag[i] - 1;
260724c233c2SKris Buschelman 
260824c233c2SKris Buschelman       /* Demote accumulator from double to float */
260924c233c2SKris Buschelman       CONVERT_DOUBLE4_FLOAT4(tmps,&t[idt]);
261024c233c2SKris Buschelman       LOAD_PS(tmps,XMM7);
261124c233c2SKris Buschelman 
261224c233c2SKris Buschelman       while (nz--) {
261324c233c2SKris Buschelman         PREFETCH_NTA(&v[16]);
261424c233c2SKris Buschelman         idx = 4*(*vi++);
261524c233c2SKris Buschelman 
261624c233c2SKris Buschelman         /* Demote solution (so far) from double to float */
261724c233c2SKris Buschelman         CONVERT_DOUBLE4_FLOAT4(tmpx,&t[idx]);
261824c233c2SKris Buschelman 
261924c233c2SKris Buschelman         /* 4x4 Matrix-Vector Product with negative accumulation: */
262024c233c2SKris Buschelman         SSE_INLINE_BEGIN_2(tmpx,v)
262124c233c2SKris Buschelman           SSE_LOAD_PS(SSE_ARG_1,FLOAT_0,XMM6)
262224c233c2SKris Buschelman 
262324c233c2SKris Buschelman           /* First Column */
262424c233c2SKris Buschelman           SSE_COPY_PS(XMM0,XMM6)
262524c233c2SKris Buschelman           SSE_SHUFFLE(XMM0,XMM0,0x00)
262624c233c2SKris Buschelman           SSE_MULT_PS_M(XMM0,SSE_ARG_2,FLOAT_0)
262724c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM0)
262824c233c2SKris Buschelman 
262924c233c2SKris Buschelman           /* Second Column */
263024c233c2SKris Buschelman           SSE_COPY_PS(XMM1,XMM6)
263124c233c2SKris Buschelman           SSE_SHUFFLE(XMM1,XMM1,0x55)
263224c233c2SKris Buschelman           SSE_MULT_PS_M(XMM1,SSE_ARG_2,FLOAT_4)
263324c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM1)
263424c233c2SKris Buschelman 
263524c233c2SKris Buschelman           SSE_PREFETCH_NTA(SSE_ARG_2,FLOAT_24)
263624c233c2SKris Buschelman 
263724c233c2SKris Buschelman           /* Third Column */
263824c233c2SKris Buschelman           SSE_COPY_PS(XMM2,XMM6)
263924c233c2SKris Buschelman           SSE_SHUFFLE(XMM2,XMM2,0xAA)
264024c233c2SKris Buschelman           SSE_MULT_PS_M(XMM2,SSE_ARG_2,FLOAT_8)
264124c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM2)
264224c233c2SKris Buschelman 
264324c233c2SKris Buschelman           /* Fourth Column */
264424c233c2SKris Buschelman           SSE_COPY_PS(XMM3,XMM6)
264524c233c2SKris Buschelman           SSE_SHUFFLE(XMM3,XMM3,0xFF)
264624c233c2SKris Buschelman           SSE_MULT_PS_M(XMM3,SSE_ARG_2,FLOAT_12)
264724c233c2SKris Buschelman           SSE_SUB_PS(XMM7,XMM3)
264824c233c2SKris Buschelman         SSE_INLINE_END_2
264924c233c2SKris Buschelman         v  += 16;
265024c233c2SKris Buschelman       }
265124c233c2SKris Buschelman       v    = aa + ai16;
265224c233c2SKris Buschelman       ai16 = 16*diag[--i];
265324c233c2SKris Buschelman       PREFETCH_NTA(aa+ai16+16);
265424c233c2SKris Buschelman       /*
265524c233c2SKris Buschelman          Scale the result by the diagonal 4x4 block,
265624c233c2SKris Buschelman          which was inverted as part of the factorization
265724c233c2SKris Buschelman       */
265824c233c2SKris Buschelman       SSE_INLINE_BEGIN_3(v,tmps,aa+ai16)
265924c233c2SKris Buschelman         /* First Column */
266024c233c2SKris Buschelman         SSE_COPY_PS(XMM0,XMM7)
266124c233c2SKris Buschelman         SSE_SHUFFLE(XMM0,XMM0,0x00)
266224c233c2SKris Buschelman         SSE_MULT_PS_M(XMM0,SSE_ARG_1,FLOAT_0)
266324c233c2SKris Buschelman 
266424c233c2SKris Buschelman         /* Second Column */
266524c233c2SKris Buschelman         SSE_COPY_PS(XMM1,XMM7)
266624c233c2SKris Buschelman         SSE_SHUFFLE(XMM1,XMM1,0x55)
266724c233c2SKris Buschelman         SSE_MULT_PS_M(XMM1,SSE_ARG_1,FLOAT_4)
266824c233c2SKris Buschelman         SSE_ADD_PS(XMM0,XMM1)
266924c233c2SKris Buschelman 
267024c233c2SKris Buschelman         SSE_PREFETCH_NTA(SSE_ARG_3,FLOAT_24)
267124c233c2SKris Buschelman 
267224c233c2SKris Buschelman         /* Third Column */
267324c233c2SKris Buschelman         SSE_COPY_PS(XMM2,XMM7)
267424c233c2SKris Buschelman         SSE_SHUFFLE(XMM2,XMM2,0xAA)
267524c233c2SKris Buschelman         SSE_MULT_PS_M(XMM2,SSE_ARG_1,FLOAT_8)
267624c233c2SKris Buschelman         SSE_ADD_PS(XMM0,XMM2)
267724c233c2SKris Buschelman 
267824c233c2SKris Buschelman         /* Fourth Column */
267924c233c2SKris Buschelman         SSE_COPY_PS(XMM3,XMM7)
268024c233c2SKris Buschelman         SSE_SHUFFLE(XMM3,XMM3,0xFF)
268124c233c2SKris Buschelman         SSE_MULT_PS_M(XMM3,SSE_ARG_1,FLOAT_12)
268224c233c2SKris Buschelman         SSE_ADD_PS(XMM0,XMM3)
268324c233c2SKris Buschelman 
268424c233c2SKris Buschelman         SSE_STORE_PS(SSE_ARG_2,FLOAT_0,XMM0)
268524c233c2SKris Buschelman       SSE_INLINE_END_3
268624c233c2SKris Buschelman 
268724c233c2SKris Buschelman       /* Promote solution from float to double */
268824c233c2SKris Buschelman       CONVERT_FLOAT4_DOUBLE4(&t[idt],tmps);
268924c233c2SKris Buschelman 
269024c233c2SKris Buschelman       /* Apply reordering to t and stream into x.    */
269124c233c2SKris Buschelman       /* This way, x doesn't pollute the cache.      */
269224c233c2SKris Buschelman       /* Be careful with size: 2 doubles = 4 floats! */
269324c233c2SKris Buschelman       idc  = 4*(*c--);
269424c233c2SKris Buschelman       SSE_INLINE_BEGIN_2((float *)&t[idt],(float *)&x[idc])
269524c233c2SKris Buschelman         /*  x[idc]   = t[idt];   x[1+idc] = t[1+idc]; */
269624c233c2SKris Buschelman         SSE_LOAD_PS(SSE_ARG_1,FLOAT_0,XMM0)
269724c233c2SKris Buschelman         SSE_STREAM_PS(SSE_ARG_2,FLOAT_0,XMM0)
269824c233c2SKris Buschelman         /*  x[idc+2] = t[idt+2]; x[3+idc] = t[3+idc]; */
269924c233c2SKris Buschelman         SSE_LOAD_PS(SSE_ARG_1,FLOAT_4,XMM1)
270024c233c2SKris Buschelman         SSE_STREAM_PS(SSE_ARG_2,FLOAT_4,XMM1)
270124c233c2SKris Buschelman       SSE_INLINE_END_2
270224c233c2SKris Buschelman       v    = aa + ai16 + 16;
270324c233c2SKris Buschelman       idt -= 4;
270424c233c2SKris Buschelman     }
270524c233c2SKris Buschelman 
270624c233c2SKris Buschelman     ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
270724c233c2SKris Buschelman     ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
27081ebc52fbSHong Zhang     ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
27091ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2710dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
271124c233c2SKris Buschelman   SSE_SCOPE_END;
271224c233c2SKris Buschelman   PetscFunctionReturn(0);
271324c233c2SKris Buschelman }
271424c233c2SKris Buschelman 
271524c233c2SKris Buschelman #endif
27160ef38995SBarry Smith 
27170ef38995SBarry Smith 
27184e2b4712SSatish Balay /*
27194e2b4712SSatish Balay       Special case where the matrix was ILU(0) factored in the natural
27204e2b4712SSatish Balay    ordering. This eliminates the need for the column and row permutation.
27214e2b4712SSatish Balay */
27224a2ae208SSatish Balay #undef __FUNCT__
27234a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_4_NaturalOrdering"
2724dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_4_NaturalOrdering(Mat A,Vec bb,Vec xx)
27254e2b4712SSatish Balay {
27264e2b4712SSatish Balay   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
2727356650c2SBarry Smith   PetscInt          n=a->mbs;
2728356650c2SBarry Smith   const PetscInt    *ai=a->i,*aj=a->j;
2729dfbe8321SBarry Smith   PetscErrorCode    ierr;
2730356650c2SBarry Smith   const PetscInt    *diag = a->diag;
2731d9fead3dSBarry Smith   const MatScalar   *aa=a->a;
2732d9fead3dSBarry Smith   PetscScalar       *x;
2733d9fead3dSBarry Smith   const PetscScalar *b;
27344e2b4712SSatish Balay 
27354e2b4712SSatish Balay   PetscFunctionBegin;
2736d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
27371ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
27384e2b4712SSatish Balay 
2739aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJBLAS)
27402853dc0eSBarry Smith   {
274187828ca2SBarry Smith     static PetscScalar w[2000]; /* very BAD need to fix */
27422853dc0eSBarry Smith     fortransolvebaij4blas_(&n,x,ai,aj,diag,aa,b,w);
27432853dc0eSBarry Smith   }
2744aa482453SBarry Smith #elif defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ)
27452853dc0eSBarry Smith   {
274687828ca2SBarry Smith     static PetscScalar w[2000]; /* very BAD need to fix */
27472853dc0eSBarry Smith     fortransolvebaij4_(&n,x,ai,aj,diag,aa,b,w);
27482853dc0eSBarry Smith   }
2749aa482453SBarry Smith #elif defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJUNROLL)
27502853dc0eSBarry Smith   fortransolvebaij4unroll_(&n,x,ai,aj,diag,aa,b);
2751e1293385SBarry Smith #else
275230d4dcafSBarry Smith   {
275387828ca2SBarry Smith     PetscScalar     s1,s2,s3,s4,x1,x2,x3,x4;
2754d9fead3dSBarry Smith     const MatScalar *v;
2755356650c2SBarry Smith     PetscInt        jdx,idt,idx,nz,i,ai16;
2756356650c2SBarry Smith     const PetscInt  *vi;
2757e1293385SBarry Smith 
27584e2b4712SSatish Balay   /* forward solve the lower triangular */
27594e2b4712SSatish Balay   idx    = 0;
2760e1293385SBarry Smith   x[0]   = b[0]; x[1] = b[1]; x[2] = b[2]; x[3] = b[3];
27614e2b4712SSatish Balay   for (i=1; i<n; i++) {
27624e2b4712SSatish Balay     v     =  aa      + 16*ai[i];
27634e2b4712SSatish Balay     vi    =  aj      + ai[i];
27644e2b4712SSatish Balay     nz    =  diag[i] - ai[i];
2765e1293385SBarry Smith     idx   +=  4;
2766f1af5d2fSBarry Smith     s1  =  b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
27674e2b4712SSatish Balay     while (nz--) {
27684e2b4712SSatish Balay       jdx   = 4*(*vi++);
27694e2b4712SSatish Balay       x1    = x[jdx];x2 = x[1+jdx];x3 = x[2+jdx];x4 = x[3+jdx];
2770f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
2771f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
2772f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
2773f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
27744e2b4712SSatish Balay       v    += 16;
27754e2b4712SSatish Balay     }
2776f1af5d2fSBarry Smith     x[idx]   = s1;
2777f1af5d2fSBarry Smith     x[1+idx] = s2;
2778f1af5d2fSBarry Smith     x[2+idx] = s3;
2779f1af5d2fSBarry Smith     x[3+idx] = s4;
27804e2b4712SSatish Balay   }
27814e2b4712SSatish Balay   /* backward solve the upper triangular */
27824e555682SBarry Smith   idt = 4*(n-1);
27834e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
27844e555682SBarry Smith     ai16 = 16*diag[i];
27854e555682SBarry Smith     v    = aa + ai16 + 16;
27864e2b4712SSatish Balay     vi   = aj + diag[i] + 1;
27874e2b4712SSatish Balay     nz   = ai[i+1] - diag[i] - 1;
2788f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt];
2789f1af5d2fSBarry Smith     s3 = x[2+idt];s4 = x[3+idt];
27904e2b4712SSatish Balay     while (nz--) {
27914e2b4712SSatish Balay       idx   = 4*(*vi++);
27924e2b4712SSatish Balay       x1    = x[idx];   x2 = x[1+idx];x3    = x[2+idx]; x4 = x[3+idx];
2793f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3   + v[12]*x4;
2794f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3   + v[13]*x4;
2795f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3  + v[14]*x4;
2796f1af5d2fSBarry Smith       s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3  + v[15]*x4;
27974e2b4712SSatish Balay       v    += 16;
27984e2b4712SSatish Balay     }
27994e555682SBarry Smith     v        = aa + ai16;
2800f1af5d2fSBarry Smith     x[idt]   = v[0]*s1 + v[4]*s2 + v[8]*s3  + v[12]*s4;
2801f1af5d2fSBarry Smith     x[1+idt] = v[1]*s1 + v[5]*s2 + v[9]*s3  + v[13]*s4;
2802f1af5d2fSBarry Smith     x[2+idt] = v[2]*s1 + v[6]*s2 + v[10]*s3 + v[14]*s4;
2803f1af5d2fSBarry Smith     x[3+idt] = v[3]*s1 + v[7]*s2 + v[11]*s3 + v[15]*s4;
2804329f5518SBarry Smith     idt -= 4;
28054e2b4712SSatish Balay   }
280630d4dcafSBarry Smith   }
2807e1293385SBarry Smith #endif
28084e2b4712SSatish Balay 
2809d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
28101ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2811dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
28124e2b4712SSatish Balay   PetscFunctionReturn(0);
28134e2b4712SSatish Balay }
28144e2b4712SSatish Balay 
2815f26ec98cSKris Buschelman #undef __FUNCT__
2816cee9d6f2SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_4_NaturalOrdering_newdatastruct"
2817cee9d6f2SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_4_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx)
2818cee9d6f2SShri Abhyankar {
2819cee9d6f2SShri Abhyankar     Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
28206464896eSShri Abhyankar     PetscInt          i,k,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz;
2821cee9d6f2SShri Abhyankar     PetscErrorCode    ierr;
2822cee9d6f2SShri Abhyankar     PetscInt          idx,jdx,idt;
2823cee9d6f2SShri Abhyankar     PetscInt          bs = A->rmap->bs,bs2 = a->bs2;
2824cee9d6f2SShri Abhyankar     const MatScalar   *aa=a->a,*v;
2825cee9d6f2SShri Abhyankar     PetscScalar       *x;
2826cee9d6f2SShri Abhyankar     const PetscScalar *b;
2827cee9d6f2SShri Abhyankar     PetscScalar        s1,s2,s3,s4,x1,x2,x3,x4;
2828cee9d6f2SShri Abhyankar 
2829cee9d6f2SShri Abhyankar     PetscFunctionBegin;
2830cee9d6f2SShri Abhyankar     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
2831cee9d6f2SShri Abhyankar     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
2832cee9d6f2SShri Abhyankar     /* forward solve the lower triangular */
2833cee9d6f2SShri Abhyankar     idx    = 0;
2834cee9d6f2SShri Abhyankar     x[0] = b[idx]; x[1] = b[1+idx];x[2] = b[2+idx];x[3] = b[3+idx];
2835cee9d6f2SShri Abhyankar     for (i=1; i<n; i++) {
2836cee9d6f2SShri Abhyankar        v    = aa + bs2*ai[i];
2837cee9d6f2SShri Abhyankar        vi   = aj + ai[i];
2838cee9d6f2SShri Abhyankar        nz   = ai[i+1] - ai[i];
2839cee9d6f2SShri Abhyankar       idx   = bs*i;
2840cee9d6f2SShri Abhyankar        s1   = b[idx];s2 = b[1+idx];s3 = b[2+idx];s4 = b[3+idx];
28416464896eSShri Abhyankar       for(k=0;k<nz;k++) {
28426464896eSShri Abhyankar           jdx   = bs*vi[k];
2843cee9d6f2SShri Abhyankar           x1    = x[jdx];x2 = x[1+jdx]; x3 =x[2+jdx];x4 =x[3+jdx];
2844cee9d6f2SShri Abhyankar           s1   -= v[0]*x1 + v[4]*x2 + v[8]*x3 + v[12]*x4;
2845cee9d6f2SShri Abhyankar           s2   -= v[1]*x1 + v[5]*x2 + v[9]*x3 + v[13]*x4;
2846cee9d6f2SShri Abhyankar           s3   -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
2847cee9d6f2SShri Abhyankar 	  s4   -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
2848cee9d6f2SShri Abhyankar 
2849cee9d6f2SShri Abhyankar           v   +=  bs2;
2850cee9d6f2SShri Abhyankar         }
2851cee9d6f2SShri Abhyankar 
2852cee9d6f2SShri Abhyankar        x[idx]   = s1;
2853cee9d6f2SShri Abhyankar        x[1+idx] = s2;
2854cee9d6f2SShri Abhyankar        x[2+idx] = s3;
2855cee9d6f2SShri Abhyankar        x[3+idx] = s4;
2856cee9d6f2SShri Abhyankar     }
2857cee9d6f2SShri Abhyankar 
2858cee9d6f2SShri Abhyankar    /* backward solve the upper triangular */
2859cee9d6f2SShri Abhyankar   for (i=n-1; i>=0; i--){
2860cee9d6f2SShri Abhyankar      v   = aa + bs2*ai[2*n-i];
2861cee9d6f2SShri Abhyankar      vi  = aj + ai[2*n-i];
2862cee9d6f2SShri Abhyankar      nz  = ai[2*n-i +1] - ai[2*n-i]-1;
2863cee9d6f2SShri Abhyankar      idt = bs*i;
2864cee9d6f2SShri Abhyankar      s1 = x[idt];  s2 = x[1+idt];s3 = x[2+idt];s4 = x[3+idt];
2865cee9d6f2SShri Abhyankar 
28666464896eSShri Abhyankar     for(k=0;k<nz;k++){
28676464896eSShri Abhyankar       idx   = bs*vi[k];
2868cee9d6f2SShri Abhyankar        x1    = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];x4 = x[3+idx];
2869cee9d6f2SShri Abhyankar        s1   -= v[0]*x1 + v[4]*x2 + v[8]*x3 + v[12]*x4;
2870cee9d6f2SShri Abhyankar        s2   -= v[1]*x1 + v[5]*x2 + v[9]*x3 + v[13]*x4;
2871cee9d6f2SShri Abhyankar        s3   -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
2872cee9d6f2SShri Abhyankar        s4   -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
2873cee9d6f2SShri Abhyankar 
2874cee9d6f2SShri Abhyankar         v   +=  bs2;
2875cee9d6f2SShri Abhyankar     }
2876cee9d6f2SShri Abhyankar     /* x = inv_diagonal*x */
2877cee9d6f2SShri Abhyankar    x[idt]   = v[0]*s1 + v[4]*s2 + v[8]*s3 + v[12]*s4;
2878cee9d6f2SShri Abhyankar    x[1+idt] = v[1]*s1 + v[5]*s2 + v[9]*s3 + v[13]*s4;;
2879cee9d6f2SShri Abhyankar    x[2+idt] = v[2]*s1 + v[6]*s2 + v[10]*s3 + v[14]*s4;
2880cee9d6f2SShri Abhyankar    x[3+idt] = v[3]*s1 + v[7]*s2 + v[11]*s3 + v[15]*s4;
2881cee9d6f2SShri Abhyankar 
2882cee9d6f2SShri Abhyankar   }
2883cee9d6f2SShri Abhyankar 
2884cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
2885cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2886cee9d6f2SShri Abhyankar   ierr = PetscLogFlops(2.0*bs2*(a->nz) - bs*A->cmap->n);CHKERRQ(ierr);
2887cee9d6f2SShri Abhyankar   PetscFunctionReturn(0);
2888cee9d6f2SShri Abhyankar }
2889cee9d6f2SShri Abhyankar 
2890cee9d6f2SShri Abhyankar 
2891cee9d6f2SShri Abhyankar 
2892cee9d6f2SShri Abhyankar #undef __FUNCT__
2893f26ec98cSKris Buschelman #define __FUNCT__ "MatSolve_SeqBAIJ_4_NaturalOrdering_Demotion"
2894dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_4_NaturalOrdering_Demotion(Mat A,Vec bb,Vec xx)
2895f26ec98cSKris Buschelman {
2896f26ec98cSKris Buschelman   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data;
2897690b6cddSBarry Smith   PetscInt       n=a->mbs,*ai=a->i,*aj=a->j;
2898dfbe8321SBarry Smith   PetscErrorCode ierr;
2899690b6cddSBarry Smith   PetscInt       *diag = a->diag;
2900f26ec98cSKris Buschelman   MatScalar      *aa=a->a;
2901f26ec98cSKris Buschelman   PetscScalar    *x,*b;
2902f26ec98cSKris Buschelman 
2903f26ec98cSKris Buschelman   PetscFunctionBegin;
29041ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
29051ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
2906f26ec98cSKris Buschelman 
2907f26ec98cSKris Buschelman   {
2908f26ec98cSKris Buschelman     MatScalar  s1,s2,s3,s4,x1,x2,x3,x4;
2909f26ec98cSKris Buschelman     MatScalar  *v,*t=(MatScalar *)x;
2910690b6cddSBarry Smith     PetscInt   jdx,idt,idx,nz,*vi,i,ai16;
2911f26ec98cSKris Buschelman 
2912f26ec98cSKris Buschelman     /* forward solve the lower triangular */
2913f26ec98cSKris Buschelman     idx  = 0;
2914f26ec98cSKris Buschelman     t[0] = (MatScalar)b[0];
2915f26ec98cSKris Buschelman     t[1] = (MatScalar)b[1];
2916f26ec98cSKris Buschelman     t[2] = (MatScalar)b[2];
2917f26ec98cSKris Buschelman     t[3] = (MatScalar)b[3];
2918f26ec98cSKris Buschelman     for (i=1; i<n; i++) {
2919f26ec98cSKris Buschelman       v     =  aa      + 16*ai[i];
2920f26ec98cSKris Buschelman       vi    =  aj      + ai[i];
2921f26ec98cSKris Buschelman       nz    =  diag[i] - ai[i];
2922f26ec98cSKris Buschelman       idx   +=  4;
2923f26ec98cSKris Buschelman       s1 = (MatScalar)b[idx];
2924f26ec98cSKris Buschelman       s2 = (MatScalar)b[1+idx];
2925f26ec98cSKris Buschelman       s3 = (MatScalar)b[2+idx];
2926f26ec98cSKris Buschelman       s4 = (MatScalar)b[3+idx];
2927f26ec98cSKris Buschelman       while (nz--) {
2928f26ec98cSKris Buschelman         jdx = 4*(*vi++);
2929f26ec98cSKris Buschelman         x1  = t[jdx];
2930f26ec98cSKris Buschelman         x2  = t[1+jdx];
2931f26ec98cSKris Buschelman         x3  = t[2+jdx];
2932f26ec98cSKris Buschelman         x4  = t[3+jdx];
2933f26ec98cSKris Buschelman         s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
2934f26ec98cSKris Buschelman         s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
2935f26ec98cSKris Buschelman         s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
2936f26ec98cSKris Buschelman         s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
2937f26ec98cSKris Buschelman         v    += 16;
2938f26ec98cSKris Buschelman       }
2939f26ec98cSKris Buschelman       t[idx]   = s1;
2940f26ec98cSKris Buschelman       t[1+idx] = s2;
2941f26ec98cSKris Buschelman       t[2+idx] = s3;
2942f26ec98cSKris Buschelman       t[3+idx] = s4;
2943f26ec98cSKris Buschelman     }
2944f26ec98cSKris Buschelman     /* backward solve the upper triangular */
2945f26ec98cSKris Buschelman     idt = 4*(n-1);
2946f26ec98cSKris Buschelman     for (i=n-1; i>=0; i--){
2947f26ec98cSKris Buschelman       ai16 = 16*diag[i];
2948f26ec98cSKris Buschelman       v    = aa + ai16 + 16;
2949f26ec98cSKris Buschelman       vi   = aj + diag[i] + 1;
2950f26ec98cSKris Buschelman       nz   = ai[i+1] - diag[i] - 1;
2951f26ec98cSKris Buschelman       s1   = t[idt];
2952f26ec98cSKris Buschelman       s2   = t[1+idt];
2953f26ec98cSKris Buschelman       s3   = t[2+idt];
2954f26ec98cSKris Buschelman       s4   = t[3+idt];
2955f26ec98cSKris Buschelman       while (nz--) {
2956f26ec98cSKris Buschelman         idx = 4*(*vi++);
2957f26ec98cSKris Buschelman         x1  = (MatScalar)x[idx];
2958f26ec98cSKris Buschelman         x2  = (MatScalar)x[1+idx];
2959f26ec98cSKris Buschelman         x3  = (MatScalar)x[2+idx];
2960f26ec98cSKris Buschelman         x4  = (MatScalar)x[3+idx];
2961f26ec98cSKris Buschelman         s1 -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
2962f26ec98cSKris Buschelman         s2 -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
2963f26ec98cSKris Buschelman         s3 -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
2964f26ec98cSKris Buschelman         s4 -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
2965f26ec98cSKris Buschelman         v    += 16;
2966f26ec98cSKris Buschelman       }
2967f26ec98cSKris Buschelman       v        = aa + ai16;
2968f26ec98cSKris Buschelman       x[idt]   = (PetscScalar)(v[0]*s1 + v[4]*s2 + v[8]*s3  + v[12]*s4);
2969f26ec98cSKris Buschelman       x[1+idt] = (PetscScalar)(v[1]*s1 + v[5]*s2 + v[9]*s3  + v[13]*s4);
2970f26ec98cSKris Buschelman       x[2+idt] = (PetscScalar)(v[2]*s1 + v[6]*s2 + v[10]*s3 + v[14]*s4);
2971f26ec98cSKris Buschelman       x[3+idt] = (PetscScalar)(v[3]*s1 + v[7]*s2 + v[11]*s3 + v[15]*s4);
2972f26ec98cSKris Buschelman       idt -= 4;
2973f26ec98cSKris Buschelman     }
2974f26ec98cSKris Buschelman   }
2975f26ec98cSKris Buschelman 
29761ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
29771ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2978dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
2979f26ec98cSKris Buschelman   PetscFunctionReturn(0);
2980f26ec98cSKris Buschelman }
2981f26ec98cSKris Buschelman 
29823660e330SKris Buschelman #if defined (PETSC_HAVE_SSE)
29833660e330SKris Buschelman 
29843660e330SKris Buschelman #include PETSC_HAVE_SSE
29853660e330SKris Buschelman #undef __FUNCT__
29867cf1b8d3SKris Buschelman #define __FUNCT__ "MatSolve_SeqBAIJ_4_NaturalOrdering_SSE_Demotion_usj"
2987dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_4_NaturalOrdering_SSE_Demotion_usj(Mat A,Vec bb,Vec xx)
29883660e330SKris Buschelman {
29893660e330SKris Buschelman   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data;
29902aa5897fSKris Buschelman   unsigned short *aj=(unsigned short *)a->j;
2991dfbe8321SBarry Smith   PetscErrorCode ierr;
2992dfbe8321SBarry Smith   int            *ai=a->i,n=a->mbs,*diag = a->diag;
29933660e330SKris Buschelman   MatScalar      *aa=a->a;
299487828ca2SBarry Smith   PetscScalar    *x,*b;
29953660e330SKris Buschelman 
29963660e330SKris Buschelman   PetscFunctionBegin;
29973660e330SKris Buschelman   SSE_SCOPE_BEGIN;
29983660e330SKris Buschelman   /*
29993660e330SKris Buschelman      Note: This code currently uses demotion of double
30003660e330SKris Buschelman      to float when performing the mixed-mode computation.
30013660e330SKris Buschelman      This may not be numerically reasonable for all applications.
30023660e330SKris Buschelman   */
30033660e330SKris Buschelman   PREFETCH_NTA(aa+16*ai[1]);
30043660e330SKris Buschelman 
30051ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
30061ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
30073660e330SKris Buschelman   {
3008eb05f457SKris Buschelman     /* x will first be computed in single precision then promoted inplace to double */
3009eb05f457SKris Buschelman     MatScalar      *v,*t=(MatScalar *)x;
30102aa5897fSKris Buschelman     int            nz,i,idt,ai16;
30112aa5897fSKris Buschelman     unsigned int   jdx,idx;
30122aa5897fSKris Buschelman     unsigned short *vi;
3013eb05f457SKris Buschelman     /* Forward solve the lower triangular factor. */
30143660e330SKris Buschelman 
3015eb05f457SKris Buschelman     /* First block is the identity. */
30163660e330SKris Buschelman     idx  = 0;
3017eb05f457SKris Buschelman     CONVERT_DOUBLE4_FLOAT4(t,b);
30182aa5897fSKris Buschelman     v    =  aa + 16*((unsigned int)ai[1]);
30193660e330SKris Buschelman 
30203660e330SKris Buschelman     for (i=1; i<n;) {
30213660e330SKris Buschelman       PREFETCH_NTA(&v[8]);
30223660e330SKris Buschelman       vi   =  aj      + ai[i];
30233660e330SKris Buschelman       nz   =  diag[i] - ai[i];
30243660e330SKris Buschelman       idx +=  4;
30253660e330SKris Buschelman 
3026eb05f457SKris Buschelman       /* Demote RHS from double to float. */
3027eb05f457SKris Buschelman       CONVERT_DOUBLE4_FLOAT4(&t[idx],&b[idx]);
3028eb05f457SKris Buschelman       LOAD_PS(&t[idx],XMM7);
30293660e330SKris Buschelman 
30303660e330SKris Buschelman       while (nz--) {
30313660e330SKris Buschelman         PREFETCH_NTA(&v[16]);
30322aa5897fSKris Buschelman         jdx = 4*((unsigned int)(*vi++));
30333660e330SKris Buschelman 
30343660e330SKris Buschelman         /* 4x4 Matrix-Vector product with negative accumulation: */
3035eb05f457SKris Buschelman         SSE_INLINE_BEGIN_2(&t[jdx],v)
30363660e330SKris Buschelman           SSE_LOAD_PS(SSE_ARG_1,FLOAT_0,XMM6)
30373660e330SKris Buschelman 
30383660e330SKris Buschelman           /* First Column */
30393660e330SKris Buschelman           SSE_COPY_PS(XMM0,XMM6)
30403660e330SKris Buschelman           SSE_SHUFFLE(XMM0,XMM0,0x00)
30413660e330SKris Buschelman           SSE_MULT_PS_M(XMM0,SSE_ARG_2,FLOAT_0)
30423660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM0)
30433660e330SKris Buschelman 
30443660e330SKris Buschelman           /* Second Column */
30453660e330SKris Buschelman           SSE_COPY_PS(XMM1,XMM6)
30463660e330SKris Buschelman           SSE_SHUFFLE(XMM1,XMM1,0x55)
30473660e330SKris Buschelman           SSE_MULT_PS_M(XMM1,SSE_ARG_2,FLOAT_4)
30483660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM1)
30493660e330SKris Buschelman 
30503660e330SKris Buschelman           SSE_PREFETCH_NTA(SSE_ARG_2,FLOAT_24)
30513660e330SKris Buschelman 
30523660e330SKris Buschelman           /* Third Column */
30533660e330SKris Buschelman           SSE_COPY_PS(XMM2,XMM6)
30543660e330SKris Buschelman           SSE_SHUFFLE(XMM2,XMM2,0xAA)
30553660e330SKris Buschelman           SSE_MULT_PS_M(XMM2,SSE_ARG_2,FLOAT_8)
30563660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM2)
30573660e330SKris Buschelman 
30583660e330SKris Buschelman           /* Fourth Column */
30593660e330SKris Buschelman           SSE_COPY_PS(XMM3,XMM6)
30603660e330SKris Buschelman           SSE_SHUFFLE(XMM3,XMM3,0xFF)
30613660e330SKris Buschelman           SSE_MULT_PS_M(XMM3,SSE_ARG_2,FLOAT_12)
30623660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM3)
30633660e330SKris Buschelman         SSE_INLINE_END_2
30643660e330SKris Buschelman 
30653660e330SKris Buschelman         v  += 16;
30663660e330SKris Buschelman       }
30673660e330SKris Buschelman       v    =  aa + 16*ai[++i];
30683660e330SKris Buschelman       PREFETCH_NTA(v);
3069eb05f457SKris Buschelman       STORE_PS(&t[idx],XMM7);
30703660e330SKris Buschelman     }
3071eb05f457SKris Buschelman 
3072eb05f457SKris Buschelman     /* Backward solve the upper triangular factor.*/
3073eb05f457SKris Buschelman 
30743660e330SKris Buschelman     idt  = 4*(n-1);
30753660e330SKris Buschelman     ai16 = 16*diag[n-1];
30763660e330SKris Buschelman     v    = aa + ai16 + 16;
30773660e330SKris Buschelman     for (i=n-1; i>=0;){
30783660e330SKris Buschelman       PREFETCH_NTA(&v[8]);
30793660e330SKris Buschelman       vi = aj + diag[i] + 1;
30803660e330SKris Buschelman       nz = ai[i+1] - diag[i] - 1;
30813660e330SKris Buschelman 
3082eb05f457SKris Buschelman       LOAD_PS(&t[idt],XMM7);
30833660e330SKris Buschelman 
30843660e330SKris Buschelman       while (nz--) {
30853660e330SKris Buschelman         PREFETCH_NTA(&v[16]);
30862aa5897fSKris Buschelman         idx = 4*((unsigned int)(*vi++));
30873660e330SKris Buschelman 
30883660e330SKris Buschelman         /* 4x4 Matrix-Vector Product with negative accumulation: */
3089eb05f457SKris Buschelman         SSE_INLINE_BEGIN_2(&t[idx],v)
30903660e330SKris Buschelman           SSE_LOAD_PS(SSE_ARG_1,FLOAT_0,XMM6)
30913660e330SKris Buschelman 
30923660e330SKris Buschelman           /* First Column */
30933660e330SKris Buschelman           SSE_COPY_PS(XMM0,XMM6)
30943660e330SKris Buschelman           SSE_SHUFFLE(XMM0,XMM0,0x00)
30953660e330SKris Buschelman           SSE_MULT_PS_M(XMM0,SSE_ARG_2,FLOAT_0)
30963660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM0)
30973660e330SKris Buschelman 
30983660e330SKris Buschelman           /* Second Column */
30993660e330SKris Buschelman           SSE_COPY_PS(XMM1,XMM6)
31003660e330SKris Buschelman           SSE_SHUFFLE(XMM1,XMM1,0x55)
31013660e330SKris Buschelman           SSE_MULT_PS_M(XMM1,SSE_ARG_2,FLOAT_4)
31023660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM1)
31033660e330SKris Buschelman 
31043660e330SKris Buschelman           SSE_PREFETCH_NTA(SSE_ARG_2,FLOAT_24)
31053660e330SKris Buschelman 
31063660e330SKris Buschelman           /* Third Column */
31073660e330SKris Buschelman           SSE_COPY_PS(XMM2,XMM6)
31083660e330SKris Buschelman           SSE_SHUFFLE(XMM2,XMM2,0xAA)
31093660e330SKris Buschelman           SSE_MULT_PS_M(XMM2,SSE_ARG_2,FLOAT_8)
31103660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM2)
31113660e330SKris Buschelman 
31123660e330SKris Buschelman           /* Fourth Column */
31133660e330SKris Buschelman           SSE_COPY_PS(XMM3,XMM6)
31143660e330SKris Buschelman           SSE_SHUFFLE(XMM3,XMM3,0xFF)
31153660e330SKris Buschelman           SSE_MULT_PS_M(XMM3,SSE_ARG_2,FLOAT_12)
31163660e330SKris Buschelman           SSE_SUB_PS(XMM7,XMM3)
31173660e330SKris Buschelman         SSE_INLINE_END_2
31183660e330SKris Buschelman         v  += 16;
31193660e330SKris Buschelman       }
31203660e330SKris Buschelman       v    = aa + ai16;
31213660e330SKris Buschelman       ai16 = 16*diag[--i];
31223660e330SKris Buschelman       PREFETCH_NTA(aa+ai16+16);
31233660e330SKris Buschelman       /*
31243660e330SKris Buschelman          Scale the result by the diagonal 4x4 block,
31253660e330SKris Buschelman          which was inverted as part of the factorization
31263660e330SKris Buschelman       */
3127eb05f457SKris Buschelman       SSE_INLINE_BEGIN_3(v,&t[idt],aa+ai16)
31283660e330SKris Buschelman         /* First Column */
31293660e330SKris Buschelman         SSE_COPY_PS(XMM0,XMM7)
31303660e330SKris Buschelman         SSE_SHUFFLE(XMM0,XMM0,0x00)
31313660e330SKris Buschelman         SSE_MULT_PS_M(XMM0,SSE_ARG_1,FLOAT_0)
31323660e330SKris Buschelman 
31333660e330SKris Buschelman         /* Second Column */
31343660e330SKris Buschelman         SSE_COPY_PS(XMM1,XMM7)
31353660e330SKris Buschelman         SSE_SHUFFLE(XMM1,XMM1,0x55)
31363660e330SKris Buschelman         SSE_MULT_PS_M(XMM1,SSE_ARG_1,FLOAT_4)
31373660e330SKris Buschelman         SSE_ADD_PS(XMM0,XMM1)
31383660e330SKris Buschelman 
31393660e330SKris Buschelman         SSE_PREFETCH_NTA(SSE_ARG_3,FLOAT_24)
31403660e330SKris Buschelman 
31413660e330SKris Buschelman         /* Third Column */
31423660e330SKris Buschelman         SSE_COPY_PS(XMM2,XMM7)
31433660e330SKris Buschelman         SSE_SHUFFLE(XMM2,XMM2,0xAA)
31443660e330SKris Buschelman         SSE_MULT_PS_M(XMM2,SSE_ARG_1,FLOAT_8)
31453660e330SKris Buschelman         SSE_ADD_PS(XMM0,XMM2)
31463660e330SKris Buschelman 
31473660e330SKris Buschelman         /* Fourth Column */
31483660e330SKris Buschelman         SSE_COPY_PS(XMM3,XMM7)
31493660e330SKris Buschelman         SSE_SHUFFLE(XMM3,XMM3,0xFF)
31503660e330SKris Buschelman         SSE_MULT_PS_M(XMM3,SSE_ARG_1,FLOAT_12)
31513660e330SKris Buschelman         SSE_ADD_PS(XMM0,XMM3)
31523660e330SKris Buschelman 
31533660e330SKris Buschelman         SSE_STORE_PS(SSE_ARG_2,FLOAT_0,XMM0)
31543660e330SKris Buschelman       SSE_INLINE_END_3
31553660e330SKris Buschelman 
31563660e330SKris Buschelman       v    = aa + ai16 + 16;
31573660e330SKris Buschelman       idt -= 4;
31583660e330SKris Buschelman     }
3159eb05f457SKris Buschelman 
3160eb05f457SKris Buschelman     /* Convert t from single precision back to double precision (inplace)*/
3161eb05f457SKris Buschelman     idt = 4*(n-1);
3162eb05f457SKris Buschelman     for (i=n-1;i>=0;i--) {
3163eb05f457SKris Buschelman       /*     CONVERT_FLOAT4_DOUBLE4(&x[idt],&t[idt]); */
3164eb05f457SKris Buschelman       /* Unfortunately, CONVERT_ will count from 0 to 3 which doesn't work here. */
3165eb05f457SKris Buschelman       PetscScalar *xtemp=&x[idt];
3166eb05f457SKris Buschelman       MatScalar   *ttemp=&t[idt];
3167eb05f457SKris Buschelman       xtemp[3] = (PetscScalar)ttemp[3];
3168eb05f457SKris Buschelman       xtemp[2] = (PetscScalar)ttemp[2];
3169eb05f457SKris Buschelman       xtemp[1] = (PetscScalar)ttemp[1];
3170eb05f457SKris Buschelman       xtemp[0] = (PetscScalar)ttemp[0];
317154693613SKris Buschelman       idt -= 4;
31723660e330SKris Buschelman     }
3173eb05f457SKris Buschelman 
3174eb05f457SKris Buschelman   } /* End of artificial scope. */
31751ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
31761ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3177dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
31783660e330SKris Buschelman   SSE_SCOPE_END;
31793660e330SKris Buschelman   PetscFunctionReturn(0);
31803660e330SKris Buschelman }
31813660e330SKris Buschelman 
31827cf1b8d3SKris Buschelman #undef __FUNCT__
31837cf1b8d3SKris Buschelman #define __FUNCT__ "MatSolve_SeqBAIJ_4_NaturalOrdering_SSE_Demotion"
3184dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_4_NaturalOrdering_SSE_Demotion(Mat A,Vec bb,Vec xx)
31857cf1b8d3SKris Buschelman {
31867cf1b8d3SKris Buschelman   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data;
31877cf1b8d3SKris Buschelman   int            *aj=a->j;
3188dfbe8321SBarry Smith   PetscErrorCode ierr;
3189dfbe8321SBarry Smith   int            *ai=a->i,n=a->mbs,*diag = a->diag;
31907cf1b8d3SKris Buschelman   MatScalar      *aa=a->a;
31917cf1b8d3SKris Buschelman   PetscScalar    *x,*b;
31927cf1b8d3SKris Buschelman 
31937cf1b8d3SKris Buschelman   PetscFunctionBegin;
31947cf1b8d3SKris Buschelman   SSE_SCOPE_BEGIN;
31957cf1b8d3SKris Buschelman   /*
31967cf1b8d3SKris Buschelman      Note: This code currently uses demotion of double
31977cf1b8d3SKris Buschelman      to float when performing the mixed-mode computation.
31987cf1b8d3SKris Buschelman      This may not be numerically reasonable for all applications.
31997cf1b8d3SKris Buschelman   */
32007cf1b8d3SKris Buschelman   PREFETCH_NTA(aa+16*ai[1]);
32017cf1b8d3SKris Buschelman 
32021ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
32031ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
32047cf1b8d3SKris Buschelman   {
32057cf1b8d3SKris Buschelman     /* x will first be computed in single precision then promoted inplace to double */
32067cf1b8d3SKris Buschelman     MatScalar *v,*t=(MatScalar *)x;
32077cf1b8d3SKris Buschelman     int       nz,i,idt,ai16;
32087cf1b8d3SKris Buschelman     int       jdx,idx;
32097cf1b8d3SKris Buschelman     int       *vi;
32107cf1b8d3SKris Buschelman     /* Forward solve the lower triangular factor. */
32117cf1b8d3SKris Buschelman 
32127cf1b8d3SKris Buschelman     /* First block is the identity. */
32137cf1b8d3SKris Buschelman     idx  = 0;
32147cf1b8d3SKris Buschelman     CONVERT_DOUBLE4_FLOAT4(t,b);
32157cf1b8d3SKris Buschelman     v    =  aa + 16*ai[1];
32167cf1b8d3SKris Buschelman 
32177cf1b8d3SKris Buschelman     for (i=1; i<n;) {
32187cf1b8d3SKris Buschelman       PREFETCH_NTA(&v[8]);
32197cf1b8d3SKris Buschelman       vi   =  aj      + ai[i];
32207cf1b8d3SKris Buschelman       nz   =  diag[i] - ai[i];
32217cf1b8d3SKris Buschelman       idx +=  4;
32227cf1b8d3SKris Buschelman 
32237cf1b8d3SKris Buschelman       /* Demote RHS from double to float. */
32247cf1b8d3SKris Buschelman       CONVERT_DOUBLE4_FLOAT4(&t[idx],&b[idx]);
32257cf1b8d3SKris Buschelman       LOAD_PS(&t[idx],XMM7);
32267cf1b8d3SKris Buschelman 
32277cf1b8d3SKris Buschelman       while (nz--) {
32287cf1b8d3SKris Buschelman         PREFETCH_NTA(&v[16]);
32297cf1b8d3SKris Buschelman         jdx = 4*(*vi++);
32307cf1b8d3SKris Buschelman /*          jdx = *vi++; */
32317cf1b8d3SKris Buschelman 
32327cf1b8d3SKris Buschelman         /* 4x4 Matrix-Vector product with negative accumulation: */
32337cf1b8d3SKris Buschelman         SSE_INLINE_BEGIN_2(&t[jdx],v)
32347cf1b8d3SKris Buschelman           SSE_LOAD_PS(SSE_ARG_1,FLOAT_0,XMM6)
32357cf1b8d3SKris Buschelman 
32367cf1b8d3SKris Buschelman           /* First Column */
32377cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM0,XMM6)
32387cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM0,XMM0,0x00)
32397cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM0,SSE_ARG_2,FLOAT_0)
32407cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM0)
32417cf1b8d3SKris Buschelman 
32427cf1b8d3SKris Buschelman           /* Second Column */
32437cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM1,XMM6)
32447cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM1,XMM1,0x55)
32457cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM1,SSE_ARG_2,FLOAT_4)
32467cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM1)
32477cf1b8d3SKris Buschelman 
32487cf1b8d3SKris Buschelman           SSE_PREFETCH_NTA(SSE_ARG_2,FLOAT_24)
32497cf1b8d3SKris Buschelman 
32507cf1b8d3SKris Buschelman           /* Third Column */
32517cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM2,XMM6)
32527cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM2,XMM2,0xAA)
32537cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM2,SSE_ARG_2,FLOAT_8)
32547cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM2)
32557cf1b8d3SKris Buschelman 
32567cf1b8d3SKris Buschelman           /* Fourth Column */
32577cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM3,XMM6)
32587cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM3,XMM3,0xFF)
32597cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM3,SSE_ARG_2,FLOAT_12)
32607cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM3)
32617cf1b8d3SKris Buschelman         SSE_INLINE_END_2
32627cf1b8d3SKris Buschelman 
32637cf1b8d3SKris Buschelman         v  += 16;
32647cf1b8d3SKris Buschelman       }
32657cf1b8d3SKris Buschelman       v    =  aa + 16*ai[++i];
32667cf1b8d3SKris Buschelman       PREFETCH_NTA(v);
32677cf1b8d3SKris Buschelman       STORE_PS(&t[idx],XMM7);
32687cf1b8d3SKris Buschelman     }
32697cf1b8d3SKris Buschelman 
32707cf1b8d3SKris Buschelman     /* Backward solve the upper triangular factor.*/
32717cf1b8d3SKris Buschelman 
32727cf1b8d3SKris Buschelman     idt  = 4*(n-1);
32737cf1b8d3SKris Buschelman     ai16 = 16*diag[n-1];
32747cf1b8d3SKris Buschelman     v    = aa + ai16 + 16;
32757cf1b8d3SKris Buschelman     for (i=n-1; i>=0;){
32767cf1b8d3SKris Buschelman       PREFETCH_NTA(&v[8]);
32777cf1b8d3SKris Buschelman       vi = aj + diag[i] + 1;
32787cf1b8d3SKris Buschelman       nz = ai[i+1] - diag[i] - 1;
32797cf1b8d3SKris Buschelman 
32807cf1b8d3SKris Buschelman       LOAD_PS(&t[idt],XMM7);
32817cf1b8d3SKris Buschelman 
32827cf1b8d3SKris Buschelman       while (nz--) {
32837cf1b8d3SKris Buschelman         PREFETCH_NTA(&v[16]);
32847cf1b8d3SKris Buschelman         idx = 4*(*vi++);
32857cf1b8d3SKris Buschelman /*          idx = *vi++; */
32867cf1b8d3SKris Buschelman 
32877cf1b8d3SKris Buschelman         /* 4x4 Matrix-Vector Product with negative accumulation: */
32887cf1b8d3SKris Buschelman         SSE_INLINE_BEGIN_2(&t[idx],v)
32897cf1b8d3SKris Buschelman           SSE_LOAD_PS(SSE_ARG_1,FLOAT_0,XMM6)
32907cf1b8d3SKris Buschelman 
32917cf1b8d3SKris Buschelman           /* First Column */
32927cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM0,XMM6)
32937cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM0,XMM0,0x00)
32947cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM0,SSE_ARG_2,FLOAT_0)
32957cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM0)
32967cf1b8d3SKris Buschelman 
32977cf1b8d3SKris Buschelman           /* Second Column */
32987cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM1,XMM6)
32997cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM1,XMM1,0x55)
33007cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM1,SSE_ARG_2,FLOAT_4)
33017cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM1)
33027cf1b8d3SKris Buschelman 
33037cf1b8d3SKris Buschelman           SSE_PREFETCH_NTA(SSE_ARG_2,FLOAT_24)
33047cf1b8d3SKris Buschelman 
33057cf1b8d3SKris Buschelman           /* Third Column */
33067cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM2,XMM6)
33077cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM2,XMM2,0xAA)
33087cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM2,SSE_ARG_2,FLOAT_8)
33097cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM2)
33107cf1b8d3SKris Buschelman 
33117cf1b8d3SKris Buschelman           /* Fourth Column */
33127cf1b8d3SKris Buschelman           SSE_COPY_PS(XMM3,XMM6)
33137cf1b8d3SKris Buschelman           SSE_SHUFFLE(XMM3,XMM3,0xFF)
33147cf1b8d3SKris Buschelman           SSE_MULT_PS_M(XMM3,SSE_ARG_2,FLOAT_12)
33157cf1b8d3SKris Buschelman           SSE_SUB_PS(XMM7,XMM3)
33167cf1b8d3SKris Buschelman         SSE_INLINE_END_2
33177cf1b8d3SKris Buschelman         v  += 16;
33187cf1b8d3SKris Buschelman       }
33197cf1b8d3SKris Buschelman       v    = aa + ai16;
33207cf1b8d3SKris Buschelman       ai16 = 16*diag[--i];
33217cf1b8d3SKris Buschelman       PREFETCH_NTA(aa+ai16+16);
33227cf1b8d3SKris Buschelman       /*
33237cf1b8d3SKris Buschelman          Scale the result by the diagonal 4x4 block,
33247cf1b8d3SKris Buschelman          which was inverted as part of the factorization
33257cf1b8d3SKris Buschelman       */
33267cf1b8d3SKris Buschelman       SSE_INLINE_BEGIN_3(v,&t[idt],aa+ai16)
33277cf1b8d3SKris Buschelman         /* First Column */
33287cf1b8d3SKris Buschelman         SSE_COPY_PS(XMM0,XMM7)
33297cf1b8d3SKris Buschelman         SSE_SHUFFLE(XMM0,XMM0,0x00)
33307cf1b8d3SKris Buschelman         SSE_MULT_PS_M(XMM0,SSE_ARG_1,FLOAT_0)
33317cf1b8d3SKris Buschelman 
33327cf1b8d3SKris Buschelman         /* Second Column */
33337cf1b8d3SKris Buschelman         SSE_COPY_PS(XMM1,XMM7)
33347cf1b8d3SKris Buschelman         SSE_SHUFFLE(XMM1,XMM1,0x55)
33357cf1b8d3SKris Buschelman         SSE_MULT_PS_M(XMM1,SSE_ARG_1,FLOAT_4)
33367cf1b8d3SKris Buschelman         SSE_ADD_PS(XMM0,XMM1)
33377cf1b8d3SKris Buschelman 
33387cf1b8d3SKris Buschelman         SSE_PREFETCH_NTA(SSE_ARG_3,FLOAT_24)
33397cf1b8d3SKris Buschelman 
33407cf1b8d3SKris Buschelman         /* Third Column */
33417cf1b8d3SKris Buschelman         SSE_COPY_PS(XMM2,XMM7)
33427cf1b8d3SKris Buschelman         SSE_SHUFFLE(XMM2,XMM2,0xAA)
33437cf1b8d3SKris Buschelman         SSE_MULT_PS_M(XMM2,SSE_ARG_1,FLOAT_8)
33447cf1b8d3SKris Buschelman         SSE_ADD_PS(XMM0,XMM2)
33457cf1b8d3SKris Buschelman 
33467cf1b8d3SKris Buschelman         /* Fourth Column */
33477cf1b8d3SKris Buschelman         SSE_COPY_PS(XMM3,XMM7)
33487cf1b8d3SKris Buschelman         SSE_SHUFFLE(XMM3,XMM3,0xFF)
33497cf1b8d3SKris Buschelman         SSE_MULT_PS_M(XMM3,SSE_ARG_1,FLOAT_12)
33507cf1b8d3SKris Buschelman         SSE_ADD_PS(XMM0,XMM3)
33517cf1b8d3SKris Buschelman 
33527cf1b8d3SKris Buschelman         SSE_STORE_PS(SSE_ARG_2,FLOAT_0,XMM0)
33537cf1b8d3SKris Buschelman       SSE_INLINE_END_3
33547cf1b8d3SKris Buschelman 
33557cf1b8d3SKris Buschelman       v    = aa + ai16 + 16;
33567cf1b8d3SKris Buschelman       idt -= 4;
33577cf1b8d3SKris Buschelman     }
33587cf1b8d3SKris Buschelman 
33597cf1b8d3SKris Buschelman     /* Convert t from single precision back to double precision (inplace)*/
33607cf1b8d3SKris Buschelman     idt = 4*(n-1);
33617cf1b8d3SKris Buschelman     for (i=n-1;i>=0;i--) {
33627cf1b8d3SKris Buschelman       /*     CONVERT_FLOAT4_DOUBLE4(&x[idt],&t[idt]); */
33637cf1b8d3SKris Buschelman       /* Unfortunately, CONVERT_ will count from 0 to 3 which doesn't work here. */
33647cf1b8d3SKris Buschelman       PetscScalar *xtemp=&x[idt];
33657cf1b8d3SKris Buschelman       MatScalar   *ttemp=&t[idt];
33667cf1b8d3SKris Buschelman       xtemp[3] = (PetscScalar)ttemp[3];
33677cf1b8d3SKris Buschelman       xtemp[2] = (PetscScalar)ttemp[2];
33687cf1b8d3SKris Buschelman       xtemp[1] = (PetscScalar)ttemp[1];
33697cf1b8d3SKris Buschelman       xtemp[0] = (PetscScalar)ttemp[0];
33707cf1b8d3SKris Buschelman       idt -= 4;
33717cf1b8d3SKris Buschelman     }
33727cf1b8d3SKris Buschelman 
33737cf1b8d3SKris Buschelman   } /* End of artificial scope. */
33741ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
33751ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3376dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*16*(a->nz) - 4.0*A->cmap->n);CHKERRQ(ierr);
33777cf1b8d3SKris Buschelman   SSE_SCOPE_END;
33787cf1b8d3SKris Buschelman   PetscFunctionReturn(0);
33797cf1b8d3SKris Buschelman }
33807cf1b8d3SKris Buschelman 
33813660e330SKris Buschelman #endif
33828f690400SShri Abhyankar 
33834a2ae208SSatish Balay #undef __FUNCT__
33844a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_3"
3385dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_3(Mat A,Vec bb,Vec xx)
33864e2b4712SSatish Balay {
33874e2b4712SSatish Balay   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
33884e2b4712SSatish Balay   IS                iscol=a->col,isrow=a->row;
33896849ba73SBarry Smith   PetscErrorCode    ierr;
33905d0c19d7SBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc;
33915d0c19d7SBarry Smith   const PetscInt    *r,*c,*diag = a->diag,*rout,*cout;
3392d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
3393d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,x1,x2,x3,*t;
3394d9fead3dSBarry Smith   const PetscScalar *b;
33954e2b4712SSatish Balay 
33964e2b4712SSatish Balay   PetscFunctionBegin;
3397d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
33981ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3399f1af5d2fSBarry Smith   t  = a->solve_work;
34004e2b4712SSatish Balay 
34014e2b4712SSatish Balay   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
34024e2b4712SSatish Balay   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
34034e2b4712SSatish Balay 
34044e2b4712SSatish Balay   /* forward solve the lower triangular */
34054e2b4712SSatish Balay   idx    = 3*(*r++);
3406f1af5d2fSBarry Smith   t[0] = b[idx]; t[1] = b[1+idx]; t[2] = b[2+idx];
34074e2b4712SSatish Balay   for (i=1; i<n; i++) {
34084e2b4712SSatish Balay     v     = aa + 9*ai[i];
34094e2b4712SSatish Balay     vi    = aj + ai[i];
34104e2b4712SSatish Balay     nz    = diag[i] - ai[i];
34114e2b4712SSatish Balay     idx   = 3*(*r++);
3412f1af5d2fSBarry Smith     s1  = b[idx]; s2 = b[1+idx]; s3 = b[2+idx];
34134e2b4712SSatish Balay     while (nz--) {
34144e2b4712SSatish Balay       idx   = 3*(*vi++);
3415f1af5d2fSBarry Smith       x1    = t[idx]; x2 = t[1+idx]; x3 = t[2+idx];
3416f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[3]*x2 + v[6]*x3;
3417f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[4]*x2 + v[7]*x3;
3418f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[5]*x2 + v[8]*x3;
34194e2b4712SSatish Balay       v += 9;
34204e2b4712SSatish Balay     }
34214e2b4712SSatish Balay     idx = 3*i;
3422f1af5d2fSBarry Smith     t[idx] = s1; t[1+idx] = s2; t[2+idx] = s3;
34234e2b4712SSatish Balay   }
34244e2b4712SSatish Balay   /* backward solve the upper triangular */
34254e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
34264e2b4712SSatish Balay     v    = aa + 9*diag[i] + 9;
34274e2b4712SSatish Balay     vi   = aj + diag[i] + 1;
34284e2b4712SSatish Balay     nz   = ai[i+1] - diag[i] - 1;
34294e2b4712SSatish Balay     idt  = 3*i;
3430f1af5d2fSBarry Smith     s1 = t[idt]; s2 = t[1+idt]; s3 = t[2+idt];
34314e2b4712SSatish Balay     while (nz--) {
34324e2b4712SSatish Balay       idx   = 3*(*vi++);
3433f1af5d2fSBarry Smith       x1    = t[idx]; x2 = t[1+idx]; x3 = t[2+idx];
3434f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[3]*x2 + v[6]*x3;
3435f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[4]*x2 + v[7]*x3;
3436f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[5]*x2 + v[8]*x3;
34374e2b4712SSatish Balay       v += 9;
34384e2b4712SSatish Balay     }
34394e2b4712SSatish Balay     idc = 3*(*c--);
34404e2b4712SSatish Balay     v   = aa + 9*diag[i];
3441f1af5d2fSBarry Smith     x[idc]   = t[idt]   = v[0]*s1 + v[3]*s2 + v[6]*s3;
3442f1af5d2fSBarry Smith     x[1+idc] = t[1+idt] = v[1]*s1 + v[4]*s2 + v[7]*s3;
3443f1af5d2fSBarry Smith     x[2+idc] = t[2+idt] = v[2]*s1 + v[5]*s2 + v[8]*s3;
34444e2b4712SSatish Balay   }
34454e2b4712SSatish Balay   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
34464e2b4712SSatish Balay   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
3447d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
34481ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3449dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*9*(a->nz) - 3.0*A->cmap->n);CHKERRQ(ierr);
34504e2b4712SSatish Balay   PetscFunctionReturn(0);
34514e2b4712SSatish Balay }
34524e2b4712SSatish Balay 
34538f690400SShri Abhyankar #undef __FUNCT__
34548f690400SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_3_newdatastruct"
34558f690400SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_3_newdatastruct(Mat A,Vec bb,Vec xx)
34568f690400SShri Abhyankar {
34578f690400SShri Abhyankar   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
34588f690400SShri Abhyankar   IS                iscol=a->col,isrow=a->row;
34598f690400SShri Abhyankar   PetscErrorCode    ierr;
346029b92fc1SShri Abhyankar   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc,k,m;
34618f690400SShri Abhyankar   const PetscInt    *r,*c,*rout,*cout;
34628f690400SShri Abhyankar   const MatScalar   *aa=a->a,*v;
34638f690400SShri Abhyankar   PetscScalar       *x,s1,s2,s3,x1,x2,x3,*t;
34648f690400SShri Abhyankar   const PetscScalar *b;
34658f690400SShri Abhyankar 
34668f690400SShri Abhyankar   PetscFunctionBegin;
34678f690400SShri Abhyankar   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
34688f690400SShri Abhyankar   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
34698f690400SShri Abhyankar   t  = a->solve_work;
34708f690400SShri Abhyankar 
34718f690400SShri Abhyankar   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
347229b92fc1SShri Abhyankar   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
34738f690400SShri Abhyankar 
34748f690400SShri Abhyankar   /* forward solve the lower triangular */
347529b92fc1SShri Abhyankar   idx    = 3*r[0];
34768f690400SShri Abhyankar   t[0] = b[idx]; t[1] = b[1+idx]; t[2] = b[2+idx];
34778f690400SShri Abhyankar   for (i=1; i<n; i++) {
34788f690400SShri Abhyankar     v     = aa + 9*ai[i];
34798f690400SShri Abhyankar     vi    = aj + ai[i];
34808f690400SShri Abhyankar     nz    = ai[i+1] - ai[i];
348129b92fc1SShri Abhyankar     idx   = 3*r[i];
34828f690400SShri Abhyankar     s1  = b[idx]; s2 = b[1+idx]; s3 = b[2+idx];
348329b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
348429b92fc1SShri Abhyankar       idx   = 3*vi[m];
34858f690400SShri Abhyankar       x1    = t[idx]; x2 = t[1+idx]; x3 = t[2+idx];
34868f690400SShri Abhyankar       s1 -= v[0]*x1 + v[3]*x2 + v[6]*x3;
34878f690400SShri Abhyankar       s2 -= v[1]*x1 + v[4]*x2 + v[7]*x3;
34888f690400SShri Abhyankar       s3 -= v[2]*x1 + v[5]*x2 + v[8]*x3;
34898f690400SShri Abhyankar       v += 9;
34908f690400SShri Abhyankar     }
34918f690400SShri Abhyankar     idx = 3*i;
34928f690400SShri Abhyankar     t[idx] = s1; t[1+idx] = s2; t[2+idx] = s3;
34938f690400SShri Abhyankar   }
34948f690400SShri Abhyankar   /* backward solve the upper triangular */
34958f690400SShri Abhyankar   for (i=n-1; i>=0; i--){
34968f690400SShri Abhyankar     k    = 2*n-i;
34978f690400SShri Abhyankar     v    = aa + 9*ai[k];
34988f690400SShri Abhyankar     vi   = aj + ai[k];
34998f690400SShri Abhyankar     nz   = ai[k +1] - ai[k] - 1;
35008f690400SShri Abhyankar     idt  = 3*i;
35018f690400SShri Abhyankar     s1 = t[idt]; s2 = t[1+idt]; s3 = t[2+idt];
350229b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
350329b92fc1SShri Abhyankar       idx   = 3*vi[m];
35048f690400SShri Abhyankar       x1    = t[idx]; x2 = t[1+idx]; x3 = t[2+idx];
35058f690400SShri Abhyankar       s1 -= v[0]*x1 + v[3]*x2 + v[6]*x3;
35068f690400SShri Abhyankar       s2 -= v[1]*x1 + v[4]*x2 + v[7]*x3;
35078f690400SShri Abhyankar       s3 -= v[2]*x1 + v[5]*x2 + v[8]*x3;
35088f690400SShri Abhyankar       v += 9;
35098f690400SShri Abhyankar     }
351029b92fc1SShri Abhyankar     idc = 3*c[i];
35118f690400SShri Abhyankar     x[idc]   = t[idt]   = v[0]*s1 + v[3]*s2 + v[6]*s3;
35128f690400SShri Abhyankar     x[1+idc] = t[1+idt] = v[1]*s1 + v[4]*s2 + v[7]*s3;
35138f690400SShri Abhyankar     x[2+idc] = t[2+idt] = v[2]*s1 + v[5]*s2 + v[8]*s3;
35148f690400SShri Abhyankar   }
35158f690400SShri Abhyankar   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
35168f690400SShri Abhyankar   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
35178f690400SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
35188f690400SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
35198f690400SShri Abhyankar   ierr = PetscLogFlops(2.0*9*(a->nz) - 3.0*A->cmap->n);CHKERRQ(ierr);
35208f690400SShri Abhyankar   PetscFunctionReturn(0);
35218f690400SShri Abhyankar }
35228f690400SShri Abhyankar 
352315091d37SBarry Smith /*
352415091d37SBarry Smith       Special case where the matrix was ILU(0) factored in the natural
352515091d37SBarry Smith    ordering. This eliminates the need for the column and row permutation.
352615091d37SBarry Smith */
35274a2ae208SSatish Balay #undef __FUNCT__
35284a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_3_NaturalOrdering"
3529dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_3_NaturalOrdering(Mat A,Vec bb,Vec xx)
353015091d37SBarry Smith {
353115091d37SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
3532690b6cddSBarry Smith   PetscInt          n=a->mbs,*ai=a->i,*aj=a->j;
3533dfbe8321SBarry Smith   PetscErrorCode    ierr;
3534690b6cddSBarry Smith   PetscInt          *diag = a->diag;
3535d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
3536d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,s3,x1,x2,x3;
3537d9fead3dSBarry Smith   const PetscScalar *b;
3538690b6cddSBarry Smith   PetscInt          jdx,idt,idx,nz,*vi,i;
353915091d37SBarry Smith 
354015091d37SBarry Smith   PetscFunctionBegin;
3541d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
35421ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
354315091d37SBarry Smith 
354415091d37SBarry Smith   /* forward solve the lower triangular */
354515091d37SBarry Smith   idx    = 0;
354615091d37SBarry Smith   x[0]   = b[0]; x[1] = b[1]; x[2] = b[2];
354715091d37SBarry Smith   for (i=1; i<n; i++) {
354815091d37SBarry Smith     v     =  aa      + 9*ai[i];
354915091d37SBarry Smith     vi    =  aj      + ai[i];
355015091d37SBarry Smith     nz    =  diag[i] - ai[i];
355115091d37SBarry Smith     idx   +=  3;
3552f1af5d2fSBarry Smith     s1  =  b[idx];s2 = b[1+idx];s3 = b[2+idx];
355315091d37SBarry Smith     while (nz--) {
355415091d37SBarry Smith       jdx   = 3*(*vi++);
355515091d37SBarry Smith       x1    = x[jdx];x2 = x[1+jdx];x3 = x[2+jdx];
3556f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[3]*x2 + v[6]*x3;
3557f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[4]*x2 + v[7]*x3;
3558f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[5]*x2 + v[8]*x3;
355915091d37SBarry Smith       v    += 9;
356015091d37SBarry Smith     }
3561f1af5d2fSBarry Smith     x[idx]   = s1;
3562f1af5d2fSBarry Smith     x[1+idx] = s2;
3563f1af5d2fSBarry Smith     x[2+idx] = s3;
356415091d37SBarry Smith   }
356515091d37SBarry Smith   /* backward solve the upper triangular */
356615091d37SBarry Smith   for (i=n-1; i>=0; i--){
356715091d37SBarry Smith     v    = aa + 9*diag[i] + 9;
356815091d37SBarry Smith     vi   = aj + diag[i] + 1;
356915091d37SBarry Smith     nz   = ai[i+1] - diag[i] - 1;
357015091d37SBarry Smith     idt  = 3*i;
3571f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt];
3572f1af5d2fSBarry Smith     s3 = x[2+idt];
357315091d37SBarry Smith     while (nz--) {
357415091d37SBarry Smith       idx   = 3*(*vi++);
357515091d37SBarry Smith       x1    = x[idx];   x2 = x[1+idx];x3    = x[2+idx];
3576f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[3]*x2 + v[6]*x3;
3577f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[4]*x2 + v[7]*x3;
3578f1af5d2fSBarry Smith       s3 -= v[2]*x1 + v[5]*x2 + v[8]*x3;
357915091d37SBarry Smith       v    += 9;
358015091d37SBarry Smith     }
358115091d37SBarry Smith     v        = aa +  9*diag[i];
3582f1af5d2fSBarry Smith     x[idt]   = v[0]*s1 + v[3]*s2 + v[6]*s3;
3583f1af5d2fSBarry Smith     x[1+idt] = v[1]*s1 + v[4]*s2 + v[7]*s3;
3584f1af5d2fSBarry Smith     x[2+idt] = v[2]*s1 + v[5]*s2 + v[8]*s3;
358515091d37SBarry Smith   }
358615091d37SBarry Smith 
3587d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
35881ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3589dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*9*(a->nz) - 3.0*A->cmap->n);CHKERRQ(ierr);
359015091d37SBarry Smith   PetscFunctionReturn(0);
359115091d37SBarry Smith }
359215091d37SBarry Smith 
35934a2ae208SSatish Balay #undef __FUNCT__
3594cee9d6f2SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_3_NaturalOrdering_newdatastruct"
3595cee9d6f2SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_3_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx)
3596cee9d6f2SShri Abhyankar {
3597cee9d6f2SShri Abhyankar     Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
3598ce3d78c0SShri Abhyankar     PetscInt          i,k,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz;
3599cee9d6f2SShri Abhyankar     PetscErrorCode    ierr;
3600cee9d6f2SShri Abhyankar     PetscInt          idx,jdx,idt;
3601cee9d6f2SShri Abhyankar     PetscInt          bs = A->rmap->bs,bs2 = a->bs2;
3602cee9d6f2SShri Abhyankar     const MatScalar   *aa=a->a,*v;
3603cee9d6f2SShri Abhyankar     PetscScalar       *x;
3604cee9d6f2SShri Abhyankar     const PetscScalar *b;
3605cee9d6f2SShri Abhyankar     PetscScalar        s1,s2,s3,x1,x2,x3;
3606cee9d6f2SShri Abhyankar 
3607cee9d6f2SShri Abhyankar     PetscFunctionBegin;
3608cee9d6f2SShri Abhyankar     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3609cee9d6f2SShri Abhyankar     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3610cee9d6f2SShri Abhyankar     /* forward solve the lower triangular */
3611cee9d6f2SShri Abhyankar     idx    = 0;
3612cee9d6f2SShri Abhyankar     x[0] = b[idx]; x[1] = b[1+idx];x[2] = b[2+idx];
3613cee9d6f2SShri Abhyankar     for (i=1; i<n; i++) {
3614cee9d6f2SShri Abhyankar        v    = aa + bs2*ai[i];
3615cee9d6f2SShri Abhyankar        vi   = aj + ai[i];
3616cee9d6f2SShri Abhyankar        nz   = ai[i+1] - ai[i];
3617cee9d6f2SShri Abhyankar       idx   = bs*i;
3618cee9d6f2SShri Abhyankar        s1   = b[idx];s2 = b[1+idx];s3 = b[2+idx];
3619ce3d78c0SShri Abhyankar       for(k=0;k<nz;k++){
3620ce3d78c0SShri Abhyankar          jdx   = bs*vi[k];
3621cee9d6f2SShri Abhyankar           x1    = x[jdx];x2 = x[1+jdx]; x3 =x[2+jdx];
3622cee9d6f2SShri Abhyankar           s1   -= v[0]*x1 + v[3]*x2 + v[6]*x3;
3623cee9d6f2SShri Abhyankar           s2   -= v[1]*x1 + v[4]*x2 + v[7]*x3;
3624cee9d6f2SShri Abhyankar           s3   -= v[2]*x1 + v[5]*x2 + v[8]*x3;
3625cee9d6f2SShri Abhyankar 
3626cee9d6f2SShri Abhyankar           v   +=  bs2;
3627cee9d6f2SShri Abhyankar         }
3628cee9d6f2SShri Abhyankar 
3629cee9d6f2SShri Abhyankar        x[idx]   = s1;
3630cee9d6f2SShri Abhyankar        x[1+idx] = s2;
3631cee9d6f2SShri Abhyankar        x[2+idx] = s3;
3632cee9d6f2SShri Abhyankar     }
3633cee9d6f2SShri Abhyankar 
3634cee9d6f2SShri Abhyankar    /* backward solve the upper triangular */
3635cee9d6f2SShri Abhyankar   for (i=n-1; i>=0; i--){
3636cee9d6f2SShri Abhyankar      v   = aa + bs2*ai[2*n-i];
3637cee9d6f2SShri Abhyankar      vi  = aj + ai[2*n-i];
3638cee9d6f2SShri Abhyankar      nz  = ai[2*n-i +1] - ai[2*n-i]-1;
3639cee9d6f2SShri Abhyankar      idt = bs*i;
3640cee9d6f2SShri Abhyankar      s1 = x[idt];  s2 = x[1+idt];s3 = x[2+idt];
3641cee9d6f2SShri Abhyankar 
3642ce3d78c0SShri Abhyankar      for(k=0;k<nz;k++){
3643ce3d78c0SShri Abhyankar        idx   = bs*vi[k];
3644cee9d6f2SShri Abhyankar        x1    = x[idx];   x2 = x[1+idx]; x3 = x[2+idx];
3645cee9d6f2SShri Abhyankar        s1 -= v[0]*x1 + v[3]*x2 + v[6]*x3;
3646cee9d6f2SShri Abhyankar        s2 -= v[1]*x1 + v[4]*x2 + v[7]*x3;
3647cee9d6f2SShri Abhyankar        s3 -= v[2]*x1 + v[5]*x2 + v[8]*x3;
3648cee9d6f2SShri Abhyankar 
3649cee9d6f2SShri Abhyankar         v   +=  bs2;
3650cee9d6f2SShri Abhyankar     }
3651cee9d6f2SShri Abhyankar     /* x = inv_diagonal*x */
3652cee9d6f2SShri Abhyankar    x[idt]   = v[0]*s1 + v[3]*s2 + v[6]*s3;
3653cee9d6f2SShri Abhyankar    x[1+idt] = v[1]*s1 + v[4]*s2 + v[7]*s3;
3654cee9d6f2SShri Abhyankar    x[2+idt] = v[2]*s1 + v[5]*s2 + v[8]*s3;
3655cee9d6f2SShri Abhyankar 
3656cee9d6f2SShri Abhyankar   }
3657cee9d6f2SShri Abhyankar 
3658cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3659cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3660cee9d6f2SShri Abhyankar   ierr = PetscLogFlops(2.0*bs2*(a->nz) - bs*A->cmap->n);CHKERRQ(ierr);
3661cee9d6f2SShri Abhyankar   PetscFunctionReturn(0);
3662cee9d6f2SShri Abhyankar }
3663cee9d6f2SShri Abhyankar 
3664cee9d6f2SShri Abhyankar #undef __FUNCT__
36654a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_2"
3666dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_2(Mat A,Vec bb,Vec xx)
36674e2b4712SSatish Balay {
36684e2b4712SSatish Balay   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
36694e2b4712SSatish Balay   IS                iscol=a->col,isrow=a->row;
36706849ba73SBarry Smith   PetscErrorCode    ierr;
36715d0c19d7SBarry Smith   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt,idc;
36725d0c19d7SBarry Smith   const PetscInt    *r,*c,*diag = a->diag,*rout,*cout;
3673d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
3674d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,x1,x2,*t;
3675d9fead3dSBarry Smith   const PetscScalar *b;
36764e2b4712SSatish Balay 
36774e2b4712SSatish Balay   PetscFunctionBegin;
3678d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
36791ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3680f1af5d2fSBarry Smith   t  = a->solve_work;
36814e2b4712SSatish Balay 
36824e2b4712SSatish Balay   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
36834e2b4712SSatish Balay   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
36844e2b4712SSatish Balay 
36854e2b4712SSatish Balay   /* forward solve the lower triangular */
36864e2b4712SSatish Balay   idx    = 2*(*r++);
3687f1af5d2fSBarry Smith   t[0] = b[idx]; t[1] = b[1+idx];
36884e2b4712SSatish Balay   for (i=1; i<n; i++) {
36894e2b4712SSatish Balay     v     = aa + 4*ai[i];
36904e2b4712SSatish Balay     vi    = aj + ai[i];
36914e2b4712SSatish Balay     nz    = diag[i] - ai[i];
36924e2b4712SSatish Balay     idx   = 2*(*r++);
3693f1af5d2fSBarry Smith     s1  = b[idx]; s2 = b[1+idx];
36944e2b4712SSatish Balay     while (nz--) {
36954e2b4712SSatish Balay       idx   = 2*(*vi++);
3696f1af5d2fSBarry Smith       x1    = t[idx]; x2 = t[1+idx];
3697f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[2]*x2;
3698f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[3]*x2;
36994e2b4712SSatish Balay       v += 4;
37004e2b4712SSatish Balay     }
37014e2b4712SSatish Balay     idx = 2*i;
3702f1af5d2fSBarry Smith     t[idx] = s1; t[1+idx] = s2;
37034e2b4712SSatish Balay   }
37044e2b4712SSatish Balay   /* backward solve the upper triangular */
37054e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
37064e2b4712SSatish Balay     v    = aa + 4*diag[i] + 4;
37074e2b4712SSatish Balay     vi   = aj + diag[i] + 1;
37084e2b4712SSatish Balay     nz   = ai[i+1] - diag[i] - 1;
37094e2b4712SSatish Balay     idt  = 2*i;
3710f1af5d2fSBarry Smith     s1 = t[idt]; s2 = t[1+idt];
37114e2b4712SSatish Balay     while (nz--) {
37124e2b4712SSatish Balay       idx   = 2*(*vi++);
3713f1af5d2fSBarry Smith       x1    = t[idx]; x2 = t[1+idx];
3714f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[2]*x2;
3715f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[3]*x2;
37164e2b4712SSatish Balay       v += 4;
37174e2b4712SSatish Balay     }
37184e2b4712SSatish Balay     idc = 2*(*c--);
37194e2b4712SSatish Balay     v   = aa + 4*diag[i];
3720f1af5d2fSBarry Smith     x[idc]   = t[idt]   = v[0]*s1 + v[2]*s2;
3721f1af5d2fSBarry Smith     x[1+idc] = t[1+idt] = v[1]*s1 + v[3]*s2;
37224e2b4712SSatish Balay   }
37234e2b4712SSatish Balay   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
37244e2b4712SSatish Balay   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
3725d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
37261ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3727dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*4*(a->nz) - 2.0*A->cmap->n);CHKERRQ(ierr);
37284e2b4712SSatish Balay   PetscFunctionReturn(0);
37294e2b4712SSatish Balay }
37304e2b4712SSatish Balay 
37318f690400SShri Abhyankar #undef __FUNCT__
37328f690400SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_2_newdatastruct"
37338f690400SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_2_newdatastruct(Mat A,Vec bb,Vec xx)
37348f690400SShri Abhyankar {
37358f690400SShri Abhyankar   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ *)A->data;
37368f690400SShri Abhyankar   IS                iscol=a->col,isrow=a->row;
37378f690400SShri Abhyankar   PetscErrorCode    ierr;
373829b92fc1SShri Abhyankar   PetscInt          i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,jdx,idt,idc,k,m;
37398f690400SShri Abhyankar   const PetscInt    *r,*c,*rout,*cout;
37408f690400SShri Abhyankar   const MatScalar   *aa=a->a,*v;
37418f690400SShri Abhyankar   PetscScalar       *x,s1,s2,x1,x2,*t;
37428f690400SShri Abhyankar   const PetscScalar *b;
37438f690400SShri Abhyankar 
37448f690400SShri Abhyankar   PetscFunctionBegin;
37458f690400SShri Abhyankar   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
37468f690400SShri Abhyankar   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
37478f690400SShri Abhyankar   t  = a->solve_work;
37488f690400SShri Abhyankar 
37498f690400SShri Abhyankar   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
375029b92fc1SShri Abhyankar   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
37518f690400SShri Abhyankar 
37528f690400SShri Abhyankar   /* forward solve the lower triangular */
375329b92fc1SShri Abhyankar   idx    = 2*r[0];
37548f690400SShri Abhyankar   t[0] = b[idx]; t[1] = b[1+idx];
37558f690400SShri Abhyankar   for (i=1; i<n; i++) {
37568f690400SShri Abhyankar     v     = aa + 4*ai[i];
37578f690400SShri Abhyankar     vi    = aj + ai[i];
37588f690400SShri Abhyankar     nz    = ai[i+1] - ai[i];
375929b92fc1SShri Abhyankar     idx   = 2*r[i];
37608f690400SShri Abhyankar     s1  = b[idx]; s2 = b[1+idx];
376129b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
376229b92fc1SShri Abhyankar       jdx   = 2*vi[m];
37638f690400SShri Abhyankar       x1    = t[jdx]; x2 = t[1+jdx];
37648f690400SShri Abhyankar       s1 -= v[0]*x1 + v[2]*x2;
37658f690400SShri Abhyankar       s2 -= v[1]*x1 + v[3]*x2;
37668f690400SShri Abhyankar       v += 4;
37678f690400SShri Abhyankar     }
37688f690400SShri Abhyankar     idx = 2*i;
37698f690400SShri Abhyankar     t[idx] = s1; t[1+idx] = s2;
37708f690400SShri Abhyankar   }
37718f690400SShri Abhyankar   /* backward solve the upper triangular */
37728f690400SShri Abhyankar   for (i=n-1; i>=0; i--){
37738f690400SShri Abhyankar     k = 2*n-i;
37748f690400SShri Abhyankar     v    = aa + 4*ai[k];
37758f690400SShri Abhyankar     vi   = aj + ai[k];
37768f690400SShri Abhyankar     nz   = ai[k +1] - ai[k] - 1;
37778f690400SShri Abhyankar     idt  = 2*i;
37788f690400SShri Abhyankar     s1 = t[idt]; s2 = t[1+idt];
377929b92fc1SShri Abhyankar     for(m=0;m<nz;m++){
378029b92fc1SShri Abhyankar       idx   = 2*vi[m];
37818f690400SShri Abhyankar       x1    = t[idx]; x2 = t[1+idx];
37828f690400SShri Abhyankar       s1 -= v[0]*x1 + v[2]*x2;
37838f690400SShri Abhyankar       s2 -= v[1]*x1 + v[3]*x2;
37848f690400SShri Abhyankar       v += 4;
37858f690400SShri Abhyankar     }
378629b92fc1SShri Abhyankar     idc = 2*c[i];
37878f690400SShri Abhyankar     x[idc]   = t[idt]   = v[0]*s1 + v[2]*s2;
37888f690400SShri Abhyankar     x[1+idc] = t[1+idt] = v[1]*s1 + v[3]*s2;
37898f690400SShri Abhyankar   }
37908f690400SShri Abhyankar   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
37918f690400SShri Abhyankar   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
37928f690400SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
37938f690400SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
37948f690400SShri Abhyankar   ierr = PetscLogFlops(2.0*4*(a->nz) - 2.0*A->cmap->n);CHKERRQ(ierr);
37958f690400SShri Abhyankar   PetscFunctionReturn(0);
37968f690400SShri Abhyankar }
37978f690400SShri Abhyankar 
37988f690400SShri Abhyankar 
379915091d37SBarry Smith /*
380015091d37SBarry Smith       Special case where the matrix was ILU(0) factored in the natural
380115091d37SBarry Smith    ordering. This eliminates the need for the column and row permutation.
380215091d37SBarry Smith */
38034a2ae208SSatish Balay #undef __FUNCT__
38044a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_2_NaturalOrdering"
3805dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_2_NaturalOrdering(Mat A,Vec bb,Vec xx)
380615091d37SBarry Smith {
380715091d37SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
3808690b6cddSBarry Smith   PetscInt          n=a->mbs,*ai=a->i,*aj=a->j;
3809dfbe8321SBarry Smith   PetscErrorCode    ierr;
3810690b6cddSBarry Smith   PetscInt          *diag = a->diag;
3811d9fead3dSBarry Smith   const MatScalar   *aa=a->a,*v;
3812d9fead3dSBarry Smith   PetscScalar       *x,s1,s2,x1,x2;
3813d9fead3dSBarry Smith   const PetscScalar *b;
3814690b6cddSBarry Smith   PetscInt          jdx,idt,idx,nz,*vi,i;
381515091d37SBarry Smith 
381615091d37SBarry Smith   PetscFunctionBegin;
3817d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
38181ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
381915091d37SBarry Smith 
382015091d37SBarry Smith   /* forward solve the lower triangular */
382115091d37SBarry Smith   idx    = 0;
382215091d37SBarry Smith   x[0]   = b[0]; x[1] = b[1];
382315091d37SBarry Smith   for (i=1; i<n; i++) {
382415091d37SBarry Smith     v     =  aa      + 4*ai[i];
382515091d37SBarry Smith     vi    =  aj      + ai[i];
382615091d37SBarry Smith     nz    =  diag[i] - ai[i];
382715091d37SBarry Smith     idx   +=  2;
3828f1af5d2fSBarry Smith     s1  =  b[idx];s2 = b[1+idx];
382915091d37SBarry Smith     while (nz--) {
383015091d37SBarry Smith       jdx   = 2*(*vi++);
383115091d37SBarry Smith       x1    = x[jdx];x2 = x[1+jdx];
3832f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[2]*x2;
3833f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[3]*x2;
383415091d37SBarry Smith       v    += 4;
383515091d37SBarry Smith     }
3836f1af5d2fSBarry Smith     x[idx]   = s1;
3837f1af5d2fSBarry Smith     x[1+idx] = s2;
383815091d37SBarry Smith   }
383915091d37SBarry Smith   /* backward solve the upper triangular */
384015091d37SBarry Smith   for (i=n-1; i>=0; i--){
384115091d37SBarry Smith     v    = aa + 4*diag[i] + 4;
384215091d37SBarry Smith     vi   = aj + diag[i] + 1;
384315091d37SBarry Smith     nz   = ai[i+1] - diag[i] - 1;
384415091d37SBarry Smith     idt  = 2*i;
3845f1af5d2fSBarry Smith     s1 = x[idt];  s2 = x[1+idt];
384615091d37SBarry Smith     while (nz--) {
384715091d37SBarry Smith       idx   = 2*(*vi++);
384815091d37SBarry Smith       x1    = x[idx];   x2 = x[1+idx];
3849f1af5d2fSBarry Smith       s1 -= v[0]*x1 + v[2]*x2;
3850f1af5d2fSBarry Smith       s2 -= v[1]*x1 + v[3]*x2;
385115091d37SBarry Smith       v    += 4;
385215091d37SBarry Smith     }
385315091d37SBarry Smith     v        = aa +  4*diag[i];
3854f1af5d2fSBarry Smith     x[idt]   = v[0]*s1 + v[2]*s2;
3855f1af5d2fSBarry Smith     x[1+idt] = v[1]*s1 + v[3]*s2;
385615091d37SBarry Smith   }
385715091d37SBarry Smith 
3858d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
38591ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3860dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*4*(a->nz) - 2.0*A->cmap->n);CHKERRQ(ierr);
386115091d37SBarry Smith   PetscFunctionReturn(0);
386215091d37SBarry Smith }
386315091d37SBarry Smith 
38644a2ae208SSatish Balay #undef __FUNCT__
3865cee9d6f2SShri Abhyankar #define __FUNCT__ "MatSolve_SeqBAIJ_2_NaturalOrdering_newdatastruct"
3866cee9d6f2SShri Abhyankar PetscErrorCode MatSolve_SeqBAIJ_2_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx)
3867cee9d6f2SShri Abhyankar {
3868cee9d6f2SShri Abhyankar     Mat_SeqBAIJ       *a = (Mat_SeqBAIJ *)A->data;
3869ce3d78c0SShri Abhyankar     PetscInt          i,k,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz,idx,idt;
3870cee9d6f2SShri Abhyankar     PetscErrorCode    ierr;
3871cee9d6f2SShri Abhyankar     PetscInt          jdx;
3872cee9d6f2SShri Abhyankar     const MatScalar   *aa=a->a,*v;
3873cee9d6f2SShri Abhyankar     PetscScalar       *x,s1,s2,x1,x2;
3874cee9d6f2SShri Abhyankar     const PetscScalar *b;
3875cee9d6f2SShri Abhyankar 
3876cee9d6f2SShri Abhyankar     PetscFunctionBegin;
3877cee9d6f2SShri Abhyankar     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3878cee9d6f2SShri Abhyankar     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3879cee9d6f2SShri Abhyankar     /* forward solve the lower triangular */
3880cee9d6f2SShri Abhyankar     idx    = 0;
3881cee9d6f2SShri Abhyankar     x[0] = b[idx]; x[1] = b[1+idx];
3882cee9d6f2SShri Abhyankar     for (i=1; i<n; i++) {
3883cee9d6f2SShri Abhyankar         v   = aa + 4*ai[i];
3884cee9d6f2SShri Abhyankar        vi   = aj + ai[i];
3885cee9d6f2SShri Abhyankar        nz   = ai[i+1] - ai[i];
3886cee9d6f2SShri Abhyankar        idx  = 2*i;
3887cee9d6f2SShri Abhyankar        s1   = b[idx];s2 = b[1+idx];
3888ce3d78c0SShri Abhyankar       for(k=0;k<nz;k++){
3889ce3d78c0SShri Abhyankar          jdx   = 2*vi[k];
3890cee9d6f2SShri Abhyankar           x1    = x[jdx];x2 = x[1+jdx];
3891cee9d6f2SShri Abhyankar           s1   -= v[0]*x1 + v[2]*x2;
3892cee9d6f2SShri Abhyankar           s2   -= v[1]*x1 + v[3]*x2;
3893cee9d6f2SShri Abhyankar            v   +=  4;
3894cee9d6f2SShri Abhyankar         }
3895cee9d6f2SShri Abhyankar        x[idx]   = s1;
3896cee9d6f2SShri Abhyankar        x[1+idx] = s2;
3897cee9d6f2SShri Abhyankar     }
3898cee9d6f2SShri Abhyankar 
3899cee9d6f2SShri Abhyankar    /* backward solve the upper triangular */
3900cee9d6f2SShri Abhyankar   for (i=n-1; i>=0; i--){
3901cee9d6f2SShri Abhyankar      v   = aa + 4*ai[2*n-i];
3902cee9d6f2SShri Abhyankar      vi  = aj + ai[2*n-i];
3903cee9d6f2SShri Abhyankar      nz  = ai[2*n-i +1] - ai[2*n-i]-1;
3904cee9d6f2SShri Abhyankar      idt = 2*i;
3905cee9d6f2SShri Abhyankar      s1 = x[idt];  s2 = x[1+idt];
3906ce3d78c0SShri Abhyankar      for(k=0;k<nz;k++){
3907ce3d78c0SShri Abhyankar       idx   = 2*vi[k];
3908cee9d6f2SShri Abhyankar        x1    = x[idx];   x2 = x[1+idx];
3909cee9d6f2SShri Abhyankar        s1 -= v[0]*x1 + v[2]*x2;
3910cee9d6f2SShri Abhyankar        s2 -= v[1]*x1 + v[3]*x2;
3911cee9d6f2SShri Abhyankar          v    += 4;
3912cee9d6f2SShri Abhyankar     }
3913cee9d6f2SShri Abhyankar     /* x = inv_diagonal*x */
3914cee9d6f2SShri Abhyankar    x[idt]   = v[0]*s1 + v[2]*s2;
3915cee9d6f2SShri Abhyankar    x[1+idt] = v[1]*s1 + v[3]*s2;
3916cee9d6f2SShri Abhyankar   }
3917cee9d6f2SShri Abhyankar 
3918cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3919cee9d6f2SShri Abhyankar   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3920cee9d6f2SShri Abhyankar   ierr = PetscLogFlops(2.0*4*(a->nz) - 2.0*A->cmap->n);CHKERRQ(ierr);
3921cee9d6f2SShri Abhyankar   PetscFunctionReturn(0);
3922cee9d6f2SShri Abhyankar }
3923cee9d6f2SShri Abhyankar 
3924cee9d6f2SShri Abhyankar #undef __FUNCT__
39254a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_1"
3926dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_1(Mat A,Vec bb,Vec xx)
39274e2b4712SSatish Balay {
39284e2b4712SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
39294e2b4712SSatish Balay   IS             iscol=a->col,isrow=a->row;
39306849ba73SBarry Smith   PetscErrorCode ierr;
39315d0c19d7SBarry Smith   PetscInt       i,n=a->mbs,*vi,*ai=a->i,*aj=a->j,nz;
39325d0c19d7SBarry Smith   const PetscInt *r,*c,*diag = a->diag,*rout,*cout;
39333f1db9ecSBarry Smith   MatScalar      *aa=a->a,*v;
393487828ca2SBarry Smith   PetscScalar    *x,*b,s1,*t;
39354e2b4712SSatish Balay 
39364e2b4712SSatish Balay   PetscFunctionBegin;
39374e2b4712SSatish Balay   if (!n) PetscFunctionReturn(0);
39384e2b4712SSatish Balay 
39391ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
39401ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3941f1af5d2fSBarry Smith   t  = a->solve_work;
39424e2b4712SSatish Balay 
39434e2b4712SSatish Balay   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
39444e2b4712SSatish Balay   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
39454e2b4712SSatish Balay 
39464e2b4712SSatish Balay   /* forward solve the lower triangular */
3947f1af5d2fSBarry Smith   t[0] = b[*r++];
39484e2b4712SSatish Balay   for (i=1; i<n; i++) {
39494e2b4712SSatish Balay     v     = aa + ai[i];
39504e2b4712SSatish Balay     vi    = aj + ai[i];
39514e2b4712SSatish Balay     nz    = diag[i] - ai[i];
3952f1af5d2fSBarry Smith     s1  = b[*r++];
39534e2b4712SSatish Balay     while (nz--) {
3954f1af5d2fSBarry Smith       s1 -= (*v++)*t[*vi++];
39554e2b4712SSatish Balay     }
3956f1af5d2fSBarry Smith     t[i] = s1;
39574e2b4712SSatish Balay   }
39584e2b4712SSatish Balay   /* backward solve the upper triangular */
39594e2b4712SSatish Balay   for (i=n-1; i>=0; i--){
39604e2b4712SSatish Balay     v    = aa + diag[i] + 1;
39614e2b4712SSatish Balay     vi   = aj + diag[i] + 1;
39624e2b4712SSatish Balay     nz   = ai[i+1] - diag[i] - 1;
3963f1af5d2fSBarry Smith     s1 = t[i];
39644e2b4712SSatish Balay     while (nz--) {
3965f1af5d2fSBarry Smith       s1 -= (*v++)*t[*vi++];
39664e2b4712SSatish Balay     }
3967f1af5d2fSBarry Smith     x[*c--] = t[i] = aa[diag[i]]*s1;
39684e2b4712SSatish Balay   }
39694e2b4712SSatish Balay 
39704e2b4712SSatish Balay   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
39714e2b4712SSatish Balay   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
39721ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
39731ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3974dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*1*(a->nz) - A->cmap->n);CHKERRQ(ierr);
39754e2b4712SSatish Balay   PetscFunctionReturn(0);
39764e2b4712SSatish Balay }
397715091d37SBarry Smith /*
397815091d37SBarry Smith       Special case where the matrix was ILU(0) factored in the natural
397915091d37SBarry Smith    ordering. This eliminates the need for the column and row permutation.
398015091d37SBarry Smith */
39814a2ae208SSatish Balay #undef __FUNCT__
39824a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqBAIJ_1_NaturalOrdering"
3983dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqBAIJ_1_NaturalOrdering(Mat A,Vec bb,Vec xx)
398415091d37SBarry Smith {
398515091d37SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data;
3986690b6cddSBarry Smith   PetscInt       n=a->mbs,*ai=a->i,*aj=a->j;
3987dfbe8321SBarry Smith   PetscErrorCode ierr;
3988690b6cddSBarry Smith   PetscInt       *diag = a->diag;
398915091d37SBarry Smith   MatScalar      *aa=a->a;
399087828ca2SBarry Smith   PetscScalar    *x,*b;
399187828ca2SBarry Smith   PetscScalar    s1,x1;
399215091d37SBarry Smith   MatScalar      *v;
3993690b6cddSBarry Smith   PetscInt       jdx,idt,idx,nz,*vi,i;
399415091d37SBarry Smith 
399515091d37SBarry Smith   PetscFunctionBegin;
39961ebc52fbSHong Zhang   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
39971ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
399815091d37SBarry Smith 
399915091d37SBarry Smith   /* forward solve the lower triangular */
400015091d37SBarry Smith   idx    = 0;
400115091d37SBarry Smith   x[0]   = b[0];
400215091d37SBarry Smith   for (i=1; i<n; i++) {
400315091d37SBarry Smith     v     =  aa      + ai[i];
400415091d37SBarry Smith     vi    =  aj      + ai[i];
400515091d37SBarry Smith     nz    =  diag[i] - ai[i];
400615091d37SBarry Smith     idx   +=  1;
4007f1af5d2fSBarry Smith     s1  =  b[idx];
400815091d37SBarry Smith     while (nz--) {
400915091d37SBarry Smith       jdx   = *vi++;
401015091d37SBarry Smith       x1    = x[jdx];
4011f1af5d2fSBarry Smith       s1 -= v[0]*x1;
401215091d37SBarry Smith       v    += 1;
401315091d37SBarry Smith     }
4014f1af5d2fSBarry Smith     x[idx]   = s1;
401515091d37SBarry Smith   }
401615091d37SBarry Smith   /* backward solve the upper triangular */
401715091d37SBarry Smith   for (i=n-1; i>=0; i--){
401815091d37SBarry Smith     v    = aa + diag[i] + 1;
401915091d37SBarry Smith     vi   = aj + diag[i] + 1;
402015091d37SBarry Smith     nz   = ai[i+1] - diag[i] - 1;
402115091d37SBarry Smith     idt  = i;
4022f1af5d2fSBarry Smith     s1 = x[idt];
402315091d37SBarry Smith     while (nz--) {
402415091d37SBarry Smith       idx   = *vi++;
402515091d37SBarry Smith       x1    = x[idx];
4026f1af5d2fSBarry Smith       s1 -= v[0]*x1;
402715091d37SBarry Smith       v    += 1;
402815091d37SBarry Smith     }
402915091d37SBarry Smith     v        = aa +  diag[i];
4030f1af5d2fSBarry Smith     x[idt]   = v[0]*s1;
403115091d37SBarry Smith   }
40321ebc52fbSHong Zhang   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
40331ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
4034dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*(a->nz) - A->cmap->n);CHKERRQ(ierr);
403515091d37SBarry Smith   PetscFunctionReturn(0);
403615091d37SBarry Smith }
40374e2b4712SSatish Balay 
40384e2b4712SSatish Balay /* ----------------------------------------------------------------*/
403916a2bf60SHong Zhang EXTERN PetscErrorCode MatDuplicateNoCreate_SeqBAIJ(Mat,Mat,MatDuplicateOption,PetscTruth);
40406bce7ff8SHong Zhang EXTERN PetscErrorCode MatSeqBAIJSetNumericFactorization(Mat,PetscTruth);
40416bce7ff8SHong Zhang 
404284a281e5SHong Zhang extern PetscErrorCode MatSolve_SeqBAIJ_N_NaturalOrdering_newdatastruct(Mat,Vec,Vec);
40438f690400SShri Abhyankar extern PetscErrorCode MatSolve_SeqBAIJ_N_newdatastruct(Mat,Vec,Vec);
40448f690400SShri Abhyankar 
40456bce7ff8SHong Zhang #undef __FUNCT__
40466bce7ff8SHong Zhang #define __FUNCT__ "MatLUFactorNumeric_SeqBAIJ_N_newdatastruct"
40476bce7ff8SHong Zhang PetscErrorCode MatLUFactorNumeric_SeqBAIJ_N_newdatastruct(Mat B,Mat A,const MatFactorInfo *info)
40486bce7ff8SHong Zhang {
40496bce7ff8SHong Zhang   Mat            C=B;
40506bce7ff8SHong Zhang   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ *)C->data;
40516bce7ff8SHong Zhang   IS             isrow = b->row,isicol = b->icol;
40526bce7ff8SHong Zhang   PetscErrorCode ierr;
40536bce7ff8SHong Zhang   const PetscInt *r,*ic,*ics;
40546bce7ff8SHong Zhang   PetscInt       i,j,k,n=a->mbs,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
40556bce7ff8SHong Zhang   PetscInt       *ajtmp,*bjtmp,nz,nzL,row,*bdiag=b->diag,*pj;
4056*b588c5a2SHong Zhang   MatScalar      *rtmp,*pc,*mwork,*v,*pv,*aa=a->a;
4057914a18a2SHong Zhang   PetscInt       bs=A->rmap->bs,bs2 = a->bs2,*v_pivots,flg;
4058914a18a2SHong Zhang   MatScalar      *v_work;
40596bce7ff8SHong Zhang 
40606bce7ff8SHong Zhang   PetscFunctionBegin;
40616bce7ff8SHong Zhang   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
40626bce7ff8SHong Zhang   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
4063914a18a2SHong Zhang   ierr = PetscMalloc((bs2*n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr);
4064914a18a2SHong Zhang   ierr = PetscMemzero(rtmp,(bs2*n+1)*sizeof(MatScalar));CHKERRQ(ierr);
40656bce7ff8SHong Zhang   ics  = ic;
40666bce7ff8SHong Zhang 
4067914a18a2SHong Zhang   /* generate work space needed by dense LU factorization */
4068914a18a2SHong Zhang   ierr     = PetscMalloc(bs*sizeof(PetscInt) + (bs+bs2)*sizeof(MatScalar),&v_work);CHKERRQ(ierr);
4069*b588c5a2SHong Zhang   mwork    = v_work + bs;
4070*b588c5a2SHong Zhang   v_pivots = (PetscInt*)(mwork + bs2);
4071914a18a2SHong Zhang 
40726bce7ff8SHong Zhang   for (i=0; i<n; i++){
40736bce7ff8SHong Zhang     /* zero rtmp */
40746bce7ff8SHong Zhang     /* L part */
40756bce7ff8SHong Zhang     nz    = bi[i+1] - bi[i];
40766bce7ff8SHong Zhang     bjtmp = bj + bi[i];
4077914a18a2SHong Zhang     for  (j=0; j<nz; j++){
4078914a18a2SHong Zhang       ierr = PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));CHKERRQ(ierr);
4079914a18a2SHong Zhang     }
40806bce7ff8SHong Zhang 
40816bce7ff8SHong Zhang     /* U part */
40826bce7ff8SHong Zhang     nz = bi[2*n-i+1] - bi[2*n-i];
40836bce7ff8SHong Zhang     bjtmp = bj + bi[2*n-i];
4084914a18a2SHong Zhang     for  (j=0; j<nz; j++){
4085914a18a2SHong Zhang       ierr = PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));CHKERRQ(ierr);
4086914a18a2SHong Zhang     }
40876bce7ff8SHong Zhang 
40886bce7ff8SHong Zhang     /* load in initial (unfactored row) */
40896bce7ff8SHong Zhang     nz    = ai[r[i]+1] - ai[r[i]];
40906bce7ff8SHong Zhang     ajtmp = aj + ai[r[i]];
4091914a18a2SHong Zhang     v     = aa + bs2*ai[r[i]];
40926bce7ff8SHong Zhang     for (j=0; j<nz; j++) {
4093914a18a2SHong Zhang       ierr = PetscMemcpy(rtmp+bs2*ic[ajtmp[j]],v+bs2*j,bs2*sizeof(MatScalar));CHKERRQ(ierr);
40946bce7ff8SHong Zhang     }
40956bce7ff8SHong Zhang 
40966bce7ff8SHong Zhang     /* elimination */
40976bce7ff8SHong Zhang     bjtmp = bj + bi[i];
40986bce7ff8SHong Zhang     row   = *bjtmp++;
40996bce7ff8SHong Zhang     nzL   = bi[i+1] - bi[i];
41006bce7ff8SHong Zhang     k   = 0;
41016bce7ff8SHong Zhang     while  (k < nzL) {
4102914a18a2SHong Zhang       pc = rtmp + bs2*row;
4103914a18a2SHong Zhang       for (flg=0,j=0; j<bs2; j++) { if (pc[j]!=0.0) { flg = 1; break; }}
4104914a18a2SHong Zhang       if (flg) {
4105914a18a2SHong Zhang         pv         = b->a + bs2*bdiag[row];
4106*b588c5a2SHong Zhang         Kernel_A_gets_A_times_B(bs,pc,pv,mwork); /* *pc = *pc * (*pv); */
41076bce7ff8SHong Zhang         pj         = b->j + bi[2*n-row]; /* begining of U(row,:) */
4108914a18a2SHong Zhang         pv         = b->a + bs2*bi[2*n-row];
41096bce7ff8SHong Zhang         nz         = bi[2*n-row+1] - bi[2*n-row] - 1; /* num of entries inU(row,:), excluding diag */
4110914a18a2SHong Zhang         for (j=0; j<nz; j++) {
4111914a18a2SHong Zhang           Kernel_A_gets_A_minus_B_times_C(bs,rtmp+bs2*pj[j],pc,pv+bs2*j);
4112914a18a2SHong Zhang         }
4113*b588c5a2SHong Zhang         ierr = PetscLogFlops(2*bs2*bs*(nz+1)-bs2);CHKERRQ(ierr); /* flops = 2*bs^3*nz + 2*bs^3 - bs2) */
41146bce7ff8SHong Zhang       }
41156bce7ff8SHong Zhang       row = *bjtmp++; k++;
41166bce7ff8SHong Zhang     }
41176bce7ff8SHong Zhang 
41186bce7ff8SHong Zhang     /* finished row so stick it into b->a */
41196bce7ff8SHong Zhang     /* L part */
4120914a18a2SHong Zhang     pv   = b->a + bs2*bi[i] ;
41216bce7ff8SHong Zhang     pj   = b->j + bi[i] ;
41226bce7ff8SHong Zhang     nz   = bi[i+1] - bi[i];
41236bce7ff8SHong Zhang     for (j=0; j<nz; j++) {
4124914a18a2SHong Zhang       ierr = PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));CHKERRQ(ierr);
41256bce7ff8SHong Zhang     }
41266bce7ff8SHong Zhang 
41276bce7ff8SHong Zhang     /* Mark diagonal and invert diagonal for simplier triangular solves */
4128914a18a2SHong Zhang     pv  = b->a + bs2*bdiag[i];
41296bce7ff8SHong Zhang     pj  = b->j + bdiag[i];
4130914a18a2SHong Zhang     /* if (*pj != i)SETERRQ2(PETSC_ERR_SUP,"row %d != *pj %d",i,*pj); */
4131914a18a2SHong Zhang     ierr = PetscMemcpy(pv,rtmp+bs2*pj[0],bs2*sizeof(MatScalar));CHKERRQ(ierr);
4132914a18a2SHong Zhang     ierr = Kernel_A_gets_inverse_A(bs,pv,v_pivots,v_work);CHKERRQ(ierr);
41336bce7ff8SHong Zhang 
41346bce7ff8SHong Zhang     /* U part */
4135914a18a2SHong Zhang     pv = b->a + bs2*bi[2*n-i];
41366bce7ff8SHong Zhang     pj = b->j + bi[2*n-i];
41376bce7ff8SHong Zhang     nz = bi[2*n-i+1] - bi[2*n-i] - 1;
4138914a18a2SHong Zhang     for (j=0; j<nz; j++){
4139914a18a2SHong Zhang       ierr = PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));CHKERRQ(ierr);
4140914a18a2SHong Zhang     }
41416bce7ff8SHong Zhang   }
41426bce7ff8SHong Zhang 
41436bce7ff8SHong Zhang   ierr = PetscFree(rtmp);CHKERRQ(ierr);
41446bce7ff8SHong Zhang   ierr = PetscFree(v_work);CHKERRQ(ierr);
41456bce7ff8SHong Zhang   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
41466bce7ff8SHong Zhang   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
414727019359SHong Zhang 
41486bce7ff8SHong Zhang   C->assembled = PETSC_TRUE;
4149914a18a2SHong Zhang   ierr = PetscLogFlops(1.3333*bs*bs2*b->mbs);CHKERRQ(ierr); /* from inverting diagonal blocks */
41506bce7ff8SHong Zhang   PetscFunctionReturn(0);
41516bce7ff8SHong Zhang }
41526bce7ff8SHong Zhang 
41536bce7ff8SHong Zhang /*
41546bce7ff8SHong Zhang    ilu(0) with natural ordering under new data structure.
415516a2bf60SHong Zhang    See MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct() for detailed description
415616a2bf60SHong Zhang    because this code is almost identical to MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct().
41576bce7ff8SHong Zhang */
41586bce7ff8SHong Zhang #undef __FUNCT__
41596bce7ff8SHong Zhang #define __FUNCT__ "MatILUFactorSymbolic_SeqBAIJ_ilu0_newdatastruct"
41606bce7ff8SHong Zhang PetscErrorCode MatILUFactorSymbolic_SeqBAIJ_ilu0_newdatastruct(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
41616bce7ff8SHong Zhang {
41626bce7ff8SHong Zhang 
41636bce7ff8SHong Zhang   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data,*b;
41646bce7ff8SHong Zhang   PetscErrorCode     ierr;
416516a2bf60SHong Zhang   PetscInt           n=a->mbs,*ai=a->i,*aj,*adiag=a->diag,bs2 = a->bs2;
416616a2bf60SHong Zhang   PetscInt           i,j,nz,*bi,*bj,*bdiag;
41676bce7ff8SHong Zhang 
41686bce7ff8SHong Zhang   PetscFunctionBegin;
416916a2bf60SHong Zhang   /* printf("MatILUFactorSymbolic_SeqBAIJ_ilu0_newdatastruct...\n"); */
417016a2bf60SHong Zhang   ierr = MatDuplicateNoCreate_SeqBAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_FALSE);CHKERRQ(ierr);
41716bce7ff8SHong Zhang   b    = (Mat_SeqBAIJ*)(fact)->data;
417216a2bf60SHong Zhang 
417316a2bf60SHong Zhang   /* allocate matrix arrays for new data structure */
417416a2bf60SHong Zhang   ierr = PetscMalloc3(bs2*ai[n]+1,PetscScalar,&b->a,ai[n]+1,PetscInt,&b->j,2*n+2,PetscInt,&b->i);CHKERRQ(ierr);
417516a2bf60SHong Zhang   ierr = PetscLogObjectMemory(fact,ai[n]*(bs2*sizeof(PetscScalar)+sizeof(PetscInt))+(2*n+2)*sizeof(PetscInt));CHKERRQ(ierr);
417616a2bf60SHong Zhang   b->singlemalloc = PETSC_TRUE;
417716a2bf60SHong Zhang   if (!b->diag){
417816a2bf60SHong Zhang     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&b->diag);CHKERRQ(ierr);
417916a2bf60SHong Zhang   }
4180914a18a2SHong Zhang   bdiag = b->diag;
41816bce7ff8SHong Zhang 
418216a2bf60SHong Zhang   if (n > 0) {
418316a2bf60SHong Zhang     ierr = PetscMemzero(b->a,bs2*ai[n]*sizeof(MatScalar));CHKERRQ(ierr);
41846bce7ff8SHong Zhang   }
41856bce7ff8SHong Zhang 
41866bce7ff8SHong Zhang   /* set bi and bj with new data structure */
41876bce7ff8SHong Zhang   bi = b->i;
41886bce7ff8SHong Zhang   bj = b->j;
41896bce7ff8SHong Zhang 
41906bce7ff8SHong Zhang   /* L part */
41916bce7ff8SHong Zhang   bi[0] = 0;
419216a2bf60SHong Zhang   for (i=0; i<n; i++){
41936bce7ff8SHong Zhang     nz = adiag[i] - ai[i];
4194914a18a2SHong Zhang     bi[i+1] = bi[i] + nz;
41956bce7ff8SHong Zhang     aj = a->j + ai[i];
41966bce7ff8SHong Zhang     for (j=0; j<nz; j++){
41976bce7ff8SHong Zhang       *bj = aj[j]; bj++;
41986bce7ff8SHong Zhang     }
41996bce7ff8SHong Zhang   }
42006bce7ff8SHong Zhang 
42016bce7ff8SHong Zhang   /* U part */
420216a2bf60SHong Zhang   bi[n+1] = bi[n];
420316a2bf60SHong Zhang   for (i=n-1; i>=0; i--){
42046bce7ff8SHong Zhang     nz = ai[i+1] - adiag[i] - 1;
420516a2bf60SHong Zhang     bi[2*n-i+1] = bi[2*n-i] + nz + 1;
42066bce7ff8SHong Zhang     aj = a->j + adiag[i] + 1;
42076bce7ff8SHong Zhang     for (j=0; j<nz; j++){
42086bce7ff8SHong Zhang       *bj = aj[j]; bj++;
42096bce7ff8SHong Zhang     }
42106bce7ff8SHong Zhang     /* diag[i] */
42116bce7ff8SHong Zhang     *bj = i; bj++;
421216a2bf60SHong Zhang     bdiag[i] = bi[2*n-i+1]-1;
42136bce7ff8SHong Zhang   }
42146bce7ff8SHong Zhang   PetscFunctionReturn(0);
42156bce7ff8SHong Zhang }
42166bce7ff8SHong Zhang 
421716a2bf60SHong Zhang #undef __FUNCT__
421816a2bf60SHong Zhang #define __FUNCT__ "MatILUFactorSymbolic_SeqBAIJ_newdatastruct"
421916a2bf60SHong Zhang PetscErrorCode MatILUFactorSymbolic_SeqBAIJ_newdatastruct(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
422016a2bf60SHong Zhang {
422116a2bf60SHong Zhang   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data,*b;
422216a2bf60SHong Zhang   IS                 isicol;
422316a2bf60SHong Zhang   PetscErrorCode     ierr;
422416a2bf60SHong Zhang   const PetscInt     *r,*ic;
42257fa3a6a0SHong Zhang   PetscInt           n=a->mbs,*ai=a->i,*aj=a->j,d;
422616a2bf60SHong Zhang   PetscInt           *bi,*cols,nnz,*cols_lvl;
422716a2bf60SHong Zhang   PetscInt           *bdiag,prow,fm,nzbd,reallocs=0,dcount=0;
422816a2bf60SHong Zhang   PetscInt           i,levels,diagonal_fill;
42297fa3a6a0SHong Zhang   PetscTruth         col_identity,row_identity,both_identity;
423016a2bf60SHong Zhang   PetscReal          f;
423116a2bf60SHong Zhang   PetscInt           nlnk,*lnk,*lnk_lvl=PETSC_NULL;
423216a2bf60SHong Zhang   PetscBT            lnkbt;
423316a2bf60SHong Zhang   PetscInt           nzi,*bj,**bj_ptr,**bjlvl_ptr;
423416a2bf60SHong Zhang   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
423516a2bf60SHong Zhang   PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL;
423616a2bf60SHong Zhang   PetscTruth         missing;
42377fa3a6a0SHong Zhang   PetscInt           bs=A->rmap->bs,bs2=a->bs2;
423816a2bf60SHong Zhang 
423916a2bf60SHong Zhang   PetscFunctionBegin;
424016a2bf60SHong Zhang   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
424116a2bf60SHong Zhang   ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr);
424216a2bf60SHong Zhang   if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
424316a2bf60SHong Zhang 
424416a2bf60SHong Zhang   f             = info->fill;
424516a2bf60SHong Zhang   levels        = (PetscInt)info->levels;
424616a2bf60SHong Zhang   diagonal_fill = (PetscInt)info->diagonal_fill;
424716a2bf60SHong Zhang   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
424816a2bf60SHong Zhang 
424916a2bf60SHong Zhang   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
425016a2bf60SHong Zhang   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
42517fa3a6a0SHong Zhang   both_identity = (PetscTruth) (row_identity && col_identity);
425216a2bf60SHong Zhang 
42537fa3a6a0SHong Zhang   if (!levels && both_identity) {
425416a2bf60SHong Zhang     /* special case: ilu(0) with natural ordering */
425516a2bf60SHong Zhang     ierr = MatILUFactorSymbolic_SeqBAIJ_ilu0_newdatastruct(fact,A,isrow,iscol,info);CHKERRQ(ierr);
425616a2bf60SHong Zhang     (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqBAIJ_N_newdatastruct;
42577fa3a6a0SHong Zhang     /* set MatSolve routines */
42587fa3a6a0SHong Zhang     switch (bs){
42597fa3a6a0SHong Zhang     case 2:
42607fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_2_NaturalOrdering_newdatastruct;
42617fa3a6a0SHong Zhang       break;
42627fa3a6a0SHong Zhang     case 3:
42637fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_3_NaturalOrdering_newdatastruct;
42647fa3a6a0SHong Zhang       break;
42657fa3a6a0SHong Zhang     case 4:
42667fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_4_NaturalOrdering_newdatastruct;
42677fa3a6a0SHong Zhang       break;
42687fa3a6a0SHong Zhang     case 5:
42697fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_5_NaturalOrdering_newdatastruct;
42707fa3a6a0SHong Zhang       break;
42717fa3a6a0SHong Zhang     case 6:
42727fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_6_NaturalOrdering_newdatastruct;
42737fa3a6a0SHong Zhang       break;
42747fa3a6a0SHong Zhang     case 7:
42757fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_7_NaturalOrdering_newdatastruct;
42767fa3a6a0SHong Zhang       break;
42777fa3a6a0SHong Zhang     default:
42787fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_N_NaturalOrdering_newdatastruct;
42797fa3a6a0SHong Zhang       break;
42807fa3a6a0SHong Zhang     }
428116a2bf60SHong Zhang 
428216a2bf60SHong Zhang     fact->factor = MAT_FACTOR_ILU;
428316a2bf60SHong Zhang     (fact)->info.factor_mallocs    = 0;
428416a2bf60SHong Zhang     (fact)->info.fill_ratio_given  = info->fill;
428516a2bf60SHong Zhang     (fact)->info.fill_ratio_needed = 1.0;
428616a2bf60SHong Zhang     b                = (Mat_SeqBAIJ*)(fact)->data;
428716a2bf60SHong Zhang     b->row           = isrow;
428816a2bf60SHong Zhang     b->col           = iscol;
428916a2bf60SHong Zhang     b->icol          = isicol;
429016a2bf60SHong Zhang     ierr             = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
429116a2bf60SHong Zhang     ierr             = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
429216a2bf60SHong Zhang     b->pivotinblocks = (info->pivotinblocks) ? PETSC_TRUE : PETSC_FALSE;
4293*b588c5a2SHong Zhang     ierr = PetscMalloc((n+1)*bs*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
429416a2bf60SHong Zhang     PetscFunctionReturn(0);
429516a2bf60SHong Zhang   }
429616a2bf60SHong Zhang 
429716a2bf60SHong Zhang   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
429816a2bf60SHong Zhang   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
429916a2bf60SHong Zhang 
430016a2bf60SHong Zhang   /* get new row pointers */
430116a2bf60SHong Zhang   ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
430216a2bf60SHong Zhang   bi[0] = 0;
430316a2bf60SHong Zhang   /* bdiag is location of diagonal in factor */
430416a2bf60SHong Zhang   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr);
430516a2bf60SHong Zhang   bdiag[0]  = 0;
430616a2bf60SHong Zhang 
430716a2bf60SHong Zhang   ierr = PetscMalloc((2*n+1)*sizeof(PetscInt**),&bj_ptr);CHKERRQ(ierr);
430816a2bf60SHong Zhang   bjlvl_ptr = (PetscInt**)(bj_ptr + n);
430916a2bf60SHong Zhang 
431016a2bf60SHong Zhang   /* create a linked list for storing column indices of the active row */
431116a2bf60SHong Zhang   nlnk = n + 1;
431216a2bf60SHong Zhang   ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
431316a2bf60SHong Zhang 
431416a2bf60SHong Zhang   /* initial FreeSpace size is f*(ai[n]+1) */
431516a2bf60SHong Zhang   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr);
431616a2bf60SHong Zhang   current_space = free_space;
431716a2bf60SHong Zhang   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr);
431816a2bf60SHong Zhang   current_space_lvl = free_space_lvl;
431916a2bf60SHong Zhang 
432016a2bf60SHong Zhang   for (i=0; i<n; i++) {
432116a2bf60SHong Zhang     nzi = 0;
432216a2bf60SHong Zhang     /* copy current row into linked list */
432316a2bf60SHong Zhang     nnz  = ai[r[i]+1] - ai[r[i]];
432416a2bf60SHong Zhang     if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
432516a2bf60SHong Zhang     cols = aj + ai[r[i]];
432616a2bf60SHong Zhang     lnk[i] = -1; /* marker to indicate if diagonal exists */
432716a2bf60SHong Zhang     ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
432816a2bf60SHong Zhang     nzi += nlnk;
432916a2bf60SHong Zhang 
433016a2bf60SHong Zhang     /* make sure diagonal entry is included */
433116a2bf60SHong Zhang     if (diagonal_fill && lnk[i] == -1) {
433216a2bf60SHong Zhang       fm = n;
433316a2bf60SHong Zhang       while (lnk[fm] < i) fm = lnk[fm];
433416a2bf60SHong Zhang       lnk[i]     = lnk[fm]; /* insert diagonal into linked list */
433516a2bf60SHong Zhang       lnk[fm]    = i;
433616a2bf60SHong Zhang       lnk_lvl[i] = 0;
433716a2bf60SHong Zhang       nzi++; dcount++;
433816a2bf60SHong Zhang     }
433916a2bf60SHong Zhang 
434016a2bf60SHong Zhang     /* add pivot rows into the active row */
434116a2bf60SHong Zhang     nzbd = 0;
434216a2bf60SHong Zhang     prow = lnk[n];
434316a2bf60SHong Zhang     while (prow < i) {
434416a2bf60SHong Zhang       nnz      = bdiag[prow];
434516a2bf60SHong Zhang       cols     = bj_ptr[prow] + nnz + 1;
434616a2bf60SHong Zhang       cols_lvl = bjlvl_ptr[prow] + nnz + 1;
434716a2bf60SHong Zhang       nnz      = bi[prow+1] - bi[prow] - nnz - 1;
434816a2bf60SHong Zhang       ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr);
434916a2bf60SHong Zhang       nzi += nlnk;
435016a2bf60SHong Zhang       prow = lnk[prow];
435116a2bf60SHong Zhang       nzbd++;
435216a2bf60SHong Zhang     }
435316a2bf60SHong Zhang     bdiag[i] = nzbd;
435416a2bf60SHong Zhang     bi[i+1]  = bi[i] + nzi;
435516a2bf60SHong Zhang 
435616a2bf60SHong Zhang     /* if free space is not available, make more free space */
435716a2bf60SHong Zhang     if (current_space->local_remaining<nzi) {
435816a2bf60SHong Zhang       nnz = 2*nzi*(n - i); /* estimated and max additional space needed */
435916a2bf60SHong Zhang       ierr = PetscFreeSpaceGet(nnz,&current_space);CHKERRQ(ierr);
436016a2bf60SHong Zhang       ierr = PetscFreeSpaceGet(nnz,&current_space_lvl);CHKERRQ(ierr);
436116a2bf60SHong Zhang       reallocs++;
436216a2bf60SHong Zhang     }
436316a2bf60SHong Zhang 
436416a2bf60SHong Zhang     /* copy data into free_space and free_space_lvl, then initialize lnk */
436516a2bf60SHong Zhang     ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr);
436616a2bf60SHong Zhang     bj_ptr[i]    = current_space->array;
436716a2bf60SHong Zhang     bjlvl_ptr[i] = current_space_lvl->array;
436816a2bf60SHong Zhang 
436916a2bf60SHong Zhang     /* make sure the active row i has diagonal entry */
437016a2bf60SHong Zhang     if (*(bj_ptr[i]+bdiag[i]) != i) {
437116a2bf60SHong Zhang       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\
437216a2bf60SHong Zhang     try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i);
437316a2bf60SHong Zhang     }
437416a2bf60SHong Zhang 
437516a2bf60SHong Zhang     current_space->array           += nzi;
437616a2bf60SHong Zhang     current_space->local_used      += nzi;
437716a2bf60SHong Zhang     current_space->local_remaining -= nzi;
437816a2bf60SHong Zhang     current_space_lvl->array           += nzi;
437916a2bf60SHong Zhang     current_space_lvl->local_used      += nzi;
438016a2bf60SHong Zhang     current_space_lvl->local_remaining -= nzi;
438116a2bf60SHong Zhang   }
438216a2bf60SHong Zhang 
438316a2bf60SHong Zhang   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
438416a2bf60SHong Zhang   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
438516a2bf60SHong Zhang 
438616a2bf60SHong Zhang   /* destroy list of free space and other temporary arrays */
438716a2bf60SHong Zhang   ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
438816a2bf60SHong Zhang 
438916a2bf60SHong Zhang   /* copy free_space into bj and free free_space; set bi, bj, bdiag in new datastructure; */
4390783ef271SHong Zhang   ierr = PetscFreeSpaceContiguous_LU(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr);
439116a2bf60SHong Zhang 
439216a2bf60SHong Zhang   ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
439316a2bf60SHong Zhang   ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr);
439416a2bf60SHong Zhang   ierr = PetscFree(bj_ptr);CHKERRQ(ierr);
439516a2bf60SHong Zhang 
439616a2bf60SHong Zhang #if defined(PETSC_USE_INFO)
439716a2bf60SHong Zhang   {
439816a2bf60SHong Zhang     PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]);
439916a2bf60SHong Zhang     ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr);
440016a2bf60SHong Zhang     ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
440116a2bf60SHong Zhang     ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr);
440216a2bf60SHong Zhang     ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr);
440316a2bf60SHong Zhang     if (diagonal_fill) {
440416a2bf60SHong Zhang       ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr);
440516a2bf60SHong Zhang     }
440616a2bf60SHong Zhang   }
440716a2bf60SHong Zhang #endif
440816a2bf60SHong Zhang 
440916a2bf60SHong Zhang   /* put together the new matrix */
441016a2bf60SHong Zhang   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(fact,bs,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
441116a2bf60SHong Zhang   ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr);
441216a2bf60SHong Zhang   b = (Mat_SeqBAIJ*)(fact)->data;
441316a2bf60SHong Zhang   b->free_a       = PETSC_TRUE;
441416a2bf60SHong Zhang   b->free_ij      = PETSC_TRUE;
441516a2bf60SHong Zhang   b->singlemalloc = PETSC_FALSE;
44167fa3a6a0SHong Zhang   ierr = PetscMalloc( (bs2*bi[2*n+1] )*sizeof(MatScalar),&b->a);CHKERRQ(ierr);
441716a2bf60SHong Zhang   b->j          = bj;
441816a2bf60SHong Zhang   b->i          = bi;
441916a2bf60SHong Zhang   b->diag       = bdiag;
442016a2bf60SHong Zhang   b->ilen       = 0;
442116a2bf60SHong Zhang   b->imax       = 0;
442216a2bf60SHong Zhang   b->row        = isrow;
442316a2bf60SHong Zhang   b->col        = iscol;
442416a2bf60SHong Zhang   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
442516a2bf60SHong Zhang   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
442616a2bf60SHong Zhang   b->icol       = isicol;
44277fa3a6a0SHong Zhang   ierr = PetscMalloc((bs*n+bs)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
442816a2bf60SHong Zhang   /* In b structure:  Free imax, ilen, old a, old j.
442916a2bf60SHong Zhang      Allocate bdiag, solve_work, new a, new j */
44307fa3a6a0SHong Zhang   ierr = PetscLogObjectMemory(fact,bi[2*n+1] * (sizeof(PetscInt)+bs2*sizeof(PetscScalar)));CHKERRQ(ierr);
443116a2bf60SHong Zhang   b->maxnz = b->nz = bi[2*n+1] ;
443216a2bf60SHong Zhang   (fact)->info.factor_mallocs    = reallocs;
443316a2bf60SHong Zhang   (fact)->info.fill_ratio_given  = f;
443416a2bf60SHong Zhang   (fact)->info.fill_ratio_needed = ((PetscReal)bi[2*n+1])/((PetscReal)ai[n]);
443516a2bf60SHong Zhang   (fact)->ops->lufactornumeric   = MatLUFactorNumeric_SeqBAIJ_N_newdatastruct;
44367fa3a6a0SHong Zhang   /* set MatSolve routines */
44377fa3a6a0SHong Zhang   if (both_identity){
44387fa3a6a0SHong Zhang     switch (bs){
44397fa3a6a0SHong Zhang     case 2:
44407fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_2_NaturalOrdering_newdatastruct;
44417fa3a6a0SHong Zhang       break;
44427fa3a6a0SHong Zhang     case 3:
44437fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_3_NaturalOrdering_newdatastruct;
44447fa3a6a0SHong Zhang       break;
44457fa3a6a0SHong Zhang     case 4:
44467fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_4_NaturalOrdering_newdatastruct;
44477fa3a6a0SHong Zhang       break;
44487fa3a6a0SHong Zhang     case 5:
44497fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_5_NaturalOrdering_newdatastruct;
44507fa3a6a0SHong Zhang       break;
44517fa3a6a0SHong Zhang     case 6:
44527fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_6_NaturalOrdering_newdatastruct;
44537fa3a6a0SHong Zhang       break;
44547fa3a6a0SHong Zhang     case 7:
44557fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_7_NaturalOrdering_newdatastruct;
44567fa3a6a0SHong Zhang       break;
44577fa3a6a0SHong Zhang     default:
44587fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_N_NaturalOrdering_newdatastruct;
44597fa3a6a0SHong Zhang       break;
44607fa3a6a0SHong Zhang     }
44617fa3a6a0SHong Zhang   } else {
44627fa3a6a0SHong Zhang     switch (bs){
44637fa3a6a0SHong Zhang     case 2:
44647fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_2_newdatastruct;
44657fa3a6a0SHong Zhang       break;
44667fa3a6a0SHong Zhang     case 3:
44677fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_3_newdatastruct;
44687fa3a6a0SHong Zhang       break;
44697fa3a6a0SHong Zhang     case 4:
44707fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_4_newdatastruct;
44717fa3a6a0SHong Zhang       break;
44727fa3a6a0SHong Zhang     case 5:
44737fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_5_newdatastruct;
44747fa3a6a0SHong Zhang       break;
44757fa3a6a0SHong Zhang     case 6:
44767fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_6_newdatastruct;
44777fa3a6a0SHong Zhang       break;
44787fa3a6a0SHong Zhang     case 7:
44797fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_7_newdatastruct;
44807fa3a6a0SHong Zhang       break;
44817fa3a6a0SHong Zhang     default:
44827fa3a6a0SHong Zhang       fact->ops->solve = MatSolve_SeqBAIJ_N_newdatastruct;
44837fa3a6a0SHong Zhang       break;
44847fa3a6a0SHong Zhang     }
44857fa3a6a0SHong Zhang   }
448616a2bf60SHong Zhang   PetscFunctionReturn(0);
448716a2bf60SHong Zhang }
448816a2bf60SHong Zhang 
44894e2b4712SSatish Balay /*
44904e2b4712SSatish Balay      This code is virtually identical to MatILUFactorSymbolic_SeqAIJ
44914e2b4712SSatish Balay    except that the data structure of Mat_SeqAIJ is slightly different.
44924e2b4712SSatish Balay    Not a good example of code reuse.
44934e2b4712SSatish Balay */
44944a2ae208SSatish Balay #undef __FUNCT__
44954a2ae208SSatish Balay #define __FUNCT__ "MatILUFactorSymbolic_SeqBAIJ"
44960481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_SeqBAIJ(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
44974e2b4712SSatish Balay {
44984e2b4712SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data,*b;
44994e2b4712SSatish Balay   IS             isicol;
45006849ba73SBarry Smith   PetscErrorCode ierr;
45015d0c19d7SBarry Smith   const PetscInt *r,*ic,*ai = a->i,*aj = a->j,*xi;
45025d0c19d7SBarry Smith   PetscInt       prow,n = a->mbs,*ainew,*ajnew,jmax,*fill,nz,*im,*ajfill,*flev,*xitmp;
4503a96a251dSBarry Smith   PetscInt       *dloc,idx,row,m,fm,nzf,nzi,reallocate = 0,dcount = 0;
4504d0f46423SBarry Smith   PetscInt       incrlev,nnz,i,bs = A->rmap->bs,bs2 = a->bs2,levels,diagonal_fill,dd;
450541df41f0SMatthew Knepley   PetscTruth     col_identity,row_identity,both_identity,flg;
4506329f5518SBarry Smith   PetscReal      f;
450716a2bf60SHong Zhang   PetscTruth     newdatastruct=PETSC_FALSE;
45084e2b4712SSatish Balay 
45094e2b4712SSatish Balay   PetscFunctionBegin;
451016a2bf60SHong Zhang   ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr);
451116a2bf60SHong Zhang   if (newdatastruct){
451216a2bf60SHong Zhang     ierr = MatILUFactorSymbolic_SeqBAIJ_newdatastruct(fact,A,isrow,iscol,info);CHKERRQ(ierr);
451316a2bf60SHong Zhang     PetscFunctionReturn(0);
451416a2bf60SHong Zhang   }
451516a2bf60SHong Zhang 
45166bce7ff8SHong Zhang   ierr = MatMissingDiagonal_SeqBAIJ(A,&flg,&dd);CHKERRQ(ierr);
45176bce7ff8SHong Zhang   if (flg) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix A is missing diagonal entry in row %D",dd);
45186bce7ff8SHong Zhang 
4519435faa5fSBarry Smith   f             = info->fill;
4520690b6cddSBarry Smith   levels        = (PetscInt)info->levels;
4521690b6cddSBarry Smith   diagonal_fill = (PetscInt)info->diagonal_fill;
45224c49b128SBarry Smith   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
452316a2bf60SHong Zhang 
4524667159a5SBarry Smith   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
4525667159a5SBarry Smith   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
45267d18ce8fSMatthew Knepley   both_identity = (PetscTruth) (row_identity && col_identity);
4527309c388cSBarry Smith 
452841df41f0SMatthew Knepley   if (!levels && both_identity) {  /* special case copy the nonzero structure */
452916a2bf60SHong Zhang     ierr = MatDuplicateNoCreate_SeqBAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
45306bce7ff8SHong Zhang     ierr = MatSeqBAIJSetNumericFactorization(fact,both_identity);CHKERRQ(ierr);
45316bce7ff8SHong Zhang 
4532719d5645SBarry Smith     fact->factor = MAT_FACTOR_ILU;
4533719d5645SBarry Smith     b            = (Mat_SeqBAIJ*)(fact)->data;
4534bb3d539aSBarry Smith     b->row       = isrow;
4535bb3d539aSBarry Smith     b->col       = iscol;
4536bb3d539aSBarry Smith     ierr         = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
4537bb3d539aSBarry Smith     ierr         = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
4538bb3d539aSBarry Smith     b->icol      = isicol;
4539bcd9e38bSBarry Smith     b->pivotinblocks = (info->pivotinblocks) ? PETSC_TRUE : PETSC_FALSE;
4540*b588c5a2SHong Zhang     ierr         = PetscMalloc((n+1)*bs*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
45416bce7ff8SHong Zhang     PetscFunctionReturn(0);
45426bce7ff8SHong Zhang   }
45436bce7ff8SHong Zhang 
45446bce7ff8SHong Zhang   /* general case perform the symbolic factorization */
45454e2b4712SSatish Balay     ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
45464e2b4712SSatish Balay     ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
45474e2b4712SSatish Balay 
45484e2b4712SSatish Balay     /* get new row pointers */
4549690b6cddSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&ainew);CHKERRQ(ierr);
45504e2b4712SSatish Balay     ainew[0] = 0;
45514e2b4712SSatish Balay     /* don't know how many column pointers are needed so estimate */
4552690b6cddSBarry Smith     jmax = (PetscInt)(f*ai[n] + 1);
4553690b6cddSBarry Smith     ierr = PetscMalloc((jmax)*sizeof(PetscInt),&ajnew);CHKERRQ(ierr);
45544e2b4712SSatish Balay     /* ajfill is level of fill for each fill entry */
4555690b6cddSBarry Smith     ierr = PetscMalloc((jmax)*sizeof(PetscInt),&ajfill);CHKERRQ(ierr);
45564e2b4712SSatish Balay     /* fill is a linked list of nonzeros in active row */
4557690b6cddSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&fill);CHKERRQ(ierr);
45584e2b4712SSatish Balay     /* im is level for each filled value */
4559690b6cddSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&im);CHKERRQ(ierr);
45604e2b4712SSatish Balay     /* dloc is location of diagonal in factor */
4561690b6cddSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&dloc);CHKERRQ(ierr);
45624e2b4712SSatish Balay     dloc[0]  = 0;
45634e2b4712SSatish Balay     for (prow=0; prow<n; prow++) {
4564435faa5fSBarry Smith 
4565435faa5fSBarry Smith       /* copy prow into linked list */
45664e2b4712SSatish Balay       nzf        = nz  = ai[r[prow]+1] - ai[r[prow]];
45673b4a8b6dSBarry Smith       if (!nz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[prow],prow);
45684e2b4712SSatish Balay       xi         = aj + ai[r[prow]];
45694e2b4712SSatish Balay       fill[n]    = n;
4570435faa5fSBarry Smith       fill[prow] = -1; /* marker for diagonal entry */
45714e2b4712SSatish Balay       while (nz--) {
45724e2b4712SSatish Balay 	fm  = n;
45734e2b4712SSatish Balay 	idx = ic[*xi++];
45744e2b4712SSatish Balay 	do {
45754e2b4712SSatish Balay 	  m  = fm;
45764e2b4712SSatish Balay 	  fm = fill[m];
45774e2b4712SSatish Balay 	} while (fm < idx);
45784e2b4712SSatish Balay 	fill[m]   = idx;
45794e2b4712SSatish Balay 	fill[idx] = fm;
45804e2b4712SSatish Balay 	im[idx]   = 0;
45814e2b4712SSatish Balay       }
4582435faa5fSBarry Smith 
4583435faa5fSBarry Smith       /* make sure diagonal entry is included */
4584435faa5fSBarry Smith       if (diagonal_fill && fill[prow] == -1) {
4585435faa5fSBarry Smith 	fm = n;
4586435faa5fSBarry Smith 	while (fill[fm] < prow) fm = fill[fm];
4587435faa5fSBarry Smith 	fill[prow] = fill[fm];  /* insert diagonal into linked list */
4588435faa5fSBarry Smith 	fill[fm]   = prow;
4589435faa5fSBarry Smith 	im[prow]   = 0;
4590435faa5fSBarry Smith 	nzf++;
4591335d9088SBarry Smith 	dcount++;
4592435faa5fSBarry Smith       }
4593435faa5fSBarry Smith 
45944e2b4712SSatish Balay       nzi = 0;
45954e2b4712SSatish Balay       row = fill[n];
45964e2b4712SSatish Balay       while (row < prow) {
45974e2b4712SSatish Balay 	incrlev = im[row] + 1;
45984e2b4712SSatish Balay 	nz      = dloc[row];
4599435faa5fSBarry Smith 	xi      = ajnew  + ainew[row] + nz + 1;
46004e2b4712SSatish Balay 	flev    = ajfill + ainew[row] + nz + 1;
46014e2b4712SSatish Balay 	nnz     = ainew[row+1] - ainew[row] - nz - 1;
46024e2b4712SSatish Balay 	fm      = row;
46034e2b4712SSatish Balay 	while (nnz-- > 0) {
46044e2b4712SSatish Balay 	  idx = *xi++;
46054e2b4712SSatish Balay 	  if (*flev + incrlev > levels) {
46064e2b4712SSatish Balay 	    flev++;
46074e2b4712SSatish Balay 	    continue;
46084e2b4712SSatish Balay 	  }
46094e2b4712SSatish Balay 	  do {
46104e2b4712SSatish Balay 	    m  = fm;
46114e2b4712SSatish Balay 	    fm = fill[m];
46124e2b4712SSatish Balay 	  } while (fm < idx);
46134e2b4712SSatish Balay 	  if (fm != idx) {
46144e2b4712SSatish Balay 	    im[idx]   = *flev + incrlev;
46154e2b4712SSatish Balay 	    fill[m]   = idx;
46164e2b4712SSatish Balay 	    fill[idx] = fm;
46174e2b4712SSatish Balay 	    fm        = idx;
46184e2b4712SSatish Balay 	    nzf++;
4619ecf371e4SBarry Smith 	  } else {
46204e2b4712SSatish Balay 	    if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
46214e2b4712SSatish Balay 	  }
46224e2b4712SSatish Balay 	  flev++;
46234e2b4712SSatish Balay 	}
46244e2b4712SSatish Balay 	row = fill[row];
46254e2b4712SSatish Balay 	nzi++;
46264e2b4712SSatish Balay       }
46274e2b4712SSatish Balay       /* copy new filled row into permanent storage */
46284e2b4712SSatish Balay       ainew[prow+1] = ainew[prow] + nzf;
46294e2b4712SSatish Balay       if (ainew[prow+1] > jmax) {
4630ecf371e4SBarry Smith 
4631ecf371e4SBarry Smith 	/* estimate how much additional space we will need */
4632ecf371e4SBarry Smith 	/* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
4633ecf371e4SBarry Smith 	/* just double the memory each time */
4634690b6cddSBarry Smith 	PetscInt maxadd = jmax;
4635ecf371e4SBarry Smith 	/* maxadd = (int)(((f*ai[n]+1)*(n-prow+5))/n); */
46364e2b4712SSatish Balay 	if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
46374e2b4712SSatish Balay 	jmax += maxadd;
4638ecf371e4SBarry Smith 
4639ecf371e4SBarry Smith 	/* allocate a longer ajnew and ajfill */
46405d0c19d7SBarry Smith 	ierr = PetscMalloc(jmax*sizeof(PetscInt),&xitmp);CHKERRQ(ierr);
46415d0c19d7SBarry Smith 	ierr = PetscMemcpy(xitmp,ajnew,ainew[prow]*sizeof(PetscInt));CHKERRQ(ierr);
4642606d414cSSatish Balay 	ierr = PetscFree(ajnew);CHKERRQ(ierr);
46435d0c19d7SBarry Smith 	ajnew = xitmp;
46445d0c19d7SBarry Smith 	ierr = PetscMalloc(jmax*sizeof(PetscInt),&xitmp);CHKERRQ(ierr);
46455d0c19d7SBarry Smith 	ierr = PetscMemcpy(xitmp,ajfill,ainew[prow]*sizeof(PetscInt));CHKERRQ(ierr);
4646606d414cSSatish Balay 	ierr = PetscFree(ajfill);CHKERRQ(ierr);
46475d0c19d7SBarry Smith 	ajfill = xitmp;
4648eb150c5cSKris Buschelman 	reallocate++; /* count how many reallocations are needed */
46494e2b4712SSatish Balay       }
46505d0c19d7SBarry Smith       xitmp       = ajnew + ainew[prow];
46514e2b4712SSatish Balay       flev        = ajfill + ainew[prow];
46524e2b4712SSatish Balay       dloc[prow]  = nzi;
46534e2b4712SSatish Balay       fm          = fill[n];
46544e2b4712SSatish Balay       while (nzf--) {
46555d0c19d7SBarry Smith 	*xitmp++ = fm;
46564e2b4712SSatish Balay 	*flev++ = im[fm];
46574e2b4712SSatish Balay 	fm      = fill[fm];
46584e2b4712SSatish Balay       }
4659435faa5fSBarry Smith       /* make sure row has diagonal entry */
4660435faa5fSBarry Smith       if (ajnew[ainew[prow]+dloc[prow]] != prow) {
466177431f27SBarry Smith 	SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\
46622401956bSBarry Smith     try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",prow);
4663435faa5fSBarry Smith       }
46644e2b4712SSatish Balay     }
4665606d414cSSatish Balay     ierr = PetscFree(ajfill);CHKERRQ(ierr);
46664e2b4712SSatish Balay     ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
46674e2b4712SSatish Balay     ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
4668606d414cSSatish Balay     ierr = PetscFree(fill);CHKERRQ(ierr);
4669606d414cSSatish Balay     ierr = PetscFree(im);CHKERRQ(ierr);
46704e2b4712SSatish Balay 
46716cf91177SBarry Smith #if defined(PETSC_USE_INFO)
46724e2b4712SSatish Balay     {
4673329f5518SBarry Smith       PetscReal af = ((PetscReal)ainew[n])/((PetscReal)ai[n]);
4674ae15b995SBarry Smith       ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocate,f,af);CHKERRQ(ierr);
4675ae15b995SBarry Smith       ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
4676ae15b995SBarry Smith       ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G);\n",af);CHKERRQ(ierr);
4677ae15b995SBarry Smith       ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr);
4678335d9088SBarry Smith       if (diagonal_fill) {
4679ae15b995SBarry Smith 	ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals\n",dcount);CHKERRQ(ierr);
4680335d9088SBarry Smith       }
46814e2b4712SSatish Balay     }
468263ba0a88SBarry Smith #endif
46834e2b4712SSatish Balay 
46844e2b4712SSatish Balay     /* put together the new matrix */
4685719d5645SBarry Smith     ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(fact,bs,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
4686719d5645SBarry Smith     ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr);
4687719d5645SBarry Smith     b    = (Mat_SeqBAIJ*)(fact)->data;
4688e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
4689e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
46907c922b88SBarry Smith     b->singlemalloc = PETSC_FALSE;
4691a96a251dSBarry Smith     ierr = PetscMalloc(bs2*ainew[n]*sizeof(MatScalar),&b->a);CHKERRQ(ierr);
46924e2b4712SSatish Balay     b->j          = ajnew;
46934e2b4712SSatish Balay     b->i          = ainew;
46944e2b4712SSatish Balay     for (i=0; i<n; i++) dloc[i] += ainew[i];
46954e2b4712SSatish Balay     b->diag       = dloc;
46964e2b4712SSatish Balay     b->ilen       = 0;
46974e2b4712SSatish Balay     b->imax       = 0;
46984e2b4712SSatish Balay     b->row        = isrow;
46994e2b4712SSatish Balay     b->col        = iscol;
4700bcd9e38bSBarry Smith     b->pivotinblocks = (info->pivotinblocks) ? PETSC_TRUE : PETSC_FALSE;
4701c38d4ed2SBarry Smith     ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
4702c38d4ed2SBarry Smith     ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
4703e51c0b9cSSatish Balay     b->icol       = isicol;
470487828ca2SBarry Smith     ierr = PetscMalloc((bs*n+bs)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
47054e2b4712SSatish Balay     /* In b structure:  Free imax, ilen, old a, old j.
47064e2b4712SSatish Balay        Allocate dloc, solve_work, new a, new j */
4707719d5645SBarry Smith     ierr = PetscLogObjectMemory(fact,(ainew[n]-n)*(sizeof(PetscInt))+bs2*ainew[n]*sizeof(PetscScalar));CHKERRQ(ierr);
47084e2b4712SSatish Balay     b->maxnz          = b->nz = ainew[n];
47094e2b4712SSatish Balay 
4710719d5645SBarry Smith     (fact)->info.factor_mallocs    = reallocate;
4711719d5645SBarry Smith     (fact)->info.fill_ratio_given  = f;
4712719d5645SBarry Smith     (fact)->info.fill_ratio_needed = ((PetscReal)ainew[n])/((PetscReal)ai[prow]);
47136bce7ff8SHong Zhang 
471441df41f0SMatthew Knepley   ierr = MatSeqBAIJSetNumericFactorization(fact,both_identity);CHKERRQ(ierr);
47158661488fSKris Buschelman   PetscFunctionReturn(0);
47168661488fSKris Buschelman }
47178661488fSKris Buschelman 
4718732ee342SKris Buschelman #undef __FUNCT__
47197e7071cdSKris Buschelman #define __FUNCT__ "MatSetUnfactored_SeqBAIJ_4_NaturalOrdering_SSE"
4720dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_SeqBAIJ_4_NaturalOrdering_SSE(Mat A)
47217e7071cdSKris Buschelman {
472212272027SHong Zhang   /* Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data; */
472312272027SHong Zhang   /* int i,*AJ=a->j,nz=a->nz; */
47245a9542e3SKris Buschelman   PetscFunctionBegin;
47257cf1b8d3SKris Buschelman   /* Undo Column scaling */
47267cf1b8d3SKris Buschelman /*    while (nz--) { */
47277cf1b8d3SKris Buschelman /*      AJ[i] = AJ[i]/4; */
47287cf1b8d3SKris Buschelman /*    } */
4729c115a38dSKris Buschelman   /* This should really invoke a push/pop logic, but we don't have that yet. */
4730c115a38dSKris Buschelman   A->ops->setunfactored = PETSC_NULL;
47317cf1b8d3SKris Buschelman   PetscFunctionReturn(0);
47327cf1b8d3SKris Buschelman }
47337cf1b8d3SKris Buschelman 
47347cf1b8d3SKris Buschelman #undef __FUNCT__
47357cf1b8d3SKris Buschelman #define __FUNCT__ "MatSetUnfactored_SeqBAIJ_4_NaturalOrdering_SSE_usj"
4736dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_SeqBAIJ_4_NaturalOrdering_SSE_usj(Mat A)
47377cf1b8d3SKris Buschelman {
47387cf1b8d3SKris Buschelman   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ *)A->data;
4739b24ad042SBarry Smith   PetscInt       *AJ=a->j,nz=a->nz;
47402aa5897fSKris Buschelman   unsigned short *aj=(unsigned short *)AJ;
47415a9542e3SKris Buschelman   PetscFunctionBegin;
47420b9da03eSKris Buschelman   /* Is this really necessary? */
474320235379SKris Buschelman   while (nz--) {
47440b9da03eSKris Buschelman     AJ[nz] = (int)((unsigned int)aj[nz]); /* First extend, then convert to signed. */
47457e7071cdSKris Buschelman   }
4746c115a38dSKris Buschelman   A->ops->setunfactored = PETSC_NULL;
47477e7071cdSKris Buschelman   PetscFunctionReturn(0);
47487e7071cdSKris Buschelman }
47497e7071cdSKris Buschelman 
4750732ee342SKris Buschelman 
4751