Actual source code: baij.c

petsc-master 2017-01-16
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: PetscErrorCode MatInvertBlockDiagonal_SeqBAIJ(Mat A,const PetscScalar **values)
 16: {
 17:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*) A->data;
 19:   PetscInt       *diag_offset,i,bs = A->rmap->bs,mbs = a->mbs,ipvt[5],bs2 = bs*bs,*v_pivots;
 20:   MatScalar      *v    = a->a,*odiag,*diag,*mdiag,work[25],*v_work;
 21:   PetscReal      shift = 0.0;
 22:   PetscBool      allowzeropivot,zeropivotdetected=PETSC_FALSE;

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

 27:   if (a->idiagvalid) {
 28:     if (values) *values = a->idiag;
 29:     return(0);
 30:   }
 31:   MatMarkDiagonal_SeqBAIJ(A);
 32:   diag_offset = a->diag;
 33:   if (!a->idiag) {
 34:     PetscMalloc1(2*bs2*mbs,&a->idiag);
 35:     PetscLogObjectMemory((PetscObject)A,2*bs2*mbs*sizeof(PetscScalar));
 36:   }
 37:   diag  = a->idiag;
 38:   mdiag = a->idiag+bs2*mbs;
 39:   if (values) *values = a->idiag;
 40:   /* factor and invert each block */
 41:   switch (bs) {
 42:   case 1:
 43:     for (i=0; i<mbs; i++) {
 44:       odiag    = v + 1*diag_offset[i];
 45:       diag[0]  = odiag[0];
 46:       mdiag[0] = odiag[0];
 47: 
 48:       if (PetscAbsScalar(diag[0] + shift) < PETSC_MACHINE_EPSILON) {
 49:         if (allowzeropivot) {
 50:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 51:           A->factorerror_zeropivot_value = PetscAbsScalar(diag[0]);
 52:           A->factorerror_zeropivot_row   = i;
 53:           PetscInfo1(A,"Zero pivot, row %D\n",i);
 54:         } 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);
 55:       }
 56: 
 57:       diag[0]  = (PetscScalar)1.0 / (diag[0] + shift);
 58:       diag    += 1;
 59:       mdiag   += 1;
 60:     }
 61:     break;
 62:   case 2:
 63:     for (i=0; i<mbs; i++) {
 64:       odiag    = v + 4*diag_offset[i];
 65:       diag[0]  = odiag[0]; diag[1] = odiag[1]; diag[2] = odiag[2]; diag[3] = odiag[3];
 66:       mdiag[0] = odiag[0]; mdiag[1] = odiag[1]; mdiag[2] = odiag[2]; mdiag[3] = odiag[3];
 67:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
 68:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 69:       diag    += 4;
 70:       mdiag   += 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:       mdiag[0] = odiag[0]; mdiag[1] = odiag[1]; mdiag[2] = odiag[2]; mdiag[3] = odiag[3];
 80:       mdiag[4] = odiag[4]; mdiag[5] = odiag[5]; mdiag[6] = odiag[6]; mdiag[7] = odiag[7];
 81:       mdiag[8] = odiag[8];
 82:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
 83:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 84:       diag    += 9;
 85:       mdiag   += 9;
 86:     }
 87:     break;
 88:   case 4:
 89:     for (i=0; i<mbs; i++) {
 90:       odiag  = v + 16*diag_offset[i];
 91:       PetscMemcpy(diag,odiag,16*sizeof(PetscScalar));
 92:       PetscMemcpy(mdiag,odiag,16*sizeof(PetscScalar));
 93:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
 94:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
 95:       diag  += 16;
 96:       mdiag += 16;
 97:     }
 98:     break;
 99:   case 5:
100:     for (i=0; i<mbs; i++) {
101:       odiag  = v + 25*diag_offset[i];
102:       PetscMemcpy(diag,odiag,25*sizeof(PetscScalar));
103:       PetscMemcpy(mdiag,odiag,25*sizeof(PetscScalar));
104:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
105:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
106:       diag  += 25;
107:       mdiag += 25;
108:     }
109:     break;
110:   case 6:
111:     for (i=0; i<mbs; i++) {
112:       odiag  = v + 36*diag_offset[i];
113:       PetscMemcpy(diag,odiag,36*sizeof(PetscScalar));
114:       PetscMemcpy(mdiag,odiag,36*sizeof(PetscScalar));
115:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
116:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
117:       diag  += 36;
118:       mdiag += 36;
119:     }
120:     break;
121:   case 7:
122:     for (i=0; i<mbs; i++) {
123:       odiag  = v + 49*diag_offset[i];
124:       PetscMemcpy(diag,odiag,49*sizeof(PetscScalar));
125:       PetscMemcpy(mdiag,odiag,49*sizeof(PetscScalar));
126:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
127:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
128:       diag  += 49;
129:       mdiag += 49;
130:     }
131:     break;
132:   default:
133:     PetscMalloc2(bs,&v_work,bs,&v_pivots);
134:     for (i=0; i<mbs; i++) {
135:       odiag  = v + bs2*diag_offset[i];
136:       PetscMemcpy(diag,odiag,bs2*sizeof(PetscScalar));
137:       PetscMemcpy(mdiag,odiag,bs2*sizeof(PetscScalar));
138:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
139:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
140:       diag  += bs2;
141:       mdiag += bs2;
142:     }
143:     PetscFree2(v_work,v_pivots);
144:   }
145:   a->idiagvalid = PETSC_TRUE;
146:   return(0);
147: }

149: PetscErrorCode MatSOR_SeqBAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
150: {
151:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
152:   PetscScalar       *x,*work,*w,*workt,*t;
153:   const MatScalar   *v,*aa = a->a, *idiag;
154:   const PetscScalar *b,*xb;
155:   PetscScalar       s[7], xw[7]={0}; /* avoid some compilers thinking xw is uninitialized */
156:   PetscErrorCode    ierr;
157:   PetscInt          m = a->mbs,i,i2,nz,bs = A->rmap->bs,bs2 = bs*bs,k,j,idx,it;
158:   const PetscInt    *diag,*ai = a->i,*aj = a->j,*vi;

161:   if (fshift == -1.0) fshift = 0.0; /* negative fshift indicates do not error on zero diagonal; this code never errors on zero diagonal */
162:   its = its*lits;
163:   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
164:   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
165:   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
166:   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
167:   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");

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

171:   if (!m) return(0);
172:   diag  = a->diag;
173:   idiag = a->idiag;
174:   k    = PetscMax(A->rmap->n,A->cmap->n);
175:   if (!a->mult_work) {
176:     PetscMalloc1(k+1,&a->mult_work);
177:   }
178:   if (!a->sor_workt) {
179:     PetscMalloc1(k,&a->sor_workt);
180:   }
181:   if (!a->sor_work) {
182:     PetscMalloc1(bs,&a->sor_work);
183:   }
184:   work = a->mult_work;
185:   t    = a->sor_workt;
186:   w    = a->sor_work;

188:   VecGetArray(xx,&x);
189:   VecGetArrayRead(bb,&b);

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

370:           PetscMemcpy(w,b+i2,bs*sizeof(PetscScalar));
371:           /* copy all rows of x that are needed into contiguous space */
372:           workt = work;
373:           for (j=0; j<nz; j++) {
374:             PetscMemcpy(workt,x + bs*(*vi++),bs*sizeof(PetscScalar));
375:             workt += bs;
376:           }
377:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
378:           PetscMemcpy(t+i2,w,bs*sizeof(PetscScalar));
379:           PetscKernel_w_gets_Ar_times_v(bs,bs,w,idiag,x+i2);

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

569:           PetscMemcpy(w,xb+i2,bs*sizeof(PetscScalar));
570:           /* copy all rows of x that are needed into contiguous space */
571:           workt = work;
572:           for (j=0; j<nz; j++) {
573:             PetscMemcpy(workt,x + bs*(*vi++),bs*sizeof(PetscScalar));
574:             workt += bs;
575:           }
576:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
577:           PetscKernel_w_gets_Ar_times_v(bs,bs,w,idiag,x+i2);

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

728:           PetscMemcpy(w,b+i2,bs*sizeof(PetscScalar));
729:           /* copy all rows of x that are needed into contiguous space */
730:           workt = work;
731:           for (j=0; j<nz; j++) {
732:             PetscMemcpy(workt,x + bs*(*vi++),bs*sizeof(PetscScalar));
733:             workt += bs;
734:           }
735:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
736:           PetscKernel_w_gets_w_plus_Ar_times_v(bs,bs,w,idiag,x+i2);

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

884:           PetscMemcpy(w,b+i2,bs*sizeof(PetscScalar));
885:           /* copy all rows of x that are needed into contiguous space */
886:           workt = work;
887:           for (j=0; j<nz; j++) {
888:             PetscMemcpy(workt,x + bs*(*vi++),bs*sizeof(PetscScalar));
889:             workt += bs;
890:           }
891:           PetscKernel_w_gets_w_minus_Ar_times_v(bs,bs*nz,w,v,work);
892:           PetscKernel_w_gets_w_plus_Ar_times_v(bs,bs,w,idiag,x+i2);

894:           idiag -= bs2;
895:           i2    -= bs;
896:         }
897:         break;
898:       }
899:       PetscLogFlops(2.0*bs2*(a->nz));
900:     }
901:   }
902:   VecRestoreArray(xx,&x);
903:   VecRestoreArrayRead(bb,&b);
904:   return(0);
905: }


908: /*
909:     Special version for direct calls from Fortran (Used in PETSc-fun3d)
910: */
911: #if defined(PETSC_HAVE_FORTRAN_CAPS)
912: #define matsetvaluesblocked4_ MATSETVALUESBLOCKED4
913: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
914: #define matsetvaluesblocked4_ matsetvaluesblocked4
915: #endif

917: PETSC_EXTERN void matsetvaluesblocked4_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[])
918: {
919:   Mat               A  = *AA;
920:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
921:   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,N,m = *mm,n = *nn;
922:   PetscInt          *ai    =a->i,*ailen=a->ilen;
923:   PetscInt          *aj    =a->j,stepval,lastcol = -1;
924:   const PetscScalar *value = v;
925:   MatScalar         *ap,*aa = a->a,*bap;

928:   if (A->rmap->bs != 4) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Can only be called with a block size of 4");
929:   stepval = (n-1)*4;
930:   for (k=0; k<m; k++) { /* loop over added rows */
931:     row  = im[k];
932:     rp   = aj + ai[row];
933:     ap   = aa + 16*ai[row];
934:     nrow = ailen[row];
935:     low  = 0;
936:     high = nrow;
937:     for (l=0; l<n; l++) { /* loop over added columns */
938:       col = in[l];
939:       if (col <= lastcol)  low = 0;
940:       else                high = nrow;
941:       lastcol = col;
942:       value   = v + k*(stepval+4 + l)*4;
943:       while (high-low > 7) {
944:         t = (low+high)/2;
945:         if (rp[t] > col) high = t;
946:         else             low  = t;
947:       }
948:       for (i=low; i<high; i++) {
949:         if (rp[i] > col) break;
950:         if (rp[i] == col) {
951:           bap = ap +  16*i;
952:           for (ii=0; ii<4; ii++,value+=stepval) {
953:             for (jj=ii; jj<16; jj+=4) {
954:               bap[jj] += *value++;
955:             }
956:           }
957:           goto noinsert2;
958:         }
959:       }
960:       N = nrow++ - 1;
961:       high++; /* added new column index thus must search to one higher than before */
962:       /* shift up all the later entries in this row */
963:       for (ii=N; ii>=i; ii--) {
964:         rp[ii+1] = rp[ii];
965:         PetscMemcpy(ap+16*(ii+1),ap+16*(ii),16*sizeof(MatScalar));
966:       }
967:       if (N >= i) {
968:         PetscMemzero(ap+16*i,16*sizeof(MatScalar));
969:       }
970:       rp[i] = col;
971:       bap   = ap +  16*i;
972:       for (ii=0; ii<4; ii++,value+=stepval) {
973:         for (jj=ii; jj<16; jj+=4) {
974:           bap[jj] = *value++;
975:         }
976:       }
977:       noinsert2:;
978:       low = i;
979:     }
980:     ailen[row] = nrow;
981:   }
982:   PetscFunctionReturnVoid();
983: }

