Actual source code: baijfact9.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:     Factorization code for BAIJ format. 
  4: */
  5: #include <../src/mat/impls/baij/seq/baij.h>
  6: #include <../src/mat/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:   PetscMalloc(25*(n+1)*sizeof(MatScalar),&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);
197:   C->ops->solve          = MatSolve_SeqBAIJ_5_inplace;
198:   C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5_inplace;
199:   C->assembled = PETSC_TRUE;
200:   PetscLogFlops(1.333333333333*5*5*5*b->mbs); /* from inverting diagonal blocks */
201:   return(0);
202: }

204: /* MatLUFactorNumeric_SeqBAIJ_5 - 
205:      copied from MatLUFactorNumeric_SeqBAIJ_N_inplace() and manually re-implemented 
206:        PetscKernel_A_gets_A_times_B()
207:        PetscKernel_A_gets_A_minus_B_times_C()
208:        PetscKernel_A_gets_inverse_A()
209: */

213: PetscErrorCode MatLUFactorNumeric_SeqBAIJ_5(Mat B,Mat A,const MatFactorInfo *info)
214: {
215:   Mat            C=B;
216:   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ *)C->data;
217:   IS             isrow = b->row,isicol = b->icol;
219:   const PetscInt *r,*ic;
220:   PetscInt       i,j,k,nz,nzL,row;
221:   const PetscInt n=a->mbs,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
222:   const PetscInt *ajtmp,*bjtmp,*bdiag=b->diag,*pj,bs2=a->bs2;
223:   MatScalar      *rtmp,*pc,*mwork,*v,*pv,*aa=a->a,work[25];
224:   PetscInt       flg,ipvt[5];
225:   PetscReal      shift = info->shiftamount;

228:   ISGetIndices(isrow,&r);
229:   ISGetIndices(isicol,&ic);

231:   /* generate work space needed by the factorization */
232:   PetscMalloc2(bs2*n,MatScalar,&rtmp,bs2,MatScalar,&mwork);
233:   PetscMemzero(rtmp,bs2*n*sizeof(MatScalar));

235:   for (i=0; i<n; i++){
236:     /* zero rtmp */
237:     /* L part */
238:     nz    = bi[i+1] - bi[i];
239:     bjtmp = bj + bi[i];
240:     for  (j=0; j<nz; j++){
241:       PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
242:     }

244:     /* U part */
245:     nz = bdiag[i] - bdiag[i+1];
246:     bjtmp = bj + bdiag[i+1]+1;
247:     for  (j=0; j<nz; j++){
248:       PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
249:     }
250: 
251:     /* load in initial (unfactored row) */
252:     nz    = ai[r[i]+1] - ai[r[i]];
253:     ajtmp = aj + ai[r[i]];
254:     v     = aa + bs2*ai[r[i]];
255:     for (j=0; j<nz; j++) {
256:       PetscMemcpy(rtmp+bs2*ic[ajtmp[j]],v+bs2*j,bs2*sizeof(MatScalar));
257:     }

259:     /* elimination */
260:     bjtmp = bj + bi[i];
261:     nzL   = bi[i+1] - bi[i];
262:     for(k=0;k < nzL;k++) {
263:       row = bjtmp[k];
264:       pc = rtmp + bs2*row;
265:       for (flg=0,j=0; j<bs2; j++) { if (pc[j]!=0.0) { flg = 1; break; }}
266:       if (flg) {
267:         pv = b->a + bs2*bdiag[row];
268:         /* PetscKernel_A_gets_A_times_B(bs,pc,pv,mwork); *pc = *pc * (*pv); */
269:         PetscKernel_A_gets_A_times_B_5(pc,pv,mwork);
270: 
271:         pj = b->j + bdiag[row+1]+1; /* begining of U(row,:) */
272:         pv = b->a + bs2*(bdiag[row+1]+1);
273:         nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries inU(row,:), excluding diag */
274:         for (j=0; j<nz; j++) {
275:           /* PetscKernel_A_gets_A_minus_B_times_C(bs,rtmp+bs2*pj[j],pc,pv+bs2*j); */
276:           /* rtmp+bs2*pj[j] = rtmp+bs2*pj[j] - (*pc)*(pv+bs2*j) */
277:           v    = rtmp + bs2*pj[j];
278:           PetscKernel_A_gets_A_minus_B_times_C_5(v,pc,pv);
279:           pv  += bs2;
280:         }
281:         PetscLogFlops(250*nz+225); /* flops = 2*bs^3*nz + 2*bs^3 - bs2) */
282:       }
283:     }

285:     /* finished row so stick it into b->a */
286:     /* L part */
287:     pv   = b->a + bs2*bi[i] ;
288:     pj   = b->j + bi[i] ;
289:     nz   = bi[i+1] - bi[i];
290:     for (j=0; j<nz; j++) {
291:       PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
292:     }

294:     /* Mark diagonal and invert diagonal for simplier triangular solves */
295:     pv   = b->a + bs2*bdiag[i];
296:     pj   = b->j + bdiag[i];
297:     PetscMemcpy(pv,rtmp+bs2*pj[0],bs2*sizeof(MatScalar));
298:     /* PetscKernel_A_gets_inverse_A(bs,pv,v_pivots,v_work); */
299:     PetscKernel_A_gets_inverse_A_5(pv,ipvt,work,shift);
300: 
301:     /* U part */
302:     pv = b->a + bs2*(bdiag[i+1]+1);
303:     pj = b->j + bdiag[i+1]+1;
304:     nz = bdiag[i] - bdiag[i+1] - 1;
305:     for (j=0; j<nz; j++){
306:       PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
307:     }
308:   }

