Actual source code: baijfact9.c
petsc-3.6.1 2015-08-06
2: /*
3: Factorization code for BAIJ format.
4: */
5: #include <../src/mat/impls/baij/seq/baij.h>
6: #include <petsc/private/kernels/blockinvert.h>
8: /* ------------------------------------------------------------*/
9: /*
10: Version for when blocks are 5 by 5
11: */
14: PetscErrorCode MatLUFactorNumeric_SeqBAIJ_5_inplace(Mat C,Mat A,const MatFactorInfo *info)
15: {
16: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)C->data;
17: IS isrow = b->row,isicol = b->icol;
18: PetscErrorCode ierr;
19: const PetscInt *r,*ic,*bi = b->i,*bj = b->j,*ajtmpold,*ajtmp;
20: PetscInt i,j,n = a->mbs,nz,row,idx,ipvt[5];
21: const PetscInt *diag_offset = b->diag,*ai=a->i,*aj=a->j,*pj;
22: MatScalar *w,*pv,*rtmp,*x,*pc;
23: const MatScalar *v,*aa = a->a;
24: MatScalar p1,p2,p3,p4,m1,m2,m3,m4,m5,m6,m7,m8,m9,x1,x2,x3,x4;
25: MatScalar p5,p6,p7,p8,p9,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16;
26: MatScalar x17,x18,x19,x20,x21,x22,x23,x24,x25,p10,p11,p12,p13,p14;
27: MatScalar p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,p25,m10,m11,m12;
28: MatScalar m13,m14,m15,m16,m17,m18,m19,m20,m21,m22,m23,m24,m25;
29: MatScalar *ba = b->a,work[25];
30: PetscReal shift = info->shiftamount;
33: ISGetIndices(isrow,&r);
34: ISGetIndices(isicol,&ic);
35: PetscMalloc1(25*(n+1),&rtmp);
37: #define PETSC_USE_MEMZERO 1
38: #define PETSC_USE_MEMCPY 1
40: for (i=0; i<n; i++) {
41: nz = bi[i+1] - bi[i];
42: ajtmp = bj + bi[i];
43: for (j=0; j<nz; j++) {
44: #if defined(PETSC_USE_MEMZERO)
45: PetscMemzero(rtmp+25*ajtmp[j],25*sizeof(PetscScalar));
46: #else
47: x = rtmp+25*ajtmp[j];
48: x[0] = x[1] = x[2] = x[3] = x[4] = x[5] = x[6] = x[7] = x[8] = x[9] = 0.0;
49: x[10] = x[11] = x[12] = x[13] = x[14] = x[15] = x[16] = x[17] = 0.0;
50: x[18] = x[19] = x[20] = x[21] = x[22] = x[23] = x[24] = 0.0;
51: #endif
52: }
53: /* load in initial (unfactored row) */
54: idx = r[i];
55: nz = ai[idx+1] - ai[idx];
56: ajtmpold = aj + ai[idx];
57: v = aa + 25*ai[idx];
58: for (j=0; j<nz; j++) {
59: #if defined(PETSC_USE_MEMCPY)
60: PetscMemcpy(rtmp+25*ic[ajtmpold[j]],v,25*sizeof(PetscScalar));
61: #else
62: x = rtmp+25*ic[ajtmpold[j]];
63: x[0] = v[0]; x[1] = v[1]; x[2] = v[2]; x[3] = v[3];
64: x[4] = v[4]; x[5] = v[5]; x[6] = v[6]; x[7] = v[7]; x[8] = v[8];
65: x[9] = v[9]; x[10] = v[10]; x[11] = v[11]; x[12] = v[12]; x[13] = v[13];
66: x[14] = v[14]; x[15] = v[15]; x[16] = v[16]; x[17] = v[17];
67: x[18] = v[18]; x[19] = v[19]; x[20] = v[20]; x[21] = v[21];
68: x[22] = v[22]; x[23] = v[23]; x[24] = v[24];
69: #endif
70: v += 25;
71: }
72: row = *ajtmp++;
73: while (row < i) {
74: pc = rtmp + 25*row;
75: p1 = pc[0]; p2 = pc[1]; p3 = pc[2]; p4 = pc[3];
76: p5 = pc[4]; p6 = pc[5]; p7 = pc[6]; p8 = pc[7]; p9 = pc[8];
77: p10 = pc[9]; p11 = pc[10]; p12 = pc[11]; p13 = pc[12]; p14 = pc[13];
78: p15 = pc[14]; p16 = pc[15]; p17 = pc[16]; p18 = pc[17]; p19 = pc[18];
79: p20 = pc[19]; p21 = pc[20]; p22 = pc[21]; p23 = pc[22]; p24 = pc[23];
80: p25 = pc[24];
81: if (p1 != 0.0 || p2 != 0.0 || p3 != 0.0 || p4 != 0.0 || p5 != 0.0 ||
82: p6 != 0.0 || p7 != 0.0 || p8 != 0.0 || p9 != 0.0 || p10 != 0.0 ||
83: p11 != 0.0 || p12 != 0.0 || p13 != 0.0 || p14 != 0.0 || p15 != 0.0
84: || p16 != 0.0 || p17 != 0.0 || p18 != 0.0 || p19 != 0.0 ||
85: p20 != 0.0 || p21 != 0.0 || p22 != 0.0 || p23 != 0.0 ||
86: p24 != 0.0 || p25 != 0.0) {
87: pv = ba + 25*diag_offset[row];
88: pj = bj + diag_offset[row] + 1;
89: x1 = pv[0]; x2 = pv[1]; x3 = pv[2]; x4 = pv[3];
90: x5 = pv[4]; x6 = pv[5]; x7 = pv[6]; x8 = pv[7]; x9 = pv[8];
91: x10 = pv[9]; x11 = pv[10]; x12 = pv[11]; x13 = pv[12]; x14 = pv[13];
92: x15 = pv[14]; x16 = pv[15]; x17 = pv[16]; x18 = pv[17];
93: x19 = pv[18]; x20 = pv[19]; x21 = pv[20]; x22 = pv[21];
94: x23 = pv[22]; x24 = pv[23]; x25 = pv[24];
95: pc[0] = m1 = p1*x1 + p6*x2 + p11*x3 + p16*x4 + p21*x5;
96: pc[1] = m2 = p2*x1 + p7*x2 + p12*x3 + p17*x4 + p22*x5;
97: pc[2] = m3 = p3*x1 + p8*x2 + p13*x3 + p18*x4 + p23*x5;
98: pc[3] = m4 = p4*x1 + p9*x2 + p14*x3 + p19*x4 + p24*x5;
99: pc[4] = m5 = p5*x1 + p10*x2 + p15*x3 + p20*x4 + p25*x5;
101: pc[5] = m6 = p1*x6 + p6*x7 + p11*x8 + p16*x9 + p21*x10;
102: pc[6] = m7 = p2*x6 + p7*x7 + p12*x8 + p17*x9 + p22*x10;
103: pc[7] = m8 = p3*x6 + p8*x7 + p13*x8 + p18*x9 + p23*x10;
104: pc[8] = m9 = p4*x6 + p9*x7 + p14*x8 + p19*x9 + p24*x10;
105: pc[9] = m10 = p5*x6 + p10*x7 + p15*x8 + p20*x9 + p25*x10;
107: pc[10] = m11 = p1*x11 + p6*x12 + p11*x13 + p16*x14 + p21*x15;
108: pc[11] = m12 = p2*x11 + p7*x12 + p12*x13 + p17*x14 + p22*x15;
109: pc[12] = m13 = p3*x11 + p8*x12 + p13*x13 + p18*x14 + p23*x15;
110: pc[13] = m14 = p4*x11 + p9*x12 + p14*x13 + p19*x14 + p24*x15;
111: pc[14] = m15 = p5*x11 + p10*x12 + p15*x13 + p20*x14 + p25*x15;
113: pc[15] = m16 = p1*x16 + p6*x17 + p11*x18 + p16*x19 + p21*x20;
114: pc[16] = m17 = p2*x16 + p7*x17 + p12*x18 + p17*x19 + p22*x20;
115: pc[17] = m18 = p3*x16 + p8*x17 + p13*x18 + p18*x19 + p23*x20;
116: pc[18] = m19 = p4*x16 + p9*x17 + p14*x18 + p19*x19 + p24*x20;
117: pc[19] = m20 = p5*x16 + p10*x17 + p15*x18 + p20*x19 + p25*x20;
119: pc[20] = m21 = p1*x21 + p6*x22 + p11*x23 + p16*x24 + p21*x25;
120: pc[21] = m22 = p2*x21 + p7*x22 + p12*x23 + p17*x24 + p22*x25;
121: pc[22] = m23 = p3*x21 + p8*x22 + p13*x23 + p18*x24 + p23*x25;
122: pc[23] = m24 = p4*x21 + p9*x22 + p14*x23 + p19*x24 + p24*x25;
123: pc[24] = m25 = p5*x21 + p10*x22 + p15*x23 + p20*x24 + p25*x25;
125: nz = bi[row+1] - diag_offset[row] - 1;
126: pv += 25;
127: for (j=0; j<nz; j++) {
128: x1 = pv[0]; x2 = pv[1]; x3 = pv[2]; x4 = pv[3];
129: x5 = pv[4]; x6 = pv[5]; x7 = pv[6]; x8 = pv[7]; x9 = pv[8];
130: x10 = pv[9]; x11 = pv[10]; x12 = pv[11]; x13 = pv[12];
131: x14 = pv[13]; x15 = pv[14]; x16 = pv[15]; x17 = pv[16];
132: x18 = pv[17]; x19 = pv[18]; x20 = pv[19]; x21 = pv[20];
133: x22 = pv[21]; x23 = pv[22]; x24 = pv[23]; x25 = pv[24];
134: x = rtmp + 25*pj[j];
135: x[0] -= m1*x1 + m6*x2 + m11*x3 + m16*x4 + m21*x5;
136: x[1] -= m2*x1 + m7*x2 + m12*x3 + m17*x4 + m22*x5;
137: x[2] -= m3*x1 + m8*x2 + m13*x3 + m18*x4 + m23*x5;
138: x[3] -= m4*x1 + m9*x2 + m14*x3 + m19*x4 + m24*x5;
139: x[4] -= m5*x1 + m10*x2 + m15*x3 + m20*x4 + m25*x5;
141: x[5] -= m1*x6 + m6*x7 + m11*x8 + m16*x9 + m21*x10;
142: x[6] -= m2*x6 + m7*x7 + m12*x8 + m17*x9 + m22*x10;
143: x[7] -= m3*x6 + m8*x7 + m13*x8 + m18*x9 + m23*x10;
144: x[8] -= m4*x6 + m9*x7 + m14*x8 + m19*x9 + m24*x10;
145: x[9] -= m5*x6 + m10*x7 + m15*x8 + m20*x9 + m25*x10;
147: x[10] -= m1*x11 + m6*x12 + m11*x13 + m16*x14 + m21*x15;
148: x[11] -= m2*x11 + m7*x12 + m12*x13 + m17*x14 + m22*x15;
149: x[12] -= m3*x11 + m8*x12 + m13*x13 + m18*x14 + m23*x15;
150: x[13] -= m4*x11 + m9*x12 + m14*x13 + m19*x14 + m24*x15;
151: x[14] -= m5*x11 + m10*x12 + m15*x13 + m20*x14 + m25*x15;
153: x[15] -= m1*x16 + m6*x17 + m11*x18 + m16*x19 + m21*x20;
154: x[16] -= m2*x16 + m7*x17 + m12*x18 + m17*x19 + m22*x20;
155: x[17] -= m3*x16 + m8*x17 + m13*x18 + m18*x19 + m23*x20;
156: x[18] -= m4*x16 + m9*x17 + m14*x18 + m19*x19 + m24*x20;
157: x[19] -= m5*x16 + m10*x17 + m15*x18 + m20*x19 + m25*x20;
159: x[20] -= m1*x21 + m6*x22 + m11*x23 + m16*x24 + m21*x25;
160: x[21] -= m2*x21 + m7*x22 + m12*x23 + m17*x24 + m22*x25;
161: x[22] -= m3*x21 + m8*x22 + m13*x23 + m18*x24 + m23*x25;
162: x[23] -= m4*x21 + m9*x22 + m14*x23 + m19*x24 + m24*x25;
163: x[24] -= m5*x21 + m10*x22 + m15*x23 + m20*x24 + m25*x25;
165: pv += 25;
166: }
167: PetscLogFlops(250.0*nz+225.0);
168: }
169: row = *ajtmp++;
170: }
171: /* finished row so stick it into b->a */
172: pv = ba + 25*bi[i];
173: pj = bj + bi[i];
174: nz = bi[i+1] - bi[i];
175: for (j=0; j<nz; j++) {
176: #if defined(PETSC_USE_MEMCPY)
177: PetscMemcpy(pv,rtmp+25*pj[j],25*sizeof(PetscScalar));
178: #else
179: x = rtmp+25*pj[j];
180: pv[0] = x[0]; pv[1] = x[1]; pv[2] = x[2]; pv[3] = x[3];
181: pv[4] = x[4]; pv[5] = x[5]; pv[6] = x[6]; pv[7] = x[7]; pv[8] = x[8];
182: pv[9] = x[9]; pv[10] = x[10]; pv[11] = x[11]; pv[12] = x[12];
183: pv[13] = x[13]; pv[14] = x[14]; pv[15] = x[15]; pv[16] = x[16];
184: pv[17] = x[17]; pv[18] = x[18]; pv[19] = x[19]; pv[20] = x[20];
185: pv[21] = x[21]; pv[22] = x[22]; pv[23] = x[23]; pv[24] = x[24];
186: #endif
187: pv += 25;
188: }
189: /* invert diagonal block */
190: w = ba + 25*diag_offset[i];
191: PetscKernel_A_gets_inverse_A_5(w,ipvt,work,shift);
192: }
194: PetscFree(rtmp);
195: ISRestoreIndices(isicol,&ic);
196: ISRestoreIndices(isrow,&r);
198: C->ops->solve = MatSolve_SeqBAIJ_5_inplace;
199: C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5_inplace;
200: C->assembled = PETSC_TRUE;
202: PetscLogFlops(1.333333333333*5*5*5*b->mbs); /* from inverting diagonal blocks */
203: return(0);
204: }
206: /* MatLUFactorNumeric_SeqBAIJ_5 -
207: copied from MatLUFactorNumeric_SeqBAIJ_N_inplace() and manually re-implemented
208: PetscKernel_A_gets_A_times_B()
209: PetscKernel_A_gets_A_minus_B_times_C()
210: PetscKernel_A_gets_inverse_A()
211: */
215: PetscErrorCode MatLUFactorNumeric_SeqBAIJ_5(Mat B,Mat A,const MatFactorInfo *info)
216: {
217: Mat C =B;
218: Mat_SeqBAIJ *a =(Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)C->data;
219: IS isrow = b->row,isicol = b->icol;
221: const PetscInt *r,*ic;
222: PetscInt i,j,k,nz,nzL,row;
223: const PetscInt n=a->mbs,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
224: const PetscInt *ajtmp,*bjtmp,*bdiag=b->diag,*pj,bs2=a->bs2;
225: MatScalar *rtmp,*pc,*mwork,*v,*pv,*aa=a->a,work[25];
226: PetscInt flg,ipvt[5];
227: PetscReal shift = info->shiftamount;
230: ISGetIndices(isrow,&r);
231: ISGetIndices(isicol,&ic);
233: /* generate work space needed by the factorization */
234: PetscMalloc2(bs2*n,&rtmp,bs2,&mwork);
235: PetscMemzero(rtmp,bs2*n*sizeof(MatScalar));
237: for (i=0; i<n; i++) {
238: /* zero rtmp */
239: /* L part */
240: nz = bi[i+1] - bi[i];
241: bjtmp = bj + bi[i];
242: for (j=0; j<nz; j++) {
243: PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
244: }
246: /* U part */
247: nz = bdiag[i] - bdiag[i+1];
248: bjtmp = bj + bdiag[i+1]+1;
249: for (j=0; j<nz; j++) {
250: PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
251: }
253: /* load in initial (unfactored row) */
254: nz = ai[r[i]+1] - ai[r[i]];
255: ajtmp = aj + ai[r[i]];
256: v = aa + bs2*ai[r[i]];
257: for (j=0; j<nz; j++) {
258: PetscMemcpy(rtmp+bs2*ic[ajtmp[j]],v+bs2*j,bs2*sizeof(MatScalar));
259: }
261: /* elimination */
262: bjtmp = bj + bi[i];
263: nzL = bi[i+1] - bi[i];
264: for (k=0; k < nzL; k++) {
265: row = bjtmp[k];
266: pc = rtmp + bs2*row;
267: for (flg=0,j=0; j<bs2; j++) {
268: if (pc[j]!=0.0) {
269: flg = 1;
270: break;
271: }
272: }
273: if (flg) {
274: pv = b->a + bs2*bdiag[row];
275: /* PetscKernel_A_gets_A_times_B(bs,pc,pv,mwork); *pc = *pc * (*pv); */
276: PetscKernel_A_gets_A_times_B_5(pc,pv,mwork);
278: pj = b->j + bdiag[row+1]+1; /* begining of U(row,:) */
279: pv = b->a + bs2*(bdiag[row+1]+1);
280: nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries inU(row,:), excluding diag */
281: for (j=0; j<nz; j++) {
282: /* PetscKernel_A_gets_A_minus_B_times_C(bs,rtmp+bs2*pj[j],pc,pv+bs2*j); */
283: /* rtmp+bs2*pj[j] = rtmp+bs2*pj[j] - (*pc)*(pv+bs2*j) */
284: v = rtmp + bs2*pj[j];
285: PetscKernel_A_gets_A_minus_B_times_C_5(v,pc,pv);
286: pv += bs2;
287: }
288: PetscLogFlops(250*nz+225); /* flops = 2*bs^3*nz + 2*bs^3 - bs2) */
289: }
290: }
292: /* finished row so stick it into b->a */
293: /* L part */
294: pv = b->a + bs2*bi[i];
295: pj = b->j + bi[i];
296: nz = bi[i+1] - bi[i];
297: for (j=0; j<nz; j++) {
298: PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
299: }
301: /* Mark diagonal and invert diagonal for simplier triangular solves */
302: pv = b->a + bs2*bdiag[i];
303: pj = b->j + bdiag[i];
304: PetscMemcpy(pv,rtmp+bs2*pj[0],bs2*sizeof(MatScalar));
305: /* PetscKernel_A_gets_inverse_A(bs,pv,v_pivots,v_work); */
306: PetscKernel_A_gets_inverse_A_5(pv,ipvt,work,shift);
308: /* U part */
309: pv = b->a + bs2*(bdiag[i+1]+1);
310: pj = b->j + bdiag[i+1]+1;
311: nz = bdiag[i] - bdiag[i+1] - 1;
312: for (j=0; j<nz; j++) {
313: PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
314: }
315: }
317: PetscFree2(rtmp,mwork);
318: ISRestoreIndices(isicol,&ic);
319: ISRestoreIndices(isrow,&r);
321: C->ops->solve = MatSolve_SeqBAIJ_5;
322: C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5;
323: C->assembled = PETSC_TRUE;
325: PetscLogFlops(1.333333333333*5*5*5*n); /* from inverting diagonal blocks */
326: return(0);
327: }
329: /*
330: Version for when blocks are 5 by 5 Using natural ordering
331: */
334: PetscErrorCode MatLUFactorNumeric_SeqBAIJ_5_NaturalOrdering_inplace(Mat C,Mat A,const MatFactorInfo *info)
335: {
336: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)C->data;
338: PetscInt i,j,n = a->mbs,*bi = b->i,*bj = b->j,ipvt[5];
339: PetscInt *ajtmpold,*ajtmp,nz,row;
340: PetscInt *diag_offset = b->diag,*ai=a->i,*aj=a->j,*pj;
341: MatScalar *pv,*v,*rtmp,*pc,*w,*x;
342: MatScalar x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15;
343: MatScalar x16,x17,x18,x19,x20,x21,x22,x23,x24,x25;
344: MatScalar p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15;
345: MatScalar p16,p17,p18,p19,p20,p21,p22,p23,p24,p25;
346: MatScalar m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15;
347: MatScalar m16,m17,m18,m19,m20,m21,m22,m23,m24,m25;
348: MatScalar *ba = b->a,*aa = a->a,work[25];
349: PetscReal shift = info->shiftamount;
352: PetscMalloc1(25*(n+1),&rtmp);
353: for (i=0; i<n; i++) {
354: nz = bi[i+1] - bi[i];
355: ajtmp = bj + bi[i];
356: for (j=0; j<nz; j++) {
357: x = rtmp+25*ajtmp[j];
358: x[0] = x[1] = x[2] = x[3] = x[4] = x[5] = x[6] = x[7] = x[8] = x[9] = 0.0;
359: x[10] = x[11] = x[12] = x[13] = x[14] = x[15] = 0.0;
360: x[16] = x[17] = x[18] = x[19] = x[20] = x[21] = x[22] = x[23] = x[24] = 0.0;
361: }
362: /* load in initial (unfactored row) */
363: nz = ai[i+1] - ai[i];
364: ajtmpold = aj + ai[i];
365: v = aa + 25*ai[i];
366: for (j=0; j<nz; j++) {
367: x = rtmp+25*ajtmpold[j];
368: x[0] = v[0]; x[1] = v[1]; x[2] = v[2]; x[3] = v[3];
369: x[4] = v[4]; x[5] = v[5]; x[6] = v[6]; x[7] = v[7]; x[8] = v[8];
370: x[9] = v[9]; x[10] = v[10]; x[11] = v[11]; x[12] = v[12]; x[13] = v[13];
371: x[14] = v[14]; x[15] = v[15]; x[16] = v[16]; x[17] = v[17]; x[18] = v[18];
372: x[19] = v[19]; x[20] = v[20]; x[21] = v[21]; x[22] = v[22]; x[23] = v[23];
373: x[24] = v[24];
374: v += 25;
375: }
376: row = *ajtmp++;
377: while (row < i) {
378: pc = rtmp + 25*row;
379: p1 = pc[0]; p2 = pc[1]; p3 = pc[2]; p4 = pc[3];
380: p5 = pc[4]; p6 = pc[5]; p7 = pc[6]; p8 = pc[7]; p9 = pc[8];
381: p10 = pc[9]; p11 = pc[10]; p12 = pc[11]; p13 = pc[12]; p14 = pc[13];
382: p15 = pc[14]; p16 = pc[15]; p17 = pc[16]; p18 = pc[17];
383: p19 = pc[18]; p20 = pc[19]; p21 = pc[20]; p22 = pc[21]; p23 = pc[22];
384: p24 = pc[23]; p25 = pc[24];
385: if (p1 != 0.0 || p2 != 0.0 || p3 != 0.0 || p4 != 0.0 || p5 != 0.0 ||
386: p6 != 0.0 || p7 != 0.0 || p8 != 0.0 || p9 != 0.0 || p10 != 0.0 ||
387: p11 != 0.0 || p12 != 0.0 || p13 != 0.0 || p14 != 0.0 || p15 != 0.0
388: || p16 != 0.0 || p17 != 0.0 || p18 != 0.0 || p19 != 0.0 || p20 != 0.0
389: || p21 != 0.0 || p22 != 0.0 || p23 != 0.0 || p24 != 0.0 || p25 != 0.0) {
390: pv = ba + 25*diag_offset[row];
391: pj = bj + diag_offset[row] + 1;
392: x1 = pv[0]; x2 = pv[1]; x3 = pv[2]; x4 = pv[3];
393: x5 = pv[4]; x6 = pv[5]; x7 = pv[6]; x8 = pv[7]; x9 = pv[8];
394: x10 = pv[9]; x11 = pv[10]; x12 = pv[11]; x13 = pv[12]; x14 = pv[13];
395: x15 = pv[14]; x16 = pv[15]; x17 = pv[16]; x18 = pv[17]; x19 = pv[18];
396: x20 = pv[19]; x21 = pv[20]; x22 = pv[21]; x23 = pv[22]; x24 = pv[23];
397: x25 = pv[24];
398: pc[0] = m1 = p1*x1 + p6*x2 + p11*x3 + p16*x4 + p21*x5;
399: pc[1] = m2 = p2*x1 + p7*x2 + p12*x3 + p17*x4 + p22*x5;
400: pc[2] = m3 = p3*x1 + p8*x2 + p13*x3 + p18*x4 + p23*x5;
401: pc[3] = m4 = p4*x1 + p9*x2 + p14*x3 + p19*x4 + p24*x5;
402: pc[4] = m5 = p5*x1 + p10*x2 + p15*x3 + p20*x4 + p25*x5;
404: pc[5] = m6 = p1*x6 + p6*x7 + p11*x8 + p16*x9 + p21*x10;
405: pc[6] = m7 = p2*x6 + p7*x7 + p12*x8 + p17*x9 + p22*x10;
406: pc[7] = m8 = p3*x6 + p8*x7 + p13*x8 + p18*x9 + p23*x10;
407: pc[8] = m9 = p4*x6 + p9*x7 + p14*x8 + p19*x9 + p24*x10;
408: pc[9] = m10 = p5*x6 + p10*x7 + p15*x8 + p20*x9 + p25*x10;
410: pc[10] = m11 = p1*x11 + p6*x12 + p11*x13 + p16*x14 + p21*x15;
411: pc[11] = m12 = p2*x11 + p7*x12 + p12*x13 + p17*x14 + p22*x15;
412: pc[12] = m13 = p3*x11 + p8*x12 + p13*x13 + p18*x14 + p23*x15;
413: pc[13] = m14 = p4*x11 + p9*x12 + p14*x13 + p19*x14 + p24*x15;
414: pc[14] = m15 = p5*x11 + p10*x12 + p15*x13 + p20*x14 + p25*x15;
416: pc[15] = m16 = p1*x16 + p6*x17 + p11*x18 + p16*x19 + p21*x20;
417: pc[16] = m17 = p2*x16 + p7*x17 + p12*x18 + p17*x19 + p22*x20;
418: pc[17] = m18 = p3*x16 + p8*x17 + p13*x18 + p18*x19 + p23*x20;
419: pc[18] = m19 = p4*x16 + p9*x17 + p14*x18 + p19*x19 + p24*x20;
420: pc[19] = m20 = p5*x16 + p10*x17 + p15*x18 + p20*x19 + p25*x20;
422: pc[20] = m21 = p1*x21 + p6*x22 + p11*x23 + p16*x24 + p21*x25;
423: pc[21] = m22 = p2*x21 + p7*x22 + p12*x23 + p17*x24 + p22*x25;
424: pc[22] = m23 = p3*x21 + p8*x22 + p13*x23 + p18*x24 + p23*x25;
425: pc[23] = m24 = p4*x21 + p9*x22 + p14*x23 + p19*x24 + p24*x25;
426: pc[24] = m25 = p5*x21 + p10*x22 + p15*x23 + p20*x24 + p25*x25;
428: nz = bi[row+1] - diag_offset[row] - 1;
429: pv += 25;
430: for (j=0; j<nz; j++) {
431: x1 = pv[0]; x2 = pv[1]; x3 = pv[2]; x4 = pv[3];
432: x5 = pv[4]; x6 = pv[5]; x7 = pv[6]; x8 = pv[7]; x9 = pv[8];
433: x10 = pv[9]; x11 = pv[10]; x12 = pv[11]; x13 = pv[12];
434: x14 = pv[13]; x15 = pv[14]; x16 = pv[15]; x17 = pv[16]; x18 = pv[17];
435: x19 = pv[18]; x20 = pv[19]; x21 = pv[20]; x22 = pv[21]; x23 = pv[22];
436: x24 = pv[23]; x25 = pv[24];
437: x = rtmp + 25*pj[j];
438: x[0] -= m1*x1 + m6*x2 + m11*x3 + m16*x4 + m21*x5;
439: x[1] -= m2*x1 + m7*x2 + m12*x3 + m17*x4 + m22*x5;
440: x[2] -= m3*x1 + m8*x2 + m13*x3 + m18*x4 + m23*x5;
441: x[3] -= m4*x1 + m9*x2 + m14*x3 + m19*x4 + m24*x5;
442: x[4] -= m5*x1 + m10*x2 + m15*x3 + m20*x4 + m25*x5;
444: x[5] -= m1*x6 + m6*x7 + m11*x8 + m16*x9 + m21*x10;
445: x[6] -= m2*x6 + m7*x7 + m12*x8 + m17*x9 + m22*x10;
446: x[7] -= m3*x6 + m8*x7 + m13*x8 + m18*x9 + m23*x10;
447: x[8] -= m4*x6 + m9*x7 + m14*x8 + m19*x9 + m24*x10;
448: x[9] -= m5*x6 + m10*x7 + m15*x8 + m20*x9 + m25*x10;
450: x[10] -= m1*x11 + m6*x12 + m11*x13 + m16*x14 + m21*x15;
451: x[11] -= m2*x11 + m7*x12 + m12*x13 + m17*x14 + m22*x15;
452: x[12] -= m3*x11 + m8*x12 + m13*x13 + m18*x14 + m23*x15;
453: x[13] -= m4*x11 + m9*x12 + m14*x13 + m19*x14 + m24*x15;
454: x[14] -= m5*x11 + m10*x12 + m15*x13 + m20*x14 + m25*x15;
456: x[15] -= m1*x16 + m6*x17 + m11*x18 + m16*x19 + m21*x20;
457: x[16] -= m2*x16 + m7*x17 + m12*x18 + m17*x19 + m22*x20;
458: x[17] -= m3*x16 + m8*x17 + m13*x18 + m18*x19 + m23*x20;
459: x[18] -= m4*x16 + m9*x17 + m14*x18 + m19*x19 + m24*x20;
460: x[19] -= m5*x16 + m10*x17 + m15*x18 + m20*x19 + m25*x20;
462: x[20] -= m1*x21 + m6*x22 + m11*x23 + m16*x24 + m21*x25;
463: x[21] -= m2*x21 + m7*x22 + m12*x23 + m17*x24 + m22*x25;
464: x[22] -= m3*x21 + m8*x22 + m13*x23 + m18*x24 + m23*x25;
465: x[23] -= m4*x21 + m9*x22 + m14*x23 + m19*x24 + m24*x25;
466: x[24] -= m5*x21 + m10*x22 + m15*x23 + m20*x24 + m25*x25;
467: pv += 25;
468: }
469: PetscLogFlops(250.0*nz+225.0);
470: }
471: row = *ajtmp++;
472: }
473: /* finished row so stick it into b->a */
474: pv = ba + 25*bi[i];
475: pj = bj + bi[i];
476: nz = bi[i+1] - bi[i];
477: for (j=0; j<nz; j++) {
478: x = rtmp+25*pj[j];
479: pv[0] = x[0]; pv[1] = x[1]; pv[2] = x[2]; pv[3] = x[3];
480: pv[4] = x[4]; pv[5] = x[5]; pv[6] = x[6]; pv[7] = x[7]; pv[8] = x[8];
481: pv[9] = x[9]; pv[10] = x[10]; pv[11] = x[11]; pv[12] = x[12];
482: pv[13] = x[13]; pv[14] = x[14]; pv[15] = x[15]; pv[16] = x[16]; pv[17] = x[17];
483: pv[18] = x[18]; pv[19] = x[19]; pv[20] = x[20]; pv[21] = x[21]; pv[22] = x[22];
484: pv[23] = x[23]; pv[24] = x[24];
485: pv += 25;
486: }
487: /* invert diagonal block */
488: w = ba + 25*diag_offset[i];
489: PetscKernel_A_gets_inverse_A_5(w,ipvt,work,shift);
490: }
492: PetscFree(rtmp);
494: C->ops->solve = MatSolve_SeqBAIJ_5_NaturalOrdering_inplace;
495: C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5_NaturalOrdering_inplace;
496: C->assembled = PETSC_TRUE;
498: PetscLogFlops(1.333333333333*5*5*5*b->mbs); /* from inverting diagonal blocks */
499: return(0);
500: }
504: PetscErrorCode MatLUFactorNumeric_SeqBAIJ_5_NaturalOrdering(Mat B,Mat A,const MatFactorInfo *info)
505: {
506: Mat C =B;
507: Mat_SeqBAIJ *a=(Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)C->data;
509: PetscInt i,j,k,nz,nzL,row;
510: const PetscInt n=a->mbs,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
511: const PetscInt *ajtmp,*bjtmp,*bdiag=b->diag,*pj,bs2=a->bs2;
512: MatScalar *rtmp,*pc,*mwork,*v,*vv,*pv,*aa=a->a,work[25];
513: PetscInt flg,ipvt[5];
514: PetscReal shift = info->shiftamount;
517: /* generate work space needed by the factorization */
518: PetscMalloc2(bs2*n,&rtmp,bs2,&mwork);
519: PetscMemzero(rtmp,bs2*n*sizeof(MatScalar));
521: for (i=0; i<n; i++) {
522: /* zero rtmp */
523: /* L part */
524: nz = bi[i+1] - bi[i];
525: bjtmp = bj + bi[i];
526: for (j=0; j<nz; j++) {
527: PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
528: }
530: /* U part */
531: nz = bdiag[i] - bdiag[i+1];
532: bjtmp = bj + bdiag[i+1]+1;
533: for (j=0; j<nz; j++) {
534: PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
535: }
537: /* load in initial (unfactored row) */
538: nz = ai[i+1] - ai[i];
539: ajtmp = aj + ai[i];
540: v = aa + bs2*ai[i];
541: for (j=0; j<nz; j++) {
542: PetscMemcpy(rtmp+bs2*ajtmp[j],v+bs2*j,bs2*sizeof(MatScalar));
543: }
545: /* elimination */
546: bjtmp = bj + bi[i];
547: nzL = bi[i+1] - bi[i];
548: for (k=0; k < nzL; k++) {
549: row = bjtmp[k];
550: pc = rtmp + bs2*row;
551: for (flg=0,j=0; j<bs2; j++) {
552: if (pc[j]!=0.0) {
553: flg = 1;
554: break;
555: }
556: }
557: if (flg) {
558: pv = b->a + bs2*bdiag[row];
559: /* PetscKernel_A_gets_A_times_B(bs,pc,pv,mwork); *pc = *pc * (*pv); */
560: PetscKernel_A_gets_A_times_B_5(pc,pv,mwork);
562: pj = b->j + bdiag[row+1]+1; /* begining of U(row,:) */
563: pv = b->a + bs2*(bdiag[row+1]+1);
564: nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries inU(row,:), excluding diag */
565: for (j=0; j<nz; j++) {
566: /* PetscKernel_A_gets_A_minus_B_times_C(bs,rtmp+bs2*pj[j],pc,pv+bs2*j); */
567: /* rtmp+bs2*pj[j] = rtmp+bs2*pj[j] - (*pc)*(pv+bs2*j) */
568: vv = rtmp + bs2*pj[j];
569: PetscKernel_A_gets_A_minus_B_times_C_5(vv,pc,pv);
570: pv += bs2;
571: }
572: PetscLogFlops(250*nz+225); /* flops = 2*bs^3*nz + 2*bs^3 - bs2) */
573: }
574: }
576: /* finished row so stick it into b->a */
577: /* L part */
578: pv = b->a + bs2*bi[i];
579: pj = b->j + bi[i];
580: nz = bi[i+1] - bi[i];
581: for (j=0; j<nz; j++) {
582: PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
583: }
585: /* Mark diagonal and invert diagonal for simplier triangular solves */
586: pv = b->a + bs2*bdiag[i];
587: pj = b->j + bdiag[i];
588: PetscMemcpy(pv,rtmp+bs2*pj[0],bs2*sizeof(MatScalar));
589: /* PetscKernel_A_gets_inverse_A(bs,pv,v_pivots,v_work); */
590: PetscKernel_A_gets_inverse_A_5(pv,ipvt,work,shift);
592: /* U part */
593: pv = b->a + bs2*(bdiag[i+1]+1);
594: pj = b->j + bdiag[i+1]+1;
595: nz = bdiag[i] - bdiag[i+1] - 1;
596: for (j=0; j<nz; j++) {
597: PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
598: }
599: }
600: PetscFree2(rtmp,mwork);
602: C->ops->solve = MatSolve_SeqBAIJ_5_NaturalOrdering;
603: C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5_NaturalOrdering;
604: C->assembled = PETSC_TRUE;
606: PetscLogFlops(1.333333333333*5*5*5*n); /* from inverting diagonal blocks */
607: return(0);
608: }