Actual source code: baij.c

petsc-3.14.6 2021-03-30
Report Typos and Errors

  2: /*
  3:     Defines the basic matrix operations for the BAIJ (compressed row)
  4:   matrix storage format.
  5: */
  6: #include <../src/mat/impls/baij/seq/baij.h>
  7: #include <petscblaslapack.h>
  8: #include <petsc/private/kernels/blockinvert.h>
  9: #include <petsc/private/kernels/blockmatmult.h>

 11: #if defined(PETSC_HAVE_HYPRE)
 12: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat,MatType,MatReuse,Mat*);
 13: #endif

 15: #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
 16: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqBAIJMKL(Mat,MatType,MatReuse,Mat*);
 17: #endif
 18: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);

 20: PetscErrorCode MatInvertBlockDiagonal_SeqBAIJ(Mat A,const PetscScalar **values)
 21: {
 22:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*) A->data;
 24:   PetscInt       *diag_offset,i,bs = A->rmap->bs,mbs = a->mbs,ipvt[5],bs2 = bs*bs,*v_pivots;
 25:   MatScalar      *v    = a->a,*odiag,*diag,work[25],*v_work;
 26:   PetscReal      shift = 0.0;
 27:   PetscBool      allowzeropivot,zeropivotdetected=PETSC_FALSE;

 30:   allowzeropivot = PetscNot(A->erroriffailure);

 32:   if (a->idiagvalid) {
 33:     if (values) *values = a->idiag;
 34:     return(0);
 35:   }
 36:   MatMarkDiagonal_SeqBAIJ(A);
 37:   diag_offset = a->diag;
 38:   if (!a->idiag) {
 39:     PetscMalloc1(bs2*mbs,&a->idiag);
 40:     PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
 41:   }
 42:   diag  = a->idiag;
 43:   if (values) *values = a->idiag;
 44:   /* factor and invert each block */
 45:   switch (bs) {
 46:   case 1:
 47:     for (i=0; i<mbs; i++) {
 48:       odiag    = v + 1*diag_offset[i];
 49:       diag[0]  = odiag[0];

 51:       if (PetscAbsScalar(diag[0] + shift) < PETSC_MACHINE_EPSILON) {
 52:         if (allowzeropivot) {
 53:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 54:           A->factorerror_zeropivot_value = PetscAbsScalar(diag[0]);
 55:           A->factorerror_zeropivot_row   = i;
 56:           PetscInfo1(A,"Zero pivot, row %D\n",i);
 57:         } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D pivot value %g tolerance %g",i,(double)PetscAbsScalar(diag[0]),(double)PETSC_MACHINE_EPSILON);
 58:       }

 60:       diag[0]  = (PetscScalar)1.0 / (diag[0] + shift);
 61:       diag    += 1;
 62:     }
 63:     break;
 64:   case 2:
 65:     for (i=0; i<mbs; i++) {
 66:       odiag    = v + 4*diag_offset[i];
 67:       diag[0]  = odiag[0]; diag[1] = odiag[1]; diag[2] = odiag[2]; diag[3] = odiag[3];
 68:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
 69:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 70:       diag    += 4;
 71:     }
 72:     break;
 73:   case 3:
 74:     for (i=0; i<mbs; i++) {
 75:       odiag    = v + 9*diag_offset[i];
 76:       diag[0]  = odiag[0]; diag[1] = odiag[1]; diag[2] = odiag[2]; diag[3] = odiag[3];
 77:       diag[4]  = odiag[4]; diag[5] = odiag[5]; diag[6] = odiag[6]; diag[7] = odiag[7];
 78:       diag[8]  = odiag[8];
 79:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
 80:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 81:       diag    += 9;
 82:     }
 83:     break;
 84:   case 4:
 85:     for (i=0; i<mbs; i++) {
 86:       odiag  = v + 16*diag_offset[i];
 87:       PetscArraycpy(diag,odiag,16);
 88:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
 89:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 90:       diag  += 16;
 91:     }
 92:     break;
 93:   case 5:
 94:     for (i=0; i<mbs; i++) {
 95:       odiag  = v + 25*diag_offset[i];
 96:       PetscArraycpy(diag,odiag,25);
 97:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
 98:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 99:       diag  += 25;
100:     }
101:     break;
102:   case 6:
103:     for (i=0; i<mbs; i++) {
104:       odiag  = v + 36*diag_offset[i];
105:       PetscArraycpy(diag,odiag,36);
106:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
107:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
108:       diag  += 36;
109:     }
110:     break;
111:   case 7:
112:     for (i=0; i<mbs; i++) {
113:       odiag  = v + 49*diag_offset[i];
114:       PetscArraycpy(diag,odiag,49);
115:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
116:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
117:       diag  += 49;
118:     }
119:     break;
120:   default:
121:     PetscMalloc2(bs,&v_work,bs,&v_pivots);
122:     for (i=0; i<mbs; i++) {
123:       odiag  = v + bs2*diag_offset[i];
124:       PetscArraycpy(diag,odiag,bs2);
125:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
126:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
127:       diag  += bs2;
128:     }
129:     PetscFree2(v_work,v_pivots);
130:   }
131:   a->idiagvalid = PETSC_TRUE;
132:   return(0);
133: }