985: #if defined(PETSC_HAVE_FORTRAN_CAPS)
986: #define matsetvalues4_ MATSETVALUES4
987: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
988: #define matsetvalues4_ matsetvalues4
989: #endif

991: PETSC_EXTERN void matsetvalues4_(Mat *AA,PetscInt *mm,PetscInt *im,PetscInt *nn,PetscInt *in,PetscScalar *v)
992: {
993:   Mat         A  = *AA;
994:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
995:   PetscInt    *rp,k,low,high,t,ii,row,nrow,i,col,l,N,n = *nn,m = *mm;
996:   PetscInt    *ai=a->i,*ailen=a->ilen;
997:   PetscInt    *aj=a->j,brow,bcol;
998:   PetscInt    ridx,cidx,lastcol = -1;
999:   MatScalar   *ap,value,*aa=a->a,*bap;

1002:   for (k=0; k<m; k++) { /* loop over added rows */
1003:     row  = im[k]; brow = row/4;
1004:     rp   = aj + ai[brow];
1005:     ap   = aa + 16*ai[brow];
1006:     nrow = ailen[brow];
1007:     low  = 0;
1008:     high = nrow;
1009:     for (l=0; l<n; l++) { /* loop over added columns */
1010:       col   = in[l]; bcol = col/4;
1011:       ridx  = row % 4; cidx = col % 4;
1012:       value = v[l + k*n];
1013:       if (col <= lastcol)  low = 0;
1014:       else                high = nrow;
1015:       lastcol = col;
1016:       while (high-low > 7) {
1017:         t = (low+high)/2;
1018:         if (rp[t] > bcol) high = t;
1019:         else              low  = t;
1020:       }
1021:       for (i=low; i<high; i++) {
1022:         if (rp[i] > bcol) break;
1023:         if (rp[i] == bcol) {
1024:           bap   = ap +  16*i + 4*cidx + ridx;
1025:           *bap += value;
1026:           goto noinsert1;
1027:         }
1028:       }
1029:       N = nrow++ - 1;
1030:       high++; /* added new column thus must search to one higher than before */
1031:       /* shift up all the later entries in this row */
1032:       for (ii=N; ii>=i; ii--) {
1033:         rp[ii+1] = rp[ii];
1034:         PetscMemcpy(ap+16*(ii+1),ap+16*(ii),16*sizeof(MatScalar));
1035:       }
1036:       if (N>=i) {
1037:         PetscMemzero(ap+16*i,16*sizeof(MatScalar));
1038:       }
1039:       rp[i]                    = bcol;
1040:       ap[16*i + 4*cidx + ridx] = value;
1041: noinsert1:;
1042:       low = i;
1043:     }
1044:     ailen[brow] = nrow;
1045:   }
1046:   PetscFunctionReturnVoid();
1047: }

1049: /*
1050:      Checks for missing diagonals
1051: */
1052: PetscErrorCode MatMissingDiagonal_SeqBAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1053: {
1054:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1056:   PetscInt       *diag,*ii = a->i,i;

1059:   MatMarkDiagonal_SeqBAIJ(A);
1060:   *missing = PETSC_FALSE;
1061:   if (A->rmap->n > 0 && !ii) {
1062:     *missing = PETSC_TRUE;
1063:     if (d) *d = 0;
1064:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1065:   } else {
1066:     diag = a->diag;
1067:     for (i=0; i<a->mbs; i++) {
1068:       if (diag[i] >= ii[i+1]) {
1069:         *missing = PETSC_TRUE;
1070:         if (d) *d = i;
1071:         PetscInfo1(A,"Matrix is missing block diagonal number %D\n",i);
1072:         break;
1073:       }
1074:     }
1075:   }
1076:   return(0);
1077: }

1079: PetscErrorCode MatMarkDiagonal_SeqBAIJ(Mat A)
1080: {
1081:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1083:   PetscInt       i,j,m = a->mbs;

1086:   if (!a->diag) {
1087:     PetscMalloc1(m,&a->diag);
1088:     PetscLogObjectMemory((PetscObject)A,m*sizeof(PetscInt));
1089:     a->free_diag = PETSC_TRUE;
1090:   }
1091:   for (i=0; i<m; i++) {
1092:     a->diag[i] = a->i[i+1];
1093:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1094:       if (a->j[j] == i) {
1095:         a->diag[i] = j;
1096:         break;
1097:       }
1098:     }
1099:   }
1100:   return(0);
1101: }


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

1112:   *nn = n;
1113:   if (!ia) return(0);
1114:   if (symmetric) {
1115:     MatToSymmetricIJ_SeqAIJ(n,a->i,a->j,PETSC_TRUE,0,0,&tia,&tja);
1116:     nz   = tia[n];
1117:   } else {
1118:     tia = a->i; tja = a->j;
1119:   }

1121:   if (!blockcompressed && bs > 1) {
1122:     (*nn) *= bs;
1123:     /* malloc & create the natural set of indices */
1124:     PetscMalloc1((n+1)*bs,ia);
1125:     if (n) {
1126:       (*ia)[0] = oshift;
1127:       for (j=1; j<bs; j++) {
1128:         (*ia)[j] = (tia[1]-tia[0])*bs+(*ia)[j-1];
1129:       }
1130:     }

1132:     for (i=1; i<n; i++) {
1133:       (*ia)[i*bs] = (tia[i]-tia[i-1])*bs + (*ia)[i*bs-1];
1134:       for (j=1; j<bs; j++) {
1135:         (*ia)[i*bs+j] = (tia[i+1]-tia[i])*bs + (*ia)[i*bs+j-1];
1136:       }
1137:     }
1138:     if (n) {
1139:       (*ia)[n*bs] = (tia[n]-tia[n-1])*bs + (*ia)[n*bs-1];
1140:     }

1142:     if (inja) {
1143:       PetscMalloc1(nz*bs*bs,ja);
1144:       cnt = 0;
1145:       for (i=0; i<n; i++) {
1146:         for (j=0; j<bs; j++) {
1147:           for (k=tia[i]; k<tia[i+1]; k++) {
1148:             for (l=0; l<bs; l++) {
1149:               (*ja)[cnt++] = bs*tja[k] + l;
1150:             }
1151:           }
1152:         }
1153:       }
1154:     }

1156:     if (symmetric) { /* deallocate memory allocated in MatToSymmetricIJ_SeqAIJ() */
1157:       PetscFree(tia);
1158:       PetscFree(tja);
1159:     }
1160:   } else if (oshift == 1) {
1161:     if (symmetric) {
1162:       nz = tia[A->rmap->n/bs];
1163:       /*  add 1 to i and j indices */
1164:       for (i=0; i<A->rmap->n/bs+1; i++) tia[i] = tia[i] + 1;
1165:       *ia = tia;
1166:       if (ja) {
1167:         for (i=0; i<nz; i++) tja[i] = tja[i] + 1;
1168:         *ja = tja;
1169:       }
1170:     } else {
1171:       nz = a->i[A->rmap->n/bs];
1172:       /* malloc space and  add 1 to i and j indices */
1173:       PetscMalloc1(A->rmap->n/bs+1,ia);
1174:       for (i=0; i<A->rmap->n/bs+1; i++) (*ia)[i] = a->i[i] + 1;
1175:       if (ja) {
1176:         PetscMalloc1(nz,ja);
1177:         for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
1178:       }
1179:     }
1180:   } else {
1181:     *ia = tia;
1182:     if (ja) *ja = tja;
1183:   }
1184:   return(0);
1185: }

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

1192:   if (!ia) return(0);
1193:   if ((!blockcompressed && A->rmap->bs > 1) || (symmetric || oshift == 1)) {
1194:     PetscFree(*ia);
1195:     if (ja) {PetscFree(*ja);}
1196:   }
1197:   return(0);
1198: }

1200: PetscErrorCode MatDestroy_SeqBAIJ(Mat A)
1201: {
1202:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;

1206: #if defined(PETSC_USE_LOG)
1207:   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->N,A->cmap->n,a->nz);
1208: #endif
1209:   MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1210:   ISDestroy(&a->row);
1211:   ISDestroy(&a->col);
1212:   if (a->free_diag) {PetscFree(a->diag);}
1213:   PetscFree(a->idiag);
1214:   if (a->free_imax_ilen) {PetscFree2(a->imax,a->ilen);}
1215:   PetscFree(a->solve_work);
1216:   PetscFree(a->mult_work);
1217:   PetscFree(a->sor_workt);
1218:   PetscFree(a->sor_work);
1219:   ISDestroy(&a->icol);
1220:   PetscFree(a->saved_values);
1221:   PetscFree2(a->compressedrow.i,a->compressedrow.rindex);

1223:   MatDestroy(&a->sbaijMat);
1224:   MatDestroy(&a->parent);
1225:   PetscFree(A->data);

1227:   PetscObjectChangeTypeName((PetscObject)A,0);
1228:   PetscObjectComposeFunction((PetscObject)A,"MatInvertBlockDiagonal_C",NULL);
1229:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1230:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1231:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetColumnIndices_C",NULL);
1232:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqaij_C",NULL);
1233:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqsbaij_C",NULL);
1234:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocation_C",NULL);
1235:   PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocationCSR_C",NULL);
1236:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqbstrm_C",NULL);
1237:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1238: #if defined(PETSC_HAVE_HYPRE)
1239:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_mpiaij_hypre_C",NULL);
1240: #endif
1241:   return(0);
1242: }

1244: PetscErrorCode MatSetOption_SeqBAIJ(Mat A,MatOption op,PetscBool flg)
1245: {
1246:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;

1250:   switch (op) {
1251:   case MAT_ROW_ORIENTED:
1252:     a->roworiented = flg;
1253:     break;
1254:   case MAT_KEEP_NONZERO_PATTERN:
1255:     a->keepnonzeropattern = flg;
1256:     break;
1257:   case MAT_NEW_NONZERO_LOCATIONS:
1258:     a->nonew = (flg ? 0 : 1);
1259:     break;
1260:   case MAT_NEW_NONZERO_LOCATION_ERR:
1261:     a->nonew = (flg ? -1 : 0);
1262:     break;
1263:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1264:     a->nonew = (flg ? -2 : 0);
1265:     break;
1266:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1267:     a->nounused = (flg ? -1 : 0);
1268:     break;
1269:   case MAT_NEW_DIAGONALS:
1270:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1271:   case MAT_USE_HASH_TABLE:
1272:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1273:     break;
1274:   case MAT_SPD:
1275:   case MAT_SYMMETRIC:
1276:   case MAT_STRUCTURALLY_SYMMETRIC:
1277:   case MAT_HERMITIAN:
1278:   case MAT_SYMMETRY_ETERNAL:
1279:   case MAT_SUBMAT_SINGLEIS:
1280:     /* These options are handled directly by MatSetOption() */
1281:     break;
1282:   default:
1283:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1284:   }
1285:   return(0);
1286: }

1288: /* used for both SeqBAIJ and SeqSBAIJ matrices */
1289: PetscErrorCode MatGetRow_SeqBAIJ_private(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v,PetscInt *ai,PetscInt *aj,PetscScalar *aa)
1290: {
1292:   PetscInt       itmp,i,j,k,M,bn,bp,*idx_i,bs,bs2;
1293:   MatScalar      *aa_i;
1294:   PetscScalar    *v_i;

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

1301:   bn  = row/bs;   /* Block number */
1302:   bp  = row % bs; /* Block Position */
1303:   M   = ai[bn+1] - ai[bn];
1304:   *nz = bs*M;

1306:   if (v) {
1307:     *v = 0;
1308:     if (*nz) {
1309:       PetscMalloc1(*nz,v);
1310:       for (i=0; i<M; i++) { /* for each block in the block row */
1311:         v_i  = *v + i*bs;
1312:         aa_i = aa + bs2*(ai[bn] + i);
1313:         for (j=bp,k=0; j<bs2; j+=bs,k++) v_i[k] = aa_i[j];
1314:       }
1315:     }
1316:   }

1318:   if (idx) {
1319:     *idx = 0;
1320:     if (*nz) {
1321:       PetscMalloc1(*nz,idx);
1322:       for (i=0; i<M; i++) { /* for each block in the block row */
1323:         idx_i = *idx + i*bs;
1324:         itmp  = bs*aj[ai[bn] + i];
1325:         for (j=0; j<bs; j++) idx_i[j] = itmp++;
1326:       }
1327:     }
1328:   }
1329:   return(0);
1330: }

