xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 4dde04f4c835a3d74306cfa47aff66fee8d17a44)
1 
2 /*
3     Defines the basic matrix operations for the AIJ (compressed row)
4   matrix storage format.
5 */
6 
7 
8 #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9 #include <petscblaslapack.h>
10 #include <petscbt.h>
11 #include <petsc-private/kernels/blocktranspose.h>
12 #if defined(PETSC_THREADCOMM_ACTIVE)
13 #include <petscthreadcomm.h>
14 #endif
15 
16 #undef __FUNCT__
17 #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
18 PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
19 {
20   PetscErrorCode ierr;
21   PetscInt       i,m,n;
22   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
23 
24   PetscFunctionBegin;
25   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
26   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
27   if (type == NORM_2) {
28     for (i=0; i<aij->i[m]; i++) {
29       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
30     }
31   } else if (type == NORM_1) {
32     for (i=0; i<aij->i[m]; i++) {
33       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
34     }
35   } else if (type == NORM_INFINITY) {
36     for (i=0; i<aij->i[m]; i++) {
37       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
38     }
39   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
40 
41   if (type == NORM_2) {
42     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
43   }
44   PetscFunctionReturn(0);
45 }
46 
47 #undef __FUNCT__
48 #define __FUNCT__ "MatFindOffBlockDiagonalEntries_SeqAIJ"
49 PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is)
50 {
51   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
52   PetscInt        i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs;
53   const PetscInt  *jj = a->j,*ii = a->i;
54   PetscInt        *rows;
55   PetscErrorCode  ierr;
56 
57   PetscFunctionBegin;
58   for (i=0; i<m; i++) {
59     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
60       cnt++;
61     }
62   }
63   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
64   cnt  = 0;
65   for (i=0; i<m; i++) {
66     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
67       rows[cnt] = i;
68       cnt++;
69     }
70   }
71   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);CHKERRQ(ierr);
72   PetscFunctionReturn(0);
73 }
74 
75 #undef __FUNCT__
76 #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ_Private"
77 PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
78 {
79   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
80   const MatScalar *aa = a->a;
81   PetscInt        i,m=A->rmap->n,cnt = 0;
82   const PetscInt  *jj = a->j,*diag;
83   PetscInt        *rows;
84   PetscErrorCode  ierr;
85 
86   PetscFunctionBegin;
87   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
88   diag = a->diag;
89   for (i=0; i<m; i++) {
90     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
91       cnt++;
92     }
93   }
94   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
95   cnt  = 0;
96   for (i=0; i<m; i++) {
97     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
98       rows[cnt++] = i;
99     }
100   }
101   *nrows = cnt;
102   *zrows = rows;
103   PetscFunctionReturn(0);
104 }
105 
106 #undef __FUNCT__
107 #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
108 PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
109 {
110   PetscInt       nrows,*rows;
111   PetscErrorCode ierr;
112 
113   PetscFunctionBegin;
114   *zrows = NULL;
115   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
116   ierr   = ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
117   PetscFunctionReturn(0);
118 }
119 
120 #undef __FUNCT__
121 #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
122 PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
123 {
124   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
125   const MatScalar *aa;
126   PetscInt        m=A->rmap->n,cnt = 0;
127   const PetscInt  *ii;
128   PetscInt        n,i,j,*rows;
129   PetscErrorCode  ierr;
130 
131   PetscFunctionBegin;
132   *keptrows = 0;
133   ii        = a->i;
134   for (i=0; i<m; i++) {
135     n = ii[i+1] - ii[i];
136     if (!n) {
137       cnt++;
138       goto ok1;
139     }
140     aa = a->a + ii[i];
141     for (j=0; j<n; j++) {
142       if (aa[j] != 0.0) goto ok1;
143     }
144     cnt++;
145 ok1:;
146   }
147   if (!cnt) PetscFunctionReturn(0);
148   ierr = PetscMalloc1((A->rmap->n-cnt),&rows);CHKERRQ(ierr);
149   cnt  = 0;
150   for (i=0; i<m; i++) {
151     n = ii[i+1] - ii[i];
152     if (!n) continue;
153     aa = a->a + ii[i];
154     for (j=0; j<n; j++) {
155       if (aa[j] != 0.0) {
156         rows[cnt++] = i;
157         break;
158       }
159     }
160   }
161   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
162   PetscFunctionReturn(0);
163 }
164 
165 #undef __FUNCT__
166 #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
167 PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
168 {
169   PetscErrorCode ierr;
170   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
171   PetscInt       i,*diag, m = Y->rmap->n;
172   MatScalar      *aa = aij->a;
173   PetscScalar    *v;
174   PetscBool      missing;
175 
176   PetscFunctionBegin;
177   if (Y->assembled) {
178     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr);
179     if (!missing) {
180       diag = aij->diag;
181       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
182       if (is == INSERT_VALUES) {
183         for (i=0; i<m; i++) {
184           aa[diag[i]] = v[i];
185         }
186       } else {
187         for (i=0; i<m; i++) {
188           aa[diag[i]] += v[i];
189         }
190       }
191       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
192       PetscFunctionReturn(0);
193     }
194     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
195   }
196   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
197   PetscFunctionReturn(0);
198 }
199 
200 #undef __FUNCT__
201 #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
202 PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
203 {
204   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
205   PetscErrorCode ierr;
206   PetscInt       i,ishift;
207 
208   PetscFunctionBegin;
209   *m = A->rmap->n;
210   if (!ia) PetscFunctionReturn(0);
211   ishift = 0;
212   if (symmetric && !A->structurally_symmetric) {
213     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
214   } else if (oshift == 1) {
215     PetscInt *tia;
216     PetscInt nz = a->i[A->rmap->n];
217     /* malloc space and  add 1 to i and j indices */
218     ierr = PetscMalloc1((A->rmap->n+1),&tia);CHKERRQ(ierr);
219     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
220     *ia = tia;
221     if (ja) {
222       PetscInt *tja;
223       ierr = PetscMalloc1((nz+1),&tja);CHKERRQ(ierr);
224       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
225       *ja = tja;
226     }
227   } else {
228     *ia = a->i;
229     if (ja) *ja = a->j;
230   }
231   PetscFunctionReturn(0);
232 }
233 
234 #undef __FUNCT__
235 #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
236 PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
237 {
238   PetscErrorCode ierr;
239 
240   PetscFunctionBegin;
241   if (!ia) PetscFunctionReturn(0);
242   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
243     ierr = PetscFree(*ia);CHKERRQ(ierr);
244     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
245   }
246   PetscFunctionReturn(0);
247 }
248 
249 #undef __FUNCT__
250 #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
251 PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
252 {
253   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
254   PetscErrorCode ierr;
255   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
256   PetscInt       nz = a->i[m],row,*jj,mr,col;
257 
258   PetscFunctionBegin;
259   *nn = n;
260   if (!ia) PetscFunctionReturn(0);
261   if (symmetric) {
262     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
263   } else {
264     ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
265     ierr = PetscMalloc1((n+1),&cia);CHKERRQ(ierr);
266     ierr = PetscMalloc1((nz+1),&cja);CHKERRQ(ierr);
267     jj   = a->j;
268     for (i=0; i<nz; i++) {
269       collengths[jj[i]]++;
270     }
271     cia[0] = oshift;
272     for (i=0; i<n; i++) {
273       cia[i+1] = cia[i] + collengths[i];
274     }
275     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
276     jj   = a->j;
277     for (row=0; row<m; row++) {
278       mr = a->i[row+1] - a->i[row];
279       for (i=0; i<mr; i++) {
280         col = *jj++;
281 
282         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
283       }
284     }
285     ierr = PetscFree(collengths);CHKERRQ(ierr);
286     *ia  = cia; *ja = cja;
287   }
288   PetscFunctionReturn(0);
289 }
290 
291 #undef __FUNCT__
292 #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
293 PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
294 {
295   PetscErrorCode ierr;
296 
297   PetscFunctionBegin;
298   if (!ia) PetscFunctionReturn(0);
299 
300   ierr = PetscFree(*ia);CHKERRQ(ierr);
301   ierr = PetscFree(*ja);CHKERRQ(ierr);
302   PetscFunctionReturn(0);
303 }
304 
305 /*
306  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
307  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
308  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
309 */
310 #undef __FUNCT__
311 #define __FUNCT__ "MatGetColumnIJ_SeqAIJ_Color"
312 PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
313 {
314   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
315   PetscErrorCode ierr;
316   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
317   PetscInt       nz = a->i[m],row,*jj,mr,col;
318   PetscInt       *cspidx;
319 
320   PetscFunctionBegin;
321   *nn = n;
322   if (!ia) PetscFunctionReturn(0);
323 
324   ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
325   ierr = PetscMalloc1((n+1),&cia);CHKERRQ(ierr);
326   ierr = PetscMalloc1((nz+1),&cja);CHKERRQ(ierr);
327   ierr = PetscMalloc1((nz+1),&cspidx);CHKERRQ(ierr);
328   jj   = a->j;
329   for (i=0; i<nz; i++) {
330     collengths[jj[i]]++;
331   }
332   cia[0] = oshift;
333   for (i=0; i<n; i++) {
334     cia[i+1] = cia[i] + collengths[i];
335   }
336   ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
337   jj   = a->j;
338   for (row=0; row<m; row++) {
339     mr = a->i[row+1] - a->i[row];
340     for (i=0; i<mr; i++) {
341       col = *jj++;
342       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
343       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
344     }
345   }
346   ierr   = PetscFree(collengths);CHKERRQ(ierr);
347   *ia    = cia; *ja = cja;
348   *spidx = cspidx;
349   PetscFunctionReturn(0);
350 }
351 
352 #undef __FUNCT__
353 #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ_Color"
354 PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
355 {
356   PetscErrorCode ierr;
357 
358   PetscFunctionBegin;
359   ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
360   ierr = PetscFree(*spidx);CHKERRQ(ierr);
361   PetscFunctionReturn(0);
362 }
363 
364 #undef __FUNCT__
365 #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
366 PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
367 {
368   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
369   PetscInt       *ai = a->i;
370   PetscErrorCode ierr;
371 
372   PetscFunctionBegin;
373   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
374   PetscFunctionReturn(0);
375 }
376 
377 /*
378     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
379 
380       -   a single row of values is set with each call
381       -   no row or column indices are negative or (in error) larger than the number of rows or columns
382       -   the values are always added to the matrix, not set
383       -   no new locations are introduced in the nonzero structure of the matrix
384 
385      This does NOT assume the global column indices are sorted
386 
387 */
388 
389 #include <petsc-private/isimpl.h>
390 #undef __FUNCT__
391 #define __FUNCT__ "MatSeqAIJSetValuesLocalFast"
392 PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
393 {
394   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
395   PetscInt       low,high,t,row,nrow,i,col,l;
396   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
397   PetscInt       lastcol = -1;
398   MatScalar      *ap,value,*aa = a->a;
399   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
400 
401   row = ridx[im[0]];
402   rp   = aj + ai[row];
403   ap = aa + ai[row];
404   nrow = ailen[row];
405   low  = 0;
406   high = nrow;
407   for (l=0; l<n; l++) { /* loop over added columns */
408     col = cidx[in[l]];
409     value = v[l];
410 
411     if (col <= lastcol) low = 0;
412     else high = nrow;
413     lastcol = col;
414     while (high-low > 5) {
415       t = (low+high)/2;
416       if (rp[t] > col) high = t;
417       else low = t;
418     }
419     for (i=low; i<high; i++) {
420       if (rp[i] == col) {
421         ap[i] += value;
422         low = i + 1;
423         break;
424       }
425     }
426   }
427   return 0;
428 }
429 
430 #undef __FUNCT__
431 #define __FUNCT__ "MatSetValues_SeqAIJ"
432 PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
433 {
434   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
435   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
436   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
437   PetscErrorCode ierr;
438   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
439   MatScalar      *ap,value,*aa = a->a;
440   PetscBool      ignorezeroentries = a->ignorezeroentries;
441   PetscBool      roworiented       = a->roworiented;
442 
443   PetscFunctionBegin;
444   for (k=0; k<m; k++) { /* loop over added rows */
445     row = im[k];
446     if (row < 0) continue;
447 #if defined(PETSC_USE_DEBUG)
448     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);
449 #endif
450     rp   = aj + ai[row]; ap = aa + ai[row];
451     rmax = imax[row]; nrow = ailen[row];
452     low  = 0;
453     high = nrow;
454     for (l=0; l<n; l++) { /* loop over added columns */
455       if (in[l] < 0) continue;
456 #if defined(PETSC_USE_DEBUG)
457       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);
458 #endif
459       col = in[l];
460       if (roworiented) {
461         value = v[l + k*n];
462       } else {
463         value = v[k + l*m];
464       }
465       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES)) continue;
466 
467       if (col <= lastcol) low = 0;
468       else high = nrow;
469       lastcol = col;
470       while (high-low > 5) {
471         t = (low+high)/2;
472         if (rp[t] > col) high = t;
473         else low = t;
474       }
475       for (i=low; i<high; i++) {
476         if (rp[i] > col) break;
477         if (rp[i] == col) {
478           if (is == ADD_VALUES) ap[i] += value;
479           else ap[i] = value;
480           low = i + 1;
481           goto noinsert;
482         }
483       }
484       if (value == 0.0 && ignorezeroentries) goto noinsert;
485       if (nonew == 1) goto noinsert;
486       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
487       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
488       N = nrow++ - 1; a->nz++; high++;
489       /* shift up all the later entries in this row */
490       for (ii=N; ii>=i; ii--) {
491         rp[ii+1] = rp[ii];
492         ap[ii+1] = ap[ii];
493       }
494       rp[i] = col;
495       ap[i] = value;
496       low   = i + 1;
497       A->nonzerostate++;
498 noinsert:;
499     }
500     ailen[row] = nrow;
501   }
502   PetscFunctionReturn(0);
503 }
504 
505 
506 #undef __FUNCT__
507 #define __FUNCT__ "MatGetValues_SeqAIJ"
508 PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
509 {
510   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
511   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
512   PetscInt   *ai = a->i,*ailen = a->ilen;
513   MatScalar  *ap,*aa = a->a;
514 
515   PetscFunctionBegin;
516   for (k=0; k<m; k++) { /* loop over rows */
517     row = im[k];
518     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
519     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);
520     rp   = aj + ai[row]; ap = aa + ai[row];
521     nrow = ailen[row];
522     for (l=0; l<n; l++) { /* loop over columns */
523       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
524       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);
525       col  = in[l];
526       high = nrow; low = 0; /* assume unsorted */
527       while (high-low > 5) {
528         t = (low+high)/2;
529         if (rp[t] > col) high = t;
530         else low = t;
531       }
532       for (i=low; i<high; i++) {
533         if (rp[i] > col) break;
534         if (rp[i] == col) {
535           *v++ = ap[i];
536           goto finished;
537         }
538       }
539       *v++ = 0.0;
540 finished:;
541     }
542   }
543   PetscFunctionReturn(0);
544 }
545 
546 
547 #undef __FUNCT__
548 #define __FUNCT__ "MatView_SeqAIJ_Binary"
549 PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
550 {
551   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
552   PetscErrorCode ierr;
553   PetscInt       i,*col_lens;
554   int            fd;
555   FILE           *file;
556 
557   PetscFunctionBegin;
558   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
559   ierr = PetscMalloc1((4+A->rmap->n),&col_lens);CHKERRQ(ierr);
560 
561   col_lens[0] = MAT_FILE_CLASSID;
562   col_lens[1] = A->rmap->n;
563   col_lens[2] = A->cmap->n;
564   col_lens[3] = a->nz;
565 
566   /* store lengths of each row and write (including header) to file */
567   for (i=0; i<A->rmap->n; i++) {
568     col_lens[4+i] = a->i[i+1] - a->i[i];
569   }
570   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
571   ierr = PetscFree(col_lens);CHKERRQ(ierr);
572 
573   /* store column indices (zero start index) */
574   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
575 
576   /* store nonzero values */
577   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
578 
579   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
580   if (file) {
581     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
582   }
583   PetscFunctionReturn(0);
584 }
585 
586 extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
587 
588 #undef __FUNCT__
589 #define __FUNCT__ "MatView_SeqAIJ_ASCII"
590 PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
591 {
592   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
593   PetscErrorCode    ierr;
594   PetscInt          i,j,m = A->rmap->n;
595   const char        *name;
596   PetscViewerFormat format;
597 
598   PetscFunctionBegin;
599   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
600   if (format == PETSC_VIEWER_ASCII_MATLAB) {
601     PetscInt nofinalvalue = 0;
602     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
603       /* Need a dummy value to ensure the dimension of the matrix. */
604       nofinalvalue = 1;
605     }
606     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
607     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
608     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
609 #if defined(PETSC_USE_COMPLEX)
610     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
611 #else
612     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
613 #endif
614     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
615 
616     for (i=0; i<m; i++) {
617       for (j=a->i[i]; j<a->i[i+1]; j++) {
618 #if defined(PETSC_USE_COMPLEX)
619         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",i+1,a->j[j]+1,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
620 #else
621         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
622 #endif
623       }
624     }
625     if (nofinalvalue) {
626 #if defined(PETSC_USE_COMPLEX)
627       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
628 #else
629       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
630 #endif
631     }
632     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
633     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
634     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
635   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
636     PetscFunctionReturn(0);
637   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
638     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
639     for (i=0; i<m; i++) {
640       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
641       for (j=a->i[i]; j<a->i[i+1]; j++) {
642 #if defined(PETSC_USE_COMPLEX)
643         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
644           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
645         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
646           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
647         } else if (PetscRealPart(a->a[j]) != 0.0) {
648           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
649         }
650 #else
651         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
652 #endif
653       }
654       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
655     }
656     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
657   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
658     PetscInt nzd=0,fshift=1,*sptr;
659     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
660     ierr = PetscMalloc1((m+1),&sptr);CHKERRQ(ierr);
661     for (i=0; i<m; i++) {
662       sptr[i] = nzd+1;
663       for (j=a->i[i]; j<a->i[i+1]; j++) {
664         if (a->j[j] >= i) {
665 #if defined(PETSC_USE_COMPLEX)
666           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
667 #else
668           if (a->a[j] != 0.0) nzd++;
669 #endif
670         }
671       }
672     }
673     sptr[m] = nzd+1;
674     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
675     for (i=0; i<m+1; i+=6) {
676       if (i+4<m) {
677         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);
678       } else if (i+3<m) {
679         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);
680       } else if (i+2<m) {
681         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
682       } else if (i+1<m) {
683         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
684       } else if (i<m) {
685         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
686       } else {
687         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
688       }
689     }
690     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
691     ierr = PetscFree(sptr);CHKERRQ(ierr);
692     for (i=0; i<m; i++) {
693       for (j=a->i[i]; j<a->i[i+1]; j++) {
694         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
695       }
696       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
697     }
698     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
699     for (i=0; i<m; i++) {
700       for (j=a->i[i]; j<a->i[i+1]; j++) {
701         if (a->j[j] >= i) {
702 #if defined(PETSC_USE_COMPLEX)
703           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
704             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
705           }
706 #else
707           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
708 #endif
709         }
710       }
711       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
712     }
713     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
714   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
715     PetscInt    cnt = 0,jcnt;
716     PetscScalar value;
717 #if defined(PETSC_USE_COMPLEX)
718     PetscBool   realonly = PETSC_TRUE;
719 
720     for (i=0; i<a->i[m]; i++) {
721       if (PetscImaginaryPart(a->a[i]) != 0.0) {
722         realonly = PETSC_FALSE;
723         break;
724       }
725     }
726 #endif
727 
728     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
729     for (i=0; i<m; i++) {
730       jcnt = 0;
731       for (j=0; j<A->cmap->n; j++) {
732         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
733           value = a->a[cnt++];
734           jcnt++;
735         } else {
736           value = 0.0;
737         }
738 #if defined(PETSC_USE_COMPLEX)
739         if (realonly) {
740           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
741         } else {
742           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
743         }
744 #else
745         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
746 #endif
747       }
748       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
749     }
750     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
751   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
752     PetscInt fshift=1;
753     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
754 #if defined(PETSC_USE_COMPLEX)
755     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
756 #else
757     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
758 #endif
759     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
760     for (i=0; i<m; i++) {
761       for (j=a->i[i]; j<a->i[i+1]; j++) {
762 #if defined(PETSC_USE_COMPLEX)
763         if (PetscImaginaryPart(a->a[j]) > 0.0) {
764           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
765         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
766           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g -%g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
767         } else {
768           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
769         }
770 #else
771         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
772 #endif
773       }
774     }
775     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
776   } else {
777     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
778     if (A->factortype) {
779       for (i=0; i<m; i++) {
780         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
781         /* L part */
782         for (j=a->i[i]; j<a->i[i+1]; j++) {
783 #if defined(PETSC_USE_COMPLEX)
784           if (PetscImaginaryPart(a->a[j]) > 0.0) {
785             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
786           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
787             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
788           } else {
789             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
790           }
791 #else
792           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
793 #endif
794         }
795         /* diagonal */
796         j = a->diag[i];
797 #if defined(PETSC_USE_COMPLEX)
798         if (PetscImaginaryPart(a->a[j]) > 0.0) {
799           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
800         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
801           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));CHKERRQ(ierr);
802         } else {
803           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
804         }
805 #else
806         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
807 #endif
808 
809         /* U part */
810         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
811 #if defined(PETSC_USE_COMPLEX)
812           if (PetscImaginaryPart(a->a[j]) > 0.0) {
813             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
814           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
815             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
816           } else {
817             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
818           }
819 #else
820           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
821 #endif
822         }
823         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
824       }
825     } else {
826       for (i=0; i<m; i++) {
827         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
828         for (j=a->i[i]; j<a->i[i+1]; j++) {
829 #if defined(PETSC_USE_COMPLEX)
830           if (PetscImaginaryPart(a->a[j]) > 0.0) {
831             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
832           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
833             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
834           } else {
835             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
836           }
837 #else
838           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
839 #endif
840         }
841         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
842       }
843     }
844     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
845   }
846   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
847   PetscFunctionReturn(0);
848 }
849 
850 #include <petscdraw.h>
851 #undef __FUNCT__
852 #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
853 PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
854 {
855   Mat               A  = (Mat) Aa;
856   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
857   PetscErrorCode    ierr;
858   PetscInt          i,j,m = A->rmap->n,color;
859   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
860   PetscViewer       viewer;
861   PetscViewerFormat format;
862 
863   PetscFunctionBegin;
864   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
865   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
866 
867   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
868   /* loop over matrix elements drawing boxes */
869 
870   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
871     /* Blue for negative, Cyan for zero and  Red for positive */
872     color = PETSC_DRAW_BLUE;
873     for (i=0; i<m; i++) {
874       y_l = m - i - 1.0; y_r = y_l + 1.0;
875       for (j=a->i[i]; j<a->i[i+1]; j++) {
876         x_l = a->j[j]; x_r = x_l + 1.0;
877         if (PetscRealPart(a->a[j]) >=  0.) continue;
878         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
879       }
880     }
881     color = PETSC_DRAW_CYAN;
882     for (i=0; i<m; i++) {
883       y_l = m - i - 1.0; y_r = y_l + 1.0;
884       for (j=a->i[i]; j<a->i[i+1]; j++) {
885         x_l = a->j[j]; x_r = x_l + 1.0;
886         if (a->a[j] !=  0.) continue;
887         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
888       }
889     }
890     color = PETSC_DRAW_RED;
891     for (i=0; i<m; i++) {
892       y_l = m - i - 1.0; y_r = y_l + 1.0;
893       for (j=a->i[i]; j<a->i[i+1]; j++) {
894         x_l = a->j[j]; x_r = x_l + 1.0;
895         if (PetscRealPart(a->a[j]) <=  0.) continue;
896         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
897       }
898     }
899   } else {
900     /* use contour shading to indicate magnitude of values */
901     /* first determine max of all nonzero values */
902     PetscInt  nz = a->nz,count;
903     PetscDraw popup;
904     PetscReal scale;
905 
906     for (i=0; i<nz; i++) {
907       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
908     }
909     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
910     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
911     if (popup) {
912       ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);
913     }
914     count = 0;
915     for (i=0; i<m; i++) {
916       y_l = m - i - 1.0; y_r = y_l + 1.0;
917       for (j=a->i[i]; j<a->i[i+1]; j++) {
918         x_l   = a->j[j]; x_r = x_l + 1.0;
919         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
920         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
921         count++;
922       }
923     }
924   }
925   PetscFunctionReturn(0);
926 }
927 
928 #include <petscdraw.h>
929 #undef __FUNCT__
930 #define __FUNCT__ "MatView_SeqAIJ_Draw"
931 PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
932 {
933   PetscErrorCode ierr;
934   PetscDraw      draw;
935   PetscReal      xr,yr,xl,yl,h,w;
936   PetscBool      isnull;
937 
938   PetscFunctionBegin;
939   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
940   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
941   if (isnull) PetscFunctionReturn(0);
942 
943   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
944   xr   = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
945   xr  += w;    yr += h;  xl = -w;     yl = -h;
946   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
947   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
948   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
949   PetscFunctionReturn(0);
950 }
951 
952 #undef __FUNCT__
953 #define __FUNCT__ "MatView_SeqAIJ"
954 PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
955 {
956   PetscErrorCode ierr;
957   PetscBool      iascii,isbinary,isdraw;
958 
959   PetscFunctionBegin;
960   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
961   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
962   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
963   if (iascii) {
964     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
965   } else if (isbinary) {
966     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
967   } else if (isdraw) {
968     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
969   }
970   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
971   PetscFunctionReturn(0);
972 }
973 
974 #undef __FUNCT__
975 #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
976 PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
977 {
978   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
979   PetscErrorCode ierr;
980   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
981   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
982   MatScalar      *aa    = a->a,*ap;
983   PetscReal      ratio  = 0.6;
984 
985   PetscFunctionBegin;
986   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
987 
988   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
989   for (i=1; i<m; i++) {
990     /* move each row back by the amount of empty slots (fshift) before it*/
991     fshift += imax[i-1] - ailen[i-1];
992     rmax    = PetscMax(rmax,ailen[i]);
993     if (fshift) {
994       ip = aj + ai[i];
995       ap = aa + ai[i];
996       N  = ailen[i];
997       for (j=0; j<N; j++) {
998         ip[j-fshift] = ip[j];
999         ap[j-fshift] = ap[j];
1000       }
1001     }
1002     ai[i] = ai[i-1] + ailen[i-1];
1003   }
1004   if (m) {
1005     fshift += imax[m-1] - ailen[m-1];
1006     ai[m]   = ai[m-1] + ailen[m-1];
1007   }
1008 
1009   /* reset ilen and imax for each row */
1010   a->nonzerorowcnt = 0;
1011   for (i=0; i<m; i++) {
1012     ailen[i] = imax[i] = ai[i+1] - ai[i];
1013     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1014   }
1015   a->nz = ai[m];
1016   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
1017 
1018   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1019   ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);CHKERRQ(ierr);
1020   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1021   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
1022 
1023   A->info.mallocs    += a->reallocs;
1024   a->reallocs         = 0;
1025   A->info.nz_unneeded = (PetscReal)fshift;
1026   a->rmax             = rmax;
1027 
1028   ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
1029   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1030   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1031   PetscFunctionReturn(0);
1032 }
1033 
1034 #undef __FUNCT__
1035 #define __FUNCT__ "MatRealPart_SeqAIJ"
1036 PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1037 {
1038   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1039   PetscInt       i,nz = a->nz;
1040   MatScalar      *aa = a->a;
1041   PetscErrorCode ierr;
1042 
1043   PetscFunctionBegin;
1044   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1045   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1046   PetscFunctionReturn(0);
1047 }
1048 
1049 #undef __FUNCT__
1050 #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
1051 PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1052 {
1053   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1054   PetscInt       i,nz = a->nz;
1055   MatScalar      *aa = a->a;
1056   PetscErrorCode ierr;
1057 
1058   PetscFunctionBegin;
1059   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1060   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1061   PetscFunctionReturn(0);
1062 }
1063 
1064 #if defined(PETSC_THREADCOMM_ACTIVE)
1065 PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
1066 {
1067   PetscErrorCode ierr;
1068   PetscInt       *trstarts=A->rmap->trstarts;
1069   PetscInt       n,start,end;
1070   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1071 
1072   start = trstarts[thread_id];
1073   end   = trstarts[thread_id+1];
1074   n     = a->i[end] - a->i[start];
1075   ierr  = PetscMemzero(a->a+a->i[start],n*sizeof(PetscScalar));CHKERRQ(ierr);
1076   return 0;
1077 }
1078 
1079 #undef __FUNCT__
1080 #define __FUNCT__ "MatZeroEntries_SeqAIJ"
1081 PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1082 {
1083   PetscErrorCode ierr;
1084 
1085   PetscFunctionBegin;
1086   ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
1087   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1088   PetscFunctionReturn(0);
1089 }
1090 #else
1091 #undef __FUNCT__
1092 #define __FUNCT__ "MatZeroEntries_SeqAIJ"
1093 PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1094 {
1095   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1096   PetscErrorCode ierr;
1097 
1098   PetscFunctionBegin;
1099   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1100   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1101   PetscFunctionReturn(0);
1102 }
1103 #endif
1104 
1105 extern PetscErrorCode MatDestroy_Redundant(Mat_Redundant **);
1106 
1107 #undef __FUNCT__
1108 #define __FUNCT__ "MatDestroy_SeqAIJ"
1109 PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1110 {
1111   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1112   PetscErrorCode ierr;
1113 
1114   PetscFunctionBegin;
1115 #if defined(PETSC_USE_LOG)
1116   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
1117 #endif
1118   ierr = MatDestroy_Redundant(&a->redundant);CHKERRQ(ierr);
1119   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1120   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1121   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1122   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1123   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
1124   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
1125   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
1126   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
1127   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
1128   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
1129   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1130   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
1131   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
1132   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
1133   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1134 
1135   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1136   ierr = PetscFree(A->data);CHKERRQ(ierr);
1137 
1138   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1139   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1140   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1141   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1142   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1143   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1144   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1145   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1146   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1147   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1148   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
1149   PetscFunctionReturn(0);
1150 }
1151 
1152 #undef __FUNCT__
1153 #define __FUNCT__ "MatSetOption_SeqAIJ"
1154 PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1155 {
1156   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1157   PetscErrorCode ierr;
1158 
1159   PetscFunctionBegin;
1160   switch (op) {
1161   case MAT_ROW_ORIENTED:
1162     a->roworiented = flg;
1163     break;
1164   case MAT_KEEP_NONZERO_PATTERN:
1165     a->keepnonzeropattern = flg;
1166     break;
1167   case MAT_NEW_NONZERO_LOCATIONS:
1168     a->nonew = (flg ? 0 : 1);
1169     break;
1170   case MAT_NEW_NONZERO_LOCATION_ERR:
1171     a->nonew = (flg ? -1 : 0);
1172     break;
1173   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1174     a->nonew = (flg ? -2 : 0);
1175     break;
1176   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1177     a->nounused = (flg ? -1 : 0);
1178     break;
1179   case MAT_IGNORE_ZERO_ENTRIES:
1180     a->ignorezeroentries = flg;
1181     break;
1182   case MAT_SPD:
1183   case MAT_SYMMETRIC:
1184   case MAT_STRUCTURALLY_SYMMETRIC:
1185   case MAT_HERMITIAN:
1186   case MAT_SYMMETRY_ETERNAL:
1187     /* These options are handled directly by MatSetOption() */
1188     break;
1189   case MAT_NEW_DIAGONALS:
1190   case MAT_IGNORE_OFF_PROC_ENTRIES:
1191   case MAT_USE_HASH_TABLE:
1192     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1193     break;
1194   case MAT_USE_INODES:
1195     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1196     break;
1197   default:
1198     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1199   }
1200   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
1201   PetscFunctionReturn(0);
1202 }
1203 
1204 #undef __FUNCT__
1205 #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1206 PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1207 {
1208   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1209   PetscErrorCode ierr;
1210   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
1211   PetscScalar    *aa=a->a,*x,zero=0.0;
1212 
1213   PetscFunctionBegin;
1214   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1215   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1216 
1217   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1218     PetscInt *diag=a->diag;
1219     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
1220     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1221     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
1222     PetscFunctionReturn(0);
1223   }
1224 
1225   ierr = VecSet(v,zero);CHKERRQ(ierr);
1226   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
1227   for (i=0; i<n; i++) {
1228     nz = ai[i+1] - ai[i];
1229     if (!nz) x[i] = 0.0;
1230     for (j=ai[i]; j<ai[i+1]; j++) {
1231       if (aj[j] == i) {
1232         x[i] = aa[j];
1233         break;
1234       }
1235     }
1236   }
1237   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
1238   PetscFunctionReturn(0);
1239 }
1240 
1241 #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1242 #undef __FUNCT__
1243 #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1244 PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1245 {
1246   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1247   PetscScalar    *x,*y;
1248   PetscErrorCode ierr;
1249   PetscInt       m = A->rmap->n;
1250 #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1251   MatScalar         *v;
1252   PetscScalar       alpha;
1253   PetscInt          n,i,j,*idx,*ii,*ridx=NULL;
1254   Mat_CompressedRow cprow    = a->compressedrow;
1255   PetscBool         usecprow = cprow.use;
1256 #endif
1257 
1258   PetscFunctionBegin;
1259   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1260   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1261   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1262 
1263 #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1264   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1265 #else
1266   if (usecprow) {
1267     m    = cprow.nrows;
1268     ii   = cprow.i;
1269     ridx = cprow.rindex;
1270   } else {
1271     ii = a->i;
1272   }
1273   for (i=0; i<m; i++) {
1274     idx = a->j + ii[i];
1275     v   = a->a + ii[i];
1276     n   = ii[i+1] - ii[i];
1277     if (usecprow) {
1278       alpha = x[ridx[i]];
1279     } else {
1280       alpha = x[i];
1281     }
1282     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1283   }
1284 #endif
1285   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1286   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1287   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1288   PetscFunctionReturn(0);
1289 }
1290 
1291 #undef __FUNCT__
1292 #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1293 PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1294 {
1295   PetscErrorCode ierr;
1296 
1297   PetscFunctionBegin;
1298   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
1299   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
1300   PetscFunctionReturn(0);
1301 }
1302 
1303 #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1304 #if defined(PETSC_THREADCOMM_ACTIVE)
1305 PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
1306 {
1307   PetscErrorCode    ierr;
1308   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1309   PetscScalar       *y;
1310   const PetscScalar *x;
1311   const MatScalar   *aa;
1312   PetscInt          *trstarts=A->rmap->trstarts;
1313   PetscInt          n,start,end,i;
1314   const PetscInt    *aj,*ai;
1315   PetscScalar       sum;
1316 
1317   ierr  = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1318   ierr  = VecGetArray(yy,&y);CHKERRQ(ierr);
1319   start = trstarts[thread_id];
1320   end   = trstarts[thread_id+1];
1321   aj    = a->j;
1322   aa    = a->a;
1323   ai    = a->i;
1324   for (i=start; i<end; i++) {
1325     n   = ai[i+1] - ai[i];
1326     aj  = a->j + ai[i];
1327     aa  = a->a + ai[i];
1328     sum = 0.0;
1329     PetscSparseDensePlusDot(sum,x,aa,aj,n);
1330     y[i] = sum;
1331   }
1332   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1333   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1334   return 0;
1335 }
1336 
1337 #undef __FUNCT__
1338 #define __FUNCT__ "MatMult_SeqAIJ"
1339 PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1340 {
1341   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1342   PetscScalar       *y;
1343   const PetscScalar *x;
1344   const MatScalar   *aa;
1345   PetscErrorCode    ierr;
1346   PetscInt          m=A->rmap->n;
1347   const PetscInt    *aj,*ii,*ridx=NULL;
1348   PetscInt          n,i;
1349   PetscScalar       sum;
1350   PetscBool         usecprow=a->compressedrow.use;
1351 
1352 #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1353 #pragma disjoint(*x,*y,*aa)
1354 #endif
1355 
1356   PetscFunctionBegin;
1357   aj = a->j;
1358   aa = a->a;
1359   ii = a->i;
1360   if (usecprow) { /* use compressed row format */
1361     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1362     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1363     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1364     m    = a->compressedrow.nrows;
1365     ii   = a->compressedrow.i;
1366     ridx = a->compressedrow.rindex;
1367     for (i=0; i<m; i++) {
1368       n           = ii[i+1] - ii[i];
1369       aj          = a->j + ii[i];
1370       aa          = a->a + ii[i];
1371       sum         = 0.0;
1372       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1373       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1374       y[*ridx++] = sum;
1375     }
1376     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1377     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1378   } else { /* do not use compressed row format */
1379 #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1380     fortranmultaij_(&m,x,ii,aj,aa,y);
1381 #else
1382     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
1383 #endif
1384   }
1385   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
1386   PetscFunctionReturn(0);
1387 }
1388 #else
1389 #undef __FUNCT__
1390 #define __FUNCT__ "MatMult_SeqAIJ"
1391 PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1392 {
1393   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1394   PetscScalar       *y;
1395   const PetscScalar *x;
1396   const MatScalar   *aa;
1397   PetscErrorCode    ierr;
1398   PetscInt          m=A->rmap->n;
1399   const PetscInt    *aj,*ii,*ridx=NULL;
1400   PetscInt          n,i;
1401   PetscScalar       sum;
1402   PetscBool         usecprow=a->compressedrow.use;
1403 
1404 #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1405 #pragma disjoint(*x,*y,*aa)
1406 #endif
1407 
1408   PetscFunctionBegin;
1409   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1410   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1411   aj   = a->j;
1412   aa   = a->a;
1413   ii   = a->i;
1414   if (usecprow) { /* use compressed row format */
1415     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1416     m    = a->compressedrow.nrows;
1417     ii   = a->compressedrow.i;
1418     ridx = a->compressedrow.rindex;
1419     for (i=0; i<m; i++) {
1420       n           = ii[i+1] - ii[i];
1421       aj          = a->j + ii[i];
1422       aa          = a->a + ii[i];
1423       sum         = 0.0;
1424       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1425       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1426       y[*ridx++] = sum;
1427     }
1428   } else { /* do not use compressed row format */
1429 #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1430     fortranmultaij_(&m,x,ii,aj,aa,y);
1431 #else
1432 #if defined(PETSC_THREADCOMM_ACTIVE)
1433     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
1434 #else
1435     for (i=0; i<m; i++) {
1436       n           = ii[i+1] - ii[i];
1437       aj          = a->j + ii[i];
1438       aa          = a->a + ii[i];
1439       sum         = 0.0;
1440       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1441       y[i] = sum;
1442     }
1443 #endif
1444 #endif
1445   }
1446   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
1447   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1448   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1449   PetscFunctionReturn(0);
1450 }
1451 #endif
1452 
1453 #undef __FUNCT__
1454 #define __FUNCT__ "MatMultMax_SeqAIJ"
1455 PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1456 {
1457   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1458   PetscScalar       *y;
1459   const PetscScalar *x;
1460   const MatScalar   *aa;
1461   PetscErrorCode    ierr;
1462   PetscInt          m=A->rmap->n;
1463   const PetscInt    *aj,*ii,*ridx=NULL;
1464   PetscInt          n,i,nonzerorow=0;
1465   PetscScalar       sum;
1466   PetscBool         usecprow=a->compressedrow.use;
1467 
1468 #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1469 #pragma disjoint(*x,*y,*aa)
1470 #endif
1471 
1472   PetscFunctionBegin;
1473   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1474   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1475   aj   = a->j;
1476   aa   = a->a;
1477   ii   = a->i;
1478   if (usecprow) { /* use compressed row format */
1479     m    = a->compressedrow.nrows;
1480     ii   = a->compressedrow.i;
1481     ridx = a->compressedrow.rindex;
1482     for (i=0; i<m; i++) {
1483       n           = ii[i+1] - ii[i];
1484       aj          = a->j + ii[i];
1485       aa          = a->a + ii[i];
1486       sum         = 0.0;
1487       nonzerorow += (n>0);
1488       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1489       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1490       y[*ridx++] = sum;
1491     }
1492   } else { /* do not use compressed row format */
1493     for (i=0; i<m; i++) {
1494       n           = ii[i+1] - ii[i];
1495       aj          = a->j + ii[i];
1496       aa          = a->a + ii[i];
1497       sum         = 0.0;
1498       nonzerorow += (n>0);
1499       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1500       y[i] = sum;
1501     }
1502   }
1503   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1504   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1505   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1506   PetscFunctionReturn(0);
1507 }
1508 
1509 #undef __FUNCT__
1510 #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1511 PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1512 {
1513   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1514   PetscScalar       *y,*z;
1515   const PetscScalar *x;
1516   const MatScalar   *aa;
1517   PetscErrorCode    ierr;
1518   PetscInt          m = A->rmap->n,*aj,*ii;
1519   PetscInt          n,i,*ridx=NULL;
1520   PetscScalar       sum;
1521   PetscBool         usecprow=a->compressedrow.use;
1522 
1523   PetscFunctionBegin;
1524   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1525   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1526   if (zz != yy) {
1527     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
1528   } else {
1529     z = y;
1530   }
1531 
1532   aj = a->j;
1533   aa = a->a;
1534   ii = a->i;
1535   if (usecprow) { /* use compressed row format */
1536     if (zz != yy) {
1537       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1538     }
1539     m    = a->compressedrow.nrows;
1540     ii   = a->compressedrow.i;
1541     ridx = a->compressedrow.rindex;
1542     for (i=0; i<m; i++) {
1543       n   = ii[i+1] - ii[i];
1544       aj  = a->j + ii[i];
1545       aa  = a->a + ii[i];
1546       sum = y[*ridx];
1547       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1548       z[*ridx++] = sum;
1549     }
1550   } else { /* do not use compressed row format */
1551     for (i=0; i<m; i++) {
1552       n   = ii[i+1] - ii[i];
1553       aj  = a->j + ii[i];
1554       aa  = a->a + ii[i];
1555       sum = y[i];
1556       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1557       z[i] = sum;
1558     }
1559   }
1560   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1561   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1562   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1563   if (zz != yy) {
1564     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
1565   }
1566   PetscFunctionReturn(0);
1567 }
1568 
1569 #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1570 #undef __FUNCT__
1571 #define __FUNCT__ "MatMultAdd_SeqAIJ"
1572 PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1573 {
1574   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1575   PetscScalar       *y,*z;
1576   const PetscScalar *x;
1577   const MatScalar   *aa;
1578   PetscErrorCode    ierr;
1579   PetscInt          m = A->rmap->n,*aj,*ii;
1580   PetscInt          n,i,*ridx=NULL;
1581   PetscScalar       sum;
1582   PetscBool         usecprow=a->compressedrow.use;
1583 
1584   PetscFunctionBegin;
1585   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1586   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1587   if (zz != yy) {
1588     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
1589   } else {
1590     z = y;
1591   }
1592 
1593   aj = a->j;
1594   aa = a->a;
1595   ii = a->i;
1596   if (usecprow) { /* use compressed row format */
1597     if (zz != yy) {
1598       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1599     }
1600     m    = a->compressedrow.nrows;
1601     ii   = a->compressedrow.i;
1602     ridx = a->compressedrow.rindex;
1603     for (i=0; i<m; i++) {
1604       n   = ii[i+1] - ii[i];
1605       aj  = a->j + ii[i];
1606       aa  = a->a + ii[i];
1607       sum = y[*ridx];
1608       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1609       z[*ridx++] = sum;
1610     }
1611   } else { /* do not use compressed row format */
1612 #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1613     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1614 #else
1615     for (i=0; i<m; i++) {
1616       n   = ii[i+1] - ii[i];
1617       aj  = a->j + ii[i];
1618       aa  = a->a + ii[i];
1619       sum = y[i];
1620       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1621       z[i] = sum;
1622     }
1623 #endif
1624   }
1625   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1626   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1627   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1628   if (zz != yy) {
1629     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
1630   }
1631 #if defined(PETSC_HAVE_CUSP)
1632   /*
1633   ierr = VecView(xx,0);CHKERRQ(ierr);
1634   ierr = VecView(zz,0);CHKERRQ(ierr);
1635   ierr = MatView(A,0);CHKERRQ(ierr);
1636   */
1637 #endif
1638   PetscFunctionReturn(0);
1639 }
1640 
1641 /*
1642      Adds diagonal pointers to sparse matrix structure.
1643 */
1644 #undef __FUNCT__
1645 #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1646 PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1647 {
1648   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1649   PetscErrorCode ierr;
1650   PetscInt       i,j,m = A->rmap->n;
1651 
1652   PetscFunctionBegin;
1653   if (!a->diag) {
1654     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
1655     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
1656   }
1657   for (i=0; i<A->rmap->n; i++) {
1658     a->diag[i] = a->i[i+1];
1659     for (j=a->i[i]; j<a->i[i+1]; j++) {
1660       if (a->j[j] == i) {
1661         a->diag[i] = j;
1662         break;
1663       }
1664     }
1665   }
1666   PetscFunctionReturn(0);
1667 }
1668 
1669 /*
1670      Checks for missing diagonals
1671 */
1672 #undef __FUNCT__
1673 #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1674 PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1675 {
1676   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1677   PetscInt   *diag,*ii = a->i,i;
1678 
1679   PetscFunctionBegin;
1680   *missing = PETSC_FALSE;
1681   if (A->rmap->n > 0 && !ii) {
1682     *missing = PETSC_TRUE;
1683     if (d) *d = 0;
1684     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
1685   } else {
1686     diag = a->diag;
1687     for (i=0; i<A->rmap->n; i++) {
1688       if (diag[i] >= ii[i+1]) {
1689         *missing = PETSC_TRUE;
1690         if (d) *d = i;
1691         PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1692         break;
1693       }
1694     }
1695   }
1696   PetscFunctionReturn(0);
1697 }
1698 
1699 #undef __FUNCT__
1700 #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
1701 PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1702 {
1703   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
1704   PetscErrorCode ierr;
1705   PetscInt       i,*diag,m = A->rmap->n;
1706   MatScalar      *v = a->a;
1707   PetscScalar    *idiag,*mdiag;
1708 
1709   PetscFunctionBegin;
1710   if (a->idiagvalid) PetscFunctionReturn(0);
1711   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1712   diag = a->diag;
1713   if (!a->idiag) {
1714     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
1715     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
1716     v    = a->a;
1717   }
1718   mdiag = a->mdiag;
1719   idiag = a->idiag;
1720 
1721   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
1722     for (i=0; i<m; i++) {
1723       mdiag[i] = v[diag[i]];
1724       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1725       idiag[i] = 1.0/v[diag[i]];
1726     }
1727     ierr = PetscLogFlops(m);CHKERRQ(ierr);
1728   } else {
1729     for (i=0; i<m; i++) {
1730       mdiag[i] = v[diag[i]];
1731       idiag[i] = omega/(fshift + v[diag[i]]);
1732     }
1733     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
1734   }
1735   a->idiagvalid = PETSC_TRUE;
1736   PetscFunctionReturn(0);
1737 }
1738 
1739 #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1740 #undef __FUNCT__
1741 #define __FUNCT__ "MatSOR_SeqAIJ"
1742 PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1743 {
1744   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1745   PetscScalar       *x,d,sum,*t,scale;
1746   const MatScalar   *v = a->a,*idiag=0,*mdiag;
1747   const PetscScalar *b, *bs,*xb, *ts;
1748   PetscErrorCode    ierr;
1749   PetscInt          n = A->cmap->n,m = A->rmap->n,i;
1750   const PetscInt    *idx,*diag;
1751 
1752   PetscFunctionBegin;
1753   its = its*lits;
1754 
1755   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1756   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
1757   a->fshift = fshift;
1758   a->omega  = omega;
1759 
1760   diag  = a->diag;
1761   t     = a->ssor_work;
1762   idiag = a->idiag;
1763   mdiag = a->mdiag;
1764 
1765   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1766   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1767   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1768   if (flag == SOR_APPLY_UPPER) {
1769     /* apply (U + D/omega) to the vector */
1770     bs = b;
1771     for (i=0; i<m; i++) {
1772       d   = fshift + mdiag[i];
1773       n   = a->i[i+1] - diag[i] - 1;
1774       idx = a->j + diag[i] + 1;
1775       v   = a->a + diag[i] + 1;
1776       sum = b[i]*d/omega;
1777       PetscSparseDensePlusDot(sum,bs,v,idx,n);
1778       x[i] = sum;
1779     }
1780     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1781     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1782     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
1783     PetscFunctionReturn(0);
1784   }
1785 
1786   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
1787   else if (flag & SOR_EISENSTAT) {
1788     /* Let  A = L + U + D; where L is lower trianglar,
1789     U is upper triangular, E = D/omega; This routine applies
1790 
1791             (L + E)^{-1} A (U + E)^{-1}
1792 
1793     to a vector efficiently using Eisenstat's trick.
1794     */
1795     scale = (2.0/omega) - 1.0;
1796 
1797     /*  x = (E + U)^{-1} b */
1798     for (i=m-1; i>=0; i--) {
1799       n   = a->i[i+1] - diag[i] - 1;
1800       idx = a->j + diag[i] + 1;
1801       v   = a->a + diag[i] + 1;
1802       sum = b[i];
1803       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1804       x[i] = sum*idiag[i];
1805     }
1806 
1807     /*  t = b - (2*E - D)x */
1808     v = a->a;
1809     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
1810 
1811     /*  t = (E + L)^{-1}t */
1812     ts   = t;
1813     diag = a->diag;
1814     for (i=0; i<m; i++) {
1815       n   = diag[i] - a->i[i];
1816       idx = a->j + a->i[i];
1817       v   = a->a + a->i[i];
1818       sum = t[i];
1819       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1820       t[i] = sum*idiag[i];
1821       /*  x = x + t */
1822       x[i] += t[i];
1823     }
1824 
1825     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
1826     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1827     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1828     PetscFunctionReturn(0);
1829   }
1830   if (flag & SOR_ZERO_INITIAL_GUESS) {
1831     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1832       for (i=0; i<m; i++) {
1833         n   = diag[i] - a->i[i];
1834         idx = a->j + a->i[i];
1835         v   = a->a + a->i[i];
1836         sum = b[i];
1837         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1838         t[i] = sum;
1839         x[i] = sum*idiag[i];
1840       }
1841       xb   = t;
1842       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
1843     } else xb = b;
1844     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1845       for (i=m-1; i>=0; i--) {
1846         n   = a->i[i+1] - diag[i] - 1;
1847         idx = a->j + diag[i] + 1;
1848         v   = a->a + diag[i] + 1;
1849         sum = xb[i];
1850         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1851         if (xb == b) {
1852           x[i] = sum*idiag[i];
1853         } else {
1854           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1855         }
1856       }
1857       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1858     }
1859     its--;
1860   }
1861   while (its--) {
1862     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1863       for (i=0; i<m; i++) {
1864         /* lower */
1865         n   = diag[i] - a->i[i];
1866         idx = a->j + a->i[i];
1867         v   = a->a + a->i[i];
1868         sum = b[i];
1869         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1870         t[i] = sum;             /* save application of the lower-triangular part */
1871         /* upper */
1872         n   = a->i[i+1] - diag[i] - 1;
1873         idx = a->j + diag[i] + 1;
1874         v   = a->a + diag[i] + 1;
1875         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1876         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1877       }
1878       xb   = t;
1879       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1880     } else xb = b;
1881     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1882       for (i=m-1; i>=0; i--) {
1883         sum = xb[i];
1884         if (xb == b) {
1885           /* whole matrix (no checkpointing available) */
1886           n   = a->i[i+1] - a->i[i];
1887           idx = a->j + a->i[i];
1888           v   = a->a + a->i[i];
1889           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1890           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1891         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1892           n   = a->i[i+1] - diag[i] - 1;
1893           idx = a->j + diag[i] + 1;
1894           v   = a->a + diag[i] + 1;
1895           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1896           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1897         }
1898       }
1899       if (xb == b) {
1900         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1901       } else {
1902         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1903       }
1904     }
1905   }
1906   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1907   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1908   PetscFunctionReturn(0);
1909 }
1910 
1911 
1912 #undef __FUNCT__
1913 #define __FUNCT__ "MatGetInfo_SeqAIJ"
1914 PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
1915 {
1916   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1917 
1918   PetscFunctionBegin;
1919   info->block_size   = 1.0;
1920   info->nz_allocated = (double)a->maxnz;
1921   info->nz_used      = (double)a->nz;
1922   info->nz_unneeded  = (double)(a->maxnz - a->nz);
1923   info->assemblies   = (double)A->num_ass;
1924   info->mallocs      = (double)A->info.mallocs;
1925   info->memory       = ((PetscObject)A)->mem;
1926   if (A->factortype) {
1927     info->fill_ratio_given  = A->info.fill_ratio_given;
1928     info->fill_ratio_needed = A->info.fill_ratio_needed;
1929     info->factor_mallocs    = A->info.factor_mallocs;
1930   } else {
1931     info->fill_ratio_given  = 0;
1932     info->fill_ratio_needed = 0;
1933     info->factor_mallocs    = 0;
1934   }
1935   PetscFunctionReturn(0);
1936 }
1937 
1938 #undef __FUNCT__
1939 #define __FUNCT__ "MatZeroRows_SeqAIJ"
1940 PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1941 {
1942   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1943   PetscInt          i,m = A->rmap->n - 1,d = 0;
1944   PetscErrorCode    ierr;
1945   const PetscScalar *xx;
1946   PetscScalar       *bb;
1947   PetscBool         missing;
1948 
1949   PetscFunctionBegin;
1950   if (x && b) {
1951     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
1952     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
1953     for (i=0; i<N; i++) {
1954       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1955       bb[rows[i]] = diag*xx[rows[i]];
1956     }
1957     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
1958     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
1959   }
1960 
1961   if (a->keepnonzeropattern) {
1962     for (i=0; i<N; i++) {
1963       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1964       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1965     }
1966     if (diag != 0.0) {
1967       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1968       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1969       for (i=0; i<N; i++) {
1970         a->a[a->diag[rows[i]]] = diag;
1971       }
1972     }
1973   } else {
1974     if (diag != 0.0) {
1975       for (i=0; i<N; i++) {
1976         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1977         if (a->ilen[rows[i]] > 0) {
1978           a->ilen[rows[i]]    = 1;
1979           a->a[a->i[rows[i]]] = diag;
1980           a->j[a->i[rows[i]]] = rows[i];
1981         } else { /* in case row was completely empty */
1982           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
1983         }
1984       }
1985     } else {
1986       for (i=0; i<N; i++) {
1987         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1988         a->ilen[rows[i]] = 0;
1989       }
1990     }
1991     A->nonzerostate++;
1992   }
1993   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1994   PetscFunctionReturn(0);
1995 }
1996 
1997 #undef __FUNCT__
1998 #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
1999 PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2000 {
2001   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2002   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
2003   PetscErrorCode    ierr;
2004   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
2005   const PetscScalar *xx;
2006   PetscScalar       *bb;
2007 
2008   PetscFunctionBegin;
2009   if (x && b) {
2010     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
2011     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
2012     vecs = PETSC_TRUE;
2013   }
2014   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
2015   for (i=0; i<N; i++) {
2016     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2017     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
2018 
2019     zeroed[rows[i]] = PETSC_TRUE;
2020   }
2021   for (i=0; i<A->rmap->n; i++) {
2022     if (!zeroed[i]) {
2023       for (j=a->i[i]; j<a->i[i+1]; j++) {
2024         if (zeroed[a->j[j]]) {
2025           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
2026           a->a[j] = 0.0;
2027         }
2028       }
2029     } else if (vecs) bb[i] = diag*xx[i];
2030   }
2031   if (x && b) {
2032     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
2033     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
2034   }
2035   ierr = PetscFree(zeroed);CHKERRQ(ierr);
2036   if (diag != 0.0) {
2037     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
2038     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
2039     for (i=0; i<N; i++) {
2040       a->a[a->diag[rows[i]]] = diag;
2041     }
2042   }
2043   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2044   PetscFunctionReturn(0);
2045 }
2046 
2047 #undef __FUNCT__
2048 #define __FUNCT__ "MatGetRow_SeqAIJ"
2049 PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2050 {
2051   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2052   PetscInt   *itmp;
2053 
2054   PetscFunctionBegin;
2055   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
2056 
2057   *nz = a->i[row+1] - a->i[row];
2058   if (v) *v = a->a + a->i[row];
2059   if (idx) {
2060     itmp = a->j + a->i[row];
2061     if (*nz) *idx = itmp;
2062     else *idx = 0;
2063   }
2064   PetscFunctionReturn(0);
2065 }
2066 
2067 /* remove this function? */
2068 #undef __FUNCT__
2069 #define __FUNCT__ "MatRestoreRow_SeqAIJ"
2070 PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2071 {
2072   PetscFunctionBegin;
2073   PetscFunctionReturn(0);
2074 }
2075 
2076 #undef __FUNCT__
2077 #define __FUNCT__ "MatNorm_SeqAIJ"
2078 PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2079 {
2080   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
2081   MatScalar      *v  = a->a;
2082   PetscReal      sum = 0.0;
2083   PetscErrorCode ierr;
2084   PetscInt       i,j;
2085 
2086   PetscFunctionBegin;
2087   if (type == NORM_FROBENIUS) {
2088     for (i=0; i<a->nz; i++) {
2089       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
2090     }
2091     *nrm = PetscSqrtReal(sum);
2092   } else if (type == NORM_1) {
2093     PetscReal *tmp;
2094     PetscInt  *jj = a->j;
2095     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
2096     *nrm = 0.0;
2097     for (j=0; j<a->nz; j++) {
2098       tmp[*jj++] += PetscAbsScalar(*v);  v++;
2099     }
2100     for (j=0; j<A->cmap->n; j++) {
2101       if (tmp[j] > *nrm) *nrm = tmp[j];
2102     }
2103     ierr = PetscFree(tmp);CHKERRQ(ierr);
2104   } else if (type == NORM_INFINITY) {
2105     *nrm = 0.0;
2106     for (j=0; j<A->rmap->n; j++) {
2107       v   = a->a + a->i[j];
2108       sum = 0.0;
2109       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2110         sum += PetscAbsScalar(*v); v++;
2111       }
2112       if (sum > *nrm) *nrm = sum;
2113     }
2114   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
2115   PetscFunctionReturn(0);
2116 }
2117 
2118 /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
2119 #undef __FUNCT__
2120 #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
2121 PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
2122 {
2123   PetscErrorCode ierr;
2124   PetscInt       i,j,anzj;
2125   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
2126   PetscInt       an=A->cmap->N,am=A->rmap->N;
2127   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
2128 
2129   PetscFunctionBegin;
2130   /* Allocate space for symbolic transpose info and work array */
2131   ierr = PetscCalloc1((an+1),&ati);CHKERRQ(ierr);
2132   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2133   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
2134 
2135   /* Walk through aj and count ## of non-zeros in each row of A^T. */
2136   /* Note: offset by 1 for fast conversion into csr format. */
2137   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
2138   /* Form ati for csr format of A^T. */
2139   for (i=0;i<an;i++) ati[i+1] += ati[i];
2140 
2141   /* Copy ati into atfill so we have locations of the next free space in atj */
2142   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
2143 
2144   /* Walk through A row-wise and mark nonzero entries of A^T. */
2145   for (i=0;i<am;i++) {
2146     anzj = ai[i+1] - ai[i];
2147     for (j=0;j<anzj;j++) {
2148       atj[atfill[*aj]] = i;
2149       atfill[*aj++]   += 1;
2150     }
2151   }
2152 
2153   /* Clean up temporary space and complete requests. */
2154   ierr = PetscFree(atfill);CHKERRQ(ierr);
2155   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
2156   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2157 
2158   b          = (Mat_SeqAIJ*)((*B)->data);
2159   b->free_a  = PETSC_FALSE;
2160   b->free_ij = PETSC_TRUE;
2161   b->nonew   = 0;
2162   PetscFunctionReturn(0);
2163 }
2164 
2165 #undef __FUNCT__
2166 #define __FUNCT__ "MatTranspose_SeqAIJ"
2167 PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
2168 {
2169   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2170   Mat            C;
2171   PetscErrorCode ierr;
2172   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
2173   MatScalar      *array = a->a;
2174 
2175   PetscFunctionBegin;
2176   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
2177 
2178   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
2179     ierr = PetscCalloc1((1+A->cmap->n),&col);CHKERRQ(ierr);
2180 
2181     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2182     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2183     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
2184     ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2185     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2186     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2187     ierr = PetscFree(col);CHKERRQ(ierr);
2188   } else {
2189     C = *B;
2190   }
2191 
2192   for (i=0; i<m; i++) {
2193     len    = ai[i+1]-ai[i];
2194     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2195     array += len;
2196     aj    += len;
2197   }
2198   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2199   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2200 
2201   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2202     *B = C;
2203   } else {
2204     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
2205   }
2206   PetscFunctionReturn(0);
2207 }
2208 
2209 #undef __FUNCT__
2210 #define __FUNCT__ "MatIsTranspose_SeqAIJ"
2211 PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2212 {
2213   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
2214   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2215   MatScalar      *va,*vb;
2216   PetscErrorCode ierr;
2217   PetscInt       ma,na,mb,nb, i;
2218 
2219   PetscFunctionBegin;
2220   bij = (Mat_SeqAIJ*) B->data;
2221 
2222   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2223   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
2224   if (ma!=nb || na!=mb) {
2225     *f = PETSC_FALSE;
2226     PetscFunctionReturn(0);
2227   }
2228   aii  = aij->i; bii = bij->i;
2229   adx  = aij->j; bdx = bij->j;
2230   va   = aij->a; vb = bij->a;
2231   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2232   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2233   for (i=0; i<ma; i++) aptr[i] = aii[i];
2234   for (i=0; i<mb; i++) bptr[i] = bii[i];
2235 
2236   *f = PETSC_TRUE;
2237   for (i=0; i<ma; i++) {
2238     while (aptr[i]<aii[i+1]) {
2239       PetscInt    idc,idr;
2240       PetscScalar vc,vr;
2241       /* column/row index/value */
2242       idc = adx[aptr[i]];
2243       idr = bdx[bptr[idc]];
2244       vc  = va[aptr[i]];
2245       vr  = vb[bptr[idc]];
2246       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
2247         *f = PETSC_FALSE;
2248         goto done;
2249       } else {
2250         aptr[i]++;
2251         if (B || i!=idc) bptr[idc]++;
2252       }
2253     }
2254   }
2255 done:
2256   ierr = PetscFree(aptr);CHKERRQ(ierr);
2257   ierr = PetscFree(bptr);CHKERRQ(ierr);
2258   PetscFunctionReturn(0);
2259 }
2260 
2261 #undef __FUNCT__
2262 #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
2263 PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2264 {
2265   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
2266   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2267   MatScalar      *va,*vb;
2268   PetscErrorCode ierr;
2269   PetscInt       ma,na,mb,nb, i;
2270 
2271   PetscFunctionBegin;
2272   bij = (Mat_SeqAIJ*) B->data;
2273 
2274   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2275   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
2276   if (ma!=nb || na!=mb) {
2277     *f = PETSC_FALSE;
2278     PetscFunctionReturn(0);
2279   }
2280   aii  = aij->i; bii = bij->i;
2281   adx  = aij->j; bdx = bij->j;
2282   va   = aij->a; vb = bij->a;
2283   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2284   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2285   for (i=0; i<ma; i++) aptr[i] = aii[i];
2286   for (i=0; i<mb; i++) bptr[i] = bii[i];
2287 
2288   *f = PETSC_TRUE;
2289   for (i=0; i<ma; i++) {
2290     while (aptr[i]<aii[i+1]) {
2291       PetscInt    idc,idr;
2292       PetscScalar vc,vr;
2293       /* column/row index/value */
2294       idc = adx[aptr[i]];
2295       idr = bdx[bptr[idc]];
2296       vc  = va[aptr[i]];
2297       vr  = vb[bptr[idc]];
2298       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
2299         *f = PETSC_FALSE;
2300         goto done;
2301       } else {
2302         aptr[i]++;
2303         if (B || i!=idc) bptr[idc]++;
2304       }
2305     }
2306   }
2307 done:
2308   ierr = PetscFree(aptr);CHKERRQ(ierr);
2309   ierr = PetscFree(bptr);CHKERRQ(ierr);
2310   PetscFunctionReturn(0);
2311 }
2312 
2313 #undef __FUNCT__
2314 #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2315 PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2316 {
2317   PetscErrorCode ierr;
2318 
2319   PetscFunctionBegin;
2320   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
2321   PetscFunctionReturn(0);
2322 }
2323 
2324 #undef __FUNCT__
2325 #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2326 PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2327 {
2328   PetscErrorCode ierr;
2329 
2330   PetscFunctionBegin;
2331   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
2332   PetscFunctionReturn(0);
2333 }
2334 
2335 #undef __FUNCT__
2336 #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2337 PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2338 {
2339   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2340   PetscScalar    *l,*r,x;
2341   MatScalar      *v;
2342   PetscErrorCode ierr;
2343   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
2344 
2345   PetscFunctionBegin;
2346   if (ll) {
2347     /* The local size is used so that VecMPI can be passed to this routine
2348        by MatDiagonalScale_MPIAIJ */
2349     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2350     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2351     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2352     v    = a->a;
2353     for (i=0; i<m; i++) {
2354       x = l[i];
2355       M = a->i[i+1] - a->i[i];
2356       for (j=0; j<M; j++) (*v++) *= x;
2357     }
2358     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2359     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
2360   }
2361   if (rr) {
2362     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2363     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2364     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2365     v    = a->a; jj = a->j;
2366     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2367     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2368     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
2369   }
2370   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
2371   PetscFunctionReturn(0);
2372 }
2373 
2374 #undef __FUNCT__
2375 #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
2376 PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2377 {
2378   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
2379   PetscErrorCode ierr;
2380   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2381   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2382   const PetscInt *irow,*icol;
2383   PetscInt       nrows,ncols;
2384   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2385   MatScalar      *a_new,*mat_a;
2386   Mat            C;
2387   PetscBool      stride,sorted;
2388 
2389   PetscFunctionBegin;
2390   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2391   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
2392   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2393   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
2394 
2395   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2396   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2397   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
2398 
2399   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2400   if (stride) {
2401     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2402   } else {
2403     first = 0;
2404     step  = 0;
2405   }
2406   if (stride && step == 1) {
2407     /* special case of contiguous rows */
2408     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
2409     /* loop over new rows determining lens and starting points */
2410     for (i=0; i<nrows; i++) {
2411       kstart = ai[irow[i]];
2412       kend   = kstart + ailen[irow[i]];
2413       for (k=kstart; k<kend; k++) {
2414         if (aj[k] >= first) {
2415           starts[i] = k;
2416           break;
2417         }
2418       }
2419       sum = 0;
2420       while (k < kend) {
2421         if (aj[k++] >= first+ncols) break;
2422         sum++;
2423       }
2424       lens[i] = sum;
2425     }
2426     /* create submatrix */
2427     if (scall == MAT_REUSE_MATRIX) {
2428       PetscInt n_cols,n_rows;
2429       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2430       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2431       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
2432       C    = *B;
2433     } else {
2434       PetscInt rbs,cbs;
2435       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2436       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2437       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
2438       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
2439       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
2440       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2441       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
2442     }
2443     c = (Mat_SeqAIJ*)C->data;
2444 
2445     /* loop over rows inserting into submatrix */
2446     a_new = c->a;
2447     j_new = c->j;
2448     i_new = c->i;
2449 
2450     for (i=0; i<nrows; i++) {
2451       ii    = starts[i];
2452       lensi = lens[i];
2453       for (k=0; k<lensi; k++) {
2454         *j_new++ = aj[ii+k] - first;
2455       }
2456       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2457       a_new     += lensi;
2458       i_new[i+1] = i_new[i] + lensi;
2459       c->ilen[i] = lensi;
2460     }
2461     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
2462   } else {
2463     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
2464     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2465     ierr = PetscMalloc1((1+nrows),&lens);CHKERRQ(ierr);
2466     for (i=0; i<ncols; i++) {
2467 #if defined(PETSC_USE_DEBUG)
2468       if (icol[i] >= oldcols) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requesting column beyond largest column icol[%D] %D <= A->cmap->n %D",i,icol[i],oldcols);
2469 #endif
2470       smap[icol[i]] = i+1;
2471     }
2472 
2473     /* determine lens of each row */
2474     for (i=0; i<nrows; i++) {
2475       kstart  = ai[irow[i]];
2476       kend    = kstart + a->ilen[irow[i]];
2477       lens[i] = 0;
2478       for (k=kstart; k<kend; k++) {
2479         if (smap[aj[k]]) {
2480           lens[i]++;
2481         }
2482       }
2483     }
2484     /* Create and fill new matrix */
2485     if (scall == MAT_REUSE_MATRIX) {
2486       PetscBool equal;
2487 
2488       c = (Mat_SeqAIJ*)((*B)->data);
2489       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2490       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2491       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2492       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
2493       C    = *B;
2494     } else {
2495       PetscInt rbs,cbs;
2496       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2497       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2498       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
2499       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
2500       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
2501       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2502       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
2503     }
2504     c = (Mat_SeqAIJ*)(C->data);
2505     for (i=0; i<nrows; i++) {
2506       row      = irow[i];
2507       kstart   = ai[row];
2508       kend     = kstart + a->ilen[row];
2509       mat_i    = c->i[i];
2510       mat_j    = c->j + mat_i;
2511       mat_a    = c->a + mat_i;
2512       mat_ilen = c->ilen + i;
2513       for (k=kstart; k<kend; k++) {
2514         if ((tcol=smap[a->j[k]])) {
2515           *mat_j++ = tcol - 1;
2516           *mat_a++ = a->a[k];
2517           (*mat_ilen)++;
2518 
2519         }
2520       }
2521     }
2522     /* Free work space */
2523     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2524     ierr = PetscFree(smap);CHKERRQ(ierr);
2525     ierr = PetscFree(lens);CHKERRQ(ierr);
2526   }
2527   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2528   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2529 
2530   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2531   *B   = C;
2532   PetscFunctionReturn(0);
2533 }
2534 
2535 #undef __FUNCT__
2536 #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2537 PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2538 {
2539   PetscErrorCode ierr;
2540   Mat            B;
2541 
2542   PetscFunctionBegin;
2543   if (scall == MAT_INITIAL_MATRIX) {
2544     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
2545     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
2546     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
2547     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2548     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
2549     *subMat = B;
2550   } else {
2551     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2552   }
2553   PetscFunctionReturn(0);
2554 }
2555 
2556 #undef __FUNCT__
2557 #define __FUNCT__ "MatILUFactor_SeqAIJ"
2558 PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2559 {
2560   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2561   PetscErrorCode ierr;
2562   Mat            outA;
2563   PetscBool      row_identity,col_identity;
2564 
2565   PetscFunctionBegin;
2566   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
2567 
2568   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2569   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2570 
2571   outA             = inA;
2572   outA->factortype = MAT_FACTOR_LU;
2573 
2574   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
2575   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2576 
2577   a->row = row;
2578 
2579   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
2580   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2581 
2582   a->col = col;
2583 
2584   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2585   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
2586   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
2587   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2588 
2589   if (!a->solve_work) { /* this matrix may have been factored before */
2590     ierr = PetscMalloc1((inA->rmap->n+1),&a->solve_work);CHKERRQ(ierr);
2591     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2592   }
2593 
2594   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2595   if (row_identity && col_identity) {
2596     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2597   } else {
2598     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2599   }
2600   PetscFunctionReturn(0);
2601 }
2602 
2603 #undef __FUNCT__
2604 #define __FUNCT__ "MatScale_SeqAIJ"
2605 PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2606 {
2607   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2608   PetscScalar    oalpha = alpha;
2609   PetscErrorCode ierr;
2610   PetscBLASInt   one = 1,bnz;
2611 
2612   PetscFunctionBegin;
2613   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
2614   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2615   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2616   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
2617   PetscFunctionReturn(0);
2618 }
2619 
2620 #undef __FUNCT__
2621 #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
2622 PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2623 {
2624   PetscErrorCode ierr;
2625   PetscInt       i;
2626 
2627   PetscFunctionBegin;
2628   if (scall == MAT_INITIAL_MATRIX) {
2629     ierr = PetscMalloc1((n+1),B);CHKERRQ(ierr);
2630   }
2631 
2632   for (i=0; i<n; i++) {
2633     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2634   }
2635   PetscFunctionReturn(0);
2636 }
2637 
2638 #undef __FUNCT__
2639 #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
2640 PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2641 {
2642   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2643   PetscErrorCode ierr;
2644   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
2645   const PetscInt *idx;
2646   PetscInt       start,end,*ai,*aj;
2647   PetscBT        table;
2648 
2649   PetscFunctionBegin;
2650   m  = A->rmap->n;
2651   ai = a->i;
2652   aj = a->j;
2653 
2654   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
2655 
2656   ierr = PetscMalloc1((m+1),&nidx);CHKERRQ(ierr);
2657   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
2658 
2659   for (i=0; i<is_max; i++) {
2660     /* Initialize the two local arrays */
2661     isz  = 0;
2662     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2663 
2664     /* Extract the indices, assume there can be duplicate entries */
2665     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2666     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2667 
2668     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2669     for (j=0; j<n; ++j) {
2670       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2671     }
2672     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
2673     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2674 
2675     k = 0;
2676     for (j=0; j<ov; j++) { /* for each overlap */
2677       n = isz;
2678       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2679         row   = nidx[k];
2680         start = ai[row];
2681         end   = ai[row+1];
2682         for (l = start; l<end; l++) {
2683           val = aj[l];
2684           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2685         }
2686       }
2687     }
2688     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2689   }
2690   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2691   ierr = PetscFree(nidx);CHKERRQ(ierr);
2692   PetscFunctionReturn(0);
2693 }
2694 
2695 /* -------------------------------------------------------------- */
2696 #undef __FUNCT__
2697 #define __FUNCT__ "MatPermute_SeqAIJ"
2698 PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
2699 {
2700   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2701   PetscErrorCode ierr;
2702   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
2703   const PetscInt *row,*col;
2704   PetscInt       *cnew,j,*lens;
2705   IS             icolp,irowp;
2706   PetscInt       *cwork = NULL;
2707   PetscScalar    *vwork = NULL;
2708 
2709   PetscFunctionBegin;
2710   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
2711   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
2712   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
2713   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
2714 
2715   /* determine lengths of permuted rows */
2716   ierr = PetscMalloc1((m+1),&lens);CHKERRQ(ierr);
2717   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2718   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2719   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
2720   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
2721   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2722   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2723   ierr = PetscFree(lens);CHKERRQ(ierr);
2724 
2725   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
2726   for (i=0; i<m; i++) {
2727     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2728     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2729     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
2730     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2731   }
2732   ierr = PetscFree(cnew);CHKERRQ(ierr);
2733 
2734   (*B)->assembled = PETSC_FALSE;
2735 
2736   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2737   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2738   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
2739   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
2740   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
2741   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
2742   PetscFunctionReturn(0);
2743 }
2744 
2745 #undef __FUNCT__
2746 #define __FUNCT__ "MatCopy_SeqAIJ"
2747 PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2748 {
2749   PetscErrorCode ierr;
2750 
2751   PetscFunctionBegin;
2752   /* If the two matrices have the same copy implementation, use fast copy. */
2753   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2754     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2755     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2756 
2757     if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2758     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2759   } else {
2760     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2761   }
2762   PetscFunctionReturn(0);
2763 }
2764 
2765 #undef __FUNCT__
2766 #define __FUNCT__ "MatSetUp_SeqAIJ"
2767 PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2768 {
2769   PetscErrorCode ierr;
2770 
2771   PetscFunctionBegin;
2772   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2773   PetscFunctionReturn(0);
2774 }
2775 
2776 #undef __FUNCT__
2777 #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
2778 PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2779 {
2780   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2781 
2782   PetscFunctionBegin;
2783   *array = a->a;
2784   PetscFunctionReturn(0);
2785 }
2786 
2787 #undef __FUNCT__
2788 #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
2789 PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2790 {
2791   PetscFunctionBegin;
2792   PetscFunctionReturn(0);
2793 }
2794 
2795 /*
2796    Computes the number of nonzeros per row needed for preallocation when X and Y
2797    have different nonzero structure.
2798 */
2799 #undef __FUNCT__
2800 #define __FUNCT__ "MatAXPYGetPreallocation_SeqX_private"
2801 PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2802 {
2803   PetscInt       i,j,k,nzx,nzy;
2804 
2805   PetscFunctionBegin;
2806   /* Set the number of nonzeros in the new matrix */
2807   for (i=0; i<m; i++) {
2808     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2809     nzx = xi[i+1] - xi[i];
2810     nzy = yi[i+1] - yi[i];
2811     nnz[i] = 0;
2812     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2813       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2814       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
2815       nnz[i]++;
2816     }
2817     for (; k<nzy; k++) nnz[i]++;
2818   }
2819   PetscFunctionReturn(0);
2820 }
2821 
2822 #undef __FUNCT__
2823 #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
2824 PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2825 {
2826   PetscInt       m = Y->rmap->N;
2827   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2828   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2829   PetscErrorCode ierr;
2830 
2831   PetscFunctionBegin;
2832   /* Set the number of nonzeros in the new matrix */
2833   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2834   PetscFunctionReturn(0);
2835 }
2836 
2837 #undef __FUNCT__
2838 #define __FUNCT__ "MatAXPY_SeqAIJ"
2839 PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2840 {
2841   PetscErrorCode ierr;
2842   PetscInt       i;
2843   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2844   PetscBLASInt   one=1,bnz;
2845 
2846   PetscFunctionBegin;
2847   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2848   if (str == SAME_NONZERO_PATTERN) {
2849     PetscScalar alpha = a;
2850     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2851     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2852     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2853   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2854     if (y->xtoy && y->XtoY != X) {
2855       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2856       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2857     }
2858     if (!y->xtoy) { /* get xtoy */
2859       ierr    = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,NULL, y->i,y->j,NULL, &y->xtoy);CHKERRQ(ierr);
2860       y->XtoY = X;
2861       ierr    = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2862     }
2863     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2864     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2865     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %D/%D = %g\n",x->nz,y->nz,(double)((PetscReal)(x->nz)/(y->nz+1)));CHKERRQ(ierr);
2866   } else {
2867     Mat      B;
2868     PetscInt *nnz;
2869     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2870     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2871     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
2872     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2873     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2874     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
2875     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2876     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2877     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2878     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
2879     ierr = PetscFree(nnz);CHKERRQ(ierr);
2880   }
2881   PetscFunctionReturn(0);
2882 }
2883 
2884 #undef __FUNCT__
2885 #define __FUNCT__ "MatConjugate_SeqAIJ"
2886 PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2887 {
2888 #if defined(PETSC_USE_COMPLEX)
2889   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2890   PetscInt    i,nz;
2891   PetscScalar *a;
2892 
2893   PetscFunctionBegin;
2894   nz = aij->nz;
2895   a  = aij->a;
2896   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2897 #else
2898   PetscFunctionBegin;
2899 #endif
2900   PetscFunctionReturn(0);
2901 }
2902 
2903 #undef __FUNCT__
2904 #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2905 PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2906 {
2907   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2908   PetscErrorCode ierr;
2909   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2910   PetscReal      atmp;
2911   PetscScalar    *x;
2912   MatScalar      *aa;
2913 
2914   PetscFunctionBegin;
2915   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2916   aa = a->a;
2917   ai = a->i;
2918   aj = a->j;
2919 
2920   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2921   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2922   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2923   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2924   for (i=0; i<m; i++) {
2925     ncols = ai[1] - ai[0]; ai++;
2926     x[i]  = 0.0;
2927     for (j=0; j<ncols; j++) {
2928       atmp = PetscAbsScalar(*aa);
2929       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2930       aa++; aj++;
2931     }
2932   }
2933   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2934   PetscFunctionReturn(0);
2935 }
2936 
2937 #undef __FUNCT__
2938 #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2939 PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2940 {
2941   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2942   PetscErrorCode ierr;
2943   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2944   PetscScalar    *x;
2945   MatScalar      *aa;
2946 
2947   PetscFunctionBegin;
2948   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2949   aa = a->a;
2950   ai = a->i;
2951   aj = a->j;
2952 
2953   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2954   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2955   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2956   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2957   for (i=0; i<m; i++) {
2958     ncols = ai[1] - ai[0]; ai++;
2959     if (ncols == A->cmap->n) { /* row is dense */
2960       x[i] = *aa; if (idx) idx[i] = 0;
2961     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2962       x[i] = 0.0;
2963       if (idx) {
2964         idx[i] = 0; /* in case ncols is zero */
2965         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2966           if (aj[j] > j) {
2967             idx[i] = j;
2968             break;
2969           }
2970         }
2971       }
2972     }
2973     for (j=0; j<ncols; j++) {
2974       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2975       aa++; aj++;
2976     }
2977   }
2978   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2979   PetscFunctionReturn(0);
2980 }
2981 
2982 #undef __FUNCT__
2983 #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2984 PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2985 {
2986   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2987   PetscErrorCode ierr;
2988   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2989   PetscReal      atmp;
2990   PetscScalar    *x;
2991   MatScalar      *aa;
2992 
2993   PetscFunctionBegin;
2994   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2995   aa = a->a;
2996   ai = a->i;
2997   aj = a->j;
2998 
2999   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3000   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3001   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3002   if (n != A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %D vs. %D rows", A->rmap->n, n);
3003   for (i=0; i<m; i++) {
3004     ncols = ai[1] - ai[0]; ai++;
3005     if (ncols) {
3006       /* Get first nonzero */
3007       for (j = 0; j < ncols; j++) {
3008         atmp = PetscAbsScalar(aa[j]);
3009         if (atmp > 1.0e-12) {
3010           x[i] = atmp;
3011           if (idx) idx[i] = aj[j];
3012           break;
3013         }
3014       }
3015       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3016     } else {
3017       x[i] = 0.0; if (idx) idx[i] = 0;
3018     }
3019     for (j = 0; j < ncols; j++) {
3020       atmp = PetscAbsScalar(*aa);
3021       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3022       aa++; aj++;
3023     }
3024   }
3025   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3026   PetscFunctionReturn(0);
3027 }
3028 
3029 #undef __FUNCT__
3030 #define __FUNCT__ "MatGetRowMin_SeqAIJ"
3031 PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3032 {
3033   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3034   PetscErrorCode ierr;
3035   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3036   PetscScalar    *x;
3037   MatScalar      *aa;
3038 
3039   PetscFunctionBegin;
3040   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3041   aa = a->a;
3042   ai = a->i;
3043   aj = a->j;
3044 
3045   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3046   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3047   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3048   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3049   for (i=0; i<m; i++) {
3050     ncols = ai[1] - ai[0]; ai++;
3051     if (ncols == A->cmap->n) { /* row is dense */
3052       x[i] = *aa; if (idx) idx[i] = 0;
3053     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3054       x[i] = 0.0;
3055       if (idx) {   /* find first implicit 0.0 in the row */
3056         idx[i] = 0; /* in case ncols is zero */
3057         for (j=0; j<ncols; j++) {
3058           if (aj[j] > j) {
3059             idx[i] = j;
3060             break;
3061           }
3062         }
3063       }
3064     }
3065     for (j=0; j<ncols; j++) {
3066       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3067       aa++; aj++;
3068     }
3069   }
3070   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3071   PetscFunctionReturn(0);
3072 }
3073 
3074 #include <petscblaslapack.h>
3075 #include <petsc-private/kernels/blockinvert.h>
3076 
3077 #undef __FUNCT__
3078 #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
3079 PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3080 {
3081   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
3082   PetscErrorCode ierr;
3083   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3084   MatScalar      *diag,work[25],*v_work;
3085   PetscReal      shift = 0.0;
3086 
3087   PetscFunctionBegin;
3088   if (a->ibdiagvalid) {
3089     if (values) *values = a->ibdiag;
3090     PetscFunctionReturn(0);
3091   }
3092   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3093   if (!a->ibdiag) {
3094     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
3095     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3096   }
3097   diag = a->ibdiag;
3098   if (values) *values = a->ibdiag;
3099   /* factor and invert each block */
3100   switch (bs) {
3101   case 1:
3102     for (i=0; i<mbs; i++) {
3103       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3104       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3105     }
3106     break;
3107   case 2:
3108     for (i=0; i<mbs; i++) {
3109       ij[0] = 2*i; ij[1] = 2*i + 1;
3110       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
3111       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
3112       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3113       diag += 4;
3114     }
3115     break;
3116   case 3:
3117     for (i=0; i<mbs; i++) {
3118       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3119       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
3120       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
3121       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3122       diag += 9;
3123     }
3124     break;
3125   case 4:
3126     for (i=0; i<mbs; i++) {
3127       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3128       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
3129       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
3130       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3131       diag += 16;
3132     }
3133     break;
3134   case 5:
3135     for (i=0; i<mbs; i++) {
3136       ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3137       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
3138       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
3139       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3140       diag += 25;
3141     }
3142     break;
3143   case 6:
3144     for (i=0; i<mbs; i++) {
3145       ij[0] = 6*i; ij[1] = 6*i + 1; ij[2] = 6*i + 2; ij[3] = 6*i + 3; ij[4] = 6*i + 4; ij[5] = 6*i + 5;
3146       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3147       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
3148       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3149       diag += 36;
3150     }
3151     break;
3152   case 7:
3153     for (i=0; i<mbs; i++) {
3154       ij[0] = 7*i; ij[1] = 7*i + 1; ij[2] = 7*i + 2; ij[3] = 7*i + 3; ij[4] = 7*i + 4; ij[5] = 7*i + 5; ij[5] = 7*i + 6;
3155       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3156       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
3157       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3158       diag += 49;
3159     }
3160     break;
3161   default:
3162     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3163     for (i=0; i<mbs; i++) {
3164       for (j=0; j<bs; j++) {
3165         IJ[j] = bs*i + j;
3166       }
3167       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
3168       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
3169       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3170       diag += bs2;
3171     }
3172     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3173   }
3174   a->ibdiagvalid = PETSC_TRUE;
3175   PetscFunctionReturn(0);
3176 }
3177 
3178 #undef __FUNCT__
3179 #define __FUNCT__ "MatSetRandom_SeqAIJ"
3180 static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3181 {
3182   PetscErrorCode ierr;
3183   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3184   PetscScalar    a;
3185   PetscInt       m,n,i,j,col;
3186 
3187   PetscFunctionBegin;
3188   if (!x->assembled) {
3189     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
3190     for (i=0; i<m; i++) {
3191       for (j=0; j<aij->imax[i]; j++) {
3192         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
3193         col  = (PetscInt)(n*PetscRealPart(a));
3194         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
3195       }
3196     }
3197   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
3198   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3199   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3200   PetscFunctionReturn(0);
3201 }
3202 
3203 /* -------------------------------------------------------------------*/
3204 static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3205                                         MatGetRow_SeqAIJ,
3206                                         MatRestoreRow_SeqAIJ,
3207                                         MatMult_SeqAIJ,
3208                                 /*  4*/ MatMultAdd_SeqAIJ,
3209                                         MatMultTranspose_SeqAIJ,
3210                                         MatMultTransposeAdd_SeqAIJ,
3211                                         0,
3212                                         0,
3213                                         0,
3214                                 /* 10*/ 0,
3215                                         MatLUFactor_SeqAIJ,
3216                                         0,
3217                                         MatSOR_SeqAIJ,
3218                                         MatTranspose_SeqAIJ,
3219                                 /*1 5*/ MatGetInfo_SeqAIJ,
3220                                         MatEqual_SeqAIJ,
3221                                         MatGetDiagonal_SeqAIJ,
3222                                         MatDiagonalScale_SeqAIJ,
3223                                         MatNorm_SeqAIJ,
3224                                 /* 20*/ 0,
3225                                         MatAssemblyEnd_SeqAIJ,
3226                                         MatSetOption_SeqAIJ,
3227                                         MatZeroEntries_SeqAIJ,
3228                                 /* 24*/ MatZeroRows_SeqAIJ,
3229                                         0,
3230                                         0,
3231                                         0,
3232                                         0,
3233                                 /* 29*/ MatSetUp_SeqAIJ,
3234                                         0,
3235                                         0,
3236                                         0,
3237                                         0,
3238                                 /* 34*/ MatDuplicate_SeqAIJ,
3239                                         0,
3240                                         0,
3241                                         MatILUFactor_SeqAIJ,
3242                                         0,
3243                                 /* 39*/ MatAXPY_SeqAIJ,
3244                                         MatGetSubMatrices_SeqAIJ,
3245                                         MatIncreaseOverlap_SeqAIJ,
3246                                         MatGetValues_SeqAIJ,
3247                                         MatCopy_SeqAIJ,
3248                                 /* 44*/ MatGetRowMax_SeqAIJ,
3249                                         MatScale_SeqAIJ,
3250                                         0,
3251                                         MatDiagonalSet_SeqAIJ,
3252                                         MatZeroRowsColumns_SeqAIJ,
3253                                 /* 49*/ MatSetRandom_SeqAIJ,
3254                                         MatGetRowIJ_SeqAIJ,
3255                                         MatRestoreRowIJ_SeqAIJ,
3256                                         MatGetColumnIJ_SeqAIJ,
3257                                         MatRestoreColumnIJ_SeqAIJ,
3258                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3259                                         0,
3260                                         0,
3261                                         MatPermute_SeqAIJ,
3262                                         0,
3263                                 /* 59*/ 0,
3264                                         MatDestroy_SeqAIJ,
3265                                         MatView_SeqAIJ,
3266                                         0,
3267                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3268                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3269                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3270                                         0,
3271                                         0,
3272                                         0,
3273                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3274                                         MatGetRowMinAbs_SeqAIJ,
3275                                         0,
3276                                         MatSetColoring_SeqAIJ,
3277                                         0,
3278                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
3279                                         MatFDColoringApply_AIJ,
3280                                         0,
3281                                         0,
3282                                         0,
3283                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3284                                         0,
3285                                         0,
3286                                         0,
3287                                         MatLoad_SeqAIJ,
3288                                 /* 84*/ MatIsSymmetric_SeqAIJ,
3289                                         MatIsHermitian_SeqAIJ,
3290                                         0,
3291                                         0,
3292                                         0,
3293                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
3294                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
3295                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
3296                                         MatPtAP_SeqAIJ_SeqAIJ,
3297                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
3298                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3299                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
3300                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
3301                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3302                                         0,
3303                                 /* 99*/ 0,
3304                                         0,
3305                                         0,
3306                                         MatConjugate_SeqAIJ,
3307                                         0,
3308                                 /*104*/ MatSetValuesRow_SeqAIJ,
3309                                         MatRealPart_SeqAIJ,
3310                                         MatImaginaryPart_SeqAIJ,
3311                                         0,
3312                                         0,
3313                                 /*109*/ MatMatSolve_SeqAIJ,
3314                                         0,
3315                                         MatGetRowMin_SeqAIJ,
3316                                         0,
3317                                         MatMissingDiagonal_SeqAIJ,
3318                                 /*114*/ 0,
3319                                         0,
3320                                         0,
3321                                         0,
3322                                         0,
3323                                 /*119*/ 0,
3324                                         0,
3325                                         0,
3326                                         0,
3327                                         MatGetMultiProcBlock_SeqAIJ,
3328                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3329                                         MatGetColumnNorms_SeqAIJ,
3330                                         MatInvertBlockDiagonal_SeqAIJ,
3331                                         0,
3332                                         0,
3333                                 /*129*/ 0,
3334                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
3335                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
3336                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3337                                         MatTransposeColoringCreate_SeqAIJ,
3338                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3339                                         MatTransColoringApplyDenToSp_SeqAIJ,
3340                                         MatRARt_SeqAIJ_SeqAIJ,
3341                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
3342                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
3343                                  /*139*/0,
3344                                         0,
3345                                         0,
3346                                         MatFDColoringSetUp_SeqXAIJ,
3347                                         MatFindOffBlockDiagonalEntries_SeqAIJ
3348 };
3349 
3350 #undef __FUNCT__
3351 #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
3352 PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3353 {
3354   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3355   PetscInt   i,nz,n;
3356 
3357   PetscFunctionBegin;
3358   nz = aij->maxnz;
3359   n  = mat->rmap->n;
3360   for (i=0; i<nz; i++) {
3361     aij->j[i] = indices[i];
3362   }
3363   aij->nz = nz;
3364   for (i=0; i<n; i++) {
3365     aij->ilen[i] = aij->imax[i];
3366   }
3367   PetscFunctionReturn(0);
3368 }
3369 
3370 #undef __FUNCT__
3371 #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3372 /*@
3373     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3374        in the matrix.
3375 
3376   Input Parameters:
3377 +  mat - the SeqAIJ matrix
3378 -  indices - the column indices
3379 
3380   Level: advanced
3381 
3382   Notes:
3383     This can be called if you have precomputed the nonzero structure of the
3384   matrix and want to provide it to the matrix object to improve the performance
3385   of the MatSetValues() operation.
3386 
3387     You MUST have set the correct numbers of nonzeros per row in the call to
3388   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3389 
3390     MUST be called before any calls to MatSetValues();
3391 
3392     The indices should start with zero, not one.
3393 
3394 @*/
3395 PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3396 {
3397   PetscErrorCode ierr;
3398 
3399   PetscFunctionBegin;
3400   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3401   PetscValidPointer(indices,2);
3402   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3403   PetscFunctionReturn(0);
3404 }
3405 
3406 /* ----------------------------------------------------------------------------------------*/
3407 
3408 #undef __FUNCT__
3409 #define __FUNCT__ "MatStoreValues_SeqAIJ"
3410 PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3411 {
3412   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3413   PetscErrorCode ierr;
3414   size_t         nz = aij->i[mat->rmap->n];
3415 
3416   PetscFunctionBegin;
3417   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3418 
3419   /* allocate space for values if not already there */
3420   if (!aij->saved_values) {
3421     ierr = PetscMalloc1((nz+1),&aij->saved_values);CHKERRQ(ierr);
3422     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3423   }
3424 
3425   /* copy values over */
3426   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3427   PetscFunctionReturn(0);
3428 }
3429 
3430 #undef __FUNCT__
3431 #define __FUNCT__ "MatStoreValues"
3432 /*@
3433     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3434        example, reuse of the linear part of a Jacobian, while recomputing the
3435        nonlinear portion.
3436 
3437    Collect on Mat
3438 
3439   Input Parameters:
3440 .  mat - the matrix (currently only AIJ matrices support this option)
3441 
3442   Level: advanced
3443 
3444   Common Usage, with SNESSolve():
3445 $    Create Jacobian matrix
3446 $    Set linear terms into matrix
3447 $    Apply boundary conditions to matrix, at this time matrix must have
3448 $      final nonzero structure (i.e. setting the nonlinear terms and applying
3449 $      boundary conditions again will not change the nonzero structure
3450 $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3451 $    ierr = MatStoreValues(mat);
3452 $    Call SNESSetJacobian() with matrix
3453 $    In your Jacobian routine
3454 $      ierr = MatRetrieveValues(mat);
3455 $      Set nonlinear terms in matrix
3456 
3457   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3458 $    // build linear portion of Jacobian
3459 $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3460 $    ierr = MatStoreValues(mat);
3461 $    loop over nonlinear iterations
3462 $       ierr = MatRetrieveValues(mat);
3463 $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3464 $       // call MatAssemblyBegin/End() on matrix
3465 $       Solve linear system with Jacobian
3466 $    endloop
3467 
3468   Notes:
3469     Matrix must already be assemblied before calling this routine
3470     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3471     calling this routine.
3472 
3473     When this is called multiple times it overwrites the previous set of stored values
3474     and does not allocated additional space.
3475 
3476 .seealso: MatRetrieveValues()
3477 
3478 @*/
3479 PetscErrorCode  MatStoreValues(Mat mat)
3480 {
3481   PetscErrorCode ierr;
3482 
3483   PetscFunctionBegin;
3484   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3485   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3486   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3487   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3488   PetscFunctionReturn(0);
3489 }
3490 
3491 #undef __FUNCT__
3492 #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
3493 PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3494 {
3495   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3496   PetscErrorCode ierr;
3497   PetscInt       nz = aij->i[mat->rmap->n];
3498 
3499   PetscFunctionBegin;
3500   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3501   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3502   /* copy values over */
3503   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3504   PetscFunctionReturn(0);
3505 }
3506 
3507 #undef __FUNCT__
3508 #define __FUNCT__ "MatRetrieveValues"
3509 /*@
3510     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3511        example, reuse of the linear part of a Jacobian, while recomputing the
3512        nonlinear portion.
3513 
3514    Collect on Mat
3515 
3516   Input Parameters:
3517 .  mat - the matrix (currently on AIJ matrices support this option)
3518 
3519   Level: advanced
3520 
3521 .seealso: MatStoreValues()
3522 
3523 @*/
3524 PetscErrorCode  MatRetrieveValues(Mat mat)
3525 {
3526   PetscErrorCode ierr;
3527 
3528   PetscFunctionBegin;
3529   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3530   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3531   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3532   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3533   PetscFunctionReturn(0);
3534 }
3535 
3536 
3537 /* --------------------------------------------------------------------------------*/
3538 #undef __FUNCT__
3539 #define __FUNCT__ "MatCreateSeqAIJ"
3540 /*@C
3541    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3542    (the default parallel PETSc format).  For good matrix assembly performance
3543    the user should preallocate the matrix storage by setting the parameter nz
3544    (or the array nnz).  By setting these parameters accurately, performance
3545    during matrix assembly can be increased by more than a factor of 50.
3546 
3547    Collective on MPI_Comm
3548 
3549    Input Parameters:
3550 +  comm - MPI communicator, set to PETSC_COMM_SELF
3551 .  m - number of rows
3552 .  n - number of columns
3553 .  nz - number of nonzeros per row (same for all rows)
3554 -  nnz - array containing the number of nonzeros in the various rows
3555          (possibly different for each row) or NULL
3556 
3557    Output Parameter:
3558 .  A - the matrix
3559 
3560    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3561    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3562    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3563 
3564    Notes:
3565    If nnz is given then nz is ignored
3566 
3567    The AIJ format (also called the Yale sparse matrix format or
3568    compressed row storage), is fully compatible with standard Fortran 77
3569    storage.  That is, the stored row and column indices can begin at
3570    either one (as in Fortran) or zero.  See the users' manual for details.
3571 
3572    Specify the preallocated storage with either nz or nnz (not both).
3573    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3574    allocation.  For large problems you MUST preallocate memory or you
3575    will get TERRIBLE performance, see the users' manual chapter on matrices.
3576 
3577    By default, this format uses inodes (identical nodes) when possible, to
3578    improve numerical efficiency of matrix-vector products and solves. We
3579    search for consecutive rows with the same nonzero structure, thereby
3580    reusing matrix information to achieve increased efficiency.
3581 
3582    Options Database Keys:
3583 +  -mat_no_inode  - Do not use inodes
3584 -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3585 
3586    Level: intermediate
3587 
3588 .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
3589 
3590 @*/
3591 PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3592 {
3593   PetscErrorCode ierr;
3594 
3595   PetscFunctionBegin;
3596   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3597   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3598   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3599   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3600   PetscFunctionReturn(0);
3601 }
3602 
3603 #undef __FUNCT__
3604 #define __FUNCT__ "MatSeqAIJSetPreallocation"
3605 /*@C
3606    MatSeqAIJSetPreallocation - For good matrix assembly performance
3607    the user should preallocate the matrix storage by setting the parameter nz
3608    (or the array nnz).  By setting these parameters accurately, performance
3609    during matrix assembly can be increased by more than a factor of 50.
3610 
3611    Collective on MPI_Comm
3612 
3613    Input Parameters:
3614 +  B - The matrix
3615 .  nz - number of nonzeros per row (same for all rows)
3616 -  nnz - array containing the number of nonzeros in the various rows
3617          (possibly different for each row) or NULL
3618 
3619    Notes:
3620      If nnz is given then nz is ignored
3621 
3622     The AIJ format (also called the Yale sparse matrix format or
3623    compressed row storage), is fully compatible with standard Fortran 77
3624    storage.  That is, the stored row and column indices can begin at
3625    either one (as in Fortran) or zero.  See the users' manual for details.
3626 
3627    Specify the preallocated storage with either nz or nnz (not both).
3628    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3629    allocation.  For large problems you MUST preallocate memory or you
3630    will get TERRIBLE performance, see the users' manual chapter on matrices.
3631 
3632    You can call MatGetInfo() to get information on how effective the preallocation was;
3633    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3634    You can also run with the option -info and look for messages with the string
3635    malloc in them to see if additional memory allocation was needed.
3636 
3637    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3638    entries or columns indices
3639 
3640    By default, this format uses inodes (identical nodes) when possible, to
3641    improve numerical efficiency of matrix-vector products and solves. We
3642    search for consecutive rows with the same nonzero structure, thereby
3643    reusing matrix information to achieve increased efficiency.
3644 
3645    Options Database Keys:
3646 +  -mat_no_inode  - Do not use inodes
3647 .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3648 -  -mat_aij_oneindex - Internally use indexing starting at 1
3649         rather than 0.  Note that when calling MatSetValues(),
3650         the user still MUST index entries starting at 0!
3651 
3652    Level: intermediate
3653 
3654 .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3655 
3656 @*/
3657 PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3658 {
3659   PetscErrorCode ierr;
3660 
3661   PetscFunctionBegin;
3662   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
3663   PetscValidType(B,1);
3664   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3665   PetscFunctionReturn(0);
3666 }
3667 
3668 #undef __FUNCT__
3669 #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
3670 PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3671 {
3672   Mat_SeqAIJ     *b;
3673   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3674   PetscErrorCode ierr;
3675   PetscInt       i;
3676 
3677   PetscFunctionBegin;
3678   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3679   if (nz == MAT_SKIP_ALLOCATION) {
3680     skipallocation = PETSC_TRUE;
3681     nz             = 0;
3682   }
3683 
3684   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3685   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3686 
3687   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3688   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3689   if (nnz) {
3690     for (i=0; i<B->rmap->n; i++) {
3691       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]);
3692       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %D value %d rowlength %D",i,nnz[i],B->cmap->n);
3693     }
3694   }
3695 
3696   B->preallocated = PETSC_TRUE;
3697 
3698   b = (Mat_SeqAIJ*)B->data;
3699 
3700   if (!skipallocation) {
3701     if (!b->imax) {
3702       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
3703       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3704     }
3705     if (!nnz) {
3706       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3707       else if (nz < 0) nz = 1;
3708       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3709       nz = nz*B->rmap->n;
3710     } else {
3711       nz = 0;
3712       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3713     }
3714     /* b->ilen will count nonzeros in each row so far. */
3715     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3716 
3717     /* allocate the matrix space */
3718     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3719     ierr    = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
3720     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3721     b->i[0] = 0;
3722     for (i=1; i<B->rmap->n+1; i++) {
3723       b->i[i] = b->i[i-1] + b->imax[i-1];
3724     }
3725     b->singlemalloc = PETSC_TRUE;
3726     b->free_a       = PETSC_TRUE;
3727     b->free_ij      = PETSC_TRUE;
3728 #if defined(PETSC_THREADCOMM_ACTIVE)
3729     ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3730 #endif
3731   } else {
3732     b->free_a  = PETSC_FALSE;
3733     b->free_ij = PETSC_FALSE;
3734   }
3735 
3736   b->nz               = 0;
3737   b->maxnz            = nz;
3738   B->info.nz_unneeded = (double)b->maxnz;
3739   if (realalloc) {
3740     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3741   }
3742   PetscFunctionReturn(0);
3743 }
3744 
3745 #undef  __FUNCT__
3746 #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
3747 /*@
3748    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3749 
3750    Input Parameters:
3751 +  B - the matrix
3752 .  i - the indices into j for the start of each row (starts with zero)
3753 .  j - the column indices for each row (starts with zero) these must be sorted for each row
3754 -  v - optional values in the matrix
3755 
3756    Level: developer
3757 
3758    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
3759 
3760 .keywords: matrix, aij, compressed row, sparse, sequential
3761 
3762 .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3763 @*/
3764 PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3765 {
3766   PetscErrorCode ierr;
3767 
3768   PetscFunctionBegin;
3769   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
3770   PetscValidType(B,1);
3771   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3772   PetscFunctionReturn(0);
3773 }
3774 
3775 #undef  __FUNCT__
3776 #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3777 PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3778 {
3779   PetscInt       i;
3780   PetscInt       m,n;
3781   PetscInt       nz;
3782   PetscInt       *nnz, nz_max = 0;
3783   PetscScalar    *values;
3784   PetscErrorCode ierr;
3785 
3786   PetscFunctionBegin;
3787   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3788 
3789   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3790   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3791 
3792   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3793   ierr = PetscMalloc1((m+1), &nnz);CHKERRQ(ierr);
3794   for (i = 0; i < m; i++) {
3795     nz     = Ii[i+1]- Ii[i];
3796     nz_max = PetscMax(nz_max, nz);
3797     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3798     nnz[i] = nz;
3799   }
3800   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3801   ierr = PetscFree(nnz);CHKERRQ(ierr);
3802 
3803   if (v) {
3804     values = (PetscScalar*) v;
3805   } else {
3806     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3807   }
3808 
3809   for (i = 0; i < m; i++) {
3810     nz   = Ii[i+1] - Ii[i];
3811     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3812   }
3813 
3814   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3815   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3816 
3817   if (!v) {
3818     ierr = PetscFree(values);CHKERRQ(ierr);
3819   }
3820   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3821   PetscFunctionReturn(0);
3822 }
3823 
3824 #include <../src/mat/impls/dense/seq/dense.h>
3825 #include <petsc-private/kernels/petscaxpy.h>
3826 
3827 #undef __FUNCT__
3828 #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3829 /*
3830     Computes (B'*A')' since computing B*A directly is untenable
3831 
3832                n                       p                          p
3833         (              )       (              )         (                  )
3834       m (      A       )  *  n (       B      )   =   m (         C        )
3835         (              )       (              )         (                  )
3836 
3837 */
3838 PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3839 {
3840   PetscErrorCode    ierr;
3841   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3842   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3843   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
3844   PetscInt          i,n,m,q,p;
3845   const PetscInt    *ii,*idx;
3846   const PetscScalar *b,*a,*a_q;
3847   PetscScalar       *c,*c_q;
3848 
3849   PetscFunctionBegin;
3850   m    = A->rmap->n;
3851   n    = A->cmap->n;
3852   p    = B->cmap->n;
3853   a    = sub_a->v;
3854   b    = sub_b->a;
3855   c    = sub_c->v;
3856   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3857 
3858   ii  = sub_b->i;
3859   idx = sub_b->j;
3860   for (i=0; i<n; i++) {
3861     q = ii[i+1] - ii[i];
3862     while (q-->0) {
3863       c_q = c + m*(*idx);
3864       a_q = a + m*i;
3865       PetscKernelAXPY(c_q,*b,a_q,m);
3866       idx++;
3867       b++;
3868     }
3869   }
3870   PetscFunctionReturn(0);
3871 }
3872 
3873 #undef __FUNCT__
3874 #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3875 PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3876 {
3877   PetscErrorCode ierr;
3878   PetscInt       m=A->rmap->n,n=B->cmap->n;
3879   Mat            Cmat;
3880 
3881   PetscFunctionBegin;
3882   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %D != B->rmap->n %D\n",A->cmap->n,B->rmap->n);
3883   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3884   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3885   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3886   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3887   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3888 
3889   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
3890 
3891   *C = Cmat;
3892   PetscFunctionReturn(0);
3893 }
3894 
3895 /* ----------------------------------------------------------------*/
3896 #undef __FUNCT__
3897 #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3898 PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3899 {
3900   PetscErrorCode ierr;
3901 
3902   PetscFunctionBegin;
3903   if (scall == MAT_INITIAL_MATRIX) {
3904     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3905     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3906     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3907   }
3908   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3909   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3910   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3911   PetscFunctionReturn(0);
3912 }
3913 
3914 
3915 /*MC
3916    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
3917    based on compressed sparse row format.
3918 
3919    Options Database Keys:
3920 . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
3921 
3922   Level: beginner
3923 
3924 .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
3925 M*/
3926 
3927 /*MC
3928    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3929 
3930    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3931    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3932   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3933   for communicators controlling multiple processes.  It is recommended that you call both of
3934   the above preallocation routines for simplicity.
3935 
3936    Options Database Keys:
3937 . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3938 
3939   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3940    enough exist.
3941 
3942   Level: beginner
3943 
3944 .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3945 M*/
3946 
3947 /*MC
3948    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3949 
3950    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3951    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3952    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3953   for communicators controlling multiple processes.  It is recommended that you call both of
3954   the above preallocation routines for simplicity.
3955 
3956    Options Database Keys:
3957 . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3958 
3959   Level: beginner
3960 
3961 .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3962 M*/
3963 
3964 #if defined(PETSC_HAVE_PASTIX)
3965 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3966 #endif
3967 #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3968 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat*);
3969 #endif
3970 PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3971 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3972 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
3973 extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool*);
3974 #if defined(PETSC_HAVE_MUMPS)
3975 PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3976 #endif
3977 #if defined(PETSC_HAVE_SUPERLU)
3978 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3979 #endif
3980 #if defined(PETSC_HAVE_MKL_PARDISO)
3981 PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mkl_pardiso(Mat,MatFactorType,Mat*);
3982 #endif
3983 #if defined(PETSC_HAVE_SUPERLU_DIST)
3984 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3985 #endif
3986 #if defined(PETSC_HAVE_SUITESPARSE)
3987 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3988 #endif
3989 #if defined(PETSC_HAVE_SUITESPARSE)
3990 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3991 #endif
3992 #if defined(PETSC_HAVE_SUITESPARSE)
3993 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_klu(Mat,MatFactorType,Mat*);
3994 #endif
3995 #if defined(PETSC_HAVE_LUSOL)
3996 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3997 #endif
3998 #if defined(PETSC_HAVE_MATLAB_ENGINE)
3999 PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
4000 extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
4001 extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
4002 #endif
4003 #if defined(PETSC_HAVE_CLIQUE)
4004 PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
4005 #endif
4006 
4007 
4008 #undef __FUNCT__
4009 #define __FUNCT__ "MatSeqAIJGetArray"
4010 /*@C
4011    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
4012 
4013    Not Collective
4014 
4015    Input Parameter:
4016 .  mat - a MATSEQDENSE matrix
4017 
4018    Output Parameter:
4019 .   array - pointer to the data
4020 
4021    Level: intermediate
4022 
4023 .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4024 @*/
4025 PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
4026 {
4027   PetscErrorCode ierr;
4028 
4029   PetscFunctionBegin;
4030   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
4031   PetscFunctionReturn(0);
4032 }
4033 
4034 #undef __FUNCT__
4035 #define __FUNCT__ "MatSeqAIJGetMaxRowNonzeros"
4036 /*@C
4037    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
4038 
4039    Not Collective
4040 
4041    Input Parameter:
4042 .  mat - a MATSEQDENSE matrix
4043 
4044    Output Parameter:
4045 .   nz - the maximum number of nonzeros in any row
4046 
4047    Level: intermediate
4048 
4049 .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4050 @*/
4051 PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4052 {
4053   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
4054 
4055   PetscFunctionBegin;
4056   *nz = aij->rmax;
4057   PetscFunctionReturn(0);
4058 }
4059 
4060 #undef __FUNCT__
4061 #define __FUNCT__ "MatSeqAIJRestoreArray"
4062 /*@C
4063    MatSeqAIJRestoreArray - returns access to the array where the data for a SeqSeqAIJ matrix is stored obtained by MatSeqAIJGetArray()
4064 
4065    Not Collective
4066 
4067    Input Parameters:
4068 .  mat - a MATSEQDENSE matrix
4069 .  array - pointer to the data
4070 
4071    Level: intermediate
4072 
4073 .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4074 @*/
4075 PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4076 {
4077   PetscErrorCode ierr;
4078 
4079   PetscFunctionBegin;
4080   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
4081   PetscFunctionReturn(0);
4082 }
4083 
4084 #undef __FUNCT__
4085 #define __FUNCT__ "MatCreate_SeqAIJ"
4086 PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4087 {
4088   Mat_SeqAIJ     *b;
4089   PetscErrorCode ierr;
4090   PetscMPIInt    size;
4091 
4092   PetscFunctionBegin;
4093   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4094   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4095 
4096   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
4097 
4098   B->data = (void*)b;
4099 
4100   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
4101 
4102   b->row                = 0;
4103   b->col                = 0;
4104   b->icol               = 0;
4105   b->reallocs           = 0;
4106   b->ignorezeroentries  = PETSC_FALSE;
4107   b->roworiented        = PETSC_TRUE;
4108   b->nonew              = 0;
4109   b->diag               = 0;
4110   b->solve_work         = 0;
4111   B->spptr              = 0;
4112   b->saved_values       = 0;
4113   b->idiag              = 0;
4114   b->mdiag              = 0;
4115   b->ssor_work          = 0;
4116   b->omega              = 1.0;
4117   b->fshift             = 0.0;
4118   b->idiagvalid         = PETSC_FALSE;
4119   b->ibdiagvalid        = PETSC_FALSE;
4120   b->keepnonzeropattern = PETSC_FALSE;
4121   b->xtoy               = 0;
4122   b->XtoY               = 0;
4123 
4124   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4125   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4126   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
4127 
4128 #if defined(PETSC_HAVE_MATLAB_ENGINE)
4129   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_matlab_C",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
4130   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4131   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4132 #endif
4133 #if defined(PETSC_HAVE_PASTIX)
4134   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
4135 #endif
4136 #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
4137   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_essl_C",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
4138 #endif
4139 #if defined(PETSC_HAVE_SUPERLU)
4140   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_C",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
4141 #endif
4142 #if defined(PETSC_HAVE_MKL_PARDISO)
4143   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mkl_pardiso_C",MatGetFactor_aij_mkl_pardiso);CHKERRQ(ierr);
4144 #endif
4145 #if defined(PETSC_HAVE_SUPERLU_DIST)
4146   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
4147 #endif
4148 #if defined(PETSC_HAVE_MUMPS)
4149   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr);
4150 #endif
4151 #if defined(PETSC_HAVE_SUITESPARSE)
4152   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_umfpack_C",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
4153 #endif
4154 #if defined(PETSC_HAVE_SUITESPARSE)
4155   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_cholmod_C",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
4156 #endif
4157 #if defined(PETSC_HAVE_SUITESPARSE)
4158   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_klu_C",MatGetFactor_seqaij_klu);CHKERRQ(ierr);
4159 #endif
4160 #if defined(PETSC_HAVE_LUSOL)
4161   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_lusol_C",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
4162 #endif
4163 #if defined(PETSC_HAVE_CLIQUE)
4164   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr);
4165 #endif
4166 
4167   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_petsc_C",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
4168   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactorAvailable_petsc_C",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
4169   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_bas_C",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
4170   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4171   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4172   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4173   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4174   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4175   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
4176   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4177   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4178   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4179   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4180   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4181   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4182   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4183   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4184   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
4185   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
4186   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4187   PetscFunctionReturn(0);
4188 }
4189 
4190 #undef __FUNCT__
4191 #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4192 /*
4193     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4194 */
4195 PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4196 {
4197   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4198   PetscErrorCode ierr;
4199   PetscInt       i,m = A->rmap->n;
4200 
4201   PetscFunctionBegin;
4202   c = (Mat_SeqAIJ*)C->data;
4203 
4204   C->factortype = A->factortype;
4205   c->row        = 0;
4206   c->col        = 0;
4207   c->icol       = 0;
4208   c->reallocs   = 0;
4209 
4210   C->assembled = PETSC_TRUE;
4211 
4212   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4213   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4214 
4215   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
4216   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
4217   for (i=0; i<m; i++) {
4218     c->imax[i] = a->imax[i];
4219     c->ilen[i] = a->ilen[i];
4220   }
4221 
4222   /* allocate the matrix space */
4223   if (mallocmatspace) {
4224     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
4225     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
4226 
4227     c->singlemalloc = PETSC_TRUE;
4228 
4229     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
4230     if (m > 0) {
4231       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4232       if (cpvalues == MAT_COPY_VALUES) {
4233         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4234       } else {
4235         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4236       }
4237     }
4238   }
4239 
4240   c->ignorezeroentries = a->ignorezeroentries;
4241   c->roworiented       = a->roworiented;
4242   c->nonew             = a->nonew;
4243   if (a->diag) {
4244     ierr = PetscMalloc1((m+1),&c->diag);CHKERRQ(ierr);
4245     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
4246     for (i=0; i<m; i++) {
4247       c->diag[i] = a->diag[i];
4248     }
4249   } else c->diag = 0;
4250 
4251   c->solve_work         = 0;
4252   c->saved_values       = 0;
4253   c->idiag              = 0;
4254   c->ssor_work          = 0;
4255   c->keepnonzeropattern = a->keepnonzeropattern;
4256   c->free_a             = PETSC_TRUE;
4257   c->free_ij            = PETSC_TRUE;
4258   c->xtoy               = 0;
4259   c->XtoY               = 0;
4260 
4261   c->rmax         = a->rmax;
4262   c->nz           = a->nz;
4263   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4264   C->preallocated = PETSC_TRUE;
4265 
4266   c->compressedrow.use   = a->compressedrow.use;
4267   c->compressedrow.nrows = a->compressedrow.nrows;
4268   if (a->compressedrow.use) {
4269     i    = a->compressedrow.nrows;
4270     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
4271     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
4272     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
4273   } else {
4274     c->compressedrow.use    = PETSC_FALSE;
4275     c->compressedrow.i      = NULL;
4276     c->compressedrow.rindex = NULL;
4277   }
4278   C->nonzerostate = A->nonzerostate;
4279 
4280   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4281   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
4282   PetscFunctionReturn(0);
4283 }
4284 
4285 #undef __FUNCT__
4286 #define __FUNCT__ "MatDuplicate_SeqAIJ"
4287 PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4288 {
4289   PetscErrorCode ierr;
4290 
4291   PetscFunctionBegin;
4292   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
4293   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4294   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4295     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4296   }
4297   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4298   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4299   PetscFunctionReturn(0);
4300 }
4301 
4302 #undef __FUNCT__
4303 #define __FUNCT__ "MatLoad_SeqAIJ"
4304 PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4305 {
4306   Mat_SeqAIJ     *a;
4307   PetscErrorCode ierr;
4308   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4309   int            fd;
4310   PetscMPIInt    size;
4311   MPI_Comm       comm;
4312   PetscInt       bs = 1;
4313 
4314   PetscFunctionBegin;
4315   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4316   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4317   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4318 
4319   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
4320   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4321   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4322   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4323 
4324   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4325   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4326   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4327   M = header[1]; N = header[2]; nz = header[3];
4328 
4329   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4330 
4331   /* read in row lengths */
4332   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4333   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4334 
4335   /* check if sum of rowlengths is same as nz */
4336   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4337   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %dD, sum-row-lengths = %D\n",nz,sum);
4338 
4339   /* set global size if not set already*/
4340   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4341     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4342   } else {
4343     /* if sizes and type are already set, check if the vector global sizes are correct */
4344     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
4345     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4346       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
4347     }
4348     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);
4349   }
4350   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4351   a    = (Mat_SeqAIJ*)newMat->data;
4352 
4353   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4354 
4355   /* read in nonzero values */
4356   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4357 
4358   /* set matrix "i" values */
4359   a->i[0] = 0;
4360   for (i=1; i<= M; i++) {
4361     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4362     a->ilen[i-1] = rowlengths[i-1];
4363   }
4364   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4365 
4366   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4367   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4368   PetscFunctionReturn(0);
4369 }
4370 
4371 #undef __FUNCT__
4372 #define __FUNCT__ "MatEqual_SeqAIJ"
4373 PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4374 {
4375   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4376   PetscErrorCode ierr;
4377 #if defined(PETSC_USE_COMPLEX)
4378   PetscInt k;
4379 #endif
4380 
4381   PetscFunctionBegin;
4382   /* If the  matrix dimensions are not equal,or no of nonzeros */
4383   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4384     *flg = PETSC_FALSE;
4385     PetscFunctionReturn(0);
4386   }
4387 
4388   /* if the a->i are the same */
4389   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4390   if (!*flg) PetscFunctionReturn(0);
4391 
4392   /* if a->j are the same */
4393   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4394   if (!*flg) PetscFunctionReturn(0);
4395 
4396   /* if a->a are the same */
4397 #if defined(PETSC_USE_COMPLEX)
4398   for (k=0; k<a->nz; k++) {
4399     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4400       *flg = PETSC_FALSE;
4401       PetscFunctionReturn(0);
4402     }
4403   }
4404 #else
4405   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4406 #endif
4407   PetscFunctionReturn(0);
4408 }
4409 
4410 #undef __FUNCT__
4411 #define __FUNCT__ "MatCreateSeqAIJWithArrays"
4412 /*@
4413      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4414               provided by the user.
4415 
4416       Collective on MPI_Comm
4417 
4418    Input Parameters:
4419 +   comm - must be an MPI communicator of size 1
4420 .   m - number of rows
4421 .   n - number of columns
4422 .   i - row indices
4423 .   j - column indices
4424 -   a - matrix values
4425 
4426    Output Parameter:
4427 .   mat - the matrix
4428 
4429    Level: intermediate
4430 
4431    Notes:
4432        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4433     once the matrix is destroyed and not before
4434 
4435        You cannot set new nonzero locations into this matrix, that will generate an error.
4436 
4437        The i and j indices are 0 based
4438 
4439        The format which is used for the sparse matrix input, is equivalent to a
4440     row-major ordering.. i.e for the following matrix, the input data expected is
4441     as shown:
4442 
4443         1 0 0
4444         2 0 3
4445         4 5 6
4446 
4447         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4448         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4449         v =  {1,2,3,4,5,6}  [size = nz = 6]
4450 
4451 
4452 .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
4453 
4454 @*/
4455 PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
4456 {
4457   PetscErrorCode ierr;
4458   PetscInt       ii;
4459   Mat_SeqAIJ     *aij;
4460 #if defined(PETSC_USE_DEBUG)
4461   PetscInt jj;
4462 #endif
4463 
4464   PetscFunctionBegin;
4465   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4466   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4467   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4468   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4469   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4470   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4471   aij  = (Mat_SeqAIJ*)(*mat)->data;
4472   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4473 
4474   aij->i            = i;
4475   aij->j            = j;
4476   aij->a            = a;
4477   aij->singlemalloc = PETSC_FALSE;
4478   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4479   aij->free_a       = PETSC_FALSE;
4480   aij->free_ij      = PETSC_FALSE;
4481 
4482   for (ii=0; ii<m; ii++) {
4483     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4484 #if defined(PETSC_USE_DEBUG)
4485     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]);
4486     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4487       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4488       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
4489     }
4490 #endif
4491   }
4492 #if defined(PETSC_USE_DEBUG)
4493   for (ii=0; ii<aij->i[m]; ii++) {
4494     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4495     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]);
4496   }
4497 #endif
4498 
4499   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4500   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4501   PetscFunctionReturn(0);
4502 }
4503 #undef __FUNCT__
4504 #define __FUNCT__ "MatCreateSeqAIJFromTriple"
4505 /*@C
4506      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4507               provided by the user.
4508 
4509       Collective on MPI_Comm
4510 
4511    Input Parameters:
4512 +   comm - must be an MPI communicator of size 1
4513 .   m   - number of rows
4514 .   n   - number of columns
4515 .   i   - row indices
4516 .   j   - column indices
4517 .   a   - matrix values
4518 .   nz  - number of nonzeros
4519 -   idx - 0 or 1 based
4520 
4521    Output Parameter:
4522 .   mat - the matrix
4523 
4524    Level: intermediate
4525 
4526    Notes:
4527        The i and j indices are 0 based
4528 
4529        The format which is used for the sparse matrix input, is equivalent to a
4530     row-major ordering.. i.e for the following matrix, the input data expected is
4531     as shown:
4532 
4533         1 0 0
4534         2 0 3
4535         4 5 6
4536 
4537         i =  {0,1,1,2,2,2}
4538         j =  {0,0,2,0,1,2}
4539         v =  {1,2,3,4,5,6}
4540 
4541 
4542 .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
4543 
4544 @*/
4545 PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
4546 {
4547   PetscErrorCode ierr;
4548   PetscInt       ii, *nnz, one = 1,row,col;
4549 
4550 
4551   PetscFunctionBegin;
4552   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
4553   for (ii = 0; ii < nz; ii++) {
4554     nnz[i[ii] - !!idx] += 1;
4555   }
4556   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4557   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4558   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4559   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
4560   for (ii = 0; ii < nz; ii++) {
4561     if (idx) {
4562       row = i[ii] - 1;
4563       col = j[ii] - 1;
4564     } else {
4565       row = i[ii];
4566       col = j[ii];
4567     }
4568     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
4569   }
4570   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4571   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4572   ierr = PetscFree(nnz);CHKERRQ(ierr);
4573   PetscFunctionReturn(0);
4574 }
4575 
4576 #undef __FUNCT__
4577 #define __FUNCT__ "MatSetColoring_SeqAIJ"
4578 PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4579 {
4580   PetscErrorCode ierr;
4581   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4582 
4583   PetscFunctionBegin;
4584   if (coloring->ctype == IS_COLORING_GLOBAL) {
4585     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4586     a->coloring = coloring;
4587   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
4588     PetscInt        i,*larray;
4589     ISColoring      ocoloring;
4590     ISColoringValue *colors;
4591 
4592     /* set coloring for diagonal portion */
4593     ierr = PetscMalloc1(A->cmap->n,&larray);CHKERRQ(ierr);
4594     for (i=0; i<A->cmap->n; i++) larray[i] = i;
4595     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4596     ierr = PetscMalloc1(A->cmap->n,&colors);CHKERRQ(ierr);
4597     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
4598     ierr        = PetscFree(larray);CHKERRQ(ierr);
4599     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4600     a->coloring = ocoloring;
4601   }
4602   PetscFunctionReturn(0);
4603 }
4604 
4605 #undef __FUNCT__
4606 #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
4607 PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4608 {
4609   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4610   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
4611   MatScalar       *v      = a->a;
4612   PetscScalar     *values = (PetscScalar*)advalues;
4613   ISColoringValue *color;
4614 
4615   PetscFunctionBegin;
4616   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4617   color = a->coloring->colors;
4618   /* loop over rows */
4619   for (i=0; i<m; i++) {
4620     nz = ii[i+1] - ii[i];
4621     /* loop over columns putting computed value into matrix */
4622     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4623     values += nl; /* jump to next row of derivatives */
4624   }
4625   PetscFunctionReturn(0);
4626 }
4627 
4628 #undef __FUNCT__
4629 #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4630 PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4631 {
4632   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4633   PetscErrorCode ierr;
4634 
4635   PetscFunctionBegin;
4636   a->idiagvalid  = PETSC_FALSE;
4637   a->ibdiagvalid = PETSC_FALSE;
4638 
4639   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4640   PetscFunctionReturn(0);
4641 }
4642 
4643 /*
4644     Special version for direct calls from Fortran
4645 */
4646 #include <petsc-private/fortranimpl.h>
4647 #if defined(PETSC_HAVE_FORTRAN_CAPS)
4648 #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4649 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4650 #define matsetvaluesseqaij_ matsetvaluesseqaij
4651 #endif
4652 
4653 /* Change these macros so can be used in void function */
4654 #undef CHKERRQ
4655 #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4656 #undef SETERRQ2
4657 #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4658 #undef SETERRQ3
4659 #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
4660 
4661 #undef __FUNCT__
4662 #define __FUNCT__ "matsetvaluesseqaij_"
4663 PETSC_EXTERN void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
4664 {
4665   Mat            A  = *AA;
4666   PetscInt       m  = *mm, n = *nn;
4667   InsertMode     is = *isis;
4668   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4669   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
4670   PetscInt       *imax,*ai,*ailen;
4671   PetscErrorCode ierr;
4672   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
4673   MatScalar      *ap,value,*aa;
4674   PetscBool      ignorezeroentries = a->ignorezeroentries;
4675   PetscBool      roworiented       = a->roworiented;
4676 
4677   PetscFunctionBegin;
4678   MatCheckPreallocated(A,1);
4679   imax  = a->imax;
4680   ai    = a->i;
4681   ailen = a->ilen;
4682   aj    = a->j;
4683   aa    = a->a;
4684 
4685   for (k=0; k<m; k++) { /* loop over added rows */
4686     row = im[k];
4687     if (row < 0) continue;
4688 #if defined(PETSC_USE_DEBUG)
4689     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
4690 #endif
4691     rp   = aj + ai[row]; ap = aa + ai[row];
4692     rmax = imax[row]; nrow = ailen[row];
4693     low  = 0;
4694     high = nrow;
4695     for (l=0; l<n; l++) { /* loop over added columns */
4696       if (in[l] < 0) continue;
4697 #if defined(PETSC_USE_DEBUG)
4698       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
4699 #endif
4700       col = in[l];
4701       if (roworiented) value = v[l + k*n];
4702       else value = v[k + l*m];
4703 
4704       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
4705 
4706       if (col <= lastcol) low = 0;
4707       else high = nrow;
4708       lastcol = col;
4709       while (high-low > 5) {
4710         t = (low+high)/2;
4711         if (rp[t] > col) high = t;
4712         else             low  = t;
4713       }
4714       for (i=low; i<high; i++) {
4715         if (rp[i] > col) break;
4716         if (rp[i] == col) {
4717           if (is == ADD_VALUES) ap[i] += value;
4718           else                  ap[i] = value;
4719           goto noinsert;
4720         }
4721       }
4722       if (value == 0.0 && ignorezeroentries) goto noinsert;
4723       if (nonew == 1) goto noinsert;
4724       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4725       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
4726       N = nrow++ - 1; a->nz++; high++;
4727       /* shift up all the later entries in this row */
4728       for (ii=N; ii>=i; ii--) {
4729         rp[ii+1] = rp[ii];
4730         ap[ii+1] = ap[ii];
4731       }
4732       rp[i] = col;
4733       ap[i] = value;
4734       A->nonzerostate++;
4735 noinsert:;
4736       low = i + 1;
4737     }
4738     ailen[row] = nrow;
4739   }
4740   PetscFunctionReturnVoid();
4741 }
4742 
4743 
4744