135: PetscErrorCode MatSOR_SeqBAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
136: {
137:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
138:   PetscScalar       *x,*work,*w,*workt,*t;
139:   const MatScalar   *v,*aa = a->a, *idiag;
140:   const PetscScalar *b,*xb;
141:   PetscScalar       s[7], xw[7]={0}; /* avoid some compilers thinking xw is uninitialized */
142:   PetscErrorCode    ierr;
143:   PetscInt          m = a->mbs,i,i2,nz,bs = A->rmap->bs,bs2 = bs*bs,k,j,idx,it;
144:   const PetscInt    *diag,*ai = a->i,*aj = a->j,*vi;

147:   its = its*lits;
148:   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
149:   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
150:   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
151:   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
152:   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");

154:   if (!a->idiagvalid) {MatInvertBlockDiagonal(A,NULL);}

156:   if (!m) return(0);
157:   diag  = a->diag;
158:   idiag = a->idiag;
159:   k    = PetscMax(A->rmap->n,A->cmap->n);
160:   if (!a->mult_work) {
161:     PetscMalloc1(k+1,&a->mult_work);
162:   }
163:   if (!a->sor_workt) {
164:     PetscMalloc1(k,&a->sor_workt);
165:   }
166:   if (!a->sor_work) {
167:     PetscMalloc1(bs,&a->sor_work);
168:   }
169:   work = a->mult_work;
170:   t    = a->sor_workt;
171:   w    = a->sor_work;

173:   VecGetArray(xx,&x);
174:   VecGetArrayRead(bb,&b);

176:   if (flag & SOR_ZERO_INITIAL_GUESS) {
177:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
178:       switch (bs) {
179:       case 1:
180:         PetscKernel_v_gets_A_times_w_1(x,idiag,b);
181:         t[0] = b[0];
182:         i2     = 1;
183:         idiag += 1;
184:         for (i=1; i<m; i++) {
185:           v  = aa + ai[i];
186:           vi = aj + ai[i];
187:           nz = diag[i] - ai[i];
188:           s[0] = b[i2];
189:           for (j=0; j<nz; j++) {
190:             xw[0] = x[vi[j]];
191:             PetscKernel_v_gets_v_minus_A_times_w_1(s,(v+j),xw);
192:           }
193:           t[i2] = s[0];
194:           PetscKernel_v_gets_A_times_w_1(xw,idiag,s);
195:           x[i2]  = xw[0];
196:           idiag += 1;
197:           i2    += 1;
198:         }
199:         break;
200:       case 2:
201:         PetscKernel_v_gets_A_times_w_2(x,idiag,b);
202:         t[0] = b[0]; t[1] = b[1];
203:         i2     = 2;
204:         idiag += 4;
205:         for (i=1; i<m; i++) {
206:           v  = aa + 4*ai[i];
207:           vi = aj + ai[i];
208:           nz = diag[i] - ai[i];
209:           s[0] = b[i2]; s[1] = b[i2+1];
210:           for (j=0; j<nz; j++) {
211:             idx = 2*vi[j];
212:             it  = 4*j;
213:             xw[0] = x[idx]; xw[1] = x[1+idx];
214:             PetscKernel_v_gets_v_minus_A_times_w_2(s,(v+it),xw);
215:           }
216:           t[i2] = s[0]; t[i2+1] = s[1];
217:           PetscKernel_v_gets_A_times_w_2(xw,idiag,s);
218:           x[i2]   = xw[0]; x[i2+1] = xw[1];
219:           idiag  += 4;
220:           i2     += 2;
221:         }
222:         break;
223:       case 3:
224:         PetscKernel_v_gets_A_times_w_3(x,idiag,b);
225:         t[0] = b[0]; t[1] = b[1]; t[2] = b[2];
226:         i2     = 3;
227:         idiag += 9;
228:         for (i=1; i<m; i++) {
229:           v  = aa + 9*ai[i];
230:           vi = aj + ai[i];
231:           nz = diag[i] - ai[i];
232:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2];
233:           while (nz--) {
234:             idx = 3*(*vi++);
235:             xw[0] = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx];
236:             PetscKernel_v_gets_v_minus_A_times_w_3(s,v,xw);
237:             v  += 9;
238:           }
239:           t[i2] = s[0]; t[i2+1] = s[1]; t[i2+2] = s[2];
240:           PetscKernel_v_gets_A_times_w_3(xw,idiag,s);
241:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2];
242:           idiag  += 9;
243:           i2     += 3;
244:         }
245:         break;
246:       case 4:
247:         PetscKernel_v_gets_A_times_w_4(x,idiag,b);
248:         t[0] = b[0]; t[1] = b[1]; t[2] = b[2]; t[3] = b[3];
249:         i2     = 4;
250:         idiag += 16;
251:         for (i=1; i<m; i++) {
252:           v  = aa + 16*ai[i];
253:           vi = aj + ai[i];
254:           nz = diag[i] - ai[i];
255:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3];
256:           while (nz--) {
257:             idx = 4*(*vi++);
258:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx];
259:             PetscKernel_v_gets_v_minus_A_times_w_4(s,v,xw);
260:             v  += 16;
261:           }
262:           t[i2] = s[0]; t[i2+1] = s[1]; t[i2+2] = s[2]; t[i2 + 3] = s[3];
263:           PetscKernel_v_gets_A_times_w_4(xw,idiag,s);
264:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3];
265:           idiag  += 16;
266:           i2     += 4;
267:         }
268:         break;
269:       case 5:
270:         PetscKernel_v_gets_A_times_w_5(x,idiag,b);
271:         t[0] = b[0]; t[1] = b[1]; t[2] = b[2]; t[3] = b[3]; t[4] = b[4];
272:         i2     = 5;
273:         idiag += 25;
274:         for (i=1; i<m; i++) {
275:           v  = aa + 25*ai[i];
276:           vi = aj + ai[i];
277:           nz = diag[i] - ai[i];
278:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3]; s[4] = b[i2+4];
279:           while (nz--) {
280:             idx = 5*(*vi++);
281:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx]; xw[4] = x[4+idx];
282:             PetscKernel_v_gets_v_minus_A_times_w_5(s,v,xw);
283:             v  += 25;
284:           }
285:           t[i2] = s[0]; t[i2+1] = s[1]; t[i2+2] = s[2]; t[i2+3] = s[3]; t[i2+4] = s[4];
286:           PetscKernel_v_gets_A_times_w_5(xw,idiag,s);
287:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3]; x[i2+4] = xw[4];
288:           idiag  += 25;
289:           i2     += 5;
290:         }
291:         break;
292:       case 6:
293:         PetscKernel_v_gets_A_times_w_6(x,idiag,b);
294:         t[0] = b[0]; t[1] = b[1]; t[2] = b[2]; t[3] = b[3]; t[4] = b[4]; t[5] = b[5];
295:         i2     = 6;
296:         idiag += 36;
297:         for (i=1; i<m; i++) {
298:           v  = aa + 36*ai[i];
299:           vi = aj + ai[i];
300:           nz = diag[i] - ai[i];
301:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3]; s[4] = b[i2+4]; s[5] = b[i2+5];
302:           while (nz--) {
303:             idx = 6*(*vi++);
304:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
305:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx];
306:             PetscKernel_v_gets_v_minus_A_times_w_6(s,v,xw);
307:             v  += 36;
308:           }
309:           t[i2]   = s[0]; t[i2+1] = s[1]; t[i2+2] = s[2];
310:           t[i2+3] = s[3]; t[i2+4] = s[4]; t[i2+5] = s[5];
311:           PetscKernel_v_gets_A_times_w_6(xw,idiag,s);
312:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3]; x[i2+4] = xw[4]; x[i2+5] = xw[5];
313:           idiag  += 36;
314:           i2     += 6;
315:         }
316:         break;
317:       case 7:
318:         PetscKernel_v_gets_A_times_w_7(x,idiag,b);
319:         t[0] = b[0]; t[1] = b[1]; t[2] = b[2];
320:         t[3] = b[3]; t[4] = b[4]; t[5] = b[5]; t[6] = b[6];
321:         i2     = 7;
322:         idiag += 49;
323:         for (i=1; i<m; i++) {
324:           v  = aa + 49*ai[i];
325:           vi = aj + ai[i];
326:           nz = diag[i] - ai[i];
327:           s[0] = b[i2];   s[1] = b[i2+1]; s[2] = b[i2+2];
328:           s[3] = b[i2+3]; s[4] = b[i2+4]; s[5] = b[i2+5]; s[6] = b[i2+6];
329:           while (nz--) {
330:             idx = 7*(*vi++);
331:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
332:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx]; xw[6] = x[6+idx];
333:             PetscKernel_v_gets_v_minus_A_times_w_7(s,v,xw);
334:             v  += 49;
335:           }
336:           t[i2]   = s[0]; t[i2+1] = s[1]; t[i2+2] = s[2];
337:           t[i2+3] = s[3]; t[i2+4] = s[4]; t[i2+5] = s[5]; t[i2+6] = s[6];
338:           PetscKernel_v_gets_A_times_w_7(xw,idiag,s);
339:           x[i2] =   xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2];
340:           x[i2+3] = xw[3]; x[i2+4] = xw[4]; x[i2+5] = xw[5]; x[i2+6] = xw[6];
341:           idiag  += 49;
342:           i2     += 7;
343:         }
344:         break;
345:       default:
346:         PetscKernel_w_gets_Ar_times_v(bs,bs,b,idiag,x);
347:         PetscArraycpy(t,b,bs);
348:         i2     = bs;
349:         idiag += bs2;
350:         for (i=1; i<m; i++) {
351:           v  = aa + bs2*ai[i];
352:           vi = aj + ai[i];
353:           nz = diag[i] - ai[i];

355:           PetscArraycpy(w,b+i2,bs);
356:           /* copy all rows of x that are needed into contiguous space */
357:           workt = work;
358:           for (j=0; j<nz; j++) {
359:             PetscArraycpy(workt,x + bs*(*vi++),bs);
360:             workt += bs;
361:           }
362:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
363:           PetscArraycpy(t+i2,w,bs);
364:           PetscKernel_w_gets_Ar_times_v(bs,bs,w,idiag,x+i2);

366:           idiag += bs2;
367:           i2    += bs;
368:         }
369:         break;
370:       }
371:       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
372:       PetscLogFlops(1.0*bs2*a->nz);
373:       xb = t;
374:     }
375:     else xb = b;
376:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
377:       idiag = a->idiag+bs2*(a->mbs-1);
378:       i2 = bs * (m-1);
379:       switch (bs) {
380:       case 1:
381:         s[0]  = xb[i2];
382:         PetscKernel_v_gets_A_times_w_1(xw,idiag,s);
383:         x[i2] = xw[0];
384:         i2   -= 1;
385:         for (i=m-2; i>=0; i--) {
386:           v  = aa + (diag[i]+1);
387:           vi = aj + diag[i] + 1;
388:           nz = ai[i+1] - diag[i] - 1;
389:           s[0] = xb[i2];
390:           for (j=0; j<nz; j++) {
391:             xw[0] = x[vi[j]];
392:             PetscKernel_v_gets_v_minus_A_times_w_1(s,(v+j),xw);
393:           }
394:           PetscKernel_v_gets_A_times_w_1(xw,idiag,s);
395:           x[i2]  = xw[0];
396:           idiag -= 1;
397:           i2    -= 1;
398:         }
399:         break;
400:       case 2:
401:         s[0]  = xb[i2]; s[1] = xb[i2+1];
402:         PetscKernel_v_gets_A_times_w_2(xw,idiag,s);
403:         x[i2] = xw[0]; x[i2+1] = xw[1];
404:         i2    -= 2;
405:         idiag -= 4;
406:         for (i=m-2; i>=0; i--) {
407:           v  = aa + 4*(diag[i] + 1);
408:           vi = aj + diag[i] + 1;
409:           nz = ai[i+1] - diag[i] - 1;
410:           s[0] = xb[i2]; s[1] = xb[i2+1];
411:           for (j=0; j<nz; j++) {
412:             idx = 2*vi[j];
413:             it  = 4*j;
414:             xw[0] = x[idx]; xw[1] = x[1+idx];
415:             PetscKernel_v_gets_v_minus_A_times_w_2(s,(v+it),xw);
416:           }
417:           PetscKernel_v_gets_A_times_w_2(xw,idiag,s);
418:           x[i2]   = xw[0]; x[i2+1] = xw[1];
419:           idiag  -= 4;
420:           i2     -= 2;
421:         }
422:         break;
423:       case 3:
424:         s[0]  = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2];
425:         PetscKernel_v_gets_A_times_w_3(xw,idiag,s);
426:         x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2];
427:         i2    -= 3;
428:         idiag -= 9;
429:         for (i=m-2; i>=0; i--) {
430:           v  = aa + 9*(diag[i]+1);
431:           vi = aj + diag[i] + 1;
432:           nz = ai[i+1] - diag[i] - 1;
433:           s[0] = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2];
434:           while (nz--) {
435:             idx = 3*(*vi++);
436:             xw[0] = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx];
437:             PetscKernel_v_gets_v_minus_A_times_w_3(s,v,xw);
438:             v  += 9;
439:           }
440:           PetscKernel_v_gets_A_times_w_3(xw,idiag,s);
441:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2];
442:           idiag  -= 9;
443:           i2     -= 3;
444:         }
445:         break;
446:       case 4:
447:         s[0]  = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2]; s[3] = xb[i2+3];
448:         PetscKernel_v_gets_A_times_w_4(xw,idiag,s);
449:         x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3];
450:         i2    -= 4;
451:         idiag -= 16;
452:         for (i=m-2; i>=0; i--) {
453:           v  = aa + 16*(diag[i]+1);
454:           vi = aj + diag[i] + 1;
455:           nz = ai[i+1] - diag[i] - 1;
456:           s[0] = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2]; s[3] = xb[i2+3];
457:           while (nz--) {
458:             idx = 4*(*vi++);
459:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx];
460:             PetscKernel_v_gets_v_minus_A_times_w_4(s,v,xw);
461:             v  += 16;
462:           }
463:           PetscKernel_v_gets_A_times_w_4(xw,idiag,s);
464:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3];
465:           idiag  -= 16;
466:           i2     -= 4;
467:         }
468:         break;
469:       case 5:
470:         s[0]  = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2]; s[3] = xb[i2+3]; s[4] = xb[i2+4];
471:         PetscKernel_v_gets_A_times_w_5(xw,idiag,s);
472:         x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3]; x[i2+4] = xw[4];
473:         i2    -= 5;
474:         idiag -= 25;
475:         for (i=m-2; i>=0; i--) {
476:           v  = aa + 25*(diag[i]+1);
477:           vi = aj + diag[i] + 1;
478:           nz = ai[i+1] - diag[i] - 1;
479:           s[0] = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2]; s[3] = xb[i2+3]; s[4] = xb[i2+4];
480:           while (nz--) {
481:             idx = 5*(*vi++);
482:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx]; xw[4] = x[4+idx];
483:             PetscKernel_v_gets_v_minus_A_times_w_5(s,v,xw);
484:             v  += 25;
485:           }
486:           PetscKernel_v_gets_A_times_w_5(xw,idiag,s);
487:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3]; x[i2+4] = xw[4];
488:           idiag  -= 25;
489:           i2     -= 5;
490:         }
491:         break;
492:       case 6:
493:         s[0]  = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2]; s[3] = xb[i2+3]; s[4] = xb[i2+4]; s[5] = xb[i2+5];
494:         PetscKernel_v_gets_A_times_w_6(xw,idiag,s);
495:         x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3]; x[i2+4] = xw[4]; x[i2+5] = xw[5];
496:         i2    -= 6;
497:         idiag -= 36;
498:         for (i=m-2; i>=0; i--) {
499:           v  = aa + 36*(diag[i]+1);
500:           vi = aj + diag[i] + 1;
501:           nz = ai[i+1] - diag[i] - 1;
502:           s[0] = xb[i2]; s[1] = xb[i2+1]; s[2] = xb[i2+2]; s[3] = xb[i2+3]; s[4] = xb[i2+4]; s[5] = xb[i2+5];
503:           while (nz--) {
504:             idx = 6*(*vi++);
505:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
506:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx];
507:             PetscKernel_v_gets_v_minus_A_times_w_6(s,v,xw);
508:             v  += 36;
509:           }
510:           PetscKernel_v_gets_A_times_w_6(xw,idiag,s);
511:           x[i2] = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2]; x[i2+3] = xw[3]; x[i2+4] = xw[4]; x[i2+5] = xw[5];
512:           idiag  -= 36;
513:           i2     -= 6;
514:         }
515:         break;
516:       case 7:
517:         s[0] = xb[i2];   s[1] = xb[i2+1]; s[2] = xb[i2+2];
518:         s[3] = xb[i2+3]; s[4] = xb[i2+4]; s[5] = xb[i2+5]; s[6] = xb[i2+6];
519:         PetscKernel_v_gets_A_times_w_7(x,idiag,b);
520:         x[i2]   = xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2];
521:         x[i2+3] = xw[3]; x[i2+4] = xw[4]; x[i2+5] = xw[5]; x[i2+6] = xw[6];
522:         i2    -= 7;
523:         idiag -= 49;
524:         for (i=m-2; i>=0; i--) {
525:           v  = aa + 49*(diag[i]+1);
526:           vi = aj + diag[i] + 1;
527:           nz = ai[i+1] - diag[i] - 1;
528:           s[0] = xb[i2];   s[1] = xb[i2+1]; s[2] = xb[i2+2];
529:           s[3] = xb[i2+3]; s[4] = xb[i2+4]; s[5] = xb[i2+5]; s[6] = xb[i2+6];
530:           while (nz--) {
531:             idx = 7*(*vi++);
532:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
533:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx]; xw[6] = x[6+idx];
534:             PetscKernel_v_gets_v_minus_A_times_w_7(s,v,xw);
535:             v  += 49;
536:           }
537:           PetscKernel_v_gets_A_times_w_7(xw,idiag,s);
538:           x[i2] =   xw[0]; x[i2+1] = xw[1]; x[i2+2] = xw[2];
539:           x[i2+3] = xw[3]; x[i2+4] = xw[4]; x[i2+5] = xw[5]; x[i2+6] = xw[6];
540:           idiag  -= 49;
541:           i2     -= 7;
542:         }
543:         break;
544:       default:
545:         PetscArraycpy(w,xb+i2,bs);
546:         PetscKernel_w_gets_Ar_times_v(bs,bs,w,idiag,x+i2);
547:         i2    -= bs;
548:         idiag -= bs2;
549:         for (i=m-2; i>=0; i--) {
550:           v  = aa + bs2*(diag[i]+1);
551:           vi = aj + diag[i] + 1;
552:           nz = ai[i+1] - diag[i] - 1;

554:           PetscArraycpy(w,xb+i2,bs);
555:           /* copy all rows of x that are needed into contiguous space */
556:           workt = work;
557:           for (j=0; j<nz; j++) {
558:             PetscArraycpy(workt,x + bs*(*vi++),bs);
559:             workt += bs;
560:           }
561:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
562:           PetscKernel_w_gets_Ar_times_v(bs,bs,w,idiag,x+i2);

564:           idiag -= bs2;
565:           i2    -= bs;
566:         }
567:         break;
568:       }
569:       PetscLogFlops(1.0*bs2*(a->nz));
570:     }
571:     its--;
572:   }
573:   while (its--) {
574:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
575:       idiag = a->idiag;
576:       i2 = 0;
577:       switch (bs) {
578:       case 1:
579:         for (i=0; i<m; i++) {
580:           v  = aa + ai[i];
581:           vi = aj + ai[i];
582:           nz = ai[i+1] - ai[i];
583:           s[0] = b[i2];
584:           for (j=0; j<nz; j++) {
585:             xw[0] = x[vi[j]];
586:             PetscKernel_v_gets_v_minus_A_times_w_1(s,(v+j),xw);
587:           }
588:           PetscKernel_v_gets_A_times_w_1(xw,idiag,s);
589:           x[i2] += xw[0];
590:           idiag += 1;
591:           i2    += 1;
592:         }
593:         break;
594:       case 2:
595:         for (i=0; i<m; i++) {
596:           v  = aa + 4*ai[i];
597:           vi = aj + ai[i];
598:           nz = ai[i+1] - ai[i];
599:           s[0] = b[i2]; s[1] = b[i2+1];
600:           for (j=0; j<nz; j++) {
601:             idx = 2*vi[j];
602:             it  = 4*j;
603:             xw[0] = x[idx]; xw[1] = x[1+idx];
604:             PetscKernel_v_gets_v_minus_A_times_w_2(s,(v+it),xw);
605:           }
606:           PetscKernel_v_gets_A_times_w_2(xw,idiag,s);
607:           x[i2]  += xw[0]; x[i2+1] += xw[1];
608:           idiag  += 4;
609:           i2     += 2;
610:         }
611:         break;
612:       case 3:
613:         for (i=0; i<m; i++) {
614:           v  = aa + 9*ai[i];
615:           vi = aj + ai[i];
616:           nz = ai[i+1] - ai[i];
617:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2];
618:           while (nz--) {
619:             idx = 3*(*vi++);
620:             xw[0] = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx];
621:             PetscKernel_v_gets_v_minus_A_times_w_3(s,v,xw);
622:             v  += 9;
623:           }
624:           PetscKernel_v_gets_A_times_w_3(xw,idiag,s);
625:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2];
626:           idiag  += 9;
627:           i2     += 3;
628:         }
629:         break;
630:       case 4:
631:         for (i=0; i<m; i++) {
632:           v  = aa + 16*ai[i];
633:           vi = aj + ai[i];
634:           nz = ai[i+1] - ai[i];
635:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3];
636:           while (nz--) {
637:             idx = 4*(*vi++);
638:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx];
639:             PetscKernel_v_gets_v_minus_A_times_w_4(s,v,xw);
640:             v  += 16;
641:           }
642:           PetscKernel_v_gets_A_times_w_4(xw,idiag,s);
643:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2]; x[i2+3] += xw[3];
644:           idiag  += 16;
645:           i2     += 4;
646:         }
647:         break;
648:       case 5:
649:         for (i=0; i<m; i++) {
650:           v  = aa + 25*ai[i];
651:           vi = aj + ai[i];
652:           nz = ai[i+1] - ai[i];
653:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3]; s[4] = b[i2+4];
654:           while (nz--) {
655:             idx = 5*(*vi++);
656:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx]; xw[4] = x[4+idx];
657:             PetscKernel_v_gets_v_minus_A_times_w_5(s,v,xw);
658:             v  += 25;
659:           }
660:           PetscKernel_v_gets_A_times_w_5(xw,idiag,s);
661:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2]; x[i2+3] += xw[3]; x[i2+4] += xw[4];
662:           idiag  += 25;
663:           i2     += 5;
664:         }
665:         break;
666:       case 6:
667:         for (i=0; i<m; i++) {
668:           v  = aa + 36*ai[i];
669:           vi = aj + ai[i];
670:           nz = ai[i+1] - ai[i];
671:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3]; s[4] = b[i2+4]; s[5] = b[i2+5];
672:           while (nz--) {
673:             idx = 6*(*vi++);
674:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
675:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx];
676:             PetscKernel_v_gets_v_minus_A_times_w_6(s,v,xw);
677:             v  += 36;
678:           }
679:           PetscKernel_v_gets_A_times_w_6(xw,idiag,s);
680:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2];
681:           x[i2+3] += xw[3]; x[i2+4] += xw[4]; x[i2+5] += xw[5];
682:           idiag  += 36;
683:           i2     += 6;
684:         }
685:         break;
686:       case 7:
687:         for (i=0; i<m; i++) {
688:           v  = aa + 49*ai[i];
689:           vi = aj + ai[i];
690:           nz = ai[i+1] - ai[i];
691:           s[0] = b[i2];   s[1] = b[i2+1]; s[2] = b[i2+2];
692:           s[3] = b[i2+3]; s[4] = b[i2+4]; s[5] = b[i2+5]; s[6] = b[i2+6];
693:           while (nz--) {
694:             idx = 7*(*vi++);
695:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
696:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx]; xw[6] = x[6+idx];
697:             PetscKernel_v_gets_v_minus_A_times_w_7(s,v,xw);
698:             v  += 49;
699:           }
700:           PetscKernel_v_gets_A_times_w_7(xw,idiag,s);
701:           x[i2]   += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2];
702:           x[i2+3] += xw[3]; x[i2+4] += xw[4]; x[i2+5] += xw[5]; x[i2+6] += xw[6];
703:           idiag  += 49;
704:           i2     += 7;
705:         }
706:         break;
707:       default:
708:         for (i=0; i<m; i++) {
709:           v  = aa + bs2*ai[i];
710:           vi = aj + ai[i];
711:           nz = ai[i+1] - ai[i];

713:           PetscArraycpy(w,b+i2,bs);
714:           /* copy all rows of x that are needed into contiguous space */
715:           workt = work;
716:           for (j=0; j<nz; j++) {
717:             PetscArraycpy(workt,x + bs*(*vi++),bs);
718:             workt += bs;
719:           }
720:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
721:           PetscKernel_w_gets_w_plus_Ar_times_v(bs,bs,w,idiag,x+i2);

723:           idiag += bs2;
724:           i2    += bs;
725:         }
726:         break;
727:       }
728:       PetscLogFlops(2.0*bs2*a->nz);
729:     }
730:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
731:       idiag = a->idiag+bs2*(a->mbs-1);
732:       i2 = bs * (m-1);
733:       switch (bs) {
734:       case 1:
735:         for (i=m-1; i>=0; i--) {
736:           v  = aa + ai[i];
737:           vi = aj + ai[i];
738:           nz = ai[i+1] - ai[i];
739:           s[0] = b[i2];
740:           for (j=0; j<nz; j++) {
741:             xw[0] = x[vi[j]];
742:             PetscKernel_v_gets_v_minus_A_times_w_1(s,(v+j),xw);
743:           }
744:           PetscKernel_v_gets_A_times_w_1(xw,idiag,s);
745:           x[i2] += xw[0];
746:           idiag -= 1;
747:           i2    -= 1;
748:         }
749:         break;
750:       case 2:
751:         for (i=m-1; i>=0; i--) {
752:           v  = aa + 4*ai[i];
753:           vi = aj + ai[i];
754:           nz = ai[i+1] - ai[i];
755:           s[0] = b[i2]; s[1] = b[i2+1];
756:           for (j=0; j<nz; j++) {
757:             idx = 2*vi[j];
758:             it  = 4*j;
759:             xw[0] = x[idx]; xw[1] = x[1+idx];
760:             PetscKernel_v_gets_v_minus_A_times_w_2(s,(v+it),xw);
761:           }
762:           PetscKernel_v_gets_A_times_w_2(xw,idiag,s);
763:           x[i2]  += xw[0]; x[i2+1] += xw[1];
764:           idiag  -= 4;
765:           i2     -= 2;
766:         }
767:         break;
768:       case 3:
769:         for (i=m-1; i>=0; i--) {
770:           v  = aa + 9*ai[i];
771:           vi = aj + ai[i];
772:           nz = ai[i+1] - ai[i];
773:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2];
774:           while (nz--) {
775:             idx = 3*(*vi++);
776:             xw[0] = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx];
777:             PetscKernel_v_gets_v_minus_A_times_w_3(s,v,xw);
778:             v  += 9;
779:           }
780:           PetscKernel_v_gets_A_times_w_3(xw,idiag,s);
781:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2];
782:           idiag  -= 9;
783:           i2     -= 3;
784:         }
785:         break;
786:       case 4:
787:         for (i=m-1; i>=0; i--) {
788:           v  = aa + 16*ai[i];
789:           vi = aj + ai[i];
790:           nz = ai[i+1] - ai[i];
791:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3];
792:           while (nz--) {
793:             idx = 4*(*vi++);
794:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx];
795:             PetscKernel_v_gets_v_minus_A_times_w_4(s,v,xw);
796:             v  += 16;
797:           }
798:           PetscKernel_v_gets_A_times_w_4(xw,idiag,s);
799:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2]; x[i2+3] += xw[3];
800:           idiag  -= 16;
801:           i2     -= 4;
802:         }
803:         break;
804:       case 5:
805:         for (i=m-1; i>=0; i--) {
806:           v  = aa + 25*ai[i];
807:           vi = aj + ai[i];
808:           nz = ai[i+1] - ai[i];
809:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3]; s[4] = b[i2+4];
810:           while (nz--) {
811:             idx = 5*(*vi++);
812:             xw[0]  = x[idx]; xw[1] = x[1+idx]; xw[2] = x[2+idx]; xw[3] = x[3+idx]; xw[4] = x[4+idx];
813:             PetscKernel_v_gets_v_minus_A_times_w_5(s,v,xw);
814:             v  += 25;
815:           }
816:           PetscKernel_v_gets_A_times_w_5(xw,idiag,s);
817:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2]; x[i2+3] += xw[3]; x[i2+4] += xw[4];
818:           idiag  -= 25;
819:           i2     -= 5;
820:         }
821:         break;
822:       case 6:
823:         for (i=m-1; i>=0; i--) {
824:           v  = aa + 36*ai[i];
825:           vi = aj + ai[i];
826:           nz = ai[i+1] - ai[i];
827:           s[0] = b[i2]; s[1] = b[i2+1]; s[2] = b[i2+2]; s[3] = b[i2+3]; s[4] = b[i2+4]; s[5] = b[i2+5];
828:           while (nz--) {
829:             idx = 6*(*vi++);
830:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
831:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx];
832:             PetscKernel_v_gets_v_minus_A_times_w_6(s,v,xw);
833:             v  += 36;
834:           }
835:           PetscKernel_v_gets_A_times_w_6(xw,idiag,s);
836:           x[i2] += xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2];
837:           x[i2+3] += xw[3]; x[i2+4] += xw[4]; x[i2+5] += xw[5];
838:           idiag  -= 36;
839:           i2     -= 6;
840:         }
841:         break;
842:       case 7:
843:         for (i=m-1; i>=0; i--) {
844:           v  = aa + 49*ai[i];
845:           vi = aj + ai[i];
846:           nz = ai[i+1] - ai[i];
847:           s[0] = b[i2];   s[1] = b[i2+1]; s[2] = b[i2+2];
848:           s[3] = b[i2+3]; s[4] = b[i2+4]; s[5] = b[i2+5]; s[6] = b[i2+6];
849:           while (nz--) {
850:             idx = 7*(*vi++);
851:             xw[0] = x[idx];   xw[1] = x[1+idx]; xw[2] = x[2+idx];
852:             xw[3] = x[3+idx]; xw[4] = x[4+idx]; xw[5] = x[5+idx]; xw[6] = x[6+idx];
853:             PetscKernel_v_gets_v_minus_A_times_w_7(s,v,xw);
854:             v  += 49;
855:           }
856:           PetscKernel_v_gets_A_times_w_7(xw,idiag,s);
857:           x[i2] +=   xw[0]; x[i2+1] += xw[1]; x[i2+2] += xw[2];
858:           x[i2+3] += xw[3]; x[i2+4] += xw[4]; x[i2+5] += xw[5]; x[i2+6] += xw[6];
859:           idiag  -= 49;
860:           i2     -= 7;
861:         }
862:         break;
863:       default:
864:         for (i=m-1; i>=0; i--) {
865:           v  = aa + bs2*ai[i];
866:           vi = aj + ai[i];
867:           nz = ai[i+1] - ai[i];

869:           PetscArraycpy(w,b+i2,bs);
870:           /* copy all rows of x that are needed into contiguous space */
871:           workt = work;
872:           for (j=0; j<nz; j++) {
873:             PetscArraycpy(workt,x + bs*(*vi++),bs);
874:             workt += bs;
875:           }
876:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
877:           PetscKernel_w_gets_w_plus_Ar_times_v(bs,bs,w,idiag,x+i2);

879:           idiag -= bs2;
880:           i2    -= bs;
881:         }
882:         break;
883:       }
884:       PetscLogFlops(2.0*bs2*(a->nz));
885:     }
886:   }
887:   VecRestoreArray(xx,&x);
888:   VecRestoreArrayRead(bb,&b);
889:   return(0);
890: }


