xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 16371a99de77e69603adb597139089a2a64ee132)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
89e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
9f5eb4b81SSatish Balay #include "src/inline/spops.h"
108d195f9aSBarry Smith #include "src/inline/dot.h"
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
134a2ae208SSatish Balay #undef __FUNCT__
1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1579299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1679299369SBarry Smith {
1779299369SBarry Smith   PetscErrorCode ierr;
1879299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
19899cda47SBarry Smith   PetscInt       i,*diag, m = Y->rmap.n;
2054f21887SBarry Smith   MatScalar      *aa = aij->a;
2154f21887SBarry Smith   PetscScalar    *v;
2209f38230SBarry Smith   PetscTruth     missing;
2379299369SBarry Smith 
2479299369SBarry Smith   PetscFunctionBegin;
2509f38230SBarry Smith   if (Y->assembled) {
2609f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2709f38230SBarry Smith     if (!missing) {
2879299369SBarry Smith       diag = aij->diag;
2979299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3079299369SBarry Smith       if (is == INSERT_VALUES) {
3179299369SBarry Smith 	for (i=0; i<m; i++) {
3279299369SBarry Smith 	  aa[diag[i]] = v[i];
3379299369SBarry Smith 	}
3479299369SBarry Smith       } else {
3579299369SBarry Smith 	for (i=0; i<m; i++) {
3679299369SBarry Smith 	  aa[diag[i]] += v[i];
3779299369SBarry Smith 	}
3879299369SBarry Smith       }
3979299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4079299369SBarry Smith       PetscFunctionReturn(0);
4179299369SBarry Smith     }
4209f38230SBarry Smith   }
4309f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4409f38230SBarry Smith   PetscFunctionReturn(0);
4509f38230SBarry Smith }
4679299369SBarry Smith 
4779299369SBarry Smith #undef __FUNCT__
484a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
498f7157efSSatish Balay PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5017ab2063SBarry Smith {
51416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
52dfbe8321SBarry Smith   PetscErrorCode ierr;
5397f1f81fSBarry Smith   PetscInt       i,ishift;
5417ab2063SBarry Smith 
553a40ed3dSBarry Smith   PetscFunctionBegin;
56899cda47SBarry Smith   *m     = A->rmap.n;
573a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
58bfeeae90SHong Zhang   ishift = 0;
5953e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
60899cda47SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
61bfeeae90SHong Zhang   } else if (oshift == 1) {
62899cda47SBarry Smith     PetscInt nz = a->i[A->rmap.n];
633b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
64899cda47SBarry Smith     ierr = PetscMalloc((A->rmap.n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
65ecc77c7aSBarry Smith     for (i=0; i<A->rmap.n+1; i++) (*ia)[i] = a->i[i] + 1;
66ecc77c7aSBarry Smith     if (ja) {
6797f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
683b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
69ecc77c7aSBarry Smith     }
706945ee14SBarry Smith   } else {
71ecc77c7aSBarry Smith     *ia = a->i;
72ecc77c7aSBarry Smith     if (ja) *ja = a->j;
73a2ce50c7SBarry Smith   }
743a40ed3dSBarry Smith   PetscFunctionReturn(0);
75a2744918SBarry Smith }
76a2744918SBarry Smith 
774a2ae208SSatish Balay #undef __FUNCT__
784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
798f7157efSSatish Balay PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
806945ee14SBarry Smith {
81dfbe8321SBarry Smith   PetscErrorCode ierr;
826945ee14SBarry Smith 
833a40ed3dSBarry Smith   PetscFunctionBegin;
843a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
85bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
86606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
87ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
88bcd2baecSBarry Smith   }
893a40ed3dSBarry Smith   PetscFunctionReturn(0);
9017ab2063SBarry Smith }
9117ab2063SBarry Smith 
924a2ae208SSatish Balay #undef __FUNCT__
934a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
948f7157efSSatish Balay PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
953b2fbd54SBarry Smith {
963b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
97dfbe8321SBarry Smith   PetscErrorCode ierr;
98899cda47SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap.n,m = A->rmap.n;
9997f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1003b2fbd54SBarry Smith 
1013a40ed3dSBarry Smith   PetscFunctionBegin;
102899cda47SBarry Smith   *nn = n;
1033a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1043b2fbd54SBarry Smith   if (symmetric) {
105899cda47SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1063b2fbd54SBarry Smith   } else {
10797f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1113b2fbd54SBarry Smith     jj = a->j;
1123b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
113bfeeae90SHong Zhang       collengths[jj[i]]++;
1143b2fbd54SBarry Smith     }
1153b2fbd54SBarry Smith     cia[0] = oshift;
1163b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1173b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1183b2fbd54SBarry Smith     }
11997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1203b2fbd54SBarry Smith     jj   = a->j;
121a93ec695SBarry Smith     for (row=0; row<m; row++) {
122a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
123a93ec695SBarry Smith       for (i=0; i<mr; i++) {
124bfeeae90SHong Zhang         col = *jj++;
1253b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1263b2fbd54SBarry Smith       }
1273b2fbd54SBarry Smith     }
128606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1293b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1303b2fbd54SBarry Smith   }
1313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1323b2fbd54SBarry Smith }
1333b2fbd54SBarry Smith 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1368f7157efSSatish Balay PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1373b2fbd54SBarry Smith {
138dfbe8321SBarry Smith   PetscErrorCode ierr;
139606d414cSSatish Balay 
1403a40ed3dSBarry Smith   PetscFunctionBegin;
1413a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1423b2fbd54SBarry Smith 
143606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
144606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1453b2fbd54SBarry Smith 
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1473b2fbd54SBarry Smith }
1483b2fbd54SBarry Smith 
14987d4246cSBarry Smith #undef __FUNCT__
15087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15287d4246cSBarry Smith {
15387d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15487d4246cSBarry Smith   PetscInt       *ai = a->i;
15587d4246cSBarry Smith   PetscErrorCode ierr;
15687d4246cSBarry Smith 
15787d4246cSBarry Smith   PetscFunctionBegin;
15887d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
15987d4246cSBarry Smith   PetscFunctionReturn(0);
16087d4246cSBarry Smith }
16187d4246cSBarry Smith 
162227d817aSBarry Smith #define CHUNKSIZE   15
16317ab2063SBarry Smith 
1644a2ae208SSatish Balay #undef __FUNCT__
1654a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16717ab2063SBarry Smith {
168416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
169e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
17097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1716849ba73SBarry Smith   PetscErrorCode ierr;
172e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17354f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
174edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
175273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17617ab2063SBarry Smith 
1773a40ed3dSBarry Smith   PetscFunctionBegin;
17817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
179416022c9SBarry Smith     row  = im[k];
1805ef9f2a5SBarry Smith     if (row < 0) continue;
1812515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
182899cda47SBarry Smith     if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1);
1833b2fbd54SBarry Smith #endif
184bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
186416022c9SBarry Smith     low  = 0;
187c71e6ed7SBarry Smith     high = nrow;
18817ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1895ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1902515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
191899cda47SBarry Smith       if (in[l] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap.n-1);
1923b2fbd54SBarry Smith #endif
193bfeeae90SHong Zhang       col = in[l];
194*16371a99SBarry Smith       if (v) {
1954b0e389bSBarry Smith 	if (roworiented) {
1965ef9f2a5SBarry Smith 	  value = v[l + k*n];
197bef8e0ddSBarry Smith 	} else {
1984b0e389bSBarry Smith 	  value = v[k + l*m];
1994b0e389bSBarry Smith 	}
200*16371a99SBarry Smith       } else {
201*16371a99SBarry Smith         value = 0;
202*16371a99SBarry Smith       }
203abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
20436db0b34SBarry Smith 
2057cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
206e2ee6c50SBarry Smith       lastcol = col;
207416022c9SBarry Smith       while (high-low > 5) {
208416022c9SBarry Smith         t = (low+high)/2;
209416022c9SBarry Smith         if (rp[t] > col) high = t;
210416022c9SBarry Smith         else             low  = t;
21117ab2063SBarry Smith       }
212416022c9SBarry Smith       for (i=low; i<high; i++) {
21317ab2063SBarry Smith         if (rp[i] > col) break;
21417ab2063SBarry Smith         if (rp[i] == col) {
215416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
21617ab2063SBarry Smith           else                  ap[i] = value;
217e44c0bd4SBarry Smith           low = i + 1;
21817ab2063SBarry Smith           goto noinsert;
21917ab2063SBarry Smith         }
22017ab2063SBarry Smith       }
221abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
222c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
223cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
224421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
225c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
226416022c9SBarry Smith       /* shift up all the later entries in this row */
227416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
22817ab2063SBarry Smith         rp[ii+1] = rp[ii];
22917ab2063SBarry Smith         ap[ii+1] = ap[ii];
23017ab2063SBarry Smith       }
23117ab2063SBarry Smith       rp[i] = col;
23217ab2063SBarry Smith       ap[i] = value;
233416022c9SBarry Smith       low   = i + 1;
234e44c0bd4SBarry Smith       noinsert:;
23517ab2063SBarry Smith     }
23617ab2063SBarry Smith     ailen[row] = nrow;
23717ab2063SBarry Smith   }
23888e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2393a40ed3dSBarry Smith   PetscFunctionReturn(0);
24017ab2063SBarry Smith }
24117ab2063SBarry Smith 
24281824310SBarry Smith 
2434a2ae208SSatish Balay #undef __FUNCT__
2444a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
245a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2467eb43aa7SLois Curfman McInnes {
2477eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
24897f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
24997f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
25054f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2517eb43aa7SLois Curfman McInnes 
2523a40ed3dSBarry Smith   PetscFunctionBegin;
2537eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2547eb43aa7SLois Curfman McInnes     row  = im[k];
25597e567efSBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
256899cda47SBarry Smith     if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1);
257bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2587eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2597eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
26097e567efSBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
261899cda47SBarry Smith       if (in[l] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap.n-1);
262bfeeae90SHong Zhang       col = in[l] ;
2637eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2647eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2657eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2667eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2677eb43aa7SLois Curfman McInnes         else             low  = t;
2687eb43aa7SLois Curfman McInnes       }
2697eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2707eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2717eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
272b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2737eb43aa7SLois Curfman McInnes           goto finished;
2747eb43aa7SLois Curfman McInnes         }
2757eb43aa7SLois Curfman McInnes       }
27697e567efSBarry Smith       *v++ = 0.0;
2777eb43aa7SLois Curfman McInnes       finished:;
2787eb43aa7SLois Curfman McInnes     }
2797eb43aa7SLois Curfman McInnes   }
2803a40ed3dSBarry Smith   PetscFunctionReturn(0);
2817eb43aa7SLois Curfman McInnes }
2827eb43aa7SLois Curfman McInnes 
28317ab2063SBarry Smith 
2844a2ae208SSatish Balay #undef __FUNCT__
2854a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
286dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28717ab2063SBarry Smith {
288416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2896849ba73SBarry Smith   PetscErrorCode ierr;
2906f69ff64SBarry Smith   PetscInt       i,*col_lens;
2916f69ff64SBarry Smith   int            fd;
29217ab2063SBarry Smith 
2933a40ed3dSBarry Smith   PetscFunctionBegin;
294b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
295899cda47SBarry Smith   ierr = PetscMalloc((4+A->rmap.n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
296552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
297899cda47SBarry Smith   col_lens[1] = A->rmap.n;
298899cda47SBarry Smith   col_lens[2] = A->cmap.n;
299416022c9SBarry Smith   col_lens[3] = a->nz;
300416022c9SBarry Smith 
301416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
302899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
303416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30417ab2063SBarry Smith   }
305899cda47SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap.n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
306606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
307416022c9SBarry Smith 
308416022c9SBarry Smith   /* store column indices (zero start index) */
3096f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
310416022c9SBarry Smith 
311416022c9SBarry Smith   /* store nonzero values */
3126f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3133a40ed3dSBarry Smith   PetscFunctionReturn(0);
31417ab2063SBarry Smith }
315416022c9SBarry Smith 
316dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
317cd155464SBarry Smith 
3184a2ae208SSatish Balay #undef __FUNCT__
3194a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
320dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
321416022c9SBarry Smith {
322416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
323dfbe8321SBarry Smith   PetscErrorCode    ierr;
324899cda47SBarry Smith   PetscInt          i,j,m = A->rmap.n,shift=0;
325e060cb09SBarry Smith   const char        *name;
326f3ef73ceSBarry Smith   PetscViewerFormat format;
32717ab2063SBarry Smith 
3283a40ed3dSBarry Smith   PetscFunctionBegin;
329435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
330b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33171c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33297f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
333899cda47SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap.n-!shift)) {
334d00d2cf4SBarry Smith       nofinalvalue = 1;
335d00d2cf4SBarry Smith     }
336b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
337899cda47SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap.n);CHKERRQ(ierr);
33877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
33977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
340b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34117ab2063SBarry Smith 
34217ab2063SBarry Smith     for (i=0; i<m; i++) {
343416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
344aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34577431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34617ab2063SBarry Smith #else
34777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34817ab2063SBarry Smith #endif
34917ab2063SBarry Smith       }
35017ab2063SBarry Smith     }
351d00d2cf4SBarry Smith     if (nofinalvalue) {
352899cda47SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap.n,0.0);CHKERRQ(ierr);
353d00d2cf4SBarry Smith     }
354fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
355b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35668369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
357cd155464SBarry Smith      PetscFunctionReturn(0);
358fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
359b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36044cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36177431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36244cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
363aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36436db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
365a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36636db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
367a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36836db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
369a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3706831982aSBarry Smith         }
37144cd7ae7SLois Curfman McInnes #else
372a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37344cd7ae7SLois Curfman McInnes #endif
37444cd7ae7SLois Curfman McInnes       }
375b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37644cd7ae7SLois Curfman McInnes     }
377b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
378fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
37997f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
380b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
38197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
382496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
383496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
384496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
385496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
386aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
388496be53dSLois Curfman McInnes #else
389496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
390496be53dSLois Curfman McInnes #endif
391496be53dSLois Curfman McInnes         }
392496be53dSLois Curfman McInnes       }
393496be53dSLois Curfman McInnes     }
3942e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3962e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39777431f27SBarry Smith       if (i+4<m) {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);}
39877431f27SBarry Smith       else if (i+3<m) {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);}
39977431f27SBarry Smith       else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);}
40077431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40177431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40277431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
403496be53dSLois Curfman McInnes     }
404b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
405606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
406496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
407496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
40877431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
409496be53dSLois Curfman McInnes       }
410b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
411496be53dSLois Curfman McInnes     }
412b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
413496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
414496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
415496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
416aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
418b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4196831982aSBarry Smith           }
420496be53dSLois Curfman McInnes #else
421b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
422496be53dSLois Curfman McInnes #endif
423496be53dSLois Curfman McInnes         }
424496be53dSLois Curfman McInnes       }
425b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
426496be53dSLois Curfman McInnes     }
427b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
428fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
42997f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
43087828ca2SBarry Smith     PetscScalar value;
43102594712SBarry Smith 
432b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43302594712SBarry Smith     for (i=0; i<m; i++) {
43402594712SBarry Smith       jcnt = 0;
435899cda47SBarry Smith       for (j=0; j<A->cmap.n; j++) {
436e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43702594712SBarry Smith           value = a->a[cnt++];
438e24b481bSBarry Smith           jcnt++;
43902594712SBarry Smith         } else {
44002594712SBarry Smith           value = 0.0;
44102594712SBarry Smith         }
442aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
443b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44402594712SBarry Smith #else
445b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44602594712SBarry Smith #endif
44702594712SBarry Smith       }
448b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44902594712SBarry Smith     }
450b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4513a40ed3dSBarry Smith   } else {
452b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
45317ab2063SBarry Smith     for (i=0; i<m; i++) {
45477431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
455416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
456aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
45736db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
458a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
45936db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
460a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4613a40ed3dSBarry Smith         } else {
462a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
46317ab2063SBarry Smith         }
46417ab2063SBarry Smith #else
465a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
46617ab2063SBarry Smith #endif
46717ab2063SBarry Smith       }
468b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
46917ab2063SBarry Smith     }
470b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
47117ab2063SBarry Smith   }
472b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4733a40ed3dSBarry Smith   PetscFunctionReturn(0);
474416022c9SBarry Smith }
475416022c9SBarry Smith 
4764a2ae208SSatish Balay #undef __FUNCT__
4774a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
478dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
479416022c9SBarry Smith {
480480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
481416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
482dfbe8321SBarry Smith   PetscErrorCode    ierr;
483899cda47SBarry Smith   PetscInt          i,j,m = A->rmap.n,color;
48436db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
485b0a32e0cSBarry Smith   PetscViewer       viewer;
486f3ef73ceSBarry Smith   PetscViewerFormat format;
487cddf8d76SBarry Smith 
4883a40ed3dSBarry Smith   PetscFunctionBegin;
489480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
490b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
49119bcc07fSBarry Smith 
492b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
493416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4940513a670SBarry Smith 
495fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4960513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
497b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
498416022c9SBarry Smith     for (i=0; i<m; i++) {
499cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
500bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
501bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
502aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50336db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
504cddf8d76SBarry Smith #else
505cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
506cddf8d76SBarry Smith #endif
507b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
508cddf8d76SBarry Smith       }
509cddf8d76SBarry Smith     }
510b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
511cddf8d76SBarry Smith     for (i=0; i<m; i++) {
512cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
513bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
514bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
515cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
516b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
517cddf8d76SBarry Smith       }
518cddf8d76SBarry Smith     }
519b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
520cddf8d76SBarry Smith     for (i=0; i<m; i++) {
521cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
522bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
523bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
524aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52536db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
526cddf8d76SBarry Smith #else
527cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
528cddf8d76SBarry Smith #endif
529b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
530416022c9SBarry Smith       }
531416022c9SBarry Smith     }
5320513a670SBarry Smith   } else {
5330513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5340513a670SBarry Smith     /* first determine max of all nonzero values */
53597f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
536b0a32e0cSBarry Smith     PetscDraw   popup;
53736db0b34SBarry Smith     PetscReal scale;
5380513a670SBarry Smith 
5390513a670SBarry Smith     for (i=0; i<nz; i++) {
5400513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5410513a670SBarry Smith     }
542b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
543b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
544b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5450513a670SBarry Smith     count = 0;
5460513a670SBarry Smith     for (i=0; i<m; i++) {
5470513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
548bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
549bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
55097f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
551b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5520513a670SBarry Smith         count++;
5530513a670SBarry Smith       }
5540513a670SBarry Smith     }
5550513a670SBarry Smith   }
556480ef9eaSBarry Smith   PetscFunctionReturn(0);
557480ef9eaSBarry Smith }
558cddf8d76SBarry Smith 
5594a2ae208SSatish Balay #undef __FUNCT__
5604a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
561dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
562480ef9eaSBarry Smith {
563dfbe8321SBarry Smith   PetscErrorCode ierr;
564b0a32e0cSBarry Smith   PetscDraw      draw;
56536db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
566480ef9eaSBarry Smith   PetscTruth     isnull;
567480ef9eaSBarry Smith 
568480ef9eaSBarry Smith   PetscFunctionBegin;
569b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
570b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
571480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
572480ef9eaSBarry Smith 
573480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
574899cda47SBarry Smith   xr  = A->cmap.n; yr = A->rmap.n; h = yr/10.0; w = xr/10.0;
575480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
576b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
577b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
578480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5793a40ed3dSBarry Smith   PetscFunctionReturn(0);
580416022c9SBarry Smith }
581416022c9SBarry Smith 
5824a2ae208SSatish Balay #undef __FUNCT__
5834a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
584dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
585416022c9SBarry Smith {
586dfbe8321SBarry Smith   PetscErrorCode ierr;
5876805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
588416022c9SBarry Smith 
5893a40ed3dSBarry Smith   PetscFunctionBegin;
59032077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
591fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
592fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
593c45a1595SBarry Smith   if (iascii) {
5943a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5950f5bd95cSBarry Smith   } else if (isbinary) {
5963a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5970f5bd95cSBarry Smith   } else if (isdraw) {
5983a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5995cd90555SBarry Smith   } else {
600958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
60117ab2063SBarry Smith   }
6024846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
6033a40ed3dSBarry Smith   PetscFunctionReturn(0);
60417ab2063SBarry Smith }
60519bcc07fSBarry Smith 
6064a2ae208SSatish Balay #undef __FUNCT__
6074a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
608dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
60917ab2063SBarry Smith {
610416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6116849ba73SBarry Smith   PetscErrorCode ierr;
61297f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
613899cda47SBarry Smith   PetscInt       m = A->rmap.n,*ip,N,*ailen = a->ilen,rmax = 0;
61454f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6153447b6efSHong Zhang   PetscReal      ratio=0.6;
61617ab2063SBarry Smith 
6173a40ed3dSBarry Smith   PetscFunctionBegin;
6183a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
61917ab2063SBarry Smith 
62043ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
62117ab2063SBarry Smith   for (i=1; i<m; i++) {
622416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
62317ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
62494a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
62517ab2063SBarry Smith     if (fshift) {
626bfeeae90SHong Zhang       ip = aj + ai[i] ;
627bfeeae90SHong Zhang       ap = aa + ai[i] ;
62817ab2063SBarry Smith       N  = ailen[i];
62917ab2063SBarry Smith       for (j=0; j<N; j++) {
63017ab2063SBarry Smith         ip[j-fshift] = ip[j];
63117ab2063SBarry Smith         ap[j-fshift] = ap[j];
63217ab2063SBarry Smith       }
63317ab2063SBarry Smith     }
63417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
63517ab2063SBarry Smith   }
63617ab2063SBarry Smith   if (m) {
63717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
63817ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
63917ab2063SBarry Smith   }
64017ab2063SBarry Smith   /* reset ilen and imax for each row */
64117ab2063SBarry Smith   for (i=0; i<m; i++) {
64217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
64317ab2063SBarry Smith   }
644bfeeae90SHong Zhang   a->nz = ai[m];
64517ab2063SBarry Smith 
64609f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
647899cda47SBarry Smith   ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap.n,fshift,a->nz);CHKERRQ(ierr);
648ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
649ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
650a0e1a203SBarry Smith 
651dd5f02e7SSatish Balay   a->reallocs          = 0;
6524e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
65336db0b34SBarry Smith   a->rmax              = rmax;
6544e220ebcSLois Curfman McInnes 
655cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
656317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
65788e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
65871c2f376SKris Buschelman 
6594846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
66071f1c65dSBarry Smith 
66171f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6623a40ed3dSBarry Smith   PetscFunctionReturn(0);
66317ab2063SBarry Smith }
66417ab2063SBarry Smith 
6654a2ae208SSatish Balay #undef __FUNCT__
66699cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
66799cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
66899cafbc1SBarry Smith {
66999cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
67099cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
67154f21887SBarry Smith   MatScalar      *aa = a->a;
67299cafbc1SBarry Smith 
67399cafbc1SBarry Smith   PetscFunctionBegin;
67499cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
67599cafbc1SBarry Smith   PetscFunctionReturn(0);
67699cafbc1SBarry Smith }
67799cafbc1SBarry Smith 
67899cafbc1SBarry Smith #undef __FUNCT__
67999cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
68099cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
68199cafbc1SBarry Smith {
68299cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
68399cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
68454f21887SBarry Smith   MatScalar      *aa = a->a;
68599cafbc1SBarry Smith 
68699cafbc1SBarry Smith   PetscFunctionBegin;
68799cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
68899cafbc1SBarry Smith   PetscFunctionReturn(0);
68999cafbc1SBarry Smith }
69099cafbc1SBarry Smith 
69199cafbc1SBarry Smith #undef __FUNCT__
6924a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
693dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
69417ab2063SBarry Smith {
695416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
696dfbe8321SBarry Smith   PetscErrorCode ierr;
6973a40ed3dSBarry Smith 
6983a40ed3dSBarry Smith   PetscFunctionBegin;
699899cda47SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr);
7003a40ed3dSBarry Smith   PetscFunctionReturn(0);
70117ab2063SBarry Smith }
702416022c9SBarry Smith 
7034a2ae208SSatish Balay #undef __FUNCT__
7044a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
705dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
70617ab2063SBarry Smith {
707416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
708dfbe8321SBarry Smith   PetscErrorCode ierr;
709d5d45c9bSBarry Smith 
7103a40ed3dSBarry Smith   PetscFunctionBegin;
711aa482453SBarry Smith #if defined(PETSC_USE_LOG)
712899cda47SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap.n,A->cmap.n,a->nz);
71317ab2063SBarry Smith #endif
714e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
715c38d4ed2SBarry Smith   if (a->row) {
716c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
717c38d4ed2SBarry Smith   }
718c38d4ed2SBarry Smith   if (a->col) {
719c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
720c38d4ed2SBarry Smith   }
72105b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
72205b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
72371f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
72405b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
72582bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
72605b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
727cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
72805b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
729407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
730d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
731a30b2313SHong Zhang 
7324846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7334846f1f5SKris Buschelman 
734606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
735901853e0SKris Buschelman 
736dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
737901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
738901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
739901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
740901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
741901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
742ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
743901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
744901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
745a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
746901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7473a40ed3dSBarry Smith   PetscFunctionReturn(0);
74817ab2063SBarry Smith }
74917ab2063SBarry Smith 
7504a2ae208SSatish Balay #undef __FUNCT__
7514a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
752dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
75317ab2063SBarry Smith {
7543a40ed3dSBarry Smith   PetscFunctionBegin;
7553a40ed3dSBarry Smith   PetscFunctionReturn(0);
75617ab2063SBarry Smith }
75717ab2063SBarry Smith 
7584a2ae208SSatish Balay #undef __FUNCT__
7594a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7604e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
76117ab2063SBarry Smith {
762416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7634846f1f5SKris Buschelman   PetscErrorCode ierr;
7643a40ed3dSBarry Smith 
7653a40ed3dSBarry Smith   PetscFunctionBegin;
766a65d3064SKris Buschelman   switch (op) {
767a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7684e0d8c25SBarry Smith       a->roworiented       = flg;
769a65d3064SKris Buschelman       break;
770a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
7714e0d8c25SBarry Smith       a->keepzeroedrows    = flg;
772a65d3064SKris Buschelman       break;
773512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
774512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
775a65d3064SKris Buschelman       break;
776a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
7774e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
778a65d3064SKris Buschelman       break;
779a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
7804e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
781a65d3064SKris Buschelman       break;
782a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
7834e0d8c25SBarry Smith       a->ignorezeroentries = flg;
7840df259c2SBarry Smith       break;
785d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
7864e0d8c25SBarry Smith       a->compressedrow.use = flg;
787d487561eSHong Zhang       break;
7884e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
789a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
790a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
791290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
792a65d3064SKris Buschelman       break;
793a65d3064SKris Buschelman     default:
79471c2f376SKris Buschelman       break;
795a65d3064SKris Buschelman   }
7964e0d8c25SBarry Smith   ierr = MatSetOption_Inode(A,op,flg);CHKERRQ(ierr);
7973a40ed3dSBarry Smith   PetscFunctionReturn(0);
79817ab2063SBarry Smith }
79917ab2063SBarry Smith 
8004a2ae208SSatish Balay #undef __FUNCT__
8014a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
802dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
80317ab2063SBarry Smith {
804416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8056849ba73SBarry Smith   PetscErrorCode ierr;
80697f1f81fSBarry Smith   PetscInt       i,j,n;
80787828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
80817ab2063SBarry Smith 
8093a40ed3dSBarry Smith   PetscFunctionBegin;
8102dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8111ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
81236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
813899cda47SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
814899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
815bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
816bfeeae90SHong Zhang       if (a->j[j] == i) {
817416022c9SBarry Smith         x[i] = a->a[j];
81817ab2063SBarry Smith         break;
81917ab2063SBarry Smith       }
82017ab2063SBarry Smith     }
82117ab2063SBarry Smith   }
8221ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8233a40ed3dSBarry Smith   PetscFunctionReturn(0);
82417ab2063SBarry Smith }
82517ab2063SBarry Smith 
8264a2ae208SSatish Balay #undef __FUNCT__
8274a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
828dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
82917ab2063SBarry Smith {
830416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8315c897100SBarry Smith   PetscScalar       *x,*y;
832dfbe8321SBarry Smith   PetscErrorCode    ierr;
833899cda47SBarry Smith   PetscInt          m = A->rmap.n;
8345c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
835a77337e4SBarry Smith   MatScalar         *v;
836a77337e4SBarry Smith   PetscScalar       alpha;
8377b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8383447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8394eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8405c897100SBarry Smith #endif
84117ab2063SBarry Smith 
8423a40ed3dSBarry Smith   PetscFunctionBegin;
8432e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8441ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8451ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8465c897100SBarry Smith 
8475c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
848bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8495c897100SBarry Smith #else
8503447b6efSHong Zhang   if (usecprow){
8513447b6efSHong Zhang     m    = cprow.nrows;
8523447b6efSHong Zhang     ii   = cprow.i;
8537b2bb3b9SHong Zhang     ridx = cprow.rindex;
8543447b6efSHong Zhang   } else {
8553447b6efSHong Zhang     ii = a->i;
8563447b6efSHong Zhang   }
85717ab2063SBarry Smith   for (i=0; i<m; i++) {
8583447b6efSHong Zhang     idx   = a->j + ii[i] ;
8593447b6efSHong Zhang     v     = a->a + ii[i] ;
8603447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8613447b6efSHong Zhang     if (usecprow){
8627b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8633447b6efSHong Zhang     } else {
86417ab2063SBarry Smith       alpha = x[i];
8653447b6efSHong Zhang     }
86617ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
86717ab2063SBarry Smith   }
8685c897100SBarry Smith #endif
869efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
8701ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8711ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8723a40ed3dSBarry Smith   PetscFunctionReturn(0);
87317ab2063SBarry Smith }
87417ab2063SBarry Smith 
8754a2ae208SSatish Balay #undef __FUNCT__
8765c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
877dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8785c897100SBarry Smith {
879dfbe8321SBarry Smith   PetscErrorCode ierr;
8805c897100SBarry Smith 
8815c897100SBarry Smith   PetscFunctionBegin;
882170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
8835c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8845c897100SBarry Smith   PetscFunctionReturn(0);
8855c897100SBarry Smith }
8865c897100SBarry Smith 
8875c897100SBarry Smith 
8885c897100SBarry Smith #undef __FUNCT__
8894a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
890dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
89117ab2063SBarry Smith {
892416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
893d9fead3dSBarry Smith   PetscScalar       *y;
89454f21887SBarry Smith   const PetscScalar *x;
89554f21887SBarry Smith   const MatScalar   *aa;
896dfbe8321SBarry Smith   PetscErrorCode    ierr;
897899cda47SBarry Smith   PetscInt          m=A->rmap.n,*aj,*ii;
898a46b3154SVictor Eijkhout   PetscInt          n,i,j,nonzerorow=0,*ridx=PETSC_NULL;
899362ced78SSatish Balay   PetscScalar       sum;
90097952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
901f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
902f2e71c43SSatish Balay   PetscInt         jrow;
903e36a17ebSSatish Balay #endif
90417ab2063SBarry Smith 
905b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
90697952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
907fee21e36SBarry Smith #endif
908fee21e36SBarry Smith 
9093a40ed3dSBarry Smith   PetscFunctionBegin;
910d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9111ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
91297952fefSHong Zhang   aj  = a->j;
91397952fefSHong Zhang   aa  = a->a;
914416022c9SBarry Smith   ii  = a->i;
9154eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
91697952fefSHong Zhang     m    = a->compressedrow.nrows;
91797952fefSHong Zhang     ii   = a->compressedrow.i;
91897952fefSHong Zhang     ridx = a->compressedrow.rindex;
91997952fefSHong Zhang     for (i=0; i<m; i++){
92097952fefSHong Zhang       n   = ii[i+1] - ii[i];
92197952fefSHong Zhang       aj  = a->j + ii[i];
92297952fefSHong Zhang       aa  = a->a + ii[i];
92397952fefSHong Zhang       sum = 0.0;
924a46b3154SVictor Eijkhout       nonzerorow += (n>0);
92597952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
92697952fefSHong Zhang       y[*ridx++] = sum;
92797952fefSHong Zhang     }
92897952fefSHong Zhang   } else { /* do not use compressed row format */
929b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
930b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
931b05257ddSBarry Smith #else
93217ab2063SBarry Smith     for (i=0; i<m; i++) {
9339ea0dfa2SSatish Balay       jrow = ii[i];
9349ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
93517ab2063SBarry Smith       sum  = 0.0;
936a46b3154SVictor Eijkhout       nonzerorow += (n>0);
9379ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
93897952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9399ea0dfa2SSatish Balay       }
94017ab2063SBarry Smith       y[i] = sum;
94117ab2063SBarry Smith     }
9428d195f9aSBarry Smith #endif
943b05257ddSBarry Smith   }
944a46b3154SVictor Eijkhout   ierr = PetscLogFlops(2*a->nz - nonzerorow);CHKERRQ(ierr);
945d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9461ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9473a40ed3dSBarry Smith   PetscFunctionReturn(0);
94817ab2063SBarry Smith }
94917ab2063SBarry Smith 
9504a2ae208SSatish Balay #undef __FUNCT__
9514a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
952dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
95317ab2063SBarry Smith {
954416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
95554f21887SBarry Smith   PetscScalar     *x,*y,*z;
95654f21887SBarry Smith   const MatScalar *aa;
957dfbe8321SBarry Smith   PetscErrorCode  ierr;
958899cda47SBarry Smith   PetscInt        m = A->rmap.n,*aj,*ii;
959aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
96097952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
961362ced78SSatish Balay   PetscScalar     sum;
96297952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
963e36a17ebSSatish Balay #endif
9649ea0dfa2SSatish Balay 
9653a40ed3dSBarry Smith   PetscFunctionBegin;
9661ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9671ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9682e8a6d31SBarry Smith   if (zz != yy) {
9691ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9702e8a6d31SBarry Smith   } else {
9712e8a6d31SBarry Smith     z = y;
9722e8a6d31SBarry Smith   }
973bfeeae90SHong Zhang 
97497952fefSHong Zhang   aj  = a->j;
97597952fefSHong Zhang   aa  = a->a;
976cddf8d76SBarry Smith   ii  = a->i;
977aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
97897952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
97902ab625aSSatish Balay #else
9804eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
9814eb6d288SHong Zhang     if (zz != yy){
9824eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
9834eb6d288SHong Zhang     }
98497952fefSHong Zhang     m    = a->compressedrow.nrows;
98597952fefSHong Zhang     ii   = a->compressedrow.i;
98697952fefSHong Zhang     ridx = a->compressedrow.rindex;
98797952fefSHong Zhang     for (i=0; i<m; i++){
98897952fefSHong Zhang       n  = ii[i+1] - ii[i];
98997952fefSHong Zhang       aj  = a->j + ii[i];
99097952fefSHong Zhang       aa  = a->a + ii[i];
99197952fefSHong Zhang       sum = y[*ridx];
99297952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
99397952fefSHong Zhang       z[*ridx++] = sum;
99497952fefSHong Zhang     }
99597952fefSHong Zhang   } else { /* do not use compressed row format */
99617ab2063SBarry Smith     for (i=0; i<m; i++) {
9979ea0dfa2SSatish Balay       jrow = ii[i];
9989ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
99917ab2063SBarry Smith       sum  = y[i];
10009ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
100197952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10029ea0dfa2SSatish Balay       }
100317ab2063SBarry Smith       z[i] = sum;
100417ab2063SBarry Smith     }
100597952fefSHong Zhang   }
100602ab625aSSatish Balay #endif
1007efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
10081ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10091ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10102e8a6d31SBarry Smith   if (zz != yy) {
10111ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10122e8a6d31SBarry Smith   }
10133a40ed3dSBarry Smith   PetscFunctionReturn(0);
101417ab2063SBarry Smith }
101517ab2063SBarry Smith 
101617ab2063SBarry Smith /*
101717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
101817ab2063SBarry Smith */
10194a2ae208SSatish Balay #undef __FUNCT__
10204a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1021dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
102217ab2063SBarry Smith {
1023416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10246849ba73SBarry Smith   PetscErrorCode ierr;
102509f38230SBarry Smith   PetscInt       i,j,m = A->rmap.n;
102617ab2063SBarry Smith 
10273a40ed3dSBarry Smith   PetscFunctionBegin;
102809f38230SBarry Smith   if (!a->diag) {
102909f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10309518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
103109f38230SBarry Smith   }
1032899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
103309f38230SBarry Smith     a->diag[i] = a->i[i+1];
1034bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1035bfeeae90SHong Zhang       if (a->j[j] == i) {
103609f38230SBarry Smith         a->diag[i] = j;
103717ab2063SBarry Smith         break;
103817ab2063SBarry Smith       }
103917ab2063SBarry Smith     }
104017ab2063SBarry Smith   }
10413a40ed3dSBarry Smith   PetscFunctionReturn(0);
104217ab2063SBarry Smith }
104317ab2063SBarry Smith 
1044be5855fcSBarry Smith /*
1045be5855fcSBarry Smith      Checks for missing diagonals
1046be5855fcSBarry Smith */
10474a2ae208SSatish Balay #undef __FUNCT__
10484a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
104909f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1050be5855fcSBarry Smith {
1051be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
105297f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1053be5855fcSBarry Smith 
1054be5855fcSBarry Smith   PetscFunctionBegin;
105509f38230SBarry Smith   *missing = PETSC_FALSE;
105609f38230SBarry Smith   if (A->rmap.n > 0 && !jj) {
105709f38230SBarry Smith     *missing  = PETSC_TRUE;
105809f38230SBarry Smith     if (d) *d = 0;
105909f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
106009f38230SBarry Smith   } else {
1061f1e2ffcdSBarry Smith     diag = a->diag;
1062899cda47SBarry Smith     for (i=0; i<A->rmap.n; i++) {
1063bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
106409f38230SBarry Smith 	*missing = PETSC_TRUE;
106509f38230SBarry Smith 	if (d) *d = i;
106609f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
106709f38230SBarry Smith       }
1068be5855fcSBarry Smith     }
1069be5855fcSBarry Smith   }
1070be5855fcSBarry Smith   PetscFunctionReturn(0);
1071be5855fcSBarry Smith }
1072be5855fcSBarry Smith 
107371f1c65dSBarry Smith EXTERN_C_BEGIN
107471f1c65dSBarry Smith #undef __FUNCT__
107571f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
107671f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
107771f1c65dSBarry Smith {
107871f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
107971f1c65dSBarry Smith   PetscErrorCode ierr;
108071f1c65dSBarry Smith   PetscInt       i,*diag,m = A->rmap.n;
108154f21887SBarry Smith   MatScalar      *v = a->a;
108254f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
108371f1c65dSBarry Smith 
108471f1c65dSBarry Smith   PetscFunctionBegin;
108571f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
108671f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
108771f1c65dSBarry Smith   diag = a->diag;
108871f1c65dSBarry Smith   if (!a->idiag) {
108971f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
109071f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
109171f1c65dSBarry Smith     v        = a->a;
109271f1c65dSBarry Smith   }
109371f1c65dSBarry Smith   mdiag = a->mdiag;
109471f1c65dSBarry Smith   idiag = a->idiag;
109571f1c65dSBarry Smith 
1096028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
109771f1c65dSBarry Smith     for (i=0; i<m; i++) {
109871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
109971f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
110071f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
110171f1c65dSBarry Smith     }
110271f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
110371f1c65dSBarry Smith   } else {
110471f1c65dSBarry Smith     for (i=0; i<m; i++) {
110571f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
110671f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
110771f1c65dSBarry Smith     }
110871f1c65dSBarry Smith     ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
110971f1c65dSBarry Smith   }
111071f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
111171f1c65dSBarry Smith   PetscFunctionReturn(0);
111271f1c65dSBarry Smith }
11135a9745a3SMatthew Knepley EXTERN_C_END
111471f1c65dSBarry Smith 
11154a2ae208SSatish Balay #undef __FUNCT__
11164a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
111797f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
111817ab2063SBarry Smith {
1119416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1120beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
112154f21887SBarry Smith   const MatScalar    *v = a->a;
112254f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1123dfbe8321SBarry Smith   PetscErrorCode     ierr;
1124899cda47SBarry Smith   PetscInt           n = A->cmap.n,m = A->rmap.n,i;
112597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
112617ab2063SBarry Smith 
11273a40ed3dSBarry Smith   PetscFunctionBegin;
1128b965ef7fSBarry Smith   its = its*lits;
112991723122SBarry Smith 
113071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
113171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
113271f1c65dSBarry Smith   a->fshift = fshift;
113371f1c65dSBarry Smith   a->omega  = omega;
1134ed480e8bSBarry Smith 
113571f1c65dSBarry Smith   diag = a->diag;
113671f1c65dSBarry Smith   t     = a->ssor_work;
1137ed480e8bSBarry Smith   idiag = a->idiag;
113871f1c65dSBarry Smith   mdiag = a->mdiag;
1139ed480e8bSBarry Smith 
11401ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1141fb2e594dSBarry Smith   if (xx != bb) {
11421ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1143fb2e594dSBarry Smith   } else {
1144fb2e594dSBarry Smith     b = x;
1145fb2e594dSBarry Smith   }
114671f1c65dSBarry Smith   CHKMEMQ;
1147ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1148ed480e8bSBarry Smith   xs   = x;
114917ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
115017ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1151ed480e8bSBarry Smith     bs = b;
115217ab2063SBarry Smith     for (i=0; i<m; i++) {
115371f1c65dSBarry Smith         d    = fshift + mdiag[i];
1154416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1155ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1156ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
115717ab2063SBarry Smith         sum  = b[i]*d/omega;
115817ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
115917ab2063SBarry Smith         x[i] = sum;
116017ab2063SBarry Smith     }
11611ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11621ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1163efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11643a40ed3dSBarry Smith     PetscFunctionReturn(0);
116517ab2063SBarry Smith   }
1166c783ea89SBarry Smith 
116748af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
116829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11693a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
117017ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
117117ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
117217ab2063SBarry Smith 
117317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
117417ab2063SBarry Smith 
117517ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
117617ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
117717ab2063SBarry Smith     is the relaxation factor.
117817ab2063SBarry Smith     */
117917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
118017ab2063SBarry Smith 
118117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
118217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1183416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1184ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1185ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
118617ab2063SBarry Smith       sum  = b[i];
118717ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1188ed480e8bSBarry Smith       x[i] = sum*idiag[i];
118917ab2063SBarry Smith     }
119017ab2063SBarry Smith 
119117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1192416022c9SBarry Smith     v = a->a;
1193ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
119417ab2063SBarry Smith 
119517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1196ed480e8bSBarry Smith     ts = t;
1197416022c9SBarry Smith     diag = a->diag;
119817ab2063SBarry Smith     for (i=0; i<m; i++) {
1199416022c9SBarry Smith       n    = diag[i] - a->i[i];
1200ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1201ed480e8bSBarry Smith       v    = a->a + a->i[i];
120217ab2063SBarry Smith       sum  = t[i];
120317ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1204ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1205733d66baSBarry Smith       /*  x = x + t */
1206733d66baSBarry Smith       x[i] += t[i];
120717ab2063SBarry Smith     }
120817ab2063SBarry Smith 
1209efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
12101ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12111ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12123a40ed3dSBarry Smith     PetscFunctionReturn(0);
121317ab2063SBarry Smith   }
121417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
121517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
121677d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
121797f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
121877d8c4bbSBarry Smith #else
121917ab2063SBarry Smith       for (i=0; i<m; i++) {
1220416022c9SBarry Smith         n    = diag[i] - a->i[i];
1221ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1222ed480e8bSBarry Smith         v    = a->a + a->i[i];
122317ab2063SBarry Smith         sum  = b[i];
122417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1225ed480e8bSBarry Smith         x[i] = sum*idiag[i];
122617ab2063SBarry Smith       }
122777d8c4bbSBarry Smith #endif
122817ab2063SBarry Smith       xb = x;
1229efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12303a40ed3dSBarry Smith     } else xb = b;
123117ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
123217ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
123317ab2063SBarry Smith       for (i=0; i<m; i++) {
1234ed480e8bSBarry Smith         x[i] *= mdiag[i];
123517ab2063SBarry Smith       }
1236efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
123717ab2063SBarry Smith     }
123817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
123977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
124097f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
124177d8c4bbSBarry Smith #else
124217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1243416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1244ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1245ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
124617ab2063SBarry Smith         sum  = xb[i];
124717ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1248ed480e8bSBarry Smith         x[i] = sum*idiag[i];
124917ab2063SBarry Smith       }
125077d8c4bbSBarry Smith #endif
1251efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
125217ab2063SBarry Smith     }
125317ab2063SBarry Smith     its--;
125417ab2063SBarry Smith   }
125517ab2063SBarry Smith   while (its--) {
125617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
125777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
125897f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
125977d8c4bbSBarry Smith #else
126017ab2063SBarry Smith       for (i=0; i<m; i++) {
1261416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1262ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1263ed480e8bSBarry Smith         v    = a->a + a->i[i];
126417ab2063SBarry Smith         sum  = b[i];
126517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1266ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
126717ab2063SBarry Smith       }
126877d8c4bbSBarry Smith #endif
1269efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
127017ab2063SBarry Smith     }
127117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
127277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
127397f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
127477d8c4bbSBarry Smith #else
127517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1276416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1277ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1278ed480e8bSBarry Smith         v    = a->a + a->i[i];
127917ab2063SBarry Smith         sum  = b[i];
128017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1281ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
128217ab2063SBarry Smith       }
128377d8c4bbSBarry Smith #endif
1284efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
128517ab2063SBarry Smith     }
128617ab2063SBarry Smith   }
12871ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12881ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
128971f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
129017ab2063SBarry Smith }
129117ab2063SBarry Smith 
12922af78befSBarry Smith 
12934a2ae208SSatish Balay #undef __FUNCT__
12944a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1295dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
129617ab2063SBarry Smith {
1297416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12984e220ebcSLois Curfman McInnes 
12993a40ed3dSBarry Smith   PetscFunctionBegin;
1300899cda47SBarry Smith   info->rows_global    = (double)A->rmap.n;
1301899cda47SBarry Smith   info->columns_global = (double)A->cmap.n;
1302899cda47SBarry Smith   info->rows_local     = (double)A->rmap.n;
1303899cda47SBarry Smith   info->columns_local  = (double)A->cmap.n;
13044e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13054e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13064e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13074e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13084e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13094e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13107adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13114e220ebcSLois Curfman McInnes   if (A->factor) {
13124e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13134e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13144e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13154e220ebcSLois Curfman McInnes   } else {
13164e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13174e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13184e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13194e220ebcSLois Curfman McInnes   }
13203a40ed3dSBarry Smith   PetscFunctionReturn(0);
132117ab2063SBarry Smith }
132217ab2063SBarry Smith 
13234a2ae208SSatish Balay #undef __FUNCT__
13244a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1325f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
132617ab2063SBarry Smith {
1327416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
132809f38230SBarry Smith   PetscInt       i,m = A->rmap.n - 1,d;
13296849ba73SBarry Smith   PetscErrorCode ierr;
133009f38230SBarry Smith   PetscTruth     missing;
133117ab2063SBarry Smith 
13323a40ed3dSBarry Smith   PetscFunctionBegin;
1333f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1334f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
133577431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1336bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1337f1e2ffcdSBarry Smith     }
1338f4df32b1SMatthew Knepley     if (diag != 0.0) {
133909f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
134009f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1341f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1342f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1343f1e2ffcdSBarry Smith       }
1344f1e2ffcdSBarry Smith     }
134588e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1346f1e2ffcdSBarry Smith   } else {
1347f4df32b1SMatthew Knepley     if (diag != 0.0) {
134817ab2063SBarry Smith       for (i=0; i<N; i++) {
134977431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13507ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1351416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1352f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1353bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13547ae801bdSBarry Smith         } else { /* in case row was completely empty */
1355f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
135617ab2063SBarry Smith         }
135717ab2063SBarry Smith       }
13583a40ed3dSBarry Smith     } else {
135917ab2063SBarry Smith       for (i=0; i<N; i++) {
136077431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1361416022c9SBarry Smith         a->ilen[rows[i]] = 0;
136217ab2063SBarry Smith       }
136317ab2063SBarry Smith     }
136488e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1365f1e2ffcdSBarry Smith   }
136643a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13673a40ed3dSBarry Smith   PetscFunctionReturn(0);
136817ab2063SBarry Smith }
136917ab2063SBarry Smith 
13704a2ae208SSatish Balay #undef __FUNCT__
13714a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1372a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
137317ab2063SBarry Smith {
1374416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
137597f1f81fSBarry Smith   PetscInt   *itmp;
137617ab2063SBarry Smith 
13773a40ed3dSBarry Smith   PetscFunctionBegin;
1378899cda47SBarry Smith   if (row < 0 || row >= A->rmap.n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
137917ab2063SBarry Smith 
1380416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1381bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
138217ab2063SBarry Smith   if (idx) {
1383bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1384bfeeae90SHong Zhang     if (*nz) {
13854e093b46SBarry Smith       *idx = itmp;
138617ab2063SBarry Smith     }
138717ab2063SBarry Smith     else *idx = 0;
138817ab2063SBarry Smith   }
13893a40ed3dSBarry Smith   PetscFunctionReturn(0);
139017ab2063SBarry Smith }
139117ab2063SBarry Smith 
1392bfeeae90SHong Zhang /* remove this function? */
13934a2ae208SSatish Balay #undef __FUNCT__
13944a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1395a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
139617ab2063SBarry Smith {
13973a40ed3dSBarry Smith   PetscFunctionBegin;
13983a40ed3dSBarry Smith   PetscFunctionReturn(0);
139917ab2063SBarry Smith }
140017ab2063SBarry Smith 
14014a2ae208SSatish Balay #undef __FUNCT__
14024a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1403dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
140417ab2063SBarry Smith {
1405416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
140654f21887SBarry Smith   MatScalar      *v = a->a;
140736db0b34SBarry Smith   PetscReal      sum = 0.0;
14086849ba73SBarry Smith   PetscErrorCode ierr;
140997f1f81fSBarry Smith   PetscInt       i,j;
141017ab2063SBarry Smith 
14113a40ed3dSBarry Smith   PetscFunctionBegin;
141217ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1413416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1414aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
141536db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
141617ab2063SBarry Smith #else
141717ab2063SBarry Smith       sum += (*v)*(*v); v++;
141817ab2063SBarry Smith #endif
141917ab2063SBarry Smith     }
1420064f8208SBarry Smith     *nrm = sqrt(sum);
14213a40ed3dSBarry Smith   } else if (type == NORM_1) {
142236db0b34SBarry Smith     PetscReal *tmp;
142397f1f81fSBarry Smith     PetscInt    *jj = a->j;
1424899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1425899cda47SBarry Smith     ierr = PetscMemzero(tmp,A->cmap.n*sizeof(PetscReal));CHKERRQ(ierr);
1426064f8208SBarry Smith     *nrm = 0.0;
1427416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1428bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
142917ab2063SBarry Smith     }
1430899cda47SBarry Smith     for (j=0; j<A->cmap.n; j++) {
1431064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
143217ab2063SBarry Smith     }
1433606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14343a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1435064f8208SBarry Smith     *nrm = 0.0;
1436899cda47SBarry Smith     for (j=0; j<A->rmap.n; j++) {
1437bfeeae90SHong Zhang       v = a->a + a->i[j];
143817ab2063SBarry Smith       sum = 0.0;
1439416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1440cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
144117ab2063SBarry Smith       }
1442064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
144317ab2063SBarry Smith     }
14443a40ed3dSBarry Smith   } else {
144529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
144617ab2063SBarry Smith   }
14473a40ed3dSBarry Smith   PetscFunctionReturn(0);
144817ab2063SBarry Smith }
144917ab2063SBarry Smith 
14504a2ae208SSatish Balay #undef __FUNCT__
14514a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1452fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
145317ab2063SBarry Smith {
1454416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1455416022c9SBarry Smith   Mat            C;
14566849ba73SBarry Smith   PetscErrorCode ierr;
1457899cda47SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap.n,len,*col;
145854f21887SBarry Smith   MatScalar      *array = a->a;
145917ab2063SBarry Smith 
14603a40ed3dSBarry Smith   PetscFunctionBegin;
1461e9695a30SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1462fc4dec0aSBarry Smith 
1463fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1464899cda47SBarry Smith     ierr = PetscMalloc((1+A->cmap.n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1465899cda47SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap.n)*sizeof(PetscInt));CHKERRQ(ierr);
1466bfeeae90SHong Zhang 
1467bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14687adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1469899cda47SBarry Smith     ierr = MatSetSizes(C,A->cmap.n,m,A->cmap.n,m);CHKERRQ(ierr);
14707adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1471ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1472606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
147317ab2063SBarry Smith     for (i=0; i<m; i++) {
147417ab2063SBarry Smith       len    = ai[i+1]-ai[i];
147587d4246cSBarry Smith       ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1476b9b97703SBarry Smith       array += len;
1477b9b97703SBarry Smith       aj    += len;
147817ab2063SBarry Smith     }
1479fc4dec0aSBarry Smith   } else {
1480fc4dec0aSBarry Smith     C = *B;
1481fc4dec0aSBarry Smith   }
148217ab2063SBarry Smith 
14836d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14846d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148517ab2063SBarry Smith 
1486815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1487416022c9SBarry Smith     *B = C;
148817ab2063SBarry Smith   } else {
1489273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
149017ab2063SBarry Smith   }
14913a40ed3dSBarry Smith   PetscFunctionReturn(0);
149217ab2063SBarry Smith }
149317ab2063SBarry Smith 
1494cd0d46ebSvictorle EXTERN_C_BEGIN
1495cd0d46ebSvictorle #undef __FUNCT__
14965fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1497be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1498cd0d46ebSvictorle {
1499cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
150054f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
150154f21887SBarry Smith   MatScalar      *va,*vb;
15026849ba73SBarry Smith   PetscErrorCode ierr;
150397f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1504cd0d46ebSvictorle 
1505cd0d46ebSvictorle   PetscFunctionBegin;
1506cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1507cd0d46ebSvictorle 
1508cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1509cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15105485867bSBarry Smith   if (ma!=nb || na!=mb){
15115485867bSBarry Smith     *f = PETSC_FALSE;
15125485867bSBarry Smith     PetscFunctionReturn(0);
15135485867bSBarry Smith   }
1514cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1515cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1516cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
151797f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
151897f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1519cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1520cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1521cd0d46ebSvictorle 
1522cd0d46ebSvictorle   *f = PETSC_TRUE;
1523cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1524cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
152597f1f81fSBarry Smith       PetscInt         idc,idr;
15265485867bSBarry Smith       PetscScalar vc,vr;
1527cd0d46ebSvictorle       /* column/row index/value */
15285485867bSBarry Smith       idc = adx[aptr[i]];
15295485867bSBarry Smith       idr = bdx[bptr[idc]];
15305485867bSBarry Smith       vc  = va[aptr[i]];
15315485867bSBarry Smith       vr  = vb[bptr[idc]];
15325485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15335485867bSBarry Smith 	*f = PETSC_FALSE;
15345485867bSBarry Smith         goto done;
1535cd0d46ebSvictorle       } else {
15365485867bSBarry Smith 	aptr[i]++;
15375485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1538cd0d46ebSvictorle       }
1539cd0d46ebSvictorle     }
1540cd0d46ebSvictorle   }
1541cd0d46ebSvictorle  done:
1542cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15433aeef889SHong Zhang   if (B) {
15443aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15453aeef889SHong Zhang   }
1546cd0d46ebSvictorle   PetscFunctionReturn(0);
1547cd0d46ebSvictorle }
1548cd0d46ebSvictorle EXTERN_C_END
1549cd0d46ebSvictorle 
15501cbb95d3SBarry Smith EXTERN_C_BEGIN
15511cbb95d3SBarry Smith #undef __FUNCT__
15521cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15531cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15541cbb95d3SBarry Smith {
15551cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
155654f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
155754f21887SBarry Smith   MatScalar      *va,*vb;
15581cbb95d3SBarry Smith   PetscErrorCode ierr;
15591cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15601cbb95d3SBarry Smith 
15611cbb95d3SBarry Smith   PetscFunctionBegin;
15621cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15631cbb95d3SBarry Smith 
15641cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15651cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15661cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15671cbb95d3SBarry Smith     *f = PETSC_FALSE;
15681cbb95d3SBarry Smith     PetscFunctionReturn(0);
15691cbb95d3SBarry Smith   }
15701cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15711cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15721cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
15731cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
15741cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
15751cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
15761cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
15771cbb95d3SBarry Smith 
15781cbb95d3SBarry Smith   *f = PETSC_TRUE;
15791cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
15801cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
15811cbb95d3SBarry Smith       PetscInt         idc,idr;
15821cbb95d3SBarry Smith       PetscScalar vc,vr;
15831cbb95d3SBarry Smith       /* column/row index/value */
15841cbb95d3SBarry Smith       idc = adx[aptr[i]];
15851cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
15861cbb95d3SBarry Smith       vc  = va[aptr[i]];
15871cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
15881cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
15891cbb95d3SBarry Smith 	*f = PETSC_FALSE;
15901cbb95d3SBarry Smith         goto done;
15911cbb95d3SBarry Smith       } else {
15921cbb95d3SBarry Smith 	aptr[i]++;
15931cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
15941cbb95d3SBarry Smith       }
15951cbb95d3SBarry Smith     }
15961cbb95d3SBarry Smith   }
15971cbb95d3SBarry Smith  done:
15981cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
15991cbb95d3SBarry Smith   if (B) {
16001cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16011cbb95d3SBarry Smith   }
16021cbb95d3SBarry Smith   PetscFunctionReturn(0);
16031cbb95d3SBarry Smith }
16041cbb95d3SBarry Smith EXTERN_C_END
16051cbb95d3SBarry Smith 
16069e29f15eSvictorle #undef __FUNCT__
16079e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1608dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16099e29f15eSvictorle {
1610dfbe8321SBarry Smith   PetscErrorCode ierr;
16119e29f15eSvictorle   PetscFunctionBegin;
16125485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16139e29f15eSvictorle   PetscFunctionReturn(0);
16149e29f15eSvictorle }
16159e29f15eSvictorle 
16164a2ae208SSatish Balay #undef __FUNCT__
16171cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16181cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16191cbb95d3SBarry Smith {
16201cbb95d3SBarry Smith   PetscErrorCode ierr;
16211cbb95d3SBarry Smith   PetscFunctionBegin;
16221cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16231cbb95d3SBarry Smith   PetscFunctionReturn(0);
16241cbb95d3SBarry Smith }
16251cbb95d3SBarry Smith 
16261cbb95d3SBarry Smith #undef __FUNCT__
16274a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1628dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
162917ab2063SBarry Smith {
1630416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
163154f21887SBarry Smith   PetscScalar    *l,*r,x;
163254f21887SBarry Smith   MatScalar      *v;
1633dfbe8321SBarry Smith   PetscErrorCode ierr;
1634899cda47SBarry Smith   PetscInt       i,j,m = A->rmap.n,n = A->cmap.n,M,nz = a->nz,*jj;
163517ab2063SBarry Smith 
16363a40ed3dSBarry Smith   PetscFunctionBegin;
163717ab2063SBarry Smith   if (ll) {
16383ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16393ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1640e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1641899cda47SBarry Smith     if (m != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16421ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1643416022c9SBarry Smith     v = a->a;
164417ab2063SBarry Smith     for (i=0; i<m; i++) {
164517ab2063SBarry Smith       x = l[i];
1646416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
164717ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
164817ab2063SBarry Smith     }
16491ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1650efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
165117ab2063SBarry Smith   }
165217ab2063SBarry Smith   if (rr) {
1653e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1654899cda47SBarry Smith     if (n != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16551ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1656416022c9SBarry Smith     v = a->a; jj = a->j;
165717ab2063SBarry Smith     for (i=0; i<nz; i++) {
1658bfeeae90SHong Zhang       (*v++) *= r[*jj++];
165917ab2063SBarry Smith     }
16601ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1661efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
166217ab2063SBarry Smith   }
16633a40ed3dSBarry Smith   PetscFunctionReturn(0);
166417ab2063SBarry Smith }
166517ab2063SBarry Smith 
16664a2ae208SSatish Balay #undef __FUNCT__
16674a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
166897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
166917ab2063SBarry Smith {
1670db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16716849ba73SBarry Smith   PetscErrorCode ierr;
1672899cda47SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap.n,*lens;
167397f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
167497f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
167597f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
167654f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1677416022c9SBarry Smith   Mat            C;
1678fee21e36SBarry Smith   PetscTruth     stride;
167917ab2063SBarry Smith 
16803a40ed3dSBarry Smith   PetscFunctionBegin;
1681d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
168229bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1683d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
168429bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
168599141d43SSatish Balay 
168617ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1687b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1688b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
168917ab2063SBarry Smith 
1690fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1691fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1692fee21e36SBarry Smith   if (stride && step == 1) {
169302834360SBarry Smith     /* special case of contiguous rows */
169497f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
169531ebf83bSSatish Balay     starts = lens + nrows;
169602834360SBarry Smith     /* loop over new rows determining lens and starting points */
169702834360SBarry Smith     for (i=0; i<nrows; i++) {
1698bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1699a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
170002834360SBarry Smith       for (k=kstart; k<kend; k++) {
1701bfeeae90SHong Zhang         if (aj[k] >= first) {
170202834360SBarry Smith           starts[i] = k;
170302834360SBarry Smith           break;
170402834360SBarry Smith 	}
170502834360SBarry Smith       }
1706a2744918SBarry Smith       sum = 0;
170702834360SBarry Smith       while (k < kend) {
1708bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1709a2744918SBarry Smith         sum++;
171002834360SBarry Smith       }
1711a2744918SBarry Smith       lens[i] = sum;
171202834360SBarry Smith     }
171302834360SBarry Smith     /* create submatrix */
1714cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
171597f1f81fSBarry Smith       PetscInt n_cols,n_rows;
171608480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
171729bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1718d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
171908480c60SBarry Smith       C = *B;
17203a40ed3dSBarry Smith     } else {
17217adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1722f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17237adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1724ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
172508480c60SBarry Smith     }
1726db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1727db02288aSLois Curfman McInnes 
172802834360SBarry Smith     /* loop over rows inserting into submatrix */
1729db02288aSLois Curfman McInnes     a_new    = c->a;
1730db02288aSLois Curfman McInnes     j_new    = c->j;
1731db02288aSLois Curfman McInnes     i_new    = c->i;
1732bfeeae90SHong Zhang 
173302834360SBarry Smith     for (i=0; i<nrows; i++) {
1734a2744918SBarry Smith       ii    = starts[i];
1735a2744918SBarry Smith       lensi = lens[i];
1736a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1737a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
173802834360SBarry Smith       }
173987828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1740a2744918SBarry Smith       a_new      += lensi;
1741a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1742a2744918SBarry Smith       c->ilen[i]  = lensi;
174302834360SBarry Smith     }
1744606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17453a40ed3dSBarry Smith   } else {
174602834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
174797f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1748bfeeae90SHong Zhang 
174997f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
175097f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
175117ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
175202834360SBarry Smith     /* determine lens of each row */
175302834360SBarry Smith     for (i=0; i<nrows; i++) {
1754bfeeae90SHong Zhang       kstart  = ai[irow[i]];
175502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
175602834360SBarry Smith       lens[i] = 0;
175702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1758bfeeae90SHong Zhang         if (smap[aj[k]]) {
175902834360SBarry Smith           lens[i]++;
176002834360SBarry Smith         }
176102834360SBarry Smith       }
176202834360SBarry Smith     }
176317ab2063SBarry Smith     /* Create and fill new matrix */
1764a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17650f5bd95cSBarry Smith       PetscTruth equal;
17660f5bd95cSBarry Smith 
176799141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1768899cda47SBarry Smith       if ((*B)->rmap.n  != nrows || (*B)->cmap.n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1769899cda47SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap.n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17700f5bd95cSBarry Smith       if (!equal) {
177129bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
177299141d43SSatish Balay       }
1773899cda47SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap.n*sizeof(PetscInt));CHKERRQ(ierr);
177408480c60SBarry Smith       C = *B;
17753a40ed3dSBarry Smith     } else {
17767adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1777f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17787adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1779ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
178008480c60SBarry Smith     }
178199141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
178217ab2063SBarry Smith     for (i=0; i<nrows; i++) {
178399141d43SSatish Balay       row    = irow[i];
1784bfeeae90SHong Zhang       kstart = ai[row];
178599141d43SSatish Balay       kend   = kstart + a->ilen[row];
1786bfeeae90SHong Zhang       mat_i  = c->i[i];
178799141d43SSatish Balay       mat_j  = c->j + mat_i;
178899141d43SSatish Balay       mat_a  = c->a + mat_i;
178999141d43SSatish Balay       mat_ilen = c->ilen + i;
179017ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1791bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1792ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
179399141d43SSatish Balay           *mat_a++ = a->a[k];
179499141d43SSatish Balay           (*mat_ilen)++;
179599141d43SSatish Balay 
179617ab2063SBarry Smith         }
179717ab2063SBarry Smith       }
179817ab2063SBarry Smith     }
179902834360SBarry Smith     /* Free work space */
180002834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1801606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1802606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
180302834360SBarry Smith   }
18046d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18056d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
180617ab2063SBarry Smith 
180717ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1808416022c9SBarry Smith   *B = C;
18093a40ed3dSBarry Smith   PetscFunctionReturn(0);
181017ab2063SBarry Smith }
181117ab2063SBarry Smith 
1812a871dcd8SBarry Smith /*
1813a871dcd8SBarry Smith */
18144a2ae208SSatish Balay #undef __FUNCT__
18154a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1816dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1817a871dcd8SBarry Smith {
181863b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1819dfbe8321SBarry Smith   PetscErrorCode ierr;
182063b91edcSBarry Smith   Mat            outA;
1821b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
182263b91edcSBarry Smith 
18233a40ed3dSBarry Smith   PetscFunctionBegin;
1824d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1825b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1826b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1827a871dcd8SBarry Smith 
182863b91edcSBarry Smith   outA          = inA;
182963b91edcSBarry Smith   inA->factor   = FACTOR_LU;
1830c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1831c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row); CHKERRQ(ierr);}
1832c3122656SLisandro Dalcin   a->row = row;
1833c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1834c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col); CHKERRQ(ierr);}
1835c3122656SLisandro Dalcin   a->col = col;
183663b91edcSBarry Smith 
183736db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1838b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18394c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
184052e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1841f0ec6fceSSatish Balay 
184294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1843899cda47SBarry Smith      ierr = PetscMalloc((inA->rmap.n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
18449518dbb4SMatthew Knepley      ierr = PetscLogObjectMemory(inA, (inA->rmap.n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
184594a9d846SBarry Smith   }
184663b91edcSBarry Smith 
1847f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1848137fb511SHong Zhang   if (row_identity && col_identity) {
1849af281ebdSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
1850137fb511SHong Zhang   } else {
1851137fb511SHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(inA,info,&outA);CHKERRQ(ierr);
1852137fb511SHong Zhang   }
18533a40ed3dSBarry Smith   PetscFunctionReturn(0);
1854a871dcd8SBarry Smith }
1855a871dcd8SBarry Smith 
1856d9eff348SSatish Balay #include "petscblaslapack.h"
18574a2ae208SSatish Balay #undef __FUNCT__
18584a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1859f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1860f0b747eeSBarry Smith {
1861f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1862f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1863efee365bSSatish Balay   PetscErrorCode ierr;
18640805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18653a40ed3dSBarry Smith 
18663a40ed3dSBarry Smith   PetscFunctionBegin;
1867f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1868efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18693a40ed3dSBarry Smith   PetscFunctionReturn(0);
1870f0b747eeSBarry Smith }
1871f0b747eeSBarry Smith 
18724a2ae208SSatish Balay #undef __FUNCT__
18734a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
187497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1875cddf8d76SBarry Smith {
1876dfbe8321SBarry Smith   PetscErrorCode ierr;
187797f1f81fSBarry Smith   PetscInt       i;
1878cddf8d76SBarry Smith 
18793a40ed3dSBarry Smith   PetscFunctionBegin;
1880cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1881b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1882cddf8d76SBarry Smith   }
1883cddf8d76SBarry Smith 
1884cddf8d76SBarry Smith   for (i=0; i<n; i++) {
18856a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1886cddf8d76SBarry Smith   }
18873a40ed3dSBarry Smith   PetscFunctionReturn(0);
1888cddf8d76SBarry Smith }
1889cddf8d76SBarry Smith 
18904a2ae208SSatish Balay #undef __FUNCT__
18914a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
189297f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
18934dcbc457SBarry Smith {
1894e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18956849ba73SBarry Smith   PetscErrorCode ierr;
189697f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
189797f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1898f1af5d2fSBarry Smith   PetscBT        table;
1899bbd702dbSSatish Balay 
19003a40ed3dSBarry Smith   PetscFunctionBegin;
1901899cda47SBarry Smith   m     = A->rmap.n;
1902e4d965acSSatish Balay   ai    = a->i;
1903bfeeae90SHong Zhang   aj    = a->j;
19048a047759SSatish Balay 
1905a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
190606763907SSatish Balay 
190797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19086831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
190906763907SSatish Balay 
1910e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1911b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1912e4d965acSSatish Balay     isz  = 0;
19136831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1914e4d965acSSatish Balay 
1915e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19164dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1917b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1918e4d965acSSatish Balay 
1919dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1920e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1921f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19224dcbc457SBarry Smith     }
192306763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
192406763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1925e4d965acSSatish Balay 
192604a348a9SBarry Smith     k = 0;
192704a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
192804a348a9SBarry Smith       n = isz;
192906763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1930e4d965acSSatish Balay         row   = nidx[k];
1931e4d965acSSatish Balay         start = ai[row];
1932e4d965acSSatish Balay         end   = ai[row+1];
193304a348a9SBarry Smith         for (l = start; l<end ; l++){
1934efb16452SHong Zhang           val = aj[l] ;
1935f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1936e4d965acSSatish Balay         }
1937e4d965acSSatish Balay       }
1938e4d965acSSatish Balay     }
1939029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1940e4d965acSSatish Balay   }
19416831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1942606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19433a40ed3dSBarry Smith   PetscFunctionReturn(0);
19444dcbc457SBarry Smith }
194517ab2063SBarry Smith 
19460513a670SBarry Smith /* -------------------------------------------------------------- */
19474a2ae208SSatish Balay #undef __FUNCT__
19484a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1949dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19500513a670SBarry Smith {
19510513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19526849ba73SBarry Smith   PetscErrorCode ierr;
1953899cda47SBarry Smith   PetscInt       i,nz,m = A->rmap.n,n = A->cmap.n,*col;
195497f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
195556cd22aeSBarry Smith   IS             icolp,irowp;
195697f1f81fSBarry Smith   PetscInt       *cwork;
195732ec9ce4SBarry Smith   PetscScalar    *vwork;
19580513a670SBarry Smith 
19593a40ed3dSBarry Smith   PetscFunctionBegin;
19604c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
196156cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19624c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
196356cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19640513a670SBarry Smith 
19650513a670SBarry Smith   /* determine lengths of permuted rows */
196697f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19670513a670SBarry Smith   for (i=0; i<m; i++) {
19680513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19690513a670SBarry Smith   }
19707adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
1971f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
19727adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1973ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1974606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
19750513a670SBarry Smith 
197697f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
19770513a670SBarry Smith   for (i=0; i<m; i++) {
197832ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19790513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1980cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
198132ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19820513a670SBarry Smith   }
1983606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
19843c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
19850513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19860513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
198756cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
198856cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
198956cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
199056cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
19913a40ed3dSBarry Smith   PetscFunctionReturn(0);
19920513a670SBarry Smith }
19930513a670SBarry Smith 
19944a2ae208SSatish Balay #undef __FUNCT__
19954a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1996dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1997cb5b572fSBarry Smith {
1998dfbe8321SBarry Smith   PetscErrorCode ierr;
1999cb5b572fSBarry Smith 
2000cb5b572fSBarry Smith   PetscFunctionBegin;
200133f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
200233f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2003be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2004be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2005be6bf707SBarry Smith 
2006899cda47SBarry Smith     if (a->i[A->rmap.n] != b->i[B->rmap.n]) {
2007634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2008cb5b572fSBarry Smith     }
2009899cda47SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr);
2010cb5b572fSBarry Smith   } else {
2011cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2012cb5b572fSBarry Smith   }
2013cb5b572fSBarry Smith   PetscFunctionReturn(0);
2014cb5b572fSBarry Smith }
2015cb5b572fSBarry Smith 
20164a2ae208SSatish Balay #undef __FUNCT__
20174a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2018dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2019273d9f13SBarry Smith {
2020dfbe8321SBarry Smith   PetscErrorCode ierr;
2021273d9f13SBarry Smith 
2022273d9f13SBarry Smith   PetscFunctionBegin;
2023ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2024273d9f13SBarry Smith   PetscFunctionReturn(0);
2025273d9f13SBarry Smith }
2026273d9f13SBarry Smith 
20274a2ae208SSatish Balay #undef __FUNCT__
20284a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2029a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20306c0721eeSBarry Smith {
20316c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20326c0721eeSBarry Smith   PetscFunctionBegin;
20336c0721eeSBarry Smith   *array = a->a;
20346c0721eeSBarry Smith   PetscFunctionReturn(0);
20356c0721eeSBarry Smith }
20366c0721eeSBarry Smith 
20374a2ae208SSatish Balay #undef __FUNCT__
20384a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2039dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20406c0721eeSBarry Smith {
20416c0721eeSBarry Smith   PetscFunctionBegin;
20426c0721eeSBarry Smith   PetscFunctionReturn(0);
20436c0721eeSBarry Smith }
2044273d9f13SBarry Smith 
2045ee4f033dSBarry Smith #undef __FUNCT__
2046ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2047dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2048ee4f033dSBarry Smith {
20496849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20506849ba73SBarry Smith   PetscErrorCode ierr;
205197f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2052efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
205387828ca2SBarry Smith   PetscScalar    *vscale_array;
2054ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2055ee4f033dSBarry Smith   Vec            w1,w2,w3;
2056ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2057ee4f033dSBarry Smith   PetscTruth     flg;
2058ee4f033dSBarry Smith 
2059ee4f033dSBarry Smith   PetscFunctionBegin;
2060ee4f033dSBarry Smith   if (!coloring->w1) {
2061ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
206252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2063ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
206452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2065ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
206652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2067ee4f033dSBarry Smith   }
2068ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2069ee4f033dSBarry Smith 
2070ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
20717adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
2072ee4f033dSBarry Smith   if (flg) {
2073ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2074ee4f033dSBarry Smith   } else {
20750b9b6f31SBarry Smith     PetscTruth assembled;
20760b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
20770b9b6f31SBarry Smith     if (assembled) {
2078ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2079ee4f033dSBarry Smith     }
20800b9b6f31SBarry Smith   }
2081ee4f033dSBarry Smith 
2082ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2083ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2084ee4f033dSBarry Smith 
2085ee4f033dSBarry Smith   /*
2086ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2087ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2088ee4f033dSBarry Smith   */
2089ee4f033dSBarry Smith   if (coloring->F) {
2090ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2091ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2092ee4f033dSBarry Smith     if (m1 != m2) {
2093ee4f033dSBarry Smith       coloring->F = 0;
2094ee4f033dSBarry Smith     }
2095ee4f033dSBarry Smith   }
2096ee4f033dSBarry Smith 
2097ee4f033dSBarry Smith   if (coloring->F) {
2098ee4f033dSBarry Smith     w1          = coloring->F;
2099ee4f033dSBarry Smith     coloring->F = 0;
2100ee4f033dSBarry Smith   } else {
210166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2102ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
210366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2104ee4f033dSBarry Smith   }
2105ee4f033dSBarry Smith 
2106ee4f033dSBarry Smith   /*
2107ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2108ee4f033dSBarry Smith   */
21091ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21101ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2111ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2112ee4f033dSBarry Smith     /*
2113ee4f033dSBarry Smith        Loop over each column associated with color adding the
2114ee4f033dSBarry Smith        perturbation to the vector w3.
2115ee4f033dSBarry Smith     */
2116ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2117ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2118ee4f033dSBarry Smith       dx  = xx[col];
2119ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2120ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2121ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2122ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2123ee4f033dSBarry Smith #else
2124ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2125ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2126ee4f033dSBarry Smith #endif
2127ee4f033dSBarry Smith       dx                *= epsilon;
2128ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2129ee4f033dSBarry Smith     }
2130ee4f033dSBarry Smith   }
21311ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2132ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2133ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2134ee4f033dSBarry Smith 
2135ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2136ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2137ee4f033dSBarry Smith 
2138ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2139ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2140ee4f033dSBarry Smith 
21411ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2142ee4f033dSBarry Smith   /*
2143ee4f033dSBarry Smith       Loop over each color
2144ee4f033dSBarry Smith   */
2145ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
214649b058dcSBarry Smith     coloring->currentcolor = k;
2147ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21481ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2149ee4f033dSBarry Smith     /*
2150ee4f033dSBarry Smith        Loop over each column associated with color adding the
2151ee4f033dSBarry Smith        perturbation to the vector w3.
2152ee4f033dSBarry Smith     */
2153ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2154ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2155ee4f033dSBarry Smith       dx  = xx[col];
21565b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2157ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2158ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2159ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2160ee4f033dSBarry Smith #else
2161ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2162ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2163ee4f033dSBarry Smith #endif
2164ee4f033dSBarry Smith       dx            *= epsilon;
2165634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2166ee4f033dSBarry Smith       w3_array[col] += dx;
2167ee4f033dSBarry Smith     }
21681ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2169ee4f033dSBarry Smith 
2170ee4f033dSBarry Smith     /*
2171ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2172ee4f033dSBarry Smith     */
2173ee4f033dSBarry Smith 
217466f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2175ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
217666f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2177efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2178ee4f033dSBarry Smith 
2179ee4f033dSBarry Smith     /*
2180ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2181ee4f033dSBarry Smith     */
21821ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2183ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2184ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2185ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2186ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2187ee4f033dSBarry Smith       srow   = row + start;
2188ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2189ee4f033dSBarry Smith     }
21901ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2191ee4f033dSBarry Smith   }
219249b058dcSBarry Smith   coloring->currentcolor = k;
21931ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
21941ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2195ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2196ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2197ee4f033dSBarry Smith   PetscFunctionReturn(0);
2198ee4f033dSBarry Smith }
2199ee4f033dSBarry Smith 
2200ac90fabeSBarry Smith #include "petscblaslapack.h"
2201ac90fabeSBarry Smith #undef __FUNCT__
2202ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2203f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2204ac90fabeSBarry Smith {
2205dfbe8321SBarry Smith   PetscErrorCode ierr;
220697f1f81fSBarry Smith   PetscInt       i;
2207ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22080805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2209ac90fabeSBarry Smith 
2210ac90fabeSBarry Smith   PetscFunctionBegin;
2211ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2212f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2213f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2214c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2215a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2216a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2217a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2218a30b2313SHong Zhang     }
2219a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2220899cda47SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap.n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2221a30b2313SHong Zhang       y->XtoY = X;
2222407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2223c537a176SHong Zhang     }
2224f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22251e2582c4SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);CHKERRQ(ierr);
2226ac90fabeSBarry Smith   } else {
2227f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2228ac90fabeSBarry Smith   }
2229ac90fabeSBarry Smith   PetscFunctionReturn(0);
2230ac90fabeSBarry Smith }
2231ac90fabeSBarry Smith 
2232521d7252SBarry Smith #undef __FUNCT__
2233521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2234521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2235521d7252SBarry Smith {
2236521d7252SBarry Smith   PetscFunctionBegin;
2237521d7252SBarry Smith   PetscFunctionReturn(0);
2238521d7252SBarry Smith }
2239521d7252SBarry Smith 
2240354c94deSBarry Smith #undef __FUNCT__
2241354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2242354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2243354c94deSBarry Smith {
2244354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2245354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2246354c94deSBarry Smith   PetscInt    i,nz;
2247354c94deSBarry Smith   PetscScalar *a;
2248354c94deSBarry Smith 
2249354c94deSBarry Smith   PetscFunctionBegin;
2250354c94deSBarry Smith   nz = aij->nz;
2251354c94deSBarry Smith   a  = aij->a;
2252354c94deSBarry Smith   for (i=0; i<nz; i++) {
2253354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2254354c94deSBarry Smith   }
2255354c94deSBarry Smith #else
2256354c94deSBarry Smith   PetscFunctionBegin;
2257354c94deSBarry Smith #endif
2258354c94deSBarry Smith   PetscFunctionReturn(0);
2259354c94deSBarry Smith }
2260354c94deSBarry Smith 
2261e34fafa9SBarry Smith #undef __FUNCT__
2262985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2263985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2264e34fafa9SBarry Smith {
2265e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2266e34fafa9SBarry Smith   PetscErrorCode ierr;
2267899cda47SBarry Smith   PetscInt       i,j,m = A->rmap.n,*ai,*aj,ncols,n;
2268e34fafa9SBarry Smith   PetscReal      atmp;
2269985db425SBarry Smith   PetscScalar    *x;
2270e34fafa9SBarry Smith   MatScalar      *aa;
2271e34fafa9SBarry Smith 
2272e34fafa9SBarry Smith   PetscFunctionBegin;
2273e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2274e34fafa9SBarry Smith   aa   = a->a;
2275e34fafa9SBarry Smith   ai   = a->i;
2276e34fafa9SBarry Smith   aj   = a->j;
2277e34fafa9SBarry Smith 
2278985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2279e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2280e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2281899cda47SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2282e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2283e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
22849189402eSHong Zhang     x[i] = 0.0;
2285e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2286985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2287985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2288985db425SBarry Smith       aa++; aj++;
2289985db425SBarry Smith     }
2290985db425SBarry Smith   }
2291985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2292985db425SBarry Smith   PetscFunctionReturn(0);
2293985db425SBarry Smith }
2294985db425SBarry Smith 
2295985db425SBarry Smith #undef __FUNCT__
2296985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2297985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2298985db425SBarry Smith {
2299985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2300985db425SBarry Smith   PetscErrorCode ierr;
2301985db425SBarry Smith   PetscInt       i,j,m = A->rmap.n,*ai,*aj,ncols,n;
2302985db425SBarry Smith   PetscScalar    *x;
2303985db425SBarry Smith   MatScalar      *aa;
2304985db425SBarry Smith 
2305985db425SBarry Smith   PetscFunctionBegin;
2306985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2307985db425SBarry Smith   aa   = a->a;
2308985db425SBarry Smith   ai   = a->i;
2309985db425SBarry Smith   aj   = a->j;
2310985db425SBarry Smith 
2311985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2312985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2313985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2314985db425SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2315985db425SBarry Smith   for (i=0; i<m; i++) {
2316985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2317985db425SBarry Smith     if (ncols == A->cmap.n) { /* row is dense */
2318985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2319985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2320985db425SBarry Smith       x[i] = 0.0;
2321985db425SBarry Smith       if (idx) {
2322985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2323985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2324985db425SBarry Smith           if (aj[j] > j) {
2325985db425SBarry Smith             idx[i] = j;
2326985db425SBarry Smith             break;
2327985db425SBarry Smith           }
2328985db425SBarry Smith         }
2329985db425SBarry Smith       }
2330985db425SBarry Smith     }
2331985db425SBarry Smith     for (j=0; j<ncols; j++){
2332985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2333985db425SBarry Smith       aa++; aj++;
2334985db425SBarry Smith     }
2335985db425SBarry Smith   }
2336985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2337985db425SBarry Smith   PetscFunctionReturn(0);
2338985db425SBarry Smith }
2339985db425SBarry Smith 
2340985db425SBarry Smith #undef __FUNCT__
2341985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2342985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2343985db425SBarry Smith {
2344985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2345985db425SBarry Smith   PetscErrorCode ierr;
2346985db425SBarry Smith   PetscInt       i,j,m = A->rmap.n,*ai,*aj,ncols,n;
2347985db425SBarry Smith   PetscScalar    *x;
2348985db425SBarry Smith   MatScalar      *aa;
2349985db425SBarry Smith 
2350985db425SBarry Smith   PetscFunctionBegin;
2351985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2352985db425SBarry Smith   aa   = a->a;
2353985db425SBarry Smith   ai   = a->i;
2354985db425SBarry Smith   aj   = a->j;
2355985db425SBarry Smith 
2356985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2357985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2358985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2359985db425SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2360985db425SBarry Smith   for (i=0; i<m; i++) {
2361985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2362985db425SBarry Smith     if (ncols == A->cmap.n) { /* row is dense */
2363985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2364985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2365985db425SBarry Smith       x[i] = 0.0;
2366985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2367985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2368985db425SBarry Smith         for (j=0;j<ncols;j++) {
2369985db425SBarry Smith           if (aj[j] > j) {
2370985db425SBarry Smith             idx[i] = j;
2371985db425SBarry Smith             break;
2372985db425SBarry Smith           }
2373985db425SBarry Smith         }
2374985db425SBarry Smith       }
2375985db425SBarry Smith     }
2376985db425SBarry Smith     for (j=0; j<ncols; j++){
2377985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2378985db425SBarry Smith       aa++; aj++;
2379e34fafa9SBarry Smith     }
2380e34fafa9SBarry Smith   }
2381e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2382e34fafa9SBarry Smith   PetscFunctionReturn(0);
2383e34fafa9SBarry Smith }
2384e34fafa9SBarry Smith 
2385682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
23860a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2387cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2388cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2389cb5b572fSBarry Smith        MatMult_SeqAIJ,
239097304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
23917c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
23927c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2393cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2394cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
23957c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
239697304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2397cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2398cb5b572fSBarry Smith        0,
239917ab2063SBarry Smith        MatRelax_SeqAIJ,
240017ab2063SBarry Smith        MatTranspose_SeqAIJ,
240197304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2402cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2403cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2404cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2405cb5b572fSBarry Smith        MatNorm_SeqAIJ,
240697304618SKris Buschelman /*20*/ 0,
2407cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
240817ab2063SBarry Smith        MatCompress_SeqAIJ,
2409cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2410cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
241197304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2412cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2413cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2414f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2415a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
241697304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2417cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2418861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
24196c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24206c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
242197304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2422cb5b572fSBarry Smith        0,
2423cb5b572fSBarry Smith        0,
2424cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2425cb5b572fSBarry Smith        0,
242697304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2427cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2428cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2429cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2430cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2431985db425SBarry Smith /*45*/ MatGetRowMax_SeqAIJ,
2432cb5b572fSBarry Smith        MatScale_SeqAIJ,
2433cb5b572fSBarry Smith        0,
243479299369SBarry Smith        MatDiagonalSet_SeqAIJ,
24356945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2436521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
24373b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
24383b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
24393b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2440a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
244197304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2442b9617806SBarry Smith        0,
24430513a670SBarry Smith        0,
2444cda55fadSBarry Smith        MatPermute_SeqAIJ,
2445cda55fadSBarry Smith        0,
244697304618SKris Buschelman /*60*/ 0,
2447b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2448b9b97703SBarry Smith        MatView_SeqAIJ,
2449357abbc8SBarry Smith        0,
2450ee4f033dSBarry Smith        0,
245197304618SKris Buschelman /*65*/ 0,
2452ee4f033dSBarry Smith        0,
2453ee4f033dSBarry Smith        0,
2454ee4f033dSBarry Smith        0,
2455ee4f033dSBarry Smith        0,
2456985db425SBarry Smith /*70*/ MatGetRowMaxAbs_SeqAIJ,
2457ee4f033dSBarry Smith        0,
2458ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2459dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2460ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2461dcf5cc72SBarry Smith #else
2462dcf5cc72SBarry Smith        0,
2463dcf5cc72SBarry Smith #endif
2464ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2465b8f8c88eSHong Zhang /*75*/ 0,
246697304618SKris Buschelman        0,
246797304618SKris Buschelman        0,
246897304618SKris Buschelman        0,
246997304618SKris Buschelman        0,
247097304618SKris Buschelman /*80*/ 0,
247197304618SKris Buschelman        0,
247297304618SKris Buschelman        0,
247397304618SKris Buschelman        0,
2474bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2475bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
24761cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
24776284ec50SHong Zhang        0,
24786284ec50SHong Zhang        0,
2479bc011b1eSHong Zhang        0,
2480bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
248126be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
248226be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2483d439da42SKris Buschelman        MatPtAP_Basic,
24847ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
24857ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2486bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2487bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2488bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
24897ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
24907ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2491609c6c4dSKris Buschelman        0,
2492609c6c4dSKris Buschelman        0,
249387d4246cSBarry Smith        MatConjugate_SeqAIJ,
249487d4246cSBarry Smith        0,
249599cafbc1SBarry Smith /*105*/MatSetValuesRow_SeqAIJ,
249699cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2497f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2498f5edf698SHong Zhang        0,
24992bebee5dSHong Zhang        0,
2500985db425SBarry Smith /*110*/MatMatSolve_SeqAIJ,
2501985db425SBarry Smith        0,
25022af78befSBarry Smith        MatGetRowMin_SeqAIJ,
25032af78befSBarry Smith        0,
25042af78befSBarry Smith        MatMissingDiagonal_SeqAIJ
25059e29f15eSvictorle };
250617ab2063SBarry Smith 
2507fb2e594dSBarry Smith EXTERN_C_BEGIN
25084a2ae208SSatish Balay #undef __FUNCT__
25094a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2510be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2511bef8e0ddSBarry Smith {
2512bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
251397f1f81fSBarry Smith   PetscInt   i,nz,n;
2514bef8e0ddSBarry Smith 
2515bef8e0ddSBarry Smith   PetscFunctionBegin;
2516bef8e0ddSBarry Smith 
2517bef8e0ddSBarry Smith   nz = aij->maxnz;
251811c9b30cSMatthew Knepley   n  = mat->rmap.n;
2519bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2520bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2521bef8e0ddSBarry Smith   }
2522bef8e0ddSBarry Smith   aij->nz = nz;
2523bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2524bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2525bef8e0ddSBarry Smith   }
2526bef8e0ddSBarry Smith 
2527bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2528bef8e0ddSBarry Smith }
2529fb2e594dSBarry Smith EXTERN_C_END
2530bef8e0ddSBarry Smith 
25314a2ae208SSatish Balay #undef __FUNCT__
25324a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2533bef8e0ddSBarry Smith /*@
2534bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2535bef8e0ddSBarry Smith        in the matrix.
2536bef8e0ddSBarry Smith 
2537bef8e0ddSBarry Smith   Input Parameters:
2538bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2539bef8e0ddSBarry Smith -  indices - the column indices
2540bef8e0ddSBarry Smith 
254115091d37SBarry Smith   Level: advanced
254215091d37SBarry Smith 
2543bef8e0ddSBarry Smith   Notes:
2544bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2545bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2546bef8e0ddSBarry Smith   of the MatSetValues() operation.
2547bef8e0ddSBarry Smith 
2548bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2549d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2550bef8e0ddSBarry Smith 
2551bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2552bef8e0ddSBarry Smith 
2553b9617806SBarry Smith     The indices should start with zero, not one.
2554b9617806SBarry Smith 
2555bef8e0ddSBarry Smith @*/
2556be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2557bef8e0ddSBarry Smith {
255897f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2559bef8e0ddSBarry Smith 
2560bef8e0ddSBarry Smith   PetscFunctionBegin;
25614482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
25624482741eSBarry Smith   PetscValidPointer(indices,2);
2563c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2564bef8e0ddSBarry Smith   if (f) {
2565bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2566bef8e0ddSBarry Smith   } else {
2567634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2568bef8e0ddSBarry Smith   }
2569bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2570bef8e0ddSBarry Smith }
2571bef8e0ddSBarry Smith 
2572be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2573be6bf707SBarry Smith 
2574fb2e594dSBarry Smith EXTERN_C_BEGIN
25754a2ae208SSatish Balay #undef __FUNCT__
25764a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2577be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2578be6bf707SBarry Smith {
2579be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
25806849ba73SBarry Smith   PetscErrorCode ierr;
2581899cda47SBarry Smith   size_t         nz = aij->i[mat->rmap.n];
2582be6bf707SBarry Smith 
2583be6bf707SBarry Smith   PetscFunctionBegin;
2584be6bf707SBarry Smith   if (aij->nonew != 1) {
2585512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2586be6bf707SBarry Smith   }
2587be6bf707SBarry Smith 
2588be6bf707SBarry Smith   /* allocate space for values if not already there */
2589be6bf707SBarry Smith   if (!aij->saved_values) {
259087828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
25919518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2592be6bf707SBarry Smith   }
2593be6bf707SBarry Smith 
2594be6bf707SBarry Smith   /* copy values over */
259587828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2596be6bf707SBarry Smith   PetscFunctionReturn(0);
2597be6bf707SBarry Smith }
2598fb2e594dSBarry Smith EXTERN_C_END
2599be6bf707SBarry Smith 
26004a2ae208SSatish Balay #undef __FUNCT__
2601b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2602be6bf707SBarry Smith /*@
2603be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2604be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2605be6bf707SBarry Smith        nonlinear portion.
2606be6bf707SBarry Smith 
2607be6bf707SBarry Smith    Collect on Mat
2608be6bf707SBarry Smith 
2609be6bf707SBarry Smith   Input Parameters:
26100e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2611be6bf707SBarry Smith 
261215091d37SBarry Smith   Level: advanced
261315091d37SBarry Smith 
2614be6bf707SBarry Smith   Common Usage, with SNESSolve():
2615be6bf707SBarry Smith $    Create Jacobian matrix
2616be6bf707SBarry Smith $    Set linear terms into matrix
2617be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2618be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2619be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2620512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2621be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2622be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2623be6bf707SBarry Smith $    In your Jacobian routine
2624be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2625be6bf707SBarry Smith $      Set nonlinear terms in matrix
2626be6bf707SBarry Smith 
2627be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2628be6bf707SBarry Smith $    // build linear portion of Jacobian
2629512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2630be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2631be6bf707SBarry Smith $    loop over nonlinear iterations
2632be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2633be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2634be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2635be6bf707SBarry Smith $       Solve linear system with Jacobian
2636be6bf707SBarry Smith $    endloop
2637be6bf707SBarry Smith 
2638be6bf707SBarry Smith   Notes:
2639be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2640512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2641be6bf707SBarry Smith     calling this routine.
2642be6bf707SBarry Smith 
26430c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
26440c468ba9SBarry Smith     and does not allocated additional space.
26450c468ba9SBarry Smith 
2646be6bf707SBarry Smith .seealso: MatRetrieveValues()
2647be6bf707SBarry Smith 
2648be6bf707SBarry Smith @*/
2649be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2650be6bf707SBarry Smith {
2651dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2652be6bf707SBarry Smith 
2653be6bf707SBarry Smith   PetscFunctionBegin;
26544482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
265529bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
265629bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2657be6bf707SBarry Smith 
2658c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2659be6bf707SBarry Smith   if (f) {
2660be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2661be6bf707SBarry Smith   } else {
2662634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2663be6bf707SBarry Smith   }
2664be6bf707SBarry Smith   PetscFunctionReturn(0);
2665be6bf707SBarry Smith }
2666be6bf707SBarry Smith 
2667fb2e594dSBarry Smith EXTERN_C_BEGIN
26684a2ae208SSatish Balay #undef __FUNCT__
26694a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2670be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2671be6bf707SBarry Smith {
2672be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26736849ba73SBarry Smith   PetscErrorCode ierr;
2674899cda47SBarry Smith   PetscInt       nz = aij->i[mat->rmap.n];
2675be6bf707SBarry Smith 
2676be6bf707SBarry Smith   PetscFunctionBegin;
2677be6bf707SBarry Smith   if (aij->nonew != 1) {
2678512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2679be6bf707SBarry Smith   }
2680be6bf707SBarry Smith   if (!aij->saved_values) {
2681634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2682be6bf707SBarry Smith   }
2683be6bf707SBarry Smith   /* copy values over */
268487828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2685be6bf707SBarry Smith   PetscFunctionReturn(0);
2686be6bf707SBarry Smith }
2687fb2e594dSBarry Smith EXTERN_C_END
2688be6bf707SBarry Smith 
26894a2ae208SSatish Balay #undef __FUNCT__
26904a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2691be6bf707SBarry Smith /*@
2692be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2693be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2694be6bf707SBarry Smith        nonlinear portion.
2695be6bf707SBarry Smith 
2696be6bf707SBarry Smith    Collect on Mat
2697be6bf707SBarry Smith 
2698be6bf707SBarry Smith   Input Parameters:
2699be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2700be6bf707SBarry Smith 
270115091d37SBarry Smith   Level: advanced
270215091d37SBarry Smith 
2703be6bf707SBarry Smith .seealso: MatStoreValues()
2704be6bf707SBarry Smith 
2705be6bf707SBarry Smith @*/
2706be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2707be6bf707SBarry Smith {
2708dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2709be6bf707SBarry Smith 
2710be6bf707SBarry Smith   PetscFunctionBegin;
27114482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
271229bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
271329bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2714be6bf707SBarry Smith 
2715c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2716be6bf707SBarry Smith   if (f) {
2717be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2718be6bf707SBarry Smith   } else {
2719634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2720be6bf707SBarry Smith   }
2721be6bf707SBarry Smith   PetscFunctionReturn(0);
2722be6bf707SBarry Smith }
2723be6bf707SBarry Smith 
2724f83d6046SBarry Smith 
2725be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
27264a2ae208SSatish Balay #undef __FUNCT__
27274a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
272817ab2063SBarry Smith /*@C
2729682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
27300d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
27316e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
273251c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
27332bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
273417ab2063SBarry Smith 
2735db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2736db81eaa0SLois Curfman McInnes 
273717ab2063SBarry Smith    Input Parameters:
2738db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
273917ab2063SBarry Smith .  m - number of rows
274017ab2063SBarry Smith .  n - number of columns
274117ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
274251c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
27432bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
274417ab2063SBarry Smith 
274517ab2063SBarry Smith    Output Parameter:
2746416022c9SBarry Smith .  A - the matrix
274717ab2063SBarry Smith 
2748175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2749175b88e8SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly. This is definitely
2750175b88e8SBarry Smith    true if you plan to use the external direct solvers such as SuperLU, MUMPS or Spooles.
2751175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2752175b88e8SBarry Smith 
2753b259b22eSLois Curfman McInnes    Notes:
275449a6f317SBarry Smith    If nnz is given then nz is ignored
275549a6f317SBarry Smith 
275617ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
275717ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
27580002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
275944cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
276017ab2063SBarry Smith 
276117ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2762a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
27633d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
27646da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
276517ab2063SBarry Smith 
2766682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
27674fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2768682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
27696c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
27706c7ebb05SLois Curfman McInnes 
27716c7ebb05SLois Curfman McInnes    Options Database Keys:
2772698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2773698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2774db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2775db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2776db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
277717ab2063SBarry Smith 
2778027ccd11SLois Curfman McInnes    Level: intermediate
2779027ccd11SLois Curfman McInnes 
278036db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
278136db0b34SBarry Smith 
278217ab2063SBarry Smith @*/
2783be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
278417ab2063SBarry Smith {
2785dfbe8321SBarry Smith   PetscErrorCode ierr;
27866945ee14SBarry Smith 
27873a40ed3dSBarry Smith   PetscFunctionBegin;
2788f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2789f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2790273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2791ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2792273d9f13SBarry Smith   PetscFunctionReturn(0);
2793273d9f13SBarry Smith }
2794273d9f13SBarry Smith 
27954a2ae208SSatish Balay #undef __FUNCT__
27964a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2797273d9f13SBarry Smith /*@C
2798273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2799273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2800273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2801273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2802273d9f13SBarry Smith 
2803273d9f13SBarry Smith    Collective on MPI_Comm
2804273d9f13SBarry Smith 
2805273d9f13SBarry Smith    Input Parameters:
280645bfc511SMatthew Knepley +  B - The matrix
2807273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2808273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2809273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2810273d9f13SBarry Smith 
2811273d9f13SBarry Smith    Notes:
281249a6f317SBarry Smith      If nnz is given then nz is ignored
281349a6f317SBarry Smith 
2814273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2815273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2816273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2817273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2818273d9f13SBarry Smith 
2819273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2820273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2821273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2822273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2823273d9f13SBarry Smith 
2824aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2825aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2826aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2827aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2828aa95bbe8SBarry Smith 
2829a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2830a96a251dSBarry Smith    entries or columns indices
2831a96a251dSBarry Smith 
2832273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2833273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2834273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2835273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2836273d9f13SBarry Smith 
2837273d9f13SBarry Smith    Options Database Keys:
2838698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2839698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2840273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2841273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2842273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2843273d9f13SBarry Smith 
2844273d9f13SBarry Smith    Level: intermediate
2845273d9f13SBarry Smith 
2846aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2847273d9f13SBarry Smith 
2848273d9f13SBarry Smith @*/
2849be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2850273d9f13SBarry Smith {
285197f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2852a23d5eceSKris Buschelman 
2853a23d5eceSKris Buschelman   PetscFunctionBegin;
2854a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2855a23d5eceSKris Buschelman   if (f) {
2856a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2857a23d5eceSKris Buschelman   }
2858a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2859a23d5eceSKris Buschelman }
2860a23d5eceSKris Buschelman 
2861a23d5eceSKris Buschelman EXTERN_C_BEGIN
2862a23d5eceSKris Buschelman #undef __FUNCT__
2863a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2864be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2865a23d5eceSKris Buschelman {
2866273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2867a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
28686849ba73SBarry Smith   PetscErrorCode ierr;
286997f1f81fSBarry Smith   PetscInt       i;
2870273d9f13SBarry Smith 
2871273d9f13SBarry Smith   PetscFunctionBegin;
2872d5d45c9bSBarry Smith 
2873a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2874c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2875c461c341SBarry Smith     nz             = 0;
2876c461c341SBarry Smith   }
2877c461c341SBarry Smith 
2878899cda47SBarry Smith   B->rmap.bs = B->cmap.bs = 1;
28796148ca0dSBarry Smith   ierr = PetscMapSetUp(&B->rmap);CHKERRQ(ierr);
28806148ca0dSBarry Smith   ierr = PetscMapSetUp(&B->cmap);CHKERRQ(ierr);
2881899cda47SBarry Smith 
2882435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2883435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2884b73539f3SBarry Smith   if (nnz) {
2885899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) {
288629bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2887899cda47SBarry Smith       if (nnz[i] > B->cmap.n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap.n);
2888b73539f3SBarry Smith     }
2889b73539f3SBarry Smith   }
2890b73539f3SBarry Smith 
2891273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2892273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2893273d9f13SBarry Smith 
2894ab93d7beSBarry Smith   if (!skipallocation) {
28952ee49352SLisandro Dalcin     if (!b->imax) {
2896899cda47SBarry Smith       ierr = PetscMalloc2(B->rmap.n,PetscInt,&b->imax,B->rmap.n,PetscInt,&b->ilen);CHKERRQ(ierr);
28979518dbb4SMatthew Knepley       ierr = PetscLogObjectMemory(B,2*B->rmap.n*sizeof(PetscInt));CHKERRQ(ierr);
28982ee49352SLisandro Dalcin     }
2899273d9f13SBarry Smith     if (!nnz) {
2900435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2901273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2902899cda47SBarry Smith       for (i=0; i<B->rmap.n; i++) b->imax[i] = nz;
2903899cda47SBarry Smith       nz = nz*B->rmap.n;
2904273d9f13SBarry Smith     } else {
2905273d9f13SBarry Smith       nz = 0;
2906899cda47SBarry Smith       for (i=0; i<B->rmap.n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2907273d9f13SBarry Smith     }
2908ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2909899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) { b->ilen[i] = 0; }
2910ab93d7beSBarry Smith 
2911273d9f13SBarry Smith     /* allocate the matrix space */
29122ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2913899cda47SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap.n+1,PetscInt,&b->i);CHKERRQ(ierr);
29149518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(B,(B->rmap.n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2915bfeeae90SHong Zhang     b->i[0] = 0;
2916899cda47SBarry Smith     for (i=1; i<B->rmap.n+1; i++) {
29175da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29185da197adSKris Buschelman     }
2919273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2920e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2921e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2922c461c341SBarry Smith   } else {
2923e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2924e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2925c461c341SBarry Smith   }
2926273d9f13SBarry Smith 
2927273d9f13SBarry Smith   b->nz                = 0;
2928273d9f13SBarry Smith   b->maxnz             = nz;
2929273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2930273d9f13SBarry Smith   PetscFunctionReturn(0);
2931273d9f13SBarry Smith }
2932a23d5eceSKris Buschelman EXTERN_C_END
2933273d9f13SBarry Smith 
2934a1661176SMatthew Knepley #undef  __FUNCT__
2935a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
293658d36128SBarry Smith /*@
2937a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2938a1661176SMatthew Knepley 
2939a1661176SMatthew Knepley    Input Parameters:
2940a1661176SMatthew Knepley +  B - the matrix
2941a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2942a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2943a1661176SMatthew Knepley -  v - optional values in the matrix
2944a1661176SMatthew Knepley 
2945d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
2946d58b2322SMatthew Knepley 
2947a1661176SMatthew Knepley    Level: developer
2948a1661176SMatthew Knepley 
294958d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
295058d36128SBarry Smith 
2951a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2952a1661176SMatthew Knepley 
2953a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
2954a1661176SMatthew Knepley @*/
2955a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
2956a1661176SMatthew Knepley {
2957a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2958a1661176SMatthew Knepley   PetscErrorCode ierr;
2959a1661176SMatthew Knepley 
2960a1661176SMatthew Knepley   PetscFunctionBegin;
2961a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
2962a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2963a1661176SMatthew Knepley   if (f) {
2964a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2965a1661176SMatthew Knepley   }
2966a1661176SMatthew Knepley   PetscFunctionReturn(0);
2967a1661176SMatthew Knepley }
2968a1661176SMatthew Knepley 
2969a1661176SMatthew Knepley EXTERN_C_BEGIN
2970a1661176SMatthew Knepley #undef  __FUNCT__
2971a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
2972b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
2973a1661176SMatthew Knepley {
2974a1661176SMatthew Knepley   PetscInt       i;
2975a1661176SMatthew Knepley   PetscInt       m,n;
2976a1661176SMatthew Knepley   PetscInt       nz;
2977a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
2978a1661176SMatthew Knepley   PetscScalar    *values;
2979a1661176SMatthew Knepley   PetscErrorCode ierr;
2980a1661176SMatthew Knepley 
2981a1661176SMatthew Knepley   PetscFunctionBegin;
2982a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
2983a1661176SMatthew Knepley 
2984b7940d39SSatish Balay   if (Ii[0]) {
2985b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
2986a1661176SMatthew Knepley   }
2987a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
2988a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2989b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
2990a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
2991a1661176SMatthew Knepley     if (nz < 0) {
2992a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
2993a1661176SMatthew Knepley     }
2994a1661176SMatthew Knepley     nnz[i] = nz;
2995a1661176SMatthew Knepley   }
2996a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
2997a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
2998a1661176SMatthew Knepley 
2999a1661176SMatthew Knepley   if (v) {
3000a1661176SMatthew Knepley     values = (PetscScalar*) v;
3001a1661176SMatthew Knepley   } else {
3002a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3003a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3004a1661176SMatthew Knepley   }
3005a1661176SMatthew Knepley 
3006a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3007b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3008b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3009a1661176SMatthew Knepley   }
3010a1661176SMatthew Knepley 
3011a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3012a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3013a1661176SMatthew Knepley 
3014a1661176SMatthew Knepley   if (!v) {
3015a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3016a1661176SMatthew Knepley   }
3017a1661176SMatthew Knepley   PetscFunctionReturn(0);
3018a1661176SMatthew Knepley }
3019a1661176SMatthew Knepley EXTERN_C_END
3020a1661176SMatthew Knepley 
3021170fe5c8SBarry Smith #include "src/mat/impls/dense/seq/dense.h"
30221de00fd4SBarry Smith #include "src/inline/axpy.h"
3023170fe5c8SBarry Smith 
3024170fe5c8SBarry Smith #undef __FUNCT__
3025170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3026170fe5c8SBarry Smith /*
3027170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3028170fe5c8SBarry Smith 
3029170fe5c8SBarry Smith                n                       p                          p
3030170fe5c8SBarry Smith         (              )       (              )         (                  )
3031170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3032170fe5c8SBarry Smith         (              )       (              )         (                  )
3033170fe5c8SBarry Smith 
3034170fe5c8SBarry Smith */
3035170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3036170fe5c8SBarry Smith {
3037170fe5c8SBarry Smith   PetscErrorCode     ierr;
3038170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3039170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3040170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
30411de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3042170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3043170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3044170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3045170fe5c8SBarry Smith 
3046170fe5c8SBarry Smith   PetscFunctionBegin;
3047170fe5c8SBarry Smith   m = A->rmap.n;
3048170fe5c8SBarry Smith   n = A->cmap.n;
3049170fe5c8SBarry Smith   p = B->cmap.n;
3050170fe5c8SBarry Smith   a = sub_a->v;
3051170fe5c8SBarry Smith   b = sub_b->a;
3052170fe5c8SBarry Smith   c = sub_c->v;
3053170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3054170fe5c8SBarry Smith 
3055170fe5c8SBarry Smith   ii  = sub_b->i;
3056170fe5c8SBarry Smith   idx = sub_b->j;
3057170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3058170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3059170fe5c8SBarry Smith     while (q-->0) {
3060170fe5c8SBarry Smith       c_q = c + m*(*idx);
3061170fe5c8SBarry Smith       a_q = a + m*i;
30621de00fd4SBarry Smith       APXY(c_q,*b,a_q,m);
3063170fe5c8SBarry Smith       idx++;
3064170fe5c8SBarry Smith       b++;
3065170fe5c8SBarry Smith     }
3066170fe5c8SBarry Smith   }
3067170fe5c8SBarry Smith   PetscFunctionReturn(0);
3068170fe5c8SBarry Smith }
3069170fe5c8SBarry Smith 
3070170fe5c8SBarry Smith #undef __FUNCT__
3071170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3072170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3073170fe5c8SBarry Smith {
3074170fe5c8SBarry Smith   PetscErrorCode ierr;
3075170fe5c8SBarry Smith   PetscInt       m=A->rmap.n,n=B->cmap.n;
3076170fe5c8SBarry Smith   Mat            Cmat;
3077170fe5c8SBarry Smith 
3078170fe5c8SBarry Smith   PetscFunctionBegin;
3079170fe5c8SBarry Smith   if (A->cmap.n != B->rmap.n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap.n %d != B->rmap.n %d\n",A->cmap.n,B->rmap.n);
3080170fe5c8SBarry Smith   ierr = MatCreate(A->hdr.comm,&Cmat);CHKERRQ(ierr);
3081170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3082170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3083170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3084170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3085170fe5c8SBarry Smith   *C = Cmat;
3086170fe5c8SBarry Smith   PetscFunctionReturn(0);
3087170fe5c8SBarry Smith }
3088170fe5c8SBarry Smith 
3089170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3090170fe5c8SBarry Smith #undef __FUNCT__
3091170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3092170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3093170fe5c8SBarry Smith {
3094170fe5c8SBarry Smith   PetscErrorCode ierr;
3095170fe5c8SBarry Smith 
3096170fe5c8SBarry Smith   PetscFunctionBegin;
3097170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3098170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3099170fe5c8SBarry Smith   }
3100170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3101170fe5c8SBarry Smith   PetscFunctionReturn(0);
3102170fe5c8SBarry Smith }
3103170fe5c8SBarry Smith 
3104170fe5c8SBarry Smith 
31050bad9183SKris Buschelman /*MC
3106fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31070bad9183SKris Buschelman    based on compressed sparse row format.
31080bad9183SKris Buschelman 
31090bad9183SKris Buschelman    Options Database Keys:
31100bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31110bad9183SKris Buschelman 
31120bad9183SKris Buschelman   Level: beginner
31130bad9183SKris Buschelman 
3114f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31150bad9183SKris Buschelman M*/
31160bad9183SKris Buschelman 
3117a6175056SHong Zhang EXTERN_C_BEGIN
31188cf70c4bSSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,const MatType,MatReuse,Mat*);
311917667f90SBarry Smith EXTERN_C_END
312017667f90SBarry Smith 
312117667f90SBarry Smith EXTERN_C_BEGIN
31224a2ae208SSatish Balay #undef __FUNCT__
31234a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3124be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3125273d9f13SBarry Smith {
3126273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3127dfbe8321SBarry Smith   PetscErrorCode ierr;
312838baddfdSBarry Smith   PetscMPIInt    size;
3129273d9f13SBarry Smith 
3130273d9f13SBarry Smith   PetscFunctionBegin;
31317adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3132273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3133273d9f13SBarry Smith 
313438f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3135b0a32e0cSBarry Smith   B->data             = (void*)b;
3136549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3137416022c9SBarry Smith   B->factor           = 0;
313890f02eecSBarry Smith   B->mapping          = 0;
3139416022c9SBarry Smith   b->row              = 0;
3140416022c9SBarry Smith   b->col              = 0;
314182bf6240SBarry Smith   b->icol             = 0;
3142b810aeb4SBarry Smith   b->reallocs         = 0;
314336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3144f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3145416022c9SBarry Smith   b->nonew             = 0;
3146416022c9SBarry Smith   b->diag              = 0;
3147416022c9SBarry Smith   b->solve_work        = 0;
31482a1b7f2aSHong Zhang   B->spptr             = 0;
3149be6bf707SBarry Smith   b->saved_values      = 0;
3150d7f994e1SBarry Smith   b->idiag             = 0;
315171f1c65dSBarry Smith   b->mdiag             = 0;
315271f1c65dSBarry Smith   b->ssor_work         = 0;
315371f1c65dSBarry Smith   b->omega             = 1.0;
315471f1c65dSBarry Smith   b->fshift            = 0.0;
315571f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3156f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
3157a30b2313SHong Zhang   b->xtoy              = 0;
3158a30b2313SHong Zhang   b->XtoY              = 0;
315973e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3160899cda47SBarry Smith   b->compressedrow.nrows   = B->rmap.n;
3161d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3162d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3163d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
316488e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
316517ab2063SBarry Smith 
316635d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3167f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3168bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3169bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3170f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3171be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3172bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3173f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3174be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3175bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3176a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3177a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3178a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
317985fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
318085fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
318185fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3182ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3183ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3184ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
318517667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
318617667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
318717667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
31885fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
31895fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
31905fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
31911cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
31921cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
31931cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3194a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3195a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3196a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3197a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3198a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3199a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
320005b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
320105b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
320205b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3203170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3204170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3205170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3206170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3207170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3208170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3209170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3210170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3211170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
32124846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
321317667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
32143a40ed3dSBarry Smith   PetscFunctionReturn(0);
321517ab2063SBarry Smith }
3216273d9f13SBarry Smith EXTERN_C_END
321717ab2063SBarry Smith 
32184a2ae208SSatish Balay #undef __FUNCT__
32194a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
3220dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
322117ab2063SBarry Smith {
3222416022c9SBarry Smith   Mat            C;
3223416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
32246849ba73SBarry Smith   PetscErrorCode ierr;
3225899cda47SBarry Smith   PetscInt       i,m = A->rmap.n;
322617ab2063SBarry Smith 
32273a40ed3dSBarry Smith   PetscFunctionBegin;
32284043dd9cSLois Curfman McInnes   *B = 0;
32297adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
3230899cda47SBarry Smith   ierr = MatSetSizes(C,A->rmap.n,A->cmap.n,A->rmap.n,A->cmap.n);CHKERRQ(ierr);
32317adad957SLisandro Dalcin   ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
32321d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
32331d5dac46SHong Zhang 
32347adad957SLisandro Dalcin   ierr = PetscMapCopy(((PetscObject)A)->comm,&A->rmap,&C->rmap);CHKERRQ(ierr);
32357adad957SLisandro Dalcin   ierr = PetscMapCopy(((PetscObject)A)->comm,&A->cmap,&C->cmap);CHKERRQ(ierr);
3236899cda47SBarry Smith 
3237273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3238273d9f13SBarry Smith 
3239416022c9SBarry Smith   C->factor           = A->factor;
32406ad4291fSHong Zhang 
3241416022c9SBarry Smith   c->row            = 0;
3242416022c9SBarry Smith   c->col            = 0;
324382bf6240SBarry Smith   c->icol           = 0;
32446ad4291fSHong Zhang   c->reallocs       = 0;
324517ab2063SBarry Smith 
32466ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
324717ab2063SBarry Smith 
324833b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
32499518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
325017ab2063SBarry Smith   for (i=0; i<m; i++) {
3251416022c9SBarry Smith     c->imax[i] = a->imax[i];
3252416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
325317ab2063SBarry Smith   }
325417ab2063SBarry Smith 
325517ab2063SBarry Smith   /* allocate the matrix space */
3256a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
32579518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3258f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
325997f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
326017ab2063SBarry Smith   if (m > 0) {
326197f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3262be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
3263bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3264be6bf707SBarry Smith     } else {
3265bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
326617ab2063SBarry Smith     }
326708480c60SBarry Smith   }
326817ab2063SBarry Smith 
32696ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3270416022c9SBarry Smith   c->roworiented       = a->roworiented;
3271416022c9SBarry Smith   c->nonew             = a->nonew;
3272416022c9SBarry Smith   if (a->diag) {
327397f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
327452e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
327517ab2063SBarry Smith     for (i=0; i<m; i++) {
3276416022c9SBarry Smith       c->diag[i] = a->diag[i];
327717ab2063SBarry Smith     }
32783a40ed3dSBarry Smith   } else c->diag           = 0;
32796ad4291fSHong Zhang   c->solve_work            = 0;
32806ad4291fSHong Zhang   c->saved_values          = 0;
32816ad4291fSHong Zhang   c->idiag                 = 0;
328271f1c65dSBarry Smith   c->ssor_work             = 0;
32836ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
3284e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3285e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
32866ad4291fSHong Zhang   c->xtoy                  = 0;
32876ad4291fSHong Zhang   c->XtoY                  = 0;
32886ad4291fSHong Zhang 
3289416022c9SBarry Smith   c->nz                 = a->nz;
3290416022c9SBarry Smith   c->maxnz              = a->maxnz;
3291273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3292754ec7b1SSatish Balay 
32936ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
32946ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
32956ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
32966ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
32976ad4291fSHong Zhang     i = a->compressedrow.nrows;
32986ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
32996ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
33006ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
33016ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
330227ea64f8SHong Zhang   } else {
330327ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
330427ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
330527ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
33066ad4291fSHong Zhang   }
330788e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
33084846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
33094846f1f5SKris Buschelman 
3310416022c9SBarry Smith   *B = C;
33117adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
33123a40ed3dSBarry Smith   PetscFunctionReturn(0);
331317ab2063SBarry Smith }
331417ab2063SBarry Smith 
33154a2ae208SSatish Balay #undef __FUNCT__
33164a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3317a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
331817ab2063SBarry Smith {
3319416022c9SBarry Smith   Mat_SeqAIJ     *a;
3320416022c9SBarry Smith   Mat            B;
33216849ba73SBarry Smith   PetscErrorCode ierr;
33223c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
332338baddfdSBarry Smith   int            fd;
332438baddfdSBarry Smith   PetscMPIInt    size;
3325bcd2baecSBarry Smith   MPI_Comm       comm;
332617ab2063SBarry Smith 
33273a40ed3dSBarry Smith   PetscFunctionBegin;
3328e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3329d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
333029bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3331b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
33320752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3333552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
333417ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
333517ab2063SBarry Smith 
3336d64ed03dSBarry Smith   if (nz < 0) {
333729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3338d64ed03dSBarry Smith   }
3339d64ed03dSBarry Smith 
334017ab2063SBarry Smith   /* read in row lengths */
334197f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
33420752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
334317ab2063SBarry Smith 
33443c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
33453c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
33463c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
33473c601197SSatish Balay 
334817ab2063SBarry Smith   /* create our matrix */
3349f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3350f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3351b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3352ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3353416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
335417ab2063SBarry Smith 
33550752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
335617ab2063SBarry Smith 
335717ab2063SBarry Smith   /* read in nonzero values */
33580752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
335917ab2063SBarry Smith 
336017ab2063SBarry Smith   /* set matrix "i" values */
3361efb16452SHong Zhang   a->i[0] = 0;
336217ab2063SBarry Smith   for (i=1; i<= M; i++) {
3363416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3364416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
336517ab2063SBarry Smith   }
3366606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
336717ab2063SBarry Smith 
33686d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
33696d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3370b3a1e11cSKris Buschelman   *A = B;
33713a40ed3dSBarry Smith   PetscFunctionReturn(0);
337217ab2063SBarry Smith }
337317ab2063SBarry Smith 
33744a2ae208SSatish Balay #undef __FUNCT__
3375b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3376dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
33777264ac53SSatish Balay {
33787264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3379dfbe8321SBarry Smith   PetscErrorCode ierr;
33807264ac53SSatish Balay 
33813a40ed3dSBarry Smith   PetscFunctionBegin;
3382bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3383899cda47SBarry Smith   if ((A->rmap.n != B->rmap.n) || (A->cmap.n != B->cmap.n) ||(a->nz != b->nz)) {
3384ca44d042SBarry Smith     *flg = PETSC_FALSE;
3385ca44d042SBarry Smith     PetscFunctionReturn(0);
3386bcd2baecSBarry Smith   }
33877264ac53SSatish Balay 
33887264ac53SSatish Balay   /* if the a->i are the same */
3389899cda47SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap.n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3390abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
33917264ac53SSatish Balay 
33927264ac53SSatish Balay   /* if a->j are the same */
339397f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3394abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3395bcd2baecSBarry Smith 
3396bcd2baecSBarry Smith   /* if a->a are the same */
339787828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
33980f5bd95cSBarry Smith 
33993a40ed3dSBarry Smith   PetscFunctionReturn(0);
34007264ac53SSatish Balay 
34017264ac53SSatish Balay }
340236db0b34SBarry Smith 
34034a2ae208SSatish Balay #undef __FUNCT__
34044a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
340505869f15SSatish Balay /*@
340636db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
340736db0b34SBarry Smith               provided by the user.
340836db0b34SBarry Smith 
3409c75a6043SHong Zhang       Collective on MPI_Comm
341036db0b34SBarry Smith 
341136db0b34SBarry Smith    Input Parameters:
341236db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
341336db0b34SBarry Smith .   m - number of rows
341436db0b34SBarry Smith .   n - number of columns
341536db0b34SBarry Smith .   i - row indices
341636db0b34SBarry Smith .   j - column indices
341736db0b34SBarry Smith -   a - matrix values
341836db0b34SBarry Smith 
341936db0b34SBarry Smith    Output Parameter:
342036db0b34SBarry Smith .   mat - the matrix
342136db0b34SBarry Smith 
342236db0b34SBarry Smith    Level: intermediate
342336db0b34SBarry Smith 
342436db0b34SBarry Smith    Notes:
34250551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
342636db0b34SBarry Smith     once the matrix is destroyed
342736db0b34SBarry Smith 
342836db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
342936db0b34SBarry Smith 
3430bfeeae90SHong Zhang        The i and j indices are 0 based
343136db0b34SBarry Smith 
3432a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3433a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3434a4552177SSatish Balay     as shown:
3435a4552177SSatish Balay 
3436a4552177SSatish Balay         1 0 0
3437a4552177SSatish Balay         2 0 3
3438a4552177SSatish Balay         4 5 6
3439a4552177SSatish Balay 
3440a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
34419985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3442a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3443a4552177SSatish Balay 
34449985e31cSBarry Smith 
34452fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
344636db0b34SBarry Smith 
344736db0b34SBarry Smith @*/
3448a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
344936db0b34SBarry Smith {
3450dfbe8321SBarry Smith   PetscErrorCode ierr;
3451cbcfb4deSHong Zhang   PetscInt       ii;
345236db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3453cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3454cbcfb4deSHong Zhang   PetscInt       jj;
3455cbcfb4deSHong Zhang #endif
345636db0b34SBarry Smith 
345736db0b34SBarry Smith   PetscFunctionBegin;
3458a96a251dSBarry Smith   if (i[0]) {
3459634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
346036db0b34SBarry Smith   }
3461f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3462f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3463ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3464ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3465ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3466ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3467ab93d7beSBarry Smith 
346836db0b34SBarry Smith   aij->i = i;
346936db0b34SBarry Smith   aij->j = j;
347036db0b34SBarry Smith   aij->a = a;
347136db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
347236db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3473e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3474e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
347536db0b34SBarry Smith 
347636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
347736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
34782515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
347979a5c55eSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
34809985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
34819985e31cSBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
34829985e31cSBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
34839985e31cSBarry Smith     }
348436db0b34SBarry Smith #endif
348536db0b34SBarry Smith   }
34862515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
348736db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
348879a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
348979a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
349036db0b34SBarry Smith   }
349136db0b34SBarry Smith #endif
349236db0b34SBarry Smith 
3493b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3494b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
349536db0b34SBarry Smith   PetscFunctionReturn(0);
349636db0b34SBarry Smith }
349736db0b34SBarry Smith 
3498cc8ba8e1SBarry Smith #undef __FUNCT__
3499ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3500dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3501cc8ba8e1SBarry Smith {
3502dfbe8321SBarry Smith   PetscErrorCode ierr;
3503cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
350436db0b34SBarry Smith 
3505cc8ba8e1SBarry Smith   PetscFunctionBegin;
35068ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3507cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3508cc8ba8e1SBarry Smith     a->coloring = coloring;
350912c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
351097f1f81fSBarry Smith     PetscInt             i,*larray;
351112c595b3SBarry Smith     ISColoring      ocoloring;
351208b6dcc0SBarry Smith     ISColoringValue *colors;
351312c595b3SBarry Smith 
351412c595b3SBarry Smith     /* set coloring for diagonal portion */
3515899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3516899cda47SBarry Smith     for (i=0; i<A->cmap.n; i++) {
351712c595b3SBarry Smith       larray[i] = i;
351812c595b3SBarry Smith     }
3519899cda47SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap.n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3520899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3521899cda47SBarry Smith     for (i=0; i<A->cmap.n; i++) {
352212c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
352312c595b3SBarry Smith     }
352412c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
352595a80f87SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap.n,colors,&ocoloring);CHKERRQ(ierr);
352612c595b3SBarry Smith     a->coloring = ocoloring;
352712c595b3SBarry Smith   }
3528cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3529cc8ba8e1SBarry Smith }
3530cc8ba8e1SBarry Smith 
3531dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3532ee4f033dSBarry Smith EXTERN_C_BEGIN
353329c1e371SBarry Smith #include "adic/ad_utils.h"
3534ee4f033dSBarry Smith EXTERN_C_END
3535cc8ba8e1SBarry Smith 
3536cc8ba8e1SBarry Smith #undef __FUNCT__
3537ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3538dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3539cc8ba8e1SBarry Smith {
3540cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3541899cda47SBarry Smith   PetscInt        m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
35424440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
354308b6dcc0SBarry Smith   ISColoringValue *color;
3544cc8ba8e1SBarry Smith 
3545cc8ba8e1SBarry Smith   PetscFunctionBegin;
3546e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
35474440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3548cc8ba8e1SBarry Smith   color = a->coloring->colors;
3549cc8ba8e1SBarry Smith   /* loop over rows */
3550cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3551cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3552cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3553cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3554cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3555cc8ba8e1SBarry Smith     }
35564440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3557ee4f033dSBarry Smith   }
3558ee4f033dSBarry Smith   PetscFunctionReturn(0);
3559ee4f033dSBarry Smith }
3560ee4f033dSBarry Smith #endif
3561ee4f033dSBarry Smith 
3562ee4f033dSBarry Smith #undef __FUNCT__
3563ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
356497f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3565ee4f033dSBarry Smith {
3566ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3567899cda47SBarry Smith   PetscInt         m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j;
356854f21887SBarry Smith   MatScalar       *v = a->a;
356954f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
357008b6dcc0SBarry Smith   ISColoringValue *color;
3571ee4f033dSBarry Smith 
3572ee4f033dSBarry Smith   PetscFunctionBegin;
3573e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3574ee4f033dSBarry Smith   color = a->coloring->colors;
3575ee4f033dSBarry Smith   /* loop over rows */
3576ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3577ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3578ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3579ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3580ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3581ee4f033dSBarry Smith     }
3582ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3583cc8ba8e1SBarry Smith   }
3584cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3585cc8ba8e1SBarry Smith }
358636db0b34SBarry Smith 
358781824310SBarry Smith /*
358881824310SBarry Smith     Special version for direct calls from Fortran
358981824310SBarry Smith */
359081824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
359181824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
359281824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
359381824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
359481824310SBarry Smith #endif
359581824310SBarry Smith 
359681824310SBarry Smith /* Change these macros so can be used in void function */
359781824310SBarry Smith #undef CHKERRQ
35987adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
359981824310SBarry Smith #undef SETERRQ2
36007adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
360181824310SBarry Smith 
360281824310SBarry Smith EXTERN_C_BEGIN
360381824310SBarry Smith #undef __FUNCT__
360481824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
36051f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
360681824310SBarry Smith {
360781824310SBarry Smith   Mat            A = *AA;
360881824310SBarry Smith   PetscInt       m = *mm, n = *nn;
360981824310SBarry Smith   InsertMode     is = *isis;
361081824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
361181824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
361281824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
361381824310SBarry Smith   PetscErrorCode ierr;
361481824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
361554f21887SBarry Smith   MatScalar      *ap,value,*aa;
3616edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
361781824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
361881824310SBarry Smith 
361981824310SBarry Smith   PetscFunctionBegin;
362081824310SBarry Smith   MatPreallocated(A);
362181824310SBarry Smith   imax = a->imax;
362281824310SBarry Smith   ai = a->i;
362381824310SBarry Smith   ailen = a->ilen;
362481824310SBarry Smith   aj = a->j;
362581824310SBarry Smith   aa = a->a;
362681824310SBarry Smith 
362781824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
362881824310SBarry Smith     row  = im[k];
362981824310SBarry Smith     if (row < 0) continue;
363081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
36317adad957SLisandro Dalcin     if (row >= A->rmap.n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
363281824310SBarry Smith #endif
363381824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
363481824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
363581824310SBarry Smith     low  = 0;
363681824310SBarry Smith     high = nrow;
363781824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
363881824310SBarry Smith       if (in[l] < 0) continue;
363981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
36407adad957SLisandro Dalcin       if (in[l] >= A->cmap.n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
364181824310SBarry Smith #endif
364281824310SBarry Smith       col = in[l];
364381824310SBarry Smith       if (roworiented) {
364481824310SBarry Smith         value = v[l + k*n];
364581824310SBarry Smith       } else {
364681824310SBarry Smith         value = v[k + l*m];
364781824310SBarry Smith       }
364881824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
364981824310SBarry Smith 
365081824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
365181824310SBarry Smith       lastcol = col;
365281824310SBarry Smith       while (high-low > 5) {
365381824310SBarry Smith         t = (low+high)/2;
365481824310SBarry Smith         if (rp[t] > col) high = t;
365581824310SBarry Smith         else             low  = t;
365681824310SBarry Smith       }
365781824310SBarry Smith       for (i=low; i<high; i++) {
365881824310SBarry Smith         if (rp[i] > col) break;
365981824310SBarry Smith         if (rp[i] == col) {
366081824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
366181824310SBarry Smith           else                  ap[i] = value;
366281824310SBarry Smith           goto noinsert;
366381824310SBarry Smith         }
366481824310SBarry Smith       }
366581824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
366681824310SBarry Smith       if (nonew == 1) goto noinsert;
36677adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3668421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
366981824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
367081824310SBarry Smith       /* shift up all the later entries in this row */
367181824310SBarry Smith       for (ii=N; ii>=i; ii--) {
367281824310SBarry Smith         rp[ii+1] = rp[ii];
367381824310SBarry Smith         ap[ii+1] = ap[ii];
367481824310SBarry Smith       }
367581824310SBarry Smith       rp[i] = col;
367681824310SBarry Smith       ap[i] = value;
367781824310SBarry Smith       noinsert:;
367881824310SBarry Smith       low = i + 1;
367981824310SBarry Smith     }
368081824310SBarry Smith     ailen[row] = nrow;
368181824310SBarry Smith   }
368281824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
368381824310SBarry Smith   PetscFunctionReturnVoid();
368481824310SBarry Smith }
368581824310SBarry Smith EXTERN_C_END
3686