1332: PetscErrorCode MatGetRow_SeqBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1333: {
1334:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;

1338:   MatGetRow_SeqBAIJ_private(A,row,nz,idx,v,a->i,a->j,a->a);
1339:   return(0);
1340: }

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

1347:   if (idx) {PetscFree(*idx);}
1348:   if (v)   {PetscFree(*v);}
1349:   return(0);
1350: }

1352: extern PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);

1354: PetscErrorCode MatTranspose_SeqBAIJ(Mat A,MatReuse reuse,Mat *B)
1355: {
1356:   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)A->data;
1357:   Mat            C;
1359:   PetscInt       i,j,k,*aj=a->j,*ai=a->i,bs=A->rmap->bs,mbs=a->mbs,nbs=a->nbs,len,*col;
1360:   PetscInt       *rows,*cols,bs2=a->bs2;
1361:   MatScalar      *array;

1364:   if (reuse == MAT_INPLACE_MATRIX && mbs != nbs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1365:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
1366:     PetscCalloc1(1+nbs,&col);

1368:     for (i=0; i<ai[mbs]; i++) col[aj[i]] += 1;
1369:     MatCreate(PetscObjectComm((PetscObject)A),&C);
1370:     MatSetSizes(C,A->cmap->n,A->rmap->N,A->cmap->n,A->rmap->N);
1371:     MatSetType(C,((PetscObject)A)->type_name);
1372:     MatSeqBAIJSetPreallocation(C,bs,0,col);
1373:     PetscFree(col);
1374:   } else {
1375:     C = *B;
1376:   }

1378:   array = a->a;
1379:   PetscMalloc2(bs,&rows,bs,&cols);
1380:   for (i=0; i<mbs; i++) {
1381:     cols[0] = i*bs;
1382:     for (k=1; k<bs; k++) cols[k] = cols[k-1] + 1;
1383:     len = ai[i+1] - ai[i];
1384:     for (j=0; j<len; j++) {
1385:       rows[0] = (*aj++)*bs;
1386:       for (k=1; k<bs; k++) rows[k] = rows[k-1] + 1;
1387:       MatSetValues_SeqBAIJ(C,bs,rows,bs,cols,array,INSERT_VALUES);
1388:       array += bs2;
1389:     }
1390:   }
1391:   PetscFree2(rows,cols);

1393:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1394:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

1396:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
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: static PetscErrorCode MatView_SeqBAIJ_Binary(Mat A,PetscViewer viewer)
1418: {
1419:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1421:   PetscInt       i,*col_lens,bs = A->rmap->bs,count,*jj,j,k,l,bs2=a->bs2;
1422:   int            fd;
1423:   PetscScalar    *aa;
1424:   FILE           *file;

1427:   PetscViewerBinaryGetDescriptor(viewer,&fd);
1428:   PetscMalloc1(4+A->rmap->N,&col_lens);
1429:   col_lens[0] = MAT_FILE_CLASSID;

1431:   col_lens[1] = A->rmap->N;
1432:   col_lens[2] = A->cmap->n;
1433:   col_lens[3] = a->nz*bs2;

1435:   /* store lengths of each row and write (including header) to file */
1436:   count = 0;
1437:   for (i=0; i<a->mbs; i++) {
1438:     for (j=0; j<bs; j++) {
1439:       col_lens[4+count++] = bs*(a->i[i+1] - a->i[i]);
1440:     }
1441:   }
1442:   PetscBinaryWrite(fd,col_lens,4+A->rmap->N,PETSC_INT,PETSC_TRUE);
1443:   PetscFree(col_lens);

1445:   /* store column indices (zero start index) */
1446:   PetscMalloc1((a->nz+1)*bs2,&jj);
1447:   count = 0;
1448:   for (i=0; i<a->mbs; i++) {
1449:     for (j=0; j<bs; j++) {
1450:       for (k=a->i[i]; k<a->i[i+1]; k++) {
1451:         for (l=0; l<bs; l++) {
1452:           jj[count++] = bs*a->j[k] + l;
1453:         }
1454:       }
1455:     }
1456:   }
1457:   PetscBinaryWrite(fd,jj,bs2*a->nz,PETSC_INT,PETSC_FALSE);
1458:   PetscFree(jj);

1460:   /* store nonzero values */
1461:   PetscMalloc1((a->nz+1)*bs2,&aa);
1462:   count = 0;
1463:   for (i=0; i<a->mbs; i++) {
1464:     for (j=0; j<bs; j++) {
1465:       for (k=a->i[i]; k<a->i[i+1]; k++) {
1466:         for (l=0; l<bs; l++) {
1467:           aa[count++] = a->a[bs2*k + l*bs + j];
1468:         }
1469:       }
1470:     }
1471:   }
1472:   PetscBinaryWrite(fd,aa,bs2*a->nz,PETSC_SCALAR,PETSC_FALSE);
1473:   PetscFree(aa);

1475:   PetscViewerBinaryGetInfoPointer(viewer,&file);
1476:   if (file) {
1477:     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
1478:   }
1479:   return(0);
1480: }

1482: static PetscErrorCode MatView_SeqBAIJ_ASCII(Mat A,PetscViewer viewer)
1483: {
1484:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1485:   PetscErrorCode    ierr;
1486:   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
1487:   PetscViewerFormat format;

1490:   PetscViewerGetFormat(viewer,&format);
1491:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1492:     PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
1493:   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
1494:     const char *matname;
1495:     Mat        aij;
1496:     MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);
1497:     PetscObjectGetName((PetscObject)A,&matname);
1498:     PetscObjectSetName((PetscObject)aij,matname);
1499:     MatView(aij,viewer);
1500:     MatDestroy(&aij);
1501:   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1502:       return(0);
1503:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
1504:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1505:     for (i=0; i<a->mbs; i++) {
1506:       for (j=0; j<bs; j++) {
1507:         PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);
1508:         for (k=a->i[i]; k<a->i[i+1]; k++) {
1509:           for (l=0; l<bs; l++) {
1510: #if defined(PETSC_USE_COMPLEX)
1511:             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1512:               PetscViewerASCIIPrintf(viewer," (%D, %g + %gi) ",bs*a->j[k]+l,
1513:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1514:             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1515:               PetscViewerASCIIPrintf(viewer," (%D, %g - %gi) ",bs*a->j[k]+l,
1516:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1517:             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1518:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));
1519:             }
1520: #else
1521:             if (a->a[bs2*k + l*bs + j] != 0.0) {
1522:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);
1523:             }
1524: #endif
1525:           }
1526:         }
1527:         PetscViewerASCIIPrintf(viewer,"\n");
1528:       }
1529:     }
1530:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1531:   } else {
1532:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1533:     for (i=0; i<a->mbs; i++) {
1534:       for (j=0; j<bs; j++) {
1535:         PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);
1536:         for (k=a->i[i]; k<a->i[i+1]; k++) {
1537:           for (l=0; l<bs; l++) {
1538: #if defined(PETSC_USE_COMPLEX)
1539:             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
1540:               PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
1541:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1542:             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
1543:               PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
1544:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
1545:             } else {
1546:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));
1547:             }
1548: #else
1549:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);
1550: #endif
1551:           }
1552:         }
1553:         PetscViewerASCIIPrintf(viewer,"\n");
1554:       }
1555:     }
1556:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1557:   }
1558:   PetscViewerFlush(viewer);
1559:   return(0);
1560: }

1562:  #include <petscdraw.h>
1563: static PetscErrorCode MatView_SeqBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
1564: {
1565:   Mat               A = (Mat) Aa;
1566:   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ*)A->data;
1567:   PetscErrorCode    ierr;
1568:   PetscInt          row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
1569:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
1570:   MatScalar         *aa;
1571:   PetscViewer       viewer;
1572:   PetscViewerFormat format;

1575:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
1576:   PetscViewerGetFormat(viewer,&format);
1577:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

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

1581:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
1582:     PetscDrawCollectiveBegin(draw);
1583:     /* Blue for negative, Cyan for zero and  Red for positive */
1584:     color = PETSC_DRAW_BLUE;
1585:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1586:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1587:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1588:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1589:         aa  = a->a + j*bs2;
1590:         for (k=0; k<bs; k++) {
1591:           for (l=0; l<bs; l++) {
1592:             if (PetscRealPart(*aa++) >=  0.) continue;
1593:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1594:           }
1595:         }
1596:       }
1597:     }
1598:     color = PETSC_DRAW_CYAN;
1599:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1600:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1601:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1602:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1603:         aa  = a->a + j*bs2;
1604:         for (k=0; k<bs; k++) {
1605:           for (l=0; l<bs; l++) {
1606:             if (PetscRealPart(*aa++) != 0.) continue;
1607:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1608:           }
1609:         }
1610:       }
1611:     }
1612:     color = PETSC_DRAW_RED;
1613:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1614:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1615:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1616:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1617:         aa  = a->a + j*bs2;
1618:         for (k=0; k<bs; k++) {
1619:           for (l=0; l<bs; l++) {
1620:             if (PetscRealPart(*aa++) <= 0.) continue;
1621:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1622:           }
1623:         }
1624:       }
1625:     }
1626:     PetscDrawCollectiveEnd(draw);
1627:   } else {
1628:     /* use contour shading to indicate magnitude of values */
1629:     /* first determine max of all nonzero values */
1630:     PetscReal minv = 0.0, maxv = 0.0;
1631:     PetscDraw popup;

1633:     for (i=0; i<a->nz*a->bs2; i++) {
1634:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
1635:     }
1636:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
1637:     PetscDrawGetPopup(draw,&popup);
1638:     PetscDrawScalePopup(popup,0.0,maxv);

1640:     PetscDrawCollectiveBegin(draw);
1641:     for (i=0,row=0; i<mbs; i++,row+=bs) {
1642:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1643:         y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
1644:         x_l = a->j[j]*bs; x_r = x_l + 1.0;
1645:         aa  = a->a + j*bs2;
1646:         for (k=0; k<bs; k++) {
1647:           for (l=0; l<bs; l++) {
1648:             MatScalar v = *aa++;
1649:             color = PetscDrawRealToColor(PetscAbsScalar(v),minv,maxv);
1650:             PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
1651:           }
1652:         }
1653:       }
1654:     }
1655:     PetscDrawCollectiveEnd(draw);
1656:   }
1657:   return(0);
1658: }

1660: static PetscErrorCode MatView_SeqBAIJ_Draw(Mat A,PetscViewer viewer)
1661: {
1663:   PetscReal      xl,yl,xr,yr,w,h;
1664:   PetscDraw      draw;
1665:   PetscBool      isnull;

1668:   PetscViewerDrawGetDraw(viewer,0,&draw);
1669:   PetscDrawIsNull(draw,&isnull);
1670:   if (isnull) return(0);

1672:   xr   = A->cmap->n; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
1673:   xr  += w;          yr += h;        xl = -w;     yl = -h;
1674:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
1675:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
1676:   PetscDrawZoom(draw,MatView_SeqBAIJ_Draw_Zoom,A);
1677:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
1678:   PetscDrawSave(draw);
1679:   return(0);
1680: }

1682: PetscErrorCode MatView_SeqBAIJ(Mat A,PetscViewer viewer)
1683: {
1685:   PetscBool      iascii,isbinary,isdraw;

1688:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1689:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1690:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1691:   if (iascii) {
1692:     MatView_SeqBAIJ_ASCII(A,viewer);
1693:   } else if (isbinary) {
1694:     MatView_SeqBAIJ_Binary(A,viewer);
1695:   } else if (isdraw) {
1696:     MatView_SeqBAIJ_Draw(A,viewer);
1697:   } else {
1698:     Mat B;
1699:     MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);
1700:     MatView(B,viewer);
1701:     MatDestroy(&B);
1702:   }
1703:   return(0);
1704: }