893: /*
894:     Special version for direct calls from Fortran (Used in PETSc-fun3d)
895: */
896: #if defined(PETSC_HAVE_FORTRAN_CAPS)
897: #define matsetvaluesblocked4_ MATSETVALUESBLOCKED4
898: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
899: #define matsetvaluesblocked4_ matsetvaluesblocked4
900: #endif

902: PETSC_EXTERN void matsetvaluesblocked4_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[])
903: {
904:   Mat               A  = *AA;
905:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
906:   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,N,m = *mm,n = *nn;
907:   PetscInt          *ai    =a->i,*ailen=a->ilen;
908:   PetscInt          *aj    =a->j,stepval,lastcol = -1;
909:   const PetscScalar *value = v;
910:   MatScalar         *ap,*aa = a->a,*bap;
911:   PetscErrorCode    ierr;

914:   if (A->rmap->bs != 4) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Can only be called with a block size of 4");
915:   stepval = (n-1)*4;
916:   for (k=0; k<m; k++) { /* loop over added rows */
917:     row  = im[k];
918:     rp   = aj + ai[row];
919:     ap   = aa + 16*ai[row];
920:     nrow = ailen[row];
921:     low  = 0;
922:     high = nrow;
923:     for (l=0; l<n; l++) { /* loop over added columns */
924:       col = in[l];
925:       if (col <= lastcol)  low = 0;
926:       else                high = nrow;
927:       lastcol = col;
928:       value   = v + k*(stepval+4 + l)*4;
929:       while (high-low > 7) {
930:         t = (low+high)/2;
931:         if (rp[t] > col) high = t;
932:         else             low  = t;
933:       }
934:       for (i=low; i<high; i++) {
935:         if (rp[i] > col) break;
936:         if (rp[i] == col) {
937:           bap = ap +  16*i;
938:           for (ii=0; ii<4; ii++,value+=stepval) {
939:             for (jj=ii; jj<16; jj+=4) {
940:               bap[jj] += *value++;
941:             }
942:           }
943:           goto noinsert2;
944:         }
945:       }
946:       N = nrow++ - 1;
947:       high++; /* added new column index thus must search to one higher than before */
948:       /* shift up all the later entries in this row */
949:       for (ii=N; ii>=i; ii--) {
950:         rp[ii+1] = rp[ii];
951:         PetscArraycpy(ap+16*(ii+1),ap+16*(ii),16);CHKERRV(ierr);
952:       }
953:       if (N >= i) {
954:         PetscArrayzero(ap+16*i,16);CHKERRV(ierr);
955:       }
956:       rp[i] = col;
957:       bap   = ap +  16*i;
958:       for (ii=0; ii<4; ii++,value+=stepval) {
959:         for (jj=ii; jj<16; jj+=4) {
960:           bap[jj] = *value++;
961:         }
962:       }
963:       noinsert2:;
964:       low = i;
965:     }
966:     ailen[row] = nrow;
967:   }
968:   PetscFunctionReturnVoid();
969: }

971: #if defined(PETSC_HAVE_FORTRAN_CAPS)
972: #define matsetvalues4_ MATSETVALUES4
973: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
974: #define matsetvalues4_ matsetvalues4
975: #endif

977: PETSC_EXTERN void matsetvalues4_(Mat *AA,PetscInt *mm,PetscInt *im,PetscInt *nn,PetscInt *in,PetscScalar *v)
978: {
979:   Mat         A  = *AA;
980:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
981:   PetscInt    *rp,k,low,high,t,row,nrow,i,col,l,N,n = *nn,m = *mm;
982:   PetscInt    *ai=a->i,*ailen=a->ilen;
983:   PetscInt    *aj=a->j,brow,bcol;
984:   PetscInt    ridx,cidx,lastcol = -1;
985:   MatScalar   *ap,value,*aa=a->a,*bap;

989:   for (k=0; k<m; k++) { /* loop over added rows */
990:     row  = im[k]; brow = row/4;
991:     rp   = aj + ai[brow];
992:     ap   = aa + 16*ai[brow];
993:     nrow = ailen[brow];
994:     low  = 0;
995:     high = nrow;
996:     for (l=0; l<n; l++) { /* loop over added columns */
997:       col   = in[l]; bcol = col/4;
998:       ridx  = row % 4; cidx = col % 4;
999:       value = v[l + k*n];
1000:       if (col <= lastcol)  low = 0;
1001:       else                high = nrow;
1002:       lastcol = col;
1003:       while (high-low > 7) {
1004:         t = (low+high)/2;
1005:         if (rp[t] > bcol) high = t;
1006:         else              low  = t;
1007:       }
1008:       for (i=low; i<high; i++) {
1009:         if (rp[i] > bcol) break;
1010:         if (rp[i] == bcol) {
1011:           bap   = ap +  16*i + 4*cidx + ridx;
1012:           *bap += value;
1013:           goto noinsert1;
1014:         }
1015:       }
1016:       N = nrow++ - 1;
1017:       high++; /* added new column thus must search to one higher than before */
1018:       /* shift up all the later entries in this row */
1019:       PetscArraymove(rp+i+1,rp+i,N-i+1);CHKERRV(ierr);
1020:       PetscArraymove(ap+16*i+16,ap+16*i,16*(N-i+1));CHKERRV(ierr);
1021:       PetscArrayzero(ap+16*i,16);CHKERRV(ierr);
1022:       rp[i]                    = bcol;
1023:       ap[16*i + 4*cidx + ridx] = value;
1024: noinsert1:;
1025:       low = i;
1026:     }
1027:     ailen[brow] = nrow;
1028:   }
1029:   PetscFunctionReturnVoid();
1030: }

1032: /*
1033:      Checks for missing diagonals
1034: */
1035: PetscErrorCode MatMissingDiagonal_SeqBAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1036: {
1037:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1039:   PetscInt       *diag,*ii = a->i,i;

1042:   MatMarkDiagonal_SeqBAIJ(A);
1043:   *missing = PETSC_FALSE;
1044:   if (A->rmap->n > 0 && !ii) {
1045:     *missing = PETSC_TRUE;
1046:     if (d) *d = 0;
1047:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1048:   } else {
1049:     PetscInt n;
1050:     n = PetscMin(a->mbs, a->nbs);
1051:     diag = a->diag;
1052:     for (i=0; i<n; i++) {
1053:       if (diag[i] >= ii[i+1]) {
1054:         *missing = PETSC_TRUE;
1055:         if (d) *d = i;
1056:         PetscInfo1(A,"Matrix is missing block diagonal number %D\n",i);
1057:         break;
1058:       }
1059:     }
1060:   }
1061:   return(0);
1062: }

1064: PetscErrorCode MatMarkDiagonal_SeqBAIJ(Mat A)
1065: {
1066:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1068:   PetscInt       i,j,m = a->mbs;

1071:   if (!a->diag) {
1072:     PetscMalloc1(m,&a->diag);
1073:     PetscLogObjectMemory((PetscObject)A,m*sizeof(PetscInt));
1074:     a->free_diag = PETSC_TRUE;
1075:   }
1076:   for (i=0; i<m; i++) {
1077:     a->diag[i] = a->i[i+1];
1078:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1079:       if (a->j[j] == i) {
1080:         a->diag[i] = j;
1081:         break;
1082:       }
1083:     }
1084:   }
1085:   return(0);
1086: }


1089: static PetscErrorCode MatGetRowIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *inia[],const PetscInt *inja[],PetscBool  *done)
1090: {
1091:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1093:   PetscInt       i,j,n = a->mbs,nz = a->i[n],*tia,*tja,bs = A->rmap->bs,k,l,cnt;
1094:   PetscInt       **ia = (PetscInt**)inia,**ja = (PetscInt**)inja;

1097:   *nn = n;
1098:   if (!ia) return(0);
1099:   if (symmetric) {
1100:     MatToSymmetricIJ_SeqAIJ(n,a->i,a->j,PETSC_TRUE,0,0,&tia,&tja);
1101:     nz   = tia[n];
1102:   } else {
1103:     tia = a->i; tja = a->j;
1104:   }

1106:   if (!blockcompressed && bs > 1) {
1107:     (*nn) *= bs;
1108:     /* malloc & create the natural set of indices */
1109:     PetscMalloc1((n+1)*bs,ia);
1110:     if (n) {
1111:       (*ia)[0] = oshift;
1112:       for (j=1; j<bs; j++) {
1113:         (*ia)[j] = (tia[1]-tia[0])*bs+(*ia)[j-1];
1114:       }
1115:     }

1117:     for (i=1; i<n; i++) {
1118:       (*ia)[i*bs] = (tia[i]-tia[i-1])*bs + (*ia)[i*bs-1];
1119:       for (j=1; j<bs; j++) {
1120:         (*ia)[i*bs+j] = (tia[i+1]-tia[i])*bs + (*ia)[i*bs+j-1];
1121:       }
1122:     }
1123:     if (n) {
1124:       (*ia)[n*bs] = (tia[n]-tia[n-1])*bs + (*ia)[n*bs-1];
1125:     }

1127:     if (inja) {
1128:       PetscMalloc1(nz*bs*bs,ja);
1129:       cnt = 0;
1130:       for (i=0; i<n; i++) {
1131:         for (j=0; j<bs; j++) {
1132:           for (k=tia[i]; k<tia[i+1]; k++) {
1133:             for (l=0; l<bs; l++) {
1134:               (*ja)[cnt++] = bs*tja[k] + l;
1135:             }
1136:           }
1137:         }
1138:       }
1139:     }

1141:     if (symmetric) { /* deallocate memory allocated in MatToSymmetricIJ_SeqAIJ() */
1142:       PetscFree(tia);
1143:       PetscFree(tja);
1144:     }
1145:   } else if (oshift == 1) {
1146:     if (symmetric) {
1147:       nz = tia[A->rmap->n/bs];
1148:       /*  add 1 to i and j indices */
1149:       for (i=0; i<A->rmap->n/bs+1; i++) tia[i] = tia[i] + 1;
1150:       *ia = tia;
1151:       if (ja) {
1152:         for (i=0; i<nz; i++) tja[i] = tja[i] + 1;
1153:         *ja = tja;
1154:       }
1155:     } else {
1156:       nz = a->i[A->rmap->n/bs];
1157:       /* malloc space and  add 1 to i and j indices */
1158:       PetscMalloc1(A->rmap->n/bs+1,ia);
1159:       for (i=0; i<A->rmap->n/bs+1; i++) (*ia)[i] = a->i[i] + 1;
1160:       if (ja) {
1161:         PetscMalloc1(nz,ja);
1162:         for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
1163:       }
1164:     }
1165:   } else {
1166:     *ia = tia;
1167:     if (ja) *ja = tja;
1168:   }
1169:   return(0);
1170: }

1172: static PetscErrorCode MatRestoreRowIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
1173: {

1177:   if (!ia) return(0);
1178:   if ((!blockcompressed && A->rmap->bs > 1) || (symmetric || oshift == 1)) {
1179:     PetscFree(*ia);
1180:     if (ja) {PetscFree(*ja);}
1181:   }
1182:   return(0);
1183: }

1185: PetscErrorCode MatDestroy_SeqBAIJ(Mat A)
1186: {
1187:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;

1191: #if defined(PETSC_USE_LOG)
1192:   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->N,A->cmap->n,a->nz);
1193: #endif
1194:   MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1195:   ISDestroy(&a->row);
1196:   ISDestroy(&a->col);
1197:   if (a->free_diag) {PetscFree(a->diag);}
1198:   PetscFree(a->idiag);
1199:   if (a->free_imax_ilen) {PetscFree2(a->imax,a->ilen);}
1200:   PetscFree(a->solve_work);
1201:   PetscFree(a->mult_work);
1202:   PetscFree(a->sor_workt);
1203:   PetscFree(a->sor_work);
1204:   ISDestroy(&a->icol);
1205:   PetscFree(a->saved_values);
1206:   PetscFree2(a->compressedrow.i,a->compressedrow.rindex);

1208:   MatDestroy(&a->sbaijMat);
1209:   MatDestroy(&a->parent);
1210:   PetscFree(A->data);

1212:   PetscObjectChangeTypeName((PetscObject)A,NULL);
1213:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJGetArray_C",NULL);
1214:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJRestoreArray_C",NULL);
1215:   PetscObjectComposeFunction((PetscObject)A,"MatInvertBlockDiagonal_C",NULL);
1216:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1217:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1218:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetColumnIndices_C",NULL);
1219:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqaij_C",NULL);
1220:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqsbaij_C",NULL);
1221:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocation_C",NULL);
1222:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocationCSR_C",NULL);
1223:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqbstrm_C",NULL);
1224:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1225: #if defined(PETSC_HAVE_HYPRE)
1226:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_hypre_C",NULL);
1227: #endif
1228:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_is_C",NULL);
1229:   return(0);
1230: }

1232: PetscErrorCode MatSetOption_SeqBAIJ(Mat A,MatOption op,PetscBool flg)
1233: {
1234:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;

1238:   switch (op) {
1239:   case MAT_ROW_ORIENTED:
1240:     a->roworiented = flg;
1241:     break;
1242:   case MAT_KEEP_NONZERO_PATTERN:
1243:     a->keepnonzeropattern = flg;
1244:     break;
1245:   case MAT_NEW_NONZERO_LOCATIONS:
1246:     a->nonew = (flg ? 0 : 1);
1247:     break;
1248:   case MAT_NEW_NONZERO_LOCATION_ERR:
1249:     a->nonew = (flg ? -1 : 0);
1250:     break;
1251:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1252:     a->nonew = (flg ? -2 : 0);
1253:     break;
1254:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1255:     a->nounused = (flg ? -1 : 0);
1256:     break;
1257:   case MAT_NEW_DIAGONALS:
1258:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1259:   case MAT_USE_HASH_TABLE:
1260:   case MAT_SORTED_FULL:
1261:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1262:     break;
1263:   case MAT_SPD:
1264:   case MAT_SYMMETRIC:
1265:   case MAT_STRUCTURALLY_SYMMETRIC:
1266:   case MAT_HERMITIAN:
1267:   case MAT_SYMMETRY_ETERNAL:
1268:   case MAT_SUBMAT_SINGLEIS:
1269:   case MAT_STRUCTURE_ONLY:
1270:     /* These options are handled directly by MatSetOption() */
1271:     break;
1272:   default:
1273:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1274:   }
1275:   return(0);
1276: }

