xref: /petsc/src/mat/impls/aij/seq/aij.c (revision fef13f97fc8065f3070ca9972f2f2fb67800151f)
1992c00aaSSatish Balay #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3b377110cSBarry Smith 
4d5d45c9bSBarry Smith /*
53369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
6d5d45c9bSBarry Smith   matrix storage format.
7d5d45c9bSBarry Smith */
83369ce9aSBarry Smith 
97c4f633dSBarry Smith 
107c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
11f3da1532SBarry Smith #include "petscblaslapack.h"
120a835dfdSSatish Balay #include "petscbt.h"
1317ab2063SBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
1579299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1679299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1779299369SBarry Smith {
1879299369SBarry Smith   PetscErrorCode ierr;
1979299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
20d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
2154f21887SBarry Smith   MatScalar      *aa = aij->a;
2254f21887SBarry Smith   PetscScalar    *v;
2309f38230SBarry Smith   PetscTruth     missing;
2479299369SBarry Smith 
2579299369SBarry Smith   PetscFunctionBegin;
2609f38230SBarry Smith   if (Y->assembled) {
2709f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2809f38230SBarry Smith     if (!missing) {
2979299369SBarry Smith       diag = aij->diag;
3079299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3179299369SBarry Smith       if (is == INSERT_VALUES) {
3279299369SBarry Smith 	for (i=0; i<m; i++) {
3379299369SBarry Smith 	  aa[diag[i]] = v[i];
3479299369SBarry Smith 	}
3579299369SBarry Smith       } else {
3679299369SBarry Smith 	for (i=0; i<m; i++) {
3779299369SBarry Smith 	  aa[diag[i]] += v[i];
3879299369SBarry Smith 	}
3979299369SBarry Smith       }
4079299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4179299369SBarry Smith       PetscFunctionReturn(0);
4279299369SBarry Smith     }
4309f38230SBarry Smith   }
4409f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4509f38230SBarry Smith   PetscFunctionReturn(0);
4609f38230SBarry Smith }
4779299369SBarry Smith 
4879299369SBarry Smith #undef __FUNCT__
494a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
503acb8795SBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5117ab2063SBarry Smith {
52416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
53dfbe8321SBarry Smith   PetscErrorCode ierr;
5497f1f81fSBarry Smith   PetscInt       i,ishift;
5517ab2063SBarry Smith 
563a40ed3dSBarry Smith   PetscFunctionBegin;
57d0f46423SBarry Smith   *m     = A->rmap->n;
583a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
59bfeeae90SHong Zhang   ishift = 0;
6053e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
61d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
62bfeeae90SHong Zhang   } else if (oshift == 1) {
63d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
643b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
65d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
66d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
67ecc77c7aSBarry Smith     if (ja) {
6897f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
693b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
70ecc77c7aSBarry Smith     }
716945ee14SBarry Smith   } else {
72ecc77c7aSBarry Smith     *ia = a->i;
73ecc77c7aSBarry Smith     if (ja) *ja = a->j;
74a2ce50c7SBarry Smith   }
753a40ed3dSBarry Smith   PetscFunctionReturn(0);
76a2744918SBarry Smith }
77a2744918SBarry Smith 
784a2ae208SSatish Balay #undef __FUNCT__
794a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
803acb8795SBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
816945ee14SBarry Smith {
82dfbe8321SBarry Smith   PetscErrorCode ierr;
836945ee14SBarry Smith 
843a40ed3dSBarry Smith   PetscFunctionBegin;
853a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
86bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
87606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
88ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
89bcd2baecSBarry Smith   }
903a40ed3dSBarry Smith   PetscFunctionReturn(0);
9117ab2063SBarry Smith }
9217ab2063SBarry Smith 
934a2ae208SSatish Balay #undef __FUNCT__
944a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
953acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
963b2fbd54SBarry Smith {
973b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
98dfbe8321SBarry Smith   PetscErrorCode ierr;
99d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
10097f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1013b2fbd54SBarry Smith 
1023a40ed3dSBarry Smith   PetscFunctionBegin;
103899cda47SBarry Smith   *nn = n;
1043a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1053b2fbd54SBarry Smith   if (symmetric) {
106d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1073b2fbd54SBarry Smith   } else {
10897f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11197f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1123b2fbd54SBarry Smith     jj = a->j;
1133b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
114bfeeae90SHong Zhang       collengths[jj[i]]++;
1153b2fbd54SBarry Smith     }
1163b2fbd54SBarry Smith     cia[0] = oshift;
1173b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1183b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1193b2fbd54SBarry Smith     }
12097f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1213b2fbd54SBarry Smith     jj   = a->j;
122a93ec695SBarry Smith     for (row=0; row<m; row++) {
123a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
124a93ec695SBarry Smith       for (i=0; i<mr; i++) {
125bfeeae90SHong Zhang         col = *jj++;
1263b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1273b2fbd54SBarry Smith       }
1283b2fbd54SBarry Smith     }
129606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1303b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1313b2fbd54SBarry Smith   }
1323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1333b2fbd54SBarry Smith }
1343b2fbd54SBarry Smith 
1354a2ae208SSatish Balay #undef __FUNCT__
1364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1373acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1383b2fbd54SBarry Smith {
139dfbe8321SBarry Smith   PetscErrorCode ierr;
140606d414cSSatish Balay 
1413a40ed3dSBarry Smith   PetscFunctionBegin;
1423a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1433b2fbd54SBarry Smith 
144606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
145606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1463b2fbd54SBarry Smith 
1473a40ed3dSBarry Smith   PetscFunctionReturn(0);
1483b2fbd54SBarry Smith }
1493b2fbd54SBarry Smith 
15087d4246cSBarry Smith #undef __FUNCT__
15187d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15287d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15387d4246cSBarry Smith {
15487d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15587d4246cSBarry Smith   PetscInt       *ai = a->i;
15687d4246cSBarry Smith   PetscErrorCode ierr;
15787d4246cSBarry Smith 
15887d4246cSBarry Smith   PetscFunctionBegin;
15987d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
16087d4246cSBarry Smith   PetscFunctionReturn(0);
16187d4246cSBarry Smith }
16287d4246cSBarry Smith 
1634a2ae208SSatish Balay #undef __FUNCT__
1644a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16597f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16617ab2063SBarry Smith {
167416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
168e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
16997f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1706849ba73SBarry Smith   PetscErrorCode ierr;
171e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17254f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
173edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
174273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17517ab2063SBarry Smith 
1763a40ed3dSBarry Smith   PetscFunctionBegin;
17771fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
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)
182e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
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)
191e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
1923b2fbd54SBarry Smith #endif
193bfeeae90SHong Zhang       col = in[l];
19416371a99SBarry 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 	}
20016371a99SBarry Smith       } else {
20175567043SBarry Smith         value = 0.;
20216371a99SBarry 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;
223e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
224*fef13f97SBarry 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];
255e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
256e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
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 */
260e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
261e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
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);
295d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
2960700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
297d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
298d0f46423SBarry 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 */
302d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
303416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30417ab2063SBarry Smith   }
305d0f46423SBarry 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;
324d0f46423SBarry 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;
333d0f46423SBarry 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);
337d0f46423SBarry 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) {
352d0f46423SBarry 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;
435d0f46423SBarry 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);
4513c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4523c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4533c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4543c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4553c215bfdSMatthew Knepley #else
4563c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4573c215bfdSMatthew Knepley #endif
458d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4593c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4603c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4613c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4623c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4633c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4643c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
4653c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G -%G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4663c215bfdSMatthew Knepley         } else {
4673c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4683c215bfdSMatthew Knepley         }
4693c215bfdSMatthew Knepley #else
4703c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4713c215bfdSMatthew Knepley #endif
4723c215bfdSMatthew Knepley       }
4733c215bfdSMatthew Knepley     }
4743c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4753a40ed3dSBarry Smith   } else {
476d6e78c31SSatish Balay     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
477d5f3da31SBarry Smith     if (A->factortype){
47816cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
47916cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
48016cd7e1dSShri Abhyankar         /* L part */
48116cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
48216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
48316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
48416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48516cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
48616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48716cd7e1dSShri Abhyankar           } else {
48816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
48916cd7e1dSShri Abhyankar           }
49016cd7e1dSShri Abhyankar #else
49116cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49216cd7e1dSShri Abhyankar #endif
49316cd7e1dSShri Abhyankar         }
49416cd7e1dSShri Abhyankar 	/* diagonal */
49516cd7e1dSShri Abhyankar 	j = a->diag[i];
49616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
49716cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
49816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
49916cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
50016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50116cd7e1dSShri Abhyankar           } else {
50216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
50316cd7e1dSShri Abhyankar           }
50416cd7e1dSShri Abhyankar #else
50516cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
50616cd7e1dSShri Abhyankar #endif
50716cd7e1dSShri Abhyankar 
50816cd7e1dSShri Abhyankar 	/* U part */
50916cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
51016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
51116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
51216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51316cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
51416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51516cd7e1dSShri Abhyankar           } else {
51616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
51716cd7e1dSShri Abhyankar           }
51816cd7e1dSShri Abhyankar #else
51916cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
52016cd7e1dSShri Abhyankar #endif
52116cd7e1dSShri Abhyankar }
52216cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
52316cd7e1dSShri Abhyankar         }
52416cd7e1dSShri Abhyankar     } else {
52517ab2063SBarry Smith       for (i=0; i<m; i++) {
52677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
527416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
528aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
530a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
53136db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
532a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5333a40ed3dSBarry Smith           } else {
534a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
53517ab2063SBarry Smith           }
53617ab2063SBarry Smith #else
537a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
53817ab2063SBarry Smith #endif
53917ab2063SBarry Smith         }
540b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
54117ab2063SBarry Smith       }
54216cd7e1dSShri Abhyankar     }
543b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
54417ab2063SBarry Smith   }
545b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5463a40ed3dSBarry Smith   PetscFunctionReturn(0);
547416022c9SBarry Smith }
548416022c9SBarry Smith 
5494a2ae208SSatish Balay #undef __FUNCT__
5504a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
551dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
552416022c9SBarry Smith {
553480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
554416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
555dfbe8321SBarry Smith   PetscErrorCode    ierr;
556d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
55736db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
558b0a32e0cSBarry Smith   PetscViewer       viewer;
559f3ef73ceSBarry Smith   PetscViewerFormat format;
560cddf8d76SBarry Smith 
5613a40ed3dSBarry Smith   PetscFunctionBegin;
562480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
563b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
56419bcc07fSBarry Smith 
565b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
566416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5670513a670SBarry Smith 
568fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5690513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
570b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
571416022c9SBarry Smith     for (i=0; i<m; i++) {
572cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
573bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
574bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
575aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
57636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
577cddf8d76SBarry Smith #else
578cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
579cddf8d76SBarry Smith #endif
580b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
581cddf8d76SBarry Smith       }
582cddf8d76SBarry Smith     }
583b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
584cddf8d76SBarry Smith     for (i=0; i<m; i++) {
585cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
586bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
587bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
588cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
589b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
590cddf8d76SBarry Smith       }
591cddf8d76SBarry Smith     }
592b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
593cddf8d76SBarry Smith     for (i=0; i<m; i++) {
594cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
595bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
596bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
597aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
59836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
599cddf8d76SBarry Smith #else
600cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
601cddf8d76SBarry Smith #endif
602b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
603416022c9SBarry Smith       }
604416022c9SBarry Smith     }
6050513a670SBarry Smith   } else {
6060513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
6070513a670SBarry Smith     /* first determine max of all nonzero values */
60897f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
609b0a32e0cSBarry Smith     PetscDraw   popup;
61036db0b34SBarry Smith     PetscReal scale;
6110513a670SBarry Smith 
6120513a670SBarry Smith     for (i=0; i<nz; i++) {
6130513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
6140513a670SBarry Smith     }
615b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
616b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
617b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
6180513a670SBarry Smith     count = 0;
6190513a670SBarry Smith     for (i=0; i<m; i++) {
6200513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
621bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
622bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
62397f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
624b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
6250513a670SBarry Smith         count++;
6260513a670SBarry Smith       }
6270513a670SBarry Smith     }
6280513a670SBarry Smith   }
629480ef9eaSBarry Smith   PetscFunctionReturn(0);
630480ef9eaSBarry Smith }
631cddf8d76SBarry Smith 
6324a2ae208SSatish Balay #undef __FUNCT__
6334a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
634dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
635480ef9eaSBarry Smith {
636dfbe8321SBarry Smith   PetscErrorCode ierr;
637b0a32e0cSBarry Smith   PetscDraw      draw;
63836db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
639480ef9eaSBarry Smith   PetscTruth     isnull;
640480ef9eaSBarry Smith 
641480ef9eaSBarry Smith   PetscFunctionBegin;
642b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
643b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
644480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
645480ef9eaSBarry Smith 
646480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
647d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
648480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
649b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
650b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
651480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6523a40ed3dSBarry Smith   PetscFunctionReturn(0);
653416022c9SBarry Smith }
654416022c9SBarry Smith 
6554a2ae208SSatish Balay #undef __FUNCT__
6564a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
657dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
658416022c9SBarry Smith {
659dfbe8321SBarry Smith   PetscErrorCode ierr;
6606805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
661416022c9SBarry Smith 
6623a40ed3dSBarry Smith   PetscFunctionBegin;
6632692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6642692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
6652692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
666c45a1595SBarry Smith   if (iascii) {
6673a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6680f5bd95cSBarry Smith   } else if (isbinary) {
6693a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6700f5bd95cSBarry Smith   } else if (isdraw) {
6713a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6725cd90555SBarry Smith   } else {
673e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
67417ab2063SBarry Smith   }
6754108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
6763a40ed3dSBarry Smith   PetscFunctionReturn(0);
67717ab2063SBarry Smith }
67819bcc07fSBarry Smith 
6794a2ae208SSatish Balay #undef __FUNCT__
6804a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
681dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
68217ab2063SBarry Smith {
683416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6846849ba73SBarry Smith   PetscErrorCode ierr;
68597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
686d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
68754f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6883447b6efSHong Zhang   PetscReal      ratio=0.6;
68917ab2063SBarry Smith 
6903a40ed3dSBarry Smith   PetscFunctionBegin;
6913a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
69217ab2063SBarry Smith 
69343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
69417ab2063SBarry Smith   for (i=1; i<m; i++) {
695416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
69617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
69794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
69817ab2063SBarry Smith     if (fshift) {
699bfeeae90SHong Zhang       ip = aj + ai[i] ;
700bfeeae90SHong Zhang       ap = aa + ai[i] ;
70117ab2063SBarry Smith       N  = ailen[i];
70217ab2063SBarry Smith       for (j=0; j<N; j++) {
70317ab2063SBarry Smith         ip[j-fshift] = ip[j];
70417ab2063SBarry Smith         ap[j-fshift] = ap[j];
70517ab2063SBarry Smith       }
70617ab2063SBarry Smith     }
70717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
70817ab2063SBarry Smith   }
70917ab2063SBarry Smith   if (m) {
71017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
71117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
71217ab2063SBarry Smith   }
71317ab2063SBarry Smith   /* reset ilen and imax for each row */
71417ab2063SBarry Smith   for (i=0; i<m; i++) {
71517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
71617ab2063SBarry Smith   }
717bfeeae90SHong Zhang   a->nz = ai[m];
71865e19b50SBarry Smith   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
71917ab2063SBarry Smith 
72009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
721d0f46423SBarry 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);
722ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
723ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
7248e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
725dd5f02e7SSatish Balay   a->reallocs          = 0;
7264e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
72736db0b34SBarry Smith   a->rmax              = rmax;
7284e220ebcSLois Curfman McInnes 
729cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
730317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
73188e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
73271c2f376SKris Buschelman 
7334108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
73471f1c65dSBarry Smith 
73571f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
7363a40ed3dSBarry Smith   PetscFunctionReturn(0);
73717ab2063SBarry Smith }
73817ab2063SBarry Smith 
7394a2ae208SSatish Balay #undef __FUNCT__
74099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
74199cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
74299cafbc1SBarry Smith {
74399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
74499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
74554f21887SBarry Smith   MatScalar      *aa = a->a;
74699cafbc1SBarry Smith 
74799cafbc1SBarry Smith   PetscFunctionBegin;
74899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
74999cafbc1SBarry Smith   PetscFunctionReturn(0);
75099cafbc1SBarry Smith }
75199cafbc1SBarry Smith 
75299cafbc1SBarry Smith #undef __FUNCT__
75399cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
75499cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
75599cafbc1SBarry Smith {
75699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
75799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
75854f21887SBarry Smith   MatScalar      *aa = a->a;
75999cafbc1SBarry Smith 
76099cafbc1SBarry Smith   PetscFunctionBegin;
76199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
76299cafbc1SBarry Smith   PetscFunctionReturn(0);
76399cafbc1SBarry Smith }
76499cafbc1SBarry Smith 
76599cafbc1SBarry Smith #undef __FUNCT__
7664a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
767dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
76817ab2063SBarry Smith {
769416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
770dfbe8321SBarry Smith   PetscErrorCode ierr;
7713a40ed3dSBarry Smith 
7723a40ed3dSBarry Smith   PetscFunctionBegin;
773d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7743a40ed3dSBarry Smith   PetscFunctionReturn(0);
77517ab2063SBarry Smith }
776416022c9SBarry Smith 
7774a2ae208SSatish Balay #undef __FUNCT__
7784a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
779dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
78017ab2063SBarry Smith {
781416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
782dfbe8321SBarry Smith   PetscErrorCode ierr;
783d5d45c9bSBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785aa482453SBarry Smith #if defined(PETSC_USE_LOG)
786d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
78717ab2063SBarry Smith #endif
788e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
789c38d4ed2SBarry Smith   if (a->row) {
790c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
791c38d4ed2SBarry Smith   }
792c38d4ed2SBarry Smith   if (a->col) {
793c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
794c38d4ed2SBarry Smith   }
79505b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
79605b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
79771f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
79805b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
79982bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
80005b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
801cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
80205b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
803407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
8040e83c824SBarry Smith   if (a->compressedrow.checked && a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);}
805a30b2313SHong Zhang 
8064108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
8074846f1f5SKris Buschelman 
808606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
809901853e0SKris Buschelman 
810dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
811901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
812901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
813901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
814901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
815901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
816ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
817901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
818901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
819a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
820901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
8213a40ed3dSBarry Smith   PetscFunctionReturn(0);
82217ab2063SBarry Smith }
82317ab2063SBarry Smith 
8244a2ae208SSatish Balay #undef __FUNCT__
8254a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
8264e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
82717ab2063SBarry Smith {
828416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8294846f1f5SKris Buschelman   PetscErrorCode ierr;
8303a40ed3dSBarry Smith 
8313a40ed3dSBarry Smith   PetscFunctionBegin;
832a65d3064SKris Buschelman   switch (op) {
833a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
8344e0d8c25SBarry Smith       a->roworiented       = flg;
835a65d3064SKris Buschelman       break;
836a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
837a9817697SBarry Smith       a->keepnonzeropattern    = flg;
838a65d3064SKris Buschelman       break;
839512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
840512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
841a65d3064SKris Buschelman       break;
842a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
8434e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
844a65d3064SKris Buschelman       break;
845a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8464e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
847a65d3064SKris Buschelman       break;
84828b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
84928b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
85028b2fa4aSMatthew Knepley       break;
851a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8524e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8530df259c2SBarry Smith       break;
854d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8554e0d8c25SBarry Smith       a->compressedrow.use = flg;
856d487561eSHong Zhang       break;
857b1646e73SJed Brown     case MAT_SYMMETRIC:
858b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
859b1646e73SJed Brown     case MAT_HERMITIAN:
860b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
8614e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
862a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
863a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
864290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
865a65d3064SKris Buschelman       break;
866b87ac2d8SJed Brown     case MAT_USE_INODES:
867b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
868b87ac2d8SJed Brown       break;
869a65d3064SKris Buschelman     default:
870e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
871a65d3064SKris Buschelman   }
8724108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
8733a40ed3dSBarry Smith   PetscFunctionReturn(0);
87417ab2063SBarry Smith }
87517ab2063SBarry Smith 
8764a2ae208SSatish Balay #undef __FUNCT__
8774a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
878dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
87917ab2063SBarry Smith {
880416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8816849ba73SBarry Smith   PetscErrorCode ierr;
882d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
88335e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
88417ab2063SBarry Smith 
8853a40ed3dSBarry Smith   PetscFunctionBegin;
886d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
887e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
88835e7444dSHong Zhang 
889d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
890d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
89135e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
89235e7444dSHong Zhang     for (i=0; i<n; i++) x[i] = aa[diag[i]];
89335e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
89435e7444dSHong Zhang     PetscFunctionReturn(0);
89535e7444dSHong Zhang   }
89635e7444dSHong Zhang 
8972dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8981ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
89935e7444dSHong Zhang   for (i=0; i<n; i++) {
90035e7444dSHong Zhang     nz = ai[i+1] - ai[i];
9012f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
90235e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
90335e7444dSHong Zhang       if (aj[j] == i) {
90435e7444dSHong Zhang         x[i] = aa[j];
90517ab2063SBarry Smith         break;
90617ab2063SBarry Smith       }
90717ab2063SBarry Smith     }
90817ab2063SBarry Smith   }
9091ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
9103a40ed3dSBarry Smith   PetscFunctionReturn(0);
91117ab2063SBarry Smith }
91217ab2063SBarry Smith 
913b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9144a2ae208SSatish Balay #undef __FUNCT__
9154a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
916dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
91717ab2063SBarry Smith {
918416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
9195c897100SBarry Smith   PetscScalar       *x,*y;
920dfbe8321SBarry Smith   PetscErrorCode    ierr;
921d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
9225c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
923a77337e4SBarry Smith   MatScalar         *v;
924a77337e4SBarry Smith   PetscScalar       alpha;
92504fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
9263447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
9274eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
9285c897100SBarry Smith #endif
92917ab2063SBarry Smith 
9303a40ed3dSBarry Smith   PetscFunctionBegin;
9312e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
9321ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9331ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9345c897100SBarry Smith 
9355c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
936bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
9375c897100SBarry Smith #else
9383447b6efSHong Zhang   if (usecprow){
9393447b6efSHong Zhang     m    = cprow.nrows;
9403447b6efSHong Zhang     ii   = cprow.i;
9417b2bb3b9SHong Zhang     ridx = cprow.rindex;
9423447b6efSHong Zhang   } else {
9433447b6efSHong Zhang     ii = a->i;
9443447b6efSHong Zhang   }
94517ab2063SBarry Smith   for (i=0; i<m; i++) {
9463447b6efSHong Zhang     idx   = a->j + ii[i] ;
9473447b6efSHong Zhang     v     = a->a + ii[i] ;
9483447b6efSHong Zhang     n     = ii[i+1] - ii[i];
9493447b6efSHong Zhang     if (usecprow){
9507b2bb3b9SHong Zhang       alpha = x[ridx[i]];
9513447b6efSHong Zhang     } else {
95217ab2063SBarry Smith       alpha = x[i];
9533447b6efSHong Zhang     }
95404fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
95517ab2063SBarry Smith   }
9565c897100SBarry Smith #endif
957dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
9581ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9591ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9603a40ed3dSBarry Smith   PetscFunctionReturn(0);
96117ab2063SBarry Smith }
96217ab2063SBarry Smith 
9634a2ae208SSatish Balay #undef __FUNCT__
9645c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
965dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9665c897100SBarry Smith {
967dfbe8321SBarry Smith   PetscErrorCode ierr;
9685c897100SBarry Smith 
9695c897100SBarry Smith   PetscFunctionBegin;
970170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9715c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9725c897100SBarry Smith   PetscFunctionReturn(0);
9735c897100SBarry Smith }
9745c897100SBarry Smith 
975a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9765c897100SBarry Smith #undef __FUNCT__
9774a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
978dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
97917ab2063SBarry Smith {
980416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
981d9fead3dSBarry Smith   PetscScalar       *y;
98254f21887SBarry Smith   const PetscScalar *x;
98354f21887SBarry Smith   const MatScalar   *aa;
984dfbe8321SBarry Smith   PetscErrorCode    ierr;
985003131ecSBarry Smith   PetscInt          m=A->rmap->n;
986003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9878aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
988362ced78SSatish Balay   PetscScalar       sum;
98997952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
99017ab2063SBarry Smith 
991b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
99297952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
993fee21e36SBarry Smith #endif
994fee21e36SBarry Smith 
9953a40ed3dSBarry Smith   PetscFunctionBegin;
996d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9971ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
99897952fefSHong Zhang   aj  = a->j;
99997952fefSHong Zhang   aa  = a->a;
1000416022c9SBarry Smith   ii  = a->i;
10014eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
100297952fefSHong Zhang     m    = a->compressedrow.nrows;
100397952fefSHong Zhang     ii   = a->compressedrow.i;
100497952fefSHong Zhang     ridx = a->compressedrow.rindex;
100597952fefSHong Zhang     for (i=0; i<m; i++){
100697952fefSHong Zhang       n   = ii[i+1] - ii[i];
100797952fefSHong Zhang       aj  = a->j + ii[i];
100897952fefSHong Zhang       aa  = a->a + ii[i];
100997952fefSHong Zhang       sum = 0.0;
1010a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1011003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1012003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
101397952fefSHong Zhang       y[*ridx++] = sum;
101497952fefSHong Zhang     }
101597952fefSHong Zhang   } else { /* do not use compressed row format */
1016b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1017b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1018b05257ddSBarry Smith #else
101917ab2063SBarry Smith     for (i=0; i<m; i++) {
1020003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1021003131ecSBarry Smith       aj  = a->j + ii[i];
1022003131ecSBarry Smith       aa  = a->a + ii[i];
102317ab2063SBarry Smith       sum  = 0.0;
1024a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1025003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
102617ab2063SBarry Smith       y[i] = sum;
102717ab2063SBarry Smith     }
10288d195f9aSBarry Smith #endif
1029b05257ddSBarry Smith   }
1030dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1031d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
10321ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10333a40ed3dSBarry Smith   PetscFunctionReturn(0);
103417ab2063SBarry Smith }
103517ab2063SBarry Smith 
1036a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
10374a2ae208SSatish Balay #undef __FUNCT__
10384a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1039dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
104017ab2063SBarry Smith {
1041416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
104254f21887SBarry Smith   PetscScalar     *x,*y,*z;
104354f21887SBarry Smith   const MatScalar *aa;
1044dfbe8321SBarry Smith   PetscErrorCode  ierr;
1045d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1046aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
104797952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1048362ced78SSatish Balay   PetscScalar     sum;
104997952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
1050e36a17ebSSatish Balay #endif
10519ea0dfa2SSatish Balay 
10523a40ed3dSBarry Smith   PetscFunctionBegin;
10531ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10541ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10552e8a6d31SBarry Smith   if (zz != yy) {
10561ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
10572e8a6d31SBarry Smith   } else {
10582e8a6d31SBarry Smith     z = y;
10592e8a6d31SBarry Smith   }
1060bfeeae90SHong Zhang 
106197952fefSHong Zhang   aj  = a->j;
106297952fefSHong Zhang   aa  = a->a;
1063cddf8d76SBarry Smith   ii  = a->i;
1064aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
106597952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
106602ab625aSSatish Balay #else
10674eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10684eb6d288SHong Zhang     if (zz != yy){
10694eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10704eb6d288SHong Zhang     }
107197952fefSHong Zhang     m    = a->compressedrow.nrows;
107297952fefSHong Zhang     ii   = a->compressedrow.i;
107397952fefSHong Zhang     ridx = a->compressedrow.rindex;
107497952fefSHong Zhang     for (i=0; i<m; i++){
107597952fefSHong Zhang       n  = ii[i+1] - ii[i];
107697952fefSHong Zhang       aj  = a->j + ii[i];
107797952fefSHong Zhang       aa  = a->a + ii[i];
107897952fefSHong Zhang       sum = y[*ridx];
107997952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
108097952fefSHong Zhang       z[*ridx++] = sum;
108197952fefSHong Zhang     }
108297952fefSHong Zhang   } else { /* do not use compressed row format */
108317ab2063SBarry Smith     for (i=0; i<m; i++) {
10849ea0dfa2SSatish Balay       jrow = ii[i];
10859ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
108617ab2063SBarry Smith       sum  = y[i];
10879ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
108897952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10899ea0dfa2SSatish Balay       }
109017ab2063SBarry Smith       z[i] = sum;
109117ab2063SBarry Smith     }
109297952fefSHong Zhang   }
109302ab625aSSatish Balay #endif
1094dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10951ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10961ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10972e8a6d31SBarry Smith   if (zz != yy) {
10981ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10992e8a6d31SBarry Smith   }
11003a40ed3dSBarry Smith   PetscFunctionReturn(0);
110117ab2063SBarry Smith }
110217ab2063SBarry Smith 
110317ab2063SBarry Smith /*
110417ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
110517ab2063SBarry Smith */
11064a2ae208SSatish Balay #undef __FUNCT__
11074a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1108dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
110917ab2063SBarry Smith {
1110416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11116849ba73SBarry Smith   PetscErrorCode ierr;
1112d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
111317ab2063SBarry Smith 
11143a40ed3dSBarry Smith   PetscFunctionBegin;
111509f38230SBarry Smith   if (!a->diag) {
111609f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
11179518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
111809f38230SBarry Smith   }
1119d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
112009f38230SBarry Smith     a->diag[i] = a->i[i+1];
1121bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1122bfeeae90SHong Zhang       if (a->j[j] == i) {
112309f38230SBarry Smith         a->diag[i] = j;
112417ab2063SBarry Smith         break;
112517ab2063SBarry Smith       }
112617ab2063SBarry Smith     }
112717ab2063SBarry Smith   }
11283a40ed3dSBarry Smith   PetscFunctionReturn(0);
112917ab2063SBarry Smith }
113017ab2063SBarry Smith 
1131be5855fcSBarry Smith /*
1132be5855fcSBarry Smith      Checks for missing diagonals
1133be5855fcSBarry Smith */
11344a2ae208SSatish Balay #undef __FUNCT__
11354a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
113609f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1137be5855fcSBarry Smith {
1138be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
113997f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1140be5855fcSBarry Smith 
1141be5855fcSBarry Smith   PetscFunctionBegin;
114209f38230SBarry Smith   *missing = PETSC_FALSE;
1143d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
114409f38230SBarry Smith     *missing  = PETSC_TRUE;
114509f38230SBarry Smith     if (d) *d = 0;
114609f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
114709f38230SBarry Smith   } else {
1148f1e2ffcdSBarry Smith     diag = a->diag;
1149d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1150bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
115109f38230SBarry Smith 	*missing = PETSC_TRUE;
115209f38230SBarry Smith 	if (d) *d = i;
115309f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
115409f38230SBarry Smith       }
1155be5855fcSBarry Smith     }
1156be5855fcSBarry Smith   }
1157be5855fcSBarry Smith   PetscFunctionReturn(0);
1158be5855fcSBarry Smith }
1159be5855fcSBarry Smith 
116071f1c65dSBarry Smith EXTERN_C_BEGIN
116171f1c65dSBarry Smith #undef __FUNCT__
116271f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
116371f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
116471f1c65dSBarry Smith {
116571f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
116671f1c65dSBarry Smith   PetscErrorCode ierr;
1167d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
116854f21887SBarry Smith   MatScalar      *v = a->a;
116954f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
117071f1c65dSBarry Smith 
117171f1c65dSBarry Smith   PetscFunctionBegin;
117271f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
117371f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
117471f1c65dSBarry Smith   diag = a->diag;
117571f1c65dSBarry Smith   if (!a->idiag) {
117671f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
117771f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
117871f1c65dSBarry Smith     v        = a->a;
117971f1c65dSBarry Smith   }
118071f1c65dSBarry Smith   mdiag = a->mdiag;
118171f1c65dSBarry Smith   idiag = a->idiag;
118271f1c65dSBarry Smith 
1183028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
118471f1c65dSBarry Smith     for (i=0; i<m; i++) {
118571f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1186e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
118771f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
118871f1c65dSBarry Smith     }
118971f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
119071f1c65dSBarry Smith   } else {
119171f1c65dSBarry Smith     for (i=0; i<m; i++) {
119271f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
119371f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
119471f1c65dSBarry Smith     }
1195dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
119671f1c65dSBarry Smith   }
119771f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
119871f1c65dSBarry Smith   PetscFunctionReturn(0);
119971f1c65dSBarry Smith }
12005a9745a3SMatthew Knepley EXTERN_C_END
120171f1c65dSBarry Smith 
1202a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
12034a2ae208SSatish Balay #undef __FUNCT__
120441f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
120541f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
120617ab2063SBarry Smith {
1207416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1208e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1209e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
121054f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1211dfbe8321SBarry Smith   PetscErrorCode     ierr;
1212d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
121397f1f81fSBarry Smith   const PetscInt     *idx,*diag;
121417ab2063SBarry Smith 
12153a40ed3dSBarry Smith   PetscFunctionBegin;
1216b965ef7fSBarry Smith   its = its*lits;
121791723122SBarry Smith 
121871f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
121971f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
122071f1c65dSBarry Smith   a->fshift = fshift;
122171f1c65dSBarry Smith   a->omega  = omega;
1222ed480e8bSBarry Smith 
122371f1c65dSBarry Smith   diag = a->diag;
122471f1c65dSBarry Smith   t     = a->ssor_work;
1225ed480e8bSBarry Smith   idiag = a->idiag;
122671f1c65dSBarry Smith   mdiag = a->mdiag;
1227ed480e8bSBarry Smith 
12281ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1229fb2e594dSBarry Smith   if (xx != bb) {
12301ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1231fb2e594dSBarry Smith   } else {
1232fb2e594dSBarry Smith     b = x;
1233fb2e594dSBarry Smith   }
123471f1c65dSBarry Smith   CHKMEMQ;
1235ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
123617ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
123717ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1238ed480e8bSBarry Smith     bs = b;
123917ab2063SBarry Smith     for (i=0; i<m; i++) {
124071f1c65dSBarry Smith         d    = fshift + mdiag[i];
1241416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1242ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1243ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
124417ab2063SBarry Smith         sum  = b[i]*d/omega;
1245003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
124617ab2063SBarry Smith         x[i] = sum;
124717ab2063SBarry Smith     }
12481ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12491ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1250efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12513a40ed3dSBarry Smith     PetscFunctionReturn(0);
125217ab2063SBarry Smith   }
1253c783ea89SBarry Smith 
125448af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1255e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
12563a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
125717ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1258887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
125917ab2063SBarry Smith 
126017ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
126117ab2063SBarry Smith 
1262887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
126317ab2063SBarry Smith     */
126417ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
126517ab2063SBarry Smith 
126617ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
126717ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1268416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1269ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1270ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
127117ab2063SBarry Smith       sum  = b[i];
1272e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1273ed480e8bSBarry Smith       x[i] = sum*idiag[i];
127417ab2063SBarry Smith     }
127517ab2063SBarry Smith 
127617ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1277416022c9SBarry Smith     v = a->a;
1278ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
127917ab2063SBarry Smith 
128017ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1281ed480e8bSBarry Smith     ts = t;
1282416022c9SBarry Smith     diag = a->diag;
128317ab2063SBarry Smith     for (i=0; i<m; i++) {
1284416022c9SBarry Smith       n    = diag[i] - a->i[i];
1285ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1286ed480e8bSBarry Smith       v    = a->a + a->i[i];
128717ab2063SBarry Smith       sum  = t[i];
1288003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1289ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1290733d66baSBarry Smith       /*  x = x + t */
1291733d66baSBarry Smith       x[i] += t[i];
129217ab2063SBarry Smith     }
129317ab2063SBarry Smith 
1294dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
12951ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12961ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12973a40ed3dSBarry Smith     PetscFunctionReturn(0);
129817ab2063SBarry Smith   }
129917ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
130017ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
130117ab2063SBarry Smith       for (i=0; i<m; i++) {
1302416022c9SBarry Smith         n    = diag[i] - a->i[i];
1303ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1304ed480e8bSBarry Smith         v    = a->a + a->i[i];
130517ab2063SBarry Smith         sum  = b[i];
1306e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13075c99c7daSBarry Smith         t[i] = sum;
1308ed480e8bSBarry Smith         x[i] = sum*idiag[i];
130917ab2063SBarry Smith       }
13105c99c7daSBarry Smith       xb = t;
1311efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13123a40ed3dSBarry Smith     } else xb = b;
131317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
131417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1315416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1316ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1317ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
131817ab2063SBarry Smith         sum  = xb[i];
1319e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13205c99c7daSBarry Smith         if (xb == b) {
1321ed480e8bSBarry Smith           x[i] = sum*idiag[i];
13225c99c7daSBarry Smith         } else {
13235c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
132417ab2063SBarry Smith         }
13255c99c7daSBarry Smith       }
1326efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
132717ab2063SBarry Smith     }
132817ab2063SBarry Smith     its--;
132917ab2063SBarry Smith   }
133017ab2063SBarry Smith   while (its--) {
133117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
133217ab2063SBarry Smith       for (i=0; i<m; i++) {
1333416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1334ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1335ed480e8bSBarry Smith         v    = a->a + a->i[i];
133617ab2063SBarry Smith         sum  = b[i];
1337e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1338ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
133917ab2063SBarry Smith       }
13409f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
134117ab2063SBarry Smith     }
134217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
134317ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1344416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1345ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1346ed480e8bSBarry Smith         v    = a->a + a->i[i];
134717ab2063SBarry Smith         sum  = b[i];
1348e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1349ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
135017ab2063SBarry Smith       }
13519f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
135217ab2063SBarry Smith     }
135317ab2063SBarry Smith   }
13541ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13551ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
135671f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
135717ab2063SBarry Smith }
135817ab2063SBarry Smith 
13592af78befSBarry Smith 
13604a2ae208SSatish Balay #undef __FUNCT__
13614a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1362dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
136317ab2063SBarry Smith {
1364416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13654e220ebcSLois Curfman McInnes 
13663a40ed3dSBarry Smith   PetscFunctionBegin;
13674e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13684e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13694e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13704e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13714e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13728e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
13737adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1374d5f3da31SBarry Smith   if (A->factortype) {
13754e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13764e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13774e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13784e220ebcSLois Curfman McInnes   } else {
13794e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13804e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13814e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13824e220ebcSLois Curfman McInnes   }
13833a40ed3dSBarry Smith   PetscFunctionReturn(0);
138417ab2063SBarry Smith }
138517ab2063SBarry Smith 
13864a2ae208SSatish Balay #undef __FUNCT__
13874a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1388f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
138917ab2063SBarry Smith {
1390416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
13913b98c0a2SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d = 0;
13926849ba73SBarry Smith   PetscErrorCode ierr;
139309f38230SBarry Smith   PetscTruth     missing;
139417ab2063SBarry Smith 
13953a40ed3dSBarry Smith   PetscFunctionBegin;
1396a9817697SBarry Smith   if (a->keepnonzeropattern) {
1397f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1398e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1399bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1400f1e2ffcdSBarry Smith     }
1401f4df32b1SMatthew Knepley     if (diag != 0.0) {
140209f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1403e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1404f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1405f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1406f1e2ffcdSBarry Smith       }
1407f1e2ffcdSBarry Smith     }
140888e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1409f1e2ffcdSBarry Smith   } else {
1410f4df32b1SMatthew Knepley     if (diag != 0.0) {
141117ab2063SBarry Smith       for (i=0; i<N; i++) {
1412e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
14137ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1414416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1415f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1416bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
14177ae801bdSBarry Smith         } else { /* in case row was completely empty */
1418f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
141917ab2063SBarry Smith         }
142017ab2063SBarry Smith       }
14213a40ed3dSBarry Smith     } else {
142217ab2063SBarry Smith       for (i=0; i<N; i++) {
1423e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1424416022c9SBarry Smith         a->ilen[rows[i]] = 0;
142517ab2063SBarry Smith       }
142617ab2063SBarry Smith     }
142788e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1428f1e2ffcdSBarry Smith   }
142943a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14303a40ed3dSBarry Smith   PetscFunctionReturn(0);
143117ab2063SBarry Smith }
143217ab2063SBarry Smith 
14334a2ae208SSatish Balay #undef __FUNCT__
14344a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1435a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
143617ab2063SBarry Smith {
1437416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
143897f1f81fSBarry Smith   PetscInt   *itmp;
143917ab2063SBarry Smith 
14403a40ed3dSBarry Smith   PetscFunctionBegin;
1441e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
144217ab2063SBarry Smith 
1443416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1444bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
144517ab2063SBarry Smith   if (idx) {
1446bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1447bfeeae90SHong Zhang     if (*nz) {
14484e093b46SBarry Smith       *idx = itmp;
144917ab2063SBarry Smith     }
145017ab2063SBarry Smith     else *idx = 0;
145117ab2063SBarry Smith   }
14523a40ed3dSBarry Smith   PetscFunctionReturn(0);
145317ab2063SBarry Smith }
145417ab2063SBarry Smith 
1455bfeeae90SHong Zhang /* remove this function? */
14564a2ae208SSatish Balay #undef __FUNCT__
14574a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1458a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
145917ab2063SBarry Smith {
14603a40ed3dSBarry Smith   PetscFunctionBegin;
14613a40ed3dSBarry Smith   PetscFunctionReturn(0);
146217ab2063SBarry Smith }
146317ab2063SBarry Smith 
14644a2ae208SSatish Balay #undef __FUNCT__
14654a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1466dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
146717ab2063SBarry Smith {
1468416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
146954f21887SBarry Smith   MatScalar      *v = a->a;
147036db0b34SBarry Smith   PetscReal      sum = 0.0;
14716849ba73SBarry Smith   PetscErrorCode ierr;
147297f1f81fSBarry Smith   PetscInt       i,j;
147317ab2063SBarry Smith 
14743a40ed3dSBarry Smith   PetscFunctionBegin;
147517ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1476416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1477aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
147836db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
147917ab2063SBarry Smith #else
148017ab2063SBarry Smith       sum += (*v)*(*v); v++;
148117ab2063SBarry Smith #endif
148217ab2063SBarry Smith     }
1483064f8208SBarry Smith     *nrm = sqrt(sum);
14843a40ed3dSBarry Smith   } else if (type == NORM_1) {
148536db0b34SBarry Smith     PetscReal *tmp;
148697f1f81fSBarry Smith     PetscInt    *jj = a->j;
1487d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1488d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1489064f8208SBarry Smith     *nrm = 0.0;
1490416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1491bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
149217ab2063SBarry Smith     }
1493d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1494064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
149517ab2063SBarry Smith     }
1496606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14973a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1498064f8208SBarry Smith     *nrm = 0.0;
1499d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1500bfeeae90SHong Zhang       v = a->a + a->i[j];
150117ab2063SBarry Smith       sum = 0.0;
1502416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1503cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
150417ab2063SBarry Smith       }
1505064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
150617ab2063SBarry Smith     }
15073a40ed3dSBarry Smith   } else {
1508e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
150917ab2063SBarry Smith   }
15103a40ed3dSBarry Smith   PetscFunctionReturn(0);
151117ab2063SBarry Smith }
151217ab2063SBarry Smith 
15134a2ae208SSatish Balay #undef __FUNCT__
15144a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1515fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
151617ab2063SBarry Smith {
1517416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1518416022c9SBarry Smith   Mat            C;
15196849ba73SBarry Smith   PetscErrorCode ierr;
1520d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
152154f21887SBarry Smith   MatScalar      *array = a->a;
152217ab2063SBarry Smith 
15233a40ed3dSBarry Smith   PetscFunctionBegin;
1524e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1525fc4dec0aSBarry Smith 
1526fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1527d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1528d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1529bfeeae90SHong Zhang 
1530bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
15317adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1532d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
15337adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1534ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1535606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1536a541d17aSBarry Smith   } else {
1537a541d17aSBarry Smith     C = *B;
1538a541d17aSBarry Smith   }
1539a541d17aSBarry Smith 
154017ab2063SBarry Smith   for (i=0; i<m; i++) {
154117ab2063SBarry Smith     len    = ai[i+1]-ai[i];
154287d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1543b9b97703SBarry Smith     array += len;
1544b9b97703SBarry Smith     aj    += len;
154517ab2063SBarry Smith   }
15466d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15476d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
154817ab2063SBarry Smith 
1549815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1550416022c9SBarry Smith     *B = C;
155117ab2063SBarry Smith   } else {
1552273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
155317ab2063SBarry Smith   }
15543a40ed3dSBarry Smith   PetscFunctionReturn(0);
155517ab2063SBarry Smith }
155617ab2063SBarry Smith 
1557cd0d46ebSvictorle EXTERN_C_BEGIN
1558cd0d46ebSvictorle #undef __FUNCT__
15595fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1560be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1561cd0d46ebSvictorle {
1562cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
156354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
156454f21887SBarry Smith   MatScalar      *va,*vb;
15656849ba73SBarry Smith   PetscErrorCode ierr;
156697f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1567cd0d46ebSvictorle 
1568cd0d46ebSvictorle   PetscFunctionBegin;
1569cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1570cd0d46ebSvictorle 
1571cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1572cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15735485867bSBarry Smith   if (ma!=nb || na!=mb){
15745485867bSBarry Smith     *f = PETSC_FALSE;
15755485867bSBarry Smith     PetscFunctionReturn(0);
15765485867bSBarry Smith   }
1577cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1578cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1579cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
158097f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
158197f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1582cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1583cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1584cd0d46ebSvictorle 
1585cd0d46ebSvictorle   *f = PETSC_TRUE;
1586cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1587cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
158897f1f81fSBarry Smith       PetscInt         idc,idr;
15895485867bSBarry Smith       PetscScalar vc,vr;
1590cd0d46ebSvictorle       /* column/row index/value */
15915485867bSBarry Smith       idc = adx[aptr[i]];
15925485867bSBarry Smith       idr = bdx[bptr[idc]];
15935485867bSBarry Smith       vc  = va[aptr[i]];
15945485867bSBarry Smith       vr  = vb[bptr[idc]];
15955485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15965485867bSBarry Smith 	*f = PETSC_FALSE;
15975485867bSBarry Smith         goto done;
1598cd0d46ebSvictorle       } else {
15995485867bSBarry Smith 	aptr[i]++;
16005485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1601cd0d46ebSvictorle       }
1602cd0d46ebSvictorle     }
1603cd0d46ebSvictorle   }
1604cd0d46ebSvictorle  done:
1605cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
16063aeef889SHong Zhang   if (B) {
16073aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
16083aeef889SHong Zhang   }
1609cd0d46ebSvictorle   PetscFunctionReturn(0);
1610cd0d46ebSvictorle }
1611cd0d46ebSvictorle EXTERN_C_END
1612cd0d46ebSvictorle 
16131cbb95d3SBarry Smith EXTERN_C_BEGIN
16141cbb95d3SBarry Smith #undef __FUNCT__
16151cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
16161cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
16171cbb95d3SBarry Smith {
16181cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
161954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
162054f21887SBarry Smith   MatScalar      *va,*vb;
16211cbb95d3SBarry Smith   PetscErrorCode ierr;
16221cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
16231cbb95d3SBarry Smith 
16241cbb95d3SBarry Smith   PetscFunctionBegin;
16251cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
16261cbb95d3SBarry Smith 
16271cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
16281cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
16291cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
16301cbb95d3SBarry Smith     *f = PETSC_FALSE;
16311cbb95d3SBarry Smith     PetscFunctionReturn(0);
16321cbb95d3SBarry Smith   }
16331cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
16341cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
16351cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
16361cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
16371cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
16381cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
16391cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
16401cbb95d3SBarry Smith 
16411cbb95d3SBarry Smith   *f = PETSC_TRUE;
16421cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
16431cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
16441cbb95d3SBarry Smith       PetscInt         idc,idr;
16451cbb95d3SBarry Smith       PetscScalar vc,vr;
16461cbb95d3SBarry Smith       /* column/row index/value */
16471cbb95d3SBarry Smith       idc = adx[aptr[i]];
16481cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16491cbb95d3SBarry Smith       vc  = va[aptr[i]];
16501cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16511cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16521cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16531cbb95d3SBarry Smith         goto done;
16541cbb95d3SBarry Smith       } else {
16551cbb95d3SBarry Smith 	aptr[i]++;
16561cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16571cbb95d3SBarry Smith       }
16581cbb95d3SBarry Smith     }
16591cbb95d3SBarry Smith   }
16601cbb95d3SBarry Smith  done:
16611cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16621cbb95d3SBarry Smith   if (B) {
16631cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16641cbb95d3SBarry Smith   }
16651cbb95d3SBarry Smith   PetscFunctionReturn(0);
16661cbb95d3SBarry Smith }
16671cbb95d3SBarry Smith EXTERN_C_END
16681cbb95d3SBarry Smith 
16699e29f15eSvictorle #undef __FUNCT__
16709e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1671dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16729e29f15eSvictorle {
1673dfbe8321SBarry Smith   PetscErrorCode ierr;
16749e29f15eSvictorle   PetscFunctionBegin;
16755485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16769e29f15eSvictorle   PetscFunctionReturn(0);
16779e29f15eSvictorle }
16789e29f15eSvictorle 
16794a2ae208SSatish Balay #undef __FUNCT__
16801cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16811cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16821cbb95d3SBarry Smith {
16831cbb95d3SBarry Smith   PetscErrorCode ierr;
16841cbb95d3SBarry Smith   PetscFunctionBegin;
16851cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16861cbb95d3SBarry Smith   PetscFunctionReturn(0);
16871cbb95d3SBarry Smith }
16881cbb95d3SBarry Smith 
16891cbb95d3SBarry Smith #undef __FUNCT__
16904a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1691dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
169217ab2063SBarry Smith {
1693416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
169454f21887SBarry Smith   PetscScalar    *l,*r,x;
169554f21887SBarry Smith   MatScalar      *v;
1696dfbe8321SBarry Smith   PetscErrorCode ierr;
1697d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
169817ab2063SBarry Smith 
16993a40ed3dSBarry Smith   PetscFunctionBegin;
170017ab2063SBarry Smith   if (ll) {
17013ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
17023ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1703e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1704e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
17051ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1706416022c9SBarry Smith     v = a->a;
170717ab2063SBarry Smith     for (i=0; i<m; i++) {
170817ab2063SBarry Smith       x = l[i];
1709416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
171017ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
171117ab2063SBarry Smith     }
17121ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1713efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
171417ab2063SBarry Smith   }
171517ab2063SBarry Smith   if (rr) {
1716e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1717e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
17181ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1719416022c9SBarry Smith     v = a->a; jj = a->j;
172017ab2063SBarry Smith     for (i=0; i<nz; i++) {
1721bfeeae90SHong Zhang       (*v++) *= r[*jj++];
172217ab2063SBarry Smith     }
17231ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1724efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
172517ab2063SBarry Smith   }
17263a40ed3dSBarry Smith   PetscFunctionReturn(0);
172717ab2063SBarry Smith }
172817ab2063SBarry Smith 
17294a2ae208SSatish Balay #undef __FUNCT__
17304a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
173197f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
173217ab2063SBarry Smith {
1733db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
17346849ba73SBarry Smith   PetscErrorCode ierr;
1735d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
173697f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
17375d0c19d7SBarry Smith   const PetscInt *irow,*icol;
17385d0c19d7SBarry Smith   PetscInt       nrows,ncols;
173997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
174054f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1741416022c9SBarry Smith   Mat            C;
174214ca34e6SBarry Smith   PetscTruth     stride,sorted;
174317ab2063SBarry Smith 
17443a40ed3dSBarry Smith   PetscFunctionBegin;
174514ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1746e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
174714ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1748e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
174999141d43SSatish Balay 
175017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1751b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1752b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
175317ab2063SBarry Smith 
1754fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1755fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1756fee21e36SBarry Smith   if (stride && step == 1) {
175702834360SBarry Smith     /* special case of contiguous rows */
17580e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
175902834360SBarry Smith     /* loop over new rows determining lens and starting points */
176002834360SBarry Smith     for (i=0; i<nrows; i++) {
1761bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1762a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
176302834360SBarry Smith       for (k=kstart; k<kend; k++) {
1764bfeeae90SHong Zhang         if (aj[k] >= first) {
176502834360SBarry Smith           starts[i] = k;
176602834360SBarry Smith           break;
176702834360SBarry Smith 	}
176802834360SBarry Smith       }
1769a2744918SBarry Smith       sum = 0;
177002834360SBarry Smith       while (k < kend) {
1771bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1772a2744918SBarry Smith         sum++;
177302834360SBarry Smith       }
1774a2744918SBarry Smith       lens[i] = sum;
177502834360SBarry Smith     }
177602834360SBarry Smith     /* create submatrix */
1777cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
177897f1f81fSBarry Smith       PetscInt n_cols,n_rows;
177908480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1780e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1781d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
178208480c60SBarry Smith       C = *B;
17833a40ed3dSBarry Smith     } else {
17847adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1785f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17867adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1787ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
178808480c60SBarry Smith     }
1789db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1790db02288aSLois Curfman McInnes 
179102834360SBarry Smith     /* loop over rows inserting into submatrix */
1792db02288aSLois Curfman McInnes     a_new    = c->a;
1793db02288aSLois Curfman McInnes     j_new    = c->j;
1794db02288aSLois Curfman McInnes     i_new    = c->i;
1795bfeeae90SHong Zhang 
179602834360SBarry Smith     for (i=0; i<nrows; i++) {
1797a2744918SBarry Smith       ii    = starts[i];
1798a2744918SBarry Smith       lensi = lens[i];
1799a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1800a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
180102834360SBarry Smith       }
180287828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1803a2744918SBarry Smith       a_new      += lensi;
1804a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1805a2744918SBarry Smith       c->ilen[i]  = lensi;
180602834360SBarry Smith     }
18070e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
18083a40ed3dSBarry Smith   } else {
180902834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
18100e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
181197f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
18120e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
181317ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
181402834360SBarry Smith     /* determine lens of each row */
181502834360SBarry Smith     for (i=0; i<nrows; i++) {
1816bfeeae90SHong Zhang       kstart  = ai[irow[i]];
181702834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
181802834360SBarry Smith       lens[i] = 0;
181902834360SBarry Smith       for (k=kstart; k<kend; k++) {
1820bfeeae90SHong Zhang         if (smap[aj[k]]) {
182102834360SBarry Smith           lens[i]++;
182202834360SBarry Smith         }
182302834360SBarry Smith       }
182402834360SBarry Smith     }
182517ab2063SBarry Smith     /* Create and fill new matrix */
1826a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
18270f5bd95cSBarry Smith       PetscTruth equal;
18280f5bd95cSBarry Smith 
182999141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1830e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1831d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
18320f5bd95cSBarry Smith       if (!equal) {
1833e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
183499141d43SSatish Balay       }
1835d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
183608480c60SBarry Smith       C = *B;
18373a40ed3dSBarry Smith     } else {
18387adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1839f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
18407adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1841ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
184208480c60SBarry Smith     }
184399141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
184417ab2063SBarry Smith     for (i=0; i<nrows; i++) {
184599141d43SSatish Balay       row    = irow[i];
1846bfeeae90SHong Zhang       kstart = ai[row];
184799141d43SSatish Balay       kend   = kstart + a->ilen[row];
1848bfeeae90SHong Zhang       mat_i  = c->i[i];
184999141d43SSatish Balay       mat_j  = c->j + mat_i;
185099141d43SSatish Balay       mat_a  = c->a + mat_i;
185199141d43SSatish Balay       mat_ilen = c->ilen + i;
185217ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1853bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1854ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
185599141d43SSatish Balay           *mat_a++ = a->a[k];
185699141d43SSatish Balay           (*mat_ilen)++;
185799141d43SSatish Balay 
185817ab2063SBarry Smith         }
185917ab2063SBarry Smith       }
186017ab2063SBarry Smith     }
186102834360SBarry Smith     /* Free work space */
186202834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1863606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1864606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
186502834360SBarry Smith   }
18666d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18676d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
186817ab2063SBarry Smith 
186917ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1870416022c9SBarry Smith   *B = C;
18713a40ed3dSBarry Smith   PetscFunctionReturn(0);
187217ab2063SBarry Smith }
187317ab2063SBarry Smith 
18741df811f5SHong Zhang #undef __FUNCT__
18754a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18760481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1877a871dcd8SBarry Smith {
187863b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1879dfbe8321SBarry Smith   PetscErrorCode ierr;
188063b91edcSBarry Smith   Mat            outA;
1881b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
188263b91edcSBarry Smith 
18833a40ed3dSBarry Smith   PetscFunctionBegin;
1884e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
18851df811f5SHong Zhang 
1886b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1887b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1888a871dcd8SBarry Smith 
188963b91edcSBarry Smith   outA              = inA;
1890d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
1891c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1892c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1893c3122656SLisandro Dalcin   a->row = row;
1894c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1895c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1896c3122656SLisandro Dalcin   a->col = col;
189763b91edcSBarry Smith 
189836db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1899b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
19004c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
190152e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1902f0ec6fceSSatish Balay 
190394a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1904d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1905d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
190694a9d846SBarry Smith   }
190763b91edcSBarry Smith 
1908f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1909137fb511SHong Zhang   if (row_identity && col_identity) {
1910ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
1911137fb511SHong Zhang   } else {
1912719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1913137fb511SHong Zhang   }
19143a40ed3dSBarry Smith   PetscFunctionReturn(0);
1915a871dcd8SBarry Smith }
1916a871dcd8SBarry Smith 
19174a2ae208SSatish Balay #undef __FUNCT__
19184a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1919f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1920f0b747eeSBarry Smith {
1921f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1922f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1923efee365bSSatish Balay   PetscErrorCode ierr;
19240805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
19253a40ed3dSBarry Smith 
19263a40ed3dSBarry Smith   PetscFunctionBegin;
1927f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1928efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
19293a40ed3dSBarry Smith   PetscFunctionReturn(0);
1930f0b747eeSBarry Smith }
1931f0b747eeSBarry Smith 
19324a2ae208SSatish Balay #undef __FUNCT__
19334a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
193497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1935cddf8d76SBarry Smith {
1936dfbe8321SBarry Smith   PetscErrorCode ierr;
193797f1f81fSBarry Smith   PetscInt       i;
1938cddf8d76SBarry Smith 
19393a40ed3dSBarry Smith   PetscFunctionBegin;
1940cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1941b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1942cddf8d76SBarry Smith   }
1943cddf8d76SBarry Smith 
1944cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19456a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1946cddf8d76SBarry Smith   }
19473a40ed3dSBarry Smith   PetscFunctionReturn(0);
1948cddf8d76SBarry Smith }
1949cddf8d76SBarry Smith 
19504a2ae208SSatish Balay #undef __FUNCT__
19514a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
195297f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19534dcbc457SBarry Smith {
1954e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19556849ba73SBarry Smith   PetscErrorCode ierr;
19565d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19575d0c19d7SBarry Smith   const PetscInt *idx;
195897f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1959f1af5d2fSBarry Smith   PetscBT        table;
1960bbd702dbSSatish Balay 
19613a40ed3dSBarry Smith   PetscFunctionBegin;
1962d0f46423SBarry Smith   m     = A->rmap->n;
1963e4d965acSSatish Balay   ai    = a->i;
1964bfeeae90SHong Zhang   aj    = a->j;
19658a047759SSatish Balay 
1966e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
196706763907SSatish Balay 
196897f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19696831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
197006763907SSatish Balay 
1971e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1972b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1973e4d965acSSatish Balay     isz  = 0;
19746831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1975e4d965acSSatish Balay 
1976e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19774dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1978b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1979e4d965acSSatish Balay 
1980dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1981e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1982f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19834dcbc457SBarry Smith     }
198406763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
198506763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1986e4d965acSSatish Balay 
198704a348a9SBarry Smith     k = 0;
198804a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
198904a348a9SBarry Smith       n = isz;
199006763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1991e4d965acSSatish Balay         row   = nidx[k];
1992e4d965acSSatish Balay         start = ai[row];
1993e4d965acSSatish Balay         end   = ai[row+1];
199404a348a9SBarry Smith         for (l = start; l<end ; l++){
1995efb16452SHong Zhang           val = aj[l] ;
1996f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1997e4d965acSSatish Balay         }
1998e4d965acSSatish Balay       }
1999e4d965acSSatish Balay     }
2000029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
2001e4d965acSSatish Balay   }
20026831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2003606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
20043a40ed3dSBarry Smith   PetscFunctionReturn(0);
20054dcbc457SBarry Smith }
200617ab2063SBarry Smith 
20070513a670SBarry Smith /* -------------------------------------------------------------- */
20084a2ae208SSatish Balay #undef __FUNCT__
20094a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2010dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
20110513a670SBarry Smith {
20120513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
20136849ba73SBarry Smith   PetscErrorCode ierr;
20143b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
20155d0c19d7SBarry Smith   const PetscInt *row,*col;
20165d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
201756cd22aeSBarry Smith   IS             icolp,irowp;
20183b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
20193b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
20200513a670SBarry Smith 
20213a40ed3dSBarry Smith   PetscFunctionBegin;
20224c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
202356cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
20244c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
202556cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
20260513a670SBarry Smith 
20270513a670SBarry Smith   /* determine lengths of permuted rows */
202897f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
20290513a670SBarry Smith   for (i=0; i<m; i++) {
20300513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
20310513a670SBarry Smith   }
20327adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2033f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
20347adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2035ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2036606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
20370513a670SBarry Smith 
203897f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
20390513a670SBarry Smith   for (i=0; i<m; i++) {
204032ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20410513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2042cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
204332ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20440513a670SBarry Smith   }
2045606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20463c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20470513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20480513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
204956cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
205056cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
205156cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
205256cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20533a40ed3dSBarry Smith   PetscFunctionReturn(0);
20540513a670SBarry Smith }
20550513a670SBarry Smith 
20564a2ae208SSatish Balay #undef __FUNCT__
20574a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2058dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2059cb5b572fSBarry Smith {
2060dfbe8321SBarry Smith   PetscErrorCode ierr;
2061cb5b572fSBarry Smith 
2062cb5b572fSBarry Smith   PetscFunctionBegin;
206333f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
206433f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2065be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2066be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2067be6bf707SBarry Smith 
2068d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2069e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2070cb5b572fSBarry Smith     }
2071d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2072cb5b572fSBarry Smith   } else {
2073cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2074cb5b572fSBarry Smith   }
2075cb5b572fSBarry Smith   PetscFunctionReturn(0);
2076cb5b572fSBarry Smith }
2077cb5b572fSBarry Smith 
20784a2ae208SSatish Balay #undef __FUNCT__
20794a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2080dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2081273d9f13SBarry Smith {
2082dfbe8321SBarry Smith   PetscErrorCode ierr;
2083273d9f13SBarry Smith 
2084273d9f13SBarry Smith   PetscFunctionBegin;
2085ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2086273d9f13SBarry Smith   PetscFunctionReturn(0);
2087273d9f13SBarry Smith }
2088273d9f13SBarry Smith 
20894a2ae208SSatish Balay #undef __FUNCT__
20904a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2091a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20926c0721eeSBarry Smith {
20936c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20946c0721eeSBarry Smith   PetscFunctionBegin;
20956c0721eeSBarry Smith   *array = a->a;
20966c0721eeSBarry Smith   PetscFunctionReturn(0);
20976c0721eeSBarry Smith }
20986c0721eeSBarry Smith 
20994a2ae208SSatish Balay #undef __FUNCT__
21004a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2101dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
21026c0721eeSBarry Smith {
21036c0721eeSBarry Smith   PetscFunctionBegin;
21046c0721eeSBarry Smith   PetscFunctionReturn(0);
21056c0721eeSBarry Smith }
2106273d9f13SBarry Smith 
2107ee4f033dSBarry Smith #undef __FUNCT__
2108ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2109dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2110ee4f033dSBarry Smith {
21116849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
21126849ba73SBarry Smith   PetscErrorCode ierr;
211397f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2114efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
211587828ca2SBarry Smith   PetscScalar    *vscale_array;
2116ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2117ee4f033dSBarry Smith   Vec            w1,w2,w3;
2118ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
211990d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2120ee4f033dSBarry Smith 
2121ee4f033dSBarry Smith   PetscFunctionBegin;
2122ee4f033dSBarry Smith   if (!coloring->w1) {
2123ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
212452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2125ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
212652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2127ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
212852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2129ee4f033dSBarry Smith   }
2130ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2131ee4f033dSBarry Smith 
2132ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
213390d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2134ee4f033dSBarry Smith   if (flg) {
2135ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2136ee4f033dSBarry Smith   } else {
21370b9b6f31SBarry Smith     PetscTruth assembled;
21380b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
21390b9b6f31SBarry Smith     if (assembled) {
2140ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2141ee4f033dSBarry Smith     }
21420b9b6f31SBarry Smith   }
2143ee4f033dSBarry Smith 
2144ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2145ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2146ee4f033dSBarry Smith 
2147ee4f033dSBarry Smith   /*
2148ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2149ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2150ee4f033dSBarry Smith   */
2151ee4f033dSBarry Smith   if (coloring->F) {
2152ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2153ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2154ee4f033dSBarry Smith     if (m1 != m2) {
2155ee4f033dSBarry Smith       coloring->F = 0;
2156ee4f033dSBarry Smith     }
2157ee4f033dSBarry Smith   }
2158ee4f033dSBarry Smith 
2159ee4f033dSBarry Smith   if (coloring->F) {
2160ee4f033dSBarry Smith     w1          = coloring->F;
2161ee4f033dSBarry Smith     coloring->F = 0;
2162ee4f033dSBarry Smith   } else {
216366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2164ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
216566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2166ee4f033dSBarry Smith   }
2167ee4f033dSBarry Smith 
2168ee4f033dSBarry Smith   /*
2169ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2170ee4f033dSBarry Smith   */
21711ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21721ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2173ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2174ee4f033dSBarry Smith     /*
2175ee4f033dSBarry Smith        Loop over each column associated with color adding the
2176ee4f033dSBarry Smith        perturbation to the vector w3.
2177ee4f033dSBarry Smith     */
2178ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2179ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2180ee4f033dSBarry Smith       dx  = xx[col];
2181ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2182ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2183ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2184ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2185ee4f033dSBarry Smith #else
2186ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2187ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2188ee4f033dSBarry Smith #endif
2189ee4f033dSBarry Smith       dx                *= epsilon;
2190ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2191ee4f033dSBarry Smith     }
2192ee4f033dSBarry Smith   }
21931ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2194ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2195ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2196ee4f033dSBarry Smith 
2197ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2198ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2199ee4f033dSBarry Smith 
2200ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2201ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2202ee4f033dSBarry Smith 
22031ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2204ee4f033dSBarry Smith   /*
2205ee4f033dSBarry Smith       Loop over each color
2206ee4f033dSBarry Smith   */
2207ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
220849b058dcSBarry Smith     coloring->currentcolor = k;
2209ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
22101ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2211ee4f033dSBarry Smith     /*
2212ee4f033dSBarry Smith        Loop over each column associated with color adding the
2213ee4f033dSBarry Smith        perturbation to the vector w3.
2214ee4f033dSBarry Smith     */
2215ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2216ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2217ee4f033dSBarry Smith       dx  = xx[col];
22185b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2219ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2220ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2221ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2222ee4f033dSBarry Smith #else
2223ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2224ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2225ee4f033dSBarry Smith #endif
2226ee4f033dSBarry Smith       dx            *= epsilon;
2227e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2228ee4f033dSBarry Smith       w3_array[col] += dx;
2229ee4f033dSBarry Smith     }
22301ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2231ee4f033dSBarry Smith 
2232ee4f033dSBarry Smith     /*
2233ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2234ee4f033dSBarry Smith     */
2235ee4f033dSBarry Smith 
223666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2237ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
223866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2239efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2240ee4f033dSBarry Smith 
2241ee4f033dSBarry Smith     /*
2242ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2243ee4f033dSBarry Smith     */
22441ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2245ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2246ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2247ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2248ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2249ee4f033dSBarry Smith       srow   = row + start;
2250ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2251ee4f033dSBarry Smith     }
22521ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2253ee4f033dSBarry Smith   }
225449b058dcSBarry Smith   coloring->currentcolor = k;
22551ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22561ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2257ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2258ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2259ee4f033dSBarry Smith   PetscFunctionReturn(0);
2260ee4f033dSBarry Smith }
2261ee4f033dSBarry Smith 
2262ac90fabeSBarry Smith #undef __FUNCT__
2263ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2264f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2265ac90fabeSBarry Smith {
2266dfbe8321SBarry Smith   PetscErrorCode ierr;
226797f1f81fSBarry Smith   PetscInt       i;
2268ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22690805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2270ac90fabeSBarry Smith 
2271ac90fabeSBarry Smith   PetscFunctionBegin;
2272ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2273f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2274f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2275c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2276a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2277a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2278a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2279a30b2313SHong Zhang     }
2280a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2281d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2282a30b2313SHong Zhang       y->XtoY = X;
2283407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2284c537a176SHong Zhang     }
2285f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22861e2582c4SBarry 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);
2287ac90fabeSBarry Smith   } else {
2288f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2289ac90fabeSBarry Smith   }
2290ac90fabeSBarry Smith   PetscFunctionReturn(0);
2291ac90fabeSBarry Smith }
2292ac90fabeSBarry Smith 
2293521d7252SBarry Smith #undef __FUNCT__
2294521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2295521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2296521d7252SBarry Smith {
229741c166b1SJed Brown   PetscErrorCode ierr;
229841c166b1SJed Brown 
2299521d7252SBarry Smith   PetscFunctionBegin;
230041c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
230141c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2302521d7252SBarry Smith   PetscFunctionReturn(0);
2303521d7252SBarry Smith }
2304521d7252SBarry Smith 
2305354c94deSBarry Smith #undef __FUNCT__
2306354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2307354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2308354c94deSBarry Smith {
2309354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2310354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2311354c94deSBarry Smith   PetscInt    i,nz;
2312354c94deSBarry Smith   PetscScalar *a;
2313354c94deSBarry Smith 
2314354c94deSBarry Smith   PetscFunctionBegin;
2315354c94deSBarry Smith   nz = aij->nz;
2316354c94deSBarry Smith   a  = aij->a;
2317354c94deSBarry Smith   for (i=0; i<nz; i++) {
2318354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2319354c94deSBarry Smith   }
2320354c94deSBarry Smith #else
2321354c94deSBarry Smith   PetscFunctionBegin;
2322354c94deSBarry Smith #endif
2323354c94deSBarry Smith   PetscFunctionReturn(0);
2324354c94deSBarry Smith }
2325354c94deSBarry Smith 
2326e34fafa9SBarry Smith #undef __FUNCT__
2327985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2328985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2329e34fafa9SBarry Smith {
2330e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2331e34fafa9SBarry Smith   PetscErrorCode ierr;
2332d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2333e34fafa9SBarry Smith   PetscReal      atmp;
2334985db425SBarry Smith   PetscScalar    *x;
2335e34fafa9SBarry Smith   MatScalar      *aa;
2336e34fafa9SBarry Smith 
2337e34fafa9SBarry Smith   PetscFunctionBegin;
2338e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2339e34fafa9SBarry Smith   aa   = a->a;
2340e34fafa9SBarry Smith   ai   = a->i;
2341e34fafa9SBarry Smith   aj   = a->j;
2342e34fafa9SBarry Smith 
2343985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2344e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2345e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2346e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2347e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2348e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23499189402eSHong Zhang     x[i] = 0.0;
2350e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2351985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2352985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2353985db425SBarry Smith       aa++; aj++;
2354985db425SBarry Smith     }
2355985db425SBarry Smith   }
2356985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2357985db425SBarry Smith   PetscFunctionReturn(0);
2358985db425SBarry Smith }
2359985db425SBarry Smith 
2360985db425SBarry Smith #undef __FUNCT__
2361985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2362985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2363985db425SBarry Smith {
2364985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2365985db425SBarry Smith   PetscErrorCode ierr;
2366d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2367985db425SBarry Smith   PetscScalar    *x;
2368985db425SBarry Smith   MatScalar      *aa;
2369985db425SBarry Smith 
2370985db425SBarry Smith   PetscFunctionBegin;
2371e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2372985db425SBarry Smith   aa   = a->a;
2373985db425SBarry Smith   ai   = a->i;
2374985db425SBarry Smith   aj   = a->j;
2375985db425SBarry Smith 
2376985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2377985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2378985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2379e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2380985db425SBarry Smith   for (i=0; i<m; i++) {
2381985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2382d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2383985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2384985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2385985db425SBarry Smith       x[i] = 0.0;
2386985db425SBarry Smith       if (idx) {
2387985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2388985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2389985db425SBarry Smith           if (aj[j] > j) {
2390985db425SBarry Smith             idx[i] = j;
2391985db425SBarry Smith             break;
2392985db425SBarry Smith           }
2393985db425SBarry Smith         }
2394985db425SBarry Smith       }
2395985db425SBarry Smith     }
2396985db425SBarry Smith     for (j=0; j<ncols; j++){
2397985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2398985db425SBarry Smith       aa++; aj++;
2399985db425SBarry Smith     }
2400985db425SBarry Smith   }
2401985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2402985db425SBarry Smith   PetscFunctionReturn(0);
2403985db425SBarry Smith }
2404985db425SBarry Smith 
2405985db425SBarry Smith #undef __FUNCT__
2406c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2407c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2408c87e5d42SMatthew Knepley {
2409c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2410c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2411c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2412c87e5d42SMatthew Knepley   PetscReal      atmp;
2413c87e5d42SMatthew Knepley   PetscScalar    *x;
2414c87e5d42SMatthew Knepley   MatScalar      *aa;
2415c87e5d42SMatthew Knepley 
2416c87e5d42SMatthew Knepley   PetscFunctionBegin;
2417e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2418c87e5d42SMatthew Knepley   aa   = a->a;
2419c87e5d42SMatthew Knepley   ai   = a->i;
2420c87e5d42SMatthew Knepley   aj   = a->j;
2421c87e5d42SMatthew Knepley 
2422c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2423c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2424c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2425e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2426c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2427c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2428289a08f5SMatthew Knepley     if (ncols) {
2429289a08f5SMatthew Knepley       /* Get first nonzero */
2430289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2431289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2432289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2433289a08f5SMatthew Knepley       }
2434289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2435289a08f5SMatthew Knepley     } else {
2436289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2437289a08f5SMatthew Knepley     }
2438c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2439c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2440289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2441c87e5d42SMatthew Knepley       aa++; aj++;
2442c87e5d42SMatthew Knepley     }
2443c87e5d42SMatthew Knepley   }
2444c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2445c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2446c87e5d42SMatthew Knepley }
2447c87e5d42SMatthew Knepley 
2448c87e5d42SMatthew Knepley #undef __FUNCT__
2449985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2450985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2451985db425SBarry Smith {
2452985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2453985db425SBarry Smith   PetscErrorCode ierr;
2454d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2455985db425SBarry Smith   PetscScalar    *x;
2456985db425SBarry Smith   MatScalar      *aa;
2457985db425SBarry Smith 
2458985db425SBarry Smith   PetscFunctionBegin;
2459e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2460985db425SBarry Smith   aa   = a->a;
2461985db425SBarry Smith   ai   = a->i;
2462985db425SBarry Smith   aj   = a->j;
2463985db425SBarry Smith 
2464985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2465985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2466985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2467e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2468985db425SBarry Smith   for (i=0; i<m; i++) {
2469985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2470d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2471985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2472985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2473985db425SBarry Smith       x[i] = 0.0;
2474985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2475985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2476985db425SBarry Smith         for (j=0;j<ncols;j++) {
2477985db425SBarry Smith           if (aj[j] > j) {
2478985db425SBarry Smith             idx[i] = j;
2479985db425SBarry Smith             break;
2480985db425SBarry Smith           }
2481985db425SBarry Smith         }
2482985db425SBarry Smith       }
2483985db425SBarry Smith     }
2484985db425SBarry Smith     for (j=0; j<ncols; j++){
2485985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2486985db425SBarry Smith       aa++; aj++;
2487e34fafa9SBarry Smith     }
2488e34fafa9SBarry Smith   }
2489e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2490e34fafa9SBarry Smith   PetscFunctionReturn(0);
2491e34fafa9SBarry Smith }
24923acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2493682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24940a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2495cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2496cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2497cb5b572fSBarry Smith        MatMult_SeqAIJ,
249897304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
24997c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
25007c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2501db4efbfdSBarry Smith        0,
2502db4efbfdSBarry Smith        0,
2503db4efbfdSBarry Smith        0,
2504db4efbfdSBarry Smith /*10*/ 0,
2505cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2506cb5b572fSBarry Smith        0,
250741f059aeSBarry Smith        MatSOR_SeqAIJ,
250817ab2063SBarry Smith        MatTranspose_SeqAIJ,
250997304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2510cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2511cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2512cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2513cb5b572fSBarry Smith        MatNorm_SeqAIJ,
251497304618SKris Buschelman /*20*/ 0,
2515cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2516cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2517cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2518d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2519db4efbfdSBarry Smith        0,
2520db4efbfdSBarry Smith        0,
2521db4efbfdSBarry Smith        0,
2522db4efbfdSBarry Smith        0,
2523d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2524db4efbfdSBarry Smith        0,
2525db4efbfdSBarry Smith        0,
25266c0721eeSBarry Smith        MatGetArray_SeqAIJ,
25276c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2528d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2529cb5b572fSBarry Smith        0,
2530cb5b572fSBarry Smith        0,
2531cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2532cb5b572fSBarry Smith        0,
2533d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2534cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2535cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2536cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2537cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2538d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2539cb5b572fSBarry Smith        MatScale_SeqAIJ,
2540cb5b572fSBarry Smith        0,
254179299369SBarry Smith        MatDiagonalSet_SeqAIJ,
2542fe97e370SBarry Smith        0,
2543d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
25443b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
25453b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
25463b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2547a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2548d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2549b9617806SBarry Smith        0,
25500513a670SBarry Smith        0,
2551cda55fadSBarry Smith        MatPermute_SeqAIJ,
2552cda55fadSBarry Smith        0,
2553d519adbfSMatthew Knepley /*59*/ 0,
2554b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2555b9b97703SBarry Smith        MatView_SeqAIJ,
2556357abbc8SBarry Smith        0,
2557ee4f033dSBarry Smith        0,
2558d519adbfSMatthew Knepley /*64*/ 0,
2559ee4f033dSBarry Smith        0,
2560ee4f033dSBarry Smith        0,
2561ee4f033dSBarry Smith        0,
2562ee4f033dSBarry Smith        0,
2563d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2564c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2565ee4f033dSBarry Smith        0,
2566ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2567dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2568ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2569dcf5cc72SBarry Smith #else
2570dcf5cc72SBarry Smith        0,
2571dcf5cc72SBarry Smith #endif
2572d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
25733acb8795SBarry Smith        MatFDColoringApply_AIJ,
257497304618SKris Buschelman        0,
257597304618SKris Buschelman        0,
257697304618SKris Buschelman        0,
2577d519adbfSMatthew Knepley /*79*/ 0,
257897304618SKris Buschelman        0,
257997304618SKris Buschelman        0,
258097304618SKris Buschelman        0,
2581bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2582d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
25831cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25846284ec50SHong Zhang        0,
25856284ec50SHong Zhang        0,
2586bc011b1eSHong Zhang        0,
2587d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
258826be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
258926be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2590d439da42SKris Buschelman        MatPtAP_Basic,
25917ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2592d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2593bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2594bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2595bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25967ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2597d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2598609c6c4dSKris Buschelman        0,
2599609c6c4dSKris Buschelman        0,
260087d4246cSBarry Smith        MatConjugate_SeqAIJ,
260187d4246cSBarry Smith        0,
2602d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
260399cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2604f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2605f5edf698SHong Zhang        0,
26062bebee5dSHong Zhang        0,
2607d519adbfSMatthew Knepley /*109*/0,
2608985db425SBarry Smith        0,
26092af78befSBarry Smith        MatGetRowMin_SeqAIJ,
26102af78befSBarry Smith        0,
2611599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2612d519adbfSMatthew Knepley /*114*/0,
2613599ef60dSHong Zhang        0,
26143c2a7987SHong Zhang        0,
2615fe97e370SBarry Smith        0,
2616fe97e370SBarry Smith        0
26179e29f15eSvictorle };
261817ab2063SBarry Smith 
2619fb2e594dSBarry Smith EXTERN_C_BEGIN
26204a2ae208SSatish Balay #undef __FUNCT__
26214a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2622be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2623bef8e0ddSBarry Smith {
2624bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
262597f1f81fSBarry Smith   PetscInt   i,nz,n;
2626bef8e0ddSBarry Smith 
2627bef8e0ddSBarry Smith   PetscFunctionBegin;
2628bef8e0ddSBarry Smith 
2629bef8e0ddSBarry Smith   nz = aij->maxnz;
2630d0f46423SBarry Smith   n  = mat->rmap->n;
2631bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2632bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2633bef8e0ddSBarry Smith   }
2634bef8e0ddSBarry Smith   aij->nz = nz;
2635bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2636bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2637bef8e0ddSBarry Smith   }
2638bef8e0ddSBarry Smith 
2639bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2640bef8e0ddSBarry Smith }
2641fb2e594dSBarry Smith EXTERN_C_END
2642bef8e0ddSBarry Smith 
26434a2ae208SSatish Balay #undef __FUNCT__
26444a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2645bef8e0ddSBarry Smith /*@
2646bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2647bef8e0ddSBarry Smith        in the matrix.
2648bef8e0ddSBarry Smith 
2649bef8e0ddSBarry Smith   Input Parameters:
2650bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2651bef8e0ddSBarry Smith -  indices - the column indices
2652bef8e0ddSBarry Smith 
265315091d37SBarry Smith   Level: advanced
265415091d37SBarry Smith 
2655bef8e0ddSBarry Smith   Notes:
2656bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2657bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2658bef8e0ddSBarry Smith   of the MatSetValues() operation.
2659bef8e0ddSBarry Smith 
2660bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2661d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2662bef8e0ddSBarry Smith 
2663bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2664bef8e0ddSBarry Smith 
2665b9617806SBarry Smith     The indices should start with zero, not one.
2666b9617806SBarry Smith 
2667bef8e0ddSBarry Smith @*/
2668be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2669bef8e0ddSBarry Smith {
267097f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2671bef8e0ddSBarry Smith 
2672bef8e0ddSBarry Smith   PetscFunctionBegin;
26730700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
26744482741eSBarry Smith   PetscValidPointer(indices,2);
2675c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2676bef8e0ddSBarry Smith   if (f) {
2677bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2678bef8e0ddSBarry Smith   } else {
2679e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2680bef8e0ddSBarry Smith   }
2681bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2682bef8e0ddSBarry Smith }
2683bef8e0ddSBarry Smith 
2684be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2685be6bf707SBarry Smith 
2686fb2e594dSBarry Smith EXTERN_C_BEGIN
26874a2ae208SSatish Balay #undef __FUNCT__
26884a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2689be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2690be6bf707SBarry Smith {
2691be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26926849ba73SBarry Smith   PetscErrorCode ierr;
2693d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2694be6bf707SBarry Smith 
2695be6bf707SBarry Smith   PetscFunctionBegin;
2696be6bf707SBarry Smith   if (aij->nonew != 1) {
2697e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2698be6bf707SBarry Smith   }
2699be6bf707SBarry Smith 
2700be6bf707SBarry Smith   /* allocate space for values if not already there */
2701be6bf707SBarry Smith   if (!aij->saved_values) {
270287828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
27039518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2704be6bf707SBarry Smith   }
2705be6bf707SBarry Smith 
2706be6bf707SBarry Smith   /* copy values over */
270787828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2708be6bf707SBarry Smith   PetscFunctionReturn(0);
2709be6bf707SBarry Smith }
2710fb2e594dSBarry Smith EXTERN_C_END
2711be6bf707SBarry Smith 
27124a2ae208SSatish Balay #undef __FUNCT__
2713b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2714be6bf707SBarry Smith /*@
2715be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2716be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2717be6bf707SBarry Smith        nonlinear portion.
2718be6bf707SBarry Smith 
2719be6bf707SBarry Smith    Collect on Mat
2720be6bf707SBarry Smith 
2721be6bf707SBarry Smith   Input Parameters:
27220e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2723be6bf707SBarry Smith 
272415091d37SBarry Smith   Level: advanced
272515091d37SBarry Smith 
2726be6bf707SBarry Smith   Common Usage, with SNESSolve():
2727be6bf707SBarry Smith $    Create Jacobian matrix
2728be6bf707SBarry Smith $    Set linear terms into matrix
2729be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2730be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2731be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2732512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2733be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2734be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2735be6bf707SBarry Smith $    In your Jacobian routine
2736be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2737be6bf707SBarry Smith $      Set nonlinear terms in matrix
2738be6bf707SBarry Smith 
2739be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2740be6bf707SBarry Smith $    // build linear portion of Jacobian
2741512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2742be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2743be6bf707SBarry Smith $    loop over nonlinear iterations
2744be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2745be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2746be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2747be6bf707SBarry Smith $       Solve linear system with Jacobian
2748be6bf707SBarry Smith $    endloop
2749be6bf707SBarry Smith 
2750be6bf707SBarry Smith   Notes:
2751be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2752512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2753be6bf707SBarry Smith     calling this routine.
2754be6bf707SBarry Smith 
27550c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
27560c468ba9SBarry Smith     and does not allocated additional space.
27570c468ba9SBarry Smith 
2758be6bf707SBarry Smith .seealso: MatRetrieveValues()
2759be6bf707SBarry Smith 
2760be6bf707SBarry Smith @*/
2761be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2762be6bf707SBarry Smith {
2763dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2764be6bf707SBarry Smith 
2765be6bf707SBarry Smith   PetscFunctionBegin;
27660700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2767e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2768e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2769be6bf707SBarry Smith 
2770c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2771be6bf707SBarry Smith   if (f) {
2772be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2773be6bf707SBarry Smith   } else {
2774e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to store values");
2775be6bf707SBarry Smith   }
2776be6bf707SBarry Smith   PetscFunctionReturn(0);
2777be6bf707SBarry Smith }
2778be6bf707SBarry Smith 
2779fb2e594dSBarry Smith EXTERN_C_BEGIN
27804a2ae208SSatish Balay #undef __FUNCT__
27814a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2782be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2783be6bf707SBarry Smith {
2784be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27856849ba73SBarry Smith   PetscErrorCode ierr;
2786d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2787be6bf707SBarry Smith 
2788be6bf707SBarry Smith   PetscFunctionBegin;
2789be6bf707SBarry Smith   if (aij->nonew != 1) {
2790e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2791be6bf707SBarry Smith   }
2792be6bf707SBarry Smith   if (!aij->saved_values) {
2793e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2794be6bf707SBarry Smith   }
2795be6bf707SBarry Smith   /* copy values over */
279687828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2797be6bf707SBarry Smith   PetscFunctionReturn(0);
2798be6bf707SBarry Smith }
2799fb2e594dSBarry Smith EXTERN_C_END
2800be6bf707SBarry Smith 
28014a2ae208SSatish Balay #undef __FUNCT__
28024a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2803be6bf707SBarry Smith /*@
2804be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2805be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2806be6bf707SBarry Smith        nonlinear portion.
2807be6bf707SBarry Smith 
2808be6bf707SBarry Smith    Collect on Mat
2809be6bf707SBarry Smith 
2810be6bf707SBarry Smith   Input Parameters:
2811be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2812be6bf707SBarry Smith 
281315091d37SBarry Smith   Level: advanced
281415091d37SBarry Smith 
2815be6bf707SBarry Smith .seealso: MatStoreValues()
2816be6bf707SBarry Smith 
2817be6bf707SBarry Smith @*/
2818be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2819be6bf707SBarry Smith {
2820dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2821be6bf707SBarry Smith 
2822be6bf707SBarry Smith   PetscFunctionBegin;
28230700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2824e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2825e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2826be6bf707SBarry Smith 
2827c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2828be6bf707SBarry Smith   if (f) {
2829be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2830be6bf707SBarry Smith   } else {
2831e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2832be6bf707SBarry Smith   }
2833be6bf707SBarry Smith   PetscFunctionReturn(0);
2834be6bf707SBarry Smith }
2835be6bf707SBarry Smith 
2836f83d6046SBarry Smith 
2837be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
28384a2ae208SSatish Balay #undef __FUNCT__
28394a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
284017ab2063SBarry Smith /*@C
2841682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
28420d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
28436e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
284451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
28452bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
284617ab2063SBarry Smith 
2847db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2848db81eaa0SLois Curfman McInnes 
284917ab2063SBarry Smith    Input Parameters:
2850db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
285117ab2063SBarry Smith .  m - number of rows
285217ab2063SBarry Smith .  n - number of columns
285317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
285451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
28552bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
285617ab2063SBarry Smith 
285717ab2063SBarry Smith    Output Parameter:
2858416022c9SBarry Smith .  A - the matrix
285917ab2063SBarry Smith 
2860175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2861ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2862175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2863175b88e8SBarry Smith 
2864b259b22eSLois Curfman McInnes    Notes:
286549a6f317SBarry Smith    If nnz is given then nz is ignored
286649a6f317SBarry Smith 
286717ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
286817ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28690002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
287044cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
287117ab2063SBarry Smith 
287217ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2873a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28743d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28756da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
287617ab2063SBarry Smith 
2877682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
28784fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2879682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
28806c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
28816c7ebb05SLois Curfman McInnes 
28826c7ebb05SLois Curfman McInnes    Options Database Keys:
2883698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2884698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2885db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2886db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2887db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
288817ab2063SBarry Smith 
2889027ccd11SLois Curfman McInnes    Level: intermediate
2890027ccd11SLois Curfman McInnes 
289136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
289236db0b34SBarry Smith 
289317ab2063SBarry Smith @*/
2894be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
289517ab2063SBarry Smith {
2896dfbe8321SBarry Smith   PetscErrorCode ierr;
28976945ee14SBarry Smith 
28983a40ed3dSBarry Smith   PetscFunctionBegin;
2899f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2900117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2901c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2902ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2903273d9f13SBarry Smith   PetscFunctionReturn(0);
2904273d9f13SBarry Smith }
2905273d9f13SBarry Smith 
29064a2ae208SSatish Balay #undef __FUNCT__
29074a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2908273d9f13SBarry Smith /*@C
2909273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2910273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2911273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2912273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2913273d9f13SBarry Smith 
2914273d9f13SBarry Smith    Collective on MPI_Comm
2915273d9f13SBarry Smith 
2916273d9f13SBarry Smith    Input Parameters:
2917117016b1SBarry Smith +  B - The matrix-free
2918273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2919273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2920273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2921273d9f13SBarry Smith 
2922273d9f13SBarry Smith    Notes:
292349a6f317SBarry Smith      If nnz is given then nz is ignored
292449a6f317SBarry Smith 
2925273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2926273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2927273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2928273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2929273d9f13SBarry Smith 
2930273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2931273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2932273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2933273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2934273d9f13SBarry Smith 
2935aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2936aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2937aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2938aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2939aa95bbe8SBarry Smith 
2940a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2941a96a251dSBarry Smith    entries or columns indices
2942a96a251dSBarry Smith 
2943273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2944273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2945273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2946273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2947273d9f13SBarry Smith 
2948273d9f13SBarry Smith    Options Database Keys:
2949698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2950698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2951273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2952273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2953273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2954273d9f13SBarry Smith 
2955273d9f13SBarry Smith    Level: intermediate
2956273d9f13SBarry Smith 
2957aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2958273d9f13SBarry Smith 
2959273d9f13SBarry Smith @*/
2960be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2961273d9f13SBarry Smith {
296297f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2963a23d5eceSKris Buschelman 
2964a23d5eceSKris Buschelman   PetscFunctionBegin;
2965a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2966a23d5eceSKris Buschelman   if (f) {
2967a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2968a23d5eceSKris Buschelman   }
2969a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2970a23d5eceSKris Buschelman }
2971a23d5eceSKris Buschelman 
2972a23d5eceSKris Buschelman EXTERN_C_BEGIN
2973a23d5eceSKris Buschelman #undef __FUNCT__
2974a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2975be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2976a23d5eceSKris Buschelman {
2977273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2978a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
29796849ba73SBarry Smith   PetscErrorCode ierr;
298097f1f81fSBarry Smith   PetscInt       i;
2981273d9f13SBarry Smith 
2982273d9f13SBarry Smith   PetscFunctionBegin;
2983d5d45c9bSBarry Smith 
2984a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2985c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2986c461c341SBarry Smith     nz             = 0;
2987c461c341SBarry Smith   }
2988c461c341SBarry Smith 
298926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
299026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
299126283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
299226283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2993899cda47SBarry Smith 
2994435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2995e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2996b73539f3SBarry Smith   if (nnz) {
2997d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
2998e32f2f54SBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2999e32f2f54SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
3000b73539f3SBarry Smith     }
3001b73539f3SBarry Smith   }
3002b73539f3SBarry Smith 
3003273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3004273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3005273d9f13SBarry Smith 
3006ab93d7beSBarry Smith   if (!skipallocation) {
30072ee49352SLisandro Dalcin     if (!b->imax) {
3008d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3009d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
30102ee49352SLisandro Dalcin     }
3011273d9f13SBarry Smith     if (!nnz) {
3012435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3013273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
3014d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3015d0f46423SBarry Smith       nz = nz*B->rmap->n;
3016273d9f13SBarry Smith     } else {
3017273d9f13SBarry Smith       nz = 0;
3018d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3019273d9f13SBarry Smith     }
3020ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3021d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3022ab93d7beSBarry Smith 
3023273d9f13SBarry Smith     /* allocate the matrix space */
30242ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3025d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3026d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3027bfeeae90SHong Zhang     b->i[0] = 0;
3028d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
30295da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
30305da197adSKris Buschelman     }
3031273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3032e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3033e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3034c461c341SBarry Smith   } else {
3035e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3036e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3037c461c341SBarry Smith   }
3038273d9f13SBarry Smith 
3039273d9f13SBarry Smith   b->nz                = 0;
3040273d9f13SBarry Smith   b->maxnz             = nz;
3041273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3042273d9f13SBarry Smith   PetscFunctionReturn(0);
3043273d9f13SBarry Smith }
3044a23d5eceSKris Buschelman EXTERN_C_END
3045273d9f13SBarry Smith 
3046a1661176SMatthew Knepley #undef  __FUNCT__
3047a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
304858d36128SBarry Smith /*@
3049a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3050a1661176SMatthew Knepley 
3051a1661176SMatthew Knepley    Input Parameters:
3052a1661176SMatthew Knepley +  B - the matrix
3053a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3054a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3055a1661176SMatthew Knepley -  v - optional values in the matrix
3056a1661176SMatthew Knepley 
3057a1661176SMatthew Knepley    Level: developer
3058a1661176SMatthew Knepley 
305958d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
306058d36128SBarry Smith 
3061a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3062a1661176SMatthew Knepley 
3063a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3064a1661176SMatthew Knepley @*/
3065a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3066a1661176SMatthew Knepley {
3067a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3068a1661176SMatthew Knepley   PetscErrorCode ierr;
3069a1661176SMatthew Knepley 
3070a1661176SMatthew Knepley   PetscFunctionBegin;
30710700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
3072a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3073a1661176SMatthew Knepley   if (f) {
3074a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3075a1661176SMatthew Knepley   }
3076a1661176SMatthew Knepley   PetscFunctionReturn(0);
3077a1661176SMatthew Knepley }
3078a1661176SMatthew Knepley 
3079a1661176SMatthew Knepley EXTERN_C_BEGIN
3080a1661176SMatthew Knepley #undef  __FUNCT__
3081a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3082b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3083a1661176SMatthew Knepley {
3084a1661176SMatthew Knepley   PetscInt       i;
3085a1661176SMatthew Knepley   PetscInt       m,n;
3086a1661176SMatthew Knepley   PetscInt       nz;
3087a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3088a1661176SMatthew Knepley   PetscScalar    *values;
3089a1661176SMatthew Knepley   PetscErrorCode ierr;
3090a1661176SMatthew Knepley 
3091a1661176SMatthew Knepley   PetscFunctionBegin;
3092a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3093a1661176SMatthew Knepley 
309465e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3095a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3096a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3097b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3098a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
309965e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3100a1661176SMatthew Knepley     nnz[i] = nz;
3101a1661176SMatthew Knepley   }
3102a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3103a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3104a1661176SMatthew Knepley 
3105a1661176SMatthew Knepley   if (v) {
3106a1661176SMatthew Knepley     values = (PetscScalar*) v;
3107a1661176SMatthew Knepley   } else {
31080e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3109a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3110a1661176SMatthew Knepley   }
3111a1661176SMatthew Knepley 
3112a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3113b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3114b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3115a1661176SMatthew Knepley   }
3116a1661176SMatthew Knepley 
3117a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3118a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3119a1661176SMatthew Knepley 
3120a1661176SMatthew Knepley   if (!v) {
3121a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3122a1661176SMatthew Knepley   }
3123a1661176SMatthew Knepley   PetscFunctionReturn(0);
3124a1661176SMatthew Knepley }
3125a1661176SMatthew Knepley EXTERN_C_END
3126a1661176SMatthew Knepley 
31277c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3128be7314b0SBarry Smith #include "private/petscaxpy.h"
3129170fe5c8SBarry Smith 
3130170fe5c8SBarry Smith #undef __FUNCT__
3131170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3132170fe5c8SBarry Smith /*
3133170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3134170fe5c8SBarry Smith 
3135170fe5c8SBarry Smith                n                       p                          p
3136170fe5c8SBarry Smith         (              )       (              )         (                  )
3137170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3138170fe5c8SBarry Smith         (              )       (              )         (                  )
3139170fe5c8SBarry Smith 
3140170fe5c8SBarry Smith */
3141170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3142170fe5c8SBarry Smith {
3143170fe5c8SBarry Smith   PetscErrorCode     ierr;
3144170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3145170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3146170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
31471de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3148170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3149170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3150170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3151170fe5c8SBarry Smith 
3152170fe5c8SBarry Smith   PetscFunctionBegin;
3153d0f46423SBarry Smith   m = A->rmap->n;
3154d0f46423SBarry Smith   n = A->cmap->n;
3155d0f46423SBarry Smith   p = B->cmap->n;
3156170fe5c8SBarry Smith   a = sub_a->v;
3157170fe5c8SBarry Smith   b = sub_b->a;
3158170fe5c8SBarry Smith   c = sub_c->v;
3159170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3160170fe5c8SBarry Smith 
3161170fe5c8SBarry Smith   ii  = sub_b->i;
3162170fe5c8SBarry Smith   idx = sub_b->j;
3163170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3164170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3165170fe5c8SBarry Smith     while (q-->0) {
3166170fe5c8SBarry Smith       c_q = c + m*(*idx);
3167170fe5c8SBarry Smith       a_q = a + m*i;
3168be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3169170fe5c8SBarry Smith       idx++;
3170170fe5c8SBarry Smith       b++;
3171170fe5c8SBarry Smith     }
3172170fe5c8SBarry Smith   }
3173170fe5c8SBarry Smith   PetscFunctionReturn(0);
3174170fe5c8SBarry Smith }
3175170fe5c8SBarry Smith 
3176170fe5c8SBarry Smith #undef __FUNCT__
3177170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3178170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3179170fe5c8SBarry Smith {
3180170fe5c8SBarry Smith   PetscErrorCode ierr;
3181d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3182170fe5c8SBarry Smith   Mat            Cmat;
3183170fe5c8SBarry Smith 
3184170fe5c8SBarry Smith   PetscFunctionBegin;
3185e32f2f54SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
318639804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3187170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3188170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3189170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3190170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3191170fe5c8SBarry Smith   *C = Cmat;
3192170fe5c8SBarry Smith   PetscFunctionReturn(0);
3193170fe5c8SBarry Smith }
3194170fe5c8SBarry Smith 
3195170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3196170fe5c8SBarry Smith #undef __FUNCT__
3197170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3198170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3199170fe5c8SBarry Smith {
3200170fe5c8SBarry Smith   PetscErrorCode ierr;
3201170fe5c8SBarry Smith 
3202170fe5c8SBarry Smith   PetscFunctionBegin;
3203170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3204170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3205170fe5c8SBarry Smith   }
3206170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3207170fe5c8SBarry Smith   PetscFunctionReturn(0);
3208170fe5c8SBarry Smith }
3209170fe5c8SBarry Smith 
3210170fe5c8SBarry Smith 
32110bad9183SKris Buschelman /*MC
3212fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
32130bad9183SKris Buschelman    based on compressed sparse row format.
32140bad9183SKris Buschelman 
32150bad9183SKris Buschelman    Options Database Keys:
32160bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
32170bad9183SKris Buschelman 
32180bad9183SKris Buschelman   Level: beginner
32190bad9183SKris Buschelman 
3220f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
32210bad9183SKris Buschelman M*/
32220bad9183SKris Buschelman 
3223a6175056SHong Zhang EXTERN_C_BEGIN
3224b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3225b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3226b5e56a35SBarry Smith #endif
322765460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3228af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3229af1023dbSSatish Balay #endif
323017667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3231b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3232a83b8d76SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
3233db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3234611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3235bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3236611f576cSBarry Smith #endif
3237611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32385c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3239611f576cSBarry Smith #endif
3240f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3241f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3242f3c0ef26SHong Zhang #endif
3243611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32445c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3245611f576cSBarry Smith #endif
3246eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3247eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3248eb3b5408SSatish Balay #endif
3249719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3250719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3251719d5645SBarry Smith #endif
3252b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3253b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
3254b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEnginePut_SeqAIJ(PetscObject,void*);
3255b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEngineGet_SeqAIJ(PetscObject,void*);
3256b3866ffcSBarry Smith #endif
325717667f90SBarry Smith EXTERN_C_END
325817667f90SBarry Smith 
3259b24902e0SBarry Smith 
326017667f90SBarry Smith EXTERN_C_BEGIN
32614a2ae208SSatish Balay #undef __FUNCT__
32624a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3263be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3264273d9f13SBarry Smith {
3265273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3266dfbe8321SBarry Smith   PetscErrorCode ierr;
326738baddfdSBarry Smith   PetscMPIInt    size;
3268273d9f13SBarry Smith 
3269273d9f13SBarry Smith   PetscFunctionBegin;
32707adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3271e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3272273d9f13SBarry Smith 
327338f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3274b0a32e0cSBarry Smith   B->data             = (void*)b;
3275549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
327690f02eecSBarry Smith   B->mapping          = 0;
3277416022c9SBarry Smith   b->row              = 0;
3278416022c9SBarry Smith   b->col              = 0;
327982bf6240SBarry Smith   b->icol             = 0;
3280b810aeb4SBarry Smith   b->reallocs         = 0;
328136db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3282f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3283416022c9SBarry Smith   b->nonew             = 0;
3284416022c9SBarry Smith   b->diag              = 0;
3285416022c9SBarry Smith   b->solve_work        = 0;
32862a1b7f2aSHong Zhang   B->spptr             = 0;
3287be6bf707SBarry Smith   b->saved_values      = 0;
3288d7f994e1SBarry Smith   b->idiag             = 0;
328971f1c65dSBarry Smith   b->mdiag             = 0;
329071f1c65dSBarry Smith   b->ssor_work         = 0;
329171f1c65dSBarry Smith   b->omega             = 1.0;
329271f1c65dSBarry Smith   b->fshift            = 0.0;
329371f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3294a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3295a30b2313SHong Zhang   b->xtoy              = 0;
3296a30b2313SHong Zhang   b->XtoY              = 0;
329773e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3298d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3299d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3300d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3301d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
330288e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
330317ab2063SBarry Smith 
330435d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3305b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3306b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C",
3307b3866ffcSBarry Smith 					   "MatGetFactor_seqaij_matlab",
3308b3866ffcSBarry Smith 					   MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3309b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3310b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3311b3866ffcSBarry Smith #endif
3312b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3313ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
3314b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3315b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3316b5e56a35SBarry Smith #endif
331765460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3318ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C",
3319719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3320719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3321719d5645SBarry Smith #endif
3322611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3323ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C",
33245c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
33255c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3326611f576cSBarry Smith #endif
3327f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3328ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
3329f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3330f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3331f3c0ef26SHong Zhang #endif
3332611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3333ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
33345c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
33355c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3336611f576cSBarry Smith #endif
3337611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3338ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
3339bccb9932SShri Abhyankar                                      "MatGetFactor_aij_mumps",
3340bccb9932SShri Abhyankar                                      MatGetFactor_aij_mumps);CHKERRQ(ierr);
3341611f576cSBarry Smith #endif
3342eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3343ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C",
3344eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3345eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3346eb3b5408SSatish Balay #endif
3347719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3348ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C",
3349719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3350719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3351719d5645SBarry Smith #endif
3352ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3353b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3354b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3355ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3356db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3357db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3358174acd27SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C",
3359a83b8d76SBarry Smith                                      "MatGetFactor_seqaij_bas",
3360174acd27SBarry Smith                                      MatGetFactor_seqaij_bas);
3361f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3362bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3363bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3364f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3365be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3366bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3367f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3368be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3369bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3370a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3371a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3372a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
337385fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
337485fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
337585fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3376ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3377ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3378ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
337917667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
338017667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
338117667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33825fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33835fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33845fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33851cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33861cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33871cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3388a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3389a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3390a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3391a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3392a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3393a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
339405b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
339505b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
339605b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3397170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3398170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3399170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3400170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3401170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3402170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3403170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3404170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3405170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
34064108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
340717667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
34083a40ed3dSBarry Smith   PetscFunctionReturn(0);
340917ab2063SBarry Smith }
3410273d9f13SBarry Smith EXTERN_C_END
341117ab2063SBarry Smith 
34124a2ae208SSatish Balay #undef __FUNCT__
3413b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3414b24902e0SBarry Smith /*
3415b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3416b24902e0SBarry Smith */
3417f77e22a1SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
341817ab2063SBarry Smith {
3419416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
34206849ba73SBarry Smith   PetscErrorCode ierr;
3421d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
342217ab2063SBarry Smith 
34233a40ed3dSBarry Smith   PetscFunctionBegin;
3424273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3425273d9f13SBarry Smith 
3426d5f3da31SBarry Smith   C->factortype     = A->factortype;
3427416022c9SBarry Smith   c->row            = 0;
3428416022c9SBarry Smith   c->col            = 0;
342982bf6240SBarry Smith   c->icol           = 0;
34306ad4291fSHong Zhang   c->reallocs       = 0;
343117ab2063SBarry Smith 
34326ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
343317ab2063SBarry Smith 
343426283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
343526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
343626283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
343726283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3438eec197d1SBarry Smith 
343933b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
34409518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
344117ab2063SBarry Smith   for (i=0; i<m; i++) {
3442416022c9SBarry Smith     c->imax[i] = a->imax[i];
3443416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
344417ab2063SBarry Smith   }
344517ab2063SBarry Smith 
344617ab2063SBarry Smith   /* allocate the matrix space */
3447f77e22a1SHong Zhang   if (mallocmatspace){
3448a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
34499518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3450f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
345197f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
345217ab2063SBarry Smith     if (m > 0) {
345397f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3454be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3455bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3456be6bf707SBarry Smith       } else {
3457bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
345817ab2063SBarry Smith       }
345908480c60SBarry Smith     }
3460f77e22a1SHong Zhang   }
346117ab2063SBarry Smith 
34626ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3463416022c9SBarry Smith   c->roworiented       = a->roworiented;
3464416022c9SBarry Smith   c->nonew             = a->nonew;
3465416022c9SBarry Smith   if (a->diag) {
346697f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
346752e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
346817ab2063SBarry Smith     for (i=0; i<m; i++) {
3469416022c9SBarry Smith       c->diag[i] = a->diag[i];
347017ab2063SBarry Smith     }
34713a40ed3dSBarry Smith   } else c->diag           = 0;
34726ad4291fSHong Zhang   c->solve_work            = 0;
34736ad4291fSHong Zhang   c->saved_values          = 0;
34746ad4291fSHong Zhang   c->idiag                 = 0;
347571f1c65dSBarry Smith   c->ssor_work             = 0;
3476a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3477e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3478e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34796ad4291fSHong Zhang   c->xtoy                  = 0;
34806ad4291fSHong Zhang   c->XtoY                  = 0;
34816ad4291fSHong Zhang 
3482416022c9SBarry Smith   c->nz                 = a->nz;
34838ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3484273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3485754ec7b1SSatish Balay 
34866ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34876ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
34886ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
34896ad4291fSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
34906ad4291fSHong Zhang     i = a->compressedrow.nrows;
34910e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
34926ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
34936ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
349427ea64f8SHong Zhang   } else {
349527ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
349627ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
349727ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
34986ad4291fSHong Zhang   }
349988e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
35004108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
35014846f1f5SKris Buschelman 
35027adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
35033a40ed3dSBarry Smith   PetscFunctionReturn(0);
350417ab2063SBarry Smith }
350517ab2063SBarry Smith 
35064a2ae208SSatish Balay #undef __FUNCT__
3507b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3508b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3509b24902e0SBarry Smith {
3510b24902e0SBarry Smith   PetscErrorCode ierr;
3511b24902e0SBarry Smith 
3512b24902e0SBarry Smith   PetscFunctionBegin;
3513b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
35144b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3515b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3516f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3517b24902e0SBarry Smith   PetscFunctionReturn(0);
3518b24902e0SBarry Smith }
3519b24902e0SBarry Smith 
3520b24902e0SBarry Smith #undef __FUNCT__
35214a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3522a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
352317ab2063SBarry Smith {
3524416022c9SBarry Smith   Mat_SeqAIJ     *a;
3525416022c9SBarry Smith   Mat            B;
35266849ba73SBarry Smith   PetscErrorCode ierr;
35273c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
352838baddfdSBarry Smith   int            fd;
352938baddfdSBarry Smith   PetscMPIInt    size;
3530bcd2baecSBarry Smith   MPI_Comm       comm;
353117ab2063SBarry Smith 
35323a40ed3dSBarry Smith   PetscFunctionBegin;
3533e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3534d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3535e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3536b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
35370752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3538e32f2f54SBarry Smith   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
353917ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
354017ab2063SBarry Smith 
3541d64ed03dSBarry Smith   if (nz < 0) {
3542e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3543d64ed03dSBarry Smith   }
3544d64ed03dSBarry Smith 
354517ab2063SBarry Smith   /* read in row lengths */
354697f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
35470752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
354817ab2063SBarry Smith 
35493c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
35503c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3551e32f2f54SBarry Smith   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
35523c601197SSatish Balay 
355317ab2063SBarry Smith   /* create our matrix */
3554f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3555f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3556b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3557ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3558416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
355917ab2063SBarry Smith 
35600752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
356117ab2063SBarry Smith 
356217ab2063SBarry Smith   /* read in nonzero values */
35630752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
356417ab2063SBarry Smith 
356517ab2063SBarry Smith   /* set matrix "i" values */
3566efb16452SHong Zhang   a->i[0] = 0;
356717ab2063SBarry Smith   for (i=1; i<= M; i++) {
3568416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3569416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
357017ab2063SBarry Smith   }
3571606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
357217ab2063SBarry Smith 
35736d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35746d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3575b3a1e11cSKris Buschelman   *A = B;
35763a40ed3dSBarry Smith   PetscFunctionReturn(0);
357717ab2063SBarry Smith }
357817ab2063SBarry Smith 
35794a2ae208SSatish Balay #undef __FUNCT__
3580b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3581dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35827264ac53SSatish Balay {
35837264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3584dfbe8321SBarry Smith   PetscErrorCode ierr;
3585eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3586eeffb40dSHong Zhang   PetscInt k;
3587eeffb40dSHong Zhang #endif
35887264ac53SSatish Balay 
35893a40ed3dSBarry Smith   PetscFunctionBegin;
3590bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3591d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3592ca44d042SBarry Smith     *flg = PETSC_FALSE;
3593ca44d042SBarry Smith     PetscFunctionReturn(0);
3594bcd2baecSBarry Smith   }
35957264ac53SSatish Balay 
35967264ac53SSatish Balay   /* if the a->i are the same */
3597d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3598abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
35997264ac53SSatish Balay 
36007264ac53SSatish Balay   /* if a->j are the same */
360197f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3602abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3603bcd2baecSBarry Smith 
3604bcd2baecSBarry Smith   /* if a->a are the same */
3605eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3606eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3607eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3608eeffb40dSHong Zhang       *flg = PETSC_FALSE;
36093a40ed3dSBarry Smith       PetscFunctionReturn(0);
3610eeffb40dSHong Zhang     }
3611eeffb40dSHong Zhang   }
3612eeffb40dSHong Zhang #else
3613eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3614eeffb40dSHong Zhang #endif
3615eeffb40dSHong Zhang   PetscFunctionReturn(0);
36167264ac53SSatish Balay }
361736db0b34SBarry Smith 
36184a2ae208SSatish Balay #undef __FUNCT__
36194a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
362005869f15SSatish Balay /*@
362136db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
362236db0b34SBarry Smith               provided by the user.
362336db0b34SBarry Smith 
3624c75a6043SHong Zhang       Collective on MPI_Comm
362536db0b34SBarry Smith 
362636db0b34SBarry Smith    Input Parameters:
362736db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
362836db0b34SBarry Smith .   m - number of rows
362936db0b34SBarry Smith .   n - number of columns
363036db0b34SBarry Smith .   i - row indices
363136db0b34SBarry Smith .   j - column indices
363236db0b34SBarry Smith -   a - matrix values
363336db0b34SBarry Smith 
363436db0b34SBarry Smith    Output Parameter:
363536db0b34SBarry Smith .   mat - the matrix
363636db0b34SBarry Smith 
363736db0b34SBarry Smith    Level: intermediate
363836db0b34SBarry Smith 
363936db0b34SBarry Smith    Notes:
36400551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
364136db0b34SBarry Smith     once the matrix is destroyed
364236db0b34SBarry Smith 
364336db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
364436db0b34SBarry Smith 
3645bfeeae90SHong Zhang        The i and j indices are 0 based
364636db0b34SBarry Smith 
3647a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3648a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3649a4552177SSatish Balay     as shown:
3650a4552177SSatish Balay 
3651a4552177SSatish Balay         1 0 0
3652a4552177SSatish Balay         2 0 3
3653a4552177SSatish Balay         4 5 6
3654a4552177SSatish Balay 
3655a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
36569985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3657a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3658a4552177SSatish Balay 
36599985e31cSBarry Smith 
36602fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
366136db0b34SBarry Smith 
366236db0b34SBarry Smith @*/
3663a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
366436db0b34SBarry Smith {
3665dfbe8321SBarry Smith   PetscErrorCode ierr;
3666cbcfb4deSHong Zhang   PetscInt       ii;
366736db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3668cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3669cbcfb4deSHong Zhang   PetscInt       jj;
3670cbcfb4deSHong Zhang #endif
367136db0b34SBarry Smith 
367236db0b34SBarry Smith   PetscFunctionBegin;
3673a96a251dSBarry Smith   if (i[0]) {
3674e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
367536db0b34SBarry Smith   }
3676f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3677f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3678ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3679ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3680ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3681ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3682ab93d7beSBarry Smith 
368336db0b34SBarry Smith   aij->i = i;
368436db0b34SBarry Smith   aij->j = j;
368536db0b34SBarry Smith   aij->a = a;
368636db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
368736db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3688e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3689e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
369036db0b34SBarry Smith 
369136db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
369236db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
36932515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
3694e32f2f54SBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
36959985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
3696e32f2f54SBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
3697e32f2f54SBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
36989985e31cSBarry Smith     }
369936db0b34SBarry Smith #endif
370036db0b34SBarry Smith   }
37012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
370236db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3703e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3704e32f2f54SBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
370536db0b34SBarry Smith   }
370636db0b34SBarry Smith #endif
370736db0b34SBarry Smith 
3708b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3709b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
371036db0b34SBarry Smith   PetscFunctionReturn(0);
371136db0b34SBarry Smith }
371236db0b34SBarry Smith 
3713cc8ba8e1SBarry Smith #undef __FUNCT__
3714ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3715dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3716cc8ba8e1SBarry Smith {
3717dfbe8321SBarry Smith   PetscErrorCode ierr;
3718cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
371936db0b34SBarry Smith 
3720cc8ba8e1SBarry Smith   PetscFunctionBegin;
37218ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3722cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3723cc8ba8e1SBarry Smith     a->coloring = coloring;
372412c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
372597f1f81fSBarry Smith     PetscInt             i,*larray;
372612c595b3SBarry Smith     ISColoring      ocoloring;
372708b6dcc0SBarry Smith     ISColoringValue *colors;
372812c595b3SBarry Smith 
372912c595b3SBarry Smith     /* set coloring for diagonal portion */
37300e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3731d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
373212c595b3SBarry Smith       larray[i] = i;
373312c595b3SBarry Smith     }
3734d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
37350e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3736d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
373712c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
373812c595b3SBarry Smith     }
373912c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3740d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
374112c595b3SBarry Smith     a->coloring = ocoloring;
374212c595b3SBarry Smith   }
3743cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3744cc8ba8e1SBarry Smith }
3745cc8ba8e1SBarry Smith 
3746dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3747ee4f033dSBarry Smith EXTERN_C_BEGIN
374829c1e371SBarry Smith #include "adic/ad_utils.h"
3749ee4f033dSBarry Smith EXTERN_C_END
3750cc8ba8e1SBarry Smith 
3751cc8ba8e1SBarry Smith #undef __FUNCT__
3752ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3753dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3754cc8ba8e1SBarry Smith {
3755cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3756d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
37574440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
375808b6dcc0SBarry Smith   ISColoringValue *color;
3759cc8ba8e1SBarry Smith 
3760cc8ba8e1SBarry Smith   PetscFunctionBegin;
3761e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
37624440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3763cc8ba8e1SBarry Smith   color = a->coloring->colors;
3764cc8ba8e1SBarry Smith   /* loop over rows */
3765cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3766cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3767cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3768cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3769cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3770cc8ba8e1SBarry Smith     }
37714440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3772ee4f033dSBarry Smith   }
3773ee4f033dSBarry Smith   PetscFunctionReturn(0);
3774ee4f033dSBarry Smith }
3775ee4f033dSBarry Smith #endif
3776ee4f033dSBarry Smith 
3777ee4f033dSBarry Smith #undef __FUNCT__
3778ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
377997f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3780ee4f033dSBarry Smith {
3781ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3782d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
378354f21887SBarry Smith   MatScalar       *v = a->a;
378454f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
378508b6dcc0SBarry Smith   ISColoringValue *color;
3786ee4f033dSBarry Smith 
3787ee4f033dSBarry Smith   PetscFunctionBegin;
3788e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3789ee4f033dSBarry Smith   color = a->coloring->colors;
3790ee4f033dSBarry Smith   /* loop over rows */
3791ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3792ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3793ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3794ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3795ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3796ee4f033dSBarry Smith     }
3797ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3798cc8ba8e1SBarry Smith   }
3799cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3800cc8ba8e1SBarry Smith }
380136db0b34SBarry Smith 
380281824310SBarry Smith /*
380381824310SBarry Smith     Special version for direct calls from Fortran
380481824310SBarry Smith */
380581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
380681824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
380781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
380881824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
380981824310SBarry Smith #endif
381081824310SBarry Smith 
381181824310SBarry Smith /* Change these macros so can be used in void function */
381281824310SBarry Smith #undef CHKERRQ
38137adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
381481824310SBarry Smith #undef SETERRQ2
3815e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
381681824310SBarry Smith 
381781824310SBarry Smith EXTERN_C_BEGIN
381881824310SBarry Smith #undef __FUNCT__
381981824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
38201f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
382181824310SBarry Smith {
382281824310SBarry Smith   Mat            A = *AA;
382381824310SBarry Smith   PetscInt       m = *mm, n = *nn;
382481824310SBarry Smith   InsertMode     is = *isis;
382581824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
382681824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
382781824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
382881824310SBarry Smith   PetscErrorCode ierr;
382981824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
383054f21887SBarry Smith   MatScalar      *ap,value,*aa;
3831edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
383281824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
383381824310SBarry Smith 
383481824310SBarry Smith   PetscFunctionBegin;
3835d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
383681824310SBarry Smith   imax = a->imax;
383781824310SBarry Smith   ai = a->i;
383881824310SBarry Smith   ailen = a->ilen;
383981824310SBarry Smith   aj = a->j;
384081824310SBarry Smith   aa = a->a;
384181824310SBarry Smith 
384281824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
384381824310SBarry Smith     row  = im[k];
384481824310SBarry Smith     if (row < 0) continue;
384581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3846d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
384781824310SBarry Smith #endif
384881824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
384981824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
385081824310SBarry Smith     low  = 0;
385181824310SBarry Smith     high = nrow;
385281824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
385381824310SBarry Smith       if (in[l] < 0) continue;
385481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3855d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
385681824310SBarry Smith #endif
385781824310SBarry Smith       col = in[l];
385881824310SBarry Smith       if (roworiented) {
385981824310SBarry Smith         value = v[l + k*n];
386081824310SBarry Smith       } else {
386181824310SBarry Smith         value = v[k + l*m];
386281824310SBarry Smith       }
386381824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
386481824310SBarry Smith 
386581824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
386681824310SBarry Smith       lastcol = col;
386781824310SBarry Smith       while (high-low > 5) {
386881824310SBarry Smith         t = (low+high)/2;
386981824310SBarry Smith         if (rp[t] > col) high = t;
387081824310SBarry Smith         else             low  = t;
387181824310SBarry Smith       }
387281824310SBarry Smith       for (i=low; i<high; i++) {
387381824310SBarry Smith         if (rp[i] > col) break;
387481824310SBarry Smith         if (rp[i] == col) {
387581824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
387681824310SBarry Smith           else                  ap[i] = value;
387781824310SBarry Smith           goto noinsert;
387881824310SBarry Smith         }
387981824310SBarry Smith       }
388081824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
388181824310SBarry Smith       if (nonew == 1) goto noinsert;
38827adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3883*fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
388481824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
388581824310SBarry Smith       /* shift up all the later entries in this row */
388681824310SBarry Smith       for (ii=N; ii>=i; ii--) {
388781824310SBarry Smith         rp[ii+1] = rp[ii];
388881824310SBarry Smith         ap[ii+1] = ap[ii];
388981824310SBarry Smith       }
389081824310SBarry Smith       rp[i] = col;
389181824310SBarry Smith       ap[i] = value;
389281824310SBarry Smith       noinsert:;
389381824310SBarry Smith       low = i + 1;
389481824310SBarry Smith     }
389581824310SBarry Smith     ailen[row] = nrow;
389681824310SBarry Smith   }
389781824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
389881824310SBarry Smith   PetscFunctionReturnVoid();
389981824310SBarry Smith }
390081824310SBarry Smith EXTERN_C_END
3901