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