1278: /* used for both SeqBAIJ and SeqSBAIJ matrices */
1279: PetscErrorCode MatGetRow_SeqBAIJ_private(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v,PetscInt *ai,PetscInt *aj,PetscScalar *aa)
1280: {
1282:   PetscInt       itmp,i,j,k,M,bn,bp,*idx_i,bs,bs2;
1283:   MatScalar      *aa_i;
1284:   PetscScalar    *v_i;

1287:   bs  = A->rmap->bs;
1288:   bs2 = bs*bs;
1289:   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range", row);

1291:   bn  = row/bs;   /* Block number */
1292:   bp  = row % bs; /* Block Position */
1293:   M   = ai[bn+1] - ai[bn];
1294:   *nz = bs*M;

1296:   if (v) {
1297:     *v = NULL;
1298:     if (*nz) {
1299:       PetscMalloc1(*nz,v);
1300:       for (i=0; i<M; i++) { /* for each block in the block row */
1301:         v_i  = *v + i*bs;
1302:         aa_i = aa + bs2*(ai[bn] + i);
1303:         for (j=bp,k=0; j<bs2; j+=bs,k++) v_i[k] = aa_i[j];
1304:       }
1305:     }
1306:   }

1308:   if (idx) {
1309:     *idx = NULL;
1310:     if (*nz) {
1311:       PetscMalloc1(*nz,idx);
1312:       for (i=0; i<M; i++) { /* for each block in the block row */
1313:         idx_i = *idx + i*bs;
1314:         itmp  = bs*aj[ai[bn] + i];
1315:         for (j=0; j<bs; j++) idx_i[j] = itmp++;
1316:       }
1317:     }
1318:   }
1319:   return(0);
1320: }

1322: PetscErrorCode MatGetRow_SeqBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1323: {
1324:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;

1328:   MatGetRow_SeqBAIJ_private(A,row,nz,idx,v,a->i,a->j,a->a);
1329:   return(0);
1330: }

1332: PetscErrorCode MatRestoreRow_SeqBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1333: {

1337:   if (idx) {PetscFree(*idx);}
1338:   if (v)   {PetscFree(*v);}
1339:   return(0);
1340: }

1342: PetscErrorCode MatTranspose_SeqBAIJ(Mat A,MatReuse reuse,Mat *B)
1343: {
1344:   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)A->data,*at;
1345:   Mat            C;
1347:   PetscInt       i,j,k,*aj=a->j,*ai=a->i,bs=A->rmap->bs,mbs=a->mbs,nbs=a->nbs,*atfill;
1348:   PetscInt       bs2=a->bs2,*ati,*atj,anzj,kr;
1349:   MatScalar      *ata,*aa=a->a;

1352:   PetscCalloc1(1+nbs,&atfill);
1353:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
1354:     for (i=0; i<ai[mbs]; i++) atfill[aj[i]] += 1; /* count num of non-zeros in row aj[i] */

1356:     MatCreate(PetscObjectComm((PetscObject)A),&C);
1357:     MatSetSizes(C,A->cmap->n,A->rmap->N,A->cmap->n,A->rmap->N);
1358:     MatSetType(C,((PetscObject)A)->type_name);
1359:     MatSeqBAIJSetPreallocation(C,bs,0,atfill);

1361:     at  = (Mat_SeqBAIJ*)C->data;
1362:     ati = at->i;
1363:     for (i=0; i<nbs; i++) at->ilen[i] = at->imax[i] = ati[i+1] - ati[i];
1364:   } else {
1365:     C = *B;
1366:     at = (Mat_SeqBAIJ*)C->data;
1367:     ati = at->i;
1368:   }

1370:   atj = at->j;
1371:   ata = at->a;

1373:   /* Copy ati into atfill so we have locations of the next free space in atj */
1374:   PetscArraycpy(atfill,ati,nbs);

1376:   /* Walk through A row-wise and mark nonzero entries of A^T. */
1377:   for (i=0; i<mbs; i++) {
1378:     anzj = ai[i+1] - ai[i];
1379:     for (j=0; j<anzj; j++) {
1380:       atj[atfill[*aj]] = i;
1381:       for (kr=0; kr<bs; kr++) {
1382:         for (k=0; k<bs; k++) {
1383:           ata[bs2*atfill[*aj]+k*bs+kr] = *aa++;
1384:         }
1385:       }
1386:       atfill[*aj++] += 1;
1387:     }
1388:   }
1389:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1390:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

1392:   /* Clean up temporary space and complete requests. */
1393:   PetscFree(atfill);

1395:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
1396:     MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
1397:     *B = C;
1398:   } else {
1399:     MatHeaderMerge(A,&C);
1400:   }
1401:   return(0);
1402: }

1404: PetscErrorCode MatIsTranspose_SeqBAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1405: {
1407:   Mat            Btrans;

1410:   *f   = PETSC_FALSE;
1411:   MatTranspose_SeqBAIJ(A,MAT_INITIAL_MATRIX,&Btrans);
1412:   MatEqual_SeqBAIJ(B,Btrans,f);
1413:   MatDestroy(&Btrans);
1414:   return(0);
1415: }

1417: /* Used for both SeqBAIJ and SeqSBAIJ matrices */
1418: PetscErrorCode MatView_SeqBAIJ_Binary(Mat mat,PetscViewer viewer)
1419: {
1420:   Mat_SeqBAIJ    *A = (Mat_SeqBAIJ*)mat->data;
1421:   PetscInt       header[4],M,N,m,bs,nz,cnt,i,j,k,l;
1422:   PetscInt       *rowlens,*colidxs;
1423:   PetscScalar    *matvals;

1427:   PetscViewerSetUp(viewer);

1429:   M  = mat->rmap->N;
1430:   N  = mat->cmap->N;
1431:   m  = mat->rmap->n;
1432:   bs = mat->rmap->bs;
1433:   nz = bs*bs*A->nz;

1435:   /* write matrix header */
1436:   header[0] = MAT_FILE_CLASSID;
1437:   header[1] = M; header[2] = N; header[3] = nz;
1438:   PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);

1440:   /* store row lengths */
1441:   PetscMalloc1(m,&rowlens);
1442:   for (cnt=0, i=0; i<A->mbs; i++)
1443:     for (j=0; j<bs; j++)
1444:       rowlens[cnt++] = bs*(A->i[i+1] - A->i[i]);
1445:   PetscViewerBinaryWrite(viewer,rowlens,m,PETSC_INT);
1446:   PetscFree(rowlens);

1448:   /* store column indices  */
1449:   PetscMalloc1(nz,&colidxs);
1450:   for (cnt=0, i=0; i<A->mbs; i++)
1451:     for (k=0; k<bs; k++)
1452:       for (j=A->i[i]; j<A->i[i+1]; j++)
1453:         for (l=0; l<bs; l++)
1454:           colidxs[cnt++] = bs*A->j[j] + l;
1455:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1456:   PetscViewerBinaryWrite(viewer,colidxs,nz,PETSC_INT);
1457:   PetscFree(colidxs);

1459:   /* store nonzero values */
1460:   PetscMalloc1(nz,&matvals);
1461:   for (cnt=0, i=0; i<A->mbs; i++)
1462:     for (k=0; k<bs; k++)
1463:       for (j=A->i[i]; j<A->i[i+1]; j++)
1464:         for (l=0; l<bs; l++)
1465:           matvals[cnt++] = A->a[bs*(bs*j + l) + k];
1466:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1467:   PetscViewerBinaryWrite(viewer,matvals,nz,PETSC_SCALAR);
1468:   PetscFree(matvals);

1470:   /* write block size option to the viewer's .info file */
1471:   MatView_Binary_BlockSizes(mat,viewer);
1472:   return(0);
1473: }

1475: static PetscErrorCode MatView_SeqBAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
1476: {
1478:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1479:   PetscInt       i,bs = A->rmap->bs,k;

1482:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1483:   for (i=0; i<a->mbs; i++) {
1484:     PetscViewerASCIIPrintf(viewer,"row %D-%D:",i*bs,i*bs+bs-1);
1485:     for (k=a->i[i]; k<a->i[i+1]; k++) {
1486:       PetscViewerASCIIPrintf(viewer," (%D-%D) ",bs*a->j[k],bs*a->j[k]+bs-1);
1487:     }
1488:     PetscViewerASCIIPrintf(viewer,"\n");
1489:   }
1490:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1491:   return(0);
1492: }

1494: static PetscErrorCode MatView_SeqBAIJ_ASCII(Mat A,PetscViewer viewer)
1495: {
1496:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1497:   PetscErrorCode    ierr;
1498:   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
1499:   PetscViewerFormat format;

1502:   if (A->structure_only) {
1503:     MatView_SeqBAIJ_ASCII_structonly(A,viewer);
1504:     return(0);
1505:   }

1507:   PetscViewerGetFormat(viewer,&format);
1508:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1509:     PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
1510:   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
1511:     const char *matname;
1512:     Mat        aij;
1513:     MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);
1514:     PetscObjectGetName((PetscObject)A,&matname);
1515:     PetscObjectSetName((PetscObject)aij,matname);
1516:     MatView(aij,viewer);
1517:     MatDestroy(&aij);
1518:   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1519:       return(0);
1520:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
1521:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1522:     for (i=0; i<a->mbs; i++) {
1523:       for (j=0; j<bs; j++) {
1524:         PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);
1525:         for (k=a->i[i]; k<a->i[i+1]; k++) {
1526:           for (l=0; l<bs; l++) {
1527: #if defined(PETSC_USE_COMPLEX)
1528:             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1529:               PetscViewerASCIIPrintf(viewer," (%D, %g + %gi) ",bs*a->j[k]+l,
1530:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1531:             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1532:               PetscViewerASCIIPrintf(viewer," (%D, %g - %gi) ",bs*a->j[k]+l,
1533:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1534:             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1535:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));
1536:             }
1537: #else
1538:             if (a->a[bs2*k + l*bs + j] != 0.0) {
1539:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);
1540:             }
1541: #endif
1542:           }
1543:         }
1544:         PetscViewerASCIIPrintf(viewer,"\n");
1545:       }
1546:     }
1547:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1548:   } else {
1549:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1550:     for (i=0; i<a->mbs; i++) {
1551:       for (j=0; j<bs; j++) {
1552:         PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);
1553:         for (k=a->i[i]; k<a->i[i+1]; k++) {
1554:           for (l=0; l<bs; l++) {
1555: #if defined(PETSC_USE_COMPLEX)
1556:             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
1557:               PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
1558:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1559:             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
1560:               PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
1561:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1562:             } else {
1563:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));
1564:             }
1565: #else
1566:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);
1567: #endif
1568:           }
1569:         }
1570:         PetscViewerASCIIPrintf(viewer,"\n");
1571:       }
1572:     }
1573:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1574:   }
1575:   PetscViewerFlush(viewer);
1576:   return(0);
1577: }

1579: #include <petscdraw.h>
1580: static PetscErrorCode MatView_SeqBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
1581: {
1582:   Mat               A = (Mat) Aa;
1583:   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ*)A->data;
1584:   PetscErrorCode    ierr;
1585:   PetscInt          row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
1586:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
1587:   MatScalar         *aa;
1588:   PetscViewer       viewer;
1589:   PetscViewerFormat format;

1592:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
1593:   PetscViewerGetFormat(viewer,&format);
1594:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

1596:   /* loop over matrix elements drawing boxes */

1598:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
1599:     PetscDrawCollectiveBegin(draw);
1600:     /* Blue for negative, Cyan for zero and  Red for positive */
1601:     color = PETSC_DRAW_BLUE;
1602:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1603:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1604:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1605:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1606:         aa  = a->a + j*bs2;
1607:         for (k=0; k<bs; k++) {
1608:           for (l=0; l<bs; l++) {
1609:             if (PetscRealPart(*aa++) >=  0.) continue;
1610:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1611:           }
1612:         }
1613:       }
1614:     }
1615:     color = PETSC_DRAW_CYAN;
1616:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1617:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1618:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1619:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1620:         aa  = a->a + j*bs2;
1621:         for (k=0; k<bs; k++) {
1622:           for (l=0; l<bs; l++) {
1623:             if (PetscRealPart(*aa++) != 0.) continue;
1624:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1625:           }
1626:         }
1627:       }
1628:     }
1629:     color = PETSC_DRAW_RED;
1630:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1631:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1632:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1633:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1634:         aa  = a->a + j*bs2;
1635:         for (k=0; k<bs; k++) {
1636:           for (l=0; l<bs; l++) {
1637:             if (PetscRealPart(*aa++) <= 0.) continue;
1638:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1639:           }
1640:         }
1641:       }
1642:     }
1643:     PetscDrawCollectiveEnd(draw);
1644:   } else {
1645:     /* use contour shading to indicate magnitude of values */
1646:     /* first determine max of all nonzero values */
1647:     PetscReal minv = 0.0, maxv = 0.0;
1648:     PetscDraw popup;

1650:     for (i=0; i<a->nz*a->bs2; i++) {
1651:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
1652:     }
1653:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
1654:     PetscDrawGetPopup(draw,&popup);
1655:     PetscDrawScalePopup(popup,0.0,maxv);

1657:     PetscDrawCollectiveBegin(draw);
1658:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1659:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1660:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1661:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1662:         aa  = a->a + j*bs2;
1663:         for (k=0; k<bs; k++) {
1664:           for (l=0; l<bs; l++) {
1665:             MatScalar v = *aa++;
1666:             color = PetscDrawRealToColor(PetscAbsScalar(v),minv,maxv);
1667:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1668:           }
1669:         }
1670:       }
1671:     }
1672:     PetscDrawCollectiveEnd(draw);
1673:   }
1674:   return(0);
1675: }

1677: static PetscErrorCode MatView_SeqBAIJ_Draw(Mat A,PetscViewer viewer)
1678: {
1680:   PetscReal      xl,yl,xr,yr,w,h;
1681:   PetscDraw      draw;
1682:   PetscBool      isnull;

1685:   PetscViewerDrawGetDraw(viewer,0,&draw);
1686:   PetscDrawIsNull(draw,&isnull);
1687:   if (isnull) return(0);

1689:   xr   = A->cmap->n; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
1690:   xr  += w;          yr += h;        xl = -w;     yl = -h;
1691:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
1692:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
1693:   PetscDrawZoom(draw,MatView_SeqBAIJ_Draw_Zoom,A);
1694:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
1695:   PetscDrawSave(draw);
1696:   return(0);
1697: }

1699: PetscErrorCode MatView_SeqBAIJ(Mat A,PetscViewer viewer)
1700: {
1702:   PetscBool      iascii,isbinary,isdraw;

1705:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1706:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1707:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1708:   if (iascii) {
1709:     MatView_SeqBAIJ_ASCII(A,viewer);
1710:   } else if (isbinary) {
1711:     MatView_SeqBAIJ_Binary(A,viewer);
1712:   } else if (isdraw) {
1713:     MatView_SeqBAIJ_Draw(A,viewer);
1714:   } else {
1715:     Mat B;
1716:     MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);
1717:     MatView(B,viewer);
1718:     MatDestroy(&B);
1719:   }
1720:   return(0);
1721: }