310:   PetscFree2(rtmp,mwork);
311:   ISRestoreIndices(isicol,&ic);
312:   ISRestoreIndices(isrow,&r);
313:   C->ops->solve          = MatSolve_SeqBAIJ_5;
314:   C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5;
315:   C->assembled = PETSC_TRUE;
316:   PetscLogFlops(1.333333333333*5*5*5*n); /* from inverting diagonal blocks */
317:   return(0);
318: }

320: /*
321:       Version for when blocks are 5 by 5 Using natural ordering
322: */
325: PetscErrorCode MatLUFactorNumeric_SeqBAIJ_5_NaturalOrdering_inplace(Mat C,Mat A,const MatFactorInfo *info)
326: {
327:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ *)C->data;
329:   PetscInt       i,j,n = a->mbs,*bi = b->i,*bj = b->j,ipvt[5];
330:   PetscInt       *ajtmpold,*ajtmp,nz,row;
331:   PetscInt       *diag_offset = b->diag,*ai=a->i,*aj=a->j,*pj;
332:   MatScalar      *pv,*v,*rtmp,*pc,*w,*x;
333:   MatScalar      x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15;
334:   MatScalar      x16,x17,x18,x19,x20,x21,x22,x23,x24,x25;
335:   MatScalar      p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15;
336:   MatScalar      p16,p17,p18,p19,p20,p21,p22,p23,p24,p25;
337:   MatScalar      m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15;
338:   MatScalar      m16,m17,m18,m19,m20,m21,m22,m23,m24,m25;
339:   MatScalar      *ba = b->a,*aa = a->a,work[25];
340:   PetscReal      shift = info->shiftamount;