1707: PetscErrorCode MatGetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
1708: {
1709:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
1710:   PetscInt    *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
1711:   PetscInt    *ai = a->i,*ailen = a->ilen;
1712:   PetscInt    brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
1713:   MatScalar   *ap,*aa = a->a;

1716:   for (k=0; k<m; k++) { /* loop over rows */
1717:     row = im[k]; brow = row/bs;
1718:     if (row < 0) {v += n; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row"); */
1719:     if (row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D too large", row);
1720:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow];
1721:     nrow = ailen[brow];
1722:     for (l=0; l<n; l++) { /* loop over columns */
1723:       if (in[l] < 0) {v++; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column"); */
1724:       if (in[l] >= A->cmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column %D too large", in[l]);
1725:       col  = in[l];
1726:       bcol = col/bs;
1727:       cidx = col%bs;
1728:       ridx = row%bs;
1729:       high = nrow;
1730:       low  = 0; /* assume unsorted */
1731:       while (high-low > 5) {
1732:         t = (low+high)/2;
1733:         if (rp[t] > bcol) high = t;
1734:         else             low  = t;
1735:       }
1736:       for (i=low; i<high; i++) {
1737:         if (rp[i] > bcol) break;
1738:         if (rp[i] == bcol) {
1739:           *v++ = ap[bs2*i+bs*cidx+ridx];
1740:           goto finished;
1741:         }
1742:       }
1743:       *v++ = 0.0;
1744: finished:;
1745:     }
1746:   }
1747:   return(0);
1748: }

1750: PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
1751: {
1752:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1753:   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
1754:   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
1755:   PetscErrorCode    ierr;
1756:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
1757:   PetscBool         roworiented=a->roworiented;
1758:   const PetscScalar *value     = v;
1759:   MatScalar         *ap,*aa = a->a,*bap;

1762:   if (roworiented) {
1763:     stepval = (n-1)*bs;
1764:   } else {
1765:     stepval = (m-1)*bs;
1766:   }
1767:   for (k=0; k<m; k++) { /* loop over added rows */
1768:     row = im[k];
1769:     if (row < 0) continue;
1770: #if defined(PETSC_USE_DEBUG)
1771:     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block row index too large %D max %D",row,a->mbs-1);
1772: #endif
1773:     rp   = aj + ai[row];
1774:     ap   = aa + bs2*ai[row];
1775:     rmax = imax[row];
1776:     nrow = ailen[row];
1777:     low  = 0;
1778:     high = nrow;
1779:     for (l=0; l<n; l++) { /* loop over added columns */
1780:       if (in[l] < 0) continue;
1781: #if defined(PETSC_USE_DEBUG)
1782:       if (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);
1783: #endif
1784:       col = in[l];
1785:       if (roworiented) {
1786:         value = v + (k*(stepval+bs) + l)*bs;
1787:       } else {
1788:         value = v + (l*(stepval+bs) + k)*bs;
1789:       }
1790:       if (col <= lastcol) low = 0;
1791:       else high = nrow;
1792:       lastcol = col;
1793:       while (high-low > 7) {
1794:         t = (low+high)/2;
1795:         if (rp[t] > col) high = t;
1796:         else             low  = t;
1797:       }
1798:       for (i=low; i<high; i++) {
1799:         if (rp[i] > col) break;
1800:         if (rp[i] == col) {
1801:           bap = ap +  bs2*i;
1802:           if (roworiented) {
1803:             if (is == ADD_VALUES) {
1804:               for (ii=0; ii<bs; ii++,value+=stepval) {
1805:                 for (jj=ii; jj<bs2; jj+=bs) {
1806:                   bap[jj] += *value++;
1807:                 }
1808:               }
1809:             } else {
1810:               for (ii=0; ii<bs; ii++,value+=stepval) {
1811:                 for (jj=ii; jj<bs2; jj+=bs) {
1812:                   bap[jj] = *value++;
1813:                 }
1814:               }
1815:             }
1816:           } else {
1817:             if (is == ADD_VALUES) {
1818:               for (ii=0; ii<bs; ii++,value+=bs+stepval) {
1819:                 for (jj=0; jj<bs; jj++) {
1820:                   bap[jj] += value[jj];
1821:                 }
1822:                 bap += bs;
1823:               }
1824:             } else {
1825:               for (ii=0; ii<bs; ii++,value+=bs+stepval) {
1826:                 for (jj=0; jj<bs; jj++) {
1827:                   bap[jj]  = value[jj];
1828:                 }
1829:                 bap += bs;
1830:               }
1831:             }
1832:           }
1833:           goto noinsert2;
1834:         }
1835:       }
1836:       if (nonew == 1) goto noinsert2;
1837:       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);
1838:       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
1839:       N = nrow++ - 1; high++;
1840:       /* shift up all the later entries in this row */
1841:       for (ii=N; ii>=i; ii--) {
1842:         rp[ii+1] = rp[ii];
1843:         PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
1844:       }
1845:       if (N >= i) {
1846:         PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
1847:       }
1848:       rp[i] = col;
1849:       bap   = ap +  bs2*i;
1850:       if (roworiented) {
1851:         for (ii=0; ii<bs; ii++,value+=stepval) {
1852:           for (jj=ii; jj<bs2; jj+=bs) {
1853:             bap[jj] = *value++;
1854:           }
1855:         }
1856:       } else {
1857:         for (ii=0; ii<bs; ii++,value+=stepval) {
1858:           for (jj=0; jj<bs; jj++) {
1859:             *bap++ = *value++;
1860:           }
1861:         }
1862:       }
1863: noinsert2:;
1864:       low = i;
1865:     }
1866:     ailen[row] = nrow;
1867:   }
1868:   return(0);
1869: }

1871: PetscErrorCode MatAssemblyEnd_SeqBAIJ(Mat A,MatAssemblyType mode)
1872: {
1873:   Mat_SeqBAIJ    *a     = (Mat_SeqBAIJ*)A->data;
1874:   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
1875:   PetscInt       m      = A->rmap->N,*ip,N,*ailen = a->ilen;
1877:   PetscInt       mbs  = a->mbs,bs2 = a->bs2,rmax = 0;
1878:   MatScalar      *aa  = a->a,*ap;
1879:   PetscReal      ratio=0.6;

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

1884:   if (m) rmax = ailen[0];
1885:   for (i=1; i<mbs; i++) {
1886:     /* move each row back by the amount of empty slots (fshift) before it*/
1887:     fshift += imax[i-1] - ailen[i-1];
1888:     rmax    = PetscMax(rmax,ailen[i]);
1889:     if (fshift) {
1890:       ip = aj + ai[i]; ap = aa + bs2*ai[i];
1891:       N  = ailen[i];
1892:       for (j=0; j<N; j++) {
1893:         ip[j-fshift] = ip[j];

1895:         PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));
1896:       }
1897:     }
1898:     ai[i] = ai[i-1] + ailen[i-1];
1899:   }
1900:   if (mbs) {
1901:     fshift += imax[mbs-1] - ailen[mbs-1];
1902:     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
1903:   }

1905:   /* reset ilen and imax for each row */
1906:   a->nonzerorowcnt = 0;
1907:   for (i=0; i<mbs; i++) {
1908:     ailen[i] = imax[i] = ai[i+1] - ai[i];
1909:     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1910:   }
1911:   a->nz = ai[mbs];

1913:   /* diagonals may have moved, so kill the diagonal pointers */
1914:   a->idiagvalid = PETSC_FALSE;
1915:   if (fshift && a->diag) {
1916:     PetscFree(a->diag);
1917:     PetscLogObjectMemory((PetscObject)A,-(mbs+1)*sizeof(PetscInt));
1918:     a->diag = 0;
1919:   }
1920:   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);
1921:   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);
1922:   PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);
1923:   PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);

1925:   A->info.mallocs    += a->reallocs;
1926:   a->reallocs         = 0;
1927:   A->info.nz_unneeded = (PetscReal)fshift*bs2;
1928:   a->rmax             = rmax;

1930:   MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,mbs,ratio);
1931:   return(0);
1932: }

1934: /*
1935:    This function returns an array of flags which indicate the locations of contiguous
1936:    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
1937:    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
1938:    Assume: sizes should be long enough to hold all the values.
1939: */
1940: static PetscErrorCode MatZeroRows_SeqBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
1941: {
1942:   PetscInt  i,j,k,row;
1943:   PetscBool flg;

1946:   for (i=0,j=0; i<n; j++) {
1947:     row = idx[i];
1948:     if (row%bs!=0) { /* Not the begining of a block */
1949:       sizes[j] = 1;
1950:       i++;
1951:     } else if (i+bs > n) { /* complete block doesn't exist (at idx end) */
1952:       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
1953:       i++;
1954:     } else { /* Begining of the block, so check if the complete block exists */
1955:       flg = PETSC_TRUE;
1956:       for (k=1; k<bs; k++) {
1957:         if (row+k != idx[i+k]) { /* break in the block */
1958:           flg = PETSC_FALSE;
1959:           break;
1960:         }
1961:       }
1962:       if (flg) { /* No break in the bs */
1963:         sizes[j] = bs;
1964:         i       += bs;
1965:       } else {
1966:         sizes[j] = 1;
1967:         i++;
1968:       }
1969:     }
1970:   }
1971:   *bs_max = j;
1972:   return(0);
1973: }

1975: PetscErrorCode MatZeroRows_SeqBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
1976: {
1977:   Mat_SeqBAIJ       *baij=(Mat_SeqBAIJ*)A->data;
1978:   PetscErrorCode    ierr;
1979:   PetscInt          i,j,k,count,*rows;
1980:   PetscInt          bs=A->rmap->bs,bs2=baij->bs2,*sizes,row,bs_max;
1981:   PetscScalar       zero = 0.0;
1982:   MatScalar         *aa;
1983:   const PetscScalar *xx;
1984:   PetscScalar       *bb;

1987:   /* fix right hand side if needed */
1988:   if (x && b) {
1989:     VecGetArrayRead(x,&xx);
1990:     VecGetArray(b,&bb);
1991:     for (i=0; i<is_n; i++) {
1992:       bb[is_idx[i]] = diag*xx[is_idx[i]];
1993:     }
1994:     VecRestoreArrayRead(x,&xx);
1995:     VecRestoreArray(b,&bb);
1996:   }

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

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

2006:   if (baij->keepnonzeropattern) {
2007:     for (i=0; i<is_n; i++) sizes[i] = 1;
2008:     bs_max          = is_n;
2009:   } else {
2010:     MatZeroRows_SeqBAIJ_Check_Blocks(rows,is_n,bs,sizes,&bs_max);
2011:     A->nonzerostate++;
2012:   }

2014:   for (i=0,j=0; i<bs_max; j+=sizes[i],i++) {
2015:     row = rows[j];
2016:     if (row < 0 || row > A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",row);
2017:     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
2018:     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
2019:     if (sizes[i] == bs && !baij->keepnonzeropattern) {
2020:       if (diag != (PetscScalar)0.0) {
2021:         if (baij->ilen[row/bs] > 0) {
2022:           baij->ilen[row/bs]       = 1;
2023:           baij->j[baij->i[row/bs]] = row/bs;

2025:           PetscMemzero(aa,count*bs*sizeof(MatScalar));
2026:         }
2027:         /* Now insert all the diagonal values for this bs */
2028:         for (k=0; k<bs; k++) {
2029:           (*A->ops->setvalues)(A,1,rows+j+k,1,rows+j+k,&diag,INSERT_VALUES);
2030:         }
2031:       } else { /* (diag == 0.0) */
2032:         baij->ilen[row/bs] = 0;
2033:       } /* end (diag == 0.0) */
2034:     } else { /* (sizes[i] != bs) */
2035: #if defined(PETSC_USE_DEBUG)
2036:       if (sizes[i] != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal Error. Value should be 1");
2037: #endif
2038:       for (k=0; k<count; k++) {
2039:         aa[0] =  zero;
2040:         aa   += bs;
2041:       }
2042:       if (diag != (PetscScalar)0.0) {
2043:         (*A->ops->setvalues)(A,1,rows+j,1,rows+j,&diag,INSERT_VALUES);
2044:       }
2045:     }
2046:   }

2048:   PetscFree2(rows,sizes);
2049:   MatAssemblyEnd_SeqBAIJ(A,MAT_FINAL_ASSEMBLY);
2050:   return(0);
2051: }

2053: PetscErrorCode MatZeroRowsColumns_SeqBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
2054: {
2055:   Mat_SeqBAIJ       *baij=(Mat_SeqBAIJ*)A->data;
2056:   PetscErrorCode    ierr;
2057:   PetscInt          i,j,k,count;
2058:   PetscInt          bs   =A->rmap->bs,bs2=baij->bs2,row,col;
2059:   PetscScalar       zero = 0.0;
2060:   MatScalar         *aa;
2061:   const PetscScalar *xx;
2062:   PetscScalar       *bb;
2063:   PetscBool         *zeroed,vecs = PETSC_FALSE;

2066:   /* fix right hand side if needed */
2067:   if (x && b) {
2068:     VecGetArrayRead(x,&xx);
2069:     VecGetArray(b,&bb);
2070:     vecs = PETSC_TRUE;
2071:   }

2073:   /* zero the columns */
2074:   PetscCalloc1(A->rmap->n,&zeroed);
2075:   for (i=0; i<is_n; i++) {
2076:     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]);
2077:     zeroed[is_idx[i]] = PETSC_TRUE;
2078:   }
2079:   for (i=0; i<A->rmap->N; i++) {
2080:     if (!zeroed[i]) {
2081:       row = i/bs;
2082:       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
2083:         for (k=0; k<bs; k++) {
2084:           col = bs*baij->j[j] + k;
2085:           if (zeroed[col]) {
2086:             aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
2087:             if (vecs) bb[i] -= aa[0]*xx[col];
2088:             aa[0] = 0.0;
2089:           }
2090:         }
2091:       }
2092:     } else if (vecs) bb[i] = diag*xx[i];
2093:   }
2094:   PetscFree(zeroed);
2095:   if (vecs) {
2096:     VecRestoreArrayRead(x,&xx);
2097:     VecRestoreArray(b,&bb);
2098:   }

