xref: /petsc/src/mat/impls/aij/seq/aij.c (revision be7314b09a558091375cab512a4901878ccc5021)
1992c00aaSSatish Balay #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
87c4f633dSBarry Smith 
97c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
107c4f633dSBarry Smith #include "../src/inline/dot.h"
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
134a2ae208SSatish Balay #undef __FUNCT__
1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1579299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1679299369SBarry Smith {
1779299369SBarry Smith   PetscErrorCode ierr;
1879299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
19d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
2054f21887SBarry Smith   MatScalar      *aa = aij->a;
2154f21887SBarry Smith   PetscScalar    *v;
2209f38230SBarry Smith   PetscTruth     missing;
2379299369SBarry Smith 
2479299369SBarry Smith   PetscFunctionBegin;
2509f38230SBarry Smith   if (Y->assembled) {
2609f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2709f38230SBarry Smith     if (!missing) {
2879299369SBarry Smith       diag = aij->diag;
2979299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3079299369SBarry Smith       if (is == INSERT_VALUES) {
3179299369SBarry Smith 	for (i=0; i<m; i++) {
3279299369SBarry Smith 	  aa[diag[i]] = v[i];
3379299369SBarry Smith 	}
3479299369SBarry Smith       } else {
3579299369SBarry Smith 	for (i=0; i<m; i++) {
3679299369SBarry Smith 	  aa[diag[i]] += v[i];
3779299369SBarry Smith 	}
3879299369SBarry Smith       }
3979299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4079299369SBarry Smith       PetscFunctionReturn(0);
4179299369SBarry Smith     }
4209f38230SBarry Smith   }
4309f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4409f38230SBarry Smith   PetscFunctionReturn(0);
4509f38230SBarry Smith }
4679299369SBarry Smith 
4779299369SBarry Smith #undef __FUNCT__
484a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
498f7157efSSatish Balay PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5017ab2063SBarry Smith {
51416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
52dfbe8321SBarry Smith   PetscErrorCode ierr;
5397f1f81fSBarry Smith   PetscInt       i,ishift;
5417ab2063SBarry Smith 
553a40ed3dSBarry Smith   PetscFunctionBegin;
56d0f46423SBarry Smith   *m     = A->rmap->n;
573a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
58bfeeae90SHong Zhang   ishift = 0;
5953e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
60d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
61bfeeae90SHong Zhang   } else if (oshift == 1) {
62d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
633b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
64d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
65d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
66ecc77c7aSBarry Smith     if (ja) {
6797f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
683b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
69ecc77c7aSBarry Smith     }
706945ee14SBarry Smith   } else {
71ecc77c7aSBarry Smith     *ia = a->i;
72ecc77c7aSBarry Smith     if (ja) *ja = a->j;
73a2ce50c7SBarry Smith   }
743a40ed3dSBarry Smith   PetscFunctionReturn(0);
75a2744918SBarry Smith }
76a2744918SBarry Smith 
774a2ae208SSatish Balay #undef __FUNCT__
784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
798f7157efSSatish Balay PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
806945ee14SBarry Smith {
81dfbe8321SBarry Smith   PetscErrorCode ierr;
826945ee14SBarry Smith 
833a40ed3dSBarry Smith   PetscFunctionBegin;
843a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
85bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
86606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
87ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
88bcd2baecSBarry Smith   }
893a40ed3dSBarry Smith   PetscFunctionReturn(0);
9017ab2063SBarry Smith }
9117ab2063SBarry Smith 
924a2ae208SSatish Balay #undef __FUNCT__
934a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
948f7157efSSatish Balay PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
953b2fbd54SBarry Smith {
963b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
97dfbe8321SBarry Smith   PetscErrorCode ierr;
98d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
9997f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1003b2fbd54SBarry Smith 
1013a40ed3dSBarry Smith   PetscFunctionBegin;
102899cda47SBarry Smith   *nn = n;
1033a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1043b2fbd54SBarry Smith   if (symmetric) {
105d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1063b2fbd54SBarry Smith   } else {
10797f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1113b2fbd54SBarry Smith     jj = a->j;
1123b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
113bfeeae90SHong Zhang       collengths[jj[i]]++;
1143b2fbd54SBarry Smith     }
1153b2fbd54SBarry Smith     cia[0] = oshift;
1163b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1173b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1183b2fbd54SBarry Smith     }
11997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1203b2fbd54SBarry Smith     jj   = a->j;
121a93ec695SBarry Smith     for (row=0; row<m; row++) {
122a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
123a93ec695SBarry Smith       for (i=0; i<mr; i++) {
124bfeeae90SHong Zhang         col = *jj++;
1253b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1263b2fbd54SBarry Smith       }
1273b2fbd54SBarry Smith     }
128606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1293b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1303b2fbd54SBarry Smith   }
1313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1323b2fbd54SBarry Smith }
1333b2fbd54SBarry Smith 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1368f7157efSSatish Balay PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1373b2fbd54SBarry Smith {
138dfbe8321SBarry Smith   PetscErrorCode ierr;
139606d414cSSatish Balay 
1403a40ed3dSBarry Smith   PetscFunctionBegin;
1413a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1423b2fbd54SBarry Smith 
143606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
144606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1453b2fbd54SBarry Smith 
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1473b2fbd54SBarry Smith }
1483b2fbd54SBarry Smith 
14987d4246cSBarry Smith #undef __FUNCT__
15087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15287d4246cSBarry Smith {
15387d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15487d4246cSBarry Smith   PetscInt       *ai = a->i;
15587d4246cSBarry Smith   PetscErrorCode ierr;
15687d4246cSBarry Smith 
15787d4246cSBarry Smith   PetscFunctionBegin;
15887d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
15987d4246cSBarry Smith   PetscFunctionReturn(0);
16087d4246cSBarry Smith }
16187d4246cSBarry Smith 
162227d817aSBarry Smith #define CHUNKSIZE   15
16317ab2063SBarry Smith 
1644a2ae208SSatish Balay #undef __FUNCT__
1654a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16717ab2063SBarry Smith {
168416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
169e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
17097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1716849ba73SBarry Smith   PetscErrorCode ierr;
172e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17354f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
174edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
175273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17617ab2063SBarry Smith 
1773a40ed3dSBarry Smith   PetscFunctionBegin;
17817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
179416022c9SBarry Smith     row  = im[k];
1805ef9f2a5SBarry Smith     if (row < 0) continue;
1812515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
182d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
1833b2fbd54SBarry Smith #endif
184bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
186416022c9SBarry Smith     low  = 0;
187c71e6ed7SBarry Smith     high = nrow;
18817ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1895ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1902515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
191d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
1923b2fbd54SBarry Smith #endif
193bfeeae90SHong Zhang       col = in[l];
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 {
20116371a99SBarry 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;
223cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
224d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
225c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
226416022c9SBarry Smith       /* shift up all the later entries in this row */
227416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
22817ab2063SBarry Smith         rp[ii+1] = rp[ii];
22917ab2063SBarry Smith         ap[ii+1] = ap[ii];
23017ab2063SBarry Smith       }
23117ab2063SBarry Smith       rp[i] = col;
23217ab2063SBarry Smith       ap[i] = value;
233416022c9SBarry Smith       low   = i + 1;
234e44c0bd4SBarry Smith       noinsert:;
23517ab2063SBarry Smith     }
23617ab2063SBarry Smith     ailen[row] = nrow;
23717ab2063SBarry Smith   }
23888e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2393a40ed3dSBarry Smith   PetscFunctionReturn(0);
24017ab2063SBarry Smith }
24117ab2063SBarry Smith 
24281824310SBarry Smith 
2434a2ae208SSatish Balay #undef __FUNCT__
2444a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
245a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2467eb43aa7SLois Curfman McInnes {
2477eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
24897f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
24997f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
25054f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2517eb43aa7SLois Curfman McInnes 
2523a40ed3dSBarry Smith   PetscFunctionBegin;
2537eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2547eb43aa7SLois Curfman McInnes     row  = im[k];
25597e567efSBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
256d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
257bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2587eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2597eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
26097e567efSBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
261d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
262bfeeae90SHong Zhang       col = in[l] ;
2637eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2647eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2657eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2667eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2677eb43aa7SLois Curfman McInnes         else             low  = t;
2687eb43aa7SLois Curfman McInnes       }
2697eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2707eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2717eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
272b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2737eb43aa7SLois Curfman McInnes           goto finished;
2747eb43aa7SLois Curfman McInnes         }
2757eb43aa7SLois Curfman McInnes       }
27697e567efSBarry Smith       *v++ = 0.0;
2777eb43aa7SLois Curfman McInnes       finished:;
2787eb43aa7SLois Curfman McInnes     }
2797eb43aa7SLois Curfman McInnes   }
2803a40ed3dSBarry Smith   PetscFunctionReturn(0);
2817eb43aa7SLois Curfman McInnes }
2827eb43aa7SLois Curfman McInnes 
28317ab2063SBarry Smith 
2844a2ae208SSatish Balay #undef __FUNCT__
2854a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
286dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28717ab2063SBarry Smith {
288416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2896849ba73SBarry Smith   PetscErrorCode ierr;
2906f69ff64SBarry Smith   PetscInt       i,*col_lens;
2916f69ff64SBarry Smith   int            fd;
29217ab2063SBarry Smith 
2933a40ed3dSBarry Smith   PetscFunctionBegin;
294b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
295d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
296552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
297d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
298d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
299416022c9SBarry Smith   col_lens[3] = a->nz;
300416022c9SBarry Smith 
301416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
302d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
303416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30417ab2063SBarry Smith   }
305d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
306606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
307416022c9SBarry Smith 
308416022c9SBarry Smith   /* store column indices (zero start index) */
3096f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
310416022c9SBarry Smith 
311416022c9SBarry Smith   /* store nonzero values */
3126f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3133a40ed3dSBarry Smith   PetscFunctionReturn(0);
31417ab2063SBarry Smith }
315416022c9SBarry Smith 
316dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
317cd155464SBarry Smith 
3184a2ae208SSatish Balay #undef __FUNCT__
3194a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
320dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
321416022c9SBarry Smith {
322416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
323dfbe8321SBarry Smith   PetscErrorCode    ierr;
324d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
325e060cb09SBarry Smith   const char        *name;
326f3ef73ceSBarry Smith   PetscViewerFormat format;
32717ab2063SBarry Smith 
3283a40ed3dSBarry Smith   PetscFunctionBegin;
329435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
330b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33171c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33297f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
333d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
334d00d2cf4SBarry Smith       nofinalvalue = 1;
335d00d2cf4SBarry Smith     }
336b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
337d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
33877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
33977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
340b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34117ab2063SBarry Smith 
34217ab2063SBarry Smith     for (i=0; i<m; i++) {
343416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
344aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34577431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34617ab2063SBarry Smith #else
34777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34817ab2063SBarry Smith #endif
34917ab2063SBarry Smith       }
35017ab2063SBarry Smith     }
351d00d2cf4SBarry Smith     if (nofinalvalue) {
352d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
353d00d2cf4SBarry Smith     }
354fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
355b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35668369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
357cd155464SBarry Smith      PetscFunctionReturn(0);
358fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
359b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36044cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36177431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36244cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
363aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36436db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
365a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36636db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
367a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36836db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
369a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3706831982aSBarry Smith         }
37144cd7ae7SLois Curfman McInnes #else
372a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37344cd7ae7SLois Curfman McInnes #endif
37444cd7ae7SLois Curfman McInnes       }
375b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37644cd7ae7SLois Curfman McInnes     }
377b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
378fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
37997f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
380b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
38197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
382496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
383496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
384496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
385496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
386aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
388496be53dSLois Curfman McInnes #else
389496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
390496be53dSLois Curfman McInnes #endif
391496be53dSLois Curfman McInnes         }
392496be53dSLois Curfman McInnes       }
393496be53dSLois Curfman McInnes     }
3942e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3962e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39777431f27SBarry Smith       if (i+4<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);}
39877431f27SBarry Smith       else if (i+3<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);}
39977431f27SBarry Smith       else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);}
40077431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40177431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40277431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
403496be53dSLois Curfman McInnes     }
404b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
405606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
406496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
407496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
40877431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
409496be53dSLois Curfman McInnes       }
410b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
411496be53dSLois Curfman McInnes     }
412b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
413496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
414496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
415496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
416aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
418b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4196831982aSBarry Smith           }
420496be53dSLois Curfman McInnes #else
421b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
422496be53dSLois Curfman McInnes #endif
423496be53dSLois Curfman McInnes         }
424496be53dSLois Curfman McInnes       }
425b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
426496be53dSLois Curfman McInnes     }
427b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
428fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
42997f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
43087828ca2SBarry Smith     PetscScalar value;
43102594712SBarry Smith 
432b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43302594712SBarry Smith     for (i=0; i<m; i++) {
43402594712SBarry Smith       jcnt = 0;
435d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
436e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43702594712SBarry Smith           value = a->a[cnt++];
438e24b481bSBarry Smith           jcnt++;
43902594712SBarry Smith         } else {
44002594712SBarry Smith           value = 0.0;
44102594712SBarry Smith         }
442aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
443b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44402594712SBarry Smith #else
445b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44602594712SBarry Smith #endif
44702594712SBarry Smith       }
448b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44902594712SBarry Smith     }
450b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4513c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4523c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4533c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4543c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4553c215bfdSMatthew Knepley #else
4563c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4573c215bfdSMatthew Knepley #endif
458d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4593c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4603c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4613c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4623c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4633c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4643c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
4653c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G -%G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4663c215bfdSMatthew Knepley         } else {
4673c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4683c215bfdSMatthew Knepley         }
4693c215bfdSMatthew Knepley #else
4703c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4713c215bfdSMatthew Knepley #endif
4723c215bfdSMatthew Knepley       }
4733c215bfdSMatthew Knepley     }
4743c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4753a40ed3dSBarry Smith   } else {
476b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
47717ab2063SBarry Smith     for (i=0; i<m; i++) {
47877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
479416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
480aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
482a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
484a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4853a40ed3dSBarry Smith         } else {
486a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
48717ab2063SBarry Smith         }
48817ab2063SBarry Smith #else
489a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49017ab2063SBarry Smith #endif
49117ab2063SBarry Smith       }
492b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
49317ab2063SBarry Smith     }
494b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
49517ab2063SBarry Smith   }
496b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4973a40ed3dSBarry Smith   PetscFunctionReturn(0);
498416022c9SBarry Smith }
499416022c9SBarry Smith 
5004a2ae208SSatish Balay #undef __FUNCT__
5014a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
502dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
503416022c9SBarry Smith {
504480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
505416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
506dfbe8321SBarry Smith   PetscErrorCode    ierr;
507d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
50836db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
509b0a32e0cSBarry Smith   PetscViewer       viewer;
510f3ef73ceSBarry Smith   PetscViewerFormat format;
511cddf8d76SBarry Smith 
5123a40ed3dSBarry Smith   PetscFunctionBegin;
513480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
514b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
51519bcc07fSBarry Smith 
516b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
517416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5180513a670SBarry Smith 
519fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5200513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
521b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
522416022c9SBarry Smith     for (i=0; i<m; i++) {
523cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
524bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
525bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
526aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
528cddf8d76SBarry Smith #else
529cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
530cddf8d76SBarry Smith #endif
531b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
532cddf8d76SBarry Smith       }
533cddf8d76SBarry Smith     }
534b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
535cddf8d76SBarry Smith     for (i=0; i<m; i++) {
536cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
537bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
538bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
539cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
540b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
541cddf8d76SBarry Smith       }
542cddf8d76SBarry Smith     }
543b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
544cddf8d76SBarry Smith     for (i=0; i<m; i++) {
545cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
546bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
547bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
548aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
54936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
550cddf8d76SBarry Smith #else
551cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
552cddf8d76SBarry Smith #endif
553b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
554416022c9SBarry Smith       }
555416022c9SBarry Smith     }
5560513a670SBarry Smith   } else {
5570513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5580513a670SBarry Smith     /* first determine max of all nonzero values */
55997f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
560b0a32e0cSBarry Smith     PetscDraw   popup;
56136db0b34SBarry Smith     PetscReal scale;
5620513a670SBarry Smith 
5630513a670SBarry Smith     for (i=0; i<nz; i++) {
5640513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5650513a670SBarry Smith     }
566b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
567b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
568b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5690513a670SBarry Smith     count = 0;
5700513a670SBarry Smith     for (i=0; i<m; i++) {
5710513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
572bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
573bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
57497f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
575b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5760513a670SBarry Smith         count++;
5770513a670SBarry Smith       }
5780513a670SBarry Smith     }
5790513a670SBarry Smith   }
580480ef9eaSBarry Smith   PetscFunctionReturn(0);
581480ef9eaSBarry Smith }
582cddf8d76SBarry Smith 
5834a2ae208SSatish Balay #undef __FUNCT__
5844a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
585dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
586480ef9eaSBarry Smith {
587dfbe8321SBarry Smith   PetscErrorCode ierr;
588b0a32e0cSBarry Smith   PetscDraw      draw;
58936db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
590480ef9eaSBarry Smith   PetscTruth     isnull;
591480ef9eaSBarry Smith 
592480ef9eaSBarry Smith   PetscFunctionBegin;
593b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
594b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
595480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
596480ef9eaSBarry Smith 
597480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
598d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
599480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
600b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
601b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
602480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6033a40ed3dSBarry Smith   PetscFunctionReturn(0);
604416022c9SBarry Smith }
605416022c9SBarry Smith 
6064a2ae208SSatish Balay #undef __FUNCT__
6074a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
608dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
609416022c9SBarry Smith {
610dfbe8321SBarry Smith   PetscErrorCode ierr;
6116805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
612416022c9SBarry Smith 
6133a40ed3dSBarry Smith   PetscFunctionBegin;
61432077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
615fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
616fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
617c45a1595SBarry Smith   if (iascii) {
6183a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6190f5bd95cSBarry Smith   } else if (isbinary) {
6203a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6210f5bd95cSBarry Smith   } else if (isdraw) {
6223a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6235cd90555SBarry Smith   } else {
624958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
62517ab2063SBarry Smith   }
6264846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
6273a40ed3dSBarry Smith   PetscFunctionReturn(0);
62817ab2063SBarry Smith }
62919bcc07fSBarry Smith 
6304a2ae208SSatish Balay #undef __FUNCT__
6314a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
632dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
63317ab2063SBarry Smith {
634416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6356849ba73SBarry Smith   PetscErrorCode ierr;
63697f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
637d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
63854f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6393447b6efSHong Zhang   PetscReal      ratio=0.6;
64017ab2063SBarry Smith 
6413a40ed3dSBarry Smith   PetscFunctionBegin;
6423a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
64317ab2063SBarry Smith 
64443ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
64517ab2063SBarry Smith   for (i=1; i<m; i++) {
646416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
64717ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
64894a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
64917ab2063SBarry Smith     if (fshift) {
650bfeeae90SHong Zhang       ip = aj + ai[i] ;
651bfeeae90SHong Zhang       ap = aa + ai[i] ;
65217ab2063SBarry Smith       N  = ailen[i];
65317ab2063SBarry Smith       for (j=0; j<N; j++) {
65417ab2063SBarry Smith         ip[j-fshift] = ip[j];
65517ab2063SBarry Smith         ap[j-fshift] = ap[j];
65617ab2063SBarry Smith       }
65717ab2063SBarry Smith     }
65817ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
65917ab2063SBarry Smith   }
66017ab2063SBarry Smith   if (m) {
66117ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
66217ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
66317ab2063SBarry Smith   }
66417ab2063SBarry Smith   /* reset ilen and imax for each row */
66517ab2063SBarry Smith   for (i=0; i<m; i++) {
66617ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
66717ab2063SBarry Smith   }
668bfeeae90SHong Zhang   a->nz = ai[m];
66928b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
67028b2fa4aSMatthew Knepley     SETERRQ3(PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
67128b2fa4aSMatthew Knepley   }
67217ab2063SBarry Smith 
67309f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
674d0f46423SBarry 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);
675ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
676ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
677a0e1a203SBarry Smith 
678dd5f02e7SSatish Balay   a->reallocs          = 0;
6794e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
68036db0b34SBarry Smith   a->rmax              = rmax;
6814e220ebcSLois Curfman McInnes 
682cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
683317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
68488e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
68571c2f376SKris Buschelman 
6864846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
68771f1c65dSBarry Smith 
68871f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6893a40ed3dSBarry Smith   PetscFunctionReturn(0);
69017ab2063SBarry Smith }
69117ab2063SBarry Smith 
6924a2ae208SSatish Balay #undef __FUNCT__
69399cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
69499cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
69599cafbc1SBarry Smith {
69699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
69799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
69854f21887SBarry Smith   MatScalar      *aa = a->a;
69999cafbc1SBarry Smith 
70099cafbc1SBarry Smith   PetscFunctionBegin;
70199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
70299cafbc1SBarry Smith   PetscFunctionReturn(0);
70399cafbc1SBarry Smith }
70499cafbc1SBarry Smith 
70599cafbc1SBarry Smith #undef __FUNCT__
70699cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
70799cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
70899cafbc1SBarry Smith {
70999cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
71099cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
71154f21887SBarry Smith   MatScalar      *aa = a->a;
71299cafbc1SBarry Smith 
71399cafbc1SBarry Smith   PetscFunctionBegin;
71499cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
71599cafbc1SBarry Smith   PetscFunctionReturn(0);
71699cafbc1SBarry Smith }
71799cafbc1SBarry Smith 
71899cafbc1SBarry Smith #undef __FUNCT__
7194a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
720dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
72117ab2063SBarry Smith {
722416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
723dfbe8321SBarry Smith   PetscErrorCode ierr;
7243a40ed3dSBarry Smith 
7253a40ed3dSBarry Smith   PetscFunctionBegin;
726d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7273a40ed3dSBarry Smith   PetscFunctionReturn(0);
72817ab2063SBarry Smith }
729416022c9SBarry Smith 
7304a2ae208SSatish Balay #undef __FUNCT__
7314a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
732dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
73317ab2063SBarry Smith {
734416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
735dfbe8321SBarry Smith   PetscErrorCode ierr;
736d5d45c9bSBarry Smith 
7373a40ed3dSBarry Smith   PetscFunctionBegin;
738aa482453SBarry Smith #if defined(PETSC_USE_LOG)
739d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
74017ab2063SBarry Smith #endif
741e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
742c38d4ed2SBarry Smith   if (a->row) {
743c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
744c38d4ed2SBarry Smith   }
745c38d4ed2SBarry Smith   if (a->col) {
746c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
747c38d4ed2SBarry Smith   }
74805b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
74905b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
75071f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
75105b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
75282bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
75305b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
754cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
75505b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
756407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
757d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
758a30b2313SHong Zhang 
7594846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7604846f1f5SKris Buschelman 
761606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
762901853e0SKris Buschelman 
763dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
764901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
765901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
766901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
767901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
768901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
769ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
770901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
771901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
772a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
773901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7743a40ed3dSBarry Smith   PetscFunctionReturn(0);
77517ab2063SBarry Smith }
77617ab2063SBarry Smith 
7774a2ae208SSatish Balay #undef __FUNCT__
7784a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7794e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
78017ab2063SBarry Smith {
781416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7824846f1f5SKris Buschelman   PetscErrorCode ierr;
7833a40ed3dSBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785a65d3064SKris Buschelman   switch (op) {
786a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7874e0d8c25SBarry Smith       a->roworiented       = flg;
788a65d3064SKris Buschelman       break;
789a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
790a9817697SBarry Smith       a->keepnonzeropattern    = flg;
791a65d3064SKris Buschelman       break;
792512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
793512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
794a65d3064SKris Buschelman       break;
795a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
7964e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
797a65d3064SKris Buschelman       break;
798a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
7994e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
800a65d3064SKris Buschelman       break;
80128b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
80228b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
80328b2fa4aSMatthew Knepley       break;
804a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8054e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8060df259c2SBarry Smith       break;
807d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8084e0d8c25SBarry Smith       a->compressedrow.use = flg;
809d487561eSHong Zhang       break;
8104e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
811a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
812a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
813290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
814a65d3064SKris Buschelman       break;
815a65d3064SKris Buschelman     default:
81671c2f376SKris Buschelman       break;
817a65d3064SKris Buschelman   }
8184e0d8c25SBarry Smith   ierr = MatSetOption_Inode(A,op,flg);CHKERRQ(ierr);
8193a40ed3dSBarry Smith   PetscFunctionReturn(0);
82017ab2063SBarry Smith }
82117ab2063SBarry Smith 
8224a2ae208SSatish Balay #undef __FUNCT__
8234a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
824dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
82517ab2063SBarry Smith {
826416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8276849ba73SBarry Smith   PetscErrorCode ierr;
82897f1f81fSBarry Smith   PetscInt       i,j,n;
82987828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
83017ab2063SBarry Smith 
8313a40ed3dSBarry Smith   PetscFunctionBegin;
8322dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8331ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
83436db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
835d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
836d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
837bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
838bfeeae90SHong Zhang       if (a->j[j] == i) {
839416022c9SBarry Smith         x[i] = a->a[j];
84017ab2063SBarry Smith         break;
84117ab2063SBarry Smith       }
84217ab2063SBarry Smith     }
84317ab2063SBarry Smith   }
8441ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8453a40ed3dSBarry Smith   PetscFunctionReturn(0);
84617ab2063SBarry Smith }
84717ab2063SBarry Smith 
8484a2ae208SSatish Balay #undef __FUNCT__
8494a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
850dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
85117ab2063SBarry Smith {
852416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8535c897100SBarry Smith   PetscScalar       *x,*y;
854dfbe8321SBarry Smith   PetscErrorCode    ierr;
855d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
8565c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
857a77337e4SBarry Smith   MatScalar         *v;
858a77337e4SBarry Smith   PetscScalar       alpha;
8597b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8603447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8614eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8625c897100SBarry Smith #endif
86317ab2063SBarry Smith 
8643a40ed3dSBarry Smith   PetscFunctionBegin;
8652e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8661ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8671ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8685c897100SBarry Smith 
8695c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
870bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8715c897100SBarry Smith #else
8723447b6efSHong Zhang   if (usecprow){
8733447b6efSHong Zhang     m    = cprow.nrows;
8743447b6efSHong Zhang     ii   = cprow.i;
8757b2bb3b9SHong Zhang     ridx = cprow.rindex;
8763447b6efSHong Zhang   } else {
8773447b6efSHong Zhang     ii = a->i;
8783447b6efSHong Zhang   }
87917ab2063SBarry Smith   for (i=0; i<m; i++) {
8803447b6efSHong Zhang     idx   = a->j + ii[i] ;
8813447b6efSHong Zhang     v     = a->a + ii[i] ;
8823447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8833447b6efSHong Zhang     if (usecprow){
8847b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8853447b6efSHong Zhang     } else {
88617ab2063SBarry Smith       alpha = x[i];
8873447b6efSHong Zhang     }
88817ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
88917ab2063SBarry Smith   }
8905c897100SBarry Smith #endif
891dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
8921ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8931ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8943a40ed3dSBarry Smith   PetscFunctionReturn(0);
89517ab2063SBarry Smith }
89617ab2063SBarry Smith 
8974a2ae208SSatish Balay #undef __FUNCT__
8985c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
899dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9005c897100SBarry Smith {
901dfbe8321SBarry Smith   PetscErrorCode ierr;
9025c897100SBarry Smith 
9035c897100SBarry Smith   PetscFunctionBegin;
904170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9055c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9065c897100SBarry Smith   PetscFunctionReturn(0);
9075c897100SBarry Smith }
9085c897100SBarry Smith 
9095c897100SBarry Smith 
9105c897100SBarry Smith #undef __FUNCT__
9114a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
912dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
91317ab2063SBarry Smith {
914416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
915d9fead3dSBarry Smith   PetscScalar       *y;
91654f21887SBarry Smith   const PetscScalar *x;
91754f21887SBarry Smith   const MatScalar   *aa;
918dfbe8321SBarry Smith   PetscErrorCode    ierr;
919003131ecSBarry Smith   PetscInt          m=A->rmap->n;
920003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9218aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
922362ced78SSatish Balay   PetscScalar       sum;
92397952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
92417ab2063SBarry Smith 
925b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
92697952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
927fee21e36SBarry Smith #endif
928fee21e36SBarry Smith 
9293a40ed3dSBarry Smith   PetscFunctionBegin;
930d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9311ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
93297952fefSHong Zhang   aj  = a->j;
93397952fefSHong Zhang   aa  = a->a;
934416022c9SBarry Smith   ii  = a->i;
9354eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
93697952fefSHong Zhang     m    = a->compressedrow.nrows;
93797952fefSHong Zhang     ii   = a->compressedrow.i;
93897952fefSHong Zhang     ridx = a->compressedrow.rindex;
93997952fefSHong Zhang     for (i=0; i<m; i++){
94097952fefSHong Zhang       n   = ii[i+1] - ii[i];
94197952fefSHong Zhang       aj  = a->j + ii[i];
94297952fefSHong Zhang       aa  = a->a + ii[i];
94397952fefSHong Zhang       sum = 0.0;
944a46b3154SVictor Eijkhout       nonzerorow += (n>0);
945003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
946003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
94797952fefSHong Zhang       y[*ridx++] = sum;
94897952fefSHong Zhang     }
94997952fefSHong Zhang   } else { /* do not use compressed row format */
950b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
951b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
952b05257ddSBarry Smith #else
95317ab2063SBarry Smith     for (i=0; i<m; i++) {
954003131ecSBarry Smith       n   = ii[i+1] - ii[i];
955003131ecSBarry Smith       aj  = a->j + ii[i];
956003131ecSBarry Smith       aa  = a->a + ii[i];
95717ab2063SBarry Smith       sum  = 0.0;
958a46b3154SVictor Eijkhout       nonzerorow += (n>0);
959003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
96017ab2063SBarry Smith       y[i] = sum;
96117ab2063SBarry Smith     }
9628d195f9aSBarry Smith #endif
963b05257ddSBarry Smith   }
964dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
965d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9661ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9673a40ed3dSBarry Smith   PetscFunctionReturn(0);
96817ab2063SBarry Smith }
96917ab2063SBarry Smith 
9704a2ae208SSatish Balay #undef __FUNCT__
9714a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
972dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
97317ab2063SBarry Smith {
974416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
97554f21887SBarry Smith   PetscScalar     *x,*y,*z;
97654f21887SBarry Smith   const MatScalar *aa;
977dfbe8321SBarry Smith   PetscErrorCode  ierr;
978d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
979aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
98097952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
981362ced78SSatish Balay   PetscScalar     sum;
98297952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
983e36a17ebSSatish Balay #endif
9849ea0dfa2SSatish Balay 
9853a40ed3dSBarry Smith   PetscFunctionBegin;
9861ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9871ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9882e8a6d31SBarry Smith   if (zz != yy) {
9891ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9902e8a6d31SBarry Smith   } else {
9912e8a6d31SBarry Smith     z = y;
9922e8a6d31SBarry Smith   }
993bfeeae90SHong Zhang 
99497952fefSHong Zhang   aj  = a->j;
99597952fefSHong Zhang   aa  = a->a;
996cddf8d76SBarry Smith   ii  = a->i;
997aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
99897952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
99902ab625aSSatish Balay #else
10004eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10014eb6d288SHong Zhang     if (zz != yy){
10024eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10034eb6d288SHong Zhang     }
100497952fefSHong Zhang     m    = a->compressedrow.nrows;
100597952fefSHong Zhang     ii   = a->compressedrow.i;
100697952fefSHong Zhang     ridx = a->compressedrow.rindex;
100797952fefSHong Zhang     for (i=0; i<m; i++){
100897952fefSHong Zhang       n  = ii[i+1] - ii[i];
100997952fefSHong Zhang       aj  = a->j + ii[i];
101097952fefSHong Zhang       aa  = a->a + ii[i];
101197952fefSHong Zhang       sum = y[*ridx];
101297952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
101397952fefSHong Zhang       z[*ridx++] = sum;
101497952fefSHong Zhang     }
101597952fefSHong Zhang   } else { /* do not use compressed row format */
101617ab2063SBarry Smith     for (i=0; i<m; i++) {
10179ea0dfa2SSatish Balay       jrow = ii[i];
10189ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
101917ab2063SBarry Smith       sum  = y[i];
10209ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
102197952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10229ea0dfa2SSatish Balay       }
102317ab2063SBarry Smith       z[i] = sum;
102417ab2063SBarry Smith     }
102597952fefSHong Zhang   }
102602ab625aSSatish Balay #endif
1027dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10281ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10291ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10302e8a6d31SBarry Smith   if (zz != yy) {
10311ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10322e8a6d31SBarry Smith   }
10333a40ed3dSBarry Smith   PetscFunctionReturn(0);
103417ab2063SBarry Smith }
103517ab2063SBarry Smith 
103617ab2063SBarry Smith /*
103717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
103817ab2063SBarry Smith */
10394a2ae208SSatish Balay #undef __FUNCT__
10404a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1041dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
104217ab2063SBarry Smith {
1043416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10446849ba73SBarry Smith   PetscErrorCode ierr;
1045d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
104617ab2063SBarry Smith 
10473a40ed3dSBarry Smith   PetscFunctionBegin;
104809f38230SBarry Smith   if (!a->diag) {
104909f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10509518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
105109f38230SBarry Smith   }
1052d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
105309f38230SBarry Smith     a->diag[i] = a->i[i+1];
1054bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1055bfeeae90SHong Zhang       if (a->j[j] == i) {
105609f38230SBarry Smith         a->diag[i] = j;
105717ab2063SBarry Smith         break;
105817ab2063SBarry Smith       }
105917ab2063SBarry Smith     }
106017ab2063SBarry Smith   }
10613a40ed3dSBarry Smith   PetscFunctionReturn(0);
106217ab2063SBarry Smith }
106317ab2063SBarry Smith 
1064be5855fcSBarry Smith /*
1065be5855fcSBarry Smith      Checks for missing diagonals
1066be5855fcSBarry Smith */
10674a2ae208SSatish Balay #undef __FUNCT__
10684a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
106909f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1070be5855fcSBarry Smith {
1071be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
107297f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1073be5855fcSBarry Smith 
1074be5855fcSBarry Smith   PetscFunctionBegin;
107509f38230SBarry Smith   *missing = PETSC_FALSE;
1076d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
107709f38230SBarry Smith     *missing  = PETSC_TRUE;
107809f38230SBarry Smith     if (d) *d = 0;
107909f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
108009f38230SBarry Smith   } else {
1081f1e2ffcdSBarry Smith     diag = a->diag;
1082d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1083bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
108409f38230SBarry Smith 	*missing = PETSC_TRUE;
108509f38230SBarry Smith 	if (d) *d = i;
108609f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
108709f38230SBarry Smith       }
1088be5855fcSBarry Smith     }
1089be5855fcSBarry Smith   }
1090be5855fcSBarry Smith   PetscFunctionReturn(0);
1091be5855fcSBarry Smith }
1092be5855fcSBarry Smith 
109371f1c65dSBarry Smith EXTERN_C_BEGIN
109471f1c65dSBarry Smith #undef __FUNCT__
109571f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
109671f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
109771f1c65dSBarry Smith {
109871f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
109971f1c65dSBarry Smith   PetscErrorCode ierr;
1100d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
110154f21887SBarry Smith   MatScalar      *v = a->a;
110254f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
110371f1c65dSBarry Smith 
110471f1c65dSBarry Smith   PetscFunctionBegin;
110571f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
110671f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
110771f1c65dSBarry Smith   diag = a->diag;
110871f1c65dSBarry Smith   if (!a->idiag) {
110971f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
111071f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
111171f1c65dSBarry Smith     v        = a->a;
111271f1c65dSBarry Smith   }
111371f1c65dSBarry Smith   mdiag = a->mdiag;
111471f1c65dSBarry Smith   idiag = a->idiag;
111571f1c65dSBarry Smith 
1116028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
111771f1c65dSBarry Smith     for (i=0; i<m; i++) {
111871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
111971f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
112071f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
112171f1c65dSBarry Smith     }
112271f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
112371f1c65dSBarry Smith   } else {
112471f1c65dSBarry Smith     for (i=0; i<m; i++) {
112571f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
112671f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
112771f1c65dSBarry Smith     }
1128dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
112971f1c65dSBarry Smith   }
113071f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
113171f1c65dSBarry Smith   PetscFunctionReturn(0);
113271f1c65dSBarry Smith }
11335a9745a3SMatthew Knepley EXTERN_C_END
113471f1c65dSBarry Smith 
11354a2ae208SSatish Balay #undef __FUNCT__
11364a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
113797f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
113817ab2063SBarry Smith {
1139416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1140e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1141e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
114254f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1143dfbe8321SBarry Smith   PetscErrorCode     ierr;
1144d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
114597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
114617ab2063SBarry Smith 
11473a40ed3dSBarry Smith   PetscFunctionBegin;
1148b965ef7fSBarry Smith   its = its*lits;
114991723122SBarry Smith 
115071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
115171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
115271f1c65dSBarry Smith   a->fshift = fshift;
115371f1c65dSBarry Smith   a->omega  = omega;
1154ed480e8bSBarry Smith 
115571f1c65dSBarry Smith   diag = a->diag;
115671f1c65dSBarry Smith   t     = a->ssor_work;
1157ed480e8bSBarry Smith   idiag = a->idiag;
115871f1c65dSBarry Smith   mdiag = a->mdiag;
1159ed480e8bSBarry Smith 
11601ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1161fb2e594dSBarry Smith   if (xx != bb) {
11621ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1163fb2e594dSBarry Smith   } else {
1164fb2e594dSBarry Smith     b = x;
1165fb2e594dSBarry Smith   }
116671f1c65dSBarry Smith   CHKMEMQ;
1167ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
116817ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
116917ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1170ed480e8bSBarry Smith     bs = b;
117117ab2063SBarry Smith     for (i=0; i<m; i++) {
117271f1c65dSBarry Smith         d    = fshift + mdiag[i];
1173416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1174ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1175ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
117617ab2063SBarry Smith         sum  = b[i]*d/omega;
1177003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
117817ab2063SBarry Smith         x[i] = sum;
117917ab2063SBarry Smith     }
11801ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11811ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1182efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11833a40ed3dSBarry Smith     PetscFunctionReturn(0);
118417ab2063SBarry Smith   }
1185c783ea89SBarry Smith 
118648af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
118729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11883a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
118917ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
119017ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
119117ab2063SBarry Smith 
119217ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
119317ab2063SBarry Smith 
119417ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
119517ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
119617ab2063SBarry Smith     is the relaxation factor.
119717ab2063SBarry Smith     */
119817ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
119917ab2063SBarry Smith 
120017ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
120117ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1202416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1203ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1204ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
120517ab2063SBarry Smith       sum  = b[i];
1206e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1207ed480e8bSBarry Smith       x[i] = sum*idiag[i];
120817ab2063SBarry Smith     }
120917ab2063SBarry Smith 
121017ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1211416022c9SBarry Smith     v = a->a;
1212ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
121317ab2063SBarry Smith 
121417ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1215ed480e8bSBarry Smith     ts = t;
1216416022c9SBarry Smith     diag = a->diag;
121717ab2063SBarry Smith     for (i=0; i<m; i++) {
1218416022c9SBarry Smith       n    = diag[i] - a->i[i];
1219ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1220ed480e8bSBarry Smith       v    = a->a + a->i[i];
122117ab2063SBarry Smith       sum  = t[i];
1222003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1223ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1224733d66baSBarry Smith       /*  x = x + t */
1225733d66baSBarry Smith       x[i] += t[i];
122617ab2063SBarry Smith     }
122717ab2063SBarry Smith 
1228dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
12291ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12301ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12313a40ed3dSBarry Smith     PetscFunctionReturn(0);
123217ab2063SBarry Smith   }
123317ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
123417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
123577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
123697f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
123777d8c4bbSBarry Smith #else
123817ab2063SBarry Smith       for (i=0; i<m; i++) {
1239416022c9SBarry Smith         n    = diag[i] - a->i[i];
1240ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1241ed480e8bSBarry Smith         v    = a->a + a->i[i];
124217ab2063SBarry Smith         sum  = b[i];
1243e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1244ed480e8bSBarry Smith         x[i] = sum*idiag[i];
124517ab2063SBarry Smith       }
124677d8c4bbSBarry Smith #endif
124717ab2063SBarry Smith       xb = x;
1248efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12493a40ed3dSBarry Smith     } else xb = b;
125017ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
125117ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
125217ab2063SBarry Smith       for (i=0; i<m; i++) {
1253ed480e8bSBarry Smith         x[i] *= mdiag[i];
125417ab2063SBarry Smith       }
1255efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
125617ab2063SBarry Smith     }
125717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
125877d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
125997f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
126077d8c4bbSBarry Smith #else
126117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1262416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1263ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1264ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
126517ab2063SBarry Smith         sum  = xb[i];
1266e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1267ed480e8bSBarry Smith         x[i] = sum*idiag[i];
126817ab2063SBarry Smith       }
126977d8c4bbSBarry Smith #endif
1270efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
127117ab2063SBarry Smith     }
127217ab2063SBarry Smith     its--;
127317ab2063SBarry Smith   }
127417ab2063SBarry Smith   while (its--) {
127517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
127677d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
127797f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
127877d8c4bbSBarry Smith #else
127917ab2063SBarry Smith       for (i=0; i<m; i++) {
1280416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1281ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1282ed480e8bSBarry Smith         v    = a->a + a->i[i];
128317ab2063SBarry Smith         sum  = b[i];
1284e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1285ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
128617ab2063SBarry Smith       }
128777d8c4bbSBarry Smith #endif
1288efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
128917ab2063SBarry Smith     }
129017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
129177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
129297f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
129377d8c4bbSBarry Smith #else
129417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1295416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1296ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1297ed480e8bSBarry Smith         v    = a->a + a->i[i];
129817ab2063SBarry Smith         sum  = b[i];
1299e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1300ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
130117ab2063SBarry Smith       }
130277d8c4bbSBarry Smith #endif
1303efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
130417ab2063SBarry Smith     }
130517ab2063SBarry Smith   }
13061ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13071ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
130871f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
130917ab2063SBarry Smith }
131017ab2063SBarry Smith 
13112af78befSBarry Smith 
13124a2ae208SSatish Balay #undef __FUNCT__
13134a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1314dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
131517ab2063SBarry Smith {
1316416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13174e220ebcSLois Curfman McInnes 
13183a40ed3dSBarry Smith   PetscFunctionBegin;
13194e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13204e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13214e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13224e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13234e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13244e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13257adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13264e220ebcSLois Curfman McInnes   if (A->factor) {
13274e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13284e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13294e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13304e220ebcSLois Curfman McInnes   } else {
13314e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13324e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13334e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13344e220ebcSLois Curfman McInnes   }
13353a40ed3dSBarry Smith   PetscFunctionReturn(0);
133617ab2063SBarry Smith }
133717ab2063SBarry Smith 
13384a2ae208SSatish Balay #undef __FUNCT__
13394a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1340f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
134117ab2063SBarry Smith {
1342416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1343d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d;
13446849ba73SBarry Smith   PetscErrorCode ierr;
134509f38230SBarry Smith   PetscTruth     missing;
134617ab2063SBarry Smith 
13473a40ed3dSBarry Smith   PetscFunctionBegin;
1348a9817697SBarry Smith   if (a->keepnonzeropattern) {
1349f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
135077431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1351bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1352f1e2ffcdSBarry Smith     }
1353f4df32b1SMatthew Knepley     if (diag != 0.0) {
135409f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
135509f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1356f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1357f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1358f1e2ffcdSBarry Smith       }
1359f1e2ffcdSBarry Smith     }
136088e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1361f1e2ffcdSBarry Smith   } else {
1362f4df32b1SMatthew Knepley     if (diag != 0.0) {
136317ab2063SBarry Smith       for (i=0; i<N; i++) {
136477431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13657ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1366416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1367f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1368bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13697ae801bdSBarry Smith         } else { /* in case row was completely empty */
1370f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
137117ab2063SBarry Smith         }
137217ab2063SBarry Smith       }
13733a40ed3dSBarry Smith     } else {
137417ab2063SBarry Smith       for (i=0; i<N; i++) {
137577431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1376416022c9SBarry Smith         a->ilen[rows[i]] = 0;
137717ab2063SBarry Smith       }
137817ab2063SBarry Smith     }
137988e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1380f1e2ffcdSBarry Smith   }
138143a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13823a40ed3dSBarry Smith   PetscFunctionReturn(0);
138317ab2063SBarry Smith }
138417ab2063SBarry Smith 
13854a2ae208SSatish Balay #undef __FUNCT__
13864a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1387a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
138817ab2063SBarry Smith {
1389416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
139097f1f81fSBarry Smith   PetscInt   *itmp;
139117ab2063SBarry Smith 
13923a40ed3dSBarry Smith   PetscFunctionBegin;
1393d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
139417ab2063SBarry Smith 
1395416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1396bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
139717ab2063SBarry Smith   if (idx) {
1398bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1399bfeeae90SHong Zhang     if (*nz) {
14004e093b46SBarry Smith       *idx = itmp;
140117ab2063SBarry Smith     }
140217ab2063SBarry Smith     else *idx = 0;
140317ab2063SBarry Smith   }
14043a40ed3dSBarry Smith   PetscFunctionReturn(0);
140517ab2063SBarry Smith }
140617ab2063SBarry Smith 
1407bfeeae90SHong Zhang /* remove this function? */
14084a2ae208SSatish Balay #undef __FUNCT__
14094a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1410a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
141117ab2063SBarry Smith {
14123a40ed3dSBarry Smith   PetscFunctionBegin;
14133a40ed3dSBarry Smith   PetscFunctionReturn(0);
141417ab2063SBarry Smith }
141517ab2063SBarry Smith 
14164a2ae208SSatish Balay #undef __FUNCT__
14174a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1418dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
141917ab2063SBarry Smith {
1420416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
142154f21887SBarry Smith   MatScalar      *v = a->a;
142236db0b34SBarry Smith   PetscReal      sum = 0.0;
14236849ba73SBarry Smith   PetscErrorCode ierr;
142497f1f81fSBarry Smith   PetscInt       i,j;
142517ab2063SBarry Smith 
14263a40ed3dSBarry Smith   PetscFunctionBegin;
142717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1428416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1429aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
143036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
143117ab2063SBarry Smith #else
143217ab2063SBarry Smith       sum += (*v)*(*v); v++;
143317ab2063SBarry Smith #endif
143417ab2063SBarry Smith     }
1435064f8208SBarry Smith     *nrm = sqrt(sum);
14363a40ed3dSBarry Smith   } else if (type == NORM_1) {
143736db0b34SBarry Smith     PetscReal *tmp;
143897f1f81fSBarry Smith     PetscInt    *jj = a->j;
1439d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1440d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1441064f8208SBarry Smith     *nrm = 0.0;
1442416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1443bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
144417ab2063SBarry Smith     }
1445d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1446064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
144717ab2063SBarry Smith     }
1448606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14493a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1450064f8208SBarry Smith     *nrm = 0.0;
1451d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1452bfeeae90SHong Zhang       v = a->a + a->i[j];
145317ab2063SBarry Smith       sum = 0.0;
1454416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1455cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
145617ab2063SBarry Smith       }
1457064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
145817ab2063SBarry Smith     }
14593a40ed3dSBarry Smith   } else {
146029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
146117ab2063SBarry Smith   }
14623a40ed3dSBarry Smith   PetscFunctionReturn(0);
146317ab2063SBarry Smith }
146417ab2063SBarry Smith 
14654a2ae208SSatish Balay #undef __FUNCT__
14664a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1467fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
146817ab2063SBarry Smith {
1469416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1470416022c9SBarry Smith   Mat            C;
14716849ba73SBarry Smith   PetscErrorCode ierr;
1472d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
147354f21887SBarry Smith   MatScalar      *array = a->a;
147417ab2063SBarry Smith 
14753a40ed3dSBarry Smith   PetscFunctionBegin;
1476d0f46423SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1477fc4dec0aSBarry Smith 
1478fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1479d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1480d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1481bfeeae90SHong Zhang 
1482bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14837adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1484d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
14857adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1486ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1487606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1488a541d17aSBarry Smith   } else {
1489a541d17aSBarry Smith     C = *B;
1490a541d17aSBarry Smith   }
1491a541d17aSBarry Smith 
149217ab2063SBarry Smith   for (i=0; i<m; i++) {
149317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
149487d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1495b9b97703SBarry Smith     array += len;
1496b9b97703SBarry Smith     aj    += len;
149717ab2063SBarry Smith   }
14986d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14996d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
150017ab2063SBarry Smith 
1501815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1502416022c9SBarry Smith     *B = C;
150317ab2063SBarry Smith   } else {
1504273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
150517ab2063SBarry Smith   }
15063a40ed3dSBarry Smith   PetscFunctionReturn(0);
150717ab2063SBarry Smith }
150817ab2063SBarry Smith 
1509cd0d46ebSvictorle EXTERN_C_BEGIN
1510cd0d46ebSvictorle #undef __FUNCT__
15115fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1512be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1513cd0d46ebSvictorle {
1514cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
151554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
151654f21887SBarry Smith   MatScalar      *va,*vb;
15176849ba73SBarry Smith   PetscErrorCode ierr;
151897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1519cd0d46ebSvictorle 
1520cd0d46ebSvictorle   PetscFunctionBegin;
1521cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1522cd0d46ebSvictorle 
1523cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1524cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15255485867bSBarry Smith   if (ma!=nb || na!=mb){
15265485867bSBarry Smith     *f = PETSC_FALSE;
15275485867bSBarry Smith     PetscFunctionReturn(0);
15285485867bSBarry Smith   }
1529cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1530cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1531cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
153297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
153397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1534cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1535cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1536cd0d46ebSvictorle 
1537cd0d46ebSvictorle   *f = PETSC_TRUE;
1538cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1539cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
154097f1f81fSBarry Smith       PetscInt         idc,idr;
15415485867bSBarry Smith       PetscScalar vc,vr;
1542cd0d46ebSvictorle       /* column/row index/value */
15435485867bSBarry Smith       idc = adx[aptr[i]];
15445485867bSBarry Smith       idr = bdx[bptr[idc]];
15455485867bSBarry Smith       vc  = va[aptr[i]];
15465485867bSBarry Smith       vr  = vb[bptr[idc]];
15475485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15485485867bSBarry Smith 	*f = PETSC_FALSE;
15495485867bSBarry Smith         goto done;
1550cd0d46ebSvictorle       } else {
15515485867bSBarry Smith 	aptr[i]++;
15525485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1553cd0d46ebSvictorle       }
1554cd0d46ebSvictorle     }
1555cd0d46ebSvictorle   }
1556cd0d46ebSvictorle  done:
1557cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15583aeef889SHong Zhang   if (B) {
15593aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15603aeef889SHong Zhang   }
1561cd0d46ebSvictorle   PetscFunctionReturn(0);
1562cd0d46ebSvictorle }
1563cd0d46ebSvictorle EXTERN_C_END
1564cd0d46ebSvictorle 
15651cbb95d3SBarry Smith EXTERN_C_BEGIN
15661cbb95d3SBarry Smith #undef __FUNCT__
15671cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15681cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15691cbb95d3SBarry Smith {
15701cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
157154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
157254f21887SBarry Smith   MatScalar      *va,*vb;
15731cbb95d3SBarry Smith   PetscErrorCode ierr;
15741cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15751cbb95d3SBarry Smith 
15761cbb95d3SBarry Smith   PetscFunctionBegin;
15771cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15781cbb95d3SBarry Smith 
15791cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15801cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15811cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15821cbb95d3SBarry Smith     *f = PETSC_FALSE;
15831cbb95d3SBarry Smith     PetscFunctionReturn(0);
15841cbb95d3SBarry Smith   }
15851cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15861cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15871cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
15881cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
15891cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
15901cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
15911cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
15921cbb95d3SBarry Smith 
15931cbb95d3SBarry Smith   *f = PETSC_TRUE;
15941cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
15951cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
15961cbb95d3SBarry Smith       PetscInt         idc,idr;
15971cbb95d3SBarry Smith       PetscScalar vc,vr;
15981cbb95d3SBarry Smith       /* column/row index/value */
15991cbb95d3SBarry Smith       idc = adx[aptr[i]];
16001cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16011cbb95d3SBarry Smith       vc  = va[aptr[i]];
16021cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16031cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16041cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16051cbb95d3SBarry Smith         goto done;
16061cbb95d3SBarry Smith       } else {
16071cbb95d3SBarry Smith 	aptr[i]++;
16081cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16091cbb95d3SBarry Smith       }
16101cbb95d3SBarry Smith     }
16111cbb95d3SBarry Smith   }
16121cbb95d3SBarry Smith  done:
16131cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16141cbb95d3SBarry Smith   if (B) {
16151cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16161cbb95d3SBarry Smith   }
16171cbb95d3SBarry Smith   PetscFunctionReturn(0);
16181cbb95d3SBarry Smith }
16191cbb95d3SBarry Smith EXTERN_C_END
16201cbb95d3SBarry Smith 
16219e29f15eSvictorle #undef __FUNCT__
16229e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1623dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16249e29f15eSvictorle {
1625dfbe8321SBarry Smith   PetscErrorCode ierr;
16269e29f15eSvictorle   PetscFunctionBegin;
16275485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16289e29f15eSvictorle   PetscFunctionReturn(0);
16299e29f15eSvictorle }
16309e29f15eSvictorle 
16314a2ae208SSatish Balay #undef __FUNCT__
16321cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16331cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16341cbb95d3SBarry Smith {
16351cbb95d3SBarry Smith   PetscErrorCode ierr;
16361cbb95d3SBarry Smith   PetscFunctionBegin;
16371cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16381cbb95d3SBarry Smith   PetscFunctionReturn(0);
16391cbb95d3SBarry Smith }
16401cbb95d3SBarry Smith 
16411cbb95d3SBarry Smith #undef __FUNCT__
16424a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1643dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
164417ab2063SBarry Smith {
1645416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
164654f21887SBarry Smith   PetscScalar    *l,*r,x;
164754f21887SBarry Smith   MatScalar      *v;
1648dfbe8321SBarry Smith   PetscErrorCode ierr;
1649d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
165017ab2063SBarry Smith 
16513a40ed3dSBarry Smith   PetscFunctionBegin;
165217ab2063SBarry Smith   if (ll) {
16533ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16543ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1655e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1656d0f46423SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16571ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1658416022c9SBarry Smith     v = a->a;
165917ab2063SBarry Smith     for (i=0; i<m; i++) {
166017ab2063SBarry Smith       x = l[i];
1661416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
166217ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
166317ab2063SBarry Smith     }
16641ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1665efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
166617ab2063SBarry Smith   }
166717ab2063SBarry Smith   if (rr) {
1668e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1669d0f46423SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16701ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1671416022c9SBarry Smith     v = a->a; jj = a->j;
167217ab2063SBarry Smith     for (i=0; i<nz; i++) {
1673bfeeae90SHong Zhang       (*v++) *= r[*jj++];
167417ab2063SBarry Smith     }
16751ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1676efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
167717ab2063SBarry Smith   }
16783a40ed3dSBarry Smith   PetscFunctionReturn(0);
167917ab2063SBarry Smith }
168017ab2063SBarry Smith 
16814a2ae208SSatish Balay #undef __FUNCT__
16824a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
168397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
168417ab2063SBarry Smith {
1685db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16866849ba73SBarry Smith   PetscErrorCode ierr;
1687d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
168897f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
16895d0c19d7SBarry Smith   const PetscInt *irow,*icol;
16905d0c19d7SBarry Smith   PetscInt       nrows,ncols;
169197f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
169254f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1693416022c9SBarry Smith   Mat            C;
169414ca34e6SBarry Smith   PetscTruth     stride,sorted;
169517ab2063SBarry Smith 
16963a40ed3dSBarry Smith   PetscFunctionBegin;
169714ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
169814ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
169914ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
170014ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
170199141d43SSatish Balay 
170217ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1703b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1704b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
170517ab2063SBarry Smith 
1706fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1707fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1708fee21e36SBarry Smith   if (stride && step == 1) {
170902834360SBarry Smith     /* special case of contiguous rows */
171097f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
171131ebf83bSSatish Balay     starts = lens + nrows;
171202834360SBarry Smith     /* loop over new rows determining lens and starting points */
171302834360SBarry Smith     for (i=0; i<nrows; i++) {
1714bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1715a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
171602834360SBarry Smith       for (k=kstart; k<kend; k++) {
1717bfeeae90SHong Zhang         if (aj[k] >= first) {
171802834360SBarry Smith           starts[i] = k;
171902834360SBarry Smith           break;
172002834360SBarry Smith 	}
172102834360SBarry Smith       }
1722a2744918SBarry Smith       sum = 0;
172302834360SBarry Smith       while (k < kend) {
1724bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1725a2744918SBarry Smith         sum++;
172602834360SBarry Smith       }
1727a2744918SBarry Smith       lens[i] = sum;
172802834360SBarry Smith     }
172902834360SBarry Smith     /* create submatrix */
1730cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
173197f1f81fSBarry Smith       PetscInt n_cols,n_rows;
173208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
173329bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1734d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
173508480c60SBarry Smith       C = *B;
17363a40ed3dSBarry Smith     } else {
17377adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1738f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17397adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1740ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
174108480c60SBarry Smith     }
1742db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1743db02288aSLois Curfman McInnes 
174402834360SBarry Smith     /* loop over rows inserting into submatrix */
1745db02288aSLois Curfman McInnes     a_new    = c->a;
1746db02288aSLois Curfman McInnes     j_new    = c->j;
1747db02288aSLois Curfman McInnes     i_new    = c->i;
1748bfeeae90SHong Zhang 
174902834360SBarry Smith     for (i=0; i<nrows; i++) {
1750a2744918SBarry Smith       ii    = starts[i];
1751a2744918SBarry Smith       lensi = lens[i];
1752a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1753a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
175402834360SBarry Smith       }
175587828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1756a2744918SBarry Smith       a_new      += lensi;
1757a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1758a2744918SBarry Smith       c->ilen[i]  = lensi;
175902834360SBarry Smith     }
1760606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17613a40ed3dSBarry Smith   } else {
176202834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
176397f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1764bfeeae90SHong Zhang 
176597f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
176697f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
176717ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
176802834360SBarry Smith     /* determine lens of each row */
176902834360SBarry Smith     for (i=0; i<nrows; i++) {
1770bfeeae90SHong Zhang       kstart  = ai[irow[i]];
177102834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
177202834360SBarry Smith       lens[i] = 0;
177302834360SBarry Smith       for (k=kstart; k<kend; k++) {
1774bfeeae90SHong Zhang         if (smap[aj[k]]) {
177502834360SBarry Smith           lens[i]++;
177602834360SBarry Smith         }
177702834360SBarry Smith       }
177802834360SBarry Smith     }
177917ab2063SBarry Smith     /* Create and fill new matrix */
1780a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17810f5bd95cSBarry Smith       PetscTruth equal;
17820f5bd95cSBarry Smith 
178399141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1784d0f46423SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1785d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17860f5bd95cSBarry Smith       if (!equal) {
178729bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
178899141d43SSatish Balay       }
1789d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
179008480c60SBarry Smith       C = *B;
17913a40ed3dSBarry Smith     } else {
17927adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1793f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17947adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1795ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
179608480c60SBarry Smith     }
179799141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
179817ab2063SBarry Smith     for (i=0; i<nrows; i++) {
179999141d43SSatish Balay       row    = irow[i];
1800bfeeae90SHong Zhang       kstart = ai[row];
180199141d43SSatish Balay       kend   = kstart + a->ilen[row];
1802bfeeae90SHong Zhang       mat_i  = c->i[i];
180399141d43SSatish Balay       mat_j  = c->j + mat_i;
180499141d43SSatish Balay       mat_a  = c->a + mat_i;
180599141d43SSatish Balay       mat_ilen = c->ilen + i;
180617ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1807bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1808ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
180999141d43SSatish Balay           *mat_a++ = a->a[k];
181099141d43SSatish Balay           (*mat_ilen)++;
181199141d43SSatish Balay 
181217ab2063SBarry Smith         }
181317ab2063SBarry Smith       }
181417ab2063SBarry Smith     }
181502834360SBarry Smith     /* Free work space */
181602834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1817606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1818606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
181902834360SBarry Smith   }
18206d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18216d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
182217ab2063SBarry Smith 
182317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1824416022c9SBarry Smith   *B = C;
18253a40ed3dSBarry Smith   PetscFunctionReturn(0);
182617ab2063SBarry Smith }
182717ab2063SBarry Smith 
1828a871dcd8SBarry Smith /*
1829a871dcd8SBarry Smith */
18304a2ae208SSatish Balay #undef __FUNCT__
18314a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18320481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1833a871dcd8SBarry Smith {
183463b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1835dfbe8321SBarry Smith   PetscErrorCode ierr;
183663b91edcSBarry Smith   Mat            outA;
1837b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
183863b91edcSBarry Smith 
18393a40ed3dSBarry Smith   PetscFunctionBegin;
1840d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1841b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1842b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1843a871dcd8SBarry Smith 
184463b91edcSBarry Smith   outA          = inA;
18455c9eb25fSBarry Smith   inA->factor   = MAT_FACTOR_LU;
1846c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1847c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1848c3122656SLisandro Dalcin   a->row = row;
1849c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1850c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1851c3122656SLisandro Dalcin   a->col = col;
185263b91edcSBarry Smith 
185336db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1854b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18554c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
185652e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1857f0ec6fceSSatish Balay 
185894a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1859d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1860d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
186194a9d846SBarry Smith   }
186263b91edcSBarry Smith 
1863f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1864137fb511SHong Zhang   if (row_identity && col_identity) {
1865719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ(outA,inA,info);CHKERRQ(ierr);
1866137fb511SHong Zhang   } else {
1867719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1868137fb511SHong Zhang   }
18693a40ed3dSBarry Smith   PetscFunctionReturn(0);
1870a871dcd8SBarry Smith }
1871a871dcd8SBarry Smith 
1872d9eff348SSatish Balay #include "petscblaslapack.h"
18734a2ae208SSatish Balay #undef __FUNCT__
18744a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1875f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1876f0b747eeSBarry Smith {
1877f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1878f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1879efee365bSSatish Balay   PetscErrorCode ierr;
18800805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18813a40ed3dSBarry Smith 
18823a40ed3dSBarry Smith   PetscFunctionBegin;
1883f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1884efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18853a40ed3dSBarry Smith   PetscFunctionReturn(0);
1886f0b747eeSBarry Smith }
1887f0b747eeSBarry Smith 
18884a2ae208SSatish Balay #undef __FUNCT__
18894a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
189097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1891cddf8d76SBarry Smith {
1892dfbe8321SBarry Smith   PetscErrorCode ierr;
189397f1f81fSBarry Smith   PetscInt       i;
1894cddf8d76SBarry Smith 
18953a40ed3dSBarry Smith   PetscFunctionBegin;
1896cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1897b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1898cddf8d76SBarry Smith   }
1899cddf8d76SBarry Smith 
1900cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19016a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1902cddf8d76SBarry Smith   }
19033a40ed3dSBarry Smith   PetscFunctionReturn(0);
1904cddf8d76SBarry Smith }
1905cddf8d76SBarry Smith 
19064a2ae208SSatish Balay #undef __FUNCT__
19074a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
190897f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19094dcbc457SBarry Smith {
1910e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19116849ba73SBarry Smith   PetscErrorCode ierr;
19125d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19135d0c19d7SBarry Smith   const PetscInt *idx;
191497f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1915f1af5d2fSBarry Smith   PetscBT        table;
1916bbd702dbSSatish Balay 
19173a40ed3dSBarry Smith   PetscFunctionBegin;
1918d0f46423SBarry Smith   m     = A->rmap->n;
1919e4d965acSSatish Balay   ai    = a->i;
1920bfeeae90SHong Zhang   aj    = a->j;
19218a047759SSatish Balay 
1922a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
192306763907SSatish Balay 
192497f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19256831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
192606763907SSatish Balay 
1927e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1928b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1929e4d965acSSatish Balay     isz  = 0;
19306831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1931e4d965acSSatish Balay 
1932e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19334dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1934b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1935e4d965acSSatish Balay 
1936dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1937e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1938f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19394dcbc457SBarry Smith     }
194006763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
194106763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1942e4d965acSSatish Balay 
194304a348a9SBarry Smith     k = 0;
194404a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
194504a348a9SBarry Smith       n = isz;
194606763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1947e4d965acSSatish Balay         row   = nidx[k];
1948e4d965acSSatish Balay         start = ai[row];
1949e4d965acSSatish Balay         end   = ai[row+1];
195004a348a9SBarry Smith         for (l = start; l<end ; l++){
1951efb16452SHong Zhang           val = aj[l] ;
1952f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1953e4d965acSSatish Balay         }
1954e4d965acSSatish Balay       }
1955e4d965acSSatish Balay     }
1956029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1957e4d965acSSatish Balay   }
19586831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1959606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19603a40ed3dSBarry Smith   PetscFunctionReturn(0);
19614dcbc457SBarry Smith }
196217ab2063SBarry Smith 
19630513a670SBarry Smith /* -------------------------------------------------------------- */
19644a2ae208SSatish Balay #undef __FUNCT__
19654a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1966dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19670513a670SBarry Smith {
19680513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19696849ba73SBarry Smith   PetscErrorCode ierr;
19705d0c19d7SBarry Smith   PetscInt       i,nz,m = A->rmap->n,n = A->cmap->n;
19715d0c19d7SBarry Smith   const PetscInt *row,*col;
19725d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
197356cd22aeSBarry Smith   IS             icolp,irowp;
197497f1f81fSBarry Smith   PetscInt       *cwork;
197532ec9ce4SBarry Smith   PetscScalar    *vwork;
19760513a670SBarry Smith 
19773a40ed3dSBarry Smith   PetscFunctionBegin;
19784c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
197956cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19804c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
198156cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19820513a670SBarry Smith 
19830513a670SBarry Smith   /* determine lengths of permuted rows */
198497f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19850513a670SBarry Smith   for (i=0; i<m; i++) {
19860513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19870513a670SBarry Smith   }
19887adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
1989f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
19907adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1991ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1992606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
19930513a670SBarry Smith 
199497f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
19950513a670SBarry Smith   for (i=0; i<m; i++) {
199632ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19970513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1998cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
199932ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20000513a670SBarry Smith   }
2001606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20023c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20030513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20040513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
200556cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
200656cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
200756cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
200856cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20093a40ed3dSBarry Smith   PetscFunctionReturn(0);
20100513a670SBarry Smith }
20110513a670SBarry Smith 
20124a2ae208SSatish Balay #undef __FUNCT__
20134a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2014dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2015cb5b572fSBarry Smith {
2016dfbe8321SBarry Smith   PetscErrorCode ierr;
2017cb5b572fSBarry Smith 
2018cb5b572fSBarry Smith   PetscFunctionBegin;
201933f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
202033f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2021be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2022be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2023be6bf707SBarry Smith 
2024d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2025634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2026cb5b572fSBarry Smith     }
2027d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2028cb5b572fSBarry Smith   } else {
2029cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2030cb5b572fSBarry Smith   }
2031cb5b572fSBarry Smith   PetscFunctionReturn(0);
2032cb5b572fSBarry Smith }
2033cb5b572fSBarry Smith 
20344a2ae208SSatish Balay #undef __FUNCT__
20354a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2036dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2037273d9f13SBarry Smith {
2038dfbe8321SBarry Smith   PetscErrorCode ierr;
2039273d9f13SBarry Smith 
2040273d9f13SBarry Smith   PetscFunctionBegin;
2041ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2042273d9f13SBarry Smith   PetscFunctionReturn(0);
2043273d9f13SBarry Smith }
2044273d9f13SBarry Smith 
20454a2ae208SSatish Balay #undef __FUNCT__
20464a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2047a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20486c0721eeSBarry Smith {
20496c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20506c0721eeSBarry Smith   PetscFunctionBegin;
20516c0721eeSBarry Smith   *array = a->a;
20526c0721eeSBarry Smith   PetscFunctionReturn(0);
20536c0721eeSBarry Smith }
20546c0721eeSBarry Smith 
20554a2ae208SSatish Balay #undef __FUNCT__
20564a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2057dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20586c0721eeSBarry Smith {
20596c0721eeSBarry Smith   PetscFunctionBegin;
20606c0721eeSBarry Smith   PetscFunctionReturn(0);
20616c0721eeSBarry Smith }
2062273d9f13SBarry Smith 
2063ee4f033dSBarry Smith #undef __FUNCT__
2064ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2065dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2066ee4f033dSBarry Smith {
20676849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20686849ba73SBarry Smith   PetscErrorCode ierr;
206997f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2070efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
207187828ca2SBarry Smith   PetscScalar    *vscale_array;
2072ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2073ee4f033dSBarry Smith   Vec            w1,w2,w3;
2074ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
207590d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2076ee4f033dSBarry Smith 
2077ee4f033dSBarry Smith   PetscFunctionBegin;
2078ee4f033dSBarry Smith   if (!coloring->w1) {
2079ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
208052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2081ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
208252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2083ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
208452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2085ee4f033dSBarry Smith   }
2086ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2087ee4f033dSBarry Smith 
2088ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
208990d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2090ee4f033dSBarry Smith   if (flg) {
2091ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2092ee4f033dSBarry Smith   } else {
20930b9b6f31SBarry Smith     PetscTruth assembled;
20940b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
20950b9b6f31SBarry Smith     if (assembled) {
2096ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2097ee4f033dSBarry Smith     }
20980b9b6f31SBarry Smith   }
2099ee4f033dSBarry Smith 
2100ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2101ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2102ee4f033dSBarry Smith 
2103ee4f033dSBarry Smith   /*
2104ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2105ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2106ee4f033dSBarry Smith   */
2107ee4f033dSBarry Smith   if (coloring->F) {
2108ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2109ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2110ee4f033dSBarry Smith     if (m1 != m2) {
2111ee4f033dSBarry Smith       coloring->F = 0;
2112ee4f033dSBarry Smith     }
2113ee4f033dSBarry Smith   }
2114ee4f033dSBarry Smith 
2115ee4f033dSBarry Smith   if (coloring->F) {
2116ee4f033dSBarry Smith     w1          = coloring->F;
2117ee4f033dSBarry Smith     coloring->F = 0;
2118ee4f033dSBarry Smith   } else {
211966f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2120ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
212166f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2122ee4f033dSBarry Smith   }
2123ee4f033dSBarry Smith 
2124ee4f033dSBarry Smith   /*
2125ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2126ee4f033dSBarry Smith   */
21271ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21281ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2129ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2130ee4f033dSBarry Smith     /*
2131ee4f033dSBarry Smith        Loop over each column associated with color adding the
2132ee4f033dSBarry Smith        perturbation to the vector w3.
2133ee4f033dSBarry Smith     */
2134ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2135ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2136ee4f033dSBarry Smith       dx  = xx[col];
2137ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2138ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2139ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2140ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2141ee4f033dSBarry Smith #else
2142ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2143ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2144ee4f033dSBarry Smith #endif
2145ee4f033dSBarry Smith       dx                *= epsilon;
2146ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2147ee4f033dSBarry Smith     }
2148ee4f033dSBarry Smith   }
21491ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2150ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2151ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2152ee4f033dSBarry Smith 
2153ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2154ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2155ee4f033dSBarry Smith 
2156ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2157ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2158ee4f033dSBarry Smith 
21591ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2160ee4f033dSBarry Smith   /*
2161ee4f033dSBarry Smith       Loop over each color
2162ee4f033dSBarry Smith   */
2163ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
216449b058dcSBarry Smith     coloring->currentcolor = k;
2165ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21661ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2167ee4f033dSBarry Smith     /*
2168ee4f033dSBarry Smith        Loop over each column associated with color adding the
2169ee4f033dSBarry Smith        perturbation to the vector w3.
2170ee4f033dSBarry Smith     */
2171ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2172ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2173ee4f033dSBarry Smith       dx  = xx[col];
21745b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2175ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2176ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2177ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2178ee4f033dSBarry Smith #else
2179ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2180ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2181ee4f033dSBarry Smith #endif
2182ee4f033dSBarry Smith       dx            *= epsilon;
2183634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2184ee4f033dSBarry Smith       w3_array[col] += dx;
2185ee4f033dSBarry Smith     }
21861ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2187ee4f033dSBarry Smith 
2188ee4f033dSBarry Smith     /*
2189ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2190ee4f033dSBarry Smith     */
2191ee4f033dSBarry Smith 
219266f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2193ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
219466f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2195efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2196ee4f033dSBarry Smith 
2197ee4f033dSBarry Smith     /*
2198ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2199ee4f033dSBarry Smith     */
22001ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2201ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2202ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2203ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2204ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2205ee4f033dSBarry Smith       srow   = row + start;
2206ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2207ee4f033dSBarry Smith     }
22081ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2209ee4f033dSBarry Smith   }
221049b058dcSBarry Smith   coloring->currentcolor = k;
22111ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22121ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2213ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2214ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2215ee4f033dSBarry Smith   PetscFunctionReturn(0);
2216ee4f033dSBarry Smith }
2217ee4f033dSBarry Smith 
2218ac90fabeSBarry Smith #include "petscblaslapack.h"
2219ac90fabeSBarry Smith #undef __FUNCT__
2220ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2221f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2222ac90fabeSBarry Smith {
2223dfbe8321SBarry Smith   PetscErrorCode ierr;
222497f1f81fSBarry Smith   PetscInt       i;
2225ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22260805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2227ac90fabeSBarry Smith 
2228ac90fabeSBarry Smith   PetscFunctionBegin;
2229ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2230f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2231f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2232c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2233a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2234a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2235a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2236a30b2313SHong Zhang     }
2237a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2238d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2239a30b2313SHong Zhang       y->XtoY = X;
2240407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2241c537a176SHong Zhang     }
2242f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22431e2582c4SBarry 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);
2244ac90fabeSBarry Smith   } else {
2245f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2246ac90fabeSBarry Smith   }
2247ac90fabeSBarry Smith   PetscFunctionReturn(0);
2248ac90fabeSBarry Smith }
2249ac90fabeSBarry Smith 
2250521d7252SBarry Smith #undef __FUNCT__
2251521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2252521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2253521d7252SBarry Smith {
2254521d7252SBarry Smith   PetscFunctionBegin;
2255521d7252SBarry Smith   PetscFunctionReturn(0);
2256521d7252SBarry Smith }
2257521d7252SBarry Smith 
2258354c94deSBarry Smith #undef __FUNCT__
2259354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2260354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2261354c94deSBarry Smith {
2262354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2263354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2264354c94deSBarry Smith   PetscInt    i,nz;
2265354c94deSBarry Smith   PetscScalar *a;
2266354c94deSBarry Smith 
2267354c94deSBarry Smith   PetscFunctionBegin;
2268354c94deSBarry Smith   nz = aij->nz;
2269354c94deSBarry Smith   a  = aij->a;
2270354c94deSBarry Smith   for (i=0; i<nz; i++) {
2271354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2272354c94deSBarry Smith   }
2273354c94deSBarry Smith #else
2274354c94deSBarry Smith   PetscFunctionBegin;
2275354c94deSBarry Smith #endif
2276354c94deSBarry Smith   PetscFunctionReturn(0);
2277354c94deSBarry Smith }
2278354c94deSBarry Smith 
2279e34fafa9SBarry Smith #undef __FUNCT__
2280985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2281985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2282e34fafa9SBarry Smith {
2283e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2284e34fafa9SBarry Smith   PetscErrorCode ierr;
2285d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2286e34fafa9SBarry Smith   PetscReal      atmp;
2287985db425SBarry Smith   PetscScalar    *x;
2288e34fafa9SBarry Smith   MatScalar      *aa;
2289e34fafa9SBarry Smith 
2290e34fafa9SBarry Smith   PetscFunctionBegin;
2291e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2292e34fafa9SBarry Smith   aa   = a->a;
2293e34fafa9SBarry Smith   ai   = a->i;
2294e34fafa9SBarry Smith   aj   = a->j;
2295e34fafa9SBarry Smith 
2296985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2297e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2298e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2299d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2300e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2301e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23029189402eSHong Zhang     x[i] = 0.0;
2303e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2304985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2305985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2306985db425SBarry Smith       aa++; aj++;
2307985db425SBarry Smith     }
2308985db425SBarry Smith   }
2309985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2310985db425SBarry Smith   PetscFunctionReturn(0);
2311985db425SBarry Smith }
2312985db425SBarry Smith 
2313985db425SBarry Smith #undef __FUNCT__
2314985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2315985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2316985db425SBarry Smith {
2317985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2318985db425SBarry Smith   PetscErrorCode ierr;
2319d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2320985db425SBarry Smith   PetscScalar    *x;
2321985db425SBarry Smith   MatScalar      *aa;
2322985db425SBarry Smith 
2323985db425SBarry Smith   PetscFunctionBegin;
2324985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2325985db425SBarry Smith   aa   = a->a;
2326985db425SBarry Smith   ai   = a->i;
2327985db425SBarry Smith   aj   = a->j;
2328985db425SBarry Smith 
2329985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2330985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2331985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2332d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2333985db425SBarry Smith   for (i=0; i<m; i++) {
2334985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2335d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2336985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2337985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2338985db425SBarry Smith       x[i] = 0.0;
2339985db425SBarry Smith       if (idx) {
2340985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2341985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2342985db425SBarry Smith           if (aj[j] > j) {
2343985db425SBarry Smith             idx[i] = j;
2344985db425SBarry Smith             break;
2345985db425SBarry Smith           }
2346985db425SBarry Smith         }
2347985db425SBarry Smith       }
2348985db425SBarry Smith     }
2349985db425SBarry Smith     for (j=0; j<ncols; j++){
2350985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2351985db425SBarry Smith       aa++; aj++;
2352985db425SBarry Smith     }
2353985db425SBarry Smith   }
2354985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2355985db425SBarry Smith   PetscFunctionReturn(0);
2356985db425SBarry Smith }
2357985db425SBarry Smith 
2358985db425SBarry Smith #undef __FUNCT__
2359c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2360c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2361c87e5d42SMatthew Knepley {
2362c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2363c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2364c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2365c87e5d42SMatthew Knepley   PetscReal      atmp;
2366c87e5d42SMatthew Knepley   PetscScalar    *x;
2367c87e5d42SMatthew Knepley   MatScalar      *aa;
2368c87e5d42SMatthew Knepley 
2369c87e5d42SMatthew Knepley   PetscFunctionBegin;
2370c87e5d42SMatthew Knepley   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2371c87e5d42SMatthew Knepley   aa   = a->a;
2372c87e5d42SMatthew Knepley   ai   = a->i;
2373c87e5d42SMatthew Knepley   aj   = a->j;
2374c87e5d42SMatthew Knepley 
2375c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2376c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2377c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2378c87e5d42SMatthew Knepley   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2379c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2380c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2381289a08f5SMatthew Knepley     if (ncols) {
2382289a08f5SMatthew Knepley       /* Get first nonzero */
2383289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2384289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2385289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2386289a08f5SMatthew Knepley       }
2387289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2388289a08f5SMatthew Knepley     } else {
2389289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2390289a08f5SMatthew Knepley     }
2391c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2392c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2393289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2394c87e5d42SMatthew Knepley       aa++; aj++;
2395c87e5d42SMatthew Knepley     }
2396c87e5d42SMatthew Knepley   }
2397c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2398c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2399c87e5d42SMatthew Knepley }
2400c87e5d42SMatthew Knepley 
2401c87e5d42SMatthew Knepley #undef __FUNCT__
2402985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2403985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2404985db425SBarry Smith {
2405985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2406985db425SBarry Smith   PetscErrorCode ierr;
2407d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2408985db425SBarry Smith   PetscScalar    *x;
2409985db425SBarry Smith   MatScalar      *aa;
2410985db425SBarry Smith 
2411985db425SBarry Smith   PetscFunctionBegin;
2412985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2413985db425SBarry Smith   aa   = a->a;
2414985db425SBarry Smith   ai   = a->i;
2415985db425SBarry Smith   aj   = a->j;
2416985db425SBarry Smith 
2417985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2418985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2419985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2420d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2421985db425SBarry Smith   for (i=0; i<m; i++) {
2422985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2423d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2424985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2425985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2426985db425SBarry Smith       x[i] = 0.0;
2427985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2428985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2429985db425SBarry Smith         for (j=0;j<ncols;j++) {
2430985db425SBarry Smith           if (aj[j] > j) {
2431985db425SBarry Smith             idx[i] = j;
2432985db425SBarry Smith             break;
2433985db425SBarry Smith           }
2434985db425SBarry Smith         }
2435985db425SBarry Smith       }
2436985db425SBarry Smith     }
2437985db425SBarry Smith     for (j=0; j<ncols; j++){
2438985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2439985db425SBarry Smith       aa++; aj++;
2440e34fafa9SBarry Smith     }
2441e34fafa9SBarry Smith   }
2442e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2443e34fafa9SBarry Smith   PetscFunctionReturn(0);
2444e34fafa9SBarry Smith }
2445e34fafa9SBarry Smith 
2446682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24470a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2448cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2449cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2450cb5b572fSBarry Smith        MatMult_SeqAIJ,
245197304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
24527c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
24537c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2454db4efbfdSBarry Smith        0,
2455db4efbfdSBarry Smith        0,
2456db4efbfdSBarry Smith        0,
2457db4efbfdSBarry Smith /*10*/ 0,
2458cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2459cb5b572fSBarry Smith        0,
246017ab2063SBarry Smith        MatRelax_SeqAIJ,
246117ab2063SBarry Smith        MatTranspose_SeqAIJ,
246297304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2463cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2464cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2465cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2466cb5b572fSBarry Smith        MatNorm_SeqAIJ,
246797304618SKris Buschelman /*20*/ 0,
2468cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2469cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2470cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2471d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2472db4efbfdSBarry Smith        0,
2473db4efbfdSBarry Smith        0,
2474db4efbfdSBarry Smith        0,
2475db4efbfdSBarry Smith        0,
2476d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2477db4efbfdSBarry Smith        0,
2478db4efbfdSBarry Smith        0,
24796c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24806c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2481d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2482cb5b572fSBarry Smith        0,
2483cb5b572fSBarry Smith        0,
2484cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2485cb5b572fSBarry Smith        0,
2486d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2487cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2488cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2489cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2490cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2491d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2492cb5b572fSBarry Smith        MatScale_SeqAIJ,
2493cb5b572fSBarry Smith        0,
249479299369SBarry Smith        MatDiagonalSet_SeqAIJ,
24956945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2496d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
24973b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
24983b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
24993b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2500a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2501d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2502b9617806SBarry Smith        0,
25030513a670SBarry Smith        0,
2504cda55fadSBarry Smith        MatPermute_SeqAIJ,
2505cda55fadSBarry Smith        0,
2506d519adbfSMatthew Knepley /*59*/ 0,
2507b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2508b9b97703SBarry Smith        MatView_SeqAIJ,
2509357abbc8SBarry Smith        0,
2510ee4f033dSBarry Smith        0,
2511d519adbfSMatthew Knepley /*64*/ 0,
2512ee4f033dSBarry Smith        0,
2513ee4f033dSBarry Smith        0,
2514ee4f033dSBarry Smith        0,
2515ee4f033dSBarry Smith        0,
2516d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2517c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2518ee4f033dSBarry Smith        0,
2519ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2520dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2521ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2522dcf5cc72SBarry Smith #else
2523dcf5cc72SBarry Smith        0,
2524dcf5cc72SBarry Smith #endif
2525d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
252697304618SKris Buschelman        0,
252797304618SKris Buschelman        0,
252897304618SKris Buschelman        0,
252997304618SKris Buschelman        0,
2530d519adbfSMatthew Knepley /*79*/ 0,
253197304618SKris Buschelman        0,
253297304618SKris Buschelman        0,
253397304618SKris Buschelman        0,
2534bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2535d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
25361cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25376284ec50SHong Zhang        0,
25386284ec50SHong Zhang        0,
2539bc011b1eSHong Zhang        0,
2540d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
254126be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
254226be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2543d439da42SKris Buschelman        MatPtAP_Basic,
25447ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2545d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2546bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2547bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2548bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25497ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2550d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2551609c6c4dSKris Buschelman        0,
2552609c6c4dSKris Buschelman        0,
255387d4246cSBarry Smith        MatConjugate_SeqAIJ,
255487d4246cSBarry Smith        0,
2555d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
255699cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2557f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2558f5edf698SHong Zhang        0,
25592bebee5dSHong Zhang        0,
2560d519adbfSMatthew Knepley /*109*/0,
2561985db425SBarry Smith        0,
25622af78befSBarry Smith        MatGetRowMin_SeqAIJ,
25632af78befSBarry Smith        0,
2564599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2565d519adbfSMatthew Knepley /*114*/0,
2566599ef60dSHong Zhang        0,
25673c2a7987SHong Zhang        0,
25683c2a7987SHong Zhang        MatILUDTFactorSymbolic_SeqAIJ,
25693c2a7987SHong Zhang        MatILUDTFactorNumeric_SeqAIJ
25709e29f15eSvictorle };
257117ab2063SBarry Smith 
2572fb2e594dSBarry Smith EXTERN_C_BEGIN
25734a2ae208SSatish Balay #undef __FUNCT__
25744a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2575be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2576bef8e0ddSBarry Smith {
2577bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
257897f1f81fSBarry Smith   PetscInt   i,nz,n;
2579bef8e0ddSBarry Smith 
2580bef8e0ddSBarry Smith   PetscFunctionBegin;
2581bef8e0ddSBarry Smith 
2582bef8e0ddSBarry Smith   nz = aij->maxnz;
2583d0f46423SBarry Smith   n  = mat->rmap->n;
2584bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2585bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2586bef8e0ddSBarry Smith   }
2587bef8e0ddSBarry Smith   aij->nz = nz;
2588bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2589bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2590bef8e0ddSBarry Smith   }
2591bef8e0ddSBarry Smith 
2592bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2593bef8e0ddSBarry Smith }
2594fb2e594dSBarry Smith EXTERN_C_END
2595bef8e0ddSBarry Smith 
25964a2ae208SSatish Balay #undef __FUNCT__
25974a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2598bef8e0ddSBarry Smith /*@
2599bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2600bef8e0ddSBarry Smith        in the matrix.
2601bef8e0ddSBarry Smith 
2602bef8e0ddSBarry Smith   Input Parameters:
2603bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2604bef8e0ddSBarry Smith -  indices - the column indices
2605bef8e0ddSBarry Smith 
260615091d37SBarry Smith   Level: advanced
260715091d37SBarry Smith 
2608bef8e0ddSBarry Smith   Notes:
2609bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2610bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2611bef8e0ddSBarry Smith   of the MatSetValues() operation.
2612bef8e0ddSBarry Smith 
2613bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2614d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2615bef8e0ddSBarry Smith 
2616bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2617bef8e0ddSBarry Smith 
2618b9617806SBarry Smith     The indices should start with zero, not one.
2619b9617806SBarry Smith 
2620bef8e0ddSBarry Smith @*/
2621be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2622bef8e0ddSBarry Smith {
262397f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2624bef8e0ddSBarry Smith 
2625bef8e0ddSBarry Smith   PetscFunctionBegin;
26264482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
26274482741eSBarry Smith   PetscValidPointer(indices,2);
2628c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2629bef8e0ddSBarry Smith   if (f) {
2630bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2631bef8e0ddSBarry Smith   } else {
2632634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2633bef8e0ddSBarry Smith   }
2634bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2635bef8e0ddSBarry Smith }
2636bef8e0ddSBarry Smith 
2637be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2638be6bf707SBarry Smith 
2639fb2e594dSBarry Smith EXTERN_C_BEGIN
26404a2ae208SSatish Balay #undef __FUNCT__
26414a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2642be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2643be6bf707SBarry Smith {
2644be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26456849ba73SBarry Smith   PetscErrorCode ierr;
2646d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2647be6bf707SBarry Smith 
2648be6bf707SBarry Smith   PetscFunctionBegin;
2649be6bf707SBarry Smith   if (aij->nonew != 1) {
2650512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2651be6bf707SBarry Smith   }
2652be6bf707SBarry Smith 
2653be6bf707SBarry Smith   /* allocate space for values if not already there */
2654be6bf707SBarry Smith   if (!aij->saved_values) {
265587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
26569518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2657be6bf707SBarry Smith   }
2658be6bf707SBarry Smith 
2659be6bf707SBarry Smith   /* copy values over */
266087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2661be6bf707SBarry Smith   PetscFunctionReturn(0);
2662be6bf707SBarry Smith }
2663fb2e594dSBarry Smith EXTERN_C_END
2664be6bf707SBarry Smith 
26654a2ae208SSatish Balay #undef __FUNCT__
2666b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2667be6bf707SBarry Smith /*@
2668be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2669be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2670be6bf707SBarry Smith        nonlinear portion.
2671be6bf707SBarry Smith 
2672be6bf707SBarry Smith    Collect on Mat
2673be6bf707SBarry Smith 
2674be6bf707SBarry Smith   Input Parameters:
26750e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2676be6bf707SBarry Smith 
267715091d37SBarry Smith   Level: advanced
267815091d37SBarry Smith 
2679be6bf707SBarry Smith   Common Usage, with SNESSolve():
2680be6bf707SBarry Smith $    Create Jacobian matrix
2681be6bf707SBarry Smith $    Set linear terms into matrix
2682be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2683be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2684be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2685512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2686be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2687be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2688be6bf707SBarry Smith $    In your Jacobian routine
2689be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2690be6bf707SBarry Smith $      Set nonlinear terms in matrix
2691be6bf707SBarry Smith 
2692be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2693be6bf707SBarry Smith $    // build linear portion of Jacobian
2694512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2695be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2696be6bf707SBarry Smith $    loop over nonlinear iterations
2697be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2698be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2699be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2700be6bf707SBarry Smith $       Solve linear system with Jacobian
2701be6bf707SBarry Smith $    endloop
2702be6bf707SBarry Smith 
2703be6bf707SBarry Smith   Notes:
2704be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2705512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2706be6bf707SBarry Smith     calling this routine.
2707be6bf707SBarry Smith 
27080c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
27090c468ba9SBarry Smith     and does not allocated additional space.
27100c468ba9SBarry Smith 
2711be6bf707SBarry Smith .seealso: MatRetrieveValues()
2712be6bf707SBarry Smith 
2713be6bf707SBarry Smith @*/
2714be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2715be6bf707SBarry Smith {
2716dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2717be6bf707SBarry Smith 
2718be6bf707SBarry Smith   PetscFunctionBegin;
27194482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
272029bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
272129bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2722be6bf707SBarry Smith 
2723c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2724be6bf707SBarry Smith   if (f) {
2725be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2726be6bf707SBarry Smith   } else {
2727634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2728be6bf707SBarry Smith   }
2729be6bf707SBarry Smith   PetscFunctionReturn(0);
2730be6bf707SBarry Smith }
2731be6bf707SBarry Smith 
2732fb2e594dSBarry Smith EXTERN_C_BEGIN
27334a2ae208SSatish Balay #undef __FUNCT__
27344a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2735be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2736be6bf707SBarry Smith {
2737be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27386849ba73SBarry Smith   PetscErrorCode ierr;
2739d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2740be6bf707SBarry Smith 
2741be6bf707SBarry Smith   PetscFunctionBegin;
2742be6bf707SBarry Smith   if (aij->nonew != 1) {
2743512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2744be6bf707SBarry Smith   }
2745be6bf707SBarry Smith   if (!aij->saved_values) {
2746634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2747be6bf707SBarry Smith   }
2748be6bf707SBarry Smith   /* copy values over */
274987828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2750be6bf707SBarry Smith   PetscFunctionReturn(0);
2751be6bf707SBarry Smith }
2752fb2e594dSBarry Smith EXTERN_C_END
2753be6bf707SBarry Smith 
27544a2ae208SSatish Balay #undef __FUNCT__
27554a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2756be6bf707SBarry Smith /*@
2757be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2758be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2759be6bf707SBarry Smith        nonlinear portion.
2760be6bf707SBarry Smith 
2761be6bf707SBarry Smith    Collect on Mat
2762be6bf707SBarry Smith 
2763be6bf707SBarry Smith   Input Parameters:
2764be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2765be6bf707SBarry Smith 
276615091d37SBarry Smith   Level: advanced
276715091d37SBarry Smith 
2768be6bf707SBarry Smith .seealso: MatStoreValues()
2769be6bf707SBarry Smith 
2770be6bf707SBarry Smith @*/
2771be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2772be6bf707SBarry Smith {
2773dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2774be6bf707SBarry Smith 
2775be6bf707SBarry Smith   PetscFunctionBegin;
27764482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
277729bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
277829bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2779be6bf707SBarry Smith 
2780c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2781be6bf707SBarry Smith   if (f) {
2782be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2783be6bf707SBarry Smith   } else {
2784634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2785be6bf707SBarry Smith   }
2786be6bf707SBarry Smith   PetscFunctionReturn(0);
2787be6bf707SBarry Smith }
2788be6bf707SBarry Smith 
2789f83d6046SBarry Smith 
2790be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
27914a2ae208SSatish Balay #undef __FUNCT__
27924a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
279317ab2063SBarry Smith /*@C
2794682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
27950d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
27966e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
279751c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
27982bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
279917ab2063SBarry Smith 
2800db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2801db81eaa0SLois Curfman McInnes 
280217ab2063SBarry Smith    Input Parameters:
2803db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
280417ab2063SBarry Smith .  m - number of rows
280517ab2063SBarry Smith .  n - number of columns
280617ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
280751c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
28082bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
280917ab2063SBarry Smith 
281017ab2063SBarry Smith    Output Parameter:
2811416022c9SBarry Smith .  A - the matrix
281217ab2063SBarry Smith 
2813175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2814ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2815175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2816175b88e8SBarry Smith 
2817b259b22eSLois Curfman McInnes    Notes:
281849a6f317SBarry Smith    If nnz is given then nz is ignored
281949a6f317SBarry Smith 
282017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
282117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28220002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
282344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
282417ab2063SBarry Smith 
282517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2826a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28273d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28286da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
282917ab2063SBarry Smith 
2830682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
28314fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2832682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
28336c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
28346c7ebb05SLois Curfman McInnes 
28356c7ebb05SLois Curfman McInnes    Options Database Keys:
2836698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2837698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2838db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2839db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2840db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
284117ab2063SBarry Smith 
2842027ccd11SLois Curfman McInnes    Level: intermediate
2843027ccd11SLois Curfman McInnes 
284436db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
284536db0b34SBarry Smith 
284617ab2063SBarry Smith @*/
2847be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
284817ab2063SBarry Smith {
2849dfbe8321SBarry Smith   PetscErrorCode ierr;
28506945ee14SBarry Smith 
28513a40ed3dSBarry Smith   PetscFunctionBegin;
2852f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2853117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2854c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2855ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2856273d9f13SBarry Smith   PetscFunctionReturn(0);
2857273d9f13SBarry Smith }
2858273d9f13SBarry Smith 
28594a2ae208SSatish Balay #undef __FUNCT__
28604a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2861273d9f13SBarry Smith /*@C
2862273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2863273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2864273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2865273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2866273d9f13SBarry Smith 
2867273d9f13SBarry Smith    Collective on MPI_Comm
2868273d9f13SBarry Smith 
2869273d9f13SBarry Smith    Input Parameters:
2870117016b1SBarry Smith +  B - The matrix-free
2871273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2872273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2873273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2874273d9f13SBarry Smith 
2875273d9f13SBarry Smith    Notes:
287649a6f317SBarry Smith      If nnz is given then nz is ignored
287749a6f317SBarry Smith 
2878273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2879273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2880273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2881273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2882273d9f13SBarry Smith 
2883273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2884273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2885273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2886273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2887273d9f13SBarry Smith 
2888aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2889aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2890aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2891aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2892aa95bbe8SBarry Smith 
2893a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2894a96a251dSBarry Smith    entries or columns indices
2895a96a251dSBarry Smith 
2896273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2897273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2898273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2899273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2900273d9f13SBarry Smith 
2901273d9f13SBarry Smith    Options Database Keys:
2902698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2903698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2904273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2905273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2906273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2907273d9f13SBarry Smith 
2908273d9f13SBarry Smith    Level: intermediate
2909273d9f13SBarry Smith 
2910aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2911273d9f13SBarry Smith 
2912273d9f13SBarry Smith @*/
2913be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2914273d9f13SBarry Smith {
291597f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2916a23d5eceSKris Buschelman 
2917a23d5eceSKris Buschelman   PetscFunctionBegin;
2918a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2919a23d5eceSKris Buschelman   if (f) {
2920a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2921a23d5eceSKris Buschelman   }
2922a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2923a23d5eceSKris Buschelman }
2924a23d5eceSKris Buschelman 
2925a23d5eceSKris Buschelman EXTERN_C_BEGIN
2926a23d5eceSKris Buschelman #undef __FUNCT__
2927a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2928be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2929a23d5eceSKris Buschelman {
2930273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2931a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
29326849ba73SBarry Smith   PetscErrorCode ierr;
293397f1f81fSBarry Smith   PetscInt       i;
2934273d9f13SBarry Smith 
2935273d9f13SBarry Smith   PetscFunctionBegin;
2936d5d45c9bSBarry Smith 
2937a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2938c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2939c461c341SBarry Smith     nz             = 0;
2940c461c341SBarry Smith   }
2941c461c341SBarry Smith 
29427408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(B->rmap,1);CHKERRQ(ierr);
29437408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(B->cmap,1);CHKERRQ(ierr);
2944d0f46423SBarry Smith   ierr = PetscMapSetUp(B->rmap);CHKERRQ(ierr);
2945d0f46423SBarry Smith   ierr = PetscMapSetUp(B->cmap);CHKERRQ(ierr);
2946899cda47SBarry Smith 
2947435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2948435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2949b73539f3SBarry Smith   if (nnz) {
2950d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
295129bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2952d0f46423SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
2953b73539f3SBarry Smith     }
2954b73539f3SBarry Smith   }
2955b73539f3SBarry Smith 
2956273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2957273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2958273d9f13SBarry Smith 
2959ab93d7beSBarry Smith   if (!skipallocation) {
29602ee49352SLisandro Dalcin     if (!b->imax) {
2961d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
2962d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
29632ee49352SLisandro Dalcin     }
2964273d9f13SBarry Smith     if (!nnz) {
2965435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2966273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2967d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
2968d0f46423SBarry Smith       nz = nz*B->rmap->n;
2969273d9f13SBarry Smith     } else {
2970273d9f13SBarry Smith       nz = 0;
2971d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2972273d9f13SBarry Smith     }
2973ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2974d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
2975ab93d7beSBarry Smith 
2976273d9f13SBarry Smith     /* allocate the matrix space */
29772ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2978d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
2979d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2980bfeeae90SHong Zhang     b->i[0] = 0;
2981d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
29825da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29835da197adSKris Buschelman     }
2984273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2985e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2986e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2987c461c341SBarry Smith   } else {
2988e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2989e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2990c461c341SBarry Smith   }
2991273d9f13SBarry Smith 
2992273d9f13SBarry Smith   b->nz                = 0;
2993273d9f13SBarry Smith   b->maxnz             = nz;
2994273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2995273d9f13SBarry Smith   PetscFunctionReturn(0);
2996273d9f13SBarry Smith }
2997a23d5eceSKris Buschelman EXTERN_C_END
2998273d9f13SBarry Smith 
2999a1661176SMatthew Knepley #undef  __FUNCT__
3000a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
300158d36128SBarry Smith /*@
3002a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3003a1661176SMatthew Knepley 
3004a1661176SMatthew Knepley    Input Parameters:
3005a1661176SMatthew Knepley +  B - the matrix
3006a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3007a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3008a1661176SMatthew Knepley -  v - optional values in the matrix
3009a1661176SMatthew Knepley 
3010d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
3011d58b2322SMatthew Knepley 
3012a1661176SMatthew Knepley    Level: developer
3013a1661176SMatthew Knepley 
301458d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
301558d36128SBarry Smith 
3016a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3017a1661176SMatthew Knepley 
3018a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3019a1661176SMatthew Knepley @*/
3020a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3021a1661176SMatthew Knepley {
3022a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3023a1661176SMatthew Knepley   PetscErrorCode ierr;
3024a1661176SMatthew Knepley 
3025a1661176SMatthew Knepley   PetscFunctionBegin;
3026a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
3027a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3028a1661176SMatthew Knepley   if (f) {
3029a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3030a1661176SMatthew Knepley   }
3031a1661176SMatthew Knepley   PetscFunctionReturn(0);
3032a1661176SMatthew Knepley }
3033a1661176SMatthew Knepley 
3034a1661176SMatthew Knepley EXTERN_C_BEGIN
3035a1661176SMatthew Knepley #undef  __FUNCT__
3036a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3037b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3038a1661176SMatthew Knepley {
3039a1661176SMatthew Knepley   PetscInt       i;
3040a1661176SMatthew Knepley   PetscInt       m,n;
3041a1661176SMatthew Knepley   PetscInt       nz;
3042a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3043a1661176SMatthew Knepley   PetscScalar    *values;
3044a1661176SMatthew Knepley   PetscErrorCode ierr;
3045a1661176SMatthew Knepley 
3046a1661176SMatthew Knepley   PetscFunctionBegin;
3047a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3048a1661176SMatthew Knepley 
3049b7940d39SSatish Balay   if (Ii[0]) {
3050b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3051a1661176SMatthew Knepley   }
3052a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3053a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3054b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3055a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
3056a1661176SMatthew Knepley     if (nz < 0) {
3057a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3058a1661176SMatthew Knepley     }
3059a1661176SMatthew Knepley     nnz[i] = nz;
3060a1661176SMatthew Knepley   }
3061a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3062a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3063a1661176SMatthew Knepley 
3064a1661176SMatthew Knepley   if (v) {
3065a1661176SMatthew Knepley     values = (PetscScalar*) v;
3066a1661176SMatthew Knepley   } else {
3067a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3068a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3069a1661176SMatthew Knepley   }
3070a1661176SMatthew Knepley 
3071a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3072b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3073b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3074a1661176SMatthew Knepley   }
3075a1661176SMatthew Knepley 
3076a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3077a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3078a1661176SMatthew Knepley 
3079a1661176SMatthew Knepley   if (!v) {
3080a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3081a1661176SMatthew Knepley   }
3082a1661176SMatthew Knepley   PetscFunctionReturn(0);
3083a1661176SMatthew Knepley }
3084a1661176SMatthew Knepley EXTERN_C_END
3085a1661176SMatthew Knepley 
30867c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3087*be7314b0SBarry Smith #include "private/petscaxpy.h"
3088170fe5c8SBarry Smith 
3089170fe5c8SBarry Smith #undef __FUNCT__
3090170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3091170fe5c8SBarry Smith /*
3092170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3093170fe5c8SBarry Smith 
3094170fe5c8SBarry Smith                n                       p                          p
3095170fe5c8SBarry Smith         (              )       (              )         (                  )
3096170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3097170fe5c8SBarry Smith         (              )       (              )         (                  )
3098170fe5c8SBarry Smith 
3099170fe5c8SBarry Smith */
3100170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3101170fe5c8SBarry Smith {
3102170fe5c8SBarry Smith   PetscErrorCode     ierr;
3103170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3104170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3105170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
31061de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3107170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3108170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3109170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3110170fe5c8SBarry Smith 
3111170fe5c8SBarry Smith   PetscFunctionBegin;
3112d0f46423SBarry Smith   m = A->rmap->n;
3113d0f46423SBarry Smith   n = A->cmap->n;
3114d0f46423SBarry Smith   p = B->cmap->n;
3115170fe5c8SBarry Smith   a = sub_a->v;
3116170fe5c8SBarry Smith   b = sub_b->a;
3117170fe5c8SBarry Smith   c = sub_c->v;
3118170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3119170fe5c8SBarry Smith 
3120170fe5c8SBarry Smith   ii  = sub_b->i;
3121170fe5c8SBarry Smith   idx = sub_b->j;
3122170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3123170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3124170fe5c8SBarry Smith     while (q-->0) {
3125170fe5c8SBarry Smith       c_q = c + m*(*idx);
3126170fe5c8SBarry Smith       a_q = a + m*i;
3127*be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3128170fe5c8SBarry Smith       idx++;
3129170fe5c8SBarry Smith       b++;
3130170fe5c8SBarry Smith     }
3131170fe5c8SBarry Smith   }
3132170fe5c8SBarry Smith   PetscFunctionReturn(0);
3133170fe5c8SBarry Smith }
3134170fe5c8SBarry Smith 
3135170fe5c8SBarry Smith #undef __FUNCT__
3136170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3137170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3138170fe5c8SBarry Smith {
3139170fe5c8SBarry Smith   PetscErrorCode ierr;
3140d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3141170fe5c8SBarry Smith   Mat            Cmat;
3142170fe5c8SBarry Smith 
3143170fe5c8SBarry Smith   PetscFunctionBegin;
3144d0f46423SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
314539804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3146170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3147170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3148170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3149170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3150170fe5c8SBarry Smith   *C = Cmat;
3151170fe5c8SBarry Smith   PetscFunctionReturn(0);
3152170fe5c8SBarry Smith }
3153170fe5c8SBarry Smith 
3154170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3155170fe5c8SBarry Smith #undef __FUNCT__
3156170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3157170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3158170fe5c8SBarry Smith {
3159170fe5c8SBarry Smith   PetscErrorCode ierr;
3160170fe5c8SBarry Smith 
3161170fe5c8SBarry Smith   PetscFunctionBegin;
3162170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3163170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3164170fe5c8SBarry Smith   }
3165170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3166170fe5c8SBarry Smith   PetscFunctionReturn(0);
3167170fe5c8SBarry Smith }
3168170fe5c8SBarry Smith 
3169170fe5c8SBarry Smith 
31700bad9183SKris Buschelman /*MC
3171fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31720bad9183SKris Buschelman    based on compressed sparse row format.
31730bad9183SKris Buschelman 
31740bad9183SKris Buschelman    Options Database Keys:
31750bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31760bad9183SKris Buschelman 
31770bad9183SKris Buschelman   Level: beginner
31780bad9183SKris Buschelman 
3179f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31800bad9183SKris Buschelman M*/
31810bad9183SKris Buschelman 
3182a6175056SHong Zhang EXTERN_C_BEGIN
3183b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3184b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3185b5e56a35SBarry Smith #endif
3186611a798aSSatish Balay #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
3187af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3188af1023dbSSatish Balay #endif
318917667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3190b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3191db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3192611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
31935c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3194611f576cSBarry Smith #endif
3195611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
31965c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3197611f576cSBarry Smith #endif
3198f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3199f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3200f3c0ef26SHong Zhang #endif
3201611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32025c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3203611f576cSBarry Smith #endif
3204eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3205eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3206eb3b5408SSatish Balay #endif
3207719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3208719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3209719d5645SBarry Smith #endif
321017667f90SBarry Smith EXTERN_C_END
321117667f90SBarry Smith 
3212b24902e0SBarry Smith 
321317667f90SBarry Smith EXTERN_C_BEGIN
32144a2ae208SSatish Balay #undef __FUNCT__
32154a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3216be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3217273d9f13SBarry Smith {
3218273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3219dfbe8321SBarry Smith   PetscErrorCode ierr;
322038baddfdSBarry Smith   PetscMPIInt    size;
3221273d9f13SBarry Smith 
3222273d9f13SBarry Smith   PetscFunctionBegin;
32237adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3224273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3225273d9f13SBarry Smith 
322638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3227b0a32e0cSBarry Smith   B->data             = (void*)b;
3228549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
322990f02eecSBarry Smith   B->mapping          = 0;
3230416022c9SBarry Smith   b->row              = 0;
3231416022c9SBarry Smith   b->col              = 0;
323282bf6240SBarry Smith   b->icol             = 0;
3233b810aeb4SBarry Smith   b->reallocs         = 0;
323436db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3235f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3236416022c9SBarry Smith   b->nonew             = 0;
3237416022c9SBarry Smith   b->diag              = 0;
3238416022c9SBarry Smith   b->solve_work        = 0;
32392a1b7f2aSHong Zhang   B->spptr             = 0;
3240be6bf707SBarry Smith   b->saved_values      = 0;
3241d7f994e1SBarry Smith   b->idiag             = 0;
324271f1c65dSBarry Smith   b->mdiag             = 0;
324371f1c65dSBarry Smith   b->ssor_work         = 0;
324471f1c65dSBarry Smith   b->omega             = 1.0;
324571f1c65dSBarry Smith   b->fshift            = 0.0;
324671f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3247a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3248a30b2313SHong Zhang   b->xtoy              = 0;
3249a30b2313SHong Zhang   b->XtoY              = 0;
325073e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3251d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3252d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3253d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3254d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
325588e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
325617ab2063SBarry Smith 
325735d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3258b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3259b5e56a35SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_pastix_C",
3260b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3261b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3262b5e56a35SBarry Smith #endif
3263611a798aSSatish Balay #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
3264719d5645SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_essl_C",
3265719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3266719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3267719d5645SBarry Smith #endif
3268611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32695c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_C",
32705c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
32715c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3272611f576cSBarry Smith #endif
3273f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3274f3c0ef26SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_dist_C",
3275f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3276f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3277f3c0ef26SHong Zhang #endif
3278611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32795c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_spooles_C",
32805c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
32815c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3282611f576cSBarry Smith #endif
3283611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
32845c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_mumps_C",
32855c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
32865c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3287611f576cSBarry Smith #endif
3288eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3289eb3b5408SSatish Balay     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_umfpack_C",
3290eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3291eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3292eb3b5408SSatish Balay #endif
3293719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3294719d5645SBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_lusol_C",
3295719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3296719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3297719d5645SBarry Smith #endif
3298b24902e0SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_petsc_C",
3299b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3300b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3301db4efbfdSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_seqaij_petsc_C",
3302db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3303db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3304f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3305bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3306bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3307f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3308be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3309bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3310f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3311be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3312bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3313a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3314a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3315a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
331685fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
331785fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
331885fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3319ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3320ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3321ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
332217667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
332317667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
332417667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33255fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33265fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33275fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33281cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33291cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33301cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3331a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3332a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3333a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3334a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3335a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3336a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
333705b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
333805b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
333905b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3340170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3341170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3342170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3343170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3344170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3345170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3346170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3347170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3348170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
33494846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
335017667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
33513a40ed3dSBarry Smith   PetscFunctionReturn(0);
335217ab2063SBarry Smith }
3353273d9f13SBarry Smith EXTERN_C_END
335417ab2063SBarry Smith 
33554a2ae208SSatish Balay #undef __FUNCT__
3356b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3357b24902e0SBarry Smith /*
3358b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3359b24902e0SBarry Smith */
3360719d5645SBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues)
336117ab2063SBarry Smith {
3362416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
33636849ba73SBarry Smith   PetscErrorCode ierr;
3364d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
336517ab2063SBarry Smith 
33663a40ed3dSBarry Smith   PetscFunctionBegin;
33671d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
33681d5dac46SHong Zhang 
3369273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3370273d9f13SBarry Smith 
3371416022c9SBarry Smith   C->factor           = A->factor;
33726ad4291fSHong Zhang 
3373416022c9SBarry Smith   c->row            = 0;
3374416022c9SBarry Smith   c->col            = 0;
337582bf6240SBarry Smith   c->icol           = 0;
33766ad4291fSHong Zhang   c->reallocs       = 0;
337717ab2063SBarry Smith 
33786ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
337917ab2063SBarry Smith 
33807408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(C->rmap,1);CHKERRQ(ierr);
33817408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(C->cmap,1);CHKERRQ(ierr);
3382d0f46423SBarry Smith   ierr = PetscMapSetUp(C->rmap);CHKERRQ(ierr);
3383d0f46423SBarry Smith   ierr = PetscMapSetUp(C->cmap);CHKERRQ(ierr);
3384eec197d1SBarry Smith 
338533b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
33869518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
338717ab2063SBarry Smith   for (i=0; i<m; i++) {
3388416022c9SBarry Smith     c->imax[i] = a->imax[i];
3389416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
339017ab2063SBarry Smith   }
339117ab2063SBarry Smith 
339217ab2063SBarry Smith   /* allocate the matrix space */
3393a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
33949518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3395f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
339697f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
339717ab2063SBarry Smith   if (m > 0) {
339897f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3399be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
3400bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3401be6bf707SBarry Smith     } else {
3402bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
340317ab2063SBarry Smith     }
340408480c60SBarry Smith   }
340517ab2063SBarry Smith 
34066ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3407416022c9SBarry Smith   c->roworiented       = a->roworiented;
3408416022c9SBarry Smith   c->nonew             = a->nonew;
3409416022c9SBarry Smith   if (a->diag) {
341097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
341152e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
341217ab2063SBarry Smith     for (i=0; i<m; i++) {
3413416022c9SBarry Smith       c->diag[i] = a->diag[i];
341417ab2063SBarry Smith     }
34153a40ed3dSBarry Smith   } else c->diag           = 0;
34166ad4291fSHong Zhang   c->solve_work            = 0;
34176ad4291fSHong Zhang   c->saved_values          = 0;
34186ad4291fSHong Zhang   c->idiag                 = 0;
341971f1c65dSBarry Smith   c->ssor_work             = 0;
3420a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3421e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3422e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34236ad4291fSHong Zhang   c->xtoy                  = 0;
34246ad4291fSHong Zhang   c->XtoY                  = 0;
34256ad4291fSHong Zhang 
3426416022c9SBarry Smith   c->nz                 = a->nz;
3427416022c9SBarry Smith   c->maxnz              = a->maxnz;
3428273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3429754ec7b1SSatish Balay 
34306ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34316ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
34326ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
34336ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
34346ad4291fSHong Zhang     i = a->compressedrow.nrows;
34356ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
34366ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
34376ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
34386ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
343927ea64f8SHong Zhang   } else {
344027ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
344127ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
344227ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
34436ad4291fSHong Zhang   }
344488e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
34454846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
34464846f1f5SKris Buschelman 
34477adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
34483a40ed3dSBarry Smith   PetscFunctionReturn(0);
344917ab2063SBarry Smith }
345017ab2063SBarry Smith 
34514a2ae208SSatish Balay #undef __FUNCT__
3452b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3453b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3454b24902e0SBarry Smith {
3455b24902e0SBarry Smith   PetscErrorCode ierr;
3456d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
3457b24902e0SBarry Smith 
3458b24902e0SBarry Smith   PetscFunctionBegin;
3459b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3460b24902e0SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
3461b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3462719d5645SBarry Smith   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues);CHKERRQ(ierr);
3463b24902e0SBarry Smith   PetscFunctionReturn(0);
3464b24902e0SBarry Smith }
3465b24902e0SBarry Smith 
3466b24902e0SBarry Smith #undef __FUNCT__
34674a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3468a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
346917ab2063SBarry Smith {
3470416022c9SBarry Smith   Mat_SeqAIJ     *a;
3471416022c9SBarry Smith   Mat            B;
34726849ba73SBarry Smith   PetscErrorCode ierr;
34733c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
347438baddfdSBarry Smith   int            fd;
347538baddfdSBarry Smith   PetscMPIInt    size;
3476bcd2baecSBarry Smith   MPI_Comm       comm;
347717ab2063SBarry Smith 
34783a40ed3dSBarry Smith   PetscFunctionBegin;
3479e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3480d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
348129bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3482b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
34830752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3484552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
348517ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
348617ab2063SBarry Smith 
3487d64ed03dSBarry Smith   if (nz < 0) {
348829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3489d64ed03dSBarry Smith   }
3490d64ed03dSBarry Smith 
349117ab2063SBarry Smith   /* read in row lengths */
349297f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
34930752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
349417ab2063SBarry Smith 
34953c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
34963c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
34973c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
34983c601197SSatish Balay 
349917ab2063SBarry Smith   /* create our matrix */
3500f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3501f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3502b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3503ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3504416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
350517ab2063SBarry Smith 
35060752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
350717ab2063SBarry Smith 
350817ab2063SBarry Smith   /* read in nonzero values */
35090752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
351017ab2063SBarry Smith 
351117ab2063SBarry Smith   /* set matrix "i" values */
3512efb16452SHong Zhang   a->i[0] = 0;
351317ab2063SBarry Smith   for (i=1; i<= M; i++) {
3514416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3515416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
351617ab2063SBarry Smith   }
3517606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
351817ab2063SBarry Smith 
35196d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35206d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3521b3a1e11cSKris Buschelman   *A = B;
35223a40ed3dSBarry Smith   PetscFunctionReturn(0);
352317ab2063SBarry Smith }
352417ab2063SBarry Smith 
35254a2ae208SSatish Balay #undef __FUNCT__
3526b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3527dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35287264ac53SSatish Balay {
35297264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3530dfbe8321SBarry Smith   PetscErrorCode ierr;
35317264ac53SSatish Balay 
35323a40ed3dSBarry Smith   PetscFunctionBegin;
3533bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3534d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3535ca44d042SBarry Smith     *flg = PETSC_FALSE;
3536ca44d042SBarry Smith     PetscFunctionReturn(0);
3537bcd2baecSBarry Smith   }
35387264ac53SSatish Balay 
35397264ac53SSatish Balay   /* if the a->i are the same */
3540d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3541abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
35427264ac53SSatish Balay 
35437264ac53SSatish Balay   /* if a->j are the same */
354497f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3545abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3546bcd2baecSBarry Smith 
3547bcd2baecSBarry Smith   /* if a->a are the same */
354887828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
35490f5bd95cSBarry Smith 
35503a40ed3dSBarry Smith   PetscFunctionReturn(0);
35517264ac53SSatish Balay 
35527264ac53SSatish Balay }
355336db0b34SBarry Smith 
35544a2ae208SSatish Balay #undef __FUNCT__
35554a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
355605869f15SSatish Balay /*@
355736db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
355836db0b34SBarry Smith               provided by the user.
355936db0b34SBarry Smith 
3560c75a6043SHong Zhang       Collective on MPI_Comm
356136db0b34SBarry Smith 
356236db0b34SBarry Smith    Input Parameters:
356336db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
356436db0b34SBarry Smith .   m - number of rows
356536db0b34SBarry Smith .   n - number of columns
356636db0b34SBarry Smith .   i - row indices
356736db0b34SBarry Smith .   j - column indices
356836db0b34SBarry Smith -   a - matrix values
356936db0b34SBarry Smith 
357036db0b34SBarry Smith    Output Parameter:
357136db0b34SBarry Smith .   mat - the matrix
357236db0b34SBarry Smith 
357336db0b34SBarry Smith    Level: intermediate
357436db0b34SBarry Smith 
357536db0b34SBarry Smith    Notes:
35760551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
357736db0b34SBarry Smith     once the matrix is destroyed
357836db0b34SBarry Smith 
357936db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
358036db0b34SBarry Smith 
3581bfeeae90SHong Zhang        The i and j indices are 0 based
358236db0b34SBarry Smith 
3583a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3584a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3585a4552177SSatish Balay     as shown:
3586a4552177SSatish Balay 
3587a4552177SSatish Balay         1 0 0
3588a4552177SSatish Balay         2 0 3
3589a4552177SSatish Balay         4 5 6
3590a4552177SSatish Balay 
3591a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
35929985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3593a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3594a4552177SSatish Balay 
35959985e31cSBarry Smith 
35962fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
359736db0b34SBarry Smith 
359836db0b34SBarry Smith @*/
3599a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
360036db0b34SBarry Smith {
3601dfbe8321SBarry Smith   PetscErrorCode ierr;
3602cbcfb4deSHong Zhang   PetscInt       ii;
360336db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3604cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3605cbcfb4deSHong Zhang   PetscInt       jj;
3606cbcfb4deSHong Zhang #endif
360736db0b34SBarry Smith 
360836db0b34SBarry Smith   PetscFunctionBegin;
3609a96a251dSBarry Smith   if (i[0]) {
3610634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
361136db0b34SBarry Smith   }
3612f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3613f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3614ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3615ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3616ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3617ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3618ab93d7beSBarry Smith 
361936db0b34SBarry Smith   aij->i = i;
362036db0b34SBarry Smith   aij->j = j;
362136db0b34SBarry Smith   aij->a = a;
362236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
362336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3624e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3625e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
362636db0b34SBarry Smith 
362736db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
362836db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
36292515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
363079a5c55eSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
36319985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
36329985e31cSBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
36339985e31cSBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
36349985e31cSBarry Smith     }
363536db0b34SBarry Smith #endif
363636db0b34SBarry Smith   }
36372515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
363836db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
363979a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
364079a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
364136db0b34SBarry Smith   }
364236db0b34SBarry Smith #endif
364336db0b34SBarry Smith 
3644b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3645b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
364636db0b34SBarry Smith   PetscFunctionReturn(0);
364736db0b34SBarry Smith }
364836db0b34SBarry Smith 
3649cc8ba8e1SBarry Smith #undef __FUNCT__
3650ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3651dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3652cc8ba8e1SBarry Smith {
3653dfbe8321SBarry Smith   PetscErrorCode ierr;
3654cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
365536db0b34SBarry Smith 
3656cc8ba8e1SBarry Smith   PetscFunctionBegin;
36578ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3658cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3659cc8ba8e1SBarry Smith     a->coloring = coloring;
366012c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
366197f1f81fSBarry Smith     PetscInt             i,*larray;
366212c595b3SBarry Smith     ISColoring      ocoloring;
366308b6dcc0SBarry Smith     ISColoringValue *colors;
366412c595b3SBarry Smith 
366512c595b3SBarry Smith     /* set coloring for diagonal portion */
3666d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3667d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
366812c595b3SBarry Smith       larray[i] = i;
366912c595b3SBarry Smith     }
3670d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3671d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3672d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
367312c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
367412c595b3SBarry Smith     }
367512c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3676d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
367712c595b3SBarry Smith     a->coloring = ocoloring;
367812c595b3SBarry Smith   }
3679cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3680cc8ba8e1SBarry Smith }
3681cc8ba8e1SBarry Smith 
3682dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3683ee4f033dSBarry Smith EXTERN_C_BEGIN
368429c1e371SBarry Smith #include "adic/ad_utils.h"
3685ee4f033dSBarry Smith EXTERN_C_END
3686cc8ba8e1SBarry Smith 
3687cc8ba8e1SBarry Smith #undef __FUNCT__
3688ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3689dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3690cc8ba8e1SBarry Smith {
3691cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3692d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
36934440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
369408b6dcc0SBarry Smith   ISColoringValue *color;
3695cc8ba8e1SBarry Smith 
3696cc8ba8e1SBarry Smith   PetscFunctionBegin;
3697e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
36984440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3699cc8ba8e1SBarry Smith   color = a->coloring->colors;
3700cc8ba8e1SBarry Smith   /* loop over rows */
3701cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3702cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3703cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3704cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3705cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3706cc8ba8e1SBarry Smith     }
37074440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3708ee4f033dSBarry Smith   }
3709ee4f033dSBarry Smith   PetscFunctionReturn(0);
3710ee4f033dSBarry Smith }
3711ee4f033dSBarry Smith #endif
3712ee4f033dSBarry Smith 
3713ee4f033dSBarry Smith #undef __FUNCT__
3714ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
371597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3716ee4f033dSBarry Smith {
3717ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3718d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
371954f21887SBarry Smith   MatScalar       *v = a->a;
372054f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
372108b6dcc0SBarry Smith   ISColoringValue *color;
3722ee4f033dSBarry Smith 
3723ee4f033dSBarry Smith   PetscFunctionBegin;
3724e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3725ee4f033dSBarry Smith   color = a->coloring->colors;
3726ee4f033dSBarry Smith   /* loop over rows */
3727ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3728ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3729ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3730ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3731ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3732ee4f033dSBarry Smith     }
3733ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3734cc8ba8e1SBarry Smith   }
3735cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3736cc8ba8e1SBarry Smith }
373736db0b34SBarry Smith 
373881824310SBarry Smith /*
373981824310SBarry Smith     Special version for direct calls from Fortran
374081824310SBarry Smith */
374181824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
374281824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
374381824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
374481824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
374581824310SBarry Smith #endif
374681824310SBarry Smith 
374781824310SBarry Smith /* Change these macros so can be used in void function */
374881824310SBarry Smith #undef CHKERRQ
37497adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
375081824310SBarry Smith #undef SETERRQ2
37517adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
375281824310SBarry Smith 
375381824310SBarry Smith EXTERN_C_BEGIN
375481824310SBarry Smith #undef __FUNCT__
375581824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
37561f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
375781824310SBarry Smith {
375881824310SBarry Smith   Mat            A = *AA;
375981824310SBarry Smith   PetscInt       m = *mm, n = *nn;
376081824310SBarry Smith   InsertMode     is = *isis;
376181824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
376281824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
376381824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
376481824310SBarry Smith   PetscErrorCode ierr;
376581824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
376654f21887SBarry Smith   MatScalar      *ap,value,*aa;
3767edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
376881824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
376981824310SBarry Smith 
377081824310SBarry Smith   PetscFunctionBegin;
3771d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
377281824310SBarry Smith   imax = a->imax;
377381824310SBarry Smith   ai = a->i;
377481824310SBarry Smith   ailen = a->ilen;
377581824310SBarry Smith   aj = a->j;
377681824310SBarry Smith   aa = a->a;
377781824310SBarry Smith 
377881824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
377981824310SBarry Smith     row  = im[k];
378081824310SBarry Smith     if (row < 0) continue;
378181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3782d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
378381824310SBarry Smith #endif
378481824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
378581824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
378681824310SBarry Smith     low  = 0;
378781824310SBarry Smith     high = nrow;
378881824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
378981824310SBarry Smith       if (in[l] < 0) continue;
379081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3791d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
379281824310SBarry Smith #endif
379381824310SBarry Smith       col = in[l];
379481824310SBarry Smith       if (roworiented) {
379581824310SBarry Smith         value = v[l + k*n];
379681824310SBarry Smith       } else {
379781824310SBarry Smith         value = v[k + l*m];
379881824310SBarry Smith       }
379981824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
380081824310SBarry Smith 
380181824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
380281824310SBarry Smith       lastcol = col;
380381824310SBarry Smith       while (high-low > 5) {
380481824310SBarry Smith         t = (low+high)/2;
380581824310SBarry Smith         if (rp[t] > col) high = t;
380681824310SBarry Smith         else             low  = t;
380781824310SBarry Smith       }
380881824310SBarry Smith       for (i=low; i<high; i++) {
380981824310SBarry Smith         if (rp[i] > col) break;
381081824310SBarry Smith         if (rp[i] == col) {
381181824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
381281824310SBarry Smith           else                  ap[i] = value;
381381824310SBarry Smith           goto noinsert;
381481824310SBarry Smith         }
381581824310SBarry Smith       }
381681824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
381781824310SBarry Smith       if (nonew == 1) goto noinsert;
38187adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3819d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
382081824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
382181824310SBarry Smith       /* shift up all the later entries in this row */
382281824310SBarry Smith       for (ii=N; ii>=i; ii--) {
382381824310SBarry Smith         rp[ii+1] = rp[ii];
382481824310SBarry Smith         ap[ii+1] = ap[ii];
382581824310SBarry Smith       }
382681824310SBarry Smith       rp[i] = col;
382781824310SBarry Smith       ap[i] = value;
382881824310SBarry Smith       noinsert:;
382981824310SBarry Smith       low = i + 1;
383081824310SBarry Smith     }
383181824310SBarry Smith     ailen[row] = nrow;
383281824310SBarry Smith   }
383381824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
383481824310SBarry Smith   PetscFunctionReturnVoid();
383581824310SBarry Smith }
383681824310SBarry Smith EXTERN_C_END
3837