343:   PetscMalloc(25*(n+1)*sizeof(MatScalar),&rtmp);
344:   for (i=0; i<n; i++) {
345:     nz    = bi[i+1] - bi[i];
346:     ajtmp = bj + bi[i];
347:     for  (j=0; j<nz; j++) {
348:       x = rtmp+25*ajtmp[j];
349:       x[0]  = x[1]  = x[2]  = x[3]  = x[4]  = x[5]  = x[6] = x[7] = x[8] = x[9] = 0.0;
350:       x[10] = x[11] = x[12] = x[13] = x[14] = x[15] = 0.0;
351:       x[16] = x[17] = x[18] = x[19] = x[20] = x[21] = x[22] = x[23] = x[24] = 0.0;
352:     }
353:     /* load in initial (unfactored row) */
354:     nz       = ai[i+1] - ai[i];
355:     ajtmpold = aj + ai[i];
356:     v        = aa + 25*ai[i];
357:     for (j=0; j<nz; j++) {
358:       x    = rtmp+25*ajtmpold[j];
359:       x[0]  = v[0];  x[1]  = v[1];  x[2]  = v[2];  x[3]  = v[3];
360:       x[4]  = v[4];  x[5]  = v[5];  x[6]  = v[6];  x[7]  = v[7];  x[8]  = v[8];
361:       x[9]  = v[9];  x[10] = v[10]; x[11] = v[11]; x[12] = v[12]; x[13] = v[13];
362:       x[14] = v[14]; x[15] = v[15]; x[16] = v[16]; x[17] = v[17]; x[18] = v[18];
363:       x[19] = v[19]; x[20] = v[20]; x[21] = v[21]; x[22] = v[22]; x[23] = v[23];
364:       x[24] = v[24];
365:       v    += 25;
366:     }
367:     row = *ajtmp++;
368:     while (row < i) {
369:       pc  = rtmp + 25*row;
370:       p1  = pc[0];  p2  = pc[1];  p3  = pc[2];  p4  = pc[3];
371:       p5  = pc[4];  p6  = pc[5];  p7  = pc[6];  p8  = pc[7];  p9  = pc[8];
372:       p10 = pc[9];  p11 = pc[10]; p12 = pc[11]; p13 = pc[12]; p14 = pc[13];
373:       p15 = pc[14]; p16 = pc[15]; p17 = pc[16]; p18 = pc[17];
374:       p19 = pc[18]; p20 = pc[19]; p21 = pc[20]; p22 = pc[21]; p23 = pc[22];
375:       p24 = pc[23]; p25 = pc[24];
376:       if (p1 != 0.0 || p2 != 0.0 || p3 != 0.0 || p4 != 0.0 || p5 != 0.0 ||
377:           p6 != 0.0 || p7 != 0.0 || p8 != 0.0 || p9 != 0.0 || p10 != 0.0 ||
378:           p11 != 0.0 || p12 != 0.0 || p13 != 0.0 || p14 != 0.0 || p15 != 0.0
379:           || p16 != 0.0 || p17 != 0.0 || p18 != 0.0 || p19 != 0.0 || p20 != 0.0
380:           || p21 != 0.0 || p22 != 0.0 || p23 != 0.0 || p24 != 0.0 || p25 != 0.0) {
381:         pv = ba + 25*diag_offset[row];
382:         pj = bj + diag_offset[row] + 1;
383:         x1  = pv[0];  x2  = pv[1];  x3  = pv[2];  x4  = pv[3];
384:         x5  = pv[4];  x6  = pv[5];  x7  = pv[6];  x8  = pv[7];  x9  = pv[8];
385:         x10 = pv[9];  x11 = pv[10]; x12 = pv[11]; x13 = pv[12]; x14 = pv[13];
386:         x15 = pv[14]; x16 = pv[15]; x17 = pv[16]; x18 = pv[17]; x19 = pv[18];
387:         x20 = pv[19]; x21 = pv[20]; x22 = pv[21]; x23 = pv[22]; x24 = pv[23];
388:         x25 = pv[24];
389:         pc[0] = m1 = p1*x1 + p6*x2  + p11*x3 + p16*x4 + p21*x5;
390:         pc[1] = m2 = p2*x1 + p7*x2  + p12*x3 + p17*x4 + p22*x5;
391:         pc[2] = m3 = p3*x1 + p8*x2  + p13*x3 + p18*x4 + p23*x5;
392:         pc[3] = m4 = p4*x1 + p9*x2  + p14*x3 + p19*x4 + p24*x5;
393:         pc[4] = m5 = p5*x1 + p10*x2 + p15*x3 + p20*x4 + p25*x5;

395:         pc[5]  = m6  = p1*x6 + p6*x7  + p11*x8 + p16*x9 + p21*x10;
396:         pc[6]  = m7  = p2*x6 + p7*x7  + p12*x8 + p17*x9 + p22*x10;
397:         pc[7]  = m8  = p3*x6 + p8*x7  + p13*x8 + p18*x9 + p23*x10;
398:         pc[8]  = m9  = p4*x6 + p9*x7  + p14*x8 + p19*x9 + p24*x10;
399:         pc[9]  = m10 = p5*x6 + p10*x7 + p15*x8 + p20*x9 + p25*x10;

401:         pc[10] = m11 = p1*x11 + p6*x12  + p11*x13 + p16*x14 + p21*x15;
402:         pc[11] = m12 = p2*x11 + p7*x12  + p12*x13 + p17*x14 + p22*x15;
403:         pc[12] = m13 = p3*x11 + p8*x12  + p13*x13 + p18*x14 + p23*x15;
404:         pc[13] = m14 = p4*x11 + p9*x12  + p14*x13 + p19*x14 + p24*x15;
405:         pc[14] = m15 = p5*x11 + p10*x12 + p15*x13 + p20*x14 + p25*x15;

407:         pc[15] = m16 = p1*x16 + p6*x17  + p11*x18 + p16*x19 + p21*x20;
408:         pc[16] = m17 = p2*x16 + p7*x17  + p12*x18 + p17*x19 + p22*x20;
409:         pc[17] = m18 = p3*x16 + p8*x17  + p13*x18 + p18*x19 + p23*x20;
410:         pc[18] = m19 = p4*x16 + p9*x17  + p14*x18 + p19*x19 + p24*x20;
411:         pc[19] = m20 = p5*x16 + p10*x17 + p15*x18 + p20*x19 + p25*x20;

413:         pc[20] = m21 = p1*x21 + p6*x22  + p11*x23 + p16*x24 + p21*x25;
414:         pc[21] = m22 = p2*x21 + p7*x22  + p12*x23 + p17*x24 + p22*x25;
415:         pc[22] = m23 = p3*x21 + p8*x22  + p13*x23 + p18*x24 + p23*x25;
416:         pc[23] = m24 = p4*x21 + p9*x22  + p14*x23 + p19*x24 + p24*x25;
417:         pc[24] = m25 = p5*x21 + p10*x22 + p15*x23 + p20*x24 + p25*x25;

419:         nz = bi[row+1] - diag_offset[row] - 1;
420:         pv += 25;
421:         for (j=0; j<nz; j++) {
422:           x1   = pv[0];  x2  = pv[1];   x3 = pv[2];  x4  = pv[3];
423:           x5   = pv[4];  x6  = pv[5];   x7 = pv[6];  x8  = pv[7]; x9 = pv[8];
424:           x10  = pv[9];  x11 = pv[10]; x12 = pv[11]; x13 = pv[12];
425:           x14  = pv[13]; x15 = pv[14]; x16 = pv[15]; x17 = pv[16]; x18 = pv[17];
426:           x19 = pv[18];  x20 = pv[19]; x21 = pv[20]; x22 = pv[21]; x23 = pv[22];
427:           x24 = pv[23];  x25 = pv[24];
428:           x    = rtmp + 25*pj[j];
429:           x[0] -= m1*x1 + m6*x2   + m11*x3  + m16*x4 + m21*x5;
430:           x[1] -= m2*x1 + m7*x2   + m12*x3  + m17*x4 + m22*x5;
431:           x[2] -= m3*x1 + m8*x2   + m13*x3  + m18*x4 + m23*x5;
432:           x[3] -= m4*x1 + m9*x2   + m14*x3  + m19*x4 + m24*x5;
433:           x[4] -= m5*x1 + m10*x2  + m15*x3  + m20*x4 + m25*x5;

435:           x[5] -= m1*x6 + m6*x7   + m11*x8  + m16*x9 + m21*x10;
436:           x[6] -= m2*x6 + m7*x7   + m12*x8  + m17*x9 + m22*x10;
437:           x[7] -= m3*x6 + m8*x7   + m13*x8  + m18*x9 + m23*x10;
438:           x[8] -= m4*x6 + m9*x7   + m14*x8  + m19*x9 + m24*x10;
439:           x[9] -= m5*x6 + m10*x7  + m15*x8  + m20*x9 + m25*x10;

441:           x[10] -= m1*x11 + m6*x12  + m11*x13 + m16*x14 + m21*x15;
442:           x[11] -= m2*x11 + m7*x12  + m12*x13 + m17*x14 + m22*x15;
443:           x[12] -= m3*x11 + m8*x12  + m13*x13 + m18*x14 + m23*x15;
444:           x[13] -= m4*x11 + m9*x12  + m14*x13 + m19*x14 + m24*x15;
445:           x[14] -= m5*x11 + m10*x12 + m15*x13 + m20*x14 + m25*x15;

447:           x[15] -= m1*x16 + m6*x17  + m11*x18 + m16*x19 + m21*x20;
448:           x[16] -= m2*x16 + m7*x17  + m12*x18 + m17*x19 + m22*x20;
449:           x[17] -= m3*x16 + m8*x17  + m13*x18 + m18*x19 + m23*x20;
450:           x[18] -= m4*x16 + m9*x17  + m14*x18 + m19*x19 + m24*x20;
451:           x[19] -= m5*x16 + m10*x17 + m15*x18 + m20*x19 + m25*x20;

453:           x[20] -= m1*x21 + m6*x22  + m11*x23 + m16*x24 + m21*x25;
454:           x[21] -= m2*x21 + m7*x22  + m12*x23 + m17*x24 + m22*x25;
455:           x[22] -= m3*x21 + m8*x22  + m13*x23 + m18*x24 + m23*x25;
456:           x[23] -= m4*x21 + m9*x22  + m14*x23 + m19*x24 + m24*x25;
457:           x[24] -= m5*x21 + m10*x22 + m15*x23 + m20*x24 + m25*x25;
458:           pv   += 25;
459:         }
460:         PetscLogFlops(250.0*nz+225.0);
461:       }
462:       row = *ajtmp++;
463:     }
464:     /* finished row so stick it into b->a */
465:     pv = ba + 25*bi[i];
466:     pj = bj + bi[i];
467:     nz = bi[i+1] - bi[i];
468:     for (j=0; j<nz; j++) {
469:       x      = rtmp+25*pj[j];
470:       pv[0]  = x[0];  pv[1]  = x[1];  pv[2]  = x[2];  pv[3]  = x[3];
471:       pv[4]  = x[4];  pv[5]  = x[5];  pv[6]  = x[6];  pv[7]  = x[7]; pv[8] = x[8];
472:       pv[9]  = x[9];  pv[10] = x[10]; pv[11] = x[11]; pv[12] = x[12];
473:       pv[13] = x[13]; pv[14] = x[14]; pv[15] = x[15]; pv[16] = x[16]; pv[17] = x[17];
474:       pv[18] = x[18]; pv[19] = x[19]; pv[20] = x[20]; pv[21] = x[21]; pv[22] = x[22];
475:       pv[23] = x[23]; pv[24] = x[24];
476:       pv   += 25;
477:     }
478:     /* invert diagonal block */
479:     w = ba + 25*diag_offset[i];
480:     PetscKernel_A_gets_inverse_A_5(w,ipvt,work,shift);
481:   }