2100:   /* zero the rows */
2101:   for (i=0; i<is_n; i++) {
2102:     row   = is_idx[i];
2103:     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
2104:     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
2105:     for (k=0; k<count; k++) {
2106:       aa[0] =  zero;
2107:       aa   += bs;
2108:     }
2109:     if (diag != (PetscScalar)0.0) {
2110:       (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);
2111:     }
2112:   }
2113:   MatAssemblyEnd_SeqBAIJ(A,MAT_FINAL_ASSEMBLY);
2114:   return(0);
2115: }

2117: PetscErrorCode MatSetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
2118: {
2119:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2120:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
2121:   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen;
2122:   PetscInt       *aj  =a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
2124:   PetscInt       ridx,cidx,bs2=a->bs2;
2125:   PetscBool      roworiented=a->roworiented;
2126:   MatScalar      *ap,value,*aa=a->a,*bap;

2129:   for (k=0; k<m; k++) { /* loop over added rows */
2130:     row  = im[k];
2131:     brow = row/bs;
2132:     if (row < 0) continue;
2133: #if defined(PETSC_USE_DEBUG)
2134:     if (row >= A->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->N-1);
2135: #endif
2136:     rp   = aj + ai[brow];
2137:     ap   = aa + bs2*ai[brow];
2138:     rmax = imax[brow];
2139:     nrow = ailen[brow];
2140:     low  = 0;
2141:     high = nrow;
2142:     for (l=0; l<n; l++) { /* loop over added columns */
2143:       if (in[l] < 0) continue;
2144: #if defined(PETSC_USE_DEBUG)
2145:       if (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);
2146: #endif
2147:       col  = in[l]; bcol = col/bs;
2148:       ridx = row % bs; cidx = col % bs;
2149:       if (roworiented) {
2150:         value = v[l + k*n];
2151:       } else {
2152:         value = v[k + l*m];
2153:       }
2154:       if (col <= lastcol) low = 0; else high = nrow;
2155:       lastcol = col;
2156:       while (high-low > 7) {
2157:         t = (low+high)/2;
2158:         if (rp[t] > bcol) high = t;
2159:         else              low  = t;
2160:       }
2161:       for (i=low; i<high; i++) {
2162:         if (rp[i] > bcol) break;
2163:         if (rp[i] == bcol) {
2164:           bap = ap +  bs2*i + bs*cidx + ridx;
2165:           if (is == ADD_VALUES) *bap += value;
2166:           else                  *bap  = value;
2167:           goto noinsert1;
2168:         }
2169:       }
2170:       if (nonew == 1) goto noinsert1;
2171:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
2172:       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
2173:       N = nrow++ - 1; high++;
2174:       /* shift up all the later entries in this row */
2175:       for (ii=N; ii>=i; ii--) {
2176:         rp[ii+1] = rp[ii];
2177:         PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
2178:       }
2179:       if (N>=i) {
2180:         PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
2181:       }
2182:       rp[i]                      = bcol;
2183:       ap[bs2*i + bs*cidx + ridx] = value;
2184:       a->nz++;
2185:       A->nonzerostate++;
2186: noinsert1:;
2187:       low = i;
2188:     }
2189:     ailen[brow] = nrow;
2190:   }
2191:   return(0);
2192: }

2194: PetscErrorCode MatILUFactor_SeqBAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2195: {
2196:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)inA->data;
2197:   Mat            outA;
2199:   PetscBool      row_identity,col_identity;

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

2207:   outA            = inA;
2208:   inA->factortype = MAT_FACTOR_LU;
2209:   PetscFree(inA->solvertype);
2210:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

2212:   MatMarkDiagonal_SeqBAIJ(inA);

2214:   PetscObjectReference((PetscObject)row);
2215:   ISDestroy(&a->row);
2216:   a->row = row;
2217:   PetscObjectReference((PetscObject)col);
2218:   ISDestroy(&a->col);
2219:   a->col = col;

2221:   /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */
2222:   ISDestroy(&a->icol);
2223:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2224:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

2226:   MatSeqBAIJSetNumericFactorization_inplace(inA,(PetscBool)(row_identity && col_identity));
2227:   if (!a->solve_work) {
2228:     PetscMalloc1(inA->rmap->N+inA->rmap->bs,&a->solve_work);
2229:     PetscLogObjectMemory((PetscObject)inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));
2230:   }
2231:   MatLUFactorNumeric(outA,inA,info);
2232:   return(0);
2233: }

2235: PetscErrorCode  MatSeqBAIJSetColumnIndices_SeqBAIJ(Mat mat,PetscInt *indices)
2236: {
2237:   Mat_SeqBAIJ *baij = (Mat_SeqBAIJ*)mat->data;
2238:   PetscInt    i,nz,mbs;

2241:   nz  = baij->maxnz;
2242:   mbs = baij->mbs;
2243:   for (i=0; i<nz; i++) {
2244:     baij->j[i] = indices[i];
2245:   }
2246:   baij->nz = nz;
2247:   for (i=0; i<mbs; i++) {
2248:     baij->ilen[i] = baij->imax[i];
2249:   }
2250:   return(0);
2251: }

2253: /*@
2254:     MatSeqBAIJSetColumnIndices - Set the column indices for all the rows
2255:        in the matrix.

2257:   Input Parameters:
2258: +  mat - the SeqBAIJ matrix
2259: -  indices - the column indices

2261:   Level: advanced

2263:   Notes:
2264:     This can be called if you have precomputed the nonzero structure of the
2265:   matrix and want to provide it to the matrix object to improve the performance
2266:   of the MatSetValues() operation.

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

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

2273: @*/
2274: PetscErrorCode  MatSeqBAIJSetColumnIndices(Mat mat,PetscInt *indices)
2275: {

2281:   PetscUseMethod(mat,"MatSeqBAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
2282:   return(0);
2283: }

2285: PetscErrorCode MatGetRowMaxAbs_SeqBAIJ(Mat A,Vec v,PetscInt idx[])
2286: {
2287:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2289:   PetscInt       i,j,n,row,bs,*ai,*aj,mbs;
2290:   PetscReal      atmp;
2291:   PetscScalar    *x,zero = 0.0;
2292:   MatScalar      *aa;
2293:   PetscInt       ncols,brow,krow,kcol;

2296:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2297:   bs  = A->rmap->bs;
2298:   aa  = a->a;
2299:   ai  = a->i;
2300:   aj  = a->j;
2301:   mbs = a->mbs;

2303:   VecSet(v,zero);
2304:   VecGetArray(v,&x);
2305:   VecGetLocalSize(v,&n);
2306:   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2307:   for (i=0; i<mbs; i++) {
2308:     ncols = ai[1] - ai[0]; ai++;
2309:     brow  = bs*i;
2310:     for (j=0; j<ncols; j++) {
2311:       for (kcol=0; kcol<bs; kcol++) {
2312:         for (krow=0; krow<bs; krow++) {
2313:           atmp = PetscAbsScalar(*aa);aa++;
2314:           row  = brow + krow;   /* row index */
2315:           if (PetscAbsScalar(x[row]) < atmp) {x[row] = atmp; if (idx) idx[row] = bs*(*aj) + kcol;}
2316:         }
2317:       }
2318:       aj++;
2319:     }
2320:   }
2321:   VecRestoreArray(v,&x);
2322:   return(0);
2323: }

2325: PetscErrorCode MatCopy_SeqBAIJ(Mat A,Mat B,MatStructure str)
2326: {

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

2336:     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]);
2337:     if (abs != bbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Block size A %D and B %D are different",abs,bbs);
2338:     PetscMemcpy(b->a,a->a,(bs2*a->i[ambs])*sizeof(PetscScalar));
2339:   } else {
2340:     MatCopy_Basic(A,B,str);
2341:   }
2342:   return(0);
2343: }

2345: PetscErrorCode MatSetUp_SeqBAIJ(Mat A)
2346: {

2350:   MatSeqBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0);
2351:   return(0);
2352: }

2354: PetscErrorCode MatSeqBAIJGetArray_SeqBAIJ(Mat A,PetscScalar *array[])
2355: {
2356:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;

2359:   *array = a->a;
2360:   return(0);
2361: }

2363: PetscErrorCode MatSeqBAIJRestoreArray_SeqBAIJ(Mat A,PetscScalar *array[])
2364: {
2366:   return(0);
2367: }

2369: PetscErrorCode MatAXPYGetPreallocation_SeqBAIJ(Mat Y,Mat X,PetscInt *nnz)
2370: {
2371:   PetscInt       bs = Y->rmap->bs,mbs = Y->rmap->N/bs;
2372:   Mat_SeqBAIJ    *x = (Mat_SeqBAIJ*)X->data;
2373:   Mat_SeqBAIJ    *y = (Mat_SeqBAIJ*)Y->data;

2377:   /* Set the number of nonzeros in the new matrix */
2378:   MatAXPYGetPreallocation_SeqX_private(mbs,x->i,x->j,y->i,y->j,nnz);
2379:   return(0);
2380: }

2382: PetscErrorCode MatAXPY_SeqBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2383: {
2384:   Mat_SeqBAIJ    *x = (Mat_SeqBAIJ*)X->data,*y = (Mat_SeqBAIJ*)Y->data;
2386:   PetscInt       bs=Y->rmap->bs,bs2=bs*bs;
2387:   PetscBLASInt   one=1;

2390:   if (str == SAME_NONZERO_PATTERN) {
2391:     PetscScalar  alpha = a;
2392:     PetscBLASInt bnz;
2393:     PetscBLASIntCast(x->nz*bs2,&bnz);
2394:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2395:     PetscObjectStateIncrease((PetscObject)Y);
2396:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2397:     MatAXPY_Basic(Y,a,X,str);
2398:   } else {
2399:     Mat      B;
2400:     PetscInt *nnz;
2401:     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
2402:     PetscMalloc1(Y->rmap->N,&nnz);
2403:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2404:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2405:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2406:     MatSetBlockSizesFromMats(B,Y,Y);
2407:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
2408:     MatAXPYGetPreallocation_SeqBAIJ(Y,X,nnz);
2409:     MatSeqBAIJSetPreallocation(B,bs,0,nnz);
2410:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2411:     MatHeaderReplace(Y,&B);
2412:     PetscFree(nnz);
2413:   }
2414:   return(0);
2415: }

