xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 97b48c8fcf5c67c401c12a3c5f8807c301cc162b)
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;
23ace3abfcSBarry Smith   PetscBool      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"
50ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *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"
80ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *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"
95ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *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"
137ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *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;
173ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
174ace3abfcSBarry Smith   PetscBool      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);
224fef13f97SBarry 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;
329b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33071c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33197f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
332d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
333d00d2cf4SBarry Smith       nofinalvalue = 1;
334d00d2cf4SBarry Smith     }
335b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
336d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
33777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
33877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
339b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34017ab2063SBarry Smith 
34117ab2063SBarry Smith     for (i=0; i<m; i++) {
342416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
343aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34477431f27SBarry 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);
34517ab2063SBarry Smith #else
34677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34717ab2063SBarry Smith #endif
34817ab2063SBarry Smith       }
34917ab2063SBarry Smith     }
350d00d2cf4SBarry Smith     if (nofinalvalue) {
351d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
352d00d2cf4SBarry Smith     }
353317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
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);
3607566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
36144cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36344cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
364aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36536db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
366a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36736db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
368a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36936db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
370a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3716831982aSBarry Smith         }
37244cd7ae7SLois Curfman McInnes #else
373a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37444cd7ae7SLois Curfman McInnes #endif
37544cd7ae7SLois Curfman McInnes       }
376b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37744cd7ae7SLois Curfman McInnes     }
378b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
379fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
38097f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
381b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
3827566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
38397f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
384496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
385496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
386496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
387496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
388aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
390496be53dSLois Curfman McInnes #else
391496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
392496be53dSLois Curfman McInnes #endif
393496be53dSLois Curfman McInnes         }
394496be53dSLois Curfman McInnes       }
395496be53dSLois Curfman McInnes     }
3962e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3982e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39977431f27SBarry 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);}
40077431f27SBarry 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);}
40177431f27SBarry 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);}
40277431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40377431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40477431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
405496be53dSLois Curfman McInnes     }
406b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
407606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
408496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
409496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
41077431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
411496be53dSLois Curfman McInnes       }
412b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
413496be53dSLois Curfman McInnes     }
414b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
415496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
416496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
417496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
418aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
420b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4216831982aSBarry Smith           }
422496be53dSLois Curfman McInnes #else
423b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
424496be53dSLois Curfman McInnes #endif
425496be53dSLois Curfman McInnes         }
426496be53dSLois Curfman McInnes       }
427b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
428496be53dSLois Curfman McInnes     }
429b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
430fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
43197f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
43287828ca2SBarry Smith     PetscScalar value;
43302594712SBarry Smith 
434b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4357566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
43602594712SBarry Smith     for (i=0; i<m; i++) {
43702594712SBarry Smith       jcnt = 0;
438d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
439e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
44002594712SBarry Smith           value = a->a[cnt++];
441e24b481bSBarry Smith           jcnt++;
44202594712SBarry Smith         } else {
44302594712SBarry Smith           value = 0.0;
44402594712SBarry Smith         }
445aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
446b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44702594712SBarry Smith #else
448b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44902594712SBarry Smith #endif
45002594712SBarry Smith       }
451b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45202594712SBarry Smith     }
453b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4543c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4553c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4567566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
4573c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4583c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4593c215bfdSMatthew Knepley #else
4603c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4613c215bfdSMatthew Knepley #endif
462d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4633c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4643c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4653c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4663c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4673c215bfdSMatthew 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);
4683c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
4693c215bfdSMatthew 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);
4703c215bfdSMatthew Knepley         } else {
4713c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4723c215bfdSMatthew Knepley         }
4733c215bfdSMatthew Knepley #else
4743c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4753c215bfdSMatthew Knepley #endif
4763c215bfdSMatthew Knepley       }
4773c215bfdSMatthew Knepley     }
4783c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4793a40ed3dSBarry Smith   } else {
480d6e78c31SSatish Balay     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4817566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
482d5f3da31SBarry Smith     if (A->factortype){
48316cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
48416cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
48516cd7e1dSShri Abhyankar         /* L part */
48616cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
48716cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
48816cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
48916cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
49016cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
49116cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
49216cd7e1dSShri Abhyankar           } else {
49316cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
49416cd7e1dSShri Abhyankar           }
49516cd7e1dSShri Abhyankar #else
49616cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49716cd7e1dSShri Abhyankar #endif
49816cd7e1dSShri Abhyankar         }
49916cd7e1dSShri Abhyankar 	/* diagonal */
50016cd7e1dSShri Abhyankar 	j = a->diag[i];
50116cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
50216cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
50316cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50416cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
50516cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50616cd7e1dSShri Abhyankar           } else {
50716cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
50816cd7e1dSShri Abhyankar           }
50916cd7e1dSShri Abhyankar #else
51016cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
51116cd7e1dSShri Abhyankar #endif
51216cd7e1dSShri Abhyankar 
51316cd7e1dSShri Abhyankar 	/* U part */
51416cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
51516cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
51616cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
51716cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51816cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
51916cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
52016cd7e1dSShri Abhyankar           } else {
52116cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
52216cd7e1dSShri Abhyankar           }
52316cd7e1dSShri Abhyankar #else
52416cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
52516cd7e1dSShri Abhyankar #endif
52616cd7e1dSShri Abhyankar }
52716cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
52816cd7e1dSShri Abhyankar         }
52916cd7e1dSShri Abhyankar     } else {
53017ab2063SBarry Smith       for (i=0; i<m; i++) {
53177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
532416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
533aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
53436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
535a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
53636db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
537a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5383a40ed3dSBarry Smith           } else {
539a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
54017ab2063SBarry Smith           }
54117ab2063SBarry Smith #else
542a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
54317ab2063SBarry Smith #endif
54417ab2063SBarry Smith         }
545b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
54617ab2063SBarry Smith       }
54716cd7e1dSShri Abhyankar     }
548b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
54917ab2063SBarry Smith   }
550b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5513a40ed3dSBarry Smith   PetscFunctionReturn(0);
552416022c9SBarry Smith }
553416022c9SBarry Smith 
5544a2ae208SSatish Balay #undef __FUNCT__
5554a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
556dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
557416022c9SBarry Smith {
558480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
559416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
560dfbe8321SBarry Smith   PetscErrorCode    ierr;
561d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
56236db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
563b0a32e0cSBarry Smith   PetscViewer       viewer;
564f3ef73ceSBarry Smith   PetscViewerFormat format;
565cddf8d76SBarry Smith 
5663a40ed3dSBarry Smith   PetscFunctionBegin;
567480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
568b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
56919bcc07fSBarry Smith 
570b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
571416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5720513a670SBarry Smith 
573fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5740513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
575b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
576416022c9SBarry Smith     for (i=0; i<m; i++) {
577cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
578bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
579bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
580aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
58136db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
582cddf8d76SBarry Smith #else
583cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
584cddf8d76SBarry Smith #endif
585b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
586cddf8d76SBarry Smith       }
587cddf8d76SBarry Smith     }
588b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
589cddf8d76SBarry Smith     for (i=0; i<m; i++) {
590cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
591bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
592bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
593cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
594b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
595cddf8d76SBarry Smith       }
596cddf8d76SBarry Smith     }
597b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
598cddf8d76SBarry Smith     for (i=0; i<m; i++) {
599cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
600bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
601bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
602aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
60336db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
604cddf8d76SBarry Smith #else
605cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
606cddf8d76SBarry Smith #endif
607b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
608416022c9SBarry Smith       }
609416022c9SBarry Smith     }
6100513a670SBarry Smith   } else {
6110513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
6120513a670SBarry Smith     /* first determine max of all nonzero values */
61397f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
614b0a32e0cSBarry Smith     PetscDraw   popup;
61536db0b34SBarry Smith     PetscReal scale;
6160513a670SBarry Smith 
6170513a670SBarry Smith     for (i=0; i<nz; i++) {
6180513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
6190513a670SBarry Smith     }
620b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
621b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
622b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
6230513a670SBarry Smith     count = 0;
6240513a670SBarry Smith     for (i=0; i<m; i++) {
6250513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
626bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
627bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
62897f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
629b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
6300513a670SBarry Smith         count++;
6310513a670SBarry Smith       }
6320513a670SBarry Smith     }
6330513a670SBarry Smith   }
634480ef9eaSBarry Smith   PetscFunctionReturn(0);
635480ef9eaSBarry Smith }
636cddf8d76SBarry Smith 
6374a2ae208SSatish Balay #undef __FUNCT__
6384a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
639dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
640480ef9eaSBarry Smith {
641dfbe8321SBarry Smith   PetscErrorCode ierr;
642b0a32e0cSBarry Smith   PetscDraw      draw;
64336db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
644ace3abfcSBarry Smith   PetscBool      isnull;
645480ef9eaSBarry Smith 
646480ef9eaSBarry Smith   PetscFunctionBegin;
647b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
648b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
649480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
650480ef9eaSBarry Smith 
651480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
652d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
653480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
654b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
655b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
656480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6573a40ed3dSBarry Smith   PetscFunctionReturn(0);
658416022c9SBarry Smith }
659416022c9SBarry Smith 
6604a2ae208SSatish Balay #undef __FUNCT__
6614a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
662dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
663416022c9SBarry Smith {
664dfbe8321SBarry Smith   PetscErrorCode ierr;
665ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
666416022c9SBarry Smith 
6673a40ed3dSBarry Smith   PetscFunctionBegin;
6682692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6692692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
6702692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
671c45a1595SBarry Smith   if (iascii) {
6723a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6730f5bd95cSBarry Smith   } else if (isbinary) {
6743a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6750f5bd95cSBarry Smith   } else if (isdraw) {
6763a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6775cd90555SBarry Smith   } else {
678e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
67917ab2063SBarry Smith   }
6804108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
6813a40ed3dSBarry Smith   PetscFunctionReturn(0);
68217ab2063SBarry Smith }
68319bcc07fSBarry Smith 
6844a2ae208SSatish Balay #undef __FUNCT__
6854a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
686dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
68717ab2063SBarry Smith {
688416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6896849ba73SBarry Smith   PetscErrorCode ierr;
69097f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
691d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
69254f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6933447b6efSHong Zhang   PetscReal      ratio=0.6;
69417ab2063SBarry Smith 
6953a40ed3dSBarry Smith   PetscFunctionBegin;
6963a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
69717ab2063SBarry Smith 
69843ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
69917ab2063SBarry Smith   for (i=1; i<m; i++) {
700416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
70117ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
70294a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
70317ab2063SBarry Smith     if (fshift) {
704bfeeae90SHong Zhang       ip = aj + ai[i] ;
705bfeeae90SHong Zhang       ap = aa + ai[i] ;
70617ab2063SBarry Smith       N  = ailen[i];
70717ab2063SBarry Smith       for (j=0; j<N; j++) {
70817ab2063SBarry Smith         ip[j-fshift] = ip[j];
70917ab2063SBarry Smith         ap[j-fshift] = ap[j];
71017ab2063SBarry Smith       }
71117ab2063SBarry Smith     }
71217ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
71317ab2063SBarry Smith   }
71417ab2063SBarry Smith   if (m) {
71517ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
71617ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
71717ab2063SBarry Smith   }
71817ab2063SBarry Smith   /* reset ilen and imax for each row */
71917ab2063SBarry Smith   for (i=0; i<m; i++) {
72017ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
72117ab2063SBarry Smith   }
722bfeeae90SHong Zhang   a->nz = ai[m];
72365e19b50SBarry 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);
72417ab2063SBarry Smith 
72509f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
726d0f46423SBarry 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);
727ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
728ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
7298e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
730dd5f02e7SSatish Balay   a->reallocs          = 0;
7314e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
73236db0b34SBarry Smith   a->rmax              = rmax;
7334e220ebcSLois Curfman McInnes 
734cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
735317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
73688e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
73771c2f376SKris Buschelman 
7384108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
73971f1c65dSBarry Smith 
74071f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
7413a40ed3dSBarry Smith   PetscFunctionReturn(0);
74217ab2063SBarry Smith }
74317ab2063SBarry Smith 
7444a2ae208SSatish Balay #undef __FUNCT__
74599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
74699cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
74799cafbc1SBarry Smith {
74899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
74999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
75054f21887SBarry Smith   MatScalar      *aa = a->a;
75199cafbc1SBarry Smith 
75299cafbc1SBarry Smith   PetscFunctionBegin;
75399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
75499cafbc1SBarry Smith   PetscFunctionReturn(0);
75599cafbc1SBarry Smith }
75699cafbc1SBarry Smith 
75799cafbc1SBarry Smith #undef __FUNCT__
75899cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
75999cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
76099cafbc1SBarry Smith {
76199cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
76299cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
76354f21887SBarry Smith   MatScalar      *aa = a->a;
76499cafbc1SBarry Smith 
76599cafbc1SBarry Smith   PetscFunctionBegin;
76699cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
76799cafbc1SBarry Smith   PetscFunctionReturn(0);
76899cafbc1SBarry Smith }
76999cafbc1SBarry Smith 
77099cafbc1SBarry Smith #undef __FUNCT__
7714a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
772dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
77317ab2063SBarry Smith {
774416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
775dfbe8321SBarry Smith   PetscErrorCode ierr;
7763a40ed3dSBarry Smith 
7773a40ed3dSBarry Smith   PetscFunctionBegin;
778d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7793a40ed3dSBarry Smith   PetscFunctionReturn(0);
78017ab2063SBarry Smith }
781416022c9SBarry Smith 
7824a2ae208SSatish Balay #undef __FUNCT__
7834a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
784dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
78517ab2063SBarry Smith {
786416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
787dfbe8321SBarry Smith   PetscErrorCode ierr;
788d5d45c9bSBarry Smith 
7893a40ed3dSBarry Smith   PetscFunctionBegin;
790aa482453SBarry Smith #if defined(PETSC_USE_LOG)
791d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
79217ab2063SBarry Smith #endif
793e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
794c38d4ed2SBarry Smith   if (a->row) {
795c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
796c38d4ed2SBarry Smith   }
797c38d4ed2SBarry Smith   if (a->col) {
798c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
799c38d4ed2SBarry Smith   }
80005b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
80105b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
80271f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
80305b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
80482bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
80505b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
806cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
80705b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
808407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
8090e83c824SBarry Smith   if (a->compressedrow.checked && a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);}
810a30b2313SHong Zhang 
8114108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
8124846f1f5SKris Buschelman 
813606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
814901853e0SKris Buschelman 
815dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
816901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
817901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
818901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
819901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
820901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
8215a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
822901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
823901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
824a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
825901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
8263a40ed3dSBarry Smith   PetscFunctionReturn(0);
82717ab2063SBarry Smith }
82817ab2063SBarry Smith 
8294a2ae208SSatish Balay #undef __FUNCT__
8304a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
831ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
83217ab2063SBarry Smith {
833416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8344846f1f5SKris Buschelman   PetscErrorCode ierr;
8353a40ed3dSBarry Smith 
8363a40ed3dSBarry Smith   PetscFunctionBegin;
837a65d3064SKris Buschelman   switch (op) {
838a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
8394e0d8c25SBarry Smith       a->roworiented       = flg;
840a65d3064SKris Buschelman       break;
841a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
842a9817697SBarry Smith       a->keepnonzeropattern    = flg;
843a65d3064SKris Buschelman       break;
844512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
845512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
846a65d3064SKris Buschelman       break;
847a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
8484e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
849a65d3064SKris Buschelman       break;
850a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8514e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
852a65d3064SKris Buschelman       break;
85328b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
85428b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
85528b2fa4aSMatthew Knepley       break;
856a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8574e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8580df259c2SBarry Smith       break;
859d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8604e0d8c25SBarry Smith       a->compressedrow.use = flg;
861d487561eSHong Zhang       break;
8623d472b54SHong Zhang     case MAT_SPD:
8633d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
8643d472b54SHong Zhang       A->spd                             = flg;
8653d472b54SHong Zhang       if (flg) {
8663d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
8673d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
8683d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
8693d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
8703d472b54SHong Zhang       }
8713d472b54SHong Zhang       break;
872b1646e73SJed Brown     case MAT_SYMMETRIC:
873b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
874b1646e73SJed Brown     case MAT_HERMITIAN:
875b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
8764e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
877a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
878a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
879290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
880a65d3064SKris Buschelman       break;
881b87ac2d8SJed Brown     case MAT_USE_INODES:
882b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
883b87ac2d8SJed Brown       break;
884a65d3064SKris Buschelman     default:
885e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
886a65d3064SKris Buschelman   }
8874108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
8883a40ed3dSBarry Smith   PetscFunctionReturn(0);
88917ab2063SBarry Smith }
89017ab2063SBarry Smith 
8914a2ae208SSatish Balay #undef __FUNCT__
8924a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
893dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
89417ab2063SBarry Smith {
895416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8966849ba73SBarry Smith   PetscErrorCode ierr;
897d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
89835e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
89917ab2063SBarry Smith 
9003a40ed3dSBarry Smith   PetscFunctionBegin;
901d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
902e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
90335e7444dSHong Zhang 
904d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
905d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
90635e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
90735e7444dSHong Zhang     for (i=0; i<n; i++) x[i] = aa[diag[i]];
90835e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
90935e7444dSHong Zhang     PetscFunctionReturn(0);
91035e7444dSHong Zhang   }
91135e7444dSHong Zhang 
9122dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
9131ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
91435e7444dSHong Zhang   for (i=0; i<n; i++) {
91535e7444dSHong Zhang     nz = ai[i+1] - ai[i];
9162f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
91735e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
91835e7444dSHong Zhang       if (aj[j] == i) {
91935e7444dSHong Zhang         x[i] = aa[j];
92017ab2063SBarry Smith         break;
92117ab2063SBarry Smith       }
92217ab2063SBarry Smith     }
92317ab2063SBarry Smith   }
9241ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
9253a40ed3dSBarry Smith   PetscFunctionReturn(0);
92617ab2063SBarry Smith }
92717ab2063SBarry Smith 
928b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9294a2ae208SSatish Balay #undef __FUNCT__
9304a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
931dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
93217ab2063SBarry Smith {
933416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
9345c897100SBarry Smith   PetscScalar       *x,*y;
935dfbe8321SBarry Smith   PetscErrorCode    ierr;
936d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
9375c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
938a77337e4SBarry Smith   MatScalar         *v;
939a77337e4SBarry Smith   PetscScalar       alpha;
94004fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
9413447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
942ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
9435c897100SBarry Smith #endif
94417ab2063SBarry Smith 
9453a40ed3dSBarry Smith   PetscFunctionBegin;
9462e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
9471ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9481ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9495c897100SBarry Smith 
9505c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
951bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
9525c897100SBarry Smith #else
9533447b6efSHong Zhang   if (usecprow){
9543447b6efSHong Zhang     m    = cprow.nrows;
9553447b6efSHong Zhang     ii   = cprow.i;
9567b2bb3b9SHong Zhang     ridx = cprow.rindex;
9573447b6efSHong Zhang   } else {
9583447b6efSHong Zhang     ii = a->i;
9593447b6efSHong Zhang   }
96017ab2063SBarry Smith   for (i=0; i<m; i++) {
9613447b6efSHong Zhang     idx   = a->j + ii[i] ;
9623447b6efSHong Zhang     v     = a->a + ii[i] ;
9633447b6efSHong Zhang     n     = ii[i+1] - ii[i];
9643447b6efSHong Zhang     if (usecprow){
9657b2bb3b9SHong Zhang       alpha = x[ridx[i]];
9663447b6efSHong Zhang     } else {
96717ab2063SBarry Smith       alpha = x[i];
9683447b6efSHong Zhang     }
96904fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
97017ab2063SBarry Smith   }
9715c897100SBarry Smith #endif
972dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
9731ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9741ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9753a40ed3dSBarry Smith   PetscFunctionReturn(0);
97617ab2063SBarry Smith }
97717ab2063SBarry Smith 
9784a2ae208SSatish Balay #undef __FUNCT__
9795c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
980dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9815c897100SBarry Smith {
982dfbe8321SBarry Smith   PetscErrorCode ierr;
9835c897100SBarry Smith 
9845c897100SBarry Smith   PetscFunctionBegin;
985170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9865c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9875c897100SBarry Smith   PetscFunctionReturn(0);
9885c897100SBarry Smith }
9895c897100SBarry Smith 
990a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9915c897100SBarry Smith #undef __FUNCT__
9924a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
993dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
99417ab2063SBarry Smith {
995416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
996d9fead3dSBarry Smith   PetscScalar       *y;
99754f21887SBarry Smith   const PetscScalar *x;
99854f21887SBarry Smith   const MatScalar   *aa;
999dfbe8321SBarry Smith   PetscErrorCode    ierr;
1000003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1001003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
10028aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1003362ced78SSatish Balay   PetscScalar       sum;
1004ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
100517ab2063SBarry Smith 
1006b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
100797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1008fee21e36SBarry Smith #endif
1009fee21e36SBarry Smith 
10103a40ed3dSBarry Smith   PetscFunctionBegin;
10113649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
10121ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
101397952fefSHong Zhang   aj  = a->j;
101497952fefSHong Zhang   aa  = a->a;
1015416022c9SBarry Smith   ii  = a->i;
10164eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
101797952fefSHong Zhang     m    = a->compressedrow.nrows;
101897952fefSHong Zhang     ii   = a->compressedrow.i;
101997952fefSHong Zhang     ridx = a->compressedrow.rindex;
102097952fefSHong Zhang     for (i=0; i<m; i++){
102197952fefSHong Zhang       n   = ii[i+1] - ii[i];
102297952fefSHong Zhang       aj  = a->j + ii[i];
102397952fefSHong Zhang       aa  = a->a + ii[i];
102497952fefSHong Zhang       sum = 0.0;
1025a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1026003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1027003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
102897952fefSHong Zhang       y[*ridx++] = sum;
102997952fefSHong Zhang     }
103097952fefSHong Zhang   } else { /* do not use compressed row format */
1031b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1032b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1033b05257ddSBarry Smith #else
103417ab2063SBarry Smith     for (i=0; i<m; i++) {
1035003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1036003131ecSBarry Smith       aj  = a->j + ii[i];
1037003131ecSBarry Smith       aa  = a->a + ii[i];
103817ab2063SBarry Smith       sum  = 0.0;
1039a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1040003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
104117ab2063SBarry Smith       y[i] = sum;
104217ab2063SBarry Smith     }
10438d195f9aSBarry Smith #endif
1044b05257ddSBarry Smith   }
1045dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
10463649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
10471ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10483a40ed3dSBarry Smith   PetscFunctionReturn(0);
104917ab2063SBarry Smith }
105017ab2063SBarry Smith 
1051a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
10524a2ae208SSatish Balay #undef __FUNCT__
10534a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1054dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
105517ab2063SBarry Smith {
1056416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
105754f21887SBarry Smith   PetscScalar     *x,*y,*z;
105854f21887SBarry Smith   const MatScalar *aa;
1059dfbe8321SBarry Smith   PetscErrorCode  ierr;
1060d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1061aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
106297952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1063362ced78SSatish Balay   PetscScalar     sum;
1064ace3abfcSBarry Smith   PetscBool       usecprow=a->compressedrow.use;
1065e36a17ebSSatish Balay #endif
10669ea0dfa2SSatish Balay 
10673a40ed3dSBarry Smith   PetscFunctionBegin;
10681ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10691ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10702e8a6d31SBarry Smith   if (zz != yy) {
10711ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
10722e8a6d31SBarry Smith   } else {
10732e8a6d31SBarry Smith     z = y;
10742e8a6d31SBarry Smith   }
1075bfeeae90SHong Zhang 
107697952fefSHong Zhang   aj  = a->j;
107797952fefSHong Zhang   aa  = a->a;
1078cddf8d76SBarry Smith   ii  = a->i;
1079aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
108097952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
108102ab625aSSatish Balay #else
10824eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10834eb6d288SHong Zhang     if (zz != yy){
10844eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10854eb6d288SHong Zhang     }
108697952fefSHong Zhang     m    = a->compressedrow.nrows;
108797952fefSHong Zhang     ii   = a->compressedrow.i;
108897952fefSHong Zhang     ridx = a->compressedrow.rindex;
108997952fefSHong Zhang     for (i=0; i<m; i++){
109097952fefSHong Zhang       n  = ii[i+1] - ii[i];
109197952fefSHong Zhang       aj  = a->j + ii[i];
109297952fefSHong Zhang       aa  = a->a + ii[i];
109397952fefSHong Zhang       sum = y[*ridx];
109497952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
109597952fefSHong Zhang       z[*ridx++] = sum;
109697952fefSHong Zhang     }
109797952fefSHong Zhang   } else { /* do not use compressed row format */
109817ab2063SBarry Smith     for (i=0; i<m; i++) {
10999ea0dfa2SSatish Balay       jrow = ii[i];
11009ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
110117ab2063SBarry Smith       sum  = y[i];
11029ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
110397952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
11049ea0dfa2SSatish Balay       }
110517ab2063SBarry Smith       z[i] = sum;
110617ab2063SBarry Smith     }
110797952fefSHong Zhang   }
110802ab625aSSatish Balay #endif
1109dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11101ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11111ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11122e8a6d31SBarry Smith   if (zz != yy) {
11131ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
11142e8a6d31SBarry Smith   }
1115918e98c3SVictor Minden #if defined(PETSC_HAVE_CUDA)
11166b375ea7SVictor Minden   /*
1117918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1118918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1119918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
11206b375ea7SVictor Minden   */
1121918e98c3SVictor Minden #endif
11223a40ed3dSBarry Smith   PetscFunctionReturn(0);
112317ab2063SBarry Smith }
112417ab2063SBarry Smith 
112517ab2063SBarry Smith /*
112617ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
112717ab2063SBarry Smith */
11284a2ae208SSatish Balay #undef __FUNCT__
11294a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1130dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
113117ab2063SBarry Smith {
1132416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11336849ba73SBarry Smith   PetscErrorCode ierr;
1134d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
113517ab2063SBarry Smith 
11363a40ed3dSBarry Smith   PetscFunctionBegin;
113709f38230SBarry Smith   if (!a->diag) {
113809f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
11399518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
114009f38230SBarry Smith   }
1141d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
114209f38230SBarry Smith     a->diag[i] = a->i[i+1];
1143bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1144bfeeae90SHong Zhang       if (a->j[j] == i) {
114509f38230SBarry Smith         a->diag[i] = j;
114617ab2063SBarry Smith         break;
114717ab2063SBarry Smith       }
114817ab2063SBarry Smith     }
114917ab2063SBarry Smith   }
11503a40ed3dSBarry Smith   PetscFunctionReturn(0);
115117ab2063SBarry Smith }
115217ab2063SBarry Smith 
1153be5855fcSBarry Smith /*
1154be5855fcSBarry Smith      Checks for missing diagonals
1155be5855fcSBarry Smith */
11564a2ae208SSatish Balay #undef __FUNCT__
11574a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1158ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1159be5855fcSBarry Smith {
1160be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
116197f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1162be5855fcSBarry Smith 
1163be5855fcSBarry Smith   PetscFunctionBegin;
116409f38230SBarry Smith   *missing = PETSC_FALSE;
1165d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
116609f38230SBarry Smith     *missing  = PETSC_TRUE;
116709f38230SBarry Smith     if (d) *d = 0;
116809f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
116909f38230SBarry Smith   } else {
1170f1e2ffcdSBarry Smith     diag = a->diag;
1171d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1172bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
117309f38230SBarry Smith 	*missing = PETSC_TRUE;
117409f38230SBarry Smith 	if (d) *d = i;
117509f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
117609f38230SBarry Smith       }
1177be5855fcSBarry Smith     }
1178be5855fcSBarry Smith   }
1179be5855fcSBarry Smith   PetscFunctionReturn(0);
1180be5855fcSBarry Smith }
1181be5855fcSBarry Smith 
118271f1c65dSBarry Smith EXTERN_C_BEGIN
118371f1c65dSBarry Smith #undef __FUNCT__
118471f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
118571f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
118671f1c65dSBarry Smith {
118771f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
118871f1c65dSBarry Smith   PetscErrorCode ierr;
1189d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
119054f21887SBarry Smith   MatScalar      *v = a->a;
119154f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
119271f1c65dSBarry Smith 
119371f1c65dSBarry Smith   PetscFunctionBegin;
119471f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
119571f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
119671f1c65dSBarry Smith   diag = a->diag;
119771f1c65dSBarry Smith   if (!a->idiag) {
119871f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
119971f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
120071f1c65dSBarry Smith     v        = a->a;
120171f1c65dSBarry Smith   }
120271f1c65dSBarry Smith   mdiag = a->mdiag;
120371f1c65dSBarry Smith   idiag = a->idiag;
120471f1c65dSBarry Smith 
1205028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
120671f1c65dSBarry Smith     for (i=0; i<m; i++) {
120771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1208e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
120971f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
121071f1c65dSBarry Smith     }
121171f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
121271f1c65dSBarry Smith   } else {
121371f1c65dSBarry Smith     for (i=0; i<m; i++) {
121471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
121571f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
121671f1c65dSBarry Smith     }
1217dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
121871f1c65dSBarry Smith   }
121971f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
122071f1c65dSBarry Smith   PetscFunctionReturn(0);
122171f1c65dSBarry Smith }
12225a9745a3SMatthew Knepley EXTERN_C_END
122371f1c65dSBarry Smith 
1224a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
12254a2ae208SSatish Balay #undef __FUNCT__
122641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
122741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
122817ab2063SBarry Smith {
1229416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1230e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1231e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
123254f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1233dfbe8321SBarry Smith   PetscErrorCode     ierr;
1234d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
123597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
123617ab2063SBarry Smith 
12373a40ed3dSBarry Smith   PetscFunctionBegin;
1238b965ef7fSBarry Smith   its = its*lits;
123991723122SBarry Smith 
124071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
124171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
124271f1c65dSBarry Smith   a->fshift = fshift;
124371f1c65dSBarry Smith   a->omega  = omega;
1244ed480e8bSBarry Smith 
124571f1c65dSBarry Smith   diag = a->diag;
124671f1c65dSBarry Smith   t     = a->ssor_work;
1247ed480e8bSBarry Smith   idiag = a->idiag;
124871f1c65dSBarry Smith   mdiag = a->mdiag;
1249ed480e8bSBarry Smith 
12501ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
12513649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
125271f1c65dSBarry Smith   CHKMEMQ;
1253ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
125417ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
125517ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1256ed480e8bSBarry Smith     bs = b;
125717ab2063SBarry Smith     for (i=0; i<m; i++) {
125871f1c65dSBarry Smith         d    = fshift + mdiag[i];
1259416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1260ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1261ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
126217ab2063SBarry Smith         sum  = b[i]*d/omega;
1263003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
126417ab2063SBarry Smith         x[i] = sum;
126517ab2063SBarry Smith     }
12661ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12673649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1268efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12693a40ed3dSBarry Smith     PetscFunctionReturn(0);
127017ab2063SBarry Smith   }
1271c783ea89SBarry Smith 
127248af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1273e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
12743a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
127517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1276887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
127717ab2063SBarry Smith 
127817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
127917ab2063SBarry Smith 
1280887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
128117ab2063SBarry Smith     */
128217ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
128317ab2063SBarry Smith 
128417ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
128517ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1286416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1287ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1288ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
128917ab2063SBarry Smith       sum  = b[i];
1290e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1291ed480e8bSBarry Smith       x[i] = sum*idiag[i];
129217ab2063SBarry Smith     }
129317ab2063SBarry Smith 
129417ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1295416022c9SBarry Smith     v = a->a;
1296ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
129717ab2063SBarry Smith 
129817ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1299ed480e8bSBarry Smith     ts = t;
1300416022c9SBarry Smith     diag = a->diag;
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  = t[i];
1306003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1307ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1308733d66baSBarry Smith       /*  x = x + t */
1309733d66baSBarry Smith       x[i] += t[i];
131017ab2063SBarry Smith     }
131117ab2063SBarry Smith 
1312dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
13131ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13143649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
13153a40ed3dSBarry Smith     PetscFunctionReturn(0);
131617ab2063SBarry Smith   }
131717ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
131817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
131917ab2063SBarry Smith       for (i=0; i<m; i++) {
1320416022c9SBarry Smith         n    = diag[i] - a->i[i];
1321ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1322ed480e8bSBarry Smith         v    = a->a + a->i[i];
132317ab2063SBarry Smith         sum  = b[i];
1324e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13255c99c7daSBarry Smith         t[i] = sum;
1326ed480e8bSBarry Smith         x[i] = sum*idiag[i];
132717ab2063SBarry Smith       }
13285c99c7daSBarry Smith       xb = t;
1329efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13303a40ed3dSBarry Smith     } else xb = b;
133117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
133217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1333416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1334ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1335ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
133617ab2063SBarry Smith         sum  = xb[i];
1337e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13385c99c7daSBarry Smith         if (xb == b) {
1339ed480e8bSBarry Smith           x[i] = sum*idiag[i];
13405c99c7daSBarry Smith         } else {
13415c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
134217ab2063SBarry Smith         }
13435c99c7daSBarry Smith       }
1344efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
134517ab2063SBarry Smith     }
134617ab2063SBarry Smith     its--;
134717ab2063SBarry Smith   }
134817ab2063SBarry Smith   while (its--) {
134917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
135017ab2063SBarry Smith       for (i=0; i<m; i++) {
1351416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1352ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1353ed480e8bSBarry Smith         v    = a->a + a->i[i];
135417ab2063SBarry Smith         sum  = b[i];
1355e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1356ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
135717ab2063SBarry Smith       }
13589f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
135917ab2063SBarry Smith     }
136017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
136117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1362416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1363ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1364ed480e8bSBarry Smith         v    = a->a + a->i[i];
136517ab2063SBarry Smith         sum  = b[i];
1366e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1367ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
136817ab2063SBarry Smith       }
13699f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
137017ab2063SBarry Smith     }
137117ab2063SBarry Smith   }
13721ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13733649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
137471f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
137517ab2063SBarry Smith }
137617ab2063SBarry Smith 
13772af78befSBarry Smith 
13784a2ae208SSatish Balay #undef __FUNCT__
13794a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1380dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
138117ab2063SBarry Smith {
1382416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13834e220ebcSLois Curfman McInnes 
13843a40ed3dSBarry Smith   PetscFunctionBegin;
13854e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13864e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13874e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13884e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13894e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13908e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
13917adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1392d5f3da31SBarry Smith   if (A->factortype) {
13934e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13944e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13954e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13964e220ebcSLois Curfman McInnes   } else {
13974e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13984e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13994e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
14004e220ebcSLois Curfman McInnes   }
14013a40ed3dSBarry Smith   PetscFunctionReturn(0);
140217ab2063SBarry Smith }
140317ab2063SBarry Smith 
14044a2ae208SSatish Balay #undef __FUNCT__
14054a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
14062b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
140717ab2063SBarry Smith {
1408416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
14093b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
14106849ba73SBarry Smith   PetscErrorCode    ierr;
1411*97b48c8fSBarry Smith   const PetscScalar *xx;
1412*97b48c8fSBarry Smith   PetscScalar       *bb;
1413ace3abfcSBarry Smith   PetscBool         missing;
141417ab2063SBarry Smith 
14153a40ed3dSBarry Smith   PetscFunctionBegin;
1416*97b48c8fSBarry Smith   if (x && b) {
1417*97b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
1418*97b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
1419*97b48c8fSBarry Smith     for (i=0; i<N; i++) {
1420*97b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1421*97b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
1422*97b48c8fSBarry Smith     }
1423*97b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
1424*97b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
1425*97b48c8fSBarry Smith   }
1426*97b48c8fSBarry Smith 
1427a9817697SBarry Smith   if (a->keepnonzeropattern) {
1428f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1429e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1430bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1431f1e2ffcdSBarry Smith     }
1432f4df32b1SMatthew Knepley     if (diag != 0.0) {
143309f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1434e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1435f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1436f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1437f1e2ffcdSBarry Smith       }
1438f1e2ffcdSBarry Smith     }
143988e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1440f1e2ffcdSBarry Smith   } else {
1441f4df32b1SMatthew Knepley     if (diag != 0.0) {
144217ab2063SBarry Smith       for (i=0; i<N; i++) {
1443e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
14447ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1445416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1446f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1447bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
14487ae801bdSBarry Smith         } else { /* in case row was completely empty */
1449f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
145017ab2063SBarry Smith         }
145117ab2063SBarry Smith       }
14523a40ed3dSBarry Smith     } else {
145317ab2063SBarry Smith       for (i=0; i<N; i++) {
1454e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1455416022c9SBarry Smith         a->ilen[rows[i]] = 0;
145617ab2063SBarry Smith       }
145717ab2063SBarry Smith     }
145888e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1459f1e2ffcdSBarry Smith   }
146043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14613a40ed3dSBarry Smith   PetscFunctionReturn(0);
146217ab2063SBarry Smith }
146317ab2063SBarry Smith 
14644a2ae208SSatish Balay #undef __FUNCT__
14656e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
14666e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
14676e169961SBarry Smith {
14686e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
14696e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
14706e169961SBarry Smith   PetscErrorCode    ierr;
14712b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
14726e169961SBarry Smith   const PetscScalar *xx;
14736e169961SBarry Smith   PetscScalar       *bb;
14746e169961SBarry Smith 
14756e169961SBarry Smith   PetscFunctionBegin;
14766e169961SBarry Smith   if (x && b) {
14776e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
14786e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
14792b40b63fSBarry Smith     vecs = PETSC_TRUE;
14806e169961SBarry Smith   }
14816e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
14826e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
14836e169961SBarry Smith   for (i=0; i<N; i++) {
14846e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
14856e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
14866e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
14876e169961SBarry Smith   }
14886e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
14896e169961SBarry Smith     if (!zeroed[i]) {
14906e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
14916e169961SBarry Smith         if (zeroed[a->j[j]]) {
14922b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
14936e169961SBarry Smith           a->a[j] = 0.0;
14946e169961SBarry Smith         }
14956e169961SBarry Smith       }
14962b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
14976e169961SBarry Smith   }
14986e169961SBarry Smith   if (x && b) {
14996e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
15006e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
15016e169961SBarry Smith   }
15026e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
15036e169961SBarry Smith   if (diag != 0.0) {
15046e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
15056e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
15066e169961SBarry Smith     for (i=0; i<N; i++) {
15076e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
15086e169961SBarry Smith     }
15096e169961SBarry Smith   }
15106e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
15116e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15126e169961SBarry Smith   PetscFunctionReturn(0);
15136e169961SBarry Smith }
15146e169961SBarry Smith 
15156e169961SBarry Smith #undef __FUNCT__
15164a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1517a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
151817ab2063SBarry Smith {
1519416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
152097f1f81fSBarry Smith   PetscInt   *itmp;
152117ab2063SBarry Smith 
15223a40ed3dSBarry Smith   PetscFunctionBegin;
1523e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
152417ab2063SBarry Smith 
1525416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1526bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
152717ab2063SBarry Smith   if (idx) {
1528bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1529bfeeae90SHong Zhang     if (*nz) {
15304e093b46SBarry Smith       *idx = itmp;
153117ab2063SBarry Smith     }
153217ab2063SBarry Smith     else *idx = 0;
153317ab2063SBarry Smith   }
15343a40ed3dSBarry Smith   PetscFunctionReturn(0);
153517ab2063SBarry Smith }
153617ab2063SBarry Smith 
1537bfeeae90SHong Zhang /* remove this function? */
15384a2ae208SSatish Balay #undef __FUNCT__
15394a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1540a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
154117ab2063SBarry Smith {
15423a40ed3dSBarry Smith   PetscFunctionBegin;
15433a40ed3dSBarry Smith   PetscFunctionReturn(0);
154417ab2063SBarry Smith }
154517ab2063SBarry Smith 
15464a2ae208SSatish Balay #undef __FUNCT__
15474a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1548dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
154917ab2063SBarry Smith {
1550416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
155154f21887SBarry Smith   MatScalar      *v = a->a;
155236db0b34SBarry Smith   PetscReal      sum = 0.0;
15536849ba73SBarry Smith   PetscErrorCode ierr;
155497f1f81fSBarry Smith   PetscInt       i,j;
155517ab2063SBarry Smith 
15563a40ed3dSBarry Smith   PetscFunctionBegin;
155717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1558416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1559aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
156036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
156117ab2063SBarry Smith #else
156217ab2063SBarry Smith       sum += (*v)*(*v); v++;
156317ab2063SBarry Smith #endif
156417ab2063SBarry Smith     }
1565064f8208SBarry Smith     *nrm = sqrt(sum);
15663a40ed3dSBarry Smith   } else if (type == NORM_1) {
156736db0b34SBarry Smith     PetscReal *tmp;
156897f1f81fSBarry Smith     PetscInt    *jj = a->j;
1569d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1570d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1571064f8208SBarry Smith     *nrm = 0.0;
1572416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1573bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
157417ab2063SBarry Smith     }
1575d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1576064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
157717ab2063SBarry Smith     }
1578606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
15793a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1580064f8208SBarry Smith     *nrm = 0.0;
1581d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1582bfeeae90SHong Zhang       v = a->a + a->i[j];
158317ab2063SBarry Smith       sum = 0.0;
1584416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1585cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
158617ab2063SBarry Smith       }
1587064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
158817ab2063SBarry Smith     }
15893a40ed3dSBarry Smith   } else {
1590e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
159117ab2063SBarry Smith   }
15923a40ed3dSBarry Smith   PetscFunctionReturn(0);
159317ab2063SBarry Smith }
159417ab2063SBarry Smith 
15954a2ae208SSatish Balay #undef __FUNCT__
15964a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1597fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
159817ab2063SBarry Smith {
1599416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1600416022c9SBarry Smith   Mat            C;
16016849ba73SBarry Smith   PetscErrorCode ierr;
1602d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
160354f21887SBarry Smith   MatScalar      *array = a->a;
160417ab2063SBarry Smith 
16053a40ed3dSBarry Smith   PetscFunctionBegin;
1606e32f2f54SBarry 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");
1607fc4dec0aSBarry Smith 
1608fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1609d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1610d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1611bfeeae90SHong Zhang 
1612bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
16137adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1614d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
16157adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1616ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1617606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1618a541d17aSBarry Smith   } else {
1619a541d17aSBarry Smith     C = *B;
1620a541d17aSBarry Smith   }
1621a541d17aSBarry Smith 
162217ab2063SBarry Smith   for (i=0; i<m; i++) {
162317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
162487d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1625b9b97703SBarry Smith     array += len;
1626b9b97703SBarry Smith     aj    += len;
162717ab2063SBarry Smith   }
16286d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16296d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
163017ab2063SBarry Smith 
1631815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1632416022c9SBarry Smith     *B = C;
163317ab2063SBarry Smith   } else {
1634eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
163517ab2063SBarry Smith   }
16363a40ed3dSBarry Smith   PetscFunctionReturn(0);
163717ab2063SBarry Smith }
163817ab2063SBarry Smith 
1639cd0d46ebSvictorle EXTERN_C_BEGIN
1640cd0d46ebSvictorle #undef __FUNCT__
16415fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1642ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1643cd0d46ebSvictorle {
1644cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
164554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
164654f21887SBarry Smith   MatScalar      *va,*vb;
16476849ba73SBarry Smith   PetscErrorCode ierr;
164897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1649cd0d46ebSvictorle 
1650cd0d46ebSvictorle   PetscFunctionBegin;
1651cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1652cd0d46ebSvictorle 
1653cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1654cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
16555485867bSBarry Smith   if (ma!=nb || na!=mb){
16565485867bSBarry Smith     *f = PETSC_FALSE;
16575485867bSBarry Smith     PetscFunctionReturn(0);
16585485867bSBarry Smith   }
1659cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1660cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1661cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
166297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
166397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1664cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1665cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1666cd0d46ebSvictorle 
1667cd0d46ebSvictorle   *f = PETSC_TRUE;
1668cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1669cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
167097f1f81fSBarry Smith       PetscInt         idc,idr;
16715485867bSBarry Smith       PetscScalar vc,vr;
1672cd0d46ebSvictorle       /* column/row index/value */
16735485867bSBarry Smith       idc = adx[aptr[i]];
16745485867bSBarry Smith       idr = bdx[bptr[idc]];
16755485867bSBarry Smith       vc  = va[aptr[i]];
16765485867bSBarry Smith       vr  = vb[bptr[idc]];
16775485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
16785485867bSBarry Smith 	*f = PETSC_FALSE;
16795485867bSBarry Smith         goto done;
1680cd0d46ebSvictorle       } else {
16815485867bSBarry Smith 	aptr[i]++;
16825485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1683cd0d46ebSvictorle       }
1684cd0d46ebSvictorle     }
1685cd0d46ebSvictorle   }
1686cd0d46ebSvictorle  done:
1687cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
16883aeef889SHong Zhang   if (B) {
16893aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
16903aeef889SHong Zhang   }
1691cd0d46ebSvictorle   PetscFunctionReturn(0);
1692cd0d46ebSvictorle }
1693cd0d46ebSvictorle EXTERN_C_END
1694cd0d46ebSvictorle 
16951cbb95d3SBarry Smith EXTERN_C_BEGIN
16961cbb95d3SBarry Smith #undef __FUNCT__
16971cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
1698ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
16991cbb95d3SBarry Smith {
17001cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
170154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
170254f21887SBarry Smith   MatScalar      *va,*vb;
17031cbb95d3SBarry Smith   PetscErrorCode ierr;
17041cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
17051cbb95d3SBarry Smith 
17061cbb95d3SBarry Smith   PetscFunctionBegin;
17071cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
17081cbb95d3SBarry Smith 
17091cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
17101cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
17111cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
17121cbb95d3SBarry Smith     *f = PETSC_FALSE;
17131cbb95d3SBarry Smith     PetscFunctionReturn(0);
17141cbb95d3SBarry Smith   }
17151cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
17161cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
17171cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
17181cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
17191cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
17201cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
17211cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
17221cbb95d3SBarry Smith 
17231cbb95d3SBarry Smith   *f = PETSC_TRUE;
17241cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
17251cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
17261cbb95d3SBarry Smith       PetscInt         idc,idr;
17271cbb95d3SBarry Smith       PetscScalar vc,vr;
17281cbb95d3SBarry Smith       /* column/row index/value */
17291cbb95d3SBarry Smith       idc = adx[aptr[i]];
17301cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
17311cbb95d3SBarry Smith       vc  = va[aptr[i]];
17321cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
17331cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
17341cbb95d3SBarry Smith 	*f = PETSC_FALSE;
17351cbb95d3SBarry Smith         goto done;
17361cbb95d3SBarry Smith       } else {
17371cbb95d3SBarry Smith 	aptr[i]++;
17381cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
17391cbb95d3SBarry Smith       }
17401cbb95d3SBarry Smith     }
17411cbb95d3SBarry Smith   }
17421cbb95d3SBarry Smith  done:
17431cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
17441cbb95d3SBarry Smith   if (B) {
17451cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
17461cbb95d3SBarry Smith   }
17471cbb95d3SBarry Smith   PetscFunctionReturn(0);
17481cbb95d3SBarry Smith }
17491cbb95d3SBarry Smith EXTERN_C_END
17501cbb95d3SBarry Smith 
17519e29f15eSvictorle #undef __FUNCT__
17529e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1753ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
17549e29f15eSvictorle {
1755dfbe8321SBarry Smith   PetscErrorCode ierr;
17569e29f15eSvictorle   PetscFunctionBegin;
17575485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
17589e29f15eSvictorle   PetscFunctionReturn(0);
17599e29f15eSvictorle }
17609e29f15eSvictorle 
17614a2ae208SSatish Balay #undef __FUNCT__
17621cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
1763ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
17641cbb95d3SBarry Smith {
17651cbb95d3SBarry Smith   PetscErrorCode ierr;
17661cbb95d3SBarry Smith   PetscFunctionBegin;
17671cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
17681cbb95d3SBarry Smith   PetscFunctionReturn(0);
17691cbb95d3SBarry Smith }
17701cbb95d3SBarry Smith 
17711cbb95d3SBarry Smith #undef __FUNCT__
17724a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1773dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
177417ab2063SBarry Smith {
1775416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
177654f21887SBarry Smith   PetscScalar    *l,*r,x;
177754f21887SBarry Smith   MatScalar      *v;
1778dfbe8321SBarry Smith   PetscErrorCode ierr;
1779d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
178017ab2063SBarry Smith 
17813a40ed3dSBarry Smith   PetscFunctionBegin;
178217ab2063SBarry Smith   if (ll) {
17833ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
17843ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1785e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1786e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
17871ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1788416022c9SBarry Smith     v = a->a;
178917ab2063SBarry Smith     for (i=0; i<m; i++) {
179017ab2063SBarry Smith       x = l[i];
1791416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
179217ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
179317ab2063SBarry Smith     }
17941ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1795efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
179617ab2063SBarry Smith   }
179717ab2063SBarry Smith   if (rr) {
1798e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1799e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
18001ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1801416022c9SBarry Smith     v = a->a; jj = a->j;
180217ab2063SBarry Smith     for (i=0; i<nz; i++) {
1803bfeeae90SHong Zhang       (*v++) *= r[*jj++];
180417ab2063SBarry Smith     }
18051ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1806efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
180717ab2063SBarry Smith   }
18083a40ed3dSBarry Smith   PetscFunctionReturn(0);
180917ab2063SBarry Smith }
181017ab2063SBarry Smith 
18114a2ae208SSatish Balay #undef __FUNCT__
18124a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
181397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
181417ab2063SBarry Smith {
1815db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
18166849ba73SBarry Smith   PetscErrorCode ierr;
1817d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
181897f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
18195d0c19d7SBarry Smith   const PetscInt *irow,*icol;
18205d0c19d7SBarry Smith   PetscInt       nrows,ncols;
182197f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
182254f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1823416022c9SBarry Smith   Mat            C;
1824ace3abfcSBarry Smith   PetscBool      stride,sorted;
182517ab2063SBarry Smith 
18263a40ed3dSBarry Smith   PetscFunctionBegin;
182714ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1828e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
182914ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1830e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
183199141d43SSatish Balay 
183217ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1833b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1834b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
183517ab2063SBarry Smith 
1836fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1837fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1838fee21e36SBarry Smith   if (stride && step == 1) {
183902834360SBarry Smith     /* special case of contiguous rows */
18400e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
184102834360SBarry Smith     /* loop over new rows determining lens and starting points */
184202834360SBarry Smith     for (i=0; i<nrows; i++) {
1843bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1844a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
184502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1846bfeeae90SHong Zhang         if (aj[k] >= first) {
184702834360SBarry Smith           starts[i] = k;
184802834360SBarry Smith           break;
184902834360SBarry Smith 	}
185002834360SBarry Smith       }
1851a2744918SBarry Smith       sum = 0;
185202834360SBarry Smith       while (k < kend) {
1853bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1854a2744918SBarry Smith         sum++;
185502834360SBarry Smith       }
1856a2744918SBarry Smith       lens[i] = sum;
185702834360SBarry Smith     }
185802834360SBarry Smith     /* create submatrix */
1859cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
186097f1f81fSBarry Smith       PetscInt n_cols,n_rows;
186108480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1862e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1863d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
186408480c60SBarry Smith       C = *B;
18653a40ed3dSBarry Smith     } else {
18667adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1867f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
18687adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1869ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
187008480c60SBarry Smith     }
1871db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1872db02288aSLois Curfman McInnes 
187302834360SBarry Smith     /* loop over rows inserting into submatrix */
1874db02288aSLois Curfman McInnes     a_new    = c->a;
1875db02288aSLois Curfman McInnes     j_new    = c->j;
1876db02288aSLois Curfman McInnes     i_new    = c->i;
1877bfeeae90SHong Zhang 
187802834360SBarry Smith     for (i=0; i<nrows; i++) {
1879a2744918SBarry Smith       ii    = starts[i];
1880a2744918SBarry Smith       lensi = lens[i];
1881a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1882a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
188302834360SBarry Smith       }
188487828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1885a2744918SBarry Smith       a_new      += lensi;
1886a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1887a2744918SBarry Smith       c->ilen[i]  = lensi;
188802834360SBarry Smith     }
18890e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
18903a40ed3dSBarry Smith   } else {
189102834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
18920e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
189397f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
18940e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
189517ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
189602834360SBarry Smith     /* determine lens of each row */
189702834360SBarry Smith     for (i=0; i<nrows; i++) {
1898bfeeae90SHong Zhang       kstart  = ai[irow[i]];
189902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
190002834360SBarry Smith       lens[i] = 0;
190102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1902bfeeae90SHong Zhang         if (smap[aj[k]]) {
190302834360SBarry Smith           lens[i]++;
190402834360SBarry Smith         }
190502834360SBarry Smith       }
190602834360SBarry Smith     }
190717ab2063SBarry Smith     /* Create and fill new matrix */
1908a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
1909ace3abfcSBarry Smith       PetscBool  equal;
19100f5bd95cSBarry Smith 
191199141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1912e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1913d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
19140f5bd95cSBarry Smith       if (!equal) {
1915e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
191699141d43SSatish Balay       }
1917d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
191808480c60SBarry Smith       C = *B;
19193a40ed3dSBarry Smith     } else {
19207adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1921f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
19227adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1923ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
192408480c60SBarry Smith     }
192599141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
192617ab2063SBarry Smith     for (i=0; i<nrows; i++) {
192799141d43SSatish Balay       row    = irow[i];
1928bfeeae90SHong Zhang       kstart = ai[row];
192999141d43SSatish Balay       kend   = kstart + a->ilen[row];
1930bfeeae90SHong Zhang       mat_i  = c->i[i];
193199141d43SSatish Balay       mat_j  = c->j + mat_i;
193299141d43SSatish Balay       mat_a  = c->a + mat_i;
193399141d43SSatish Balay       mat_ilen = c->ilen + i;
193417ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1935bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1936ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
193799141d43SSatish Balay           *mat_a++ = a->a[k];
193899141d43SSatish Balay           (*mat_ilen)++;
193999141d43SSatish Balay 
194017ab2063SBarry Smith         }
194117ab2063SBarry Smith       }
194217ab2063SBarry Smith     }
194302834360SBarry Smith     /* Free work space */
194402834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1945606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1946606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
194702834360SBarry Smith   }
19486d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19496d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
195017ab2063SBarry Smith 
195117ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1952416022c9SBarry Smith   *B = C;
19533a40ed3dSBarry Smith   PetscFunctionReturn(0);
195417ab2063SBarry Smith }
195517ab2063SBarry Smith 
19561df811f5SHong Zhang #undef __FUNCT__
195782d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
195882d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
195982d44351SHong Zhang {
196082d44351SHong Zhang   PetscErrorCode ierr;
196182d44351SHong Zhang   Mat            B;
196282d44351SHong Zhang 
196382d44351SHong Zhang   PetscFunctionBegin;
196482d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
196582d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
196682d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
196782d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
196882d44351SHong Zhang   *subMat = B;
196982d44351SHong Zhang   PetscFunctionReturn(0);
197082d44351SHong Zhang }
197182d44351SHong Zhang 
197282d44351SHong Zhang #undef __FUNCT__
19734a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
19740481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1975a871dcd8SBarry Smith {
197663b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1977dfbe8321SBarry Smith   PetscErrorCode ierr;
197863b91edcSBarry Smith   Mat            outA;
1979ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
198063b91edcSBarry Smith 
19813a40ed3dSBarry Smith   PetscFunctionBegin;
1982e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
19831df811f5SHong Zhang 
1984b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1985b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1986a871dcd8SBarry Smith 
198763b91edcSBarry Smith   outA              = inA;
1988d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
1989c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1990c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1991c3122656SLisandro Dalcin   a->row = row;
1992c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1993c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1994c3122656SLisandro Dalcin   a->col = col;
199563b91edcSBarry Smith 
199636db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1997b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
19984c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
199952e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2000f0ec6fceSSatish Balay 
200194a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2002d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2003d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
200494a9d846SBarry Smith   }
200563b91edcSBarry Smith 
2006f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2007137fb511SHong Zhang   if (row_identity && col_identity) {
2008ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2009137fb511SHong Zhang   } else {
2010719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2011137fb511SHong Zhang   }
20123a40ed3dSBarry Smith   PetscFunctionReturn(0);
2013a871dcd8SBarry Smith }
2014a871dcd8SBarry Smith 
20154a2ae208SSatish Balay #undef __FUNCT__
20164a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2017f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2018f0b747eeSBarry Smith {
2019f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2020f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2021efee365bSSatish Balay   PetscErrorCode ierr;
20220805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
20233a40ed3dSBarry Smith 
20243a40ed3dSBarry Smith   PetscFunctionBegin;
2025f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2026efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
20273a40ed3dSBarry Smith   PetscFunctionReturn(0);
2028f0b747eeSBarry Smith }
2029f0b747eeSBarry Smith 
20304a2ae208SSatish Balay #undef __FUNCT__
20314a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
203297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2033cddf8d76SBarry Smith {
2034dfbe8321SBarry Smith   PetscErrorCode ierr;
203597f1f81fSBarry Smith   PetscInt       i;
2036cddf8d76SBarry Smith 
20373a40ed3dSBarry Smith   PetscFunctionBegin;
2038cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2039b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2040cddf8d76SBarry Smith   }
2041cddf8d76SBarry Smith 
2042cddf8d76SBarry Smith   for (i=0; i<n; i++) {
20436a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2044cddf8d76SBarry Smith   }
20453a40ed3dSBarry Smith   PetscFunctionReturn(0);
2046cddf8d76SBarry Smith }
2047cddf8d76SBarry Smith 
20484a2ae208SSatish Balay #undef __FUNCT__
20494a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
205097f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
20514dcbc457SBarry Smith {
2052e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
20536849ba73SBarry Smith   PetscErrorCode ierr;
20545d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
20555d0c19d7SBarry Smith   const PetscInt *idx;
205697f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2057f1af5d2fSBarry Smith   PetscBT        table;
2058bbd702dbSSatish Balay 
20593a40ed3dSBarry Smith   PetscFunctionBegin;
2060d0f46423SBarry Smith   m     = A->rmap->n;
2061e4d965acSSatish Balay   ai    = a->i;
2062bfeeae90SHong Zhang   aj    = a->j;
20638a047759SSatish Balay 
2064e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
206506763907SSatish Balay 
206697f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
20676831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
206806763907SSatish Balay 
2069e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2070b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2071e4d965acSSatish Balay     isz  = 0;
20726831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2073e4d965acSSatish Balay 
2074e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
20754dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2076b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2077e4d965acSSatish Balay 
2078dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2079e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2080f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
20814dcbc457SBarry Smith     }
208206763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
208306763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
2084e4d965acSSatish Balay 
208504a348a9SBarry Smith     k = 0;
208604a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
208704a348a9SBarry Smith       n = isz;
208806763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2089e4d965acSSatish Balay         row   = nidx[k];
2090e4d965acSSatish Balay         start = ai[row];
2091e4d965acSSatish Balay         end   = ai[row+1];
209204a348a9SBarry Smith         for (l = start; l<end ; l++){
2093efb16452SHong Zhang           val = aj[l] ;
2094f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2095e4d965acSSatish Balay         }
2096e4d965acSSatish Balay       }
2097e4d965acSSatish Balay     }
209870b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2099e4d965acSSatish Balay   }
21006831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2101606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
21023a40ed3dSBarry Smith   PetscFunctionReturn(0);
21034dcbc457SBarry Smith }
210417ab2063SBarry Smith 
21050513a670SBarry Smith /* -------------------------------------------------------------- */
21064a2ae208SSatish Balay #undef __FUNCT__
21074a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2108dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
21090513a670SBarry Smith {
21100513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
21116849ba73SBarry Smith   PetscErrorCode ierr;
21123b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
21135d0c19d7SBarry Smith   const PetscInt *row,*col;
21145d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
211556cd22aeSBarry Smith   IS             icolp,irowp;
21163b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
21173b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
21180513a670SBarry Smith 
21193a40ed3dSBarry Smith   PetscFunctionBegin;
21204c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
212156cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
21224c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
212356cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
21240513a670SBarry Smith 
21250513a670SBarry Smith   /* determine lengths of permuted rows */
212697f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
21270513a670SBarry Smith   for (i=0; i<m; i++) {
21280513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
21290513a670SBarry Smith   }
21307adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2131f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
21327adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2133ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2134606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
21350513a670SBarry Smith 
213697f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
21370513a670SBarry Smith   for (i=0; i<m; i++) {
213832ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
21390513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2140cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
214132ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
21420513a670SBarry Smith   }
2143606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
21443c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
21450513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21460513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
214756cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
214856cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
214956cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
215056cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
21513a40ed3dSBarry Smith   PetscFunctionReturn(0);
21520513a670SBarry Smith }
21530513a670SBarry Smith 
21544a2ae208SSatish Balay #undef __FUNCT__
21554a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2156dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2157cb5b572fSBarry Smith {
2158dfbe8321SBarry Smith   PetscErrorCode ierr;
2159cb5b572fSBarry Smith 
2160cb5b572fSBarry Smith   PetscFunctionBegin;
216133f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
216233f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2163be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2164be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2165be6bf707SBarry Smith 
2166d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2167e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2168cb5b572fSBarry Smith     }
2169d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2170cb5b572fSBarry Smith   } else {
2171cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2172cb5b572fSBarry Smith   }
2173cb5b572fSBarry Smith   PetscFunctionReturn(0);
2174cb5b572fSBarry Smith }
2175cb5b572fSBarry Smith 
21764a2ae208SSatish Balay #undef __FUNCT__
21774a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2178dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2179273d9f13SBarry Smith {
2180dfbe8321SBarry Smith   PetscErrorCode ierr;
2181273d9f13SBarry Smith 
2182273d9f13SBarry Smith   PetscFunctionBegin;
2183ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2184273d9f13SBarry Smith   PetscFunctionReturn(0);
2185273d9f13SBarry Smith }
2186273d9f13SBarry Smith 
21874a2ae208SSatish Balay #undef __FUNCT__
21884a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2189a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
21906c0721eeSBarry Smith {
21916c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
21926c0721eeSBarry Smith   PetscFunctionBegin;
21936c0721eeSBarry Smith   *array = a->a;
21946c0721eeSBarry Smith   PetscFunctionReturn(0);
21956c0721eeSBarry Smith }
21966c0721eeSBarry Smith 
21974a2ae208SSatish Balay #undef __FUNCT__
21984a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2199dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
22006c0721eeSBarry Smith {
22016c0721eeSBarry Smith   PetscFunctionBegin;
22026c0721eeSBarry Smith   PetscFunctionReturn(0);
22036c0721eeSBarry Smith }
2204273d9f13SBarry Smith 
2205ee4f033dSBarry Smith #undef __FUNCT__
2206ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2207dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2208ee4f033dSBarry Smith {
22096849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
22106849ba73SBarry Smith   PetscErrorCode ierr;
221197f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2212efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
221387828ca2SBarry Smith   PetscScalar    *vscale_array;
2214ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2215ee4f033dSBarry Smith   Vec            w1,w2,w3;
2216ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2217ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2218ee4f033dSBarry Smith 
2219ee4f033dSBarry Smith   PetscFunctionBegin;
2220ee4f033dSBarry Smith   if (!coloring->w1) {
2221ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
222252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2223ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
222452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2225ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
222652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2227ee4f033dSBarry Smith   }
2228ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2229ee4f033dSBarry Smith 
2230ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2231acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2232ee4f033dSBarry Smith   if (flg) {
2233ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2234ee4f033dSBarry Smith   } else {
2235ace3abfcSBarry Smith     PetscBool  assembled;
22360b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
22370b9b6f31SBarry Smith     if (assembled) {
2238ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2239ee4f033dSBarry Smith     }
22400b9b6f31SBarry Smith   }
2241ee4f033dSBarry Smith 
2242ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2243ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2244ee4f033dSBarry Smith 
2245ee4f033dSBarry Smith   /*
2246ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2247ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2248ee4f033dSBarry Smith   */
2249ee4f033dSBarry Smith   if (coloring->F) {
2250ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2251ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2252ee4f033dSBarry Smith     if (m1 != m2) {
2253ee4f033dSBarry Smith       coloring->F = 0;
2254ee4f033dSBarry Smith     }
2255ee4f033dSBarry Smith   }
2256ee4f033dSBarry Smith 
2257ee4f033dSBarry Smith   if (coloring->F) {
2258ee4f033dSBarry Smith     w1          = coloring->F;
2259ee4f033dSBarry Smith     coloring->F = 0;
2260ee4f033dSBarry Smith   } else {
226166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2262ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
226366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2264ee4f033dSBarry Smith   }
2265ee4f033dSBarry Smith 
2266ee4f033dSBarry Smith   /*
2267ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2268ee4f033dSBarry Smith   */
22691ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
22701ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2271ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2272ee4f033dSBarry Smith     /*
2273ee4f033dSBarry Smith        Loop over each column associated with color adding the
2274ee4f033dSBarry Smith        perturbation to the vector w3.
2275ee4f033dSBarry Smith     */
2276ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2277ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2278ee4f033dSBarry Smith       dx  = xx[col];
2279ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2280ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2281ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2282ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2283ee4f033dSBarry Smith #else
2284ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2285ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2286ee4f033dSBarry Smith #endif
2287ee4f033dSBarry Smith       dx                *= epsilon;
2288ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2289ee4f033dSBarry Smith     }
2290ee4f033dSBarry Smith   }
22911ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2292ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2293ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2294ee4f033dSBarry Smith 
2295ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2296ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2297ee4f033dSBarry Smith 
2298ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2299ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2300ee4f033dSBarry Smith 
23011ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2302ee4f033dSBarry Smith   /*
2303ee4f033dSBarry Smith       Loop over each color
2304ee4f033dSBarry Smith   */
2305ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
230649b058dcSBarry Smith     coloring->currentcolor = k;
2307ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
23081ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2309ee4f033dSBarry Smith     /*
2310ee4f033dSBarry Smith        Loop over each column associated with color adding the
2311ee4f033dSBarry Smith        perturbation to the vector w3.
2312ee4f033dSBarry Smith     */
2313ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2314ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2315ee4f033dSBarry Smith       dx  = xx[col];
23165b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2317ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2318ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2319ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2320ee4f033dSBarry Smith #else
2321ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2322ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2323ee4f033dSBarry Smith #endif
2324ee4f033dSBarry Smith       dx            *= epsilon;
2325e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2326ee4f033dSBarry Smith       w3_array[col] += dx;
2327ee4f033dSBarry Smith     }
23281ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2329ee4f033dSBarry Smith 
2330ee4f033dSBarry Smith     /*
2331ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2332ee4f033dSBarry Smith     */
2333ee4f033dSBarry Smith 
233466f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2335ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
233666f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2337efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2338ee4f033dSBarry Smith 
2339ee4f033dSBarry Smith     /*
2340ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2341ee4f033dSBarry Smith     */
23421ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2343ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2344ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2345ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2346ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2347ee4f033dSBarry Smith       srow   = row + start;
2348ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2349ee4f033dSBarry Smith     }
23501ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2351ee4f033dSBarry Smith   }
235249b058dcSBarry Smith   coloring->currentcolor = k;
23531ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
23541ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2355ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2356ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2357ee4f033dSBarry Smith   PetscFunctionReturn(0);
2358ee4f033dSBarry Smith }
2359ee4f033dSBarry Smith 
23608229c054SShri Abhyankar /*
23618229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
23628229c054SShri Abhyankar    have different nonzero structure.
23638229c054SShri Abhyankar */
2364ac90fabeSBarry Smith #undef __FUNCT__
23658229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
23668229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2367ec7775f6SShri Abhyankar {
23688229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2369ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2370ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2371ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2372ec7775f6SShri Abhyankar 
2373ec7775f6SShri Abhyankar   PetscFunctionBegin;
2374ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2375ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
23768af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
23778af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
23788af7cee1SJed Brown     nnz[i] = 0;
23798af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
23808af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
23818af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
23828af7cee1SJed Brown       nnz[i]++;
23838af7cee1SJed Brown     }
23848af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2385ec7775f6SShri Abhyankar   }
2386ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2387ec7775f6SShri Abhyankar }
2388ec7775f6SShri Abhyankar 
2389ec7775f6SShri Abhyankar #undef __FUNCT__
2390ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2391f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2392ac90fabeSBarry Smith {
2393dfbe8321SBarry Smith   PetscErrorCode ierr;
239497f1f81fSBarry Smith   PetscInt       i;
2395ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
23960805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2397ac90fabeSBarry Smith 
2398ac90fabeSBarry Smith   PetscFunctionBegin;
2399ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2400f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2401f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2402c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2403a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2404a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2405a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2406a30b2313SHong Zhang     }
2407a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2408d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2409a30b2313SHong Zhang       y->XtoY = X;
2410407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2411c537a176SHong Zhang     }
2412f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
24131e2582c4SBarry 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);
2414ac90fabeSBarry Smith   } else {
24158229c054SShri Abhyankar     Mat B;
24168229c054SShri Abhyankar     PetscInt *nnz;
241716b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2418ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2419bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
24204aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2421ec7775f6SShri Abhyankar     ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
24228229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
24238229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2424ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2425ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
24268229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2427ac90fabeSBarry Smith   }
2428ac90fabeSBarry Smith   PetscFunctionReturn(0);
2429ac90fabeSBarry Smith }
2430ac90fabeSBarry Smith 
2431521d7252SBarry Smith #undef __FUNCT__
2432521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2433521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2434521d7252SBarry Smith {
243541c166b1SJed Brown   PetscErrorCode ierr;
243641c166b1SJed Brown 
2437521d7252SBarry Smith   PetscFunctionBegin;
243841c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
243941c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2440521d7252SBarry Smith   PetscFunctionReturn(0);
2441521d7252SBarry Smith }
2442521d7252SBarry Smith 
2443354c94deSBarry Smith #undef __FUNCT__
2444354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2445354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2446354c94deSBarry Smith {
2447354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2448354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2449354c94deSBarry Smith   PetscInt    i,nz;
2450354c94deSBarry Smith   PetscScalar *a;
2451354c94deSBarry Smith 
2452354c94deSBarry Smith   PetscFunctionBegin;
2453354c94deSBarry Smith   nz = aij->nz;
2454354c94deSBarry Smith   a  = aij->a;
2455354c94deSBarry Smith   for (i=0; i<nz; i++) {
2456354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2457354c94deSBarry Smith   }
2458354c94deSBarry Smith #else
2459354c94deSBarry Smith   PetscFunctionBegin;
2460354c94deSBarry Smith #endif
2461354c94deSBarry Smith   PetscFunctionReturn(0);
2462354c94deSBarry Smith }
2463354c94deSBarry Smith 
2464e34fafa9SBarry Smith #undef __FUNCT__
2465985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2466985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2467e34fafa9SBarry Smith {
2468e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2469e34fafa9SBarry Smith   PetscErrorCode ierr;
2470d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2471e34fafa9SBarry Smith   PetscReal      atmp;
2472985db425SBarry Smith   PetscScalar    *x;
2473e34fafa9SBarry Smith   MatScalar      *aa;
2474e34fafa9SBarry Smith 
2475e34fafa9SBarry Smith   PetscFunctionBegin;
2476e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2477e34fafa9SBarry Smith   aa   = a->a;
2478e34fafa9SBarry Smith   ai   = a->i;
2479e34fafa9SBarry Smith   aj   = a->j;
2480e34fafa9SBarry Smith 
2481985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2482e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2483e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2484e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2485e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2486e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
24879189402eSHong Zhang     x[i] = 0.0;
2488e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2489985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2490985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2491985db425SBarry Smith       aa++; aj++;
2492985db425SBarry Smith     }
2493985db425SBarry Smith   }
2494985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2495985db425SBarry Smith   PetscFunctionReturn(0);
2496985db425SBarry Smith }
2497985db425SBarry Smith 
2498985db425SBarry Smith #undef __FUNCT__
2499985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2500985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2501985db425SBarry Smith {
2502985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2503985db425SBarry Smith   PetscErrorCode ierr;
2504d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2505985db425SBarry Smith   PetscScalar    *x;
2506985db425SBarry Smith   MatScalar      *aa;
2507985db425SBarry Smith 
2508985db425SBarry Smith   PetscFunctionBegin;
2509e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2510985db425SBarry Smith   aa   = a->a;
2511985db425SBarry Smith   ai   = a->i;
2512985db425SBarry Smith   aj   = a->j;
2513985db425SBarry Smith 
2514985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2515985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2516985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2517e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2518985db425SBarry Smith   for (i=0; i<m; i++) {
2519985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2520d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2521985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2522985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2523985db425SBarry Smith       x[i] = 0.0;
2524985db425SBarry Smith       if (idx) {
2525985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2526985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2527985db425SBarry Smith           if (aj[j] > j) {
2528985db425SBarry Smith             idx[i] = j;
2529985db425SBarry Smith             break;
2530985db425SBarry Smith           }
2531985db425SBarry Smith         }
2532985db425SBarry Smith       }
2533985db425SBarry Smith     }
2534985db425SBarry Smith     for (j=0; j<ncols; j++){
2535985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2536985db425SBarry Smith       aa++; aj++;
2537985db425SBarry Smith     }
2538985db425SBarry Smith   }
2539985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2540985db425SBarry Smith   PetscFunctionReturn(0);
2541985db425SBarry Smith }
2542985db425SBarry Smith 
2543985db425SBarry Smith #undef __FUNCT__
2544c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2545c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2546c87e5d42SMatthew Knepley {
2547c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2548c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2549c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2550c87e5d42SMatthew Knepley   PetscReal      atmp;
2551c87e5d42SMatthew Knepley   PetscScalar    *x;
2552c87e5d42SMatthew Knepley   MatScalar      *aa;
2553c87e5d42SMatthew Knepley 
2554c87e5d42SMatthew Knepley   PetscFunctionBegin;
2555e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2556c87e5d42SMatthew Knepley   aa   = a->a;
2557c87e5d42SMatthew Knepley   ai   = a->i;
2558c87e5d42SMatthew Knepley   aj   = a->j;
2559c87e5d42SMatthew Knepley 
2560c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2561c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2562c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2563e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2564c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2565c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2566289a08f5SMatthew Knepley     if (ncols) {
2567289a08f5SMatthew Knepley       /* Get first nonzero */
2568289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2569289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2570289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2571289a08f5SMatthew Knepley       }
2572289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2573289a08f5SMatthew Knepley     } else {
2574289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2575289a08f5SMatthew Knepley     }
2576c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2577c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2578289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2579c87e5d42SMatthew Knepley       aa++; aj++;
2580c87e5d42SMatthew Knepley     }
2581c87e5d42SMatthew Knepley   }
2582c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2583c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2584c87e5d42SMatthew Knepley }
2585c87e5d42SMatthew Knepley 
2586c87e5d42SMatthew Knepley #undef __FUNCT__
2587985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2588985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2589985db425SBarry Smith {
2590985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2591985db425SBarry Smith   PetscErrorCode ierr;
2592d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2593985db425SBarry Smith   PetscScalar    *x;
2594985db425SBarry Smith   MatScalar      *aa;
2595985db425SBarry Smith 
2596985db425SBarry Smith   PetscFunctionBegin;
2597e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2598985db425SBarry Smith   aa   = a->a;
2599985db425SBarry Smith   ai   = a->i;
2600985db425SBarry Smith   aj   = a->j;
2601985db425SBarry Smith 
2602985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2603985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2604985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2605e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2606985db425SBarry Smith   for (i=0; i<m; i++) {
2607985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2608d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2609985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2610985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2611985db425SBarry Smith       x[i] = 0.0;
2612985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2613985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2614985db425SBarry Smith         for (j=0;j<ncols;j++) {
2615985db425SBarry Smith           if (aj[j] > j) {
2616985db425SBarry Smith             idx[i] = j;
2617985db425SBarry Smith             break;
2618985db425SBarry Smith           }
2619985db425SBarry Smith         }
2620985db425SBarry Smith       }
2621985db425SBarry Smith     }
2622985db425SBarry Smith     for (j=0; j<ncols; j++){
2623985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2624985db425SBarry Smith       aa++; aj++;
2625e34fafa9SBarry Smith     }
2626e34fafa9SBarry Smith   }
2627e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2628e34fafa9SBarry Smith   PetscFunctionReturn(0);
2629e34fafa9SBarry Smith }
26303acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2631682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
26320a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2633cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2634cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2635cb5b572fSBarry Smith        MatMult_SeqAIJ,
263697304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
26377c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
26387c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2639db4efbfdSBarry Smith        0,
2640db4efbfdSBarry Smith        0,
2641db4efbfdSBarry Smith        0,
2642db4efbfdSBarry Smith /*10*/ 0,
2643cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2644cb5b572fSBarry Smith        0,
264541f059aeSBarry Smith        MatSOR_SeqAIJ,
264617ab2063SBarry Smith        MatTranspose_SeqAIJ,
264797304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2648cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2649cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2650cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2651cb5b572fSBarry Smith        MatNorm_SeqAIJ,
265297304618SKris Buschelman /*20*/ 0,
2653cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2654cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2655cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2656d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2657db4efbfdSBarry Smith        0,
2658db4efbfdSBarry Smith        0,
2659db4efbfdSBarry Smith        0,
2660db4efbfdSBarry Smith        0,
2661d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2662db4efbfdSBarry Smith        0,
2663db4efbfdSBarry Smith        0,
26646c0721eeSBarry Smith        MatGetArray_SeqAIJ,
26656c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2666d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2667cb5b572fSBarry Smith        0,
2668cb5b572fSBarry Smith        0,
2669cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2670cb5b572fSBarry Smith        0,
2671d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2672cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2673cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2674cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2675cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2676d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2677cb5b572fSBarry Smith        MatScale_SeqAIJ,
2678cb5b572fSBarry Smith        0,
267979299369SBarry Smith        MatDiagonalSet_SeqAIJ,
26806e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
2681d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
26823b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
26833b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
26843b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2685a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2686d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2687b9617806SBarry Smith        0,
26880513a670SBarry Smith        0,
2689cda55fadSBarry Smith        MatPermute_SeqAIJ,
2690cda55fadSBarry Smith        0,
2691d519adbfSMatthew Knepley /*59*/ 0,
2692b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2693b9b97703SBarry Smith        MatView_SeqAIJ,
2694357abbc8SBarry Smith        0,
2695ee4f033dSBarry Smith        0,
2696d519adbfSMatthew Knepley /*64*/ 0,
2697ee4f033dSBarry Smith        0,
2698ee4f033dSBarry Smith        0,
2699ee4f033dSBarry Smith        0,
2700ee4f033dSBarry Smith        0,
2701d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2702c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2703ee4f033dSBarry Smith        0,
2704ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2705dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2706ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2707dcf5cc72SBarry Smith #else
2708dcf5cc72SBarry Smith        0,
2709dcf5cc72SBarry Smith #endif
2710d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
27113acb8795SBarry Smith        MatFDColoringApply_AIJ,
271297304618SKris Buschelman        0,
271397304618SKris Buschelman        0,
271497304618SKris Buschelman        0,
2715d519adbfSMatthew Knepley /*79*/ 0,
271697304618SKris Buschelman        0,
271797304618SKris Buschelman        0,
271897304618SKris Buschelman        0,
2719bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2720d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
27211cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
27226284ec50SHong Zhang        0,
27236284ec50SHong Zhang        0,
2724bc011b1eSHong Zhang        0,
2725d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
272626be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
272726be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2728d439da42SKris Buschelman        MatPtAP_Basic,
27297ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2730d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2731bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2732bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2733bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
27347ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2735d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2736609c6c4dSKris Buschelman        0,
2737609c6c4dSKris Buschelman        0,
273887d4246cSBarry Smith        MatConjugate_SeqAIJ,
273987d4246cSBarry Smith        0,
2740d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
274199cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2742f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2743f5edf698SHong Zhang        0,
27442bebee5dSHong Zhang        0,
2745d519adbfSMatthew Knepley /*109*/0,
2746985db425SBarry Smith        0,
27472af78befSBarry Smith        MatGetRowMin_SeqAIJ,
27482af78befSBarry Smith        0,
2749599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2750d519adbfSMatthew Knepley /*114*/0,
2751599ef60dSHong Zhang        0,
27523c2a7987SHong Zhang        0,
2753fe97e370SBarry Smith        0,
2754fbdbba38SShri Abhyankar        0,
2755fbdbba38SShri Abhyankar /*119*/0,
2756fbdbba38SShri Abhyankar        0,
2757fbdbba38SShri Abhyankar        0,
275882d44351SHong Zhang        0,
275982d44351SHong Zhang        MatGetMultiProcBlock_SeqAIJ
27609e29f15eSvictorle };
276117ab2063SBarry Smith 
2762fb2e594dSBarry Smith EXTERN_C_BEGIN
27634a2ae208SSatish Balay #undef __FUNCT__
27644a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2765be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2766bef8e0ddSBarry Smith {
2767bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
276897f1f81fSBarry Smith   PetscInt   i,nz,n;
2769bef8e0ddSBarry Smith 
2770bef8e0ddSBarry Smith   PetscFunctionBegin;
2771bef8e0ddSBarry Smith 
2772bef8e0ddSBarry Smith   nz = aij->maxnz;
2773d0f46423SBarry Smith   n  = mat->rmap->n;
2774bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2775bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2776bef8e0ddSBarry Smith   }
2777bef8e0ddSBarry Smith   aij->nz = nz;
2778bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2779bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2780bef8e0ddSBarry Smith   }
2781bef8e0ddSBarry Smith 
2782bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2783bef8e0ddSBarry Smith }
2784fb2e594dSBarry Smith EXTERN_C_END
2785bef8e0ddSBarry Smith 
27864a2ae208SSatish Balay #undef __FUNCT__
27874a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2788bef8e0ddSBarry Smith /*@
2789bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2790bef8e0ddSBarry Smith        in the matrix.
2791bef8e0ddSBarry Smith 
2792bef8e0ddSBarry Smith   Input Parameters:
2793bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2794bef8e0ddSBarry Smith -  indices - the column indices
2795bef8e0ddSBarry Smith 
279615091d37SBarry Smith   Level: advanced
279715091d37SBarry Smith 
2798bef8e0ddSBarry Smith   Notes:
2799bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2800bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2801bef8e0ddSBarry Smith   of the MatSetValues() operation.
2802bef8e0ddSBarry Smith 
2803bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2804d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2805bef8e0ddSBarry Smith 
2806bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2807bef8e0ddSBarry Smith 
2808b9617806SBarry Smith     The indices should start with zero, not one.
2809b9617806SBarry Smith 
2810bef8e0ddSBarry Smith @*/
2811be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2812bef8e0ddSBarry Smith {
28134ac538c5SBarry Smith   PetscErrorCode ierr;
2814bef8e0ddSBarry Smith 
2815bef8e0ddSBarry Smith   PetscFunctionBegin;
28160700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
28174482741eSBarry Smith   PetscValidPointer(indices,2);
28184ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
2819bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2820bef8e0ddSBarry Smith }
2821bef8e0ddSBarry Smith 
2822be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2823be6bf707SBarry Smith 
2824fb2e594dSBarry Smith EXTERN_C_BEGIN
28254a2ae208SSatish Balay #undef __FUNCT__
28264a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2827be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2828be6bf707SBarry Smith {
2829be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
28306849ba73SBarry Smith   PetscErrorCode ierr;
2831d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2832be6bf707SBarry Smith 
2833be6bf707SBarry Smith   PetscFunctionBegin;
2834be6bf707SBarry Smith   if (aij->nonew != 1) {
2835e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2836be6bf707SBarry Smith   }
2837be6bf707SBarry Smith 
2838be6bf707SBarry Smith   /* allocate space for values if not already there */
2839be6bf707SBarry Smith   if (!aij->saved_values) {
284087828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
28419518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2842be6bf707SBarry Smith   }
2843be6bf707SBarry Smith 
2844be6bf707SBarry Smith   /* copy values over */
284587828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2846be6bf707SBarry Smith   PetscFunctionReturn(0);
2847be6bf707SBarry Smith }
2848fb2e594dSBarry Smith EXTERN_C_END
2849be6bf707SBarry Smith 
28504a2ae208SSatish Balay #undef __FUNCT__
2851b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2852be6bf707SBarry Smith /*@
2853be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2854be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2855be6bf707SBarry Smith        nonlinear portion.
2856be6bf707SBarry Smith 
2857be6bf707SBarry Smith    Collect on Mat
2858be6bf707SBarry Smith 
2859be6bf707SBarry Smith   Input Parameters:
28600e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2861be6bf707SBarry Smith 
286215091d37SBarry Smith   Level: advanced
286315091d37SBarry Smith 
2864be6bf707SBarry Smith   Common Usage, with SNESSolve():
2865be6bf707SBarry Smith $    Create Jacobian matrix
2866be6bf707SBarry Smith $    Set linear terms into matrix
2867be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2868be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2869be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2870512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2871be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2872be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2873be6bf707SBarry Smith $    In your Jacobian routine
2874be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2875be6bf707SBarry Smith $      Set nonlinear terms in matrix
2876be6bf707SBarry Smith 
2877be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2878be6bf707SBarry Smith $    // build linear portion of Jacobian
2879512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2880be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2881be6bf707SBarry Smith $    loop over nonlinear iterations
2882be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2883be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2884be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2885be6bf707SBarry Smith $       Solve linear system with Jacobian
2886be6bf707SBarry Smith $    endloop
2887be6bf707SBarry Smith 
2888be6bf707SBarry Smith   Notes:
2889be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2890512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2891be6bf707SBarry Smith     calling this routine.
2892be6bf707SBarry Smith 
28930c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
28940c468ba9SBarry Smith     and does not allocated additional space.
28950c468ba9SBarry Smith 
2896be6bf707SBarry Smith .seealso: MatRetrieveValues()
2897be6bf707SBarry Smith 
2898be6bf707SBarry Smith @*/
2899be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2900be6bf707SBarry Smith {
29014ac538c5SBarry Smith   PetscErrorCode ierr;
2902be6bf707SBarry Smith 
2903be6bf707SBarry Smith   PetscFunctionBegin;
29040700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2905e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2906e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
29074ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
2908be6bf707SBarry Smith   PetscFunctionReturn(0);
2909be6bf707SBarry Smith }
2910be6bf707SBarry Smith 
2911fb2e594dSBarry Smith EXTERN_C_BEGIN
29124a2ae208SSatish Balay #undef __FUNCT__
29134a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2914be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2915be6bf707SBarry Smith {
2916be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
29176849ba73SBarry Smith   PetscErrorCode ierr;
2918d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2919be6bf707SBarry Smith 
2920be6bf707SBarry Smith   PetscFunctionBegin;
2921be6bf707SBarry Smith   if (aij->nonew != 1) {
2922e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2923be6bf707SBarry Smith   }
2924be6bf707SBarry Smith   if (!aij->saved_values) {
2925e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2926be6bf707SBarry Smith   }
2927be6bf707SBarry Smith   /* copy values over */
292887828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2929be6bf707SBarry Smith   PetscFunctionReturn(0);
2930be6bf707SBarry Smith }
2931fb2e594dSBarry Smith EXTERN_C_END
2932be6bf707SBarry Smith 
29334a2ae208SSatish Balay #undef __FUNCT__
29344a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2935be6bf707SBarry Smith /*@
2936be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2937be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2938be6bf707SBarry Smith        nonlinear portion.
2939be6bf707SBarry Smith 
2940be6bf707SBarry Smith    Collect on Mat
2941be6bf707SBarry Smith 
2942be6bf707SBarry Smith   Input Parameters:
2943be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2944be6bf707SBarry Smith 
294515091d37SBarry Smith   Level: advanced
294615091d37SBarry Smith 
2947be6bf707SBarry Smith .seealso: MatStoreValues()
2948be6bf707SBarry Smith 
2949be6bf707SBarry Smith @*/
2950be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2951be6bf707SBarry Smith {
29524ac538c5SBarry Smith   PetscErrorCode ierr;
2953be6bf707SBarry Smith 
2954be6bf707SBarry Smith   PetscFunctionBegin;
29550700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2956e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2957e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
29584ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
2959be6bf707SBarry Smith   PetscFunctionReturn(0);
2960be6bf707SBarry Smith }
2961be6bf707SBarry Smith 
2962f83d6046SBarry Smith 
2963be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
29644a2ae208SSatish Balay #undef __FUNCT__
29654a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
296617ab2063SBarry Smith /*@C
2967682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
29680d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
29696e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
297051c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
29712bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
297217ab2063SBarry Smith 
2973db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2974db81eaa0SLois Curfman McInnes 
297517ab2063SBarry Smith    Input Parameters:
2976db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
297717ab2063SBarry Smith .  m - number of rows
297817ab2063SBarry Smith .  n - number of columns
297917ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
298051c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
29812bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
298217ab2063SBarry Smith 
298317ab2063SBarry Smith    Output Parameter:
2984416022c9SBarry Smith .  A - the matrix
298517ab2063SBarry Smith 
2986175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2987ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2988175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2989175b88e8SBarry Smith 
2990b259b22eSLois Curfman McInnes    Notes:
299149a6f317SBarry Smith    If nnz is given then nz is ignored
299249a6f317SBarry Smith 
299317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
299417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
29950002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
299644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
299717ab2063SBarry Smith 
299817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2999a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
30003d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
30016da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
300217ab2063SBarry Smith 
3003682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
30044fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3005682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
30066c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
30076c7ebb05SLois Curfman McInnes 
30086c7ebb05SLois Curfman McInnes    Options Database Keys:
3009698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
30109db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
301117ab2063SBarry Smith 
3012027ccd11SLois Curfman McInnes    Level: intermediate
3013027ccd11SLois Curfman McInnes 
301436db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
301536db0b34SBarry Smith 
301617ab2063SBarry Smith @*/
3017be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
301817ab2063SBarry Smith {
3019dfbe8321SBarry Smith   PetscErrorCode ierr;
30206945ee14SBarry Smith 
30213a40ed3dSBarry Smith   PetscFunctionBegin;
3022f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3023117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3024c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3025d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3026273d9f13SBarry Smith   PetscFunctionReturn(0);
3027273d9f13SBarry Smith }
3028273d9f13SBarry Smith 
30294a2ae208SSatish Balay #undef __FUNCT__
30304a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3031273d9f13SBarry Smith /*@C
3032273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3033273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3034273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3035273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3036273d9f13SBarry Smith 
3037273d9f13SBarry Smith    Collective on MPI_Comm
3038273d9f13SBarry Smith 
3039273d9f13SBarry Smith    Input Parameters:
3040117016b1SBarry Smith +  B - The matrix-free
3041273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3042273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3043273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3044273d9f13SBarry Smith 
3045273d9f13SBarry Smith    Notes:
304649a6f317SBarry Smith      If nnz is given then nz is ignored
304749a6f317SBarry Smith 
3048273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3049273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3050273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3051273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3052273d9f13SBarry Smith 
3053273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3054273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3055273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3056273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3057273d9f13SBarry Smith 
3058aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3059aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3060aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3061aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3062aa95bbe8SBarry Smith 
3063a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3064a96a251dSBarry Smith    entries or columns indices
3065a96a251dSBarry Smith 
3066273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3067273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3068273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3069273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3070273d9f13SBarry Smith 
3071273d9f13SBarry Smith    Options Database Keys:
3072698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3073698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3074273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3075273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3076273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3077273d9f13SBarry Smith 
3078273d9f13SBarry Smith    Level: intermediate
3079273d9f13SBarry Smith 
3080aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3081273d9f13SBarry Smith 
3082273d9f13SBarry Smith @*/
3083be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3084273d9f13SBarry Smith {
30854ac538c5SBarry Smith   PetscErrorCode ierr;
3086a23d5eceSKris Buschelman 
3087a23d5eceSKris Buschelman   PetscFunctionBegin;
30884ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3089a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3090a23d5eceSKris Buschelman }
3091a23d5eceSKris Buschelman 
3092a23d5eceSKris Buschelman EXTERN_C_BEGIN
3093a23d5eceSKris Buschelman #undef __FUNCT__
3094a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
3095d28bb7d2SJed Brown PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3096a23d5eceSKris Buschelman {
3097273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3098ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
30996849ba73SBarry Smith   PetscErrorCode ierr;
310097f1f81fSBarry Smith   PetscInt       i;
3101273d9f13SBarry Smith 
3102273d9f13SBarry Smith   PetscFunctionBegin;
3103d5d45c9bSBarry Smith 
3104a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3105c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3106c461c341SBarry Smith     nz             = 0;
3107c461c341SBarry Smith   }
3108c461c341SBarry Smith 
310926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
311026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
311126283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
311226283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3113899cda47SBarry Smith 
3114435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3115e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3116b73539f3SBarry Smith   if (nnz) {
3117d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3118e32f2f54SBarry 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]);
3119e32f2f54SBarry 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);
3120b73539f3SBarry Smith     }
3121b73539f3SBarry Smith   }
3122b73539f3SBarry Smith 
3123273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3124273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3125273d9f13SBarry Smith 
3126ab93d7beSBarry Smith   if (!skipallocation) {
31272ee49352SLisandro Dalcin     if (!b->imax) {
3128d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3129d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
31302ee49352SLisandro Dalcin     }
3131273d9f13SBarry Smith     if (!nnz) {
3132435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3133c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3134d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3135d0f46423SBarry Smith       nz = nz*B->rmap->n;
3136273d9f13SBarry Smith     } else {
3137273d9f13SBarry Smith       nz = 0;
3138d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3139273d9f13SBarry Smith     }
3140ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3141d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3142ab93d7beSBarry Smith 
3143273d9f13SBarry Smith     /* allocate the matrix space */
31442ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3145d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3146d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3147bfeeae90SHong Zhang     b->i[0] = 0;
3148d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
31495da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
31505da197adSKris Buschelman     }
3151273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3152e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3153e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3154c461c341SBarry Smith   } else {
3155e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3156e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3157c461c341SBarry Smith   }
3158273d9f13SBarry Smith 
3159273d9f13SBarry Smith   b->nz                = 0;
3160273d9f13SBarry Smith   b->maxnz             = nz;
3161273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3162273d9f13SBarry Smith   PetscFunctionReturn(0);
3163273d9f13SBarry Smith }
3164a23d5eceSKris Buschelman EXTERN_C_END
3165273d9f13SBarry Smith 
3166a1661176SMatthew Knepley #undef  __FUNCT__
3167a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
316858d36128SBarry Smith /*@
3169a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3170a1661176SMatthew Knepley 
3171a1661176SMatthew Knepley    Input Parameters:
3172a1661176SMatthew Knepley +  B - the matrix
3173a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3174a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3175a1661176SMatthew Knepley -  v - optional values in the matrix
3176a1661176SMatthew Knepley 
3177a1661176SMatthew Knepley    Level: developer
3178a1661176SMatthew Knepley 
317958d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
318058d36128SBarry Smith 
3181a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3182a1661176SMatthew Knepley 
3183a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3184a1661176SMatthew Knepley @*/
3185a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3186a1661176SMatthew Knepley {
3187a1661176SMatthew Knepley   PetscErrorCode ierr;
3188a1661176SMatthew Knepley 
3189a1661176SMatthew Knepley   PetscFunctionBegin;
31900700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
31914ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3192a1661176SMatthew Knepley   PetscFunctionReturn(0);
3193a1661176SMatthew Knepley }
3194a1661176SMatthew Knepley 
3195a1661176SMatthew Knepley EXTERN_C_BEGIN
3196a1661176SMatthew Knepley #undef  __FUNCT__
3197a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3198b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3199a1661176SMatthew Knepley {
3200a1661176SMatthew Knepley   PetscInt       i;
3201a1661176SMatthew Knepley   PetscInt       m,n;
3202a1661176SMatthew Knepley   PetscInt       nz;
3203a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3204a1661176SMatthew Knepley   PetscScalar    *values;
3205a1661176SMatthew Knepley   PetscErrorCode ierr;
3206a1661176SMatthew Knepley 
3207a1661176SMatthew Knepley   PetscFunctionBegin;
3208a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3209a1661176SMatthew Knepley 
321065e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3211a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3212a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3213b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3214a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
321565e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3216a1661176SMatthew Knepley     nnz[i] = nz;
3217a1661176SMatthew Knepley   }
3218a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3219a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3220a1661176SMatthew Knepley 
3221a1661176SMatthew Knepley   if (v) {
3222a1661176SMatthew Knepley     values = (PetscScalar*) v;
3223a1661176SMatthew Knepley   } else {
32240e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3225a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3226a1661176SMatthew Knepley   }
3227a1661176SMatthew Knepley 
3228a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3229b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3230b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3231a1661176SMatthew Knepley   }
3232a1661176SMatthew Knepley 
3233a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3234a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3235a1661176SMatthew Knepley 
3236a1661176SMatthew Knepley   if (!v) {
3237a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3238a1661176SMatthew Knepley   }
3239a1661176SMatthew Knepley   PetscFunctionReturn(0);
3240a1661176SMatthew Knepley }
3241a1661176SMatthew Knepley EXTERN_C_END
3242a1661176SMatthew Knepley 
32437c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3244be7314b0SBarry Smith #include "private/petscaxpy.h"
3245170fe5c8SBarry Smith 
3246170fe5c8SBarry Smith #undef __FUNCT__
3247170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3248170fe5c8SBarry Smith /*
3249170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3250170fe5c8SBarry Smith 
3251170fe5c8SBarry Smith                n                       p                          p
3252170fe5c8SBarry Smith         (              )       (              )         (                  )
3253170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3254170fe5c8SBarry Smith         (              )       (              )         (                  )
3255170fe5c8SBarry Smith 
3256170fe5c8SBarry Smith */
3257170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3258170fe5c8SBarry Smith {
3259170fe5c8SBarry Smith   PetscErrorCode     ierr;
3260170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3261170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3262170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
32631de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3264170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3265170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3266170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3267170fe5c8SBarry Smith 
3268170fe5c8SBarry Smith   PetscFunctionBegin;
3269d0f46423SBarry Smith   m = A->rmap->n;
3270d0f46423SBarry Smith   n = A->cmap->n;
3271d0f46423SBarry Smith   p = B->cmap->n;
3272170fe5c8SBarry Smith   a = sub_a->v;
3273170fe5c8SBarry Smith   b = sub_b->a;
3274170fe5c8SBarry Smith   c = sub_c->v;
3275170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3276170fe5c8SBarry Smith 
3277170fe5c8SBarry Smith   ii  = sub_b->i;
3278170fe5c8SBarry Smith   idx = sub_b->j;
3279170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3280170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3281170fe5c8SBarry Smith     while (q-->0) {
3282170fe5c8SBarry Smith       c_q = c + m*(*idx);
3283170fe5c8SBarry Smith       a_q = a + m*i;
3284be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3285170fe5c8SBarry Smith       idx++;
3286170fe5c8SBarry Smith       b++;
3287170fe5c8SBarry Smith     }
3288170fe5c8SBarry Smith   }
3289170fe5c8SBarry Smith   PetscFunctionReturn(0);
3290170fe5c8SBarry Smith }
3291170fe5c8SBarry Smith 
3292170fe5c8SBarry Smith #undef __FUNCT__
3293170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3294170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3295170fe5c8SBarry Smith {
3296170fe5c8SBarry Smith   PetscErrorCode ierr;
3297d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3298170fe5c8SBarry Smith   Mat            Cmat;
3299170fe5c8SBarry Smith 
3300170fe5c8SBarry Smith   PetscFunctionBegin;
3301e32f2f54SBarry 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);
330239804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3303170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3304170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3305170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3306170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3307170fe5c8SBarry Smith   *C = Cmat;
3308170fe5c8SBarry Smith   PetscFunctionReturn(0);
3309170fe5c8SBarry Smith }
3310170fe5c8SBarry Smith 
3311170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3312170fe5c8SBarry Smith #undef __FUNCT__
3313170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3314170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3315170fe5c8SBarry Smith {
3316170fe5c8SBarry Smith   PetscErrorCode ierr;
3317170fe5c8SBarry Smith 
3318170fe5c8SBarry Smith   PetscFunctionBegin;
3319170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3320170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3321170fe5c8SBarry Smith   }
3322170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3323170fe5c8SBarry Smith   PetscFunctionReturn(0);
3324170fe5c8SBarry Smith }
3325170fe5c8SBarry Smith 
3326170fe5c8SBarry Smith 
33270bad9183SKris Buschelman /*MC
3328fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
33290bad9183SKris Buschelman    based on compressed sparse row format.
33300bad9183SKris Buschelman 
33310bad9183SKris Buschelman    Options Database Keys:
33320bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
33330bad9183SKris Buschelman 
33340bad9183SKris Buschelman   Level: beginner
33350bad9183SKris Buschelman 
3336f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
33370bad9183SKris Buschelman M*/
33380bad9183SKris Buschelman 
3339a6175056SHong Zhang EXTERN_C_BEGIN
3340b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3341b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3342b5e56a35SBarry Smith #endif
334365460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3344af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3345af1023dbSSatish Balay #endif
33465a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3347b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3348a83b8d76SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
3349ace3abfcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3350611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3351bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3352611f576cSBarry Smith #endif
3353611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
33545c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3355611f576cSBarry Smith #endif
3356f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3357f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3358f3c0ef26SHong Zhang #endif
3359611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
33605c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3361611f576cSBarry Smith #endif
3362eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3363eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3364eb3b5408SSatish Balay #endif
3365586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3366586621ddSJed Brown extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3367586621ddSJed Brown #endif
3368719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3369719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3370719d5645SBarry Smith #endif
3371b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3372b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
3373b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEnginePut_SeqAIJ(PetscObject,void*);
3374b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEngineGet_SeqAIJ(PetscObject,void*);
3375b3866ffcSBarry Smith #endif
337617667f90SBarry Smith EXTERN_C_END
337717667f90SBarry Smith 
3378b24902e0SBarry Smith 
337917667f90SBarry Smith EXTERN_C_BEGIN
33804a2ae208SSatish Balay #undef __FUNCT__
33814a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3382be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3383273d9f13SBarry Smith {
3384273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3385dfbe8321SBarry Smith   PetscErrorCode ierr;
338638baddfdSBarry Smith   PetscMPIInt    size;
3387273d9f13SBarry Smith 
3388273d9f13SBarry Smith   PetscFunctionBegin;
33897adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3390e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3391273d9f13SBarry Smith 
339238f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3393b0a32e0cSBarry Smith   B->data             = (void*)b;
3394549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
339590f02eecSBarry Smith   B->mapping          = 0;
3396416022c9SBarry Smith   b->row              = 0;
3397416022c9SBarry Smith   b->col              = 0;
339882bf6240SBarry Smith   b->icol             = 0;
3399b810aeb4SBarry Smith   b->reallocs         = 0;
340036db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3401f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3402416022c9SBarry Smith   b->nonew             = 0;
3403416022c9SBarry Smith   b->diag              = 0;
3404416022c9SBarry Smith   b->solve_work        = 0;
34052a1b7f2aSHong Zhang   B->spptr             = 0;
3406be6bf707SBarry Smith   b->saved_values      = 0;
3407d7f994e1SBarry Smith   b->idiag             = 0;
340871f1c65dSBarry Smith   b->mdiag             = 0;
340971f1c65dSBarry Smith   b->ssor_work         = 0;
341071f1c65dSBarry Smith   b->omega             = 1.0;
341171f1c65dSBarry Smith   b->fshift            = 0.0;
341271f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3413a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3414a30b2313SHong Zhang   b->xtoy              = 0;
3415a30b2313SHong Zhang   b->XtoY              = 0;
341673e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3417d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3418d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3419d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3420d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
342188e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
342217ab2063SBarry Smith 
342335d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3424b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3425b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C",
3426b3866ffcSBarry Smith 					   "MatGetFactor_seqaij_matlab",
3427b3866ffcSBarry Smith 					   MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3428b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3429b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3430b3866ffcSBarry Smith #endif
3431b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3432ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
3433b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3434b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3435b5e56a35SBarry Smith #endif
343665460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3437ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C",
3438719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3439719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3440719d5645SBarry Smith #endif
3441611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3442ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C",
34435c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
34445c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3445611f576cSBarry Smith #endif
3446f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3447ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
3448f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3449f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3450f3c0ef26SHong Zhang #endif
3451611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3452ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
34535c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
34545c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3455611f576cSBarry Smith #endif
3456611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3457ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
3458bccb9932SShri Abhyankar                                      "MatGetFactor_aij_mumps",
3459bccb9932SShri Abhyankar                                      MatGetFactor_aij_mumps);CHKERRQ(ierr);
3460611f576cSBarry Smith #endif
3461eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3462ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C",
3463eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3464eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3465eb3b5408SSatish Balay #endif
3466586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3467586621ddSJed Brown     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C",
3468586621ddSJed Brown                                      "MatGetFactor_seqaij_cholmod",
3469586621ddSJed Brown                                      MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3470586621ddSJed Brown #endif
3471719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3472ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C",
3473719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3474719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3475719d5645SBarry Smith #endif
3476ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3477b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3478b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3479ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3480db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3481db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3482174acd27SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C",
3483a83b8d76SBarry Smith                                      "MatGetFactor_seqaij_bas",
3484174acd27SBarry Smith                                      MatGetFactor_seqaij_bas);
3485f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3486bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3487bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3488f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3489be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3490bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3491f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3492be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3493bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3494a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3495a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3496a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
349785fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
349885fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
349985fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
35005a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",
35015a11e1b2SBarry Smith                                      "MatConvert_SeqAIJ_SeqAIJPERM",
35025a11e1b2SBarry Smith                                       MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
35035a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",
35045a11e1b2SBarry Smith                                      "MatConvert_SeqAIJ_SeqAIJCRL",
35055a11e1b2SBarry Smith                                       MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
35065fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
35075fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
35085fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
35091cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
35101cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
35111cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3512a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3513a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3514a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3515a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3516a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3517a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
351805b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
351905b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
352005b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3521170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3522170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3523170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3524170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3525170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3526170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3527170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3528170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3529170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
35304108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
353117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
35323a40ed3dSBarry Smith   PetscFunctionReturn(0);
353317ab2063SBarry Smith }
3534273d9f13SBarry Smith EXTERN_C_END
353517ab2063SBarry Smith 
35364a2ae208SSatish Balay #undef __FUNCT__
3537b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3538b24902e0SBarry Smith /*
3539b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3540b24902e0SBarry Smith */
3541ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
354217ab2063SBarry Smith {
3543416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
35446849ba73SBarry Smith   PetscErrorCode ierr;
3545d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
354617ab2063SBarry Smith 
35473a40ed3dSBarry Smith   PetscFunctionBegin;
3548273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3549273d9f13SBarry Smith 
3550d5f3da31SBarry Smith   C->factortype     = A->factortype;
3551416022c9SBarry Smith   c->row            = 0;
3552416022c9SBarry Smith   c->col            = 0;
355382bf6240SBarry Smith   c->icol           = 0;
35546ad4291fSHong Zhang   c->reallocs       = 0;
355517ab2063SBarry Smith 
35566ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
355717ab2063SBarry Smith 
355826283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
355926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
356026283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
356126283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3562eec197d1SBarry Smith 
356333b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
35649518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
356517ab2063SBarry Smith   for (i=0; i<m; i++) {
3566416022c9SBarry Smith     c->imax[i] = a->imax[i];
3567416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
356817ab2063SBarry Smith   }
356917ab2063SBarry Smith 
357017ab2063SBarry Smith   /* allocate the matrix space */
3571f77e22a1SHong Zhang   if (mallocmatspace){
3572a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
35739518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3574f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
357597f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
357617ab2063SBarry Smith     if (m > 0) {
357797f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3578be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3579bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3580be6bf707SBarry Smith       } else {
3581bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
358217ab2063SBarry Smith       }
358308480c60SBarry Smith     }
3584f77e22a1SHong Zhang   }
358517ab2063SBarry Smith 
35866ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3587416022c9SBarry Smith   c->roworiented       = a->roworiented;
3588416022c9SBarry Smith   c->nonew             = a->nonew;
3589416022c9SBarry Smith   if (a->diag) {
359097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
359152e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
359217ab2063SBarry Smith     for (i=0; i<m; i++) {
3593416022c9SBarry Smith       c->diag[i] = a->diag[i];
359417ab2063SBarry Smith     }
35953a40ed3dSBarry Smith   } else c->diag           = 0;
35966ad4291fSHong Zhang   c->solve_work            = 0;
35976ad4291fSHong Zhang   c->saved_values          = 0;
35986ad4291fSHong Zhang   c->idiag                 = 0;
359971f1c65dSBarry Smith   c->ssor_work             = 0;
3600a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3601e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3602e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
36036ad4291fSHong Zhang   c->xtoy                  = 0;
36046ad4291fSHong Zhang   c->XtoY                  = 0;
36056ad4291fSHong Zhang 
3606416022c9SBarry Smith   c->nz                 = a->nz;
36078ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3608273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3609754ec7b1SSatish Balay 
36106ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
36116ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
36126ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
36136ad4291fSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
36146ad4291fSHong Zhang     i = a->compressedrow.nrows;
36150e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
36166ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
36176ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
361827ea64f8SHong Zhang   } else {
361927ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
362027ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
362127ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
36226ad4291fSHong Zhang   }
362388e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
36244108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
36254846f1f5SKris Buschelman 
36267adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
36273a40ed3dSBarry Smith   PetscFunctionReturn(0);
362817ab2063SBarry Smith }
362917ab2063SBarry Smith 
36304a2ae208SSatish Balay #undef __FUNCT__
3631b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3632b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3633b24902e0SBarry Smith {
3634b24902e0SBarry Smith   PetscErrorCode ierr;
3635b24902e0SBarry Smith 
3636b24902e0SBarry Smith   PetscFunctionBegin;
3637b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
36384b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3639b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3640f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3641b24902e0SBarry Smith   PetscFunctionReturn(0);
3642b24902e0SBarry Smith }
3643b24902e0SBarry Smith 
3644b24902e0SBarry Smith #undef __FUNCT__
36454a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3646112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3647fbdbba38SShri Abhyankar {
3648fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3649fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3650fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3651fbdbba38SShri Abhyankar   int            fd;
3652fbdbba38SShri Abhyankar   PetscMPIInt    size;
3653fbdbba38SShri Abhyankar   MPI_Comm       comm;
3654fbdbba38SShri Abhyankar 
3655fbdbba38SShri Abhyankar   PetscFunctionBegin;
3656fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3657fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3658fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3659fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3660fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3661fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
3662fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3663fbdbba38SShri Abhyankar 
3664fbdbba38SShri Abhyankar   if (nz < 0) {
3665fbdbba38SShri Abhyankar     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3666fbdbba38SShri Abhyankar   }
3667fbdbba38SShri Abhyankar 
3668fbdbba38SShri Abhyankar   /* read in row lengths */
3669fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3670fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3671fbdbba38SShri Abhyankar 
3672fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
3673fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3674fbdbba38SShri Abhyankar   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);
3675fbdbba38SShri Abhyankar 
3676fbdbba38SShri Abhyankar   /* set global size if not set already*/
3677f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
3678fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3679aabbc4fbSShri Abhyankar   } else {
3680fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
3681fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
3682f501eaabSShri Abhyankar     if (M != rows ||  N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols);
3683aabbc4fbSShri Abhyankar   }
3684fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
3685fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
3686fbdbba38SShri Abhyankar 
3687fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
3688fbdbba38SShri Abhyankar 
3689fbdbba38SShri Abhyankar   /* read in nonzero values */
3690fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
3691fbdbba38SShri Abhyankar 
3692fbdbba38SShri Abhyankar   /* set matrix "i" values */
3693fbdbba38SShri Abhyankar   a->i[0] = 0;
3694fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
3695fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3696fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
3697fbdbba38SShri Abhyankar   }
3698fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3699fbdbba38SShri Abhyankar 
3700fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3701fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3702fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
3703fbdbba38SShri Abhyankar }
3704fbdbba38SShri Abhyankar 
3705fbdbba38SShri Abhyankar #undef __FUNCT__
3706b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3707ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
37087264ac53SSatish Balay {
37097264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3710dfbe8321SBarry Smith   PetscErrorCode ierr;
3711eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3712eeffb40dSHong Zhang   PetscInt k;
3713eeffb40dSHong Zhang #endif
37147264ac53SSatish Balay 
37153a40ed3dSBarry Smith   PetscFunctionBegin;
3716bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3717d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3718ca44d042SBarry Smith     *flg = PETSC_FALSE;
3719ca44d042SBarry Smith     PetscFunctionReturn(0);
3720bcd2baecSBarry Smith   }
37217264ac53SSatish Balay 
37227264ac53SSatish Balay   /* if the a->i are the same */
3723d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3724abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
37257264ac53SSatish Balay 
37267264ac53SSatish Balay   /* if a->j are the same */
372797f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3728abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3729bcd2baecSBarry Smith 
3730bcd2baecSBarry Smith   /* if a->a are the same */
3731eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3732eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3733eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3734eeffb40dSHong Zhang       *flg = PETSC_FALSE;
37353a40ed3dSBarry Smith       PetscFunctionReturn(0);
3736eeffb40dSHong Zhang     }
3737eeffb40dSHong Zhang   }
3738eeffb40dSHong Zhang #else
3739eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3740eeffb40dSHong Zhang #endif
3741eeffb40dSHong Zhang   PetscFunctionReturn(0);
37427264ac53SSatish Balay }
374336db0b34SBarry Smith 
37444a2ae208SSatish Balay #undef __FUNCT__
37454a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
374605869f15SSatish Balay /*@
374736db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
374836db0b34SBarry Smith               provided by the user.
374936db0b34SBarry Smith 
3750c75a6043SHong Zhang       Collective on MPI_Comm
375136db0b34SBarry Smith 
375236db0b34SBarry Smith    Input Parameters:
375336db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
375436db0b34SBarry Smith .   m - number of rows
375536db0b34SBarry Smith .   n - number of columns
375636db0b34SBarry Smith .   i - row indices
375736db0b34SBarry Smith .   j - column indices
375836db0b34SBarry Smith -   a - matrix values
375936db0b34SBarry Smith 
376036db0b34SBarry Smith    Output Parameter:
376136db0b34SBarry Smith .   mat - the matrix
376236db0b34SBarry Smith 
376336db0b34SBarry Smith    Level: intermediate
376436db0b34SBarry Smith 
376536db0b34SBarry Smith    Notes:
37660551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
376736db0b34SBarry Smith     once the matrix is destroyed
376836db0b34SBarry Smith 
376936db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
377036db0b34SBarry Smith 
3771bfeeae90SHong Zhang        The i and j indices are 0 based
377236db0b34SBarry Smith 
3773a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3774a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3775a4552177SSatish Balay     as shown:
3776a4552177SSatish Balay 
3777a4552177SSatish Balay         1 0 0
3778a4552177SSatish Balay         2 0 3
3779a4552177SSatish Balay         4 5 6
3780a4552177SSatish Balay 
3781a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
37829985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3783a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3784a4552177SSatish Balay 
37859985e31cSBarry Smith 
37862fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
378736db0b34SBarry Smith 
378836db0b34SBarry Smith @*/
3789a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
379036db0b34SBarry Smith {
3791dfbe8321SBarry Smith   PetscErrorCode ierr;
3792cbcfb4deSHong Zhang   PetscInt       ii;
379336db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3794cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3795cbcfb4deSHong Zhang   PetscInt       jj;
3796cbcfb4deSHong Zhang #endif
379736db0b34SBarry Smith 
379836db0b34SBarry Smith   PetscFunctionBegin;
3799a96a251dSBarry Smith   if (i[0]) {
3800e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
380136db0b34SBarry Smith   }
3802f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3803f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3804ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3805ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3806ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3807ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3808ab93d7beSBarry Smith 
380936db0b34SBarry Smith   aij->i = i;
381036db0b34SBarry Smith   aij->j = j;
381136db0b34SBarry Smith   aij->a = a;
381236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
381336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3814e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3815e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
381636db0b34SBarry Smith 
381736db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
381836db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
38192515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
3820e32f2f54SBarry 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]);
38219985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
3822e32f2f54SBarry 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);
3823e32f2f54SBarry 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);
38249985e31cSBarry Smith     }
382536db0b34SBarry Smith #endif
382636db0b34SBarry Smith   }
38272515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
382836db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3829e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3830e32f2f54SBarry 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]);
383136db0b34SBarry Smith   }
383236db0b34SBarry Smith #endif
383336db0b34SBarry Smith 
3834b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3835b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
383636db0b34SBarry Smith   PetscFunctionReturn(0);
383736db0b34SBarry Smith }
383836db0b34SBarry Smith 
3839cc8ba8e1SBarry Smith #undef __FUNCT__
3840ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3841dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3842cc8ba8e1SBarry Smith {
3843dfbe8321SBarry Smith   PetscErrorCode ierr;
3844cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
384536db0b34SBarry Smith 
3846cc8ba8e1SBarry Smith   PetscFunctionBegin;
38478ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3848cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3849cc8ba8e1SBarry Smith     a->coloring = coloring;
385012c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
385197f1f81fSBarry Smith     PetscInt             i,*larray;
385212c595b3SBarry Smith     ISColoring      ocoloring;
385308b6dcc0SBarry Smith     ISColoringValue *colors;
385412c595b3SBarry Smith 
385512c595b3SBarry Smith     /* set coloring for diagonal portion */
38560e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3857d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
385812c595b3SBarry Smith       larray[i] = i;
385912c595b3SBarry Smith     }
3860d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
38610e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3862d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
386312c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
386412c595b3SBarry Smith     }
386512c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3866d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
386712c595b3SBarry Smith     a->coloring = ocoloring;
386812c595b3SBarry Smith   }
3869cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3870cc8ba8e1SBarry Smith }
3871cc8ba8e1SBarry Smith 
3872dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3873ee4f033dSBarry Smith EXTERN_C_BEGIN
387429c1e371SBarry Smith #include "adic/ad_utils.h"
3875ee4f033dSBarry Smith EXTERN_C_END
3876cc8ba8e1SBarry Smith 
3877cc8ba8e1SBarry Smith #undef __FUNCT__
3878ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3879dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3880cc8ba8e1SBarry Smith {
3881cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3882d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
38834440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
388408b6dcc0SBarry Smith   ISColoringValue *color;
3885cc8ba8e1SBarry Smith 
3886cc8ba8e1SBarry Smith   PetscFunctionBegin;
3887e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
38884440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3889cc8ba8e1SBarry Smith   color = a->coloring->colors;
3890cc8ba8e1SBarry Smith   /* loop over rows */
3891cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3892cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3893cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3894cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3895cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3896cc8ba8e1SBarry Smith     }
38974440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3898ee4f033dSBarry Smith   }
3899ee4f033dSBarry Smith   PetscFunctionReturn(0);
3900ee4f033dSBarry Smith }
3901ee4f033dSBarry Smith #endif
3902ee4f033dSBarry Smith 
3903ee4f033dSBarry Smith #undef __FUNCT__
3904ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
390597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3906ee4f033dSBarry Smith {
3907ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3908d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
390954f21887SBarry Smith   MatScalar       *v = a->a;
391054f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
391108b6dcc0SBarry Smith   ISColoringValue *color;
3912ee4f033dSBarry Smith 
3913ee4f033dSBarry Smith   PetscFunctionBegin;
3914e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3915ee4f033dSBarry Smith   color = a->coloring->colors;
3916ee4f033dSBarry Smith   /* loop over rows */
3917ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3918ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3919ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3920ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3921ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3922ee4f033dSBarry Smith     }
3923ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3924cc8ba8e1SBarry Smith   }
3925cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3926cc8ba8e1SBarry Smith }
392736db0b34SBarry Smith 
392881824310SBarry Smith /*
392981824310SBarry Smith     Special version for direct calls from Fortran
393081824310SBarry Smith */
393181824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
393281824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
393381824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
393481824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
393581824310SBarry Smith #endif
393681824310SBarry Smith 
393781824310SBarry Smith /* Change these macros so can be used in void function */
393881824310SBarry Smith #undef CHKERRQ
39397adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
394081824310SBarry Smith #undef SETERRQ2
3941e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
394281824310SBarry Smith 
394381824310SBarry Smith EXTERN_C_BEGIN
394481824310SBarry Smith #undef __FUNCT__
394581824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
39461f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
394781824310SBarry Smith {
394881824310SBarry Smith   Mat            A = *AA;
394981824310SBarry Smith   PetscInt       m = *mm, n = *nn;
395081824310SBarry Smith   InsertMode     is = *isis;
395181824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
395281824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
395381824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
395481824310SBarry Smith   PetscErrorCode ierr;
395581824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
395654f21887SBarry Smith   MatScalar      *ap,value,*aa;
3957ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
3958ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
395981824310SBarry Smith 
396081824310SBarry Smith   PetscFunctionBegin;
3961d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
396281824310SBarry Smith   imax = a->imax;
396381824310SBarry Smith   ai = a->i;
396481824310SBarry Smith   ailen = a->ilen;
396581824310SBarry Smith   aj = a->j;
396681824310SBarry Smith   aa = a->a;
396781824310SBarry Smith 
396881824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
396981824310SBarry Smith     row  = im[k];
397081824310SBarry Smith     if (row < 0) continue;
397181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3972d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
397381824310SBarry Smith #endif
397481824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
397581824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
397681824310SBarry Smith     low  = 0;
397781824310SBarry Smith     high = nrow;
397881824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
397981824310SBarry Smith       if (in[l] < 0) continue;
398081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3981d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
398281824310SBarry Smith #endif
398381824310SBarry Smith       col = in[l];
398481824310SBarry Smith       if (roworiented) {
398581824310SBarry Smith         value = v[l + k*n];
398681824310SBarry Smith       } else {
398781824310SBarry Smith         value = v[k + l*m];
398881824310SBarry Smith       }
398981824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
399081824310SBarry Smith 
399181824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
399281824310SBarry Smith       lastcol = col;
399381824310SBarry Smith       while (high-low > 5) {
399481824310SBarry Smith         t = (low+high)/2;
399581824310SBarry Smith         if (rp[t] > col) high = t;
399681824310SBarry Smith         else             low  = t;
399781824310SBarry Smith       }
399881824310SBarry Smith       for (i=low; i<high; i++) {
399981824310SBarry Smith         if (rp[i] > col) break;
400081824310SBarry Smith         if (rp[i] == col) {
400181824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
400281824310SBarry Smith           else                  ap[i] = value;
400381824310SBarry Smith           goto noinsert;
400481824310SBarry Smith         }
400581824310SBarry Smith       }
400681824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
400781824310SBarry Smith       if (nonew == 1) goto noinsert;
40087adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4009fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
401081824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
401181824310SBarry Smith       /* shift up all the later entries in this row */
401281824310SBarry Smith       for (ii=N; ii>=i; ii--) {
401381824310SBarry Smith         rp[ii+1] = rp[ii];
401481824310SBarry Smith         ap[ii+1] = ap[ii];
401581824310SBarry Smith       }
401681824310SBarry Smith       rp[i] = col;
401781824310SBarry Smith       ap[i] = value;
401881824310SBarry Smith       noinsert:;
401981824310SBarry Smith       low = i + 1;
402081824310SBarry Smith     }
402181824310SBarry Smith     ailen[row] = nrow;
402281824310SBarry Smith   }
402381824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
402481824310SBarry Smith   PetscFunctionReturnVoid();
402581824310SBarry Smith }
402681824310SBarry Smith EXTERN_C_END
4027