483:   PetscFree(rtmp);
484:   C->ops->solve          = MatSolve_SeqBAIJ_5_NaturalOrdering_inplace;
485:   C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5_NaturalOrdering_inplace;
486:   C->assembled = PETSC_TRUE;
487:   PetscLogFlops(1.333333333333*5*5*5*b->mbs); /* from inverting diagonal blocks */
488:   return(0);
489: }

493: PetscErrorCode MatLUFactorNumeric_SeqBAIJ_5_NaturalOrdering(Mat B,Mat A,const MatFactorInfo *info)
494: {
495:   Mat            C=B;
496:   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ *)C->data;
498:   PetscInt       i,j,k,nz,nzL,row;
499:   const PetscInt n=a->mbs,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
500:   const PetscInt *ajtmp,*bjtmp,*bdiag=b->diag,*pj,bs2=a->bs2;
501:   MatScalar      *rtmp,*pc,*mwork,*v,*vv,*pv,*aa=a->a,work[25];
502:   PetscInt       flg,ipvt[5];
503:   PetscReal      shift = info->shiftamount;

506:   /* generate work space needed by the factorization */
507:   PetscMalloc2(bs2*n,MatScalar,&rtmp,bs2,MatScalar,&mwork);
508:   PetscMemzero(rtmp,bs2*n*sizeof(MatScalar));