2417: PetscErrorCode MatRealPart_SeqBAIJ(Mat A)
2418: {
2419:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
2420:   PetscInt    i,nz = a->bs2*a->i[a->mbs];
2421:   MatScalar   *aa = a->a;

2424:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
2425:   return(0);
2426: }

2428: PetscErrorCode MatImaginaryPart_SeqBAIJ(Mat A)
2429: {
2430:   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
2431:   PetscInt    i,nz = a->bs2*a->i[a->mbs];
2432:   MatScalar   *aa = a->a;

2435:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
2436:   return(0);
2437: }

2439: /*
2440:     Code almost idential to MatGetColumnIJ_SeqAIJ() should share common code
2441: */
2442: PetscErrorCode MatGetColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2443: {
2444:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2446:   PetscInt       bs = A->rmap->bs,i,*collengths,*cia,*cja,n = A->cmap->n/bs,m = A->rmap->n/bs;
2447:   PetscInt       nz = a->i[m],row,*jj,mr,col;

2450:   *nn = n;
2451:   if (!ia) return(0);
2452:   if (symmetric) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not for BAIJ matrices");
2453:   else {
2454:     PetscCalloc1(n+1,&collengths);
2455:     PetscMalloc1(n+1,&cia);
2456:     PetscMalloc1(nz+1,&cja);
2457:     jj   = a->j;
2458:     for (i=0; i<nz; i++) {
2459:       collengths[jj[i]]++;
2460:     }
2461:     cia[0] = oshift;
2462:     for (i=0; i<n; i++) {
2463:       cia[i+1] = cia[i] + collengths[i];
2464:     }
2465:     PetscMemzero(collengths,n*sizeof(PetscInt));
2466:     jj   = a->j;
2467:     for (row=0; row<m; row++) {
2468:       mr = a->i[row+1] - a->i[row];
2469:       for (i=0; i<mr; i++) {
2470:         col = *jj++;

2472:         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2473:       }
2474:     }
2475:     PetscFree(collengths);
2476:     *ia  = cia; *ja = cja;
2477:   }
2478:   return(0);
2479: }

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

2486:   if (!ia) return(0);
2487:   PetscFree(*ia);
2488:   PetscFree(*ja);
2489:   return(0);
2490: }

2492: /*
2493:  MatGetColumnIJ_SeqBAIJ_Color() and MatRestoreColumnIJ_SeqBAIJ_Color() are customized from
2494:  MatGetColumnIJ_SeqBAIJ() and MatRestoreColumnIJ_SeqBAIJ() by adding an output
2495:  spidx[], index of a->a, to be used in MatTransposeColoringCreate() and MatFDColoringCreate()
2496:  */
2497: PetscErrorCode MatGetColumnIJ_SeqBAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
2498: {
2499:   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2501:   PetscInt       i,*collengths,*cia,*cja,n=a->nbs,m=a->mbs;
2502:   PetscInt       nz = a->i[m],row,*jj,mr,col;
2503:   PetscInt       *cspidx;

2506:   *nn = n;
2507:   if (!ia) return(0);

2509:   PetscCalloc1(n+1,&collengths);
2510:   PetscMalloc1(n+1,&cia);
2511:   PetscMalloc1(nz+1,&cja);
2512:   PetscMalloc1(nz+1,&cspidx);
2513:   jj   = a->j;
2514:   for (i=0; i<nz; i++) {
2515:     collengths[jj[i]]++;
2516:   }
2517:   cia[0] = oshift;
2518:   for (i=0; i<n; i++) {
2519:     cia[i+1] = cia[i] + collengths[i];
2520:   }
2521:   PetscMemzero(collengths,n*sizeof(PetscInt));
2522:   jj   = a->j;
2523:   for (row=0; row<m; row++) {
2524:     mr = a->i[row+1] - a->i[row];
2525:     for (i=0; i<mr; i++) {
2526:       col = *jj++;
2527:       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
2528:       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
2529:     }
2530:   }
2531:   PetscFree(collengths);
2532:   *ia    = cia; *ja = cja;
2533:   *spidx = cspidx;
2534:   return(0);
2535: }

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

2542:   MatRestoreColumnIJ_SeqBAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
2543:   PetscFree(*spidx);
2544:   return(0);
2545: }

2547: PetscErrorCode MatShift_SeqBAIJ(Mat Y,PetscScalar a)
2548: {
2550:   Mat_SeqBAIJ     *aij = (Mat_SeqBAIJ*)Y->data;

2553:   if (!Y->preallocated || !aij->nz) {
2554:     MatSeqBAIJSetPreallocation(Y,Y->rmap->bs,1,NULL);
2555:   }
2556:   MatShift_Basic(Y,a);
2557:   return(0);
2558: }

2560: /* -------------------------------------------------------------------*/
2561: static struct _MatOps MatOps_Values = {MatSetValues_SeqBAIJ,
2562:                                        MatGetRow_SeqBAIJ,
2563:                                        MatRestoreRow_SeqBAIJ,
2564:                                        MatMult_SeqBAIJ_N,
2565:                                /* 4*/  MatMultAdd_SeqBAIJ_N,
2566:                                        MatMultTranspose_SeqBAIJ,
2567:                                        MatMultTransposeAdd_SeqBAIJ,
2568:                                        0,
2569:                                        0,
2570:                                        0,
2571:                                /* 10*/ 0,
2572:                                        MatLUFactor_SeqBAIJ,
2573:                                        0,
2574:                                        0,
2575:                                        MatTranspose_SeqBAIJ,
2576:                                /* 15*/ MatGetInfo_SeqBAIJ,
2577:                                        MatEqual_SeqBAIJ,
2578:                                        MatGetDiagonal_SeqBAIJ,
2579:                                        MatDiagonalScale_SeqBAIJ,
2580:                                        MatNorm_SeqBAIJ,
2581:                                /* 20*/ 0,
2582:                                        MatAssemblyEnd_SeqBAIJ,
2583:                                        MatSetOption_SeqBAIJ,
2584:                                        MatZeroEntries_SeqBAIJ,
2585:                                /* 24*/ MatZeroRows_SeqBAIJ,
2586:                                        0,
2587:                                        0,
2588:                                        0,
2589:                                        0,
2590:                                /* 29*/ MatSetUp_SeqBAIJ,
2591:                                        0,
2592:                                        0,
2593:                                        0,
2594:                                        0,
2595:                                /* 34*/ MatDuplicate_SeqBAIJ,
2596:                                        0,
2597:                                        0,
2598:                                        MatILUFactor_SeqBAIJ,
2599:                                        0,
2600:                                /* 39*/ MatAXPY_SeqBAIJ,
2601:                                        MatGetSubMatrices_SeqBAIJ,
2602:                                        MatIncreaseOverlap_SeqBAIJ,
2603:                                        MatGetValues_SeqBAIJ,
2604:                                        MatCopy_SeqBAIJ,
2605:                                /* 44*/ 0,
2606:                                        MatScale_SeqBAIJ,
2607:                                        MatShift_SeqBAIJ,
2608:                                        0,
2609:                                        MatZeroRowsColumns_SeqBAIJ,
2610:                                /* 49*/ 0,
2611:                                        MatGetRowIJ_SeqBAIJ,
2612:                                        MatRestoreRowIJ_SeqBAIJ,
2613:                                        MatGetColumnIJ_SeqBAIJ,
2614:                                        MatRestoreColumnIJ_SeqBAIJ,
2615:                                /* 54*/ MatFDColoringCreate_SeqXAIJ,
2616:                                        0,
2617:                                        0,
2618:                                        0,
2619:                                        MatSetValuesBlocked_SeqBAIJ,
2620:                                /* 59*/ MatGetSubMatrix_SeqBAIJ,
2621:                                        MatDestroy_SeqBAIJ,
2622:                                        MatView_SeqBAIJ,
2623:                                        0,
2624:                                        0,
2625:                                /* 64*/ 0,
2626:                                        0,
2627:                                        0,
2628:                                        0,
2629:                                        0,
2630:                                /* 69*/ MatGetRowMaxAbs_SeqBAIJ,
2631:                                        0,
2632:                                        MatConvert_Basic,
2633:                                        0,
2634:                                        0,
2635:                                /* 74*/ 0,
2636:                                        MatFDColoringApply_BAIJ,
2637:                                        0,
2638:                                        0,
2639:                                        0,
2640:                                /* 79*/ 0,
2641:                                        0,
2642:                                        0,
2643:                                        0,
2644:                                        MatLoad_SeqBAIJ,
2645:                                /* 84*/ 0,
2646:                                        0,
2647:                                        0,
2648:                                        0,
2649:                                        0,
2650:                                /* 89*/ 0,
2651:                                        0,
2652:                                        0,
2653:                                        0,
2654:                                        0,
2655:                                /* 94*/ 0,
2656:                                        0,
2657:                                        0,
2658:                                        0,
2659:                                        0,
2660:                                /* 99*/ 0,
2661:                                        0,
2662:                                        0,
2663:                                        0,
2664:                                        0,
2665:                                /*104*/ 0,
2666:                                        MatRealPart_SeqBAIJ,
2667:                                        MatImaginaryPart_SeqBAIJ,
2668:                                        0,
2669:                                        0,
2670:                                /*109*/ 0,
2671:                                        0,
2672:                                        0,
2673:                                        0,
2674:                                        MatMissingDiagonal_SeqBAIJ,
2675:                                /*114*/ 0,
2676:                                        0,
2677:                                        0,
2678:                                        0,
2679:                                        0,
2680:                                /*119*/ 0,
2681:                                        0,
2682:                                        MatMultHermitianTranspose_SeqBAIJ,
2683:                                        MatMultHermitianTransposeAdd_SeqBAIJ,
2684:                                        0,
2685:                                /*124*/ 0,
2686:                                        0,
2687:                                        MatInvertBlockDiagonal_SeqBAIJ,
2688:                                        0,
2689:                                        0,
2690:                                /*129*/ 0,
2691:                                        0,
2692:                                        0,
2693:                                        0,
2694:                                        0,
2695:                                /*134*/ 0,
2696:                                        0,
2697:                                        0,
2698:                                        0,
2699:                                        0,
2700:                                /*139*/ MatSetBlockSizes_Default,
2701:                                        0,
2702:                                        0,
2703:                                        MatFDColoringSetUp_SeqXAIJ,
2704:                                        0,
2705:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_SeqBAIJ
2706: };

2708: PetscErrorCode  MatStoreValues_SeqBAIJ(Mat mat)
2709: {
2710:   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ*)mat->data;
2711:   PetscInt       nz   = aij->i[aij->mbs]*aij->bs2;

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

2717:   /* allocate space for values if not already there */
2718:   if (!aij->saved_values) {
2719:     PetscMalloc1(nz+1,&aij->saved_values);
2720:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
2721:   }

2723:   /* copy values over */
2724:   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));
2725:   return(0);
2726: }

2728: PetscErrorCode  MatRetrieveValues_SeqBAIJ(Mat mat)
2729: {
2730:   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ*)mat->data;
2732:   PetscInt       nz = aij->i[aij->mbs]*aij->bs2;

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

2738:   /* copy values over */
2739:   PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));
2740:   return(0);
2741: }

2743: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
2744: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqSBAIJ(Mat, MatType,MatReuse,Mat*);