1724: PetscErrorCode MatGetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
1725: {
1726:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
1727:   PetscInt    *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
1728:   PetscInt    *ai = a->i,*ailen = a->ilen;
1729:   PetscInt    brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
1730:   MatScalar   *ap,*aa = a->a;

1733:   for (k=0; k<m; k++) { /* loop over rows */
1734:     row = im[k]; brow = row/bs;
1735:     if (row < 0) {v += n; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row"); */
1736:     if (row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D too large", row);
1737:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow];
1738:     nrow = ailen[brow];
1739:     for (l=0; l<n; l++) { /* loop over columns */
1740:       if (in[l] < 0) {v++; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column"); */
1741:       if (in[l] >= A->cmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column %D too large", in[l]);
1742:       col  = in[l];
1743:       bcol = col/bs;
1744:       cidx = col%bs;
1745:       ridx = row%bs;
1746:       high = nrow;
1747:       low  = 0; /* assume unsorted */
1748:       while (high-low > 5) {
1749:         t = (low+high)/2;
1750:         if (rp[t] > bcol) high = t;
1751:         else             low  = t;
1752:       }
1753:       for (i=low; i<high; i++) {
1754:         if (rp[i] > bcol) break;
1755:         if (rp[i] == bcol) {
1756:           *v++ = ap[bs2*i+bs*cidx+ridx];
1757:           goto finished;
1758:         }
1759:       }
1760:       *v++ = 0.0;
1761: finished:;
1762:     }
1763:   }
1764:   return(0);
1765: }

1767: PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
1768: {
1769:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1770:   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
1771:   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
1772:   PetscErrorCode    ierr;
1773:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
1774:   PetscBool         roworiented=a->roworiented;
1775:   const PetscScalar *value     = v;
1776:   MatScalar         *ap=NULL,*aa = a->a,*bap;

1779:   if (roworiented) {
1780:     stepval = (n-1)*bs;
1781:   } else {
1782:     stepval = (m-1)*bs;
1783:   }
1784:   for (k=0; k<m; k++) { /* loop over added rows */
1785:     row = im[k];
1786:     if (row < 0) continue;
1787:     if (PetscUnlikelyDebug(row >= a->mbs)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block row index too large %D max %D",row,a->mbs-1);
1788:     rp   = aj + ai[row];
1789:     if (!A->structure_only) ap = aa + bs2*ai[row];
1790:     rmax = imax[row];
1791:     nrow = ailen[row];
1792:     low  = 0;
1793:     high = nrow;
1794:     for (l=0; l<n; l++) { /* loop over added columns */
1795:       if (in[l] < 0) continue;
1796:       if (PetscUnlikelyDebug(in[l] >= a->nbs)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block column index too large %D max %D",in[l],a->nbs-1);
1797:       col = in[l];
1798:       if (!A->structure_only) {
1799:         if (roworiented) {
1800:           value = v + (k*(stepval+bs) + l)*bs;
1801:         } else {
1802:           value = v + (l*(stepval+bs) + k)*bs;
1803:         }
1804:       }
1805:       if (col <= lastcol) low = 0;
1806:       else high = nrow;
1807:       lastcol = col;
1808:       while (high-low > 7) {
1809:         t = (low+high)/2;
1810:         if (rp[t] > col) high = t;
1811:         else             low  = t;
1812:       }
1813:       for (i=low; i<high; i++) {
1814:         if (rp[i] > col) break;
1815:         if (rp[i] == col) {
1816:           if (A->structure_only) goto noinsert2;
1817:           bap = ap +  bs2*i;
1818:           if (roworiented) {
1819:             if (is == ADD_VALUES) {
1820:               for (ii=0; ii<bs; ii++,value+=stepval) {
1821:                 for (jj=ii; jj<bs2; jj+=bs) {
1822:                   bap[jj] += *value++;
1823:                 }
1824:               }
1825:             } else {
1826:               for (ii=0; ii<bs; ii++,value+=stepval) {
1827:                 for (jj=ii; jj<bs2; jj+=bs) {
1828:                   bap[jj] = *value++;
1829:                 }
1830:               }
1831:             }
1832:           } else {
1833:             if (is == ADD_VALUES) {
1834:               for (ii=0; ii<bs; ii++,value+=bs+stepval) {
1835:                 for (jj=0; jj<bs; jj++) {
1836:                   bap[jj] += value[jj];
1837:                 }
1838:                 bap += bs;
1839:               }
1840:             } else {
1841:               for (ii=0; ii<bs; ii++,value+=bs+stepval) {
1842:                 for (jj=0; jj<bs; jj++) {
1843:                   bap[jj]  = value[jj];
1844:                 }
1845:                 bap += bs;
1846:               }
1847:             }
1848:           }
1849:           goto noinsert2;
1850:         }
1851:       }
1852:       if (nonew == 1) goto noinsert2;
1853:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new blocked index new nonzero block (%D, %D) in the matrix", row, col);
1854:       if (A->structure_only) {
1855:         MatSeqXAIJReallocateAIJ_structure_only(A,a->mbs,bs2,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
1856:       } else {
1857:         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
1858:       }
1859:       N = nrow++ - 1; high++;
1860:       /* shift up all the later entries in this row */
1861:       PetscArraymove(rp+i+1,rp+i,N-i+1);
1862:       rp[i] = col;
1863:       if (!A->structure_only) {
1864:         PetscArraymove(ap+bs2*(i+1),ap+bs2*i,bs2*(N-i+1));
1865:         bap   = ap +  bs2*i;
1866:         if (roworiented) {
1867:           for (ii=0; ii<bs; ii++,value+=stepval) {
1868:             for (jj=ii; jj<bs2; jj+=bs) {
1869:               bap[jj] = *value++;
1870:             }
1871:           }
1872:         } else {
1873:           for (ii=0; ii<bs; ii++,value+=stepval) {
1874:             for (jj=0; jj<bs; jj++) {
1875:               *bap++ = *value++;
1876:             }
1877:           }
1878:         }
1879:       }
1880: noinsert2:;
1881:       low = i;
1882:     }
1883:     ailen[row] = nrow;
1884:   }
1885:   return(0);
1886: }

1888: PetscErrorCode MatAssemblyEnd_SeqBAIJ(Mat A,MatAssemblyType mode)
1889: {
1890:   Mat_SeqBAIJ    *a     = (Mat_SeqBAIJ*)A->data;
1891:   PetscInt       fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax;
1892:   PetscInt       m      = A->rmap->N,*ip,N,*ailen = a->ilen;
1894:   PetscInt       mbs  = a->mbs,bs2 = a->bs2,rmax = 0;
1895:   MatScalar      *aa  = a->a,*ap;
1896:   PetscReal      ratio=0.6;

1899:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);

1901:   if (m) rmax = ailen[0];
1902:   for (i=1; i<mbs; i++) {
1903:     /* move each row back by the amount of empty slots (fshift) before it*/
1904:     fshift += imax[i-1] - ailen[i-1];
1905:     rmax    = PetscMax(rmax,ailen[i]);
1906:     if (fshift) {
1907:       ip = aj + ai[i];
1908:       ap = aa + bs2*ai[i];
1909:       N  = ailen[i];
1910:       PetscArraymove(ip-fshift,ip,N);
1911:       if (!A->structure_only) {
1912:         PetscArraymove(ap-bs2*fshift,ap,bs2*N);
1913:       }
1914:     }
1915:     ai[i] = ai[i-1] + ailen[i-1];
1916:   }
1917:   if (mbs) {
1918:     fshift += imax[mbs-1] - ailen[mbs-1];
1919:     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
1920:   }

1922:   /* reset ilen and imax for each row */
1923:   a->nonzerorowcnt = 0;
1924:   if (A->structure_only) {
1925:     PetscFree2(a->imax,a->ilen);
1926:   } else { /* !A->structure_only */
1927:     for (i=0; i<mbs; i++) {
1928:       ailen[i] = imax[i] = ai[i+1] - ai[i];
1929:       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1930:     }
1931:   }
1932:   a->nz = ai[mbs];

1934:   /* diagonals may have moved, so kill the diagonal pointers */
1935:   a->idiagvalid = PETSC_FALSE;
1936:   if (fshift && a->diag) {
1937:     PetscFree(a->diag);
1938:     PetscLogObjectMemory((PetscObject)A,-(mbs+1)*sizeof(PetscInt));
1939:     a->diag = NULL;
1940:   }
1941:   if (fshift && a->nounused == -1) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D block size %D, %D unneeded", m, A->cmap->n, A->rmap->bs, fshift*bs2);
1942:   PetscInfo5(A,"Matrix size: %D X %D, block size %D; storage space: %D unneeded, %D used\n",m,A->cmap->n,A->rmap->bs,fshift*bs2,a->nz*bs2);
1943:   PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);
1944:   PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);

1946:   A->info.mallocs    += a->reallocs;
1947:   a->reallocs         = 0;
1948:   A->info.nz_unneeded = (PetscReal)fshift*bs2;
1949:   a->rmax             = rmax;

1951:   if (!A->structure_only) {
1952:     MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,mbs,ratio);
1953:   }
1954:   return(0);
1955: }

1957: /*
1958:    This function returns an array of flags which indicate the locations of contiguous
1959:    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
1960:    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
1961:    Assume: sizes should be long enough to hold all the values.
1962: */
1963: static PetscErrorCode MatZeroRows_SeqBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
1964: {
1965:   PetscInt  i,j,k,row;
1966:   PetscBool flg;

1969:   for (i=0,j=0; i<n; j++) {
1970:     row = idx[i];
1971:     if (row%bs!=0) { /* Not the begining of a block */
1972:       sizes[j] = 1;
1973:       i++;
1974:     } else if (i+bs > n) { /* complete block doesn't exist (at idx end) */
1975:       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
1976:       i++;
1977:     } else { /* Begining of the block, so check if the complete block exists */
1978:       flg = PETSC_TRUE;
1979:       for (k=1; k<bs; k++) {
1980:         if (row+k != idx[i+k]) { /* break in the block */
1981:           flg = PETSC_FALSE;
1982:           break;
1983:         }
1984:       }
1985:       if (flg) { /* No break in the bs */
1986:         sizes[j] = bs;
1987:         i       += bs;
1988:       } else {
1989:         sizes[j] = 1;
1990:         i++;
1991:       }
1992:     }
1993:   }
1994:   *bs_max = j;
1995:   return(0);
1996: }

1998: PetscErrorCode MatZeroRows_SeqBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
1999: {
2000:   Mat_SeqBAIJ       *baij=(Mat_SeqBAIJ*)A->data;
2001:   PetscErrorCode    ierr;
2002:   PetscInt          i,j,k,count,*rows;
2003:   PetscInt          bs=A->rmap->bs,bs2=baij->bs2,*sizes,row,bs_max;
2004:   PetscScalar       zero = 0.0;
2005:   MatScalar         *aa;
2006:   const PetscScalar *xx;
2007:   PetscScalar       *bb;

2010:   /* fix right hand side if needed */
2011:   if (x && b) {
2012:     VecGetArrayRead(x,&xx);
2013:     VecGetArray(b,&bb);
2014:     for (i=0; i<is_n; i++) {
2015:       bb[is_idx[i]] = diag*xx[is_idx[i]];
2016:     }
2017:     VecRestoreArrayRead(x,&xx);
2018:     VecRestoreArray(b,&bb);
2019:   }

2021:   /* Make a copy of the IS and  sort it */
2022:   /* allocate memory for rows,sizes */
2023:   PetscMalloc2(is_n,&rows,2*is_n,&sizes);

2025:   /* copy IS values to rows, and sort them */
2026:   for (i=0; i<is_n; i++) rows[i] = is_idx[i];
2027:   PetscSortInt(is_n,rows);

2029:   if (baij->keepnonzeropattern) {
2030:     for (i=0; i<is_n; i++) sizes[i] = 1;
2031:     bs_max          = is_n;
2032:   } else {
2033:     MatZeroRows_SeqBAIJ_Check_Blocks(rows,is_n,bs,sizes,&bs_max);
2034:     A->nonzerostate++;
2035:   }

2037:   for (i=0,j=0; i<bs_max; j+=sizes[i],i++) {
2038:     row = rows[j];
2039:     if (row < 0 || row > A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",row);
2040:     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
2041:     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
2042:     if (sizes[i] == bs && !baij->keepnonzeropattern) {
2043:       if (diag != (PetscScalar)0.0) {
2044:         if (baij->ilen[row/bs] > 0) {
2045:           baij->ilen[row/bs]       = 1;
2046:           baij->j[baij->i[row/bs]] = row/bs;

2048:           PetscArrayzero(aa,count*bs);
2049:         }
2050:         /* Now insert all the diagonal values for this bs */
2051:         for (k=0; k<bs; k++) {
2052:           (*A->ops->setvalues)(A,1,rows+j+k,1,rows+j+k,&diag,INSERT_VALUES);
2053:         }
2054:       } else { /* (diag == 0.0) */
2055:         baij->ilen[row/bs] = 0;
2056:       } /* end (diag == 0.0) */
2057:     } else { /* (sizes[i] != bs) */
2058:       if (PetscUnlikelyDebug(sizes[i] != 1)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal Error. Value should be 1");
2059:       for (k=0; k<count; k++) {
2060:         aa[0] =  zero;
2061:         aa   += bs;
2062:       }
2063:       if (diag != (PetscScalar)0.0) {
2064:         (*A->ops->setvalues)(A,1,rows+j,1,rows+j,&diag,INSERT_VALUES);
2065:       }
2066:     }
2067:   }

2069:   PetscFree2(rows,sizes);
2070:   MatAssemblyEnd_SeqBAIJ(A,MAT_FINAL_ASSEMBLY);
2071:   return(0);
2072: }

2074: PetscErrorCode MatZeroRowsColumns_SeqBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
2075: {
2076:   Mat_SeqBAIJ       *baij=(Mat_SeqBAIJ*)A->data;
2077:   PetscErrorCode    ierr;
2078:   PetscInt          i,j,k,count;
2079:   PetscInt          bs   =A->rmap->bs,bs2=baij->bs2,row,col;
2080:   PetscScalar       zero = 0.0;
2081:   MatScalar         *aa;
2082:   const PetscScalar *xx;
2083:   PetscScalar       *bb;
2084:   PetscBool         *zeroed,vecs = PETSC_FALSE;

2087:   /* fix right hand side if needed */
2088:   if (x && b) {
2089:     VecGetArrayRead(x,&xx);
2090:     VecGetArray(b,&bb);
2091:     vecs = PETSC_TRUE;
2092:   }

2094:   /* zero the columns */
2095:   PetscCalloc1(A->rmap->n,&zeroed);
2096:   for (i=0; i<is_n; i++) {
2097:     if (is_idx[i] < 0 || is_idx[i] >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",is_idx[i]);
2098:     zeroed[is_idx[i]] = PETSC_TRUE;
2099:   }
2100:   for (i=0; i<A->rmap->N; i++) {
2101:     if (!zeroed[i]) {
2102:       row = i/bs;
2103:       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
2104:         for (k=0; k<bs; k++) {
2105:           col = bs*baij->j[j] + k;
2106:           if (zeroed[col]) {
2107:             aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
2108:             if (vecs) bb[i] -= aa[0]*xx[col];
2109:             aa[0] = 0.0;
2110:           }
2111:         }
2112:       }
2113:     } else if (vecs) bb[i] = diag*xx[i];
2114:   }
2115:   PetscFree(zeroed);
2116:   if (vecs) {
2117:     VecRestoreArrayRead(x,&xx);
2118:     VecRestoreArray(b,&bb);
2119:   }

2121:   /* zero the rows */
2122:   for (i=0; i<is_n; i++) {
2123:     row   = is_idx[i];
2124:     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
2125:     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
2126:     for (k=0; k<count; k++) {
2127:       aa[0] =  zero;
2128:       aa   += bs;
2129:     }
2130:     if (diag != (PetscScalar)0.0) {
2131:       (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);
2132:     }
2133:   }
2134:   MatAssemblyEnd_SeqBAIJ(A,MAT_FINAL_ASSEMBLY);
2135:   return(0);
2136: }

2138: PetscErrorCode MatSetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
2139: {
2140:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2141:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
2142:   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen;
2143:   PetscInt       *aj  =a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
2145:   PetscInt       ridx,cidx,bs2=a->bs2;
2146:   PetscBool      roworiented=a->roworiented;
2147:   MatScalar      *ap=NULL,value=0.0,*aa=a->a,*bap;

2150:   for (k=0; k<m; k++) { /* loop over added rows */
2151:     row  = im[k];
2152:     brow = row/bs;
2153:     if (row < 0) continue;
2154:     if (PetscUnlikelyDebug(row >= A->rmap->N)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->N-1);
2155:     rp   = aj + ai[brow];
2156:     if (!A->structure_only) ap = aa + bs2*ai[brow];
2157:     rmax = imax[brow];
2158:     nrow = ailen[brow];
2159:     low  = 0;
2160:     high = nrow;
2161:     for (l=0; l<n; l++) { /* loop over added columns */
2162:       if (in[l] < 0) continue;
2163:       if (PetscUnlikelyDebug(in[l] >= A->cmap->n)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
2164:       col  = in[l]; bcol = col/bs;
2165:       ridx = row % bs; cidx = col % bs;
2166:       if (!A->structure_only) {
2167:         if (roworiented) {
2168:           value = v[l + k*n];
2169:         } else {
2170:           value = v[k + l*m];
2171:         }
2172:       }
2173:       if (col <= lastcol) low = 0; else high = nrow;
2174:       lastcol = col;
2175:       while (high-low > 7) {
2176:         t = (low+high)/2;
2177:         if (rp[t] > bcol) high = t;
2178:         else              low  = t;
2179:       }
2180:       for (i=low; i<high; i++) {
2181:         if (rp[i] > bcol) break;
2182:         if (rp[i] == bcol) {
2183:           bap = ap +  bs2*i + bs*cidx + ridx;
2184:           if (!A->structure_only) {
2185:             if (is == ADD_VALUES) *bap += value;
2186:             else                  *bap  = value;
2187:           }
2188:           goto noinsert1;
2189:         }
2190:       }
2191:       if (nonew == 1) goto noinsert1;
2192:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
2193:       if (A->structure_only) {
2194:         MatSeqXAIJReallocateAIJ_structure_only(A,a->mbs,bs2,nrow,brow,bcol,rmax,ai,aj,rp,imax,nonew,MatScalar);
2195:       } else {
2196:         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
2197:       }
2198:       N = nrow++ - 1; high++;
2199:       /* shift up all the later entries in this row */
2200:       PetscArraymove(rp+i+1,rp+i,N-i+1);
2201:       rp[i] = bcol;
2202:       if (!A->structure_only) {
2203:         PetscArraymove(ap+bs2*(i+1),ap+bs2*i,bs2*(N-i+1));
2204:         PetscArrayzero(ap+bs2*i,bs2);
2205:         ap[bs2*i + bs*cidx + ridx] = value;
2206:       }
2207:       a->nz++;
2208:       A->nonzerostate++;
2209: noinsert1:;
2210:       low = i;
2211:     }
2212:     ailen[brow] = nrow;
2213:   }
2214:   return(0);
2215: }

2217: PetscErrorCode MatILUFactor_SeqBAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2218: {
2219:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)inA->data;
2220:   Mat            outA;
2222:   PetscBool      row_identity,col_identity;

2225:   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for in-place ILU");
2226:   ISIdentity(row,&row_identity);
2227:   ISIdentity(col,&col_identity);
2228:   if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");

2230:   outA            = inA;
2231:   inA->factortype = MAT_FACTOR_LU;
2232:   PetscFree(inA->solvertype);
2233:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

2235:   MatMarkDiagonal_SeqBAIJ(inA);

2237:   PetscObjectReference((PetscObject)row);
2238:   ISDestroy(&a->row);
2239:   a->row = row;
2240:   PetscObjectReference((PetscObject)col);
2241:   ISDestroy(&a->col);
2242:   a->col = col;

2244:   /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */
2245:   ISDestroy(&a->icol);
2246:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2247:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

2249:   MatSeqBAIJSetNumericFactorization_inplace(inA,(PetscBool)(row_identity && col_identity));
2250:   if (!a->solve_work) {
2251:     PetscMalloc1(inA->rmap->N+inA->rmap->bs,&a->solve_work);
2252:     PetscLogObjectMemory((PetscObject)inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));
2253:   }
2254:   MatLUFactorNumeric(outA,inA,info);
2255:   return(0);
2256: }

2258: PetscErrorCode  MatSeqBAIJSetColumnIndices_SeqBAIJ(Mat mat,PetscInt *indices)
2259: {
2260:   Mat_SeqBAIJ *baij = (Mat_SeqBAIJ*)mat->data;
2261:   PetscInt    i,nz,mbs;

2264:   nz  = baij->maxnz;
2265:   mbs = baij->mbs;
2266:   for (i=0; i<nz; i++) {
2267:     baij->j[i] = indices[i];
2268:   }
2269:   baij->nz = nz;
2270:   for (i=0; i<mbs; i++) {
2271:     baij->ilen[i] = baij->imax[i];
2272:   }
2273:   return(0);
2274: }

2276: /*@
2277:     MatSeqBAIJSetColumnIndices - Set the column indices for all the rows
2278:        in the matrix.

2280:   Input Parameters:
2281: +  mat - the SeqBAIJ matrix
2282: -  indices - the column indices

2284:   Level: advanced

2286:   Notes:
2287:     This can be called if you have precomputed the nonzero structure of the
2288:   matrix and want to provide it to the matrix object to improve the performance
2289:   of the MatSetValues() operation.

2291:     You MUST have set the correct numbers of nonzeros per row in the call to
2292:   MatCreateSeqBAIJ(), and the columns indices MUST be sorted.

2294:     MUST be called before any calls to MatSetValues();

2296: @*/
2297: PetscErrorCode  MatSeqBAIJSetColumnIndices(Mat mat,PetscInt *indices)
2298: {

2304:   PetscUseMethod(mat,"MatSeqBAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
2305:   return(0);
2306: }

2308: PetscErrorCode MatGetRowMaxAbs_SeqBAIJ(Mat A,Vec v,PetscInt idx[])
2309: {
2310:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2312:   PetscInt       i,j,n,row,bs,*ai,*aj,mbs;
2313:   PetscReal      atmp;
2314:   PetscScalar    *x,zero = 0.0;
2315:   MatScalar      *aa;
2316:   PetscInt       ncols,brow,krow,kcol;

2319:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2320:   bs  = A->rmap->bs;
2321:   aa  = a->a;
2322:   ai  = a->i;
2323:   aj  = a->j;
2324:   mbs = a->mbs;

2326:   VecSet(v,zero);
2327:   VecGetArray(v,&x);
2328:   VecGetLocalSize(v,&n);
2329:   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2330:   for (i=0; i<mbs; i++) {
2331:     ncols = ai[1] - ai[0]; ai++;
2332:     brow  = bs*i;
2333:     for (j=0; j<ncols; j++) {
2334:       for (kcol=0; kcol<bs; kcol++) {
2335:         for (krow=0; krow<bs; krow++) {
2336:           atmp = PetscAbsScalar(*aa);aa++;
2337:           row  = brow + krow;   /* row index */
2338:           if (PetscAbsScalar(x[row]) < atmp) {x[row] = atmp; if (idx) idx[row] = bs*(*aj) + kcol;}
2339:         }
2340:       }
2341:       aj++;
2342:     }
2343:   }
2344:   VecRestoreArray(v,&x);
2345:   return(0);
2346: }

2348: PetscErrorCode MatCopy_SeqBAIJ(Mat A,Mat B,MatStructure str)
2349: {

2353:   /* If the two matrices have the same copy implementation, use fast copy. */
2354:   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2355:     Mat_SeqBAIJ *a  = (Mat_SeqBAIJ*)A->data;
2356:     Mat_SeqBAIJ *b  = (Mat_SeqBAIJ*)B->data;
2357:     PetscInt    ambs=a->mbs,bmbs=b->mbs,abs=A->rmap->bs,bbs=B->rmap->bs,bs2=abs*abs;

2359:     if (a->i[ambs] != b->i[bmbs]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzero blocks in matrices A %D and B %D are different",a->i[ambs],b->i[bmbs]);
2360:     if (abs != bbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Block size A %D and B %D are different",abs,bbs);
2361:     PetscArraycpy(b->a,a->a,bs2*a->i[ambs]);
2362:     PetscObjectStateIncrease((PetscObject)B);
2363:   } else {
2364:     MatCopy_Basic(A,B,str);
2365:   }
2366:   return(0);
2367: }

2369: PetscErrorCode MatSetUp_SeqBAIJ(Mat A)
2370: {

2374:   MatSeqBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,NULL);
2375:   return(0);
2376: }

2378: static PetscErrorCode MatSeqBAIJGetArray_SeqBAIJ(Mat A,PetscScalar *array[])
2379: {
2380:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;

2383:   *array = a->a;
2384:   return(0);
2385: }

2387: static PetscErrorCode MatSeqBAIJRestoreArray_SeqBAIJ(Mat A,PetscScalar *array[])
2388: {
2390:   *array = NULL;
2391:   return(0);
2392: }

2394: PetscErrorCode MatAXPYGetPreallocation_SeqBAIJ(Mat Y,Mat X,PetscInt *nnz)
2395: {
2396:   PetscInt       bs = Y->rmap->bs,mbs = Y->rmap->N/bs;
2397:   Mat_SeqBAIJ    *x = (Mat_SeqBAIJ*)X->data;
2398:   Mat_SeqBAIJ    *y = (Mat_SeqBAIJ*)Y->data;

2402:   /* Set the number of nonzeros in the new matrix */
2403:   MatAXPYGetPreallocation_SeqX_private(mbs,x->i,x->j,y->i,y->j,nnz);
2404:   return(0);
2405: }

2407: PetscErrorCode MatAXPY_SeqBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2408: {
2409:   Mat_SeqBAIJ    *x = (Mat_SeqBAIJ*)X->data,*y = (Mat_SeqBAIJ*)Y->data;
2411:   PetscInt       bs=Y->rmap->bs,bs2=bs*bs;
2412:   PetscBLASInt   one=1;

2415:   if (str == SAME_NONZERO_PATTERN) {
2416:     PetscScalar  alpha = a;
2417:     PetscBLASInt bnz;
2418:     PetscBLASIntCast(x->nz*bs2,&bnz);
2419:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2420:     PetscObjectStateIncrease((PetscObject)Y);
2421:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2422:     MatAXPY_Basic(Y,a,X,str);
2423:   } else {
2424:     Mat      B;
2425:     PetscInt *nnz;
2426:     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
2427:     PetscMalloc1(Y->rmap->N,&nnz);
2428:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2429:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2430:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2431:     MatSetBlockSizesFromMats(B,Y,Y);
2432:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
2433:     MatAXPYGetPreallocation_SeqBAIJ(Y,X,nnz);
2434:     MatSeqBAIJSetPreallocation(B,bs,0,nnz);
2435:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2436:     MatHeaderReplace(Y,&B);
2437:     PetscFree(nnz);
2438:   }
2439:   return(0);
2440: }

2442: PetscErrorCode MatRealPart_SeqBAIJ(Mat A)
2443: {
2444:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
2445:   PetscInt    i,nz = a->bs2*a->i[a->mbs];
2446:   MatScalar   *aa = a->a;

2449:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
2450:   return(0);
2451: }

2453: PetscErrorCode MatImaginaryPart_SeqBAIJ(Mat A)
2454: {
2455:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
2456:   PetscInt    i,nz = a->bs2*a->i[a->mbs];
2457:   MatScalar   *aa = a->a;

2460:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
2461:   return(0);
2462: }

2464: /*
2465:     Code almost identical to MatGetColumnIJ_SeqAIJ() should share common code
2466: */
2467: PetscErrorCode MatGetColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2468: {
2469:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2471:   PetscInt       bs = A->rmap->bs,i,*collengths,*cia,*cja,n = A->cmap->n/bs,m = A->rmap->n/bs;
2472:   PetscInt       nz = a->i[m],row,*jj,mr,col;

2475:   *nn = n;
2476:   if (!ia) return(0);
2477:   if (symmetric) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not for BAIJ matrices");
2478:   else {
2479:     PetscCalloc1(n,&collengths);
2480:     PetscMalloc1(n+1,&cia);
2481:     PetscMalloc1(nz,&cja);
2482:     jj   = a->j;
2483:     for (i=0; i<nz; i++) {
2484:       collengths[jj[i]]++;
2485:     }
2486:     cia[0] = oshift;
2487:     for (i=0; i<n; i++) {
2488:       cia[i+1] = cia[i] + collengths[i];
2489:     }
2490:     PetscArrayzero(collengths,n);
2491:     jj   = a->j;
2492:     for (row=0; row<m; row++) {
2493:       mr = a->i[row+1] - a->i[row];
2494:       for (i=0; i<mr; i++) {
2495:         col = *jj++;

2497:         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2498:       }
2499:     }
2500:     PetscFree(collengths);
2501:     *ia  = cia; *ja = cja;
2502:   }
2503:   return(0);
2504: }

2506: PetscErrorCode MatRestoreColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2507: {

2511:   if (!ia) return(0);
2512:   PetscFree(*ia);
2513:   PetscFree(*ja);
2514:   return(0);
2515: }

2517: /*
2518:  MatGetColumnIJ_SeqBAIJ_Color() and MatRestoreColumnIJ_SeqBAIJ_Color() are customized from
2519:  MatGetColumnIJ_SeqBAIJ() and MatRestoreColumnIJ_SeqBAIJ() by adding an output
2520:  spidx[], index of a->a, to be used in MatTransposeColoringCreate() and MatFDColoringCreate()
2521:  */
2522: PetscErrorCode MatGetColumnIJ_SeqBAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
2523: {
2524:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2526:   PetscInt       i,*collengths,*cia,*cja,n=a->nbs,m=a->mbs;
2527:   PetscInt       nz = a->i[m],row,*jj,mr,col;
2528:   PetscInt       *cspidx;

2531:   *nn = n;
2532:   if (!ia) return(0);

2534:   PetscCalloc1(n,&collengths);
2535:   PetscMalloc1(n+1,&cia);
2536:   PetscMalloc1(nz,&cja);
2537:   PetscMalloc1(nz,&cspidx);
2538:   jj   = a->j;
2539:   for (i=0; i<nz; i++) {
2540:     collengths[jj[i]]++;
2541:   }
2542:   cia[0] = oshift;
2543:   for (i=0; i<n; i++) {
2544:     cia[i+1] = cia[i] + collengths[i];
2545:   }
2546:   PetscArrayzero(collengths,n);
2547:   jj   = a->j;
2548:   for (row=0; row<m; row++) {
2549:     mr = a->i[row+1] - a->i[row];
2550:     for (i=0; i<mr; i++) {
2551:       col = *jj++;
2552:       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
2553:       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
2554:     }
2555:   }
2556:   PetscFree(collengths);
2557:   *ia    = cia;
2558:   *ja    = cja;
2559:   *spidx = cspidx;
2560:   return(0);
2561: }

2563: PetscErrorCode MatRestoreColumnIJ_SeqBAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
2564: {

2568:   MatRestoreColumnIJ_SeqBAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
2569:   PetscFree(*spidx);
2570:   return(0);
2571: }

2573: PetscErrorCode MatShift_SeqBAIJ(Mat Y,PetscScalar a)
2574: {
2576:   Mat_SeqBAIJ     *aij = (Mat_SeqBAIJ*)Y->data;

2579:   if (!Y->preallocated || !aij->nz) {
2580:     MatSeqBAIJSetPreallocation(Y,Y->rmap->bs,1,NULL);
2581:   }
2582:   MatShift_Basic(Y,a);
2583:   return(0);
2584: }

2586: /* -------------------------------------------------------------------*/
2587: static struct _MatOps MatOps_Values = {MatSetValues_SeqBAIJ,
2588:                                        MatGetRow_SeqBAIJ,
2589:                                        MatRestoreRow_SeqBAIJ,
2590:                                        MatMult_SeqBAIJ_N,
2591:                                /* 4*/  MatMultAdd_SeqBAIJ_N,
2592:                                        MatMultTranspose_SeqBAIJ,
2593:                                        MatMultTransposeAdd_SeqBAIJ,
2594:                                        NULL,
2595:                                        NULL,
2596:                                        NULL,
2597:                                /* 10*/ NULL,
2598:                                        MatLUFactor_SeqBAIJ,
2599:                                        NULL,
2600:                                        NULL,
2601:                                        MatTranspose_SeqBAIJ,
2602:                                /* 15*/ MatGetInfo_SeqBAIJ,
2603:                                        MatEqual_SeqBAIJ,
2604:                                        MatGetDiagonal_SeqBAIJ,
2605:                                        MatDiagonalScale_SeqBAIJ,
2606:                                        MatNorm_SeqBAIJ,
2607:                                /* 20*/ NULL,
2608:                                        MatAssemblyEnd_SeqBAIJ,
2609:                                        MatSetOption_SeqBAIJ,
2610:                                        MatZeroEntries_SeqBAIJ,
2611:                                /* 24*/ MatZeroRows_SeqBAIJ,
2612:                                        NULL,
2613:                                        NULL,
2614:                                        NULL,
2615:                                        NULL,
2616:                                /* 29*/ MatSetUp_SeqBAIJ,
2617:                                        NULL,
2618:                                        NULL,
2619:                                        NULL,
2620:                                        NULL,
2621:                                /* 34*/ MatDuplicate_SeqBAIJ,
2622:                                        NULL,
2623:                                        NULL,
2624:                                        MatILUFactor_SeqBAIJ,
2625:                                        NULL,
2626:                                /* 39*/ MatAXPY_SeqBAIJ,
2627:                                        MatCreateSubMatrices_SeqBAIJ,
2628:                                        MatIncreaseOverlap_SeqBAIJ,
2629:                                        MatGetValues_SeqBAIJ,
2630:                                        MatCopy_SeqBAIJ,
2631:                                /* 44*/ NULL,
2632:                                        MatScale_SeqBAIJ,
2633:                                        MatShift_SeqBAIJ,
2634:                                        NULL,
2635:                                        MatZeroRowsColumns_SeqBAIJ,
2636:                                /* 49*/ NULL,
2637:                                        MatGetRowIJ_SeqBAIJ,
2638:                                        MatRestoreRowIJ_SeqBAIJ,
2639:                                        MatGetColumnIJ_SeqBAIJ,
2640:                                        MatRestoreColumnIJ_SeqBAIJ,
2641:                                /* 54*/ MatFDColoringCreate_SeqXAIJ,
2642:                                        NULL,
2643:                                        NULL,
2644:                                        NULL,
2645:                                        MatSetValuesBlocked_SeqBAIJ,
2646:                                /* 59*/ MatCreateSubMatrix_SeqBAIJ,
2647:                                        MatDestroy_SeqBAIJ,
2648:                                        MatView_SeqBAIJ,
2649:                                        NULL,
2650:                                        NULL,
2651:                                /* 64*/ NULL,
2652:                                        NULL,
2653:                                        NULL,
2654:                                        NULL,
2655:                                        NULL,
2656:                                /* 69*/ MatGetRowMaxAbs_SeqBAIJ,
2657:                                        NULL,
2658:                                        MatConvert_Basic,
2659:                                        NULL,
2660:                                        NULL,
2661:                                /* 74*/ NULL,
2662:                                        MatFDColoringApply_BAIJ,
2663:                                        NULL,
2664:                                        NULL,
2665:                                        NULL,
2666:                                /* 79*/ NULL,
2667:                                        NULL,
2668:                                        NULL,
2669:                                        NULL,
2670:                                        MatLoad_SeqBAIJ,
2671:                                /* 84*/ NULL,
2672:                                        NULL,
2673:                                        NULL,
2674:                                        NULL,
2675:                                        NULL,
2676:                                /* 89*/ NULL,
2677:                                        NULL,
2678:                                        NULL,
2679:                                        NULL,
2680:                                        NULL,
2681:                                /* 94*/ NULL,
2682:                                        NULL,
2683:                                        NULL,
2684:                                        NULL,
2685:                                        NULL,
2686:                                /* 99*/ NULL,
2687:                                        NULL,
2688:                                        NULL,
2689:                                        NULL,
2690:                                        NULL,
2691:                                /*104*/ NULL,
2692:                                        MatRealPart_SeqBAIJ,
2693:                                        MatImaginaryPart_SeqBAIJ,
2694:                                        NULL,
2695:                                        NULL,
2696:                                /*109*/ NULL,
2697:                                        NULL,
2698:                                        NULL,
2699:                                        NULL,
2700:                                        MatMissingDiagonal_SeqBAIJ,
2701:                                /*114*/ NULL,
2702:                                        NULL,
2703:                                        NULL,
2704:                                        NULL,
2705:                                        NULL,
2706:                                /*119*/ NULL,
2707:                                        NULL,
2708:                                        MatMultHermitianTranspose_SeqBAIJ,
2709:                                        MatMultHermitianTransposeAdd_SeqBAIJ,
2710:                                        NULL,
2711:                                /*124*/ NULL,
2712:                                        NULL,
2713:                                        MatInvertBlockDiagonal_SeqBAIJ,
2714:                                        NULL,
2715:                                        NULL,
2716:                                /*129*/ NULL,
2717:                                        NULL,
2718:                                        NULL,
2719:                                        NULL,
2720:                                        NULL,
2721:                                /*134*/ NULL,
2722:                                        NULL,
2723:                                        NULL,
2724:                                        NULL,
2725:                                        NULL,
2726:                                /*139*/ MatSetBlockSizes_Default,
2727:                                        NULL,
2728:                                        NULL,
2729:                                        MatFDColoringSetUp_SeqXAIJ,
2730:                                        NULL,
2731:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_SeqBAIJ,
2732:                                        MatDestroySubMatrices_SeqBAIJ
2733: };

2735: PetscErrorCode  MatStoreValues_SeqBAIJ(Mat mat)
2736: {
2737:   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ*)mat->data;
2738:   PetscInt       nz   = aij->i[aij->mbs]*aij->bs2;

2742:   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");

2744:   /* allocate space for values if not already there */
2745:   if (!aij->saved_values) {
2746:     PetscMalloc1(nz+1,&aij->saved_values);
2747:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
2748:   }

2750:   /* copy values over */
2751:   PetscArraycpy(aij->saved_values,aij->a,nz);
2752:   return(0);
2753: }

2755: PetscErrorCode  MatRetrieveValues_SeqBAIJ(Mat mat)
2756: {
2757:   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ*)mat->data;
2759:   PetscInt       nz = aij->i[aij->mbs]*aij->bs2;

2762:   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2763:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");

2765:   /* copy values over */
2766:   PetscArraycpy(aij->a,aij->saved_values,nz);
2767:   return(0);
2768: }

2770: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
2771: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqSBAIJ(Mat, MatType,MatReuse,Mat*);

2773: PetscErrorCode  MatSeqBAIJSetPreallocation_SeqBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
2774: {
2775:   Mat_SeqBAIJ    *b;
2777:   PetscInt       i,mbs,nbs,bs2;
2778:   PetscBool      flg = PETSC_FALSE,skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;

2781:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
2782:   if (nz == MAT_SKIP_ALLOCATION) {
2783:     skipallocation = PETSC_TRUE;
2784:     nz             = 0;
2785:   }

2787:   MatSetBlockSize(B,PetscAbs(bs));
2788:   PetscLayoutSetUp(B->rmap);
2789:   PetscLayoutSetUp(B->cmap);
2790:   PetscLayoutGetBlockSize(B->rmap,&bs);

2792:   B->preallocated = PETSC_TRUE;

2794:   mbs = B->rmap->n/bs;
2795:   nbs = B->cmap->n/bs;
2796:   bs2 = bs*bs;

2798:   if (mbs*bs!=B->rmap->n || nbs*bs!=B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows %D, cols %D must be divisible by blocksize %D",B->rmap->N,B->cmap->n,bs);

2800:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2801:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
2802:   if (nnz) {
2803:     for (i=0; i<mbs; i++) {
2804:       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %D value %D",i,nnz[i]);
2805:       if (nnz[i] > nbs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than block row length: local row %D value %D rowlength %D",i,nnz[i],nbs);
2806:     }
2807:   }

2809:   b    = (Mat_SeqBAIJ*)B->data;
2810:   PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Optimize options for SEQBAIJ matrix 2 ","Mat");
2811:   PetscOptionsBool("-mat_no_unroll","Do not optimize for block size (slow)",NULL,flg,&flg,NULL);
2812:   PetscOptionsEnd();

2814:   if (!flg) {
2815:     switch (bs) {
2816:     case 1:
2817:       B->ops->mult    = MatMult_SeqBAIJ_1;
2818:       B->ops->multadd = MatMultAdd_SeqBAIJ_1;
2819:       break;
2820:     case 2:
2821:       B->ops->mult    = MatMult_SeqBAIJ_2;
2822:       B->ops->multadd = MatMultAdd_SeqBAIJ_2;
2823:       break;
2824:     case 3:
2825:       B->ops->mult    = MatMult_SeqBAIJ_3;
2826:       B->ops->multadd = MatMultAdd_SeqBAIJ_3;
2827:       break;
2828:     case 4:
2829:       B->ops->mult    = MatMult_SeqBAIJ_4;
2830:       B->ops->multadd = MatMultAdd_SeqBAIJ_4;
2831:       break;
2832:     case 5:
2833:       B->ops->mult    = MatMult_SeqBAIJ_5;
2834:       B->ops->multadd = MatMultAdd_SeqBAIJ_5;
2835:       break;
2836:     case 6:
2837:       B->ops->mult    = MatMult_SeqBAIJ_6;
2838:       B->ops->multadd = MatMultAdd_SeqBAIJ_6;
2839:       break;
2840:     case 7:
2841:       B->ops->mult    = MatMult_SeqBAIJ_7;
2842:       B->ops->multadd = MatMultAdd_SeqBAIJ_7;
2843:       break;
2844:     case 9:
2845: #if defined(PETSC_HAVE_IMMINTRIN_H) && defined(__AVX2__) && defined(__FMA__) && defined(PETSC_USE_REAL_DOUBLE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_64BIT_INDICES)
2846:       B->ops->mult    = MatMult_SeqBAIJ_9_AVX2;
2847:       B->ops->multadd = MatMultAdd_SeqBAIJ_9_AVX2;
2848: #else
2849:       B->ops->mult    = MatMult_SeqBAIJ_N;
2850:       B->ops->multadd = MatMultAdd_SeqBAIJ_N;
2851: #endif
2852:       break;
2853:     case 11:
2854:       B->ops->mult    = MatMult_SeqBAIJ_11;
2855:       B->ops->multadd = MatMultAdd_SeqBAIJ_11;
2856:       break;
2857:     case 15:
2858:       B->ops->mult    = MatMult_SeqBAIJ_15_ver1;
2859:       B->ops->multadd = MatMultAdd_SeqBAIJ_N;
2860:       break;
2861:     default:
2862:       B->ops->mult    = MatMult_SeqBAIJ_N;
2863:       B->ops->multadd = MatMultAdd_SeqBAIJ_N;
2864:       break;
2865:     }
2866:   }
2867:   B->ops->sor = MatSOR_SeqBAIJ;
2868:   b->mbs = mbs;
2869:   b->nbs = nbs;
2870:   if (!skipallocation) {
2871:     if (!b->imax) {
2872:       PetscMalloc2(mbs,&b->imax,mbs,&b->ilen);
2873:       PetscLogObjectMemory((PetscObject)B,2*mbs*sizeof(PetscInt));

2875:       b->free_imax_ilen = PETSC_TRUE;
2876:     }
2877:     /* b->ilen will count nonzeros in each block row so far. */
2878:     for (i=0; i<mbs; i++) b->ilen[i] = 0;
2879:     if (!nnz) {
2880:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2881:       else if (nz < 0) nz = 1;
2882:       nz = PetscMin(nz,nbs);
2883:       for (i=0; i<mbs; i++) b->imax[i] = nz;
2884:       PetscIntMultError(nz,mbs,&nz);
2885:     } else {
2886:       PetscInt64 nz64 = 0;
2887:       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];}
2888:       PetscIntCast(nz64,&nz);
2889:     }

2891:     /* allocate the matrix space */
2892:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
2893:     if (B->structure_only) {
2894:       PetscMalloc1(nz,&b->j);
2895:       PetscMalloc1(B->rmap->N+1,&b->i);
2896:       PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
2897:     } else {
2898:       PetscMalloc3(bs2*nz,&b->a,nz,&b->j,B->rmap->N+1,&b->i);
2899:       PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));
2900:       PetscArrayzero(b->a,nz*bs2);
2901:     }
2902:     PetscArrayzero(b->j,nz);

2904:     if (B->structure_only) {
2905:       b->singlemalloc = PETSC_FALSE;
2906:       b->free_a       = PETSC_FALSE;
2907:     } else {
2908:       b->singlemalloc = PETSC_TRUE;
2909:       b->free_a       = PETSC_TRUE;
2910:     }
2911:     b->free_ij = PETSC_TRUE;

2913:     b->i[0] = 0;
2914:     for (i=1; i<mbs+1; i++) {
2915:       b->i[i] = b->i[i-1] + b->imax[i-1];
2916:     }

2918:   } else {
2919:     b->free_a  = PETSC_FALSE;
2920:     b->free_ij = PETSC_FALSE;
2921:   }

2923:   b->bs2              = bs2;
2924:   b->mbs              = mbs;
2925:   b->nz               = 0;
2926:   b->maxnz            = nz;
2927:   B->info.nz_unneeded = (PetscReal)b->maxnz*bs2;
2928:   B->was_assembled    = PETSC_FALSE;
2929:   B->assembled        = PETSC_FALSE;
2930:   if (realalloc) {MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);}
2931:   return(0);
2932: }

