Actual source code: sbaij2.c

petsc-master 2019-11-16
Report Typos and Errors

  2:  #include <../src/mat/impls/baij/seq/baij.h>
  3:  #include <../src/mat/impls/dense/seq/dense.h>
  4:  #include <../src/mat/impls/sbaij/seq/sbaij.h>
  5:  #include <petsc/private/kernels/blockinvert.h>
  6:  #include <petscbt.h>
  7:  #include <petscblaslapack.h>

  9: PetscErrorCode MatIncreaseOverlap_SeqSBAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
 10: {
 11:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
 13:   PetscInt       brow,i,j,k,l,mbs,n,*nidx,isz,bcol,bcol_max,start,end,*ai,*aj,bs,*nidx2;
 14:   const PetscInt *idx;
 15:   PetscBT        table_out,table_in;

 18:   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified");
 19:   mbs  = a->mbs;
 20:   ai   = a->i;
 21:   aj   = a->j;
 22:   bs   = A->rmap->bs;
 23:   PetscBTCreate(mbs,&table_out);
 24:   PetscMalloc1(mbs+1,&nidx);
 25:   PetscMalloc1(A->rmap->N+1,&nidx2);
 26:   PetscBTCreate(mbs,&table_in);

 28:   for (i=0; i<is_max; i++) { /* for each is */
 29:     isz  = 0;
 30:     PetscBTMemzero(mbs,table_out);

 32:     /* Extract the indices, assume there can be duplicate entries */
 33:     ISGetIndices(is[i],&idx);
 34:     ISGetLocalSize(is[i],&n);

 36:     /* Enter these into the temp arrays i.e mark table_out[brow], enter brow into new index */
 37:     bcol_max = 0;
 38:     for (j=0; j<n; ++j) {
 39:       brow = idx[j]/bs; /* convert the indices into block indices */
 40:       if (brow >= mbs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"index greater than mat-dim");
 41:       if (!PetscBTLookupSet(table_out,brow)) {
 42:         nidx[isz++] = brow;
 43:         if (bcol_max < brow) bcol_max = brow;
 44:       }
 45:     }
 46:     ISRestoreIndices(is[i],&idx);
 47:     ISDestroy(&is[i]);

 49:     k = 0;
 50:     for (j=0; j<ov; j++) { /* for each overlap */
 51:       /* set table_in for lookup - only mark entries that are added onto nidx in (j-1)-th overlap */
 52:       PetscBTMemzero(mbs,table_in);
 53:       for (l=k; l<isz; l++) { PetscBTSet(table_in,nidx[l]); }

 55:       n = isz;  /* length of the updated is[i] */
 56:       for (brow=0; brow<mbs; brow++) {
 57:         start = ai[brow]; end   = ai[brow+1];
 58:         if (PetscBTLookup(table_in,brow)) { /* brow is on nidx - row search: collect all bcol in this brow */
 59:           for (l = start; l<end; l++) {
 60:             bcol = aj[l];
 61:             if (!PetscBTLookupSet(table_out,bcol)) {
 62:               nidx[isz++] = bcol;
 63:               if (bcol_max < bcol) bcol_max = bcol;
 64:             }
 65:           }
 66:           k++;
 67:           if (k >= n) break; /* for (brow=0; brow<mbs; brow++) */
 68:         } else { /* brow is not on nidx - col serach: add brow onto nidx if there is a bcol in nidx */
 69:           for (l = start; l<end; l++) {
 70:             bcol = aj[l];
 71:             if (bcol > bcol_max) break;
 72:             if (PetscBTLookup(table_in,bcol)) {
 73:               if (!PetscBTLookupSet(table_out,brow)) nidx[isz++] = brow;
 74:               break; /* for l = start; l<end ; l++) */
 75:             }
 76:           }
 77:         }
 78:       }
 79:     } /* for each overlap */

 81:     /* expand the Index Set */
 82:     for (j=0; j<isz; j++) {
 83:       for (k=0; k<bs; k++) nidx2[j*bs+k] = nidx[j]*bs+k;
 84:     }
 85:     ISCreateGeneral(PETSC_COMM_SELF,isz*bs,nidx2,PETSC_COPY_VALUES,is+i);
 86:   }
 87:   PetscBTDestroy(&table_out);
 88:   PetscFree(nidx);
 89:   PetscFree(nidx2);
 90:   PetscBTDestroy(&table_in);
 91:   return(0);
 92: }

 94: /* Bseq is non-symmetric SBAIJ matrix, only used internally by PETSc.
 95:         Zero some ops' to avoid invalid usse */
 96: PetscErrorCode MatSeqSBAIJZeroOps_Private(Mat Bseq)
 97: {

101:   MatSetOption(Bseq,MAT_SYMMETRIC,PETSC_FALSE);
102:   Bseq->ops->mult                   = 0;
103:   Bseq->ops->multadd                = 0;
104:   Bseq->ops->multtranspose          = 0;
105:   Bseq->ops->multtransposeadd       = 0;
106:   Bseq->ops->lufactor               = 0;
107:   Bseq->ops->choleskyfactor         = 0;
108:   Bseq->ops->lufactorsymbolic       = 0;
109:   Bseq->ops->choleskyfactorsymbolic = 0;
110:   Bseq->ops->getinertia             = 0;
111:   return(0);
112: }