2746: static PetscErrorCode  MatSeqBAIJSetPreallocation_SeqBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
2747: {
2748:   Mat_SeqBAIJ    *b;
2750:   PetscInt       i,mbs,nbs,bs2;
2751:   PetscBool      flg = PETSC_FALSE,skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;

2754:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
2755:   if (nz == MAT_SKIP_ALLOCATION) {
2756:     skipallocation = PETSC_TRUE;
2757:     nz             = 0;
2758:   }

2760:   MatSetBlockSize(B,PetscAbs(bs));
2761:   PetscLayoutSetUp(B->rmap);
2762:   PetscLayoutSetUp(B->cmap);
2763:   PetscLayoutGetBlockSize(B->rmap,&bs);

2765:   B->preallocated = PETSC_TRUE;

2767:   mbs = B->rmap->n/bs;
2768:   nbs = B->cmap->n/bs;
2769:   bs2 = bs*bs;

2771:   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);

2773:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2774:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
2775:   if (nnz) {
2776:     for (i=0; i<mbs; i++) {
2777:       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]);
2778:       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);
2779:     }
2780:   }

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

2787:   if (!flg) {
2788:     switch (bs) {
2789:     case 1:
2790:       B->ops->mult    = MatMult_SeqBAIJ_1;
2791:       B->ops->multadd = MatMultAdd_SeqBAIJ_1;
2792:       break;
2793:     case 2:
2794:       B->ops->mult    = MatMult_SeqBAIJ_2;
2795:       B->ops->multadd = MatMultAdd_SeqBAIJ_2;
2796:       break;
2797:     case 3:
2798:       B->ops->mult    = MatMult_SeqBAIJ_3;
2799:       B->ops->multadd = MatMultAdd_SeqBAIJ_3;
2800:       break;
2801:     case 4:
2802:       B->ops->mult    = MatMult_SeqBAIJ_4;
2803:       B->ops->multadd = MatMultAdd_SeqBAIJ_4;
2804:       break;
2805:     case 5:
2806:       B->ops->mult    = MatMult_SeqBAIJ_5;
2807:       B->ops->multadd = MatMultAdd_SeqBAIJ_5;
2808:       break;
2809:     case 6:
2810:       B->ops->mult    = MatMult_SeqBAIJ_6;
2811:       B->ops->multadd = MatMultAdd_SeqBAIJ_6;
2812:       break;
2813:     case 7:
2814:       B->ops->mult    = MatMult_SeqBAIJ_7;
2815:       B->ops->multadd = MatMultAdd_SeqBAIJ_7;
2816:       break;
2817:     case 15:
2818:       B->ops->mult    = MatMult_SeqBAIJ_15_ver1;
2819:       B->ops->multadd = MatMultAdd_SeqBAIJ_N;
2820:       break;
2821:     default:
2822:       B->ops->mult    = MatMult_SeqBAIJ_N;
2823:       B->ops->multadd = MatMultAdd_SeqBAIJ_N;
2824:       break;
2825:     }
2826:   }
2827:   B->ops->sor = MatSOR_SeqBAIJ;
2828:   b->mbs = mbs;
2829:   b->nbs = nbs;
2830:   if (!skipallocation) {
2831:     if (!b->imax) {
2832:       PetscMalloc2(mbs,&b->imax,mbs,&b->ilen);
2833:       PetscLogObjectMemory((PetscObject)B,2*mbs*sizeof(PetscInt));

2835:       b->free_imax_ilen = PETSC_TRUE;
2836:     }
2837:     /* b->ilen will count nonzeros in each block row so far. */
2838:     for (i=0; i<mbs; i++) b->ilen[i] = 0;
2839:     if (!nnz) {
2840:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2841:       else if (nz < 0) nz = 1;
2842:       for (i=0; i<mbs; i++) b->imax[i] = nz;
2843:       nz = nz*mbs;
2844:     } else {
2845:       nz = 0;
2846:       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2847:     }

2849:     /* allocate the matrix space */
2850:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
2851:     PetscMalloc3(bs2*nz,&b->a,nz,&b->j,B->rmap->N+1,&b->i);
2852:     PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));
2853:     PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));
2854:     PetscMemzero(b->j,nz*sizeof(PetscInt));

2856:     b->singlemalloc = PETSC_TRUE;
2857:     b->i[0]         = 0;
2858:     for (i=1; i<mbs+1; i++) {
2859:       b->i[i] = b->i[i-1] + b->imax[i-1];
2860:     }
2861:     b->free_a  = PETSC_TRUE;
2862:     b->free_ij = PETSC_TRUE;
2863:   } else {
2864:     b->free_a  = PETSC_FALSE;
2865:     b->free_ij = PETSC_FALSE;
2866:   }

2868:   b->bs2              = bs2;
2869:   b->mbs              = mbs;
2870:   b->nz               = 0;
2871:   b->maxnz            = nz;
2872:   B->info.nz_unneeded = (PetscReal)b->maxnz*bs2;
2873:   B->was_assembled    = PETSC_FALSE;
2874:   B->assembled        = PETSC_FALSE;
2875:   if (realalloc) {MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);}
2876:   return(0);
2877: }

2879: PetscErrorCode MatSeqBAIJSetPreallocationCSR_SeqBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2880: {
2881:   PetscInt       i,m,nz,nz_max=0,*nnz;
2882:   PetscScalar    *values=0;
2883:   PetscBool      roworiented = ((Mat_SeqBAIJ*)B->data)->roworiented;

2887:   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
2888:   PetscLayoutSetBlockSize(B->rmap,bs);
2889:   PetscLayoutSetBlockSize(B->cmap,bs);
2890:   PetscLayoutSetUp(B->rmap);
2891:   PetscLayoutSetUp(B->cmap);
2892:   PetscLayoutGetBlockSize(B->rmap,&bs);
2893:   m    = B->rmap->n/bs;

2895:   if (ii[0] != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %D",ii[0]);
2896:   PetscMalloc1(m+1, &nnz);
2897:   for (i=0; i<m; i++) {
2898:     nz = ii[i+1]- ii[i];
2899:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D",i,nz);
2900:     nz_max = PetscMax(nz_max, nz);
2901:     nnz[i] = nz;
2902:   }
2903:   MatSeqBAIJSetPreallocation(B,bs,0,nnz);
2904:   PetscFree(nnz);

2906:   values = (PetscScalar*)V;
2907:   if (!values) {
2908:     PetscCalloc1(bs*bs*(nz_max+1),&values);
2909:   }
2910:   for (i=0; i<m; i++) {
2911:     PetscInt          ncols  = ii[i+1] - ii[i];
2912:     const PetscInt    *icols = jj + ii[i];
2913:     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2914:     if (!roworiented) {
2915:       MatSetValuesBlocked_SeqBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);
2916:     } else {
2917:       PetscInt j;
2918:       for (j=0; j<ncols; j++) {
2919:         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
2920:         MatSetValuesBlocked_SeqBAIJ(B,1,&i,1,&icols[j],svals,INSERT_VALUES);
2921:       }
2922:     }
2923:   }
2924:   if (!V) { PetscFree(values); }
2925:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2926:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2927:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2928:   return(0);
2929: }

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

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

2938:   Level: beginner

2940: .seealso: MatCreateSeqBAIJ()
2941: M*/

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

2945: PETSC_EXTERN PetscErrorCode MatCreate_SeqBAIJ(Mat B)
2946: {
2948:   PetscMPIInt    size;
2949:   Mat_SeqBAIJ    *b;

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

2955:   PetscNewLog(B,&b);
2956:   B->data = (void*)b;
2957:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

2959:   b->row          = 0;
2960:   b->col          = 0;
2961:   b->icol         = 0;
2962:   b->reallocs     = 0;
2963:   b->saved_values = 0;

2965:   b->roworiented        = PETSC_TRUE;
2966:   b->nonew              = 0;
2967:   b->diag               = 0;
2968:   B->spptr              = 0;
2969:   B->info.nz_unneeded   = (PetscReal)b->maxnz*b->bs2;
2970:   b->keepnonzeropattern = PETSC_FALSE;

2972:   PetscObjectComposeFunction((PetscObject)B,"MatInvertBlockDiagonal_C",MatInvertBlockDiagonal_SeqBAIJ);
2973:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqBAIJ);
2974:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqBAIJ);
2975:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJSetColumnIndices_C",MatSeqBAIJSetColumnIndices_SeqBAIJ);
2976:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqbaij_seqaij_C",MatConvert_SeqBAIJ_SeqAIJ);
2977:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqbaij_seqsbaij_C",MatConvert_SeqBAIJ_SeqSBAIJ);
2978:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJSetPreallocation_C",MatSeqBAIJSetPreallocation_SeqBAIJ);
2979:   PetscObjectComposeFunction((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",MatSeqBAIJSetPreallocationCSR_SeqBAIJ);
2980:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqBAIJ);
2981: #if defined(PETSC_HAVE_HYPRE)
2982:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_baij_hypre_C",MatConvert_AIJ_HYPRE);
2983: #endif
2984:   PetscObjectChangeTypeName((PetscObject)B,MATSEQBAIJ);
2985:   return(0);
2986: }

2988: PetscErrorCode MatDuplicateNoCreate_SeqBAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
2989: {
2990:   Mat_SeqBAIJ    *c = (Mat_SeqBAIJ*)C->data,*a = (Mat_SeqBAIJ*)A->data;
2992:   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 = a->bs2;

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

2997:   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2998:     c->imax           = a->imax;
2999:     c->ilen           = a->ilen;
3000:     c->free_imax_ilen = PETSC_FALSE;
3001:   } else {
3002:     PetscMalloc2(mbs,&c->imax,mbs,&c->ilen);
3003:     PetscLogObjectMemory((PetscObject)C,2*mbs*sizeof(PetscInt));
3004:     for (i=0; i<mbs; i++) {
3005:       c->imax[i] = a->imax[i];
3006:       c->ilen[i] = a->ilen[i];
3007:     }
3008:     c->free_imax_ilen = PETSC_TRUE;
3009:   }

3011:   /* allocate the matrix space */
3012:   if (mallocmatspace) {
3013:     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
3014:       PetscCalloc1(bs2*nz,&c->a);
3015:       PetscLogObjectMemory((PetscObject)C,a->i[mbs]*bs2*sizeof(PetscScalar));

3017:       c->i            = a->i;
3018:       c->j            = a->j;
3019:       c->singlemalloc = PETSC_FALSE;
3020:       c->free_a       = PETSC_TRUE;
3021:       c->free_ij      = PETSC_FALSE;
3022:       c->parent       = A;
3023:       C->preallocated = PETSC_TRUE;
3024:       C->assembled    = PETSC_TRUE;

3026:       PetscObjectReference((PetscObject)A);
3027:       MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3028:       MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3029:     } else {
3030:       PetscMalloc3(bs2*nz,&c->a,nz,&c->j,mbs+1,&c->i);
3031:       PetscLogObjectMemory((PetscObject)C,a->i[mbs]*(bs2*sizeof(PetscScalar)+sizeof(PetscInt))+(mbs+1)*sizeof(PetscInt));

3033:       c->singlemalloc = PETSC_TRUE;
3034:       c->free_a       = PETSC_TRUE;
3035:       c->free_ij      = PETSC_TRUE;

3037:       PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));
3038:       if (mbs > 0) {
3039:         PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));
3040:         if (cpvalues == MAT_COPY_VALUES) {
3041:           PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));
3042:         } else {
3043:           PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));
3044:         }
3045:       }
3046:       C->preallocated = PETSC_TRUE;
3047:       C->assembled    = PETSC_TRUE;
3048:     }
3049:   }

3051:   c->roworiented = a->roworiented;
3052:   c->nonew       = a->nonew;

3054:   PetscLayoutReference(A->rmap,&C->rmap);
3055:   PetscLayoutReference(A->cmap,&C->cmap);

3057:   c->bs2         = a->bs2;
3058:   c->mbs         = a->mbs;
3059:   c->nbs         = a->nbs;

3061:   if (a->diag) {
3062:     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
3063:       c->diag      = a->diag;
3064:       c->free_diag = PETSC_FALSE;
3065:     } else {
3066:       PetscMalloc1(mbs+1,&c->diag);
3067:       PetscLogObjectMemory((PetscObject)C,(mbs+1)*sizeof(PetscInt));
3068:       for (i=0; i<mbs; i++) c->diag[i] = a->diag[i];
3069:       c->free_diag = PETSC_TRUE;
3070:     }
3071:   } else c->diag = 0;