2934: PetscErrorCode MatSeqBAIJSetPreallocationCSR_SeqBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2935: {
2936:   PetscInt       i,m,nz,nz_max=0,*nnz;
2937:   PetscScalar    *values=NULL;
2938:   PetscBool      roworiented = ((Mat_SeqBAIJ*)B->data)->roworiented;

2942:   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
2943:   PetscLayoutSetBlockSize(B->rmap,bs);
2944:   PetscLayoutSetBlockSize(B->cmap,bs);
2945:   PetscLayoutSetUp(B->rmap);
2946:   PetscLayoutSetUp(B->cmap);
2947:   PetscLayoutGetBlockSize(B->rmap,&bs);
2948:   m    = B->rmap->n/bs;

2950:   if (ii[0] != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %D",ii[0]);
2951:   PetscMalloc1(m+1, &nnz);
2952:   for (i=0; i<m; i++) {
2953:     nz = ii[i+1]- ii[i];
2954:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D",i,nz);
2955:     nz_max = PetscMax(nz_max, nz);
2956:     nnz[i] = nz;
2957:   }
2958:   MatSeqBAIJSetPreallocation(B,bs,0,nnz);
2959:   PetscFree(nnz);

2961:   values = (PetscScalar*)V;
2962:   if (!values) {
2963:     PetscCalloc1(bs*bs*(nz_max+1),&values);
2964:   }
2965:   for (i=0; i<m; i++) {
2966:     PetscInt          ncols  = ii[i+1] - ii[i];
2967:     const PetscInt    *icols = jj + ii[i];
2968:     if (bs == 1 || !roworiented) {
2969:       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2970:       MatSetValuesBlocked_SeqBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);
2971:     } else {
2972:       PetscInt j;
2973:       for (j=0; j<ncols; j++) {
2974:         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
2975:         MatSetValuesBlocked_SeqBAIJ(B,1,&i,1,&icols[j],svals,INSERT_VALUES);
2976:       }
2977:     }
2978:   }
2979:   if (!V) { PetscFree(values); }
2980:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2981:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2982:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2983:   return(0);
2984: }