510:   for (i=0; i<n; i++){
511:     /* zero rtmp */
512:     /* L part */
513:     nz    = bi[i+1] - bi[i];
514:     bjtmp = bj + bi[i];
515:     for  (j=0; j<nz; j++){
516:       PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
517:     }

519:     /* U part */
520:     nz = bdiag[i] - bdiag[i+1];
521:     bjtmp = bj + bdiag[i+1]+1;
522:     for  (j=0; j<nz; j++){
523:       PetscMemzero(rtmp+bs2*bjtmp[j],bs2*sizeof(MatScalar));
524:     }
525: 
526:     /* load in initial (unfactored row) */
527:     nz    = ai[i+1] - ai[i];
528:     ajtmp = aj + ai[i];
529:     v     = aa + bs2*ai[i];
530:     for (j=0; j<nz; j++) {
531:       PetscMemcpy(rtmp+bs2*ajtmp[j],v+bs2*j,bs2*sizeof(MatScalar));
532:     }

534:     /* elimination */
535:     bjtmp = bj + bi[i];
536:     nzL   = bi[i+1] - bi[i];
537:     for(k=0;k < nzL;k++) {
538:       row = bjtmp[k];
539:       pc = rtmp + bs2*row;
540:       for (flg=0,j=0; j<bs2; j++) { if (pc[j]!=0.0) { flg = 1; break; }}
541:       if (flg) {
542:         pv = b->a + bs2*bdiag[row];
543:         /* PetscKernel_A_gets_A_times_B(bs,pc,pv,mwork); *pc = *pc * (*pv); */
544:         PetscKernel_A_gets_A_times_B_5(pc,pv,mwork);
545: 
546:         pj = b->j + bdiag[row+1]+1; /* begining of U(row,:) */
547:         pv = b->a + bs2*(bdiag[row+1]+1);
548:         nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries inU(row,:), excluding diag */
549:         for (j=0; j<nz; j++) {
550:           /* PetscKernel_A_gets_A_minus_B_times_C(bs,rtmp+bs2*pj[j],pc,pv+bs2*j); */
551:           /* rtmp+bs2*pj[j] = rtmp+bs2*pj[j] - (*pc)*(pv+bs2*j) */
552:           vv    = rtmp + bs2*pj[j];
553:           PetscKernel_A_gets_A_minus_B_times_C_5(vv,pc,pv);
554:           pv  += bs2;
555:         }
556:         PetscLogFlops(250*nz+225); /* flops = 2*bs^3*nz + 2*bs^3 - bs2) */
557:       }
558:     }

560:     /* finished row so stick it into b->a */
561:     /* L part */
562:     pv   = b->a + bs2*bi[i] ;
563:     pj   = b->j + bi[i] ;
564:     nz   = bi[i+1] - bi[i];
565:     for (j=0; j<nz; j++) {
566:       PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
567:     }
568: 
569:     /* Mark diagonal and invert diagonal for simplier triangular solves */
570:     pv   = b->a + bs2*bdiag[i];
571:     pj   = b->j + bdiag[i];
572:     PetscMemcpy(pv,rtmp+bs2*pj[0],bs2*sizeof(MatScalar));
573:     /* PetscKernel_A_gets_inverse_A(bs,pv,v_pivots,v_work); */
574:     PetscKernel_A_gets_inverse_A_5(pv,ipvt,work,shift);
575: 
576:     /* U part */
577:     pv = b->a + bs2*(bdiag[i+1]+1);
578:     pj = b->j + bdiag[i+1]+1;
579:     nz = bdiag[i] - bdiag[i+1] - 1;
580:     for (j=0; j<nz; j++){
581:       PetscMemcpy(pv+bs2*j,rtmp+bs2*pj[j],bs2*sizeof(MatScalar));
582:     }
583:   }
584:   PetscFree2(rtmp,mwork);
585:   C->ops->solve          = MatSolve_SeqBAIJ_5_NaturalOrdering;
586:   C->ops->solvetranspose = MatSolveTranspose_SeqBAIJ_5_NaturalOrdering;
587:   C->assembled = PETSC_TRUE;
588:   PetscLogFlops(1.333333333333*5*5*5*n); /* from inverting diagonal blocks */
589:   return(0);
590: }