114: /* same as MatCreateSubMatrices_SeqBAIJ(), except cast Mat_SeqSBAIJ */
115: PetscErrorCode MatCreateSubMatrix_SeqSBAIJ_Private(Mat A,IS isrow,IS iscol,MatReuse scall,Mat *B)
116: {
117:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data,*c;
119:   PetscInt       *smap,i,k,kstart,kend,oldcols = a->nbs,*lens;
120:   PetscInt       row,mat_i,*mat_j,tcol,*mat_ilen;
121:   const PetscInt *irow,*icol;
122:   PetscInt       nrows,ncols,*ssmap,bs=A->rmap->bs,bs2=a->bs2;
123:   PetscInt       *aj = a->j,*ai = a->i;
124:   MatScalar      *mat_a;
125:   Mat            C;
126:   PetscBool      flag;


130:   ISGetIndices(isrow,&irow);
131:   ISGetIndices(iscol,&icol);
132:   ISGetLocalSize(isrow,&nrows);
133:   ISGetLocalSize(iscol,&ncols);

135:   PetscCalloc1(1+oldcols,&smap);
136:   ssmap = smap;
137:   PetscMalloc1(1+nrows,&lens);
138:   for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
139:   /* determine lens of each row */
140:   for (i=0; i<nrows; i++) {
141:     kstart  = ai[irow[i]];
142:     kend    = kstart + a->ilen[irow[i]];
143:     lens[i] = 0;
144:     for (k=kstart; k<kend; k++) {
145:       if (ssmap[aj[k]]) lens[i]++;
146:     }
147:   }
148:   /* Create and fill new matrix */
149:   if (scall == MAT_REUSE_MATRIX) {
150:     c = (Mat_SeqSBAIJ*)((*B)->data);

152:     if (c->mbs!=nrows || c->nbs!=ncols || (*B)->rmap->bs!=bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Submatrix wrong size");
153:     PetscArraycmp(c->ilen,lens,c->mbs,&flag);
154:     if (!flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
155:     PetscArrayzero(c->ilen,c->mbs);
156:     C    = *B;
157:   } else {
158:     MatCreate(PetscObjectComm((PetscObject)A),&C);
159:     MatSetSizes(C,nrows*bs,ncols*bs,PETSC_DETERMINE,PETSC_DETERMINE);
160:     MatSetType(C,((PetscObject)A)->type_name);
161:     MatSeqSBAIJSetPreallocation(C,bs,0,lens);
162:   }
163:   c = (Mat_SeqSBAIJ*)(C->data);
164:   for (i=0; i<nrows; i++) {
165:     row      = irow[i];
166:     kstart   = ai[row];
167:     kend     = kstart + a->ilen[row];
168:     mat_i    = c->i[i];
169:     mat_j    = c->j + mat_i;
170:     mat_a    = c->a + mat_i*bs2;
171:     mat_ilen = c->ilen + i;
172:     for (k=kstart; k<kend; k++) {
173:       if ((tcol=ssmap[a->j[k]])) {
174:         *mat_j++ = tcol - 1;
175:         PetscArraycpy(mat_a,a->a+k*bs2,bs2);
176:         mat_a   += bs2;
177:         (*mat_ilen)++;
178:       }
179:     }
180:   }
181:   /* sort */
182:   {
183:     MatScalar *work;

185:     PetscMalloc1(bs2,&work);
186:     for (i=0; i<nrows; i++) {
187:       PetscInt ilen;
188:       mat_i = c->i[i];
189:       mat_j = c->j + mat_i;
190:       mat_a = c->a + mat_i*bs2;
191:       ilen  = c->ilen[i];
192:       PetscSortIntWithDataArray(ilen,mat_j,mat_a,bs2*sizeof(MatScalar),work);
193:     }
194:     PetscFree(work);
195:   }

197:   /* Free work space */
198:   ISRestoreIndices(iscol,&icol);
199:   PetscFree(smap);
200:   PetscFree(lens);
201:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
202:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

204:   ISRestoreIndices(isrow,&irow);
205:   *B   = C;
206:   return(0);
207: }

209: PetscErrorCode MatCreateSubMatrix_SeqSBAIJ(Mat A,IS isrow,IS iscol,MatReuse scall,Mat *B)
210: {
211:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
212:   IS             is1,is2;
214:   PetscInt       *vary,*iary,nrows,ncols,i,bs=A->rmap->bs,count,maxmnbs;
215:   const PetscInt *irow,*icol;

218:   ISGetIndices(isrow,&irow);
219:   ISGetIndices(iscol,&icol);
220:   ISGetLocalSize(isrow,&nrows);
221:   ISGetLocalSize(iscol,&ncols);

223:   /* Verify if the indices corespond to each element in a block
224:    and form the IS with compressed IS */
225:   maxmnbs = PetscMax(a->mbs,a->nbs);
226:   PetscMalloc2(maxmnbs,&vary,maxmnbs,&iary);
227:   PetscArrayzero(vary,a->mbs);
228:   for (i=0; i<nrows; i++) vary[irow[i]/bs]++;
229:   for (i=0; i<a->mbs; i++) {
230:     if (vary[i]!=0 && vary[i]!=bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Index set does not match blocks");
231:   }
232:   count = 0;
233:   for (i=0; i<nrows; i++) {
234:     PetscInt j = irow[i] / bs;
235:     if ((vary[j]--)==bs) iary[count++] = j;
236:   }
237:   ISCreateGeneral(PETSC_COMM_SELF,count,iary,PETSC_COPY_VALUES,&is1);

239:   PetscArrayzero(vary,a->nbs);
240:   for (i=0; i<ncols; i++) vary[icol[i]/bs]++;
241:   for (i=0; i<a->nbs; i++) {
242:     if (vary[i]!=0 && vary[i]!=bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error in PETSc");
243:   }
244:   count = 0;
245:   for (i=0; i<ncols; i++) {
246:     PetscInt j = icol[i] / bs;
247:     if ((vary[j]--)==bs) iary[count++] = j;
248:   }
249:   ISCreateGeneral(PETSC_COMM_SELF,count,iary,PETSC_COPY_VALUES,&is2);
250:   ISRestoreIndices(isrow,&irow);
251:   ISRestoreIndices(iscol,&icol);
252:   PetscFree2(vary,iary);

254:   MatCreateSubMatrix_SeqSBAIJ_Private(A,is1,is2,scall,B);
255:   ISDestroy(&is1);
256:   ISDestroy(&is2);

258:   if (isrow != iscol) {
259:     PetscBool isequal;
260:     ISEqual(isrow,iscol,&isequal);
261:     if (!isequal) {
262:       MatSeqSBAIJZeroOps_Private(*B);
263:     }
264:   }
265:   return(0);
266: }

268: PetscErrorCode MatCreateSubMatrices_SeqSBAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
269: {
271:   PetscInt       i;

274:   if (scall == MAT_INITIAL_MATRIX) {
275:     PetscCalloc1(n+1,B);
276:   }

278:   for (i=0; i<n; i++) {
279:     MatCreateSubMatrix_SeqSBAIJ(A,irow[i],icol[i],scall,&(*B)[i]);
280:   }
281:   return(0);
282: }

284: /* -------------------------------------------------------*/
285: /* Should check that shapes of vectors and matrices match */
286: /* -------------------------------------------------------*/

288: PetscErrorCode MatMult_SeqSBAIJ_2(Mat A,Vec xx,Vec zz)
289: {
290:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
291:   PetscScalar       *z,x1,x2,zero=0.0;
292:   const PetscScalar *x,*xb;
293:   const MatScalar   *v;
294:   PetscErrorCode    ierr;
295:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
296:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
297:   PetscInt          nonzerorow=0;

300:   VecSet(zz,zero);
301:   if (!a->nz) return(0);
302:   VecGetArrayRead(xx,&x);
303:   VecGetArray(zz,&z);

305:   v  = a->a;
306:   xb = x;

308:   for (i=0; i<mbs; i++) {
309:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
310:     x1          = xb[0]; x2 = xb[1];
311:     ib          = aj + *ai;
312:     jmin        = 0;
313:     nonzerorow += (n>0);
314:     if (*ib == i) {     /* (diag of A)*x */
315:       z[2*i]   += v[0]*x1 + v[2]*x2;
316:       z[2*i+1] += v[2]*x1 + v[3]*x2;
317:       v        += 4; jmin++;
318:     }
319:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
320:     PetscPrefetchBlock(v+4*n,4*n,0,PETSC_PREFETCH_HINT_NTA);   /* Entries for the next row */
321:     for (j=jmin; j<n; j++) {
322:       /* (strict lower triangular part of A)*x  */
323:       cval       = ib[j]*2;
324:       z[cval]   += v[0]*x1 + v[1]*x2;
325:       z[cval+1] += v[2]*x1 + v[3]*x2;
326:       /* (strict upper triangular part of A)*x  */
327:       z[2*i]   += v[0]*x[cval] + v[2]*x[cval+1];
328:       z[2*i+1] += v[1]*x[cval] + v[3]*x[cval+1];
329:       v        += 4;
330:     }
331:     xb +=2; ai++;
332:   }

334:   VecRestoreArrayRead(xx,&x);
335:   VecRestoreArray(zz,&z);
336:   PetscLogFlops(8.0*(a->nz*2.0 - nonzerorow) - nonzerorow);
337:   return(0);
338: }

340: PetscErrorCode MatMult_SeqSBAIJ_3(Mat A,Vec xx,Vec zz)
341: {
342:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
343:   PetscScalar       *z,x1,x2,x3,zero=0.0;
344:   const PetscScalar *x,*xb;
345:   const MatScalar   *v;
346:   PetscErrorCode    ierr;
347:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
348:   const PetscInt    *aj = a->j,*ai = a->i,*ib;
349:   PetscInt          nonzerorow=0;

352:   VecSet(zz,zero);
353:   if (!a->nz) return(0);
354:   VecGetArrayRead(xx,&x);
355:   VecGetArray(zz,&z);

357:   v  = a->a;
358:   xb = x;

360:   for (i=0; i<mbs; i++) {
361:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
362:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2];
363:     ib          = aj + *ai;
364:     jmin        = 0;
365:     nonzerorow += (n>0);
366:     if (*ib == i) {     /* (diag of A)*x */
367:       z[3*i]   += v[0]*x1 + v[3]*x2 + v[6]*x3;
368:       z[3*i+1] += v[3]*x1 + v[4]*x2 + v[7]*x3;
369:       z[3*i+2] += v[6]*x1 + v[7]*x2 + v[8]*x3;
370:       v        += 9; jmin++;
371:     }
372:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
373:     PetscPrefetchBlock(v+9*n,9*n,0,PETSC_PREFETCH_HINT_NTA);   /* Entries for the next row */
374:     for (j=jmin; j<n; j++) {
375:       /* (strict lower triangular part of A)*x  */
376:       cval       = ib[j]*3;
377:       z[cval]   += v[0]*x1 + v[1]*x2 + v[2]*x3;
378:       z[cval+1] += v[3]*x1 + v[4]*x2 + v[5]*x3;
379:       z[cval+2] += v[6]*x1 + v[7]*x2 + v[8]*x3;
380:       /* (strict upper triangular part of A)*x  */
381:       z[3*i]   += v[0]*x[cval] + v[3]*x[cval+1]+ v[6]*x[cval+2];
382:       z[3*i+1] += v[1]*x[cval] + v[4]*x[cval+1]+ v[7]*x[cval+2];
383:       z[3*i+2] += v[2]*x[cval] + v[5]*x[cval+1]+ v[8]*x[cval+2];
384:       v        += 9;
385:     }
386:     xb +=3; ai++;
387:   }

389:   VecRestoreArrayRead(xx,&x);
390:   VecRestoreArray(zz,&z);
391:   PetscLogFlops(18.0*(a->nz*2.0 - nonzerorow) - nonzerorow);
392:   return(0);
393: }

395: PetscErrorCode MatMult_SeqSBAIJ_4(Mat A,Vec xx,Vec zz)
396: {
397:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
398:   PetscScalar       *z,x1,x2,x3,x4,zero=0.0;
399:   const PetscScalar *x,*xb;
400:   const MatScalar   *v;
401:   PetscErrorCode    ierr;
402:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
403:   const PetscInt    *aj = a->j,*ai = a->i,*ib;
404:   PetscInt          nonzerorow = 0;

407:   VecSet(zz,zero);
408:   if (!a->nz) return(0);
409:   VecGetArrayRead(xx,&x);
410:   VecGetArray(zz,&z);

412:   v  = a->a;
413:   xb = x;

415:   for (i=0; i<mbs; i++) {
416:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
417:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3];
418:     ib          = aj + *ai;
419:     jmin        = 0;
420:     nonzerorow += (n>0);
421:     if (*ib == i) {     /* (diag of A)*x */
422:       z[4*i]   += v[0]*x1 + v[4]*x2 +  v[8]*x3 + v[12]*x4;
423:       z[4*i+1] += v[4]*x1 + v[5]*x2 +  v[9]*x3 + v[13]*x4;
424:       z[4*i+2] += v[8]*x1 + v[9]*x2 + v[10]*x3 + v[14]*x4;
425:       z[4*i+3] += v[12]*x1+ v[13]*x2+ v[14]*x3 + v[15]*x4;
426:       v        += 16; jmin++;
427:     }
428:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
429:     PetscPrefetchBlock(v+16*n,16*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
430:     for (j=jmin; j<n; j++) {
431:       /* (strict lower triangular part of A)*x  */
432:       cval       = ib[j]*4;
433:       z[cval]   += v[0]*x1 + v[1]*x2 + v[2]*x3 + v[3]*x4;
434:       z[cval+1] += v[4]*x1 + v[5]*x2 + v[6]*x3 + v[7]*x4;
435:       z[cval+2] += v[8]*x1 + v[9]*x2 + v[10]*x3 + v[11]*x4;
436:       z[cval+3] += v[12]*x1 + v[13]*x2 + v[14]*x3 + v[15]*x4;
437:       /* (strict upper triangular part of A)*x  */
438:       z[4*i]   += v[0]*x[cval] + v[4]*x[cval+1]+ v[8]*x[cval+2] + v[12]*x[cval+3];
439:       z[4*i+1] += v[1]*x[cval] + v[5]*x[cval+1]+ v[9]*x[cval+2] + v[13]*x[cval+3];
440:       z[4*i+2] += v[2]*x[cval] + v[6]*x[cval+1]+ v[10]*x[cval+2]+ v[14]*x[cval+3];
441:       z[4*i+3] += v[3]*x[cval] + v[7]*x[cval+1]+ v[11]*x[cval+2]+ v[15]*x[cval+3];
442:       v        += 16;
443:     }
444:     xb +=4; ai++;
445:   }

447:   VecRestoreArrayRead(xx,&x);
448:   VecRestoreArray(zz,&z);
449:   PetscLogFlops(32.0*(a->nz*2.0 - nonzerorow) - nonzerorow);
450:   return(0);
451: }

453: PetscErrorCode MatMult_SeqSBAIJ_5(Mat A,Vec xx,Vec zz)
454: {
455:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
456:   PetscScalar       *z,x1,x2,x3,x4,x5,zero=0.0;
457:   const PetscScalar *x,*xb;
458:   const MatScalar   *v;
459:   PetscErrorCode    ierr;
460:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
461:   const PetscInt    *aj = a->j,*ai = a->i,*ib;
462:   PetscInt          nonzerorow=0;

465:   VecSet(zz,zero);
466:   if (!a->nz) return(0);
467:   VecGetArrayRead(xx,&x);
468:   VecGetArray(zz,&z);

470:   v  = a->a;
471:   xb = x;

473:   for (i=0; i<mbs; i++) {
474:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
475:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3]; x5=xb[4];
476:     ib          = aj + *ai;
477:     jmin        = 0;
478:     nonzerorow += (n>0);
479:     if (*ib == i) {      /* (diag of A)*x */
480:       z[5*i]   += v[0]*x1  + v[5]*x2 + v[10]*x3 + v[15]*x4+ v[20]*x5;
481:       z[5*i+1] += v[5]*x1  + v[6]*x2 + v[11]*x3 + v[16]*x4+ v[21]*x5;
482:       z[5*i+2] += v[10]*x1 +v[11]*x2 + v[12]*x3 + v[17]*x4+ v[22]*x5;
483:       z[5*i+3] += v[15]*x1 +v[16]*x2 + v[17]*x3 + v[18]*x4+ v[23]*x5;
484:       z[5*i+4] += v[20]*x1 +v[21]*x2 + v[22]*x3 + v[23]*x4+ v[24]*x5;
485:       v        += 25; jmin++;
486:     }
487:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
488:     PetscPrefetchBlock(v+25*n,25*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
489:     for (j=jmin; j<n; j++) {
490:       /* (strict lower triangular part of A)*x  */
491:       cval       = ib[j]*5;
492:       z[cval]   += v[0]*x1 + v[1]*x2 + v[2]*x3 + v[3]*x4 + v[4]*x5;
493:       z[cval+1] += v[5]*x1 + v[6]*x2 + v[7]*x3 + v[8]*x4 + v[9]*x5;
494:       z[cval+2] += v[10]*x1 + v[11]*x2 + v[12]*x3 + v[13]*x4+ v[14]*x5;
495:       z[cval+3] += v[15]*x1 + v[16]*x2 + v[17]*x3 + v[18]*x4+ v[19]*x5;
496:       z[cval+4] += v[20]*x1 + v[21]*x2 + v[22]*x3 + v[23]*x4+ v[24]*x5;
497:       /* (strict upper triangular part of A)*x  */
498:       z[5*i]   +=v[0]*x[cval]+v[5]*x[cval+1]+v[10]*x[cval+2]+v[15]*x[cval+3]+v[20]*x[cval+4];
499:       z[5*i+1] +=v[1]*x[cval]+v[6]*x[cval+1]+v[11]*x[cval+2]+v[16]*x[cval+3]+v[21]*x[cval+4];
500:       z[5*i+2] +=v[2]*x[cval]+v[7]*x[cval+1]+v[12]*x[cval+2]+v[17]*x[cval+3]+v[22]*x[cval+4];
501:       z[5*i+3] +=v[3]*x[cval]+v[8]*x[cval+1]+v[13]*x[cval+2]+v[18]*x[cval+3]+v[23]*x[cval+4];
502:       z[5*i+4] +=v[4]*x[cval]+v[9]*x[cval+1]+v[14]*x[cval+2]+v[19]*x[cval+3]+v[24]*x[cval+4];
503:       v        += 25;
504:     }
505:     xb +=5; ai++;
506:   }

508:   VecRestoreArrayRead(xx,&x);
509:   VecRestoreArray(zz,&z);
510:   PetscLogFlops(50.0*(a->nz*2.0 - nonzerorow) - nonzerorow);
511:   return(0);
512: }

514: PetscErrorCode MatMult_SeqSBAIJ_6(Mat A,Vec xx,Vec zz)
515: {
516:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
517:   PetscScalar       *z,x1,x2,x3,x4,x5,x6,zero=0.0;
518:   const PetscScalar *x,*xb;
519:   const MatScalar   *v;
520:   PetscErrorCode    ierr;
521:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
522:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
523:   PetscInt          nonzerorow=0;

526:   VecSet(zz,zero);
527:   if (!a->nz) return(0);
528:   VecGetArrayRead(xx,&x);
529:   VecGetArray(zz,&z);

531:   v  = a->a;
532:   xb = x;

534:   for (i=0; i<mbs; i++) {
535:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
536:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3]; x5=xb[4]; x6=xb[5];
537:     ib          = aj + *ai;
538:     jmin        = 0;
539:     nonzerorow += (n>0);
540:     if (*ib == i) {      /* (diag of A)*x */
541:       z[6*i]   += v[0]*x1  + v[6]*x2 + v[12]*x3 + v[18]*x4+ v[24]*x5 + v[30]*x6;
542:       z[6*i+1] += v[6]*x1  + v[7]*x2 + v[13]*x3 + v[19]*x4+ v[25]*x5 + v[31]*x6;
543:       z[6*i+2] += v[12]*x1 +v[13]*x2 + v[14]*x3 + v[20]*x4+ v[26]*x5 + v[32]*x6;
544:       z[6*i+3] += v[18]*x1 +v[19]*x2 + v[20]*x3 + v[21]*x4+ v[27]*x5 + v[33]*x6;
545:       z[6*i+4] += v[24]*x1 +v[25]*x2 + v[26]*x3 + v[27]*x4+ v[28]*x5 + v[34]*x6;
546:       z[6*i+5] += v[30]*x1 +v[31]*x2 + v[32]*x3 + v[33]*x4+ v[34]*x5 + v[35]*x6;
547:       v        += 36; jmin++;
548:     }
549:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
550:     PetscPrefetchBlock(v+36*n,36*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
551:     for (j=jmin; j<n; j++) {
552:       /* (strict lower triangular part of A)*x  */
553:       cval       = ib[j]*6;
554:       z[cval]   += v[0]*x1  + v[1]*x2 + v[2]*x3 + v[3]*x4+ v[4]*x5 + v[5]*x6;
555:       z[cval+1] += v[6]*x1  + v[7]*x2 + v[8]*x3 + v[9]*x4+ v[10]*x5 + v[11]*x6;
556:       z[cval+2] += v[12]*x1  + v[13]*x2 + v[14]*x3 + v[15]*x4+ v[16]*x5 + v[17]*x6;
557:       z[cval+3] += v[18]*x1  + v[19]*x2 + v[20]*x3 + v[21]*x4+ v[22]*x5 + v[23]*x6;
558:       z[cval+4] += v[24]*x1  + v[25]*x2 + v[26]*x3 + v[27]*x4+ v[28]*x5 + v[29]*x6;
559:       z[cval+5] += v[30]*x1  + v[31]*x2 + v[32]*x3 + v[33]*x4+ v[34]*x5 + v[35]*x6;
560:       /* (strict upper triangular part of A)*x  */
561:       z[6*i]   +=v[0]*x[cval]+v[6]*x[cval+1]+v[12]*x[cval+2]+v[18]*x[cval+3]+v[24]*x[cval+4]+v[30]*x[cval+5];
562:       z[6*i+1] +=v[1]*x[cval]+v[7]*x[cval+1]+v[13]*x[cval+2]+v[19]*x[cval+3]+v[25]*x[cval+4]+v[31]*x[cval+5];
563:       z[6*i+2] +=v[2]*x[cval]+v[8]*x[cval+1]+v[14]*x[cval+2]+v[20]*x[cval+3]+v[26]*x[cval+4]+v[32]*x[cval+5];
564:       z[6*i+3] +=v[3]*x[cval]+v[9]*x[cval+1]+v[15]*x[cval+2]+v[21]*x[cval+3]+v[27]*x[cval+4]+v[33]*x[cval+5];
565:       z[6*i+4] +=v[4]*x[cval]+v[10]*x[cval+1]+v[16]*x[cval+2]+v[22]*x[cval+3]+v[28]*x[cval+4]+v[34]*x[cval+5];
566:       z[6*i+5] +=v[5]*x[cval]+v[11]*x[cval+1]+v[17]*x[cval+2]+v[23]*x[cval+3]+v[29]*x[cval+4]+v[35]*x[cval+5];
567:       v        += 36;
568:     }
569:     xb +=6; ai++;
570:   }

572:   VecRestoreArrayRead(xx,&x);
573:   VecRestoreArray(zz,&z);
574:   PetscLogFlops(72.0*(a->nz*2.0 - nonzerorow) - nonzerorow);
575:   return(0);
576: }

578: PetscErrorCode MatMult_SeqSBAIJ_7(Mat A,Vec xx,Vec zz)
579: {
580:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
581:   PetscScalar       *z,x1,x2,x3,x4,x5,x6,x7,zero=0.0;
582:   const PetscScalar *x,*xb;
583:   const MatScalar   *v;
584:   PetscErrorCode    ierr;
585:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
586:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
587:   PetscInt          nonzerorow=0;

590:   VecSet(zz,zero);
591:   if (!a->nz) return(0);
592:   VecGetArrayRead(xx,&x);
593:   VecGetArray(zz,&z);

595:   v  = a->a;
596:   xb = x;

598:   for (i=0; i<mbs; i++) {
599:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
600:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3]; x5=xb[4]; x6=xb[5]; x7=xb[6];
601:     ib          = aj + *ai;
602:     jmin        = 0;
603:     nonzerorow += (n>0);
604:     if (*ib == i) {      /* (diag of A)*x */
605:       z[7*i]   += v[0]*x1 + v[7]*x2 + v[14]*x3 + v[21]*x4+ v[28]*x5 + v[35]*x6+ v[42]*x7;
606:       z[7*i+1] += v[7]*x1 + v[8]*x2 + v[15]*x3 + v[22]*x4+ v[29]*x5 + v[36]*x6+ v[43]*x7;
607:       z[7*i+2] += v[14]*x1+ v[15]*x2 +v[16]*x3 + v[23]*x4+ v[30]*x5 + v[37]*x6+ v[44]*x7;
608:       z[7*i+3] += v[21]*x1+ v[22]*x2 +v[23]*x3 + v[24]*x4+ v[31]*x5 + v[38]*x6+ v[45]*x7;
609:       z[7*i+4] += v[28]*x1+ v[29]*x2 +v[30]*x3 + v[31]*x4+ v[32]*x5 + v[39]*x6+ v[46]*x7;
610:       z[7*i+5] += v[35]*x1+ v[36]*x2 +v[37]*x3 + v[38]*x4+ v[39]*x5 + v[40]*x6+ v[47]*x7;
611:       z[7*i+6] += v[42]*x1+ v[43]*x2 +v[44]*x3 + v[45]*x4+ v[46]*x5 + v[47]*x6+ v[48]*x7;
612:       v        += 49; jmin++;
613:     }
614:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
615:     PetscPrefetchBlock(v+49*n,49*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
616:     for (j=jmin; j<n; j++) {
617:       /* (strict lower triangular part of A)*x  */
618:       cval       = ib[j]*7;
619:       z[cval]   += v[0]*x1  + v[1]*x2 + v[2]*x3 + v[3]*x4+ v[4]*x5 + v[5]*x6+ v[6]*x7;
620:       z[cval+1] += v[7]*x1  + v[8]*x2 + v[9]*x3 + v[10]*x4+ v[11]*x5 + v[12]*x6+ v[13]*x7;
621:       z[cval+2] += v[14]*x1  + v[15]*x2 + v[16]*x3 + v[17]*x4+ v[18]*x5 + v[19]*x6+ v[20]*x7;
622:       z[cval+3] += v[21]*x1  + v[22]*x2 + v[23]*x3 + v[24]*x4+ v[25]*x5 + v[26]*x6+ v[27]*x7;
623:       z[cval+4] += v[28]*x1  + v[29]*x2 + v[30]*x3 + v[31]*x4+ v[32]*x5 + v[33]*x6+ v[34]*x7;
624:       z[cval+5] += v[35]*x1  + v[36]*x2 + v[37]*x3 + v[38]*x4+ v[39]*x5 + v[40]*x6+ v[41]*x7;
625:       z[cval+6] += v[42]*x1  + v[43]*x2 + v[44]*x3 + v[45]*x4+ v[46]*x5 + v[47]*x6+ v[48]*x7;
626:       /* (strict upper triangular part of A)*x  */
627:       z[7*i]  +=v[0]*x[cval]+v[7]*x[cval+1]+v[14]*x[cval+2]+v[21]*x[cval+3]+v[28]*x[cval+4]+v[35]*x[cval+5]+v[42]*x[cval+6];
628:       z[7*i+1]+=v[1]*x[cval]+v[8]*x[cval+1]+v[15]*x[cval+2]+v[22]*x[cval+3]+v[29]*x[cval+4]+v[36]*x[cval+5]+v[43]*x[cval+6];
629:       z[7*i+2]+=v[2]*x[cval]+v[9]*x[cval+1]+v[16]*x[cval+2]+v[23]*x[cval+3]+v[30]*x[cval+4]+v[37]*x[cval+5]+v[44]*x[cval+6];
630:       z[7*i+3]+=v[3]*x[cval]+v[10]*x[cval+1]+v[17]*x[cval+2]+v[24]*x[cval+3]+v[31]*x[cval+4]+v[38]*x[cval+5]+v[45]*x[cval+6];
631:       z[7*i+4]+=v[4]*x[cval]+v[11]*x[cval+1]+v[18]*x[cval+2]+v[25]*x[cval+3]+v[32]*x[cval+4]+v[39]*x[cval+5]+v[46]*x[cval+6];
632:       z[7*i+5]+=v[5]*x[cval]+v[12]*x[cval+1]+v[19]*x[cval+2]+v[26]*x[cval+3]+v[33]*x[cval+4]+v[40]*x[cval+5]+v[47]*x[cval+6];
633:       z[7*i+6]+=v[6]*x[cval]+v[13]*x[cval+1]+v[20]*x[cval+2]+v[27]*x[cval+3]+v[34]*x[cval+4]+v[41]*x[cval+5]+v[48]*x[cval+6];
634:       v       += 49;
635:     }
636:     xb +=7; ai++;
637:   }
638:   VecRestoreArrayRead(xx,&x);
639:   VecRestoreArray(zz,&z);
640:   PetscLogFlops(98.0*(a->nz*2.0 - nonzerorow) - nonzerorow);
641:   return(0);
642: }

644: /*
645:     This will not work with MatScalar == float because it calls the BLAS
646: */
647: PetscErrorCode MatMult_SeqSBAIJ_N(Mat A,Vec xx,Vec zz)
648: {
649:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
650:   PetscScalar       *z,*z_ptr,*zb,*work,*workt,zero=0.0;
651:   const PetscScalar *x,*x_ptr,*xb;
652:   const MatScalar   *v;
653:   PetscErrorCode    ierr;
654:   PetscInt          mbs =a->mbs,i,bs=A->rmap->bs,j,n,bs2=a->bs2,ncols,k;
655:   const PetscInt    *idx,*aj,*ii;
656:   PetscInt          nonzerorow=0;

659:   VecSet(zz,zero);
660:   if (!a->nz) return(0);
661:   VecGetArrayRead(xx,&x);x_ptr = x;
662:   VecGetArray(zz,&z); z_ptr=z;

664:   aj = a->j;
665:   v  = a->a;
666:   ii = a->i;

668:   if (!a->mult_work) {
669:     PetscMalloc1(A->rmap->N+1,&a->mult_work);
670:   }
671:   work = a->mult_work;

673:   for (i=0; i<mbs; i++) {
674:     n           = ii[1] - ii[0]; ncols = n*bs;
675:     workt       = work; idx=aj+ii[0];
676:     nonzerorow += (n>0);

678:     /* upper triangular part */
679:     for (j=0; j<n; j++) {
680:       xb = x_ptr + bs*(*idx++);
681:       for (k=0; k<bs; k++) workt[k] = xb[k];
682:       workt += bs;
683:     }
684:     /* z(i*bs:(i+1)*bs-1) += A(i,:)*x */
685:     PetscKernel_w_gets_w_plus_Ar_times_v(bs,ncols,work,v,z);

687:     /* strict lower triangular part */
688:     idx = aj+ii[0];
689:     if (*idx == i) {
690:       ncols -= bs; v += bs2; idx++; n--;
691:     }

693:     if (ncols > 0) {
694:       workt = work;
695:       PetscArrayzero(workt,ncols);
696:       PetscKernel_w_gets_w_plus_trans_Ar_times_v(bs,ncols,x,v,workt);
697:       for (j=0; j<n; j++) {
698:         zb = z_ptr + bs*(*idx++);
699:         for (k=0; k<bs; k++) zb[k] += workt[k];
700:         workt += bs;
701:       }
702:     }
703:     x += bs; v += n*bs2; z += bs; ii++;
704:   }

706:   VecRestoreArrayRead(xx,&x);
707:   VecRestoreArray(zz,&z);
708:   PetscLogFlops(2.0*(a->nz*2.0 - nonzerorow)*bs2 - nonzerorow);
709:   return(0);
710: }

712: PetscErrorCode MatMultAdd_SeqSBAIJ_1(Mat A,Vec xx,Vec yy,Vec zz)
713: {
714:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
715:   PetscScalar       *z,x1;
716:   const PetscScalar *x,*xb;
717:   const MatScalar   *v;
718:   PetscErrorCode    ierr;
719:   PetscInt          mbs =a->mbs,i,n,cval,j,jmin;
720:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
721:   PetscInt          nonzerorow=0;

724:   VecCopy(yy,zz);
725:   if (!a->nz) return(0);
726:   VecGetArrayRead(xx,&x);
727:   VecGetArray(zz,&z);
728:   v    = a->a;
729:   xb   = x;

731:   for (i=0; i<mbs; i++) {
732:     n           = ai[1] - ai[0]; /* length of i_th row of A */
733:     x1          = xb[0];
734:     ib          = aj + *ai;
735:     jmin        = 0;
736:     nonzerorow += (n>0);
737:     if (*ib == i) {            /* (diag of A)*x */
738:       z[i] += *v++ * x[*ib++]; jmin++;
739:     }
740:     for (j=jmin; j<n; j++) {
741:       cval    = *ib;
742:       z[cval] += *v * x1;      /* (strict lower triangular part of A)*x  */
743:       z[i] += *v++ * x[*ib++]; /* (strict upper triangular part of A)*x  */
744:     }
745:     xb++; ai++;
746:   }

748:   VecRestoreArrayRead(xx,&x);
749:   VecRestoreArray(zz,&z);

751:   PetscLogFlops(2.0*(a->nz*2.0 - nonzerorow));
752:   return(0);
753: }

755: PetscErrorCode MatMultAdd_SeqSBAIJ_2(Mat A,Vec xx,Vec yy,Vec zz)
756: {
757:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
758:   PetscScalar       *z,x1,x2;
759:   const PetscScalar *x,*xb;
760:   const MatScalar   *v;
761:   PetscErrorCode    ierr;
762:   PetscInt          mbs =a->mbs,i,n,cval,j,jmin;
763:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
764:   PetscInt          nonzerorow=0;

767:   VecCopy(yy,zz);
768:   if (!a->nz) return(0);
769:   VecGetArrayRead(xx,&x);
770:   VecGetArray(zz,&z);

772:   v  = a->a;
773:   xb = x;

775:   for (i=0; i<mbs; i++) {
776:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
777:     x1          = xb[0]; x2 = xb[1];
778:     ib          = aj + *ai;
779:     jmin        = 0;
780:     nonzerorow += (n>0);
781:     if (*ib == i) {      /* (diag of A)*x */
782:       z[2*i]   += v[0]*x1 + v[2]*x2;
783:       z[2*i+1] += v[2]*x1 + v[3]*x2;
784:       v        += 4; jmin++;
785:     }
786:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
787:     PetscPrefetchBlock(v+4*n,4*n,0,PETSC_PREFETCH_HINT_NTA);   /* Entries for the next row */
788:     for (j=jmin; j<n; j++) {
789:       /* (strict lower triangular part of A)*x  */
790:       cval       = ib[j]*2;
791:       z[cval]   += v[0]*x1 + v[1]*x2;
792:       z[cval+1] += v[2]*x1 + v[3]*x2;
793:       /* (strict upper triangular part of A)*x  */
794:       z[2*i]   += v[0]*x[cval] + v[2]*x[cval+1];
795:       z[2*i+1] += v[1]*x[cval] + v[3]*x[cval+1];
796:       v        += 4;
797:     }
798:     xb +=2; ai++;
799:   }
800:   VecRestoreArrayRead(xx,&x);
801:   VecRestoreArray(zz,&z);

803:   PetscLogFlops(4.0*(a->nz*2.0 - nonzerorow));
804:   return(0);
805: }

807: PetscErrorCode MatMultAdd_SeqSBAIJ_3(Mat A,Vec xx,Vec yy,Vec zz)
808: {
809:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
810:   PetscScalar       *z,x1,x2,x3;
811:   const PetscScalar *x,*xb;
812:   const MatScalar   *v;
813:   PetscErrorCode    ierr;
814:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
815:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
816:   PetscInt          nonzerorow=0;

819:   VecCopy(yy,zz);
820:   if (!a->nz) return(0);
821:   VecGetArrayRead(xx,&x);
822:   VecGetArray(zz,&z);

824:   v  = a->a;
825:   xb = x;

827:   for (i=0; i<mbs; i++) {
828:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
829:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2];
830:     ib          = aj + *ai;
831:     jmin        = 0;
832:     nonzerorow += (n>0);
833:     if (*ib == i) {     /* (diag of A)*x */
834:       z[3*i]   += v[0]*x1 + v[3]*x2 + v[6]*x3;
835:       z[3*i+1] += v[3]*x1 + v[4]*x2 + v[7]*x3;
836:       z[3*i+2] += v[6]*x1 + v[7]*x2 + v[8]*x3;
837:       v        += 9; jmin++;
838:     }
839:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
840:     PetscPrefetchBlock(v+9*n,9*n,0,PETSC_PREFETCH_HINT_NTA);   /* Entries for the next row */
841:     for (j=jmin; j<n; j++) {
842:       /* (strict lower triangular part of A)*x  */
843:       cval       = ib[j]*3;
844:       z[cval]   += v[0]*x1 + v[1]*x2 + v[2]*x3;
845:       z[cval+1] += v[3]*x1 + v[4]*x2 + v[5]*x3;
846:       z[cval+2] += v[6]*x1 + v[7]*x2 + v[8]*x3;
847:       /* (strict upper triangular part of A)*x  */
848:       z[3*i]   += v[0]*x[cval] + v[3]*x[cval+1]+ v[6]*x[cval+2];
849:       z[3*i+1] += v[1]*x[cval] + v[4]*x[cval+1]+ v[7]*x[cval+2];
850:       z[3*i+2] += v[2]*x[cval] + v[5]*x[cval+1]+ v[8]*x[cval+2];
851:       v        += 9;
852:     }
853:     xb +=3; ai++;
854:   }

856:   VecRestoreArrayRead(xx,&x);
857:   VecRestoreArray(zz,&z);

859:   PetscLogFlops(18.0*(a->nz*2.0 - nonzerorow));
860:   return(0);
861: }

863: PetscErrorCode MatMultAdd_SeqSBAIJ_4(Mat A,Vec xx,Vec yy,Vec zz)
864: {
865:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
866:   PetscScalar       *z,x1,x2,x3,x4;
867:   const PetscScalar *x,*xb;
868:   const MatScalar   *v;
869:   PetscErrorCode    ierr;
870:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
871:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
872:   PetscInt          nonzerorow=0;

875:   VecCopy(yy,zz);
876:   if (!a->nz) return(0);
877:   VecGetArrayRead(xx,&x);
878:   VecGetArray(zz,&z);

880:   v  = a->a;
881:   xb = x;

883:   for (i=0; i<mbs; i++) {
884:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
885:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3];
886:     ib          = aj + *ai;
887:     jmin        = 0;
888:     nonzerorow += (n>0);
889:     if (*ib == i) {      /* (diag of A)*x */
890:       z[4*i]   += v[0]*x1 + v[4]*x2 +  v[8]*x3 + v[12]*x4;
891:       z[4*i+1] += v[4]*x1 + v[5]*x2 +  v[9]*x3 + v[13]*x4;
892:       z[4*i+2] += v[8]*x1 + v[9]*x2 + v[10]*x3 + v[14]*x4;
893:       z[4*i+3] += v[12]*x1+ v[13]*x2+ v[14]*x3 + v[15]*x4;
894:       v        += 16; jmin++;
895:     }
896:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
897:     PetscPrefetchBlock(v+16*n,16*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
898:     for (j=jmin; j<n; j++) {
899:       /* (strict lower triangular part of A)*x  */
900:       cval       = ib[j]*4;
901:       z[cval]   += v[0]*x1 + v[1]*x2 + v[2]*x3 + v[3]*x4;
902:       z[cval+1] += v[4]*x1 + v[5]*x2 + v[6]*x3 + v[7]*x4;
903:       z[cval+2] += v[8]*x1 + v[9]*x2 + v[10]*x3 + v[11]*x4;
904:       z[cval+3] += v[12]*x1 + v[13]*x2 + v[14]*x3 + v[15]*x4;
905:       /* (strict upper triangular part of A)*x  */
906:       z[4*i]   += v[0]*x[cval] + v[4]*x[cval+1]+ v[8]*x[cval+2] + v[12]*x[cval+3];
907:       z[4*i+1] += v[1]*x[cval] + v[5]*x[cval+1]+ v[9]*x[cval+2] + v[13]*x[cval+3];
908:       z[4*i+2] += v[2]*x[cval] + v[6]*x[cval+1]+ v[10]*x[cval+2]+ v[14]*x[cval+3];
909:       z[4*i+3] += v[3]*x[cval] + v[7]*x[cval+1]+ v[11]*x[cval+2]+ v[15]*x[cval+3];
910:       v        += 16;
911:     }
912:     xb +=4; ai++;
913:   }

915:   VecRestoreArrayRead(xx,&x);
916:   VecRestoreArray(zz,&z);

918:   PetscLogFlops(32.0*(a->nz*2.0 - nonzerorow));
919:   return(0);
920: }

922: PetscErrorCode MatMultAdd_SeqSBAIJ_5(Mat A,Vec xx,Vec yy,Vec zz)
923: {
924:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
925:   PetscScalar       *z,x1,x2,x3,x4,x5;
926:   const PetscScalar *x,*xb;
927:   const MatScalar   *v;
928:   PetscErrorCode    ierr;
929:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
930:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
931:   PetscInt          nonzerorow=0;

934:   VecCopy(yy,zz);
935:   if (!a->nz) return(0);
936:   VecGetArrayRead(xx,&x);
937:   VecGetArray(zz,&z);

939:   v  = a->a;
940:   xb = x;

942:   for (i=0; i<mbs; i++) {
943:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
944:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3]; x5=xb[4];
945:     ib          = aj + *ai;
946:     jmin        = 0;
947:     nonzerorow += (n>0);
948:     if (*ib == i) {      /* (diag of A)*x */
949:       z[5*i]   += v[0]*x1  + v[5]*x2 + v[10]*x3 + v[15]*x4+ v[20]*x5;
950:       z[5*i+1] += v[5]*x1  + v[6]*x2 + v[11]*x3 + v[16]*x4+ v[21]*x5;
951:       z[5*i+2] += v[10]*x1 +v[11]*x2 + v[12]*x3 + v[17]*x4+ v[22]*x5;
952:       z[5*i+3] += v[15]*x1 +v[16]*x2 + v[17]*x3 + v[18]*x4+ v[23]*x5;
953:       z[5*i+4] += v[20]*x1 +v[21]*x2 + v[22]*x3 + v[23]*x4+ v[24]*x5;
954:       v        += 25; jmin++;
955:     }
956:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
957:     PetscPrefetchBlock(v+25*n,25*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
958:     for (j=jmin; j<n; j++) {
959:       /* (strict lower triangular part of A)*x  */
960:       cval       = ib[j]*5;
961:       z[cval]   += v[0]*x1 + v[1]*x2 + v[2]*x3 + v[3]*x4 + v[4]*x5;
962:       z[cval+1] += v[5]*x1 + v[6]*x2 + v[7]*x3 + v[8]*x4 + v[9]*x5;
963:       z[cval+2] += v[10]*x1 + v[11]*x2 + v[12]*x3 + v[13]*x4+ v[14]*x5;
964:       z[cval+3] += v[15]*x1 + v[16]*x2 + v[17]*x3 + v[18]*x4+ v[19]*x5;
965:       z[cval+4] += v[20]*x1 + v[21]*x2 + v[22]*x3 + v[23]*x4+ v[24]*x5;
966:       /* (strict upper triangular part of A)*x  */
967:       z[5*i]   +=v[0]*x[cval]+v[5]*x[cval+1]+v[10]*x[cval+2]+v[15]*x[cval+3]+v[20]*x[cval+4];
968:       z[5*i+1] +=v[1]*x[cval]+v[6]*x[cval+1]+v[11]*x[cval+2]+v[16]*x[cval+3]+v[21]*x[cval+4];
969:       z[5*i+2] +=v[2]*x[cval]+v[7]*x[cval+1]+v[12]*x[cval+2]+v[17]*x[cval+3]+v[22]*x[cval+4];
970:       z[5*i+3] +=v[3]*x[cval]+v[8]*x[cval+1]+v[13]*x[cval+2]+v[18]*x[cval+3]+v[23]*x[cval+4];
971:       z[5*i+4] +=v[4]*x[cval]+v[9]*x[cval+1]+v[14]*x[cval+2]+v[19]*x[cval+3]+v[24]*x[cval+4];
972:       v        += 25;
973:     }
974:     xb +=5; ai++;
975:   }

977:   VecRestoreArrayRead(xx,&x);
978:   VecRestoreArray(zz,&z);

980:   PetscLogFlops(50.0*(a->nz*2.0 - nonzerorow));
981:   return(0);
982: }

984: PetscErrorCode MatMultAdd_SeqSBAIJ_6(Mat A,Vec xx,Vec yy,Vec zz)
985: {
986:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
987:   PetscScalar       *z,x1,x2,x3,x4,x5,x6;
988:   const PetscScalar *x,*xb;
989:   const MatScalar   *v;
990:   PetscErrorCode    ierr;
991:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
992:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
993:   PetscInt          nonzerorow=0;

996:   VecCopy(yy,zz);
997:   if (!a->nz) return(0);
998:   VecGetArrayRead(xx,&x);
999:   VecGetArray(zz,&z);

1001:   v  = a->a;
1002:   xb = x;

1004:   for (i=0; i<mbs; i++) {
1005:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
1006:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3]; x5=xb[4]; x6=xb[5];
1007:     ib          = aj + *ai;
1008:     jmin        = 0;
1009:     nonzerorow += (n>0);
1010:     if (*ib == i) {     /* (diag of A)*x */
1011:       z[6*i]   += v[0]*x1  + v[6]*x2 + v[12]*x3 + v[18]*x4+ v[24]*x5 + v[30]*x6;
1012:       z[6*i+1] += v[6]*x1  + v[7]*x2 + v[13]*x3 + v[19]*x4+ v[25]*x5 + v[31]*x6;
1013:       z[6*i+2] += v[12]*x1 +v[13]*x2 + v[14]*x3 + v[20]*x4+ v[26]*x5 + v[32]*x6;
1014:       z[6*i+3] += v[18]*x1 +v[19]*x2 + v[20]*x3 + v[21]*x4+ v[27]*x5 + v[33]*x6;
1015:       z[6*i+4] += v[24]*x1 +v[25]*x2 + v[26]*x3 + v[27]*x4+ v[28]*x5 + v[34]*x6;
1016:       z[6*i+5] += v[30]*x1 +v[31]*x2 + v[32]*x3 + v[33]*x4+ v[34]*x5 + v[35]*x6;
1017:       v        += 36; jmin++;
1018:     }
1019:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
1020:     PetscPrefetchBlock(v+36*n,36*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
1021:     for (j=jmin; j<n; j++) {
1022:       /* (strict lower triangular part of A)*x  */
1023:       cval       = ib[j]*6;
1024:       z[cval]   += v[0]*x1  + v[1]*x2 + v[2]*x3 + v[3]*x4+ v[4]*x5 + v[5]*x6;
1025:       z[cval+1] += v[6]*x1  + v[7]*x2 + v[8]*x3 + v[9]*x4+ v[10]*x5 + v[11]*x6;
1026:       z[cval+2] += v[12]*x1  + v[13]*x2 + v[14]*x3 + v[15]*x4+ v[16]*x5 + v[17]*x6;
1027:       z[cval+3] += v[18]*x1  + v[19]*x2 + v[20]*x3 + v[21]*x4+ v[22]*x5 + v[23]*x6;
1028:       z[cval+4] += v[24]*x1  + v[25]*x2 + v[26]*x3 + v[27]*x4+ v[28]*x5 + v[29]*x6;
1029:       z[cval+5] += v[30]*x1  + v[31]*x2 + v[32]*x3 + v[33]*x4+ v[34]*x5 + v[35]*x6;
1030:       /* (strict upper triangular part of A)*x  */
1031:       z[6*i]   +=v[0]*x[cval]+v[6]*x[cval+1]+v[12]*x[cval+2]+v[18]*x[cval+3]+v[24]*x[cval+4]+v[30]*x[cval+5];
1032:       z[6*i+1] +=v[1]*x[cval]+v[7]*x[cval+1]+v[13]*x[cval+2]+v[19]*x[cval+3]+v[25]*x[cval+4]+v[31]*x[cval+5];
1033:       z[6*i+2] +=v[2]*x[cval]+v[8]*x[cval+1]+v[14]*x[cval+2]+v[20]*x[cval+3]+v[26]*x[cval+4]+v[32]*x[cval+5];
1034:       z[6*i+3] +=v[3]*x[cval]+v[9]*x[cval+1]+v[15]*x[cval+2]+v[21]*x[cval+3]+v[27]*x[cval+4]+v[33]*x[cval+5];
1035:       z[6*i+4] +=v[4]*x[cval]+v[10]*x[cval+1]+v[16]*x[cval+2]+v[22]*x[cval+3]+v[28]*x[cval+4]+v[34]*x[cval+5];
1036:       z[6*i+5] +=v[5]*x[cval]+v[11]*x[cval+1]+v[17]*x[cval+2]+v[23]*x[cval+3]+v[29]*x[cval+4]+v[35]*x[cval+5];
1037:       v        += 36;
1038:     }
1039:     xb +=6; ai++;
1040:   }

1042:   VecRestoreArrayRead(xx,&x);
1043:   VecRestoreArray(zz,&z);

1045:   PetscLogFlops(72.0*(a->nz*2.0 - nonzerorow));
1046:   return(0);
1047: }

1049: PetscErrorCode MatMultAdd_SeqSBAIJ_7(Mat A,Vec xx,Vec yy,Vec zz)
1050: {
1051:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1052:   PetscScalar       *z,x1,x2,x3,x4,x5,x6,x7;
1053:   const PetscScalar *x,*xb;
1054:   const MatScalar   *v;
1055:   PetscErrorCode    ierr;
1056:   PetscInt          mbs = a->mbs,i,n,cval,j,jmin;
1057:   const PetscInt    *aj=a->j,*ai=a->i,*ib;
1058:   PetscInt          nonzerorow=0;

1061:   VecCopy(yy,zz);
1062:   if (!a->nz) return(0);
1063:   VecGetArrayRead(xx,&x);
1064:   VecGetArray(zz,&z);

1066:   v  = a->a;
1067:   xb = x;

1069:   for (i=0; i<mbs; i++) {
1070:     n           = ai[1] - ai[0]; /* length of i_th block row of A */
1071:     x1          = xb[0]; x2 = xb[1]; x3 = xb[2]; x4 = xb[3]; x5=xb[4]; x6=xb[5]; x7=xb[6];
1072:     ib          = aj + *ai;
1073:     jmin        = 0;
1074:     nonzerorow += (n>0);
1075:     if (*ib == i) {     /* (diag of A)*x */
1076:       z[7*i]   += v[0]*x1 + v[7]*x2 + v[14]*x3 + v[21]*x4+ v[28]*x5 + v[35]*x6+ v[42]*x7;
1077:       z[7*i+1] += v[7]*x1 + v[8]*x2 + v[15]*x3 + v[22]*x4+ v[29]*x5 + v[36]*x6+ v[43]*x7;
1078:       z[7*i+2] += v[14]*x1+ v[15]*x2 +v[16]*x3 + v[23]*x4+ v[30]*x5 + v[37]*x6+ v[44]*x7;
1079:       z[7*i+3] += v[21]*x1+ v[22]*x2 +v[23]*x3 + v[24]*x4+ v[31]*x5 + v[38]*x6+ v[45]*x7;
1080:       z[7*i+4] += v[28]*x1+ v[29]*x2 +v[30]*x3 + v[31]*x4+ v[32]*x5 + v[39]*x6+ v[46]*x7;
1081:       z[7*i+5] += v[35]*x1+ v[36]*x2 +v[37]*x3 + v[38]*x4+ v[39]*x5 + v[40]*x6+ v[47]*x7;
1082:       z[7*i+6] += v[42]*x1+ v[43]*x2 +v[44]*x3 + v[45]*x4+ v[46]*x5 + v[47]*x6+ v[48]*x7;
1083:       v        += 49; jmin++;
1084:     }
1085:     PetscPrefetchBlock(ib+jmin+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Indices for the next row (assumes same size as this one) */
1086:     PetscPrefetchBlock(v+49*n,49*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
1087:     for (j=jmin; j<n; j++) {
1088:       /* (strict lower triangular part of A)*x  */
1089:       cval       = ib[j]*7;
1090:       z[cval]   += v[0]*x1  + v[1]*x2 + v[2]*x3 + v[3]*x4+ v[4]*x5 + v[5]*x6+ v[6]*x7;
1091:       z[cval+1] += v[7]*x1  + v[8]*x2 + v[9]*x3 + v[10]*x4+ v[11]*x5 + v[12]*x6+ v[13]*x7;
1092:       z[cval+2] += v[14]*x1  + v[15]*x2 + v[16]*x3 + v[17]*x4+ v[18]*x5 + v[19]*x6+ v[20]*x7;
1093:       z[cval+3] += v[21]*x1  + v[22]*x2 + v[23]*x3 + v[24]*x4+ v[25]*x5 + v[26]*x6+ v[27]*x7;
1094:       z[cval+4] += v[28]*x1  + v[29]*x2 + v[30]*x3 + v[31]*x4+ v[32]*x5 + v[33]*x6+ v[34]*x7;
1095:       z[cval+5] += v[35]*x1  + v[36]*x2 + v[37]*x3 + v[38]*x4+ v[39]*x5 + v[40]*x6+ v[41]*x7;
1096:       z[cval+6] += v[42]*x1  + v[43]*x2 + v[44]*x3 + v[45]*x4+ v[46]*x5 + v[47]*x6+ v[48]*x7;
1097:       /* (strict upper triangular part of A)*x  */
1098:       z[7*i]  +=v[0]*x[cval]+v[7]*x[cval+1]+v[14]*x[cval+2]+v[21]*x[cval+3]+v[28]*x[cval+4]+v[35]*x[cval+5]+v[42]*x[cval+6];
1099:       z[7*i+1]+=v[1]*x[cval]+v[8]*x[cval+1]+v[15]*x[cval+2]+v[22]*x[cval+3]+v[29]*x[cval+4]+v[36]*x[cval+5]+v[43]*x[cval+6];
1100:       z[7*i+2]+=v[2]*x[cval]+v[9]*x[cval+1]+v[16]*x[cval+2]+v[23]*x[cval+3]+v[30]*x[cval+4]+v[37]*x[cval+5]+v[44]*x[cval+6];
1101:       z[7*i+3]+=v[3]*x[cval]+v[10]*x[cval+1]+v[17]*x[cval+2]+v[24]*x[cval+3]+v[31]*x[cval+4]+v[38]*x[cval+5]+v[45]*x[cval+6];
1102:       z[7*i+4]+=v[4]*x[cval]+v[11]*x[cval+1]+v[18]*x[cval+2]+v[25]*x[cval+3]+v[32]*x[cval+4]+v[39]*x[cval+5]+v[46]*x[cval+6];
1103:       z[7*i+5]+=v[5]*x[cval]+v[12]*x[cval+1]+v[19]*x[cval+2]+v[26]*x[cval+3]+v[33]*x[cval+4]+v[40]*x[cval+5]+v[47]*x[cval+6];
1104:       z[7*i+6]+=v[6]*x[cval]+v[13]*x[cval+1]+v[20]*x[cval+2]+v[27]*x[cval+3]+v[34]*x[cval+4]+v[41]*x[cval+5]+v[48]*x[cval+6];
1105:       v       += 49;
1106:     }
1107:     xb +=7; ai++;
1108:   }

1110:   VecRestoreArrayRead(xx,&x);
1111:   VecRestoreArray(zz,&z);

1113:   PetscLogFlops(98.0*(a->nz*2.0 - nonzerorow));
1114:   return(0);
1115: }

1117: PetscErrorCode MatMultAdd_SeqSBAIJ_N(Mat A,Vec xx,Vec yy,Vec zz)
1118: {
1119:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1120:   PetscScalar       *z,*z_ptr=0,*zb,*work,*workt;
1121:   const PetscScalar *x,*x_ptr,*xb;
1122:   const MatScalar   *v;
1123:   PetscErrorCode    ierr;
1124:   PetscInt          mbs = a->mbs,i,bs=A->rmap->bs,j,n,bs2=a->bs2,ncols,k;
1125:   const PetscInt    *idx,*aj,*ii;
1126:   PetscInt          nonzerorow=0;

1129:   VecCopy(yy,zz);
1130:   if (!a->nz) return(0);
1131:   VecGetArrayRead(xx,&x); x_ptr=x;
1132:   VecGetArray(zz,&z); z_ptr=z;

1134:   aj = a->j;
1135:   v  = a->a;
1136:   ii = a->i;

1138:   if (!a->mult_work) {
1139:     PetscMalloc1(A->rmap->n+1,&a->mult_work);
1140:   }
1141:   work = a->mult_work;


1144:   for (i=0; i<mbs; i++) {
1145:     n           = ii[1] - ii[0]; ncols = n*bs;
1146:     workt       = work; idx=aj+ii[0];
1147:     nonzerorow += (n>0);

1149:     /* upper triangular part */
1150:     for (j=0; j<n; j++) {
1151:       xb = x_ptr + bs*(*idx++);
1152:       for (k=0; k<bs; k++) workt[k] = xb[k];
1153:       workt += bs;
1154:     }
1155:     /* z(i*bs:(i+1)*bs-1) += A(i,:)*x */
1156:     PetscKernel_w_gets_w_plus_Ar_times_v(bs,ncols,work,v,z);

1158:     /* strict lower triangular part */
1159:     idx = aj+ii[0];
1160:     if (*idx == i) {
1161:       ncols -= bs; v += bs2; idx++; n--;
1162:     }
1163:     if (ncols > 0) {
1164:       workt = work;
1165:       PetscArrayzero(workt,ncols);
1166:       PetscKernel_w_gets_w_plus_trans_Ar_times_v(bs,ncols,x,v,workt);
1167:       for (j=0; j<n; j++) {
1168:         zb = z_ptr + bs*(*idx++);
1169:         for (k=0; k<bs; k++) zb[k] += workt[k];
1170:         workt += bs;
1171:       }
1172:     }

1174:     x += bs; v += n*bs2; z += bs; ii++;
1175:   }

1177:   VecRestoreArrayRead(xx,&x);
1178:   VecRestoreArray(zz,&z);

1180:   PetscLogFlops(2.0*(a->nz*2.0 - nonzerorow));
1181:   return(0);
1182: }

1184: PetscErrorCode MatScale_SeqSBAIJ(Mat inA,PetscScalar alpha)
1185: {
1186:   Mat_SeqSBAIJ   *a     = (Mat_SeqSBAIJ*)inA->data;
1187:   PetscScalar    oalpha = alpha;
1189:   PetscBLASInt   one = 1,totalnz;

1192:   PetscBLASIntCast(a->bs2*a->nz,&totalnz);
1193:   PetscStackCallBLAS("BLASscal",BLASscal_(&totalnz,&oalpha,a->a,&one));
1194:   PetscLogFlops(totalnz);
1195:   return(0);
1196: }

1198: PetscErrorCode MatNorm_SeqSBAIJ(Mat A,NormType type,PetscReal *norm)
1199: {
1200:   Mat_SeqSBAIJ    *a       = (Mat_SeqSBAIJ*)A->data;
1201:   const MatScalar *v       = a->a;
1202:   PetscReal       sum_diag = 0.0, sum_off = 0.0, *sum;
1203:   PetscInt        i,j,k,bs = A->rmap->bs,bs2=a->bs2,k1,mbs=a->mbs,jmin,jmax,nexti,ik,*jl,*il;
1204:   PetscErrorCode  ierr;
1205:   const PetscInt  *aj=a->j,*col;

1208:   if (!a->nz) {
1209:     *norm = 0.0;
1210:     return(0);
1211:   }
1212:   if (type == NORM_FROBENIUS) {
1213:     for (k=0; k<mbs; k++) {
1214:       jmin = a->i[k]; jmax = a->i[k+1];
1215:       col  = aj + jmin;
1216:       if (*col == k) {         /* diagonal block */
1217:         for (i=0; i<bs2; i++) {
1218:           sum_diag += PetscRealPart(PetscConj(*v)*(*v)); v++;
1219:         }
1220:         jmin++;
1221:       }
1222:       for (j=jmin; j<jmax; j++) {  /* off-diagonal blocks */
1223:         for (i=0; i<bs2; i++) {
1224:           sum_off += PetscRealPart(PetscConj(*v)*(*v)); v++;
1225:         }
1226:       }
1227:     }
1228:     *norm = PetscSqrtReal(sum_diag + 2*sum_off);
1229:     PetscLogFlops(2*bs2*a->nz);
1230:   } else if (type == NORM_INFINITY || type == NORM_1) {  /* maximum row/column sum */
1231:     PetscMalloc3(bs,&sum,mbs,&il,mbs,&jl);
1232:     for (i=0; i<mbs; i++) jl[i] = mbs;
1233:     il[0] = 0;

1235:     *norm = 0.0;
1236:     for (k=0; k<mbs; k++) { /* k_th block row */
1237:       for (j=0; j<bs; j++) sum[j]=0.0;
1238:       /*-- col sum --*/
1239:       i = jl[k]; /* first |A(i,k)| to be added */
1240:       /* jl[k]=i: first nozero element in row i for submatrix A(1:k,k:n) (active window)
1241:                   at step k */
1242:       while (i<mbs) {
1243:         nexti = jl[i];  /* next block row to be added */
1244:         ik    = il[i];  /* block index of A(i,k) in the array a */
1245:         for (j=0; j<bs; j++) {
1246:           v = a->a + ik*bs2 + j*bs;
1247:           for (k1=0; k1<bs; k1++) {
1248:             sum[j] += PetscAbsScalar(*v); v++;
1249:           }
1250:         }
1251:         /* update il, jl */
1252:         jmin = ik + 1; /* block index of array a: points to the next nonzero of A in row i */
1253:         jmax = a->i[i+1];
1254:         if (jmin < jmax) {
1255:           il[i] = jmin;
1256:           j     = a->j[jmin];
1257:           jl[i] = jl[j]; jl[j]=i;
1258:         }
1259:         i = nexti;
1260:       }
1261:       /*-- row sum --*/
1262:       jmin = a->i[k]; jmax = a->i[k+1];
1263:       for (i=jmin; i<jmax; i++) {
1264:         for (j=0; j<bs; j++) {
1265:           v = a->a + i*bs2 + j;
1266:           for (k1=0; k1<bs; k1++) {
1267:             sum[j] += PetscAbsScalar(*v); v += bs;
1268:           }
1269:         }
1270:       }
1271:       /* add k_th block row to il, jl */
1272:       col = aj+jmin;
1273:       if (*col == k) jmin++;
1274:       if (jmin < jmax) {
1275:         il[k] = jmin;
1276:         j = a->j[jmin]; jl[k] = jl[j]; jl[j] = k;
1277:       }
1278:       for (j=0; j<bs; j++) {
1279:         if (sum[j] > *norm) *norm = sum[j];
1280:       }
1281:     }
1282:     PetscFree3(sum,il,jl);
1283:     PetscLogFlops(PetscMax(mbs*a->nz-1,0));
1284:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for this norm yet");
1285:   return(0);
1286: }

1288: PetscErrorCode MatEqual_SeqSBAIJ(Mat A,Mat B,PetscBool * flg)
1289: {
1290:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)B->data;

1294:   /* If the  matrix/block dimensions are not equal, or no of nonzeros or shift */
1295:   if ((A->rmap->N != B->rmap->N) || (A->cmap->n != B->cmap->n) || (A->rmap->bs != B->rmap->bs)|| (a->nz != b->nz)) {
1296:     *flg = PETSC_FALSE;
1297:     return(0);
1298:   }

1300:   /* if the a->i are the same */
1301:   PetscArraycmp(a->i,b->i,a->mbs+1,flg);
1302:   if (!*flg) return(0);

1304:   /* if a->j are the same */
1305:   PetscArraycmp(a->j,b->j,a->nz,flg);
1306:   if (!*flg) return(0);

1308:   /* if a->a are the same */
1309:   PetscArraycmp(a->a,b->a,(a->nz)*(A->rmap->bs)*(A->rmap->bs),flg);
1310:   return(0);
1311: }

1313: PetscErrorCode MatGetDiagonal_SeqSBAIJ(Mat A,Vec v)
1314: {
1315:   Mat_SeqSBAIJ    *a = (Mat_SeqSBAIJ*)A->data;
1316:   PetscErrorCode  ierr;
1317:   PetscInt        i,j,k,row,bs,ambs,bs2;
1318:   const PetscInt  *ai,*aj;
1319:   PetscScalar     *x,zero = 0.0;
1320:   const MatScalar *aa,*aa_j;

1323:   bs = A->rmap->bs;
1324:   if (A->factortype && bs>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix with bs>1");

1326:   aa   = a->a;
1327:   ambs = a->mbs;

1329:   if (A->factortype == MAT_FACTOR_CHOLESKY || A->factortype == MAT_FACTOR_ICC) {
1330:     PetscInt *diag=a->diag;
1331:     aa   = a->a;
1332:     ambs = a->mbs;
1333:     VecGetArray(v,&x);
1334:     for (i=0; i<ambs; i++) x[i] = 1.0/aa[diag[i]];
1335:     VecRestoreArray(v,&x);
1336:     return(0);
1337:   }

1339:   ai   = a->i;
1340:   aj   = a->j;
1341:   bs2  = a->bs2;
1342:   VecSet(v,zero);
1343:   if (!a->nz) return(0);
1344:   VecGetArray(v,&x);
1345:   for (i=0; i<ambs; i++) {
1346:     j=ai[i];
1347:     if (aj[j] == i) {    /* if this is a diagonal element */
1348:       row  = i*bs;
1349:       aa_j = aa + j*bs2;
1350:       for (k=0; k<bs2; k+=(bs+1),row++) x[row] = aa_j[k];
1351:     }
1352:   }
1353:   VecRestoreArray(v,&x);
1354:   return(0);
1355: }

1357: PetscErrorCode MatDiagonalScale_SeqSBAIJ(Mat A,Vec ll,Vec rr)
1358: {
1359:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1360:   PetscScalar       x;
1361:   const PetscScalar *l,*li,*ri;
1362:   MatScalar         *aa,*v;
1363:   PetscErrorCode    ierr;
1364:   PetscInt          i,j,k,lm,M,m,mbs,tmp,bs,bs2;
1365:   const PetscInt    *ai,*aj;
1366:   PetscBool         flg;

1369:   if (ll != rr) {
1370:     VecEqual(ll,rr,&flg);
1371:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"For symmetric format, left and right scaling vectors must be same\n");
1372:   }
1373:   if (!ll) return(0);
1374:   ai  = a->i;
1375:   aj  = a->j;
1376:   aa  = a->a;
1377:   m   = A->rmap->N;
1378:   bs  = A->rmap->bs;
1379:   mbs = a->mbs;
1380:   bs2 = a->bs2;

1382:   VecGetArrayRead(ll,&l);
1383:   VecGetLocalSize(ll,&lm);
1384:   if (lm != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1385:   for (i=0; i<mbs; i++) { /* for each block row */
1386:     M  = ai[i+1] - ai[i];
1387:     li = l + i*bs;
1388:     v  = aa + bs2*ai[i];
1389:     for (j=0; j<M; j++) { /* for each block */
1390:       ri = l + bs*aj[ai[i]+j];
1391:       for (k=0; k<bs; k++) {
1392:         x = ri[k];
1393:         for (tmp=0; tmp<bs; tmp++) (*v++) *= li[tmp]*x;
1394:       }
1395:     }
1396:   }
1397:   VecRestoreArrayRead(ll,&l);
1398:   PetscLogFlops(2.0*a->nz);
1399:   return(0);
1400: }

1402: PetscErrorCode MatGetInfo_SeqSBAIJ(Mat A,MatInfoType flag,MatInfo *info)
1403: {
1404:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;

1407:   info->block_size   = a->bs2;
1408:   info->nz_allocated = a->bs2*a->maxnz;   /*num. of nonzeros in upper triangular part */
1409:   info->nz_used      = a->bs2*a->nz;   /*num. of nonzeros in upper triangular part */
1410:   info->nz_unneeded  = info->nz_allocated - info->nz_used;
1411:   info->assemblies   = A->num_ass;
1412:   info->mallocs      = A->info.mallocs;
1413:   info->memory       = ((PetscObject)A)->mem;
1414:   if (A->factortype) {
1415:     info->fill_ratio_given  = A->info.fill_ratio_given;
1416:     info->fill_ratio_needed = A->info.fill_ratio_needed;
1417:     info->factor_mallocs    = A->info.factor_mallocs;
1418:   } else {
1419:     info->fill_ratio_given  = 0;
1420:     info->fill_ratio_needed = 0;
1421:     info->factor_mallocs    = 0;
1422:   }
1423:   return(0);
1424: }

1426: PetscErrorCode MatZeroEntries_SeqSBAIJ(Mat A)
1427: {
1428:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;

1432:   PetscArrayzero(a->a,a->bs2*a->i[a->mbs]);
1433:   return(0);
1434: }

1436: /*
1437:    This code does not work since it only checks the upper triangular part of
1438:   the matrix. Hence it is not listed in the function table.
1439: */
1440: PetscErrorCode MatGetRowMaxAbs_SeqSBAIJ(Mat A,Vec v,PetscInt idx[])
1441: {
1442:   Mat_SeqSBAIJ    *a = (Mat_SeqSBAIJ*)A->data;
1443:   PetscErrorCode  ierr;
1444:   PetscInt        i,j,n,row,col,bs,mbs;
1445:   const PetscInt  *ai,*aj;
1446:   PetscReal       atmp;
1447:   const MatScalar *aa;
1448:   PetscScalar     *x;
1449:   PetscInt        ncols,brow,bcol,krow,kcol;

1452:   if (idx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Send email to petsc-maint@mcs.anl.gov");
1453:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1454:   bs  = A->rmap->bs;
1455:   aa  = a->a;
1456:   ai  = a->i;
1457:   aj  = a->j;
1458:   mbs = a->mbs;

1460:   VecSet(v,0.0);
1461:   VecGetArray(v,&x);
1462:   VecGetLocalSize(v,&n);
1463:   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1464:   for (i=0; i<mbs; i++) {
1465:     ncols = ai[1] - ai[0]; ai++;
1466:     brow  = bs*i;
1467:     for (j=0; j<ncols; j++) {
1468:       bcol = bs*(*aj);
1469:       for (kcol=0; kcol<bs; kcol++) {
1470:         col = bcol + kcol;      /* col index */
1471:         for (krow=0; krow<bs; krow++) {
1472:           atmp = PetscAbsScalar(*aa); aa++;
1473:           row  = brow + krow;   /* row index */
1474:           if (PetscRealPart(x[row]) < atmp) x[row] = atmp;
1475:           if (*aj > i && PetscRealPart(x[col]) < atmp) x[col] = atmp;
1476:         }
1477:       }
1478:       aj++;
1479:     }
1480:   }
1481:   VecRestoreArray(v,&x);
1482:   return(0);
1483: }

1485: PETSC_INTERN PetscErrorCode MatMatMult_SeqSBAIJ_SeqDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
1486: {

1490:   if (scall == MAT_INITIAL_MATRIX) {
1491:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
1492:     MatMatMultSymbolic_SeqSBAIJ_SeqDense(A,B,fill,C);
1493:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
1494:   }
1495:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
1496:   MatMatMultNumeric_SeqSBAIJ_SeqDense(A,B,*C);
1497:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
1498:   return(0);
1499: }

1501: PetscErrorCode MatMatMultSymbolic_SeqSBAIJ_SeqDense(Mat A,Mat B,PetscReal fill,Mat *C)
1502: {

1506:   MatMatMultSymbolic_SeqDense_SeqDense(A,B,0.0,C);

1508:   (*C)->ops->matmultnumeric = MatMatMultNumeric_SeqSBAIJ_SeqDense;
1509:   return(0);
1510: }

1512: PetscErrorCode MatMatMult_SeqSBAIJ_1_Private(Mat A,PetscScalar* b,PetscInt bm,PetscScalar* c,PetscInt cm,PetscInt cn)
1513: {
1514:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1515:   PetscScalar       *z = c;
1516:   const PetscScalar *xb;
1517:   PetscScalar       x1;
1518:   const MatScalar   *v = a->a,*vv;
1519:   PetscInt          mbs = a->mbs,i,*idx = a->j,*ii = a->i,j,*jj,n,k;

1522:   for (i=0; i<mbs; i++) {
1523:     n = ii[1] - ii[0]; ii++;
1524:     PetscPrefetchBlock(idx+n,n,0,PETSC_PREFETCH_HINT_NTA);   /* Indices for the next row (assumes same size as this one) */
1525:     PetscPrefetchBlock(v+n,n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
1526:     jj = idx;
1527:     vv = v;
1528:     for (k=0; k<cn; k++) {
1529:       idx = jj;
1530:       v = vv;
1531:       for (j=0; j<n; j++) {
1532:         xb = b + (*idx); x1 = xb[0+k*bm];
1533:         z[0+k*cm] += v[0]*x1;
1534:         if (*idx != i) c[(*idx)+k*cm] += v[0]*b[i+k*bm];
1535:         v += 1;
1536:         ++idx;
1537:       }
1538:     }
1539:     z += 1;
1540:   }
1541:   return(0);
1542: }

1544: PetscErrorCode MatMatMult_SeqSBAIJ_2_Private(Mat A,PetscScalar* b,PetscInt bm,PetscScalar* c,PetscInt cm,PetscInt cn)
1545: {
1546:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1547:   PetscScalar       *z = c;
1548:   const PetscScalar *xb;
1549:   PetscScalar       x1,x2;
1550:   const MatScalar   *v = a->a,*vv;
1551:   PetscInt          mbs = a->mbs,i,*idx = a->j,*ii = a->i,j,*jj,n,k;

1554:   for (i=0; i<mbs; i++) {
1555:     n = ii[1] - ii[0]; ii++;
1556:     PetscPrefetchBlock(idx+n,n,0,PETSC_PREFETCH_HINT_NTA);   /* Indices for the next row (assumes same size as this one) */
1557:     PetscPrefetchBlock(v+4*n,4*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
1558:     jj = idx;
1559:     vv = v;
1560:     for (k=0; k<cn; k++) {
1561:       idx = jj;
1562:       v = vv;
1563:       for (j=0; j<n; j++) {
1564:         xb    = b + 2*(*idx); x1 = xb[0+k*bm]; x2 = xb[1+k*bm];
1565:         z[0+k*cm] += v[0]*x1 + v[2]*x2;
1566:         z[1+k*cm] += v[1]*x1 + v[3]*x2;
1567:         if (*idx != i) {
1568:           c[2*(*idx)+0+k*cm] += v[0]*b[2*i+k*bm] + v[1]*b[2*i+1+k*bm];
1569:           c[2*(*idx)+1+k*cm] += v[2]*b[2*i+k*bm] + v[3]*b[2*i+1+k*bm];
1570:         }
1571:         v += 4;
1572:         ++idx;
1573:       }
1574:     }
1575:     z += 2;
1576:   }
1577:   return(0);
1578: }

1580: PetscErrorCode MatMatMult_SeqSBAIJ_3_Private(Mat A,PetscScalar* b,PetscInt bm,PetscScalar* c,PetscInt cm,PetscInt cn)
1581: {
1582:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1583:   PetscScalar       *z = c;
1584:   const PetscScalar *xb;
1585:   PetscScalar       x1,x2,x3;
1586:   const MatScalar   *v = a->a,*vv;
1587:   PetscInt          mbs = a->mbs,i,*idx = a->j,*ii = a->i,j,*jj,n,k;

1590:   for (i=0; i<mbs; i++) {
1591:     n = ii[1] - ii[0]; ii++;
1592:     PetscPrefetchBlock(idx+n,n,0,PETSC_PREFETCH_HINT_NTA);   /* Indices for the next row (assumes same size as this one) */
1593:     PetscPrefetchBlock(v+9*n,9*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
1594:     jj = idx;
1595:     vv = v;
1596:     for (k=0; k<cn; k++) {
1597:       idx = jj;
1598:       v = vv;
1599:       for (j=0; j<n; j++) {
1600:         xb = b + 3*(*idx); x1 = xb[0+k*bm]; x2 = xb[1+k*bm]; x3 = xb[2+k*bm];
1601:         z[0+k*cm] += v[0]*x1 + v[3]*x2 + v[6]*x3;
1602:         z[1+k*cm] += v[1]*x1 + v[4]*x2 + v[7]*x3;
1603:         z[2+k*cm] += v[2]*x1 + v[5]*x2 + v[8]*x3;
1604:         if (*idx != i) {
1605:           c[3*(*idx)+0+k*cm] += v[0]*b[3*i+k*bm] + v[3]*b[3*i+1+k*bm] + v[6]*b[3*i+2+k*bm];
1606:           c[3*(*idx)+1+k*cm] += v[1]*b[3*i+k*bm] + v[4]*b[3*i+1+k*bm] + v[7]*b[3*i+2+k*bm];
1607:           c[3*(*idx)+2+k*cm] += v[2]*b[3*i+k*bm] + v[5]*b[3*i+1+k*bm] + v[8]*b[3*i+2+k*bm];
1608:         }
1609:         v += 9;
1610:         ++idx;
1611:       }
1612:     }
1613:     z += 3;
1614:   }
1615:   return(0);
1616: }

1618: PetscErrorCode MatMatMult_SeqSBAIJ_4_Private(Mat A,PetscScalar* b,PetscInt bm,PetscScalar* c,PetscInt cm,PetscInt cn)
1619: {
1620:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1621:   PetscScalar       *z = c;
1622:   const PetscScalar *xb;
1623:   PetscScalar       x1,x2,x3,x4;
1624:   const MatScalar   *v = a->a,*vv;
1625:   PetscInt          mbs = a->mbs,i,*idx = a->j,*ii = a->i,j,*jj,n,k;

1628:   for (i=0; i<mbs; i++) {
1629:     n = ii[1] - ii[0]; ii++;
1630:     PetscPrefetchBlock(idx+n,n,0,PETSC_PREFETCH_HINT_NTA);   /* Indices for the next row (assumes same size as this one) */
1631:     PetscPrefetchBlock(v+16*n,16*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
1632:     jj = idx;
1633:     vv = v;
1634:     for (k=0; k<cn; k++) {
1635:       idx = jj;
1636:       v = vv;
1637:       for (j=0; j<n; j++) {
1638:         xb = b + 4*(*idx); x1 = xb[0+k*bm]; x2 = xb[1+k*bm]; x3 = xb[2+k*bm]; x4 = xb[3+k*bm];
1639:         z[0+k*cm] += v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
1640:         z[1+k*cm] += v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
1641:         z[2+k*cm] += v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
1642:         z[3+k*cm] += v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
1643:         if (*idx != i) {
1644:           c[4*(*idx)+0+k*cm] += v[0]*b[4*i+k*bm] + v[4]*b[4*i+1+k*bm] + v[8]*b[4*i+2+k*bm]  + v[12]*b[4*i+3+k*bm];
1645:           c[4*(*idx)+1+k*cm] += v[1]*b[4*i+k*bm] + v[5]*b[4*i+1+k*bm] + v[9]*b[4*i+2+k*bm]  + v[13]*b[4*i+3+k*bm];
1646:           c[4*(*idx)+2+k*cm] += v[2]*b[4*i+k*bm] + v[6]*b[4*i+1+k*bm] + v[10]*b[4*i+2+k*bm] + v[14]*b[4*i+3+k*bm];
1647:           c[4*(*idx)+3+k*cm] += v[3]*b[4*i+k*bm] + v[7]*b[4*i+1+k*bm] + v[11]*b[4*i+2+k*bm] + v[15]*b[4*i+3+k*bm];
1648:         }
1649:         v += 16;
1650:         ++idx;
1651:       }
1652:     }
1653:     z += 4;
1654:   }
1655:   return(0);
1656: }

1658: PetscErrorCode MatMatMult_SeqSBAIJ_5_Private(Mat A,PetscScalar* b,PetscInt bm,PetscScalar* c,PetscInt cm,PetscInt cn)
1659: {
1660:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1661:   PetscScalar       *z = c;
1662:   const PetscScalar *xb;
1663:   PetscScalar       x1,x2,x3,x4,x5;
1664:   const MatScalar   *v = a->a,*vv;
1665:   PetscInt          mbs = a->mbs,i,*idx = a->j,*ii = a->i,j,*jj,n,k;

1668:   for (i=0; i<mbs; i++) {
1669:     n = ii[1] - ii[0]; ii++;
1670:     PetscPrefetchBlock(idx+n,n,0,PETSC_PREFETCH_HINT_NTA);   /* Indices for the next row (assumes same size as this one) */
1671:     PetscPrefetchBlock(v+25*n,25*n,0,PETSC_PREFETCH_HINT_NTA); /* Entries for the next row */
1672:     jj = idx;
1673:     vv = v;
1674:     for (k=0; k<cn; k++) {
1675:       idx = jj;
1676:       v = vv;
1677:       for (j=0; j<n; j++) {
1678:         xb = b + 5*(*idx); x1 = xb[0+k*bm]; x2 = xb[1+k*bm]; x3 = xb[2+k*bm]; x4 = xb[3+k*bm]; x5 = xb[4+k*cm];
1679:         z[0+k*cm] += v[0]*x1 + v[5]*x2 + v[10]*x3 + v[15]*x4 + v[20]*x5;
1680:         z[1+k*cm] += v[1]*x1 + v[6]*x2 + v[11]*x3 + v[16]*x4 + v[21]*x5;
1681:         z[2+k*cm] += v[2]*x1 + v[7]*x2 + v[12]*x3 + v[17]*x4 + v[22]*x5;
1682:         z[3+k*cm] += v[3]*x1 + v[8]*x2 + v[13]*x3 + v[18]*x4 + v[23]*x5;
1683:         z[4+k*cm] += v[4]*x1 + v[9]*x2 + v[14]*x3 + v[19]*x4 + v[24]*x5;
1684:         if (*idx != i) {
1685:           c[5*(*idx)+0+k*cm] += v[0]*b[5*i+k*bm] + v[5]*b[5*i+1+k*bm] + v[10]*b[5*i+2+k*bm] + v[15]*b[5*i+3+k*bm] + v[20]*b[5*i+4+k*bm];
1686:           c[5*(*idx)+1+k*cm] += v[1]*b[5*i+k*bm] + v[6]*b[5*i+1+k*bm] + v[11]*b[5*i+2+k*bm] + v[16]*b[5*i+3+k*bm] + v[21]*b[5*i+4+k*bm];
1687:           c[5*(*idx)+2+k*cm] += v[2]*b[5*i+k*bm] + v[7]*b[5*i+1+k*bm] + v[12]*b[5*i+2+k*bm] + v[17]*b[5*i+3+k*bm] + v[22]*b[5*i+4+k*bm];
1688:           c[5*(*idx)+3+k*cm] += v[3]*b[5*i+k*bm] + v[8]*b[5*i+1+k*bm] + v[13]*b[5*i+2+k*bm] + v[18]*b[5*i+3+k*bm] + v[23]*b[5*i+4+k*bm];
1689:           c[5*(*idx)+4+k*cm] += v[4]*b[5*i+k*bm] + v[9]*b[5*i+1+k*bm] + v[14]*b[5*i+2+k*bm] + v[19]*b[5*i+3+k*bm] + v[24]*b[5*i+4+k*bm];
1690:         }
1691:         v += 25;
1692:         ++idx;
1693:       }
1694:     }
1695:     z += 5;
1696:   }
1697:   return(0);
1698: }

1700: PetscErrorCode MatMatMultNumeric_SeqSBAIJ_SeqDense(Mat A,Mat B,Mat C)
1701: {
1702:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
1703:   Mat_SeqDense      *bd = (Mat_SeqDense*)B->data;
1704:   Mat_SeqDense      *cd = (Mat_SeqDense*)B->data;
1705:   PetscInt          cm=cd->lda,cn=B->cmap->n,bm=bd->lda;
1706:   PetscInt          mbs,i,bs=A->rmap->bs,j,n,bs2=a->bs2;
1707:   PetscBLASInt      bbs,bcn,bbm,bcm;
1708:   PetscScalar       *z = 0;
1709:   PetscScalar       *c,*b;
1710:   const MatScalar   *v;
1711:   const PetscInt    *idx,*ii;
1712:   PetscScalar       _DOne=1.0;
1713:   PetscErrorCode    ierr;

1716:   if (!cm || !cn) return(0);
1717:   if (B->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number columns in A %D not equal rows in B %D\n",A->cmap->n,B->rmap->n);
1718:   if (A->rmap->n != C->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows in C %D not equal rows in A %D\n",C->rmap->n,A->rmap->n);
1719:   if (B->cmap->n != C->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number columns in B %D not equal columns in C %D\n",B->cmap->n,C->cmap->n);
1720:   b = bd->v;
1721:   MatZeroEntries(C);
1722:   MatDenseGetArray(C,&c);
1723:   switch (bs) {
1724:   case 1:
1725:     MatMatMult_SeqSBAIJ_1_Private(A, b, bm, c, cm, cn);
1726:     break;
1727:   case 2:
1728:     MatMatMult_SeqSBAIJ_2_Private(A, b, bm, c, cm, cn);
1729:     break;
1730:   case 3:
1731:     MatMatMult_SeqSBAIJ_3_Private(A, b, bm, c, cm, cn);
1732:     break;
1733:   case 4:
1734:     MatMatMult_SeqSBAIJ_4_Private(A, b, bm, c, cm, cn);
1735:     break;
1736:   case 5:
1737:     MatMatMult_SeqSBAIJ_5_Private(A, b, bm, c, cm, cn);
1738:     break;
1739:   default: /* block sizes larger than 5 by 5 are handled by BLAS */
1740:     PetscBLASIntCast(bs,&bbs);
1741:     PetscBLASIntCast(cn,&bcn);
1742:     PetscBLASIntCast(bm,&bbm);
1743:     PetscBLASIntCast(cm,&bcm);
1744:     idx = a->j;
1745:     v   = a->a;
1746:     mbs = a->mbs;
1747:     ii  = a->i;
1748:     z   = c;
1749:     for (i=0; i<mbs; i++) {
1750:       n = ii[1] - ii[0]; ii++;
1751:       for (j=0; j<n; j++) {
1752:         if (*idx != i)
1753:           PetscStackCallBLAS("BLASgemm",BLASgemm_("T","N",&bbs,&bcn,&bbs,&_DOne,v,&bbs,b+bs*i,&bbm,&_DOne,c+bs*(*idx),&bcm));
1754:         PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&bbs,&bcn,&bbs,&_DOne,v,&bbs,b+bs*(*idx++),&bbm,&_DOne,z,&bcm));
1755:         v += bs2;
1756:       }
1757:       z += bs;
1758:     }
1759:   }
1760:   MatDenseRestoreArray(C,&c);
1761:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1762:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
1763:   PetscLogFlops((2.0*(a->nz*2.0 - a->nonzerorowcnt)*bs2 - a->nonzerorowcnt)*cn);
1764:   return(0);
1765: }