2986: /*@C
2987:    MatSeqBAIJGetArray - gives access to the array where the data for a MATSEQBAIJ matrix is stored

2989:    Not Collective

2991:    Input Parameter:
2992: .  mat - a MATSEQBAIJ matrix

2994:    Output Parameter:
2995: .   array - pointer to the data

2997:    Level: intermediate

2999: .seealso: MatSeqBAIJRestoreArray(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray()
3000: @*/
3001: PetscErrorCode MatSeqBAIJGetArray(Mat A,PetscScalar **array)
3002: {

3006:   PetscUseMethod(A,"MatSeqBAIJGetArray_C",(Mat,PetscScalar**),(A,array));
3007:   return(0);
3008: }

3010: /*@C
3011:    MatSeqBAIJRestoreArray - returns access to the array where the data for a MATSEQBAIJ matrix is stored obtained by MatSeqBAIJGetArray()

3013:    Not Collective

3015:    Input Parameters:
3016: +  mat - a MATSEQBAIJ matrix
3017: -  array - pointer to the data

3019:    Level: intermediate

3021: .seealso: MatSeqBAIJGetArray(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray()
3022: @*/
3023: PetscErrorCode MatSeqBAIJRestoreArray(Mat A,PetscScalar **array)
3024: {

3028:   PetscUseMethod(A,"MatSeqBAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
3029:   return(0);
3030: }

3032: /*MC
3033:    MATSEQBAIJ - MATSEQBAIJ = "seqbaij" - A matrix type to be used for sequential block sparse matrices, based on
3034:    block sparse compressed row format.

3036:    Options Database Keys:
3037: . -mat_type seqbaij - sets the matrix type to "seqbaij" during a call to MatSetFromOptions()

3039:    Level: beginner

3041:    Notes:
3042:     MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no
3043:     space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored

3045: .seealso: MatCreateSeqBAIJ()
3046: M*/

3048: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqBSTRM(Mat, MatType,MatReuse,Mat*);

3050: PETSC_EXTERN PetscErrorCode MatCreate_SeqBAIJ(Mat B)
3051: {
3053:   PetscMPIInt    size;
3054:   Mat_SeqBAIJ    *b;

3057:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
3058:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");

3060:   PetscNewLog(B,&b);
3061:   B->data = (void*)b;
3062:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

3064:   b->row          = NULL;
3065:   b->col          = NULL;
3066:   b->icol         = NULL;
3067:   b->reallocs     = 0;
3068:   b->saved_values = NULL;

3070:   b->roworiented        = PETSC_TRUE;
3071:   b->nonew              = 0;
3072:   b->diag               = NULL;
3073:   B->spptr              = NULL;
3074:   B->info.nz_unneeded   = (PetscReal)b->maxnz*b->bs2;
3075:   b->keepnonzeropattern = PETSC_FALSE;

3077:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJGetArray_C",MatSeqBAIJGetArray_SeqBAIJ);
3078:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJRestoreArray_C",MatSeqBAIJRestoreArray_SeqBAIJ);
3079:   PetscObjectComposeFunction((PetscObject)B,"MatInvertBlockDiagonal_C",MatInvertBlockDiagonal_SeqBAIJ);
3080:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqBAIJ);
3081:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqBAIJ);
3082:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJSetColumnIndices_C",MatSeqBAIJSetColumnIndices_SeqBAIJ);
3083:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqbaij_seqaij_C",MatConvert_SeqBAIJ_SeqAIJ);
3084:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqbaij_seqsbaij_C",MatConvert_SeqBAIJ_SeqSBAIJ);
3085:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJSetPreallocation_C",MatSeqBAIJSetPreallocation_SeqBAIJ);
3086:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",MatSeqBAIJSetPreallocationCSR_SeqBAIJ);
3087:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqBAIJ);
3088: #if defined(PETSC_HAVE_HYPRE)
3089:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqbaij_hypre_C",MatConvert_AIJ_HYPRE);
3090: #endif
3091:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqbaij_is_C",MatConvert_XAIJ_IS);
3092:   PetscObjectChangeTypeName((PetscObject)B,MATSEQBAIJ);
3093:   return(0);
3094: }

3096: PetscErrorCode MatDuplicateNoCreate_SeqBAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
3097: {
3098:   Mat_SeqBAIJ    *c = (Mat_SeqBAIJ*)C->data,*a = (Mat_SeqBAIJ*)A->data;
3100:   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 = a->bs2;

3103:   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");

3105:   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
3106:     c->imax           = a->imax;
3107:     c->ilen           = a->ilen;
3108:     c->free_imax_ilen = PETSC_FALSE;
3109:   } else {
3110:     PetscMalloc2(mbs,&c->imax,mbs,&c->ilen);
3111:     PetscLogObjectMemory((PetscObject)C,2*mbs*sizeof(PetscInt));
3112:     for (i=0; i<mbs; i++) {
3113:       c->imax[i] = a->imax[i];
3114:       c->ilen[i] = a->ilen[i];
3115:     }
3116:     c->free_imax_ilen = PETSC_TRUE;
3117:   }