3073:   c->nz         = a->nz;
3074:   c->maxnz      = a->nz;         /* Since we allocate exactly the right amount */
3075:   c->solve_work = NULL;
3076:   c->mult_work  = NULL;
3077:   c->sor_workt  = NULL;
3078:   c->sor_work   = NULL;

3080:   c->compressedrow.use   = a->compressedrow.use;
3081:   c->compressedrow.nrows = a->compressedrow.nrows;
3082:   if (a->compressedrow.use) {
3083:     i    = a->compressedrow.nrows;
3084:     PetscMalloc2(i+1,&c->compressedrow.i,i+1,&c->compressedrow.rindex);
3085:     PetscLogObjectMemory((PetscObject)C,(2*i+1)*sizeof(PetscInt));
3086:     PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));
3087:     PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));
3088:   } else {
3089:     c->compressedrow.use    = PETSC_FALSE;
3090:     c->compressedrow.i      = NULL;
3091:     c->compressedrow.rindex = NULL;
3092:   }
3093:   C->nonzerostate = A->nonzerostate;

3095:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
3096:   PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));
3097:   return(0);
3098: }

3100: PetscErrorCode MatDuplicate_SeqBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3101: {

3105:   MatCreate(PetscObjectComm((PetscObject)A),B);
3106:   MatSetSizes(*B,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);
3107:   MatSetType(*B,MATSEQBAIJ);
3108:   MatDuplicateNoCreate_SeqBAIJ(*B,A,cpvalues,PETSC_TRUE);
3109:   return(0);
3110: }

3112: PetscErrorCode MatLoad_SeqBAIJ(Mat newmat,PetscViewer viewer)
3113: {
3114:   Mat_SeqBAIJ    *a;
3116:   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs = newmat->rmap->bs;
3117:   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*browlengths,maskcount;
3118:   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
3119:   PetscInt       *masked,nmask,tmp,bs2,ishift;
3120:   PetscMPIInt    size;
3121:   int            fd;
3122:   PetscScalar    *aa;
3123:   MPI_Comm       comm;

3126:   /* force binary viewer to load .info file if it has not yet done so */
3127:   PetscViewerSetUp(viewer);
3128:   PetscObjectGetComm((PetscObject)viewer,&comm);
3129:   PetscOptionsBegin(comm,NULL,"Options for loading SEQBAIJ matrix","Mat");
3130:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
3131:   PetscOptionsEnd();
3132:   if (bs < 0) bs = 1;
3133:   bs2  = bs*bs;

3135:   MPI_Comm_size(comm,&size);
3136:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
3137:   PetscViewerBinaryGetDescriptor(viewer,&fd);
3138:   PetscBinaryRead(fd,header,4,PETSC_INT);
3139:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
3140:   M = header[1]; N = header[2]; nz = header[3];

3142:   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqBAIJ");
3143:   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");

3145:   /*
3146:      This code adds extra rows to make sure the number of rows is
3147:     divisible by the blocksize
3148:   */
3149:   mbs        = M/bs;
3150:   extra_rows = bs - M + bs*(mbs);
3151:   if (extra_rows == bs) extra_rows = 0;
3152:   else mbs++;
3153:   if (extra_rows) {
3154:     PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
3155:   }

3157:   /* Set global sizes if not already set */
3158:   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
3159:     MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);
3160:   } else { /* Check if the matrix global sizes are correct */
3161:     MatGetSize(newmat,&rows,&cols);
3162:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
3163:       MatGetLocalSize(newmat,&rows,&cols);
3164:     }
3165:     if (M != rows ||  N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols);
3166:   }

3168:   /* read in row lengths */
3169:   PetscMalloc1(M+extra_rows,&rowlengths);
3170:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
3171:   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;

3173:   /* read in column indices */
3174:   PetscMalloc1(nz+extra_rows,&jj);
3175:   PetscBinaryRead(fd,jj,nz,PETSC_INT);
3176:   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;

3178:   /* loop over row lengths determining block row lengths */
3179:   PetscCalloc1(mbs,&browlengths);
3180:   PetscMalloc2(mbs,&mask,mbs,&masked);
3181:   PetscMemzero(mask,mbs*sizeof(PetscInt));
3182:   rowcount = 0;
3183:   nzcount  = 0;
3184:   for (i=0; i<mbs; i++) {
3185:     nmask = 0;
3186:     for (j=0; j<bs; j++) {
3187:       kmax = rowlengths[rowcount];
3188:       for (k=0; k<kmax; k++) {
3189:         tmp = jj[nzcount++]/bs;
3190:         if (!mask[tmp]) {masked[nmask++] = tmp; mask[tmp] = 1;}
3191:       }
3192:       rowcount++;
3193:     }
3194:     browlengths[i] += nmask;
3195:     /* zero out the mask elements we set */
3196:     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3197:   }

3199:   /* Do preallocation  */
3200:   MatSeqBAIJSetPreallocation(newmat,bs,0,browlengths);
3201:   a    = (Mat_SeqBAIJ*)newmat->data;

3203:   /* set matrix "i" values */
3204:   a->i[0] = 0;
3205:   for (i=1; i<= mbs; i++) {
3206:     a->i[i]      = a->i[i-1] + browlengths[i-1];
3207:     a->ilen[i-1] = browlengths[i-1];
3208:   }
3209:   a->nz = 0;
3210:   for (i=0; i<mbs; i++) a->nz += browlengths[i];

3212:   /* read in nonzero values */
3213:   PetscMalloc1(nz+extra_rows,&aa);
3214:   PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);
3215:   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;

3217:   /* set "a" and "j" values into matrix */
3218:   nzcount = 0; jcount = 0;
3219:   for (i=0; i<mbs; i++) {
3220:     nzcountb = nzcount;
3221:     nmask    = 0;
3222:     for (j=0; j<bs; j++) {
3223:       kmax = rowlengths[i*bs+j];
3224:       for (k=0; k<kmax; k++) {
3225:         tmp = jj[nzcount++]/bs;
3226:         if (!mask[tmp]) { masked[nmask++] = tmp; mask[tmp] = 1;}
3227:       }
3228:     }
3229:     /* sort the masked values */
3230:     PetscSortInt(nmask,masked);

3232:     /* set "j" values into matrix */
3233:     maskcount = 1;
3234:     for (j=0; j<nmask; j++) {
3235:       a->j[jcount++]  = masked[j];
3236:       mask[masked[j]] = maskcount++;
3237:     }
3238:     /* set "a" values into matrix */
3239:     ishift = bs2*a->i[i];
3240:     for (j=0; j<bs; j++) {
3241:       kmax = rowlengths[i*bs+j];
3242:       for (k=0; k<kmax; k++) {
3243:         tmp       = jj[nzcountb]/bs;
3244:         block     = mask[tmp] - 1;
3245:         point     = jj[nzcountb] - bs*tmp;
3246:         idx       = ishift + bs2*block + j + bs*point;
3247:         a->a[idx] = (MatScalar)aa[nzcountb++];
3248:       }
3249:     }
3250:     /* zero out the mask elements we set */
3251:     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3252:   }
3253:   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");

3255:   PetscFree(rowlengths);
3256:   PetscFree(browlengths);
3257:   PetscFree(aa);
3258:   PetscFree(jj);
3259:   PetscFree2(mask,masked);

3261:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
3262:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
3263:   return(0);
3264: }

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

3273:    Collective on MPI_Comm

3275:    Input Parameters:
3276: +  comm - MPI communicator, set to PETSC_COMM_SELF
3277: .  bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
3278:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
3279: .  m - number of rows
3280: .  n - number of columns
3281: .  nz - number of nonzero blocks  per block row (same for all rows)
3282: -  nnz - array containing the number of nonzero blocks in the various block rows
3283:          (possibly different for each block row) or NULL

3285:    Output Parameter:
3286: .  A - the matrix

3288:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3289:    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3290:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

3292:    Options Database Keys:
3293: .   -mat_no_unroll - uses code that does not unroll the loops in the
3294:                      block calculations (much slower)
3295: .    -mat_block_size - size of the blocks to use

3297:    Level: intermediate

3299:    Notes:
3300:    The number of rows and columns must be divisible by blocksize.

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

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

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

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

3315: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateBAIJ()
3316: @*/
3317: PetscErrorCode  MatCreateSeqBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3318: {

3322:   MatCreate(comm,A);
3323:   MatSetSizes(*A,m,n,m,n);
3324:   MatSetType(*A,MATSEQBAIJ);
3325:   MatSeqBAIJSetPreallocation(*A,bs,nz,(PetscInt*)nnz);
3326:   return(0);
3327: }

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

3336:    Collective on MPI_Comm

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

3346:    Options Database Keys:
3347: .   -mat_no_unroll - uses code that does not unroll the loops in the
3348:                      block calculations (much slower)
3349: .   -mat_block_size - size of the blocks to use

3351:    Level: intermediate

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

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

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

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

3369: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateBAIJ(), MatGetInfo()
3370: @*/
3371: PetscErrorCode  MatSeqBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
3372: {

3379:   PetscTryMethod(B,"MatSeqBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));
3380:   return(0);
3381: }

3383: /*@C
3384:    MatSeqBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format
3385:    (the default sequential PETSc format).

3387:    Collective on MPI_Comm

3389:    Input Parameters:
3390: +  B - the matrix
3391: .  i - the indices into j for the start of each local row (starts with zero)
3392: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
3393: -  v - optional values in the matrix

3395:    Level: developer

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

3404: .keywords: matrix, aij, compressed row, sparse

3406: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatSeqBAIJSetPreallocation(), MATSEQBAIJ
3407: @*/
3408: PetscErrorCode  MatSeqBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3409: {

3416:   PetscTryMethod(B,"MatSeqBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
3417:   return(0);
3418: }


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

3424:      Collective on MPI_Comm

3426:    Input Parameters:
3427: +  comm - must be an MPI communicator of size 1
3428: .  bs - size of block
3429: .  m - number of rows
3430: .  n - number of columns
3431: .  i - row indices
3432: .  j - column indices
3433: -  a - matrix values

3435:    Output Parameter:
3436: .  mat - the matrix

3438:    Level: advanced

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

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

3446:        The i and j indices are 0 based

3448:        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).

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

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

3457: @*/
3458: PetscErrorCode  MatCreateSeqBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
3459: {
3461:   PetscInt       ii;
3462:   Mat_SeqBAIJ    *baij;

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

3468:   MatCreate(comm,mat);
3469:   MatSetSizes(*mat,m,n,m,n);
3470:   MatSetType(*mat,MATSEQBAIJ);
3471:   MatSeqBAIJSetPreallocation(*mat,bs,MAT_SKIP_ALLOCATION,0);
3472:   baij = (Mat_SeqBAIJ*)(*mat)->data;
3473:   PetscMalloc2(m,&baij->imax,m,&baij->ilen);
3474:   PetscLogObjectMemory((PetscObject)*mat,2*m*sizeof(PetscInt));

3476:   baij->i = i;
3477:   baij->j = j;
3478:   baij->a = a;

3480:   baij->singlemalloc = PETSC_FALSE;
3481:   baij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3482:   baij->free_a       = PETSC_FALSE;
3483:   baij->free_ij      = PETSC_FALSE;

3485:   for (ii=0; ii<m; ii++) {
3486:     baij->ilen[ii] = baij->imax[ii] = i[ii+1] - i[ii];
3487: #if defined(PETSC_USE_DEBUG)
3488:     if (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]);
3489: #endif
3490:   }
3491: #if defined(PETSC_USE_DEBUG)
3492:   for (ii=0; ii<baij->i[m]; ii++) {
3493:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3494:     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]);
3495:   }
3496: #endif

3498:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
3499:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
3500:   return(0);
3501: }

3503: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
3504: {

3508:   MatCreateMPIMatConcatenateSeqMat_MPIBAIJ(comm,inmat,n,scall,outmat);
3509:   return(0);
3510: }