3119:   /* allocate the matrix space */
3120:   if (mallocmatspace) {
3121:     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
3122:       PetscCalloc1(bs2*nz,&c->a);
3123:       PetscLogObjectMemory((PetscObject)C,a->i[mbs]*bs2*sizeof(PetscScalar));

3125:       c->i            = a->i;
3126:       c->j            = a->j;
3127:       c->singlemalloc = PETSC_FALSE;
3128:       c->free_a       = PETSC_TRUE;
3129:       c->free_ij      = PETSC_FALSE;
3130:       c->parent       = A;
3131:       C->preallocated = PETSC_TRUE;
3132:       C->assembled    = PETSC_TRUE;

3134:       PetscObjectReference((PetscObject)A);
3135:       MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3136:       MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3137:     } else {
3138:       PetscMalloc3(bs2*nz,&c->a,nz,&c->j,mbs+1,&c->i);
3139:       PetscLogObjectMemory((PetscObject)C,a->i[mbs]*(bs2*sizeof(PetscScalar)+sizeof(PetscInt))+(mbs+1)*sizeof(PetscInt));

3141:       c->singlemalloc = PETSC_TRUE;
3142:       c->free_a       = PETSC_TRUE;
3143:       c->free_ij      = PETSC_TRUE;

3145:       PetscArraycpy(c->i,a->i,mbs+1);
3146:       if (mbs > 0) {
3147:         PetscArraycpy(c->j,a->j,nz);
3148:         if (cpvalues == MAT_COPY_VALUES) {
3149:           PetscArraycpy(c->a,a->a,bs2*nz);
3150:         } else {
3151:           PetscArrayzero(c->a,bs2*nz);
3152:         }
3153:       }
3154:       C->preallocated = PETSC_TRUE;
3155:       C->assembled    = PETSC_TRUE;
3156:     }
3157:   }

3159:   c->roworiented = a->roworiented;
3160:   c->nonew       = a->nonew;

3162:   PetscLayoutReference(A->rmap,&C->rmap);
3163:   PetscLayoutReference(A->cmap,&C->cmap);

3165:   c->bs2         = a->bs2;
3166:   c->mbs         = a->mbs;
3167:   c->nbs         = a->nbs;

3169:   if (a->diag) {
3170:     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
3171:       c->diag      = a->diag;
3172:       c->free_diag = PETSC_FALSE;
3173:     } else {
3174:       PetscMalloc1(mbs+1,&c->diag);
3175:       PetscLogObjectMemory((PetscObject)C,(mbs+1)*sizeof(PetscInt));
3176:       for (i=0; i<mbs; i++) c->diag[i] = a->diag[i];
3177:       c->free_diag = PETSC_TRUE;
3178:     }
3179:   } else c->diag = NULL;

3181:   c->nz         = a->nz;
3182:   c->maxnz      = a->nz;         /* Since we allocate exactly the right amount */
3183:   c->solve_work = NULL;
3184:   c->mult_work  = NULL;
3185:   c->sor_workt  = NULL;
3186:   c->sor_work   = NULL;

3188:   c->compressedrow.use   = a->compressedrow.use;
3189:   c->compressedrow.nrows = a->compressedrow.nrows;
3190:   if (a->compressedrow.use) {
3191:     i    = a->compressedrow.nrows;
3192:     PetscMalloc2(i+1,&c->compressedrow.i,i+1,&c->compressedrow.rindex);
3193:     PetscLogObjectMemory((PetscObject)C,(2*i+1)*sizeof(PetscInt));
3194:     PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);
3195:     PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);
3196:   } else {
3197:     c->compressedrow.use    = PETSC_FALSE;
3198:     c->compressedrow.i      = NULL;
3199:     c->compressedrow.rindex = NULL;
3200:   }
3201:   C->nonzerostate = A->nonzerostate;

3203:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
3204:   return(0);
3205: }

3207: PetscErrorCode MatDuplicate_SeqBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3208: {

3212:   MatCreate(PetscObjectComm((PetscObject)A),B);
3213:   MatSetSizes(*B,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);
3214:   MatSetType(*B,MATSEQBAIJ);
3215:   MatDuplicateNoCreate_SeqBAIJ(*B,A,cpvalues,PETSC_TRUE);
3216:   return(0);
3217: }

3219: /* Used for both SeqBAIJ and SeqSBAIJ matrices */
3220: PetscErrorCode MatLoad_SeqBAIJ_Binary(Mat mat,PetscViewer viewer)
3221: {
3222:   PetscInt       header[4],M,N,nz,bs,m,n,mbs,nbs,rows,cols,sum,i,j,k;
3223:   PetscInt       *rowidxs,*colidxs;
3224:   PetscScalar    *matvals;

3228:   PetscViewerSetUp(viewer);

3230:   /* read matrix header */
3231:   PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);
3232:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file");
3233:   M = header[1]; N = header[2]; nz = header[3];
3234:   if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M);
3235:   if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N);
3236:   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as SeqBAIJ");

3238:   /* set block sizes from the viewer's .info file */
3239:   MatLoad_Binary_BlockSizes(mat,viewer);
3240:   /* set local and global sizes if not set already */
3241:   if (mat->rmap->n < 0) mat->rmap->n = M;
3242:   if (mat->cmap->n < 0) mat->cmap->n = N;
3243:   if (mat->rmap->N < 0) mat->rmap->N = M;
3244:   if (mat->cmap->N < 0) mat->cmap->N = N;
3245:   PetscLayoutSetUp(mat->rmap);
3246:   PetscLayoutSetUp(mat->cmap);

3248:   /* check if the matrix sizes are correct */
3249:   MatGetSize(mat,&rows,&cols);
3250:   if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols);
3251:   MatGetBlockSize(mat,&bs);
3252:   MatGetLocalSize(mat,&m,&n);
3253:   mbs = m/bs; nbs = n/bs;

3255:   /* read in row lengths, column indices and nonzero values */
3256:   PetscMalloc1(m+1,&rowidxs);
3257:   PetscViewerBinaryRead(viewer,rowidxs+1,m,NULL,PETSC_INT);
3258:   rowidxs[0] = 0; for (i=0; i<m; i++) rowidxs[i+1] += rowidxs[i];
3259:   sum = rowidxs[m];
3260:   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent matrix data in file: nonzeros = %D, sum-row-lengths = %D\n",nz,sum);

3262:   /* read in column indices and nonzero values */
3263:   PetscMalloc2(rowidxs[m],&colidxs,nz,&matvals);
3264:   PetscViewerBinaryRead(viewer,colidxs,rowidxs[m],NULL,PETSC_INT);
3265:   PetscViewerBinaryRead(viewer,matvals,rowidxs[m],NULL,PETSC_SCALAR);

3267:   { /* preallocate matrix storage */
3268:     PetscBT   bt; /* helper bit set to count nonzeros */
3269:     PetscInt  *nnz;
3270:     PetscBool sbaij;

3272:     PetscBTCreate(nbs,&bt);
3273:     PetscCalloc1(mbs,&nnz);
3274:     PetscObjectTypeCompare((PetscObject)mat,MATSEQSBAIJ,&sbaij);
3275:     for (i=0; i<mbs; i++) {
3276:       PetscBTMemzero(nbs,bt);
3277:       for (k=0; k<bs; k++) {
3278:         PetscInt row = bs*i + k;
3279:         for (j=rowidxs[row]; j<rowidxs[row+1]; j++) {
3280:           PetscInt col = colidxs[j];
3281:           if (!sbaij || col >= row)
3282:             if (!PetscBTLookupSet(bt,col/bs)) nnz[i]++;
3283:         }
3284:       }
3285:     }
3286:     PetscBTDestroy(&bt);
3287:     MatSeqBAIJSetPreallocation(mat,bs,0,nnz);
3288:     MatSeqSBAIJSetPreallocation(mat,bs,0,nnz);
3289:     PetscFree(nnz);
3290:   }

3292:   /* store matrix values */
3293:   for (i=0; i<m; i++) {
3294:     PetscInt row = i, s = rowidxs[i], e = rowidxs[i+1];
3295:     (*mat->ops->setvalues)(mat,1,&row,e-s,colidxs+s,matvals+s,INSERT_VALUES);
3296:   }

3298:   PetscFree(rowidxs);
3299:   PetscFree2(colidxs,matvals);
3300:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
3301:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
3302:   return(0);
3303: }

3305: PetscErrorCode MatLoad_SeqBAIJ(Mat mat,PetscViewer viewer)
3306: {
3308:   PetscBool      isbinary;

3311:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
3312:   if (!isbinary) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)mat)->type_name);
3313:   MatLoad_SeqBAIJ_Binary(mat,viewer);
3314:   return(0);
3315: }

3317: /*@C
3318:    MatCreateSeqBAIJ - Creates a sparse matrix in block AIJ (block
3319:    compressed row) format.  For good matrix assembly performance the
3320:    user should preallocate the matrix storage by setting the parameter nz
3321:    (or the array nnz).  By setting these parameters accurately, performance
3322:    during matrix assembly can be increased by more than a factor of 50.

3324:    Collective

3326:    Input Parameters:
3327: +  comm - MPI communicator, set to PETSC_COMM_SELF
3328: .  bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
3329:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
3330: .  m - number of rows
3331: .  n - number of columns
3332: .  nz - number of nonzero blocks  per block row (same for all rows)
3333: -  nnz - array containing the number of nonzero blocks in the various block rows
3334:          (possibly different for each block row) or NULL

3336:    Output Parameter:
3337: .  A - the matrix

3339:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3340:    MatXXXXSetPreallocation() paradigm instead of this routine directly.
3341:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

3343:    Options Database Keys:
3344: +   -mat_no_unroll - uses code that does not unroll the loops in the
3345:                      block calculations (much slower)
3346: -    -mat_block_size - size of the blocks to use

3348:    Level: intermediate

3350:    Notes:
3351:    The number of rows and columns must be divisible by blocksize.

3353:    If the nnz parameter is given then the nz parameter is ignored

3355:    A nonzero block is any block that as 1 or more nonzeros in it

3357:    The block AIJ format is fully compatible with standard Fortran 77
3358:    storage.  That is, the stored row and column indices can begin at
3359:    either one (as in Fortran) or zero.  See the users' manual for details.

3361:    Specify the preallocated storage with either nz or nnz (not both).
3362:    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3363:    allocation.  See Users-Manual: ch_mat for details.
3364:    matrices.

3366: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateBAIJ()
3367: @*/
3368: PetscErrorCode  MatCreateSeqBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3369: {

3373:   MatCreate(comm,A);
3374:   MatSetSizes(*A,m,n,m,n);
3375:   MatSetType(*A,MATSEQBAIJ);
3376:   MatSeqBAIJSetPreallocation(*A,bs,nz,(PetscInt*)nnz);
3377:   return(0);
3378: }

3380: /*@C
3381:    MatSeqBAIJSetPreallocation - Sets the block size and expected nonzeros
3382:    per row in the matrix. For good matrix assembly performance the
3383:    user should preallocate the matrix storage by setting the parameter nz
3384:    (or the array nnz).  By setting these parameters accurately, performance
3385:    during matrix assembly can be increased by more than a factor of 50.

3387:    Collective

3389:    Input Parameters:
3390: +  B - the matrix
3391: .  bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
3392:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
3393: .  nz - number of block nonzeros per block row (same for all rows)
3394: -  nnz - array containing the number of block nonzeros in the various block rows
3395:          (possibly different for each block row) or NULL

3397:    Options Database Keys:
3398: +   -mat_no_unroll - uses code that does not unroll the loops in the
3399:                      block calculations (much slower)
3400: -   -mat_block_size - size of the blocks to use

3402:    Level: intermediate

3404:    Notes:
3405:    If the nnz parameter is given then the nz parameter is ignored

3407:    You can call MatGetInfo() to get information on how effective the preallocation was;
3408:    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3409:    You can also run with the option -info and look for messages with the string
3410:    malloc in them to see if additional memory allocation was needed.

3412:    The block AIJ format is fully compatible with standard Fortran 77
3413:    storage.  That is, the stored row and column indices can begin at
3414:    either one (as in Fortran) or zero.  See the users' manual for details.

3416:    Specify the preallocated storage with either nz or nnz (not both).
3417:    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3418:    allocation.  See Users-Manual: ch_mat for details.

3420: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateBAIJ(), MatGetInfo()
3421: @*/
3422: PetscErrorCode  MatSeqBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
3423: {

3430:   PetscTryMethod(B,"MatSeqBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));
3431:   return(0);
3432: }

3434: /*@C
3435:    MatSeqBAIJSetPreallocationCSR - Creates a sparse parallel matrix in BAIJ format using the given nonzero structure and (optional) numerical values

3437:    Collective

3439:    Input Parameters:
3440: +  B - the matrix
3441: .  i - the indices into j for the start of each local row (starts with zero)
3442: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
3443: -  v - optional values in the matrix

3445:    Level: advanced

3447:    Notes:
3448:    The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED.  For example, C programs
3449:    may want to use the default MAT_ROW_ORIENTED=PETSC_TRUE and use an array v[nnz][bs][bs] where the second index is
3450:    over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
3451:    MAT_ROW_ORIENTED=PETSC_FALSE and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
3452:    block column and the second index is over columns within a block.

3454:    Though this routine has Preallocation() in the name it also sets the exact nonzero locations of the matrix entries and usually the numerical values as well

3456: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatSeqBAIJSetPreallocation(), MATSEQBAIJ
3457: @*/
3458: PetscErrorCode  MatSeqBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3459: {

3466:   PetscTryMethod(B,"MatSeqBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
3467:   return(0);
3468: }


3471: /*@
3472:      MatCreateSeqBAIJWithArrays - Creates an sequential BAIJ matrix using matrix elements provided by the user.

3474:      Collective

3476:    Input Parameters:
3477: +  comm - must be an MPI communicator of size 1
3478: .  bs - size of block
3479: .  m - number of rows
3480: .  n - number of columns
3481: .  i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row block row of the matrix
3482: .  j - column indices
3483: -  a - matrix values

3485:    Output Parameter:
3486: .  mat - the matrix

3488:    Level: advanced

3490:    Notes:
3491:        The i, j, and a arrays are not copied by this routine, the user must free these arrays
3492:     once the matrix is destroyed

3494:        You cannot set new nonzero locations into this matrix, that will generate an error.

3496:        The i and j indices are 0 based

3498:        When block size is greater than 1 the matrix values must be stored using the BAIJ storage format (see the BAIJ code to determine this).

3500:       The order of the entries in values is the same as the block compressed sparse row storage format; that is, it is
3501:       the same as a three dimensional array in Fortran values(bs,bs,nnz) that contains the first column of the first
3502:       block, followed by the second column of the first block etc etc.  That is, the blocks are contiguous in memory
3503:       with column-major ordering within blocks.

3505: .seealso: MatCreate(), MatCreateBAIJ(), MatCreateSeqBAIJ()

3507: @*/
3508: PetscErrorCode  MatCreateSeqBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
3509: {
3511:   PetscInt       ii;
3512:   Mat_SeqBAIJ    *baij;

3515:   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
3516:   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");

3518:   MatCreate(comm,mat);
3519:   MatSetSizes(*mat,m,n,m,n);
3520:   MatSetType(*mat,MATSEQBAIJ);
3521:   MatSeqBAIJSetPreallocation(*mat,bs,MAT_SKIP_ALLOCATION,NULL);
3522:   baij = (Mat_SeqBAIJ*)(*mat)->data;
3523:   PetscMalloc2(m,&baij->imax,m,&baij->ilen);
3524:   PetscLogObjectMemory((PetscObject)*mat,2*m*sizeof(PetscInt));

3526:   baij->i = i;
3527:   baij->j = j;
3528:   baij->a = a;

3530:   baij->singlemalloc = PETSC_FALSE;
3531:   baij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3532:   baij->free_a       = PETSC_FALSE;
3533:   baij->free_ij      = PETSC_FALSE;

3535:   for (ii=0; ii<m; ii++) {
3536:     baij->ilen[ii] = baij->imax[ii] = i[ii+1] - i[ii];
3537:     if (PetscUnlikelyDebug(i[ii+1] - i[ii] < 0)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
3538:   }
3539:   if (PetscDefined(USE_DEBUG)) {
3540:     for (ii=0; ii<baij->i[m]; ii++) {
3541:       if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3542:       if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
3543:     }
3544:   }

3546:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
3547:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
3548:   return(0);
3549: }

3551: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
3552: {
3554:   PetscMPIInt    size;

3557:   MPI_Comm_size(comm,&size);
3558:   if (size == 1 && scall == MAT_REUSE_MATRIX) {
3559:     MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
3560:   } else {
3561:     MatCreateMPIMatConcatenateSeqMat_MPIBAIJ(comm,inmat,n,scall,outmat);
3562:   }
3563:   return(0);
3564: }