xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 719d5645761d844e4357b7ee00a3296dffe0b787)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
89e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
9f5eb4b81SSatish Balay #include "src/inline/spops.h"
108d195f9aSBarry Smith #include "src/inline/dot.h"
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
134a2ae208SSatish Balay #undef __FUNCT__
1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1579299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1679299369SBarry Smith {
1779299369SBarry Smith   PetscErrorCode ierr;
1879299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
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];
66917ab2063SBarry Smith 
67009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
671d0f46423SBarry 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);
672ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
673ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
674a0e1a203SBarry Smith 
675dd5f02e7SSatish Balay   a->reallocs          = 0;
6764e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
67736db0b34SBarry Smith   a->rmax              = rmax;
6784e220ebcSLois Curfman McInnes 
679cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
680317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
68188e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
68271c2f376SKris Buschelman 
6834846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
68471f1c65dSBarry Smith 
68571f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6863a40ed3dSBarry Smith   PetscFunctionReturn(0);
68717ab2063SBarry Smith }
68817ab2063SBarry Smith 
6894a2ae208SSatish Balay #undef __FUNCT__
69099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
69199cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
69299cafbc1SBarry Smith {
69399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
69499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
69554f21887SBarry Smith   MatScalar      *aa = a->a;
69699cafbc1SBarry Smith 
69799cafbc1SBarry Smith   PetscFunctionBegin;
69899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
69999cafbc1SBarry Smith   PetscFunctionReturn(0);
70099cafbc1SBarry Smith }
70199cafbc1SBarry Smith 
70299cafbc1SBarry Smith #undef __FUNCT__
70399cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
70499cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
70599cafbc1SBarry Smith {
70699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
70799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
70854f21887SBarry Smith   MatScalar      *aa = a->a;
70999cafbc1SBarry Smith 
71099cafbc1SBarry Smith   PetscFunctionBegin;
71199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
71299cafbc1SBarry Smith   PetscFunctionReturn(0);
71399cafbc1SBarry Smith }
71499cafbc1SBarry Smith 
71599cafbc1SBarry Smith #undef __FUNCT__
7164a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
717dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
71817ab2063SBarry Smith {
719416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
720dfbe8321SBarry Smith   PetscErrorCode ierr;
7213a40ed3dSBarry Smith 
7223a40ed3dSBarry Smith   PetscFunctionBegin;
723d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7243a40ed3dSBarry Smith   PetscFunctionReturn(0);
72517ab2063SBarry Smith }
726416022c9SBarry Smith 
7274a2ae208SSatish Balay #undef __FUNCT__
7284a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
729dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
73017ab2063SBarry Smith {
731416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
732dfbe8321SBarry Smith   PetscErrorCode ierr;
733d5d45c9bSBarry Smith 
7343a40ed3dSBarry Smith   PetscFunctionBegin;
735aa482453SBarry Smith #if defined(PETSC_USE_LOG)
736d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
73717ab2063SBarry Smith #endif
738e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
739c38d4ed2SBarry Smith   if (a->row) {
740c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
741c38d4ed2SBarry Smith   }
742c38d4ed2SBarry Smith   if (a->col) {
743c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
744c38d4ed2SBarry Smith   }
74505b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
74605b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
74771f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
74805b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
74982bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
75005b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
751cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
75205b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
753407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
754d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
755a30b2313SHong Zhang 
7564846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7574846f1f5SKris Buschelman 
758606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
759901853e0SKris Buschelman 
760dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
761901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
762901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
763901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
764901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
765901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
766ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
767901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
768901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
769a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
770901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7713a40ed3dSBarry Smith   PetscFunctionReturn(0);
77217ab2063SBarry Smith }
77317ab2063SBarry Smith 
7744a2ae208SSatish Balay #undef __FUNCT__
7754a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
776dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
77717ab2063SBarry Smith {
7783a40ed3dSBarry Smith   PetscFunctionBegin;
7793a40ed3dSBarry Smith   PetscFunctionReturn(0);
78017ab2063SBarry Smith }
78117ab2063SBarry Smith 
7824a2ae208SSatish Balay #undef __FUNCT__
7834a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7844e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
78517ab2063SBarry Smith {
786416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7874846f1f5SKris Buschelman   PetscErrorCode ierr;
7883a40ed3dSBarry Smith 
7893a40ed3dSBarry Smith   PetscFunctionBegin;
790a65d3064SKris Buschelman   switch (op) {
791a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7924e0d8c25SBarry Smith       a->roworiented       = flg;
793a65d3064SKris Buschelman       break;
794a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
7954e0d8c25SBarry Smith       a->keepzeroedrows    = flg;
796a65d3064SKris Buschelman       break;
797512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
798512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
799a65d3064SKris Buschelman       break;
800a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
8014e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
802a65d3064SKris Buschelman       break;
803a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8044e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
805a65d3064SKris Buschelman       break;
806a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8074e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8080df259c2SBarry Smith       break;
809d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8104e0d8c25SBarry Smith       a->compressedrow.use = flg;
811d487561eSHong Zhang       break;
8124e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
813a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
814a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
815290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
816a65d3064SKris Buschelman       break;
817a65d3064SKris Buschelman     default:
81871c2f376SKris Buschelman       break;
819a65d3064SKris Buschelman   }
8204e0d8c25SBarry Smith   ierr = MatSetOption_Inode(A,op,flg);CHKERRQ(ierr);
8213a40ed3dSBarry Smith   PetscFunctionReturn(0);
82217ab2063SBarry Smith }
82317ab2063SBarry Smith 
8244a2ae208SSatish Balay #undef __FUNCT__
8254a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
826dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
82717ab2063SBarry Smith {
828416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8296849ba73SBarry Smith   PetscErrorCode ierr;
83097f1f81fSBarry Smith   PetscInt       i,j,n;
83187828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
83217ab2063SBarry Smith 
8333a40ed3dSBarry Smith   PetscFunctionBegin;
8342dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8351ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
83636db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
837d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
838d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
839bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
840bfeeae90SHong Zhang       if (a->j[j] == i) {
841416022c9SBarry Smith         x[i] = a->a[j];
84217ab2063SBarry Smith         break;
84317ab2063SBarry Smith       }
84417ab2063SBarry Smith     }
84517ab2063SBarry Smith   }
8461ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8473a40ed3dSBarry Smith   PetscFunctionReturn(0);
84817ab2063SBarry Smith }
84917ab2063SBarry Smith 
8504a2ae208SSatish Balay #undef __FUNCT__
8514a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
852dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
85317ab2063SBarry Smith {
854416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8555c897100SBarry Smith   PetscScalar       *x,*y;
856dfbe8321SBarry Smith   PetscErrorCode    ierr;
857d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
8585c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
859a77337e4SBarry Smith   MatScalar         *v;
860a77337e4SBarry Smith   PetscScalar       alpha;
8617b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8623447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8634eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8645c897100SBarry Smith #endif
86517ab2063SBarry Smith 
8663a40ed3dSBarry Smith   PetscFunctionBegin;
8672e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8681ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8691ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8705c897100SBarry Smith 
8715c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
872bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8735c897100SBarry Smith #else
8743447b6efSHong Zhang   if (usecprow){
8753447b6efSHong Zhang     m    = cprow.nrows;
8763447b6efSHong Zhang     ii   = cprow.i;
8777b2bb3b9SHong Zhang     ridx = cprow.rindex;
8783447b6efSHong Zhang   } else {
8793447b6efSHong Zhang     ii = a->i;
8803447b6efSHong Zhang   }
88117ab2063SBarry Smith   for (i=0; i<m; i++) {
8823447b6efSHong Zhang     idx   = a->j + ii[i] ;
8833447b6efSHong Zhang     v     = a->a + ii[i] ;
8843447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8853447b6efSHong Zhang     if (usecprow){
8867b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8873447b6efSHong Zhang     } else {
88817ab2063SBarry Smith       alpha = x[i];
8893447b6efSHong Zhang     }
89017ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
89117ab2063SBarry Smith   }
8925c897100SBarry Smith #endif
893efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
8941ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8951ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8963a40ed3dSBarry Smith   PetscFunctionReturn(0);
89717ab2063SBarry Smith }
89817ab2063SBarry Smith 
8994a2ae208SSatish Balay #undef __FUNCT__
9005c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
901dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9025c897100SBarry Smith {
903dfbe8321SBarry Smith   PetscErrorCode ierr;
9045c897100SBarry Smith 
9055c897100SBarry Smith   PetscFunctionBegin;
906170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9075c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9085c897100SBarry Smith   PetscFunctionReturn(0);
9095c897100SBarry Smith }
9105c897100SBarry Smith 
9115c897100SBarry Smith 
9125c897100SBarry Smith #undef __FUNCT__
9134a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
914dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
91517ab2063SBarry Smith {
916416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
917d9fead3dSBarry Smith   PetscScalar       *y;
91854f21887SBarry Smith   const PetscScalar *x;
91954f21887SBarry Smith   const MatScalar   *aa;
920dfbe8321SBarry Smith   PetscErrorCode    ierr;
921d0f46423SBarry Smith   PetscInt          m=A->rmap->n,*aj,*ii;
922a46b3154SVictor Eijkhout   PetscInt          n,i,j,nonzerorow=0,*ridx=PETSC_NULL;
923362ced78SSatish Balay   PetscScalar       sum;
92497952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
925f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
926f2e71c43SSatish Balay   PetscInt         jrow;
927e36a17ebSSatish Balay #endif
92817ab2063SBarry Smith 
929b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
93097952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
931fee21e36SBarry Smith #endif
932fee21e36SBarry Smith 
9333a40ed3dSBarry Smith   PetscFunctionBegin;
934d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9351ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
93697952fefSHong Zhang   aj  = a->j;
93797952fefSHong Zhang   aa  = a->a;
938416022c9SBarry Smith   ii  = a->i;
9394eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
94097952fefSHong Zhang     m    = a->compressedrow.nrows;
94197952fefSHong Zhang     ii   = a->compressedrow.i;
94297952fefSHong Zhang     ridx = a->compressedrow.rindex;
94397952fefSHong Zhang     for (i=0; i<m; i++){
94497952fefSHong Zhang       n   = ii[i+1] - ii[i];
94597952fefSHong Zhang       aj  = a->j + ii[i];
94697952fefSHong Zhang       aa  = a->a + ii[i];
94797952fefSHong Zhang       sum = 0.0;
948a46b3154SVictor Eijkhout       nonzerorow += (n>0);
94997952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
95097952fefSHong Zhang       y[*ridx++] = sum;
95197952fefSHong Zhang     }
95297952fefSHong Zhang   } else { /* do not use compressed row format */
953b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
954b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
955b05257ddSBarry Smith #else
95617ab2063SBarry Smith     for (i=0; i<m; i++) {
9579ea0dfa2SSatish Balay       jrow = ii[i];
9589ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
95917ab2063SBarry Smith       sum  = 0.0;
960a46b3154SVictor Eijkhout       nonzerorow += (n>0);
9619ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
96297952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9639ea0dfa2SSatish Balay       }
96417ab2063SBarry Smith       y[i] = sum;
96517ab2063SBarry Smith     }
9668d195f9aSBarry Smith #endif
967b05257ddSBarry Smith   }
968a46b3154SVictor Eijkhout   ierr = PetscLogFlops(2*a->nz - nonzerorow);CHKERRQ(ierr);
969d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9701ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9713a40ed3dSBarry Smith   PetscFunctionReturn(0);
97217ab2063SBarry Smith }
97317ab2063SBarry Smith 
9744a2ae208SSatish Balay #undef __FUNCT__
9754a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
976dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
97717ab2063SBarry Smith {
978416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
97954f21887SBarry Smith   PetscScalar     *x,*y,*z;
98054f21887SBarry Smith   const MatScalar *aa;
981dfbe8321SBarry Smith   PetscErrorCode  ierr;
982d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
983aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
98497952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
985362ced78SSatish Balay   PetscScalar     sum;
98697952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
987e36a17ebSSatish Balay #endif
9889ea0dfa2SSatish Balay 
9893a40ed3dSBarry Smith   PetscFunctionBegin;
9901ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9911ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9922e8a6d31SBarry Smith   if (zz != yy) {
9931ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9942e8a6d31SBarry Smith   } else {
9952e8a6d31SBarry Smith     z = y;
9962e8a6d31SBarry Smith   }
997bfeeae90SHong Zhang 
99897952fefSHong Zhang   aj  = a->j;
99997952fefSHong Zhang   aa  = a->a;
1000cddf8d76SBarry Smith   ii  = a->i;
1001aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
100297952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
100302ab625aSSatish Balay #else
10044eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10054eb6d288SHong Zhang     if (zz != yy){
10064eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10074eb6d288SHong Zhang     }
100897952fefSHong Zhang     m    = a->compressedrow.nrows;
100997952fefSHong Zhang     ii   = a->compressedrow.i;
101097952fefSHong Zhang     ridx = a->compressedrow.rindex;
101197952fefSHong Zhang     for (i=0; i<m; i++){
101297952fefSHong Zhang       n  = ii[i+1] - ii[i];
101397952fefSHong Zhang       aj  = a->j + ii[i];
101497952fefSHong Zhang       aa  = a->a + ii[i];
101597952fefSHong Zhang       sum = y[*ridx];
101697952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
101797952fefSHong Zhang       z[*ridx++] = sum;
101897952fefSHong Zhang     }
101997952fefSHong Zhang   } else { /* do not use compressed row format */
102017ab2063SBarry Smith     for (i=0; i<m; i++) {
10219ea0dfa2SSatish Balay       jrow = ii[i];
10229ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
102317ab2063SBarry Smith       sum  = y[i];
10249ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
102597952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10269ea0dfa2SSatish Balay       }
102717ab2063SBarry Smith       z[i] = sum;
102817ab2063SBarry Smith     }
102997952fefSHong Zhang   }
103002ab625aSSatish Balay #endif
1031efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
10321ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10331ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10342e8a6d31SBarry Smith   if (zz != yy) {
10351ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10362e8a6d31SBarry Smith   }
10373a40ed3dSBarry Smith   PetscFunctionReturn(0);
103817ab2063SBarry Smith }
103917ab2063SBarry Smith 
104017ab2063SBarry Smith /*
104117ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
104217ab2063SBarry Smith */
10434a2ae208SSatish Balay #undef __FUNCT__
10444a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1045dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
104617ab2063SBarry Smith {
1047416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10486849ba73SBarry Smith   PetscErrorCode ierr;
1049d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
105017ab2063SBarry Smith 
10513a40ed3dSBarry Smith   PetscFunctionBegin;
105209f38230SBarry Smith   if (!a->diag) {
105309f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10549518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
105509f38230SBarry Smith   }
1056d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
105709f38230SBarry Smith     a->diag[i] = a->i[i+1];
1058bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1059bfeeae90SHong Zhang       if (a->j[j] == i) {
106009f38230SBarry Smith         a->diag[i] = j;
106117ab2063SBarry Smith         break;
106217ab2063SBarry Smith       }
106317ab2063SBarry Smith     }
106417ab2063SBarry Smith   }
10653a40ed3dSBarry Smith   PetscFunctionReturn(0);
106617ab2063SBarry Smith }
106717ab2063SBarry Smith 
1068be5855fcSBarry Smith /*
1069be5855fcSBarry Smith      Checks for missing diagonals
1070be5855fcSBarry Smith */
10714a2ae208SSatish Balay #undef __FUNCT__
10724a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
107309f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1074be5855fcSBarry Smith {
1075be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
107697f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1077be5855fcSBarry Smith 
1078be5855fcSBarry Smith   PetscFunctionBegin;
107909f38230SBarry Smith   *missing = PETSC_FALSE;
1080d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
108109f38230SBarry Smith     *missing  = PETSC_TRUE;
108209f38230SBarry Smith     if (d) *d = 0;
108309f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
108409f38230SBarry Smith   } else {
1085f1e2ffcdSBarry Smith     diag = a->diag;
1086d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1087bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
108809f38230SBarry Smith 	*missing = PETSC_TRUE;
108909f38230SBarry Smith 	if (d) *d = i;
109009f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
109109f38230SBarry Smith       }
1092be5855fcSBarry Smith     }
1093be5855fcSBarry Smith   }
1094be5855fcSBarry Smith   PetscFunctionReturn(0);
1095be5855fcSBarry Smith }
1096be5855fcSBarry Smith 
109771f1c65dSBarry Smith EXTERN_C_BEGIN
109871f1c65dSBarry Smith #undef __FUNCT__
109971f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
110071f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
110171f1c65dSBarry Smith {
110271f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
110371f1c65dSBarry Smith   PetscErrorCode ierr;
1104d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
110554f21887SBarry Smith   MatScalar      *v = a->a;
110654f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
110771f1c65dSBarry Smith 
110871f1c65dSBarry Smith   PetscFunctionBegin;
110971f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
111071f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
111171f1c65dSBarry Smith   diag = a->diag;
111271f1c65dSBarry Smith   if (!a->idiag) {
111371f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
111471f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
111571f1c65dSBarry Smith     v        = a->a;
111671f1c65dSBarry Smith   }
111771f1c65dSBarry Smith   mdiag = a->mdiag;
111871f1c65dSBarry Smith   idiag = a->idiag;
111971f1c65dSBarry Smith 
1120028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
112171f1c65dSBarry Smith     for (i=0; i<m; i++) {
112271f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
112371f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
112471f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
112571f1c65dSBarry Smith     }
112671f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
112771f1c65dSBarry Smith   } else {
112871f1c65dSBarry Smith     for (i=0; i<m; i++) {
112971f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
113071f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
113171f1c65dSBarry Smith     }
113271f1c65dSBarry Smith     ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
113371f1c65dSBarry Smith   }
113471f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
113571f1c65dSBarry Smith   PetscFunctionReturn(0);
113671f1c65dSBarry Smith }
11375a9745a3SMatthew Knepley EXTERN_C_END
113871f1c65dSBarry Smith 
11394a2ae208SSatish Balay #undef __FUNCT__
11404a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
114197f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
114217ab2063SBarry Smith {
1143416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1144beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
114554f21887SBarry Smith   const MatScalar    *v = a->a;
114654f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1147dfbe8321SBarry Smith   PetscErrorCode     ierr;
1148d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
114997f1f81fSBarry Smith   const PetscInt     *idx,*diag;
115017ab2063SBarry Smith 
11513a40ed3dSBarry Smith   PetscFunctionBegin;
1152b965ef7fSBarry Smith   its = its*lits;
115391723122SBarry Smith 
115471f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
115571f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
115671f1c65dSBarry Smith   a->fshift = fshift;
115771f1c65dSBarry Smith   a->omega  = omega;
1158ed480e8bSBarry Smith 
115971f1c65dSBarry Smith   diag = a->diag;
116071f1c65dSBarry Smith   t     = a->ssor_work;
1161ed480e8bSBarry Smith   idiag = a->idiag;
116271f1c65dSBarry Smith   mdiag = a->mdiag;
1163ed480e8bSBarry Smith 
11641ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1165fb2e594dSBarry Smith   if (xx != bb) {
11661ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1167fb2e594dSBarry Smith   } else {
1168fb2e594dSBarry Smith     b = x;
1169fb2e594dSBarry Smith   }
117071f1c65dSBarry Smith   CHKMEMQ;
1171ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1172ed480e8bSBarry Smith   xs   = x;
117317ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
117417ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1175ed480e8bSBarry Smith     bs = b;
117617ab2063SBarry Smith     for (i=0; i<m; i++) {
117771f1c65dSBarry Smith         d    = fshift + mdiag[i];
1178416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1179ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1180ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
118117ab2063SBarry Smith         sum  = b[i]*d/omega;
118217ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
118317ab2063SBarry Smith         x[i] = sum;
118417ab2063SBarry Smith     }
11851ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11861ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1187efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11883a40ed3dSBarry Smith     PetscFunctionReturn(0);
118917ab2063SBarry Smith   }
1190c783ea89SBarry Smith 
119148af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
119229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11933a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
119417ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
119517ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
119617ab2063SBarry Smith 
119717ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
119817ab2063SBarry Smith 
119917ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
120017ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
120117ab2063SBarry Smith     is the relaxation factor.
120217ab2063SBarry Smith     */
120317ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
120417ab2063SBarry Smith 
120517ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
120617ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1207416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1208ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1209ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
121017ab2063SBarry Smith       sum  = b[i];
121117ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1212ed480e8bSBarry Smith       x[i] = sum*idiag[i];
121317ab2063SBarry Smith     }
121417ab2063SBarry Smith 
121517ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1216416022c9SBarry Smith     v = a->a;
1217ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
121817ab2063SBarry Smith 
121917ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1220ed480e8bSBarry Smith     ts = t;
1221416022c9SBarry Smith     diag = a->diag;
122217ab2063SBarry Smith     for (i=0; i<m; i++) {
1223416022c9SBarry Smith       n    = diag[i] - a->i[i];
1224ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1225ed480e8bSBarry Smith       v    = a->a + a->i[i];
122617ab2063SBarry Smith       sum  = t[i];
122717ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1228ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1229733d66baSBarry Smith       /*  x = x + t */
1230733d66baSBarry Smith       x[i] += t[i];
123117ab2063SBarry Smith     }
123217ab2063SBarry Smith 
1233efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
12341ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12351ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12363a40ed3dSBarry Smith     PetscFunctionReturn(0);
123717ab2063SBarry Smith   }
123817ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
123917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
124077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
124197f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
124277d8c4bbSBarry Smith #else
124317ab2063SBarry Smith       for (i=0; i<m; i++) {
1244416022c9SBarry Smith         n    = diag[i] - a->i[i];
1245ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1246ed480e8bSBarry Smith         v    = a->a + a->i[i];
124717ab2063SBarry Smith         sum  = b[i];
124817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1249ed480e8bSBarry Smith         x[i] = sum*idiag[i];
125017ab2063SBarry Smith       }
125177d8c4bbSBarry Smith #endif
125217ab2063SBarry Smith       xb = x;
1253efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12543a40ed3dSBarry Smith     } else xb = b;
125517ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
125617ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
125717ab2063SBarry Smith       for (i=0; i<m; i++) {
1258ed480e8bSBarry Smith         x[i] *= mdiag[i];
125917ab2063SBarry Smith       }
1260efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
126117ab2063SBarry Smith     }
126217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
126377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
126497f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
126577d8c4bbSBarry Smith #else
126617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1267416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1268ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1269ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
127017ab2063SBarry Smith         sum  = xb[i];
127117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1272ed480e8bSBarry Smith         x[i] = sum*idiag[i];
127317ab2063SBarry Smith       }
127477d8c4bbSBarry Smith #endif
1275efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
127617ab2063SBarry Smith     }
127717ab2063SBarry Smith     its--;
127817ab2063SBarry Smith   }
127917ab2063SBarry Smith   while (its--) {
128017ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
128177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
128297f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
128377d8c4bbSBarry Smith #else
128417ab2063SBarry Smith       for (i=0; i<m; i++) {
1285416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1286ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1287ed480e8bSBarry Smith         v    = a->a + a->i[i];
128817ab2063SBarry Smith         sum  = b[i];
128917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1290ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
129117ab2063SBarry Smith       }
129277d8c4bbSBarry Smith #endif
1293efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
129417ab2063SBarry Smith     }
129517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
129677d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
129797f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
129877d8c4bbSBarry Smith #else
129917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1300416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1301ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1302ed480e8bSBarry Smith         v    = a->a + a->i[i];
130317ab2063SBarry Smith         sum  = b[i];
130417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1305ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
130617ab2063SBarry Smith       }
130777d8c4bbSBarry Smith #endif
1308efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
130917ab2063SBarry Smith     }
131017ab2063SBarry Smith   }
13111ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13121ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
131371f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
131417ab2063SBarry Smith }
131517ab2063SBarry Smith 
13162af78befSBarry Smith 
13174a2ae208SSatish Balay #undef __FUNCT__
13184a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1319dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
132017ab2063SBarry Smith {
1321416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13224e220ebcSLois Curfman McInnes 
13233a40ed3dSBarry Smith   PetscFunctionBegin;
1324d0f46423SBarry Smith   info->rows_global    = (double)A->rmap->n;
1325d0f46423SBarry Smith   info->columns_global = (double)A->cmap->n;
1326d0f46423SBarry Smith   info->rows_local     = (double)A->rmap->n;
1327d0f46423SBarry Smith   info->columns_local  = (double)A->cmap->n;
13284e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13294e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13304e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13314e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13324e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13334e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13347adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13354e220ebcSLois Curfman McInnes   if (A->factor) {
13364e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13374e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13384e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13394e220ebcSLois Curfman McInnes   } else {
13404e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13414e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13424e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13434e220ebcSLois Curfman McInnes   }
13443a40ed3dSBarry Smith   PetscFunctionReturn(0);
134517ab2063SBarry Smith }
134617ab2063SBarry Smith 
13474a2ae208SSatish Balay #undef __FUNCT__
13484a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1349f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
135017ab2063SBarry Smith {
1351416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1352d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d;
13536849ba73SBarry Smith   PetscErrorCode ierr;
135409f38230SBarry Smith   PetscTruth     missing;
135517ab2063SBarry Smith 
13563a40ed3dSBarry Smith   PetscFunctionBegin;
1357f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1358f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
135977431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1360bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1361f1e2ffcdSBarry Smith     }
1362f4df32b1SMatthew Knepley     if (diag != 0.0) {
136309f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
136409f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1365f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1366f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1367f1e2ffcdSBarry Smith       }
1368f1e2ffcdSBarry Smith     }
136988e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1370f1e2ffcdSBarry Smith   } else {
1371f4df32b1SMatthew Knepley     if (diag != 0.0) {
137217ab2063SBarry Smith       for (i=0; i<N; i++) {
137377431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13747ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1375416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1376f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1377bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13787ae801bdSBarry Smith         } else { /* in case row was completely empty */
1379f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
138017ab2063SBarry Smith         }
138117ab2063SBarry Smith       }
13823a40ed3dSBarry Smith     } else {
138317ab2063SBarry Smith       for (i=0; i<N; i++) {
138477431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1385416022c9SBarry Smith         a->ilen[rows[i]] = 0;
138617ab2063SBarry Smith       }
138717ab2063SBarry Smith     }
138888e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1389f1e2ffcdSBarry Smith   }
139043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13913a40ed3dSBarry Smith   PetscFunctionReturn(0);
139217ab2063SBarry Smith }
139317ab2063SBarry Smith 
13944a2ae208SSatish Balay #undef __FUNCT__
13954a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1396a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
139717ab2063SBarry Smith {
1398416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
139997f1f81fSBarry Smith   PetscInt   *itmp;
140017ab2063SBarry Smith 
14013a40ed3dSBarry Smith   PetscFunctionBegin;
1402d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
140317ab2063SBarry Smith 
1404416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1405bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
140617ab2063SBarry Smith   if (idx) {
1407bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1408bfeeae90SHong Zhang     if (*nz) {
14094e093b46SBarry Smith       *idx = itmp;
141017ab2063SBarry Smith     }
141117ab2063SBarry Smith     else *idx = 0;
141217ab2063SBarry Smith   }
14133a40ed3dSBarry Smith   PetscFunctionReturn(0);
141417ab2063SBarry Smith }
141517ab2063SBarry Smith 
1416bfeeae90SHong Zhang /* remove this function? */
14174a2ae208SSatish Balay #undef __FUNCT__
14184a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1419a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
142017ab2063SBarry Smith {
14213a40ed3dSBarry Smith   PetscFunctionBegin;
14223a40ed3dSBarry Smith   PetscFunctionReturn(0);
142317ab2063SBarry Smith }
142417ab2063SBarry Smith 
14254a2ae208SSatish Balay #undef __FUNCT__
14264a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1427dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
142817ab2063SBarry Smith {
1429416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
143054f21887SBarry Smith   MatScalar      *v = a->a;
143136db0b34SBarry Smith   PetscReal      sum = 0.0;
14326849ba73SBarry Smith   PetscErrorCode ierr;
143397f1f81fSBarry Smith   PetscInt       i,j;
143417ab2063SBarry Smith 
14353a40ed3dSBarry Smith   PetscFunctionBegin;
143617ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1437416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1438aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
143936db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
144017ab2063SBarry Smith #else
144117ab2063SBarry Smith       sum += (*v)*(*v); v++;
144217ab2063SBarry Smith #endif
144317ab2063SBarry Smith     }
1444064f8208SBarry Smith     *nrm = sqrt(sum);
14453a40ed3dSBarry Smith   } else if (type == NORM_1) {
144636db0b34SBarry Smith     PetscReal *tmp;
144797f1f81fSBarry Smith     PetscInt    *jj = a->j;
1448d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1449d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1450064f8208SBarry Smith     *nrm = 0.0;
1451416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1452bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
145317ab2063SBarry Smith     }
1454d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1455064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
145617ab2063SBarry Smith     }
1457606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14583a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1459064f8208SBarry Smith     *nrm = 0.0;
1460d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1461bfeeae90SHong Zhang       v = a->a + a->i[j];
146217ab2063SBarry Smith       sum = 0.0;
1463416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1464cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
146517ab2063SBarry Smith       }
1466064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
146717ab2063SBarry Smith     }
14683a40ed3dSBarry Smith   } else {
146929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
147017ab2063SBarry Smith   }
14713a40ed3dSBarry Smith   PetscFunctionReturn(0);
147217ab2063SBarry Smith }
147317ab2063SBarry Smith 
14744a2ae208SSatish Balay #undef __FUNCT__
14754a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1476fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
147717ab2063SBarry Smith {
1478416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1479416022c9SBarry Smith   Mat            C;
14806849ba73SBarry Smith   PetscErrorCode ierr;
1481d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
148254f21887SBarry Smith   MatScalar      *array = a->a;
148317ab2063SBarry Smith 
14843a40ed3dSBarry Smith   PetscFunctionBegin;
1485d0f46423SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1486fc4dec0aSBarry Smith 
1487fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1488d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1489d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1490bfeeae90SHong Zhang 
1491bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14927adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1493d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
14947adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1495ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1496606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1497a541d17aSBarry Smith   } else {
1498a541d17aSBarry Smith     C = *B;
1499a541d17aSBarry Smith   }
1500a541d17aSBarry Smith 
150117ab2063SBarry Smith   for (i=0; i<m; i++) {
150217ab2063SBarry Smith     len    = ai[i+1]-ai[i];
150387d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1504b9b97703SBarry Smith     array += len;
1505b9b97703SBarry Smith     aj    += len;
150617ab2063SBarry Smith   }
15076d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15086d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
150917ab2063SBarry Smith 
1510815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1511416022c9SBarry Smith     *B = C;
151217ab2063SBarry Smith   } else {
1513273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
151417ab2063SBarry Smith   }
15153a40ed3dSBarry Smith   PetscFunctionReturn(0);
151617ab2063SBarry Smith }
151717ab2063SBarry Smith 
1518cd0d46ebSvictorle EXTERN_C_BEGIN
1519cd0d46ebSvictorle #undef __FUNCT__
15205fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1521be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1522cd0d46ebSvictorle {
1523cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
152454f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
152554f21887SBarry Smith   MatScalar      *va,*vb;
15266849ba73SBarry Smith   PetscErrorCode ierr;
152797f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1528cd0d46ebSvictorle 
1529cd0d46ebSvictorle   PetscFunctionBegin;
1530cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1531cd0d46ebSvictorle 
1532cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1533cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15345485867bSBarry Smith   if (ma!=nb || na!=mb){
15355485867bSBarry Smith     *f = PETSC_FALSE;
15365485867bSBarry Smith     PetscFunctionReturn(0);
15375485867bSBarry Smith   }
1538cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1539cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1540cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
154197f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
154297f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1543cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1544cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1545cd0d46ebSvictorle 
1546cd0d46ebSvictorle   *f = PETSC_TRUE;
1547cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1548cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
154997f1f81fSBarry Smith       PetscInt         idc,idr;
15505485867bSBarry Smith       PetscScalar vc,vr;
1551cd0d46ebSvictorle       /* column/row index/value */
15525485867bSBarry Smith       idc = adx[aptr[i]];
15535485867bSBarry Smith       idr = bdx[bptr[idc]];
15545485867bSBarry Smith       vc  = va[aptr[i]];
15555485867bSBarry Smith       vr  = vb[bptr[idc]];
15565485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15575485867bSBarry Smith 	*f = PETSC_FALSE;
15585485867bSBarry Smith         goto done;
1559cd0d46ebSvictorle       } else {
15605485867bSBarry Smith 	aptr[i]++;
15615485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1562cd0d46ebSvictorle       }
1563cd0d46ebSvictorle     }
1564cd0d46ebSvictorle   }
1565cd0d46ebSvictorle  done:
1566cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15673aeef889SHong Zhang   if (B) {
15683aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15693aeef889SHong Zhang   }
1570cd0d46ebSvictorle   PetscFunctionReturn(0);
1571cd0d46ebSvictorle }
1572cd0d46ebSvictorle EXTERN_C_END
1573cd0d46ebSvictorle 
15741cbb95d3SBarry Smith EXTERN_C_BEGIN
15751cbb95d3SBarry Smith #undef __FUNCT__
15761cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15771cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15781cbb95d3SBarry Smith {
15791cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
158054f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
158154f21887SBarry Smith   MatScalar      *va,*vb;
15821cbb95d3SBarry Smith   PetscErrorCode ierr;
15831cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15841cbb95d3SBarry Smith 
15851cbb95d3SBarry Smith   PetscFunctionBegin;
15861cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15871cbb95d3SBarry Smith 
15881cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15891cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15901cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15911cbb95d3SBarry Smith     *f = PETSC_FALSE;
15921cbb95d3SBarry Smith     PetscFunctionReturn(0);
15931cbb95d3SBarry Smith   }
15941cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15951cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15961cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
15971cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
15981cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
15991cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
16001cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
16011cbb95d3SBarry Smith 
16021cbb95d3SBarry Smith   *f = PETSC_TRUE;
16031cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
16041cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
16051cbb95d3SBarry Smith       PetscInt         idc,idr;
16061cbb95d3SBarry Smith       PetscScalar vc,vr;
16071cbb95d3SBarry Smith       /* column/row index/value */
16081cbb95d3SBarry Smith       idc = adx[aptr[i]];
16091cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16101cbb95d3SBarry Smith       vc  = va[aptr[i]];
16111cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16121cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16131cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16141cbb95d3SBarry Smith         goto done;
16151cbb95d3SBarry Smith       } else {
16161cbb95d3SBarry Smith 	aptr[i]++;
16171cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16181cbb95d3SBarry Smith       }
16191cbb95d3SBarry Smith     }
16201cbb95d3SBarry Smith   }
16211cbb95d3SBarry Smith  done:
16221cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16231cbb95d3SBarry Smith   if (B) {
16241cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16251cbb95d3SBarry Smith   }
16261cbb95d3SBarry Smith   PetscFunctionReturn(0);
16271cbb95d3SBarry Smith }
16281cbb95d3SBarry Smith EXTERN_C_END
16291cbb95d3SBarry Smith 
16309e29f15eSvictorle #undef __FUNCT__
16319e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1632dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16339e29f15eSvictorle {
1634dfbe8321SBarry Smith   PetscErrorCode ierr;
16359e29f15eSvictorle   PetscFunctionBegin;
16365485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16379e29f15eSvictorle   PetscFunctionReturn(0);
16389e29f15eSvictorle }
16399e29f15eSvictorle 
16404a2ae208SSatish Balay #undef __FUNCT__
16411cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16421cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16431cbb95d3SBarry Smith {
16441cbb95d3SBarry Smith   PetscErrorCode ierr;
16451cbb95d3SBarry Smith   PetscFunctionBegin;
16461cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16471cbb95d3SBarry Smith   PetscFunctionReturn(0);
16481cbb95d3SBarry Smith }
16491cbb95d3SBarry Smith 
16501cbb95d3SBarry Smith #undef __FUNCT__
16514a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1652dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
165317ab2063SBarry Smith {
1654416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
165554f21887SBarry Smith   PetscScalar    *l,*r,x;
165654f21887SBarry Smith   MatScalar      *v;
1657dfbe8321SBarry Smith   PetscErrorCode ierr;
1658d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
165917ab2063SBarry Smith 
16603a40ed3dSBarry Smith   PetscFunctionBegin;
166117ab2063SBarry Smith   if (ll) {
16623ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16633ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1664e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1665d0f46423SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16661ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1667416022c9SBarry Smith     v = a->a;
166817ab2063SBarry Smith     for (i=0; i<m; i++) {
166917ab2063SBarry Smith       x = l[i];
1670416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
167117ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
167217ab2063SBarry Smith     }
16731ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1674efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
167517ab2063SBarry Smith   }
167617ab2063SBarry Smith   if (rr) {
1677e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1678d0f46423SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16791ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1680416022c9SBarry Smith     v = a->a; jj = a->j;
168117ab2063SBarry Smith     for (i=0; i<nz; i++) {
1682bfeeae90SHong Zhang       (*v++) *= r[*jj++];
168317ab2063SBarry Smith     }
16841ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1685efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
168617ab2063SBarry Smith   }
16873a40ed3dSBarry Smith   PetscFunctionReturn(0);
168817ab2063SBarry Smith }
168917ab2063SBarry Smith 
16904a2ae208SSatish Balay #undef __FUNCT__
16914a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
169297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
169317ab2063SBarry Smith {
1694db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16956849ba73SBarry Smith   PetscErrorCode ierr;
1696d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
169797f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
169897f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
169997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
170054f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1701416022c9SBarry Smith   Mat            C;
1702fee21e36SBarry Smith   PetscTruth     stride;
170317ab2063SBarry Smith 
17043a40ed3dSBarry Smith   PetscFunctionBegin;
1705d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
170629bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1707d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
170829bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
170999141d43SSatish Balay 
171017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1711b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1712b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
171317ab2063SBarry Smith 
1714fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1715fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1716fee21e36SBarry Smith   if (stride && step == 1) {
171702834360SBarry Smith     /* special case of contiguous rows */
171897f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
171931ebf83bSSatish Balay     starts = lens + nrows;
172002834360SBarry Smith     /* loop over new rows determining lens and starting points */
172102834360SBarry Smith     for (i=0; i<nrows; i++) {
1722bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1723a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
172402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1725bfeeae90SHong Zhang         if (aj[k] >= first) {
172602834360SBarry Smith           starts[i] = k;
172702834360SBarry Smith           break;
172802834360SBarry Smith 	}
172902834360SBarry Smith       }
1730a2744918SBarry Smith       sum = 0;
173102834360SBarry Smith       while (k < kend) {
1732bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1733a2744918SBarry Smith         sum++;
173402834360SBarry Smith       }
1735a2744918SBarry Smith       lens[i] = sum;
173602834360SBarry Smith     }
173702834360SBarry Smith     /* create submatrix */
1738cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
173997f1f81fSBarry Smith       PetscInt n_cols,n_rows;
174008480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
174129bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1742d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
174308480c60SBarry Smith       C = *B;
17443a40ed3dSBarry Smith     } else {
17457adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1746f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17477adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1748ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
174908480c60SBarry Smith     }
1750db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1751db02288aSLois Curfman McInnes 
175202834360SBarry Smith     /* loop over rows inserting into submatrix */
1753db02288aSLois Curfman McInnes     a_new    = c->a;
1754db02288aSLois Curfman McInnes     j_new    = c->j;
1755db02288aSLois Curfman McInnes     i_new    = c->i;
1756bfeeae90SHong Zhang 
175702834360SBarry Smith     for (i=0; i<nrows; i++) {
1758a2744918SBarry Smith       ii    = starts[i];
1759a2744918SBarry Smith       lensi = lens[i];
1760a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1761a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
176202834360SBarry Smith       }
176387828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1764a2744918SBarry Smith       a_new      += lensi;
1765a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1766a2744918SBarry Smith       c->ilen[i]  = lensi;
176702834360SBarry Smith     }
1768606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17693a40ed3dSBarry Smith   } else {
177002834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
177197f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1772bfeeae90SHong Zhang 
177397f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
177497f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
177517ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
177602834360SBarry Smith     /* determine lens of each row */
177702834360SBarry Smith     for (i=0; i<nrows; i++) {
1778bfeeae90SHong Zhang       kstart  = ai[irow[i]];
177902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
178002834360SBarry Smith       lens[i] = 0;
178102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1782bfeeae90SHong Zhang         if (smap[aj[k]]) {
178302834360SBarry Smith           lens[i]++;
178402834360SBarry Smith         }
178502834360SBarry Smith       }
178602834360SBarry Smith     }
178717ab2063SBarry Smith     /* Create and fill new matrix */
1788a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17890f5bd95cSBarry Smith       PetscTruth equal;
17900f5bd95cSBarry Smith 
179199141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1792d0f46423SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1793d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17940f5bd95cSBarry Smith       if (!equal) {
179529bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
179699141d43SSatish Balay       }
1797d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
179808480c60SBarry Smith       C = *B;
17993a40ed3dSBarry Smith     } else {
18007adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1801f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
18027adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1803ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
180408480c60SBarry Smith     }
180599141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
180617ab2063SBarry Smith     for (i=0; i<nrows; i++) {
180799141d43SSatish Balay       row    = irow[i];
1808bfeeae90SHong Zhang       kstart = ai[row];
180999141d43SSatish Balay       kend   = kstart + a->ilen[row];
1810bfeeae90SHong Zhang       mat_i  = c->i[i];
181199141d43SSatish Balay       mat_j  = c->j + mat_i;
181299141d43SSatish Balay       mat_a  = c->a + mat_i;
181399141d43SSatish Balay       mat_ilen = c->ilen + i;
181417ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1815bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1816ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
181799141d43SSatish Balay           *mat_a++ = a->a[k];
181899141d43SSatish Balay           (*mat_ilen)++;
181999141d43SSatish Balay 
182017ab2063SBarry Smith         }
182117ab2063SBarry Smith       }
182217ab2063SBarry Smith     }
182302834360SBarry Smith     /* Free work space */
182402834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1825606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1826606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
182702834360SBarry Smith   }
18286d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18296d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
183017ab2063SBarry Smith 
183117ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1832416022c9SBarry Smith   *B = C;
18333a40ed3dSBarry Smith   PetscFunctionReturn(0);
183417ab2063SBarry Smith }
183517ab2063SBarry Smith 
1836a871dcd8SBarry Smith /*
1837a871dcd8SBarry Smith */
18384a2ae208SSatish Balay #undef __FUNCT__
18394a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1840dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1841a871dcd8SBarry Smith {
184263b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1843dfbe8321SBarry Smith   PetscErrorCode ierr;
184463b91edcSBarry Smith   Mat            outA;
1845b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
184663b91edcSBarry Smith 
18473a40ed3dSBarry Smith   PetscFunctionBegin;
1848d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1849b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1850b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1851a871dcd8SBarry Smith 
185263b91edcSBarry Smith   outA          = inA;
18535c9eb25fSBarry Smith   inA->factor   = MAT_FACTOR_LU;
1854c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1855c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row); CHKERRQ(ierr);}
1856c3122656SLisandro Dalcin   a->row = row;
1857c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1858c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col); CHKERRQ(ierr);}
1859c3122656SLisandro Dalcin   a->col = col;
186063b91edcSBarry Smith 
186136db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1862b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18634c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
186452e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1865f0ec6fceSSatish Balay 
186694a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1867d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1868d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
186994a9d846SBarry Smith   }
187063b91edcSBarry Smith 
1871f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1872137fb511SHong Zhang   if (row_identity && col_identity) {
1873*719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ(outA,inA,info);CHKERRQ(ierr);
1874137fb511SHong Zhang   } else {
1875*719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1876137fb511SHong Zhang   }
18773a40ed3dSBarry Smith   PetscFunctionReturn(0);
1878a871dcd8SBarry Smith }
1879a871dcd8SBarry Smith 
1880d9eff348SSatish Balay #include "petscblaslapack.h"
18814a2ae208SSatish Balay #undef __FUNCT__
18824a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1883f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1884f0b747eeSBarry Smith {
1885f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1886f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1887efee365bSSatish Balay   PetscErrorCode ierr;
18880805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18893a40ed3dSBarry Smith 
18903a40ed3dSBarry Smith   PetscFunctionBegin;
1891f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1892efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18933a40ed3dSBarry Smith   PetscFunctionReturn(0);
1894f0b747eeSBarry Smith }
1895f0b747eeSBarry Smith 
18964a2ae208SSatish Balay #undef __FUNCT__
18974a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
189897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1899cddf8d76SBarry Smith {
1900dfbe8321SBarry Smith   PetscErrorCode ierr;
190197f1f81fSBarry Smith   PetscInt       i;
1902cddf8d76SBarry Smith 
19033a40ed3dSBarry Smith   PetscFunctionBegin;
1904cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1905b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1906cddf8d76SBarry Smith   }
1907cddf8d76SBarry Smith 
1908cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19096a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1910cddf8d76SBarry Smith   }
19113a40ed3dSBarry Smith   PetscFunctionReturn(0);
1912cddf8d76SBarry Smith }
1913cddf8d76SBarry Smith 
19144a2ae208SSatish Balay #undef __FUNCT__
19154a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
191697f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19174dcbc457SBarry Smith {
1918e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19196849ba73SBarry Smith   PetscErrorCode ierr;
192097f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
192197f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1922f1af5d2fSBarry Smith   PetscBT        table;
1923bbd702dbSSatish Balay 
19243a40ed3dSBarry Smith   PetscFunctionBegin;
1925d0f46423SBarry Smith   m     = A->rmap->n;
1926e4d965acSSatish Balay   ai    = a->i;
1927bfeeae90SHong Zhang   aj    = a->j;
19288a047759SSatish Balay 
1929a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
193006763907SSatish Balay 
193197f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19326831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
193306763907SSatish Balay 
1934e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1935b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1936e4d965acSSatish Balay     isz  = 0;
19376831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1938e4d965acSSatish Balay 
1939e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19404dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1941b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1942e4d965acSSatish Balay 
1943dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1944e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1945f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19464dcbc457SBarry Smith     }
194706763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
194806763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1949e4d965acSSatish Balay 
195004a348a9SBarry Smith     k = 0;
195104a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
195204a348a9SBarry Smith       n = isz;
195306763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1954e4d965acSSatish Balay         row   = nidx[k];
1955e4d965acSSatish Balay         start = ai[row];
1956e4d965acSSatish Balay         end   = ai[row+1];
195704a348a9SBarry Smith         for (l = start; l<end ; l++){
1958efb16452SHong Zhang           val = aj[l] ;
1959f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1960e4d965acSSatish Balay         }
1961e4d965acSSatish Balay       }
1962e4d965acSSatish Balay     }
1963029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1964e4d965acSSatish Balay   }
19656831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1966606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19673a40ed3dSBarry Smith   PetscFunctionReturn(0);
19684dcbc457SBarry Smith }
196917ab2063SBarry Smith 
19700513a670SBarry Smith /* -------------------------------------------------------------- */
19714a2ae208SSatish Balay #undef __FUNCT__
19724a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1973dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19740513a670SBarry Smith {
19750513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19766849ba73SBarry Smith   PetscErrorCode ierr;
1977d0f46423SBarry Smith   PetscInt       i,nz,m = A->rmap->n,n = A->cmap->n,*col;
197897f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
197956cd22aeSBarry Smith   IS             icolp,irowp;
198097f1f81fSBarry Smith   PetscInt       *cwork;
198132ec9ce4SBarry Smith   PetscScalar    *vwork;
19820513a670SBarry Smith 
19833a40ed3dSBarry Smith   PetscFunctionBegin;
19844c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
198556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19864c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
198756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19880513a670SBarry Smith 
19890513a670SBarry Smith   /* determine lengths of permuted rows */
199097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19910513a670SBarry Smith   for (i=0; i<m; i++) {
19920513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19930513a670SBarry Smith   }
19947adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
1995f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
19967adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1997ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1998606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
19990513a670SBarry Smith 
200097f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
20010513a670SBarry Smith   for (i=0; i<m; i++) {
200232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20030513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2004cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
200532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20060513a670SBarry Smith   }
2007606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20083c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20090513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20100513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
201156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
201256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
201356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
201456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20153a40ed3dSBarry Smith   PetscFunctionReturn(0);
20160513a670SBarry Smith }
20170513a670SBarry Smith 
20184a2ae208SSatish Balay #undef __FUNCT__
20194a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2020dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2021cb5b572fSBarry Smith {
2022dfbe8321SBarry Smith   PetscErrorCode ierr;
2023cb5b572fSBarry Smith 
2024cb5b572fSBarry Smith   PetscFunctionBegin;
202533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
202633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2027be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2028be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2029be6bf707SBarry Smith 
2030d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2031634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2032cb5b572fSBarry Smith     }
2033d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2034cb5b572fSBarry Smith   } else {
2035cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2036cb5b572fSBarry Smith   }
2037cb5b572fSBarry Smith   PetscFunctionReturn(0);
2038cb5b572fSBarry Smith }
2039cb5b572fSBarry Smith 
20404a2ae208SSatish Balay #undef __FUNCT__
20414a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2042dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2043273d9f13SBarry Smith {
2044dfbe8321SBarry Smith   PetscErrorCode ierr;
2045273d9f13SBarry Smith 
2046273d9f13SBarry Smith   PetscFunctionBegin;
2047ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2048273d9f13SBarry Smith   PetscFunctionReturn(0);
2049273d9f13SBarry Smith }
2050273d9f13SBarry Smith 
20514a2ae208SSatish Balay #undef __FUNCT__
20524a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2053a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20546c0721eeSBarry Smith {
20556c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20566c0721eeSBarry Smith   PetscFunctionBegin;
20576c0721eeSBarry Smith   *array = a->a;
20586c0721eeSBarry Smith   PetscFunctionReturn(0);
20596c0721eeSBarry Smith }
20606c0721eeSBarry Smith 
20614a2ae208SSatish Balay #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2063dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20646c0721eeSBarry Smith {
20656c0721eeSBarry Smith   PetscFunctionBegin;
20666c0721eeSBarry Smith   PetscFunctionReturn(0);
20676c0721eeSBarry Smith }
2068273d9f13SBarry Smith 
2069ee4f033dSBarry Smith #undef __FUNCT__
2070ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2071dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2072ee4f033dSBarry Smith {
20736849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20746849ba73SBarry Smith   PetscErrorCode ierr;
207597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2076efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
207787828ca2SBarry Smith   PetscScalar    *vscale_array;
2078ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2079ee4f033dSBarry Smith   Vec            w1,w2,w3;
2080ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2081ee4f033dSBarry Smith   PetscTruth     flg;
2082ee4f033dSBarry Smith 
2083ee4f033dSBarry Smith   PetscFunctionBegin;
2084ee4f033dSBarry Smith   if (!coloring->w1) {
2085ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
208652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2087ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
208852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2089ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
209052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2091ee4f033dSBarry Smith   }
2092ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2093ee4f033dSBarry Smith 
2094ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
20957adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
2096ee4f033dSBarry Smith   if (flg) {
2097ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2098ee4f033dSBarry Smith   } else {
20990b9b6f31SBarry Smith     PetscTruth assembled;
21000b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
21010b9b6f31SBarry Smith     if (assembled) {
2102ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2103ee4f033dSBarry Smith     }
21040b9b6f31SBarry Smith   }
2105ee4f033dSBarry Smith 
2106ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2107ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2108ee4f033dSBarry Smith 
2109ee4f033dSBarry Smith   /*
2110ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2111ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2112ee4f033dSBarry Smith   */
2113ee4f033dSBarry Smith   if (coloring->F) {
2114ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2115ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2116ee4f033dSBarry Smith     if (m1 != m2) {
2117ee4f033dSBarry Smith       coloring->F = 0;
2118ee4f033dSBarry Smith     }
2119ee4f033dSBarry Smith   }
2120ee4f033dSBarry Smith 
2121ee4f033dSBarry Smith   if (coloring->F) {
2122ee4f033dSBarry Smith     w1          = coloring->F;
2123ee4f033dSBarry Smith     coloring->F = 0;
2124ee4f033dSBarry Smith   } else {
212566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2126ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
212766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2128ee4f033dSBarry Smith   }
2129ee4f033dSBarry Smith 
2130ee4f033dSBarry Smith   /*
2131ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2132ee4f033dSBarry Smith   */
21331ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21341ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2135ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2136ee4f033dSBarry Smith     /*
2137ee4f033dSBarry Smith        Loop over each column associated with color adding the
2138ee4f033dSBarry Smith        perturbation to the vector w3.
2139ee4f033dSBarry Smith     */
2140ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2141ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2142ee4f033dSBarry Smith       dx  = xx[col];
2143ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2144ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2145ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2146ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2147ee4f033dSBarry Smith #else
2148ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2149ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2150ee4f033dSBarry Smith #endif
2151ee4f033dSBarry Smith       dx                *= epsilon;
2152ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2153ee4f033dSBarry Smith     }
2154ee4f033dSBarry Smith   }
21551ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2156ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2157ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2158ee4f033dSBarry Smith 
2159ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2160ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2161ee4f033dSBarry Smith 
2162ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2163ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2164ee4f033dSBarry Smith 
21651ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2166ee4f033dSBarry Smith   /*
2167ee4f033dSBarry Smith       Loop over each color
2168ee4f033dSBarry Smith   */
2169ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
217049b058dcSBarry Smith     coloring->currentcolor = k;
2171ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21721ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2173ee4f033dSBarry Smith     /*
2174ee4f033dSBarry Smith        Loop over each column associated with color adding the
2175ee4f033dSBarry Smith        perturbation to the vector w3.
2176ee4f033dSBarry Smith     */
2177ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2178ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2179ee4f033dSBarry Smith       dx  = xx[col];
21805b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2181ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2182ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2183ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2184ee4f033dSBarry Smith #else
2185ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2186ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2187ee4f033dSBarry Smith #endif
2188ee4f033dSBarry Smith       dx            *= epsilon;
2189634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2190ee4f033dSBarry Smith       w3_array[col] += dx;
2191ee4f033dSBarry Smith     }
21921ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2193ee4f033dSBarry Smith 
2194ee4f033dSBarry Smith     /*
2195ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2196ee4f033dSBarry Smith     */
2197ee4f033dSBarry Smith 
219866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2199ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
220066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2201efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2202ee4f033dSBarry Smith 
2203ee4f033dSBarry Smith     /*
2204ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2205ee4f033dSBarry Smith     */
22061ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2207ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2208ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2209ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2210ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2211ee4f033dSBarry Smith       srow   = row + start;
2212ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2213ee4f033dSBarry Smith     }
22141ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2215ee4f033dSBarry Smith   }
221649b058dcSBarry Smith   coloring->currentcolor = k;
22171ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22181ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2219ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2220ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2221ee4f033dSBarry Smith   PetscFunctionReturn(0);
2222ee4f033dSBarry Smith }
2223ee4f033dSBarry Smith 
2224ac90fabeSBarry Smith #include "petscblaslapack.h"
2225ac90fabeSBarry Smith #undef __FUNCT__
2226ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2227f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2228ac90fabeSBarry Smith {
2229dfbe8321SBarry Smith   PetscErrorCode ierr;
223097f1f81fSBarry Smith   PetscInt       i;
2231ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22320805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2233ac90fabeSBarry Smith 
2234ac90fabeSBarry Smith   PetscFunctionBegin;
2235ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2236f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2237f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2238c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2239a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2240a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2241a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2242a30b2313SHong Zhang     }
2243a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2244d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2245a30b2313SHong Zhang       y->XtoY = X;
2246407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2247c537a176SHong Zhang     }
2248f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22491e2582c4SBarry 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);
2250ac90fabeSBarry Smith   } else {
2251f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2252ac90fabeSBarry Smith   }
2253ac90fabeSBarry Smith   PetscFunctionReturn(0);
2254ac90fabeSBarry Smith }
2255ac90fabeSBarry Smith 
2256521d7252SBarry Smith #undef __FUNCT__
2257521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2258521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2259521d7252SBarry Smith {
2260521d7252SBarry Smith   PetscFunctionBegin;
2261521d7252SBarry Smith   PetscFunctionReturn(0);
2262521d7252SBarry Smith }
2263521d7252SBarry Smith 
2264354c94deSBarry Smith #undef __FUNCT__
2265354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2266354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2267354c94deSBarry Smith {
2268354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2269354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2270354c94deSBarry Smith   PetscInt    i,nz;
2271354c94deSBarry Smith   PetscScalar *a;
2272354c94deSBarry Smith 
2273354c94deSBarry Smith   PetscFunctionBegin;
2274354c94deSBarry Smith   nz = aij->nz;
2275354c94deSBarry Smith   a  = aij->a;
2276354c94deSBarry Smith   for (i=0; i<nz; i++) {
2277354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2278354c94deSBarry Smith   }
2279354c94deSBarry Smith #else
2280354c94deSBarry Smith   PetscFunctionBegin;
2281354c94deSBarry Smith #endif
2282354c94deSBarry Smith   PetscFunctionReturn(0);
2283354c94deSBarry Smith }
2284354c94deSBarry Smith 
2285e34fafa9SBarry Smith #undef __FUNCT__
2286985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2287985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2288e34fafa9SBarry Smith {
2289e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2290e34fafa9SBarry Smith   PetscErrorCode ierr;
2291d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2292e34fafa9SBarry Smith   PetscReal      atmp;
2293985db425SBarry Smith   PetscScalar    *x;
2294e34fafa9SBarry Smith   MatScalar      *aa;
2295e34fafa9SBarry Smith 
2296e34fafa9SBarry Smith   PetscFunctionBegin;
2297e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2298e34fafa9SBarry Smith   aa   = a->a;
2299e34fafa9SBarry Smith   ai   = a->i;
2300e34fafa9SBarry Smith   aj   = a->j;
2301e34fafa9SBarry Smith 
2302985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2303e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2304e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2305d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2306e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2307e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23089189402eSHong Zhang     x[i] = 0.0;
2309e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2310985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2311985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2312985db425SBarry Smith       aa++; aj++;
2313985db425SBarry Smith     }
2314985db425SBarry Smith   }
2315985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2316985db425SBarry Smith   PetscFunctionReturn(0);
2317985db425SBarry Smith }
2318985db425SBarry Smith 
2319985db425SBarry Smith #undef __FUNCT__
2320985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2321985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2322985db425SBarry Smith {
2323985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2324985db425SBarry Smith   PetscErrorCode ierr;
2325d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2326985db425SBarry Smith   PetscScalar    *x;
2327985db425SBarry Smith   MatScalar      *aa;
2328985db425SBarry Smith 
2329985db425SBarry Smith   PetscFunctionBegin;
2330985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2331985db425SBarry Smith   aa   = a->a;
2332985db425SBarry Smith   ai   = a->i;
2333985db425SBarry Smith   aj   = a->j;
2334985db425SBarry Smith 
2335985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2336985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2337985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2338d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2339985db425SBarry Smith   for (i=0; i<m; i++) {
2340985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2341d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2342985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2343985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2344985db425SBarry Smith       x[i] = 0.0;
2345985db425SBarry Smith       if (idx) {
2346985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2347985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2348985db425SBarry Smith           if (aj[j] > j) {
2349985db425SBarry Smith             idx[i] = j;
2350985db425SBarry Smith             break;
2351985db425SBarry Smith           }
2352985db425SBarry Smith         }
2353985db425SBarry Smith       }
2354985db425SBarry Smith     }
2355985db425SBarry Smith     for (j=0; j<ncols; j++){
2356985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2357985db425SBarry Smith       aa++; aj++;
2358985db425SBarry Smith     }
2359985db425SBarry Smith   }
2360985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2361985db425SBarry Smith   PetscFunctionReturn(0);
2362985db425SBarry Smith }
2363985db425SBarry Smith 
2364985db425SBarry Smith #undef __FUNCT__
2365985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2366985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2367985db425SBarry Smith {
2368985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2369985db425SBarry Smith   PetscErrorCode ierr;
2370d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2371985db425SBarry Smith   PetscScalar    *x;
2372985db425SBarry Smith   MatScalar      *aa;
2373985db425SBarry Smith 
2374985db425SBarry Smith   PetscFunctionBegin;
2375985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2376985db425SBarry Smith   aa   = a->a;
2377985db425SBarry Smith   ai   = a->i;
2378985db425SBarry Smith   aj   = a->j;
2379985db425SBarry Smith 
2380985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2381985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2382985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2383d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2384985db425SBarry Smith   for (i=0; i<m; i++) {
2385985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2386d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2387985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2388985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2389985db425SBarry Smith       x[i] = 0.0;
2390985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2391985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2392985db425SBarry Smith         for (j=0;j<ncols;j++) {
2393985db425SBarry Smith           if (aj[j] > j) {
2394985db425SBarry Smith             idx[i] = j;
2395985db425SBarry Smith             break;
2396985db425SBarry Smith           }
2397985db425SBarry Smith         }
2398985db425SBarry Smith       }
2399985db425SBarry Smith     }
2400985db425SBarry Smith     for (j=0; j<ncols; j++){
2401985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2402985db425SBarry Smith       aa++; aj++;
2403e34fafa9SBarry Smith     }
2404e34fafa9SBarry Smith   }
2405e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2406e34fafa9SBarry Smith   PetscFunctionReturn(0);
2407e34fafa9SBarry Smith }
2408e34fafa9SBarry Smith 
2409682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24100a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2411cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2412cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2413cb5b572fSBarry Smith        MatMult_SeqAIJ,
241497304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
24157c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
24167c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2417db4efbfdSBarry Smith        0,
2418db4efbfdSBarry Smith        0,
2419db4efbfdSBarry Smith        0,
2420db4efbfdSBarry Smith /*10*/ 0,
2421cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2422cb5b572fSBarry Smith        0,
242317ab2063SBarry Smith        MatRelax_SeqAIJ,
242417ab2063SBarry Smith        MatTranspose_SeqAIJ,
242597304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2426cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2427cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2428cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2429cb5b572fSBarry Smith        MatNorm_SeqAIJ,
243097304618SKris Buschelman /*20*/ 0,
2431cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
243217ab2063SBarry Smith        MatCompress_SeqAIJ,
2433cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2434cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
243597304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2436db4efbfdSBarry Smith        0,
2437db4efbfdSBarry Smith        0,
2438db4efbfdSBarry Smith        0,
2439db4efbfdSBarry Smith        0,
244097304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2441db4efbfdSBarry Smith        0,
2442db4efbfdSBarry Smith        0,
24436c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24446c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
244597304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2446cb5b572fSBarry Smith        0,
2447cb5b572fSBarry Smith        0,
2448cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2449cb5b572fSBarry Smith        0,
245097304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2451cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2452cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2453cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2454cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2455985db425SBarry Smith /*45*/ MatGetRowMax_SeqAIJ,
2456cb5b572fSBarry Smith        MatScale_SeqAIJ,
2457cb5b572fSBarry Smith        0,
245879299369SBarry Smith        MatDiagonalSet_SeqAIJ,
24596945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2460521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
24613b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
24623b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
24633b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2464a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
246597304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2466b9617806SBarry Smith        0,
24670513a670SBarry Smith        0,
2468cda55fadSBarry Smith        MatPermute_SeqAIJ,
2469cda55fadSBarry Smith        0,
247097304618SKris Buschelman /*60*/ 0,
2471b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2472b9b97703SBarry Smith        MatView_SeqAIJ,
2473357abbc8SBarry Smith        0,
2474ee4f033dSBarry Smith        0,
247597304618SKris Buschelman /*65*/ 0,
2476ee4f033dSBarry Smith        0,
2477ee4f033dSBarry Smith        0,
2478ee4f033dSBarry Smith        0,
2479ee4f033dSBarry Smith        0,
2480985db425SBarry Smith /*70*/ MatGetRowMaxAbs_SeqAIJ,
2481ee4f033dSBarry Smith        0,
2482ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2483dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2484ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2485dcf5cc72SBarry Smith #else
2486dcf5cc72SBarry Smith        0,
2487dcf5cc72SBarry Smith #endif
2488ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2489b8f8c88eSHong Zhang /*75*/ 0,
249097304618SKris Buschelman        0,
249197304618SKris Buschelman        0,
249297304618SKris Buschelman        0,
249397304618SKris Buschelman        0,
249497304618SKris Buschelman /*80*/ 0,
249597304618SKris Buschelman        0,
249697304618SKris Buschelman        0,
249797304618SKris Buschelman        0,
2498bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2499bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
25001cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25016284ec50SHong Zhang        0,
25026284ec50SHong Zhang        0,
2503bc011b1eSHong Zhang        0,
2504bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
250526be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
250626be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2507d439da42SKris Buschelman        MatPtAP_Basic,
25087ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
25097ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2510bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2511bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2512bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25137ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
25147ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2515609c6c4dSKris Buschelman        0,
2516609c6c4dSKris Buschelman        0,
251787d4246cSBarry Smith        MatConjugate_SeqAIJ,
251887d4246cSBarry Smith        0,
251999cafbc1SBarry Smith /*105*/MatSetValuesRow_SeqAIJ,
252099cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2521f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2522f5edf698SHong Zhang        0,
25232bebee5dSHong Zhang        0,
2524de26497bSBarry Smith /*110*/0,
2525985db425SBarry Smith        0,
25262af78befSBarry Smith        MatGetRowMin_SeqAIJ,
25272af78befSBarry Smith        0,
25282af78befSBarry Smith        MatMissingDiagonal_SeqAIJ
25299e29f15eSvictorle };
253017ab2063SBarry Smith 
2531fb2e594dSBarry Smith EXTERN_C_BEGIN
25324a2ae208SSatish Balay #undef __FUNCT__
25334a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2534be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2535bef8e0ddSBarry Smith {
2536bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
253797f1f81fSBarry Smith   PetscInt   i,nz,n;
2538bef8e0ddSBarry Smith 
2539bef8e0ddSBarry Smith   PetscFunctionBegin;
2540bef8e0ddSBarry Smith 
2541bef8e0ddSBarry Smith   nz = aij->maxnz;
2542d0f46423SBarry Smith   n  = mat->rmap->n;
2543bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2544bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2545bef8e0ddSBarry Smith   }
2546bef8e0ddSBarry Smith   aij->nz = nz;
2547bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2548bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2549bef8e0ddSBarry Smith   }
2550bef8e0ddSBarry Smith 
2551bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2552bef8e0ddSBarry Smith }
2553fb2e594dSBarry Smith EXTERN_C_END
2554bef8e0ddSBarry Smith 
25554a2ae208SSatish Balay #undef __FUNCT__
25564a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2557bef8e0ddSBarry Smith /*@
2558bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2559bef8e0ddSBarry Smith        in the matrix.
2560bef8e0ddSBarry Smith 
2561bef8e0ddSBarry Smith   Input Parameters:
2562bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2563bef8e0ddSBarry Smith -  indices - the column indices
2564bef8e0ddSBarry Smith 
256515091d37SBarry Smith   Level: advanced
256615091d37SBarry Smith 
2567bef8e0ddSBarry Smith   Notes:
2568bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2569bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2570bef8e0ddSBarry Smith   of the MatSetValues() operation.
2571bef8e0ddSBarry Smith 
2572bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2573d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2574bef8e0ddSBarry Smith 
2575bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2576bef8e0ddSBarry Smith 
2577b9617806SBarry Smith     The indices should start with zero, not one.
2578b9617806SBarry Smith 
2579bef8e0ddSBarry Smith @*/
2580be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2581bef8e0ddSBarry Smith {
258297f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2583bef8e0ddSBarry Smith 
2584bef8e0ddSBarry Smith   PetscFunctionBegin;
25854482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
25864482741eSBarry Smith   PetscValidPointer(indices,2);
2587c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2588bef8e0ddSBarry Smith   if (f) {
2589bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2590bef8e0ddSBarry Smith   } else {
2591634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2592bef8e0ddSBarry Smith   }
2593bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2594bef8e0ddSBarry Smith }
2595bef8e0ddSBarry Smith 
2596be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2597be6bf707SBarry Smith 
2598fb2e594dSBarry Smith EXTERN_C_BEGIN
25994a2ae208SSatish Balay #undef __FUNCT__
26004a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2601be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2602be6bf707SBarry Smith {
2603be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26046849ba73SBarry Smith   PetscErrorCode ierr;
2605d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2606be6bf707SBarry Smith 
2607be6bf707SBarry Smith   PetscFunctionBegin;
2608be6bf707SBarry Smith   if (aij->nonew != 1) {
2609512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2610be6bf707SBarry Smith   }
2611be6bf707SBarry Smith 
2612be6bf707SBarry Smith   /* allocate space for values if not already there */
2613be6bf707SBarry Smith   if (!aij->saved_values) {
261487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
26159518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2616be6bf707SBarry Smith   }
2617be6bf707SBarry Smith 
2618be6bf707SBarry Smith   /* copy values over */
261987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2620be6bf707SBarry Smith   PetscFunctionReturn(0);
2621be6bf707SBarry Smith }
2622fb2e594dSBarry Smith EXTERN_C_END
2623be6bf707SBarry Smith 
26244a2ae208SSatish Balay #undef __FUNCT__
2625b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2626be6bf707SBarry Smith /*@
2627be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2628be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2629be6bf707SBarry Smith        nonlinear portion.
2630be6bf707SBarry Smith 
2631be6bf707SBarry Smith    Collect on Mat
2632be6bf707SBarry Smith 
2633be6bf707SBarry Smith   Input Parameters:
26340e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2635be6bf707SBarry Smith 
263615091d37SBarry Smith   Level: advanced
263715091d37SBarry Smith 
2638be6bf707SBarry Smith   Common Usage, with SNESSolve():
2639be6bf707SBarry Smith $    Create Jacobian matrix
2640be6bf707SBarry Smith $    Set linear terms into matrix
2641be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2642be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2643be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2644512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2645be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2646be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2647be6bf707SBarry Smith $    In your Jacobian routine
2648be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2649be6bf707SBarry Smith $      Set nonlinear terms in matrix
2650be6bf707SBarry Smith 
2651be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2652be6bf707SBarry Smith $    // build linear portion of Jacobian
2653512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2654be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2655be6bf707SBarry Smith $    loop over nonlinear iterations
2656be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2657be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2658be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2659be6bf707SBarry Smith $       Solve linear system with Jacobian
2660be6bf707SBarry Smith $    endloop
2661be6bf707SBarry Smith 
2662be6bf707SBarry Smith   Notes:
2663be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2664512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2665be6bf707SBarry Smith     calling this routine.
2666be6bf707SBarry Smith 
26670c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
26680c468ba9SBarry Smith     and does not allocated additional space.
26690c468ba9SBarry Smith 
2670be6bf707SBarry Smith .seealso: MatRetrieveValues()
2671be6bf707SBarry Smith 
2672be6bf707SBarry Smith @*/
2673be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2674be6bf707SBarry Smith {
2675dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2676be6bf707SBarry Smith 
2677be6bf707SBarry Smith   PetscFunctionBegin;
26784482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
267929bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
268029bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2681be6bf707SBarry Smith 
2682c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2683be6bf707SBarry Smith   if (f) {
2684be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2685be6bf707SBarry Smith   } else {
2686634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2687be6bf707SBarry Smith   }
2688be6bf707SBarry Smith   PetscFunctionReturn(0);
2689be6bf707SBarry Smith }
2690be6bf707SBarry Smith 
2691fb2e594dSBarry Smith EXTERN_C_BEGIN
26924a2ae208SSatish Balay #undef __FUNCT__
26934a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2694be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2695be6bf707SBarry Smith {
2696be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26976849ba73SBarry Smith   PetscErrorCode ierr;
2698d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2699be6bf707SBarry Smith 
2700be6bf707SBarry Smith   PetscFunctionBegin;
2701be6bf707SBarry Smith   if (aij->nonew != 1) {
2702512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2703be6bf707SBarry Smith   }
2704be6bf707SBarry Smith   if (!aij->saved_values) {
2705634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2706be6bf707SBarry Smith   }
2707be6bf707SBarry Smith   /* copy values over */
270887828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2709be6bf707SBarry Smith   PetscFunctionReturn(0);
2710be6bf707SBarry Smith }
2711fb2e594dSBarry Smith EXTERN_C_END
2712be6bf707SBarry Smith 
27134a2ae208SSatish Balay #undef __FUNCT__
27144a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2715be6bf707SBarry Smith /*@
2716be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2717be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2718be6bf707SBarry Smith        nonlinear portion.
2719be6bf707SBarry Smith 
2720be6bf707SBarry Smith    Collect on Mat
2721be6bf707SBarry Smith 
2722be6bf707SBarry Smith   Input Parameters:
2723be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2724be6bf707SBarry Smith 
272515091d37SBarry Smith   Level: advanced
272615091d37SBarry Smith 
2727be6bf707SBarry Smith .seealso: MatStoreValues()
2728be6bf707SBarry Smith 
2729be6bf707SBarry Smith @*/
2730be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2731be6bf707SBarry Smith {
2732dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2733be6bf707SBarry Smith 
2734be6bf707SBarry Smith   PetscFunctionBegin;
27354482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
273629bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
273729bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2738be6bf707SBarry Smith 
2739c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2740be6bf707SBarry Smith   if (f) {
2741be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2742be6bf707SBarry Smith   } else {
2743634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2744be6bf707SBarry Smith   }
2745be6bf707SBarry Smith   PetscFunctionReturn(0);
2746be6bf707SBarry Smith }
2747be6bf707SBarry Smith 
2748f83d6046SBarry Smith 
2749be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
27504a2ae208SSatish Balay #undef __FUNCT__
27514a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
275217ab2063SBarry Smith /*@C
2753682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
27540d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
27556e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
275651c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
27572bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
275817ab2063SBarry Smith 
2759db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2760db81eaa0SLois Curfman McInnes 
276117ab2063SBarry Smith    Input Parameters:
2762db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
276317ab2063SBarry Smith .  m - number of rows
276417ab2063SBarry Smith .  n - number of columns
276517ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
276651c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
27672bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
276817ab2063SBarry Smith 
276917ab2063SBarry Smith    Output Parameter:
2770416022c9SBarry Smith .  A - the matrix
277117ab2063SBarry Smith 
2772175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2773175b88e8SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly. This is definitely
2774175b88e8SBarry Smith    true if you plan to use the external direct solvers such as SuperLU, MUMPS or Spooles.
2775175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2776175b88e8SBarry Smith 
2777b259b22eSLois Curfman McInnes    Notes:
277849a6f317SBarry Smith    If nnz is given then nz is ignored
277949a6f317SBarry Smith 
278017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
278117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
27820002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
278344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
278417ab2063SBarry Smith 
278517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2786a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
27873d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
27886da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
278917ab2063SBarry Smith 
2790682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
27914fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2792682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
27936c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
27946c7ebb05SLois Curfman McInnes 
27956c7ebb05SLois Curfman McInnes    Options Database Keys:
2796698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2797698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2798db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2799db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2800db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
280117ab2063SBarry Smith 
2802027ccd11SLois Curfman McInnes    Level: intermediate
2803027ccd11SLois Curfman McInnes 
280436db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
280536db0b34SBarry Smith 
280617ab2063SBarry Smith @*/
2807be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
280817ab2063SBarry Smith {
2809dfbe8321SBarry Smith   PetscErrorCode ierr;
28106945ee14SBarry Smith 
28113a40ed3dSBarry Smith   PetscFunctionBegin;
2812f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2813f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2814273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2815ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2816273d9f13SBarry Smith   PetscFunctionReturn(0);
2817273d9f13SBarry Smith }
2818273d9f13SBarry Smith 
28194a2ae208SSatish Balay #undef __FUNCT__
28204a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2821273d9f13SBarry Smith /*@C
2822273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2823273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2824273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2825273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2826273d9f13SBarry Smith 
2827273d9f13SBarry Smith    Collective on MPI_Comm
2828273d9f13SBarry Smith 
2829273d9f13SBarry Smith    Input Parameters:
283045bfc511SMatthew Knepley +  B - The matrix
2831273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2832273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2833273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2834273d9f13SBarry Smith 
2835273d9f13SBarry Smith    Notes:
283649a6f317SBarry Smith      If nnz is given then nz is ignored
283749a6f317SBarry Smith 
2838273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2839273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2840273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2841273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2842273d9f13SBarry Smith 
2843273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2844273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2845273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2846273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2847273d9f13SBarry Smith 
2848aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2849aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2850aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2851aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2852aa95bbe8SBarry Smith 
2853a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2854a96a251dSBarry Smith    entries or columns indices
2855a96a251dSBarry Smith 
2856273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2857273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2858273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2859273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2860273d9f13SBarry Smith 
2861273d9f13SBarry Smith    Options Database Keys:
2862698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2863698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2864273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2865273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2866273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2867273d9f13SBarry Smith 
2868273d9f13SBarry Smith    Level: intermediate
2869273d9f13SBarry Smith 
2870aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2871273d9f13SBarry Smith 
2872273d9f13SBarry Smith @*/
2873be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2874273d9f13SBarry Smith {
287597f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2876a23d5eceSKris Buschelman 
2877a23d5eceSKris Buschelman   PetscFunctionBegin;
2878a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2879a23d5eceSKris Buschelman   if (f) {
2880a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2881a23d5eceSKris Buschelman   }
2882a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2883a23d5eceSKris Buschelman }
2884a23d5eceSKris Buschelman 
2885a23d5eceSKris Buschelman EXTERN_C_BEGIN
2886a23d5eceSKris Buschelman #undef __FUNCT__
2887a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2888be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2889a23d5eceSKris Buschelman {
2890273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2891a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
28926849ba73SBarry Smith   PetscErrorCode ierr;
289397f1f81fSBarry Smith   PetscInt       i;
2894273d9f13SBarry Smith 
2895273d9f13SBarry Smith   PetscFunctionBegin;
2896d5d45c9bSBarry Smith 
2897a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2898c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2899c461c341SBarry Smith     nz             = 0;
2900c461c341SBarry Smith   }
2901c461c341SBarry Smith 
2902d0f46423SBarry Smith   B->rmap->bs = B->cmap->bs = 1;
2903d0f46423SBarry Smith   ierr = PetscMapSetUp(B->rmap);CHKERRQ(ierr);
2904d0f46423SBarry Smith   ierr = PetscMapSetUp(B->cmap);CHKERRQ(ierr);
2905899cda47SBarry Smith 
2906435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2907435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2908b73539f3SBarry Smith   if (nnz) {
2909d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
291029bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2911d0f46423SBarry 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);
2912b73539f3SBarry Smith     }
2913b73539f3SBarry Smith   }
2914b73539f3SBarry Smith 
2915273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2916273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2917273d9f13SBarry Smith 
2918ab93d7beSBarry Smith   if (!skipallocation) {
29192ee49352SLisandro Dalcin     if (!b->imax) {
2920d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
2921d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
29222ee49352SLisandro Dalcin     }
2923273d9f13SBarry Smith     if (!nnz) {
2924435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2925273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2926d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
2927d0f46423SBarry Smith       nz = nz*B->rmap->n;
2928273d9f13SBarry Smith     } else {
2929273d9f13SBarry Smith       nz = 0;
2930d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2931273d9f13SBarry Smith     }
2932ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2933d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
2934ab93d7beSBarry Smith 
2935273d9f13SBarry Smith     /* allocate the matrix space */
29362ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2937d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
2938d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2939bfeeae90SHong Zhang     b->i[0] = 0;
2940d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
29415da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29425da197adSKris Buschelman     }
2943273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2944e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2945e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2946c461c341SBarry Smith   } else {
2947e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2948e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2949c461c341SBarry Smith   }
2950273d9f13SBarry Smith 
2951273d9f13SBarry Smith   b->nz                = 0;
2952273d9f13SBarry Smith   b->maxnz             = nz;
2953273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2954273d9f13SBarry Smith   PetscFunctionReturn(0);
2955273d9f13SBarry Smith }
2956a23d5eceSKris Buschelman EXTERN_C_END
2957273d9f13SBarry Smith 
2958a1661176SMatthew Knepley #undef  __FUNCT__
2959a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
296058d36128SBarry Smith /*@
2961a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2962a1661176SMatthew Knepley 
2963a1661176SMatthew Knepley    Input Parameters:
2964a1661176SMatthew Knepley +  B - the matrix
2965a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2966a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2967a1661176SMatthew Knepley -  v - optional values in the matrix
2968a1661176SMatthew Knepley 
2969d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
2970d58b2322SMatthew Knepley 
2971a1661176SMatthew Knepley    Level: developer
2972a1661176SMatthew Knepley 
297358d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
297458d36128SBarry Smith 
2975a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2976a1661176SMatthew Knepley 
2977a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
2978a1661176SMatthew Knepley @*/
2979a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
2980a1661176SMatthew Knepley {
2981a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2982a1661176SMatthew Knepley   PetscErrorCode ierr;
2983a1661176SMatthew Knepley 
2984a1661176SMatthew Knepley   PetscFunctionBegin;
2985a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
2986a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2987a1661176SMatthew Knepley   if (f) {
2988a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2989a1661176SMatthew Knepley   }
2990a1661176SMatthew Knepley   PetscFunctionReturn(0);
2991a1661176SMatthew Knepley }
2992a1661176SMatthew Knepley 
2993a1661176SMatthew Knepley EXTERN_C_BEGIN
2994a1661176SMatthew Knepley #undef  __FUNCT__
2995a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
2996b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
2997a1661176SMatthew Knepley {
2998a1661176SMatthew Knepley   PetscInt       i;
2999a1661176SMatthew Knepley   PetscInt       m,n;
3000a1661176SMatthew Knepley   PetscInt       nz;
3001a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3002a1661176SMatthew Knepley   PetscScalar    *values;
3003a1661176SMatthew Knepley   PetscErrorCode ierr;
3004a1661176SMatthew Knepley 
3005a1661176SMatthew Knepley   PetscFunctionBegin;
3006a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3007a1661176SMatthew Knepley 
3008b7940d39SSatish Balay   if (Ii[0]) {
3009b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3010a1661176SMatthew Knepley   }
3011a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3012a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3013b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3014a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
3015a1661176SMatthew Knepley     if (nz < 0) {
3016a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3017a1661176SMatthew Knepley     }
3018a1661176SMatthew Knepley     nnz[i] = nz;
3019a1661176SMatthew Knepley   }
3020a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3021a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3022a1661176SMatthew Knepley 
3023a1661176SMatthew Knepley   if (v) {
3024a1661176SMatthew Knepley     values = (PetscScalar*) v;
3025a1661176SMatthew Knepley   } else {
3026a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3027a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3028a1661176SMatthew Knepley   }
3029a1661176SMatthew Knepley 
3030a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3031b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3032b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3033a1661176SMatthew Knepley   }
3034a1661176SMatthew Knepley 
3035a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3036a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3037a1661176SMatthew Knepley 
3038a1661176SMatthew Knepley   if (!v) {
3039a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3040a1661176SMatthew Knepley   }
3041a1661176SMatthew Knepley   PetscFunctionReturn(0);
3042a1661176SMatthew Knepley }
3043a1661176SMatthew Knepley EXTERN_C_END
3044a1661176SMatthew Knepley 
3045170fe5c8SBarry Smith #include "src/mat/impls/dense/seq/dense.h"
30461de00fd4SBarry Smith #include "src/inline/axpy.h"
3047170fe5c8SBarry Smith 
3048170fe5c8SBarry Smith #undef __FUNCT__
3049170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3050170fe5c8SBarry Smith /*
3051170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3052170fe5c8SBarry Smith 
3053170fe5c8SBarry Smith                n                       p                          p
3054170fe5c8SBarry Smith         (              )       (              )         (                  )
3055170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3056170fe5c8SBarry Smith         (              )       (              )         (                  )
3057170fe5c8SBarry Smith 
3058170fe5c8SBarry Smith */
3059170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3060170fe5c8SBarry Smith {
3061170fe5c8SBarry Smith   PetscErrorCode     ierr;
3062170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3063170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3064170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
30651de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3066170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3067170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3068170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3069170fe5c8SBarry Smith 
3070170fe5c8SBarry Smith   PetscFunctionBegin;
3071d0f46423SBarry Smith   m = A->rmap->n;
3072d0f46423SBarry Smith   n = A->cmap->n;
3073d0f46423SBarry Smith   p = B->cmap->n;
3074170fe5c8SBarry Smith   a = sub_a->v;
3075170fe5c8SBarry Smith   b = sub_b->a;
3076170fe5c8SBarry Smith   c = sub_c->v;
3077170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3078170fe5c8SBarry Smith 
3079170fe5c8SBarry Smith   ii  = sub_b->i;
3080170fe5c8SBarry Smith   idx = sub_b->j;
3081170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3082170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3083170fe5c8SBarry Smith     while (q-->0) {
3084170fe5c8SBarry Smith       c_q = c + m*(*idx);
3085170fe5c8SBarry Smith       a_q = a + m*i;
30861de00fd4SBarry Smith       APXY(c_q,*b,a_q,m);
3087170fe5c8SBarry Smith       idx++;
3088170fe5c8SBarry Smith       b++;
3089170fe5c8SBarry Smith     }
3090170fe5c8SBarry Smith   }
3091170fe5c8SBarry Smith   PetscFunctionReturn(0);
3092170fe5c8SBarry Smith }
3093170fe5c8SBarry Smith 
3094170fe5c8SBarry Smith #undef __FUNCT__
3095170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3096170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3097170fe5c8SBarry Smith {
3098170fe5c8SBarry Smith   PetscErrorCode ierr;
3099d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3100170fe5c8SBarry Smith   Mat            Cmat;
3101170fe5c8SBarry Smith 
3102170fe5c8SBarry Smith   PetscFunctionBegin;
3103d0f46423SBarry 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);
310439804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3105170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3106170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3107170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3108170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3109170fe5c8SBarry Smith   *C = Cmat;
3110170fe5c8SBarry Smith   PetscFunctionReturn(0);
3111170fe5c8SBarry Smith }
3112170fe5c8SBarry Smith 
3113170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3114170fe5c8SBarry Smith #undef __FUNCT__
3115170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3116170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3117170fe5c8SBarry Smith {
3118170fe5c8SBarry Smith   PetscErrorCode ierr;
3119170fe5c8SBarry Smith 
3120170fe5c8SBarry Smith   PetscFunctionBegin;
3121170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3122170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3123170fe5c8SBarry Smith   }
3124170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3125170fe5c8SBarry Smith   PetscFunctionReturn(0);
3126170fe5c8SBarry Smith }
3127170fe5c8SBarry Smith 
3128170fe5c8SBarry Smith 
31290bad9183SKris Buschelman /*MC
3130fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31310bad9183SKris Buschelman    based on compressed sparse row format.
31320bad9183SKris Buschelman 
31330bad9183SKris Buschelman    Options Database Keys:
31340bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31350bad9183SKris Buschelman 
31360bad9183SKris Buschelman   Level: beginner
31370bad9183SKris Buschelman 
3138f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31390bad9183SKris Buschelman M*/
31400bad9183SKris Buschelman 
3141a6175056SHong Zhang EXTERN_C_BEGIN
314217667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3143b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3144db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3145611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
31465c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3147611f576cSBarry Smith #endif
3148611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
31495c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3150611f576cSBarry Smith #endif
3151611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
31525c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3153611f576cSBarry Smith #endif
3154eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3155eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3156eb3b5408SSatish Balay #endif
3157*719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3158*719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3159*719d5645SBarry Smith #endif
316017667f90SBarry Smith EXTERN_C_END
316117667f90SBarry Smith 
3162b24902e0SBarry Smith 
316317667f90SBarry Smith EXTERN_C_BEGIN
31644a2ae208SSatish Balay #undef __FUNCT__
31654a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3166be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3167273d9f13SBarry Smith {
3168273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3169dfbe8321SBarry Smith   PetscErrorCode ierr;
317038baddfdSBarry Smith   PetscMPIInt    size;
3171273d9f13SBarry Smith 
3172273d9f13SBarry Smith   PetscFunctionBegin;
31737adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3174273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3175273d9f13SBarry Smith 
317638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3177b0a32e0cSBarry Smith   B->data             = (void*)b;
3178549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
317990f02eecSBarry Smith   B->mapping          = 0;
3180416022c9SBarry Smith   b->row              = 0;
3181416022c9SBarry Smith   b->col              = 0;
318282bf6240SBarry Smith   b->icol             = 0;
3183b810aeb4SBarry Smith   b->reallocs         = 0;
318436db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3185f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3186416022c9SBarry Smith   b->nonew             = 0;
3187416022c9SBarry Smith   b->diag              = 0;
3188416022c9SBarry Smith   b->solve_work        = 0;
31892a1b7f2aSHong Zhang   B->spptr             = 0;
3190be6bf707SBarry Smith   b->saved_values      = 0;
3191d7f994e1SBarry Smith   b->idiag             = 0;
319271f1c65dSBarry Smith   b->mdiag             = 0;
319371f1c65dSBarry Smith   b->ssor_work         = 0;
319471f1c65dSBarry Smith   b->omega             = 1.0;
319571f1c65dSBarry Smith   b->fshift            = 0.0;
319671f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3197f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
3198a30b2313SHong Zhang   b->xtoy              = 0;
3199a30b2313SHong Zhang   b->XtoY              = 0;
320073e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3201d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3202d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3203d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3204d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
320588e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
320617ab2063SBarry Smith 
320735d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3208*719d5645SBarry Smith #if defined(PETSC_HAVE_ESSL)
3209*719d5645SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_essl_C",
3210*719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3211*719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3212*719d5645SBarry Smith #endif
3213611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32145c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_C",
32155c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
32165c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3217611f576cSBarry Smith #endif
3218611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32195c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_spooles_C",
32205c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
32215c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3222611f576cSBarry Smith #endif
3223611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
32245c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_mumps_C",
32255c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
32265c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3227611f576cSBarry Smith #endif
3228eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3229eb3b5408SSatish Balay     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_umfpack_C",
3230eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3231eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3232eb3b5408SSatish Balay #endif
3233*719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3234*719d5645SBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_lusol_C",
3235*719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3236*719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3237*719d5645SBarry Smith #endif
3238b24902e0SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_petsc_C",
3239b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3240b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3241db4efbfdSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_seqaij_petsc_C",
3242db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3243db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3244f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3245bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3246bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3247f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3248be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3249bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3250f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3251be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3252bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3253a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3254a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3255a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
325685fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
325785fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
325885fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3259ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3260ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3261ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
326217667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
326317667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
326417667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
32655fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
32665fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
32675fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
32681cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
32691cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
32701cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3271a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3272a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3273a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3274a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3275a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3276a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
327705b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
327805b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
327905b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3280170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3281170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3282170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3283170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3284170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3285170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3286170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3287170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3288170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
32894846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
329017667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
32913a40ed3dSBarry Smith   PetscFunctionReturn(0);
329217ab2063SBarry Smith }
3293273d9f13SBarry Smith EXTERN_C_END
329417ab2063SBarry Smith 
32954a2ae208SSatish Balay #undef __FUNCT__
3296b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3297b24902e0SBarry Smith /*
3298b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3299b24902e0SBarry Smith */
3300*719d5645SBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues)
330117ab2063SBarry Smith {
3302416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
33036849ba73SBarry Smith   PetscErrorCode ierr;
3304d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
330517ab2063SBarry Smith 
33063a40ed3dSBarry Smith   PetscFunctionBegin;
33071d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
33081d5dac46SHong Zhang 
3309273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3310273d9f13SBarry Smith 
3311416022c9SBarry Smith   C->factor           = A->factor;
33126ad4291fSHong Zhang 
3313416022c9SBarry Smith   c->row            = 0;
3314416022c9SBarry Smith   c->col            = 0;
331582bf6240SBarry Smith   c->icol           = 0;
33166ad4291fSHong Zhang   c->reallocs       = 0;
331717ab2063SBarry Smith 
33186ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
331917ab2063SBarry Smith 
3320d0f46423SBarry Smith   C->rmap->bs = C->cmap->bs = 1;
3321d0f46423SBarry Smith   ierr = PetscMapSetUp(C->rmap);CHKERRQ(ierr);
3322d0f46423SBarry Smith   ierr = PetscMapSetUp(C->cmap);CHKERRQ(ierr);
3323eec197d1SBarry Smith 
332433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
33259518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
332617ab2063SBarry Smith   for (i=0; i<m; i++) {
3327416022c9SBarry Smith     c->imax[i] = a->imax[i];
3328416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
332917ab2063SBarry Smith   }
333017ab2063SBarry Smith 
333117ab2063SBarry Smith   /* allocate the matrix space */
3332a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
33339518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3334f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
333597f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
333617ab2063SBarry Smith   if (m > 0) {
333797f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3338be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
3339bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3340be6bf707SBarry Smith     } else {
3341bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
334217ab2063SBarry Smith     }
334308480c60SBarry Smith   }
334417ab2063SBarry Smith 
33456ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3346416022c9SBarry Smith   c->roworiented       = a->roworiented;
3347416022c9SBarry Smith   c->nonew             = a->nonew;
3348416022c9SBarry Smith   if (a->diag) {
334997f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
335052e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
335117ab2063SBarry Smith     for (i=0; i<m; i++) {
3352416022c9SBarry Smith       c->diag[i] = a->diag[i];
335317ab2063SBarry Smith     }
33543a40ed3dSBarry Smith   } else c->diag           = 0;
33556ad4291fSHong Zhang   c->solve_work            = 0;
33566ad4291fSHong Zhang   c->saved_values          = 0;
33576ad4291fSHong Zhang   c->idiag                 = 0;
335871f1c65dSBarry Smith   c->ssor_work             = 0;
33596ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
3360e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3361e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
33626ad4291fSHong Zhang   c->xtoy                  = 0;
33636ad4291fSHong Zhang   c->XtoY                  = 0;
33646ad4291fSHong Zhang 
3365416022c9SBarry Smith   c->nz                 = a->nz;
3366416022c9SBarry Smith   c->maxnz              = a->maxnz;
3367273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3368754ec7b1SSatish Balay 
33696ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
33706ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
33716ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
33726ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
33736ad4291fSHong Zhang     i = a->compressedrow.nrows;
33746ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
33756ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
33766ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
33776ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
337827ea64f8SHong Zhang   } else {
337927ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
338027ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
338127ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
33826ad4291fSHong Zhang   }
338388e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
33844846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
33854846f1f5SKris Buschelman 
33867adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
33873a40ed3dSBarry Smith   PetscFunctionReturn(0);
338817ab2063SBarry Smith }
338917ab2063SBarry Smith 
33904a2ae208SSatish Balay #undef __FUNCT__
3391b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3392b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3393b24902e0SBarry Smith {
3394b24902e0SBarry Smith   PetscErrorCode ierr;
3395d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
3396b24902e0SBarry Smith 
3397b24902e0SBarry Smith   PetscFunctionBegin;
3398b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3399b24902e0SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
3400b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3401*719d5645SBarry Smith   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues);CHKERRQ(ierr);
3402b24902e0SBarry Smith   PetscFunctionReturn(0);
3403b24902e0SBarry Smith }
3404b24902e0SBarry Smith 
3405b24902e0SBarry Smith #undef __FUNCT__
34064a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3407a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
340817ab2063SBarry Smith {
3409416022c9SBarry Smith   Mat_SeqAIJ     *a;
3410416022c9SBarry Smith   Mat            B;
34116849ba73SBarry Smith   PetscErrorCode ierr;
34123c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
341338baddfdSBarry Smith   int            fd;
341438baddfdSBarry Smith   PetscMPIInt    size;
3415bcd2baecSBarry Smith   MPI_Comm       comm;
341617ab2063SBarry Smith 
34173a40ed3dSBarry Smith   PetscFunctionBegin;
3418e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3419d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
342029bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3421b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
34220752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3423552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
342417ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
342517ab2063SBarry Smith 
3426d64ed03dSBarry Smith   if (nz < 0) {
342729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3428d64ed03dSBarry Smith   }
3429d64ed03dSBarry Smith 
343017ab2063SBarry Smith   /* read in row lengths */
343197f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
34320752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
343317ab2063SBarry Smith 
34343c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
34353c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
34363c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
34373c601197SSatish Balay 
343817ab2063SBarry Smith   /* create our matrix */
3439f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3440f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3441b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3442ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3443416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
344417ab2063SBarry Smith 
34450752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
344617ab2063SBarry Smith 
344717ab2063SBarry Smith   /* read in nonzero values */
34480752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
344917ab2063SBarry Smith 
345017ab2063SBarry Smith   /* set matrix "i" values */
3451efb16452SHong Zhang   a->i[0] = 0;
345217ab2063SBarry Smith   for (i=1; i<= M; i++) {
3453416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3454416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
345517ab2063SBarry Smith   }
3456606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
345717ab2063SBarry Smith 
34586d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
34596d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3460b3a1e11cSKris Buschelman   *A = B;
34613a40ed3dSBarry Smith   PetscFunctionReturn(0);
346217ab2063SBarry Smith }
346317ab2063SBarry Smith 
34644a2ae208SSatish Balay #undef __FUNCT__
3465b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3466dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
34677264ac53SSatish Balay {
34687264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3469dfbe8321SBarry Smith   PetscErrorCode ierr;
34707264ac53SSatish Balay 
34713a40ed3dSBarry Smith   PetscFunctionBegin;
3472bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3473d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3474ca44d042SBarry Smith     *flg = PETSC_FALSE;
3475ca44d042SBarry Smith     PetscFunctionReturn(0);
3476bcd2baecSBarry Smith   }
34777264ac53SSatish Balay 
34787264ac53SSatish Balay   /* if the a->i are the same */
3479d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3480abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
34817264ac53SSatish Balay 
34827264ac53SSatish Balay   /* if a->j are the same */
348397f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3484abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3485bcd2baecSBarry Smith 
3486bcd2baecSBarry Smith   /* if a->a are the same */
348787828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
34880f5bd95cSBarry Smith 
34893a40ed3dSBarry Smith   PetscFunctionReturn(0);
34907264ac53SSatish Balay 
34917264ac53SSatish Balay }
349236db0b34SBarry Smith 
34934a2ae208SSatish Balay #undef __FUNCT__
34944a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
349505869f15SSatish Balay /*@
349636db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
349736db0b34SBarry Smith               provided by the user.
349836db0b34SBarry Smith 
3499c75a6043SHong Zhang       Collective on MPI_Comm
350036db0b34SBarry Smith 
350136db0b34SBarry Smith    Input Parameters:
350236db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
350336db0b34SBarry Smith .   m - number of rows
350436db0b34SBarry Smith .   n - number of columns
350536db0b34SBarry Smith .   i - row indices
350636db0b34SBarry Smith .   j - column indices
350736db0b34SBarry Smith -   a - matrix values
350836db0b34SBarry Smith 
350936db0b34SBarry Smith    Output Parameter:
351036db0b34SBarry Smith .   mat - the matrix
351136db0b34SBarry Smith 
351236db0b34SBarry Smith    Level: intermediate
351336db0b34SBarry Smith 
351436db0b34SBarry Smith    Notes:
35150551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
351636db0b34SBarry Smith     once the matrix is destroyed
351736db0b34SBarry Smith 
351836db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
351936db0b34SBarry Smith 
3520bfeeae90SHong Zhang        The i and j indices are 0 based
352136db0b34SBarry Smith 
3522a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3523a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3524a4552177SSatish Balay     as shown:
3525a4552177SSatish Balay 
3526a4552177SSatish Balay         1 0 0
3527a4552177SSatish Balay         2 0 3
3528a4552177SSatish Balay         4 5 6
3529a4552177SSatish Balay 
3530a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
35319985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3532a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3533a4552177SSatish Balay 
35349985e31cSBarry Smith 
35352fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
353636db0b34SBarry Smith 
353736db0b34SBarry Smith @*/
3538a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
353936db0b34SBarry Smith {
3540dfbe8321SBarry Smith   PetscErrorCode ierr;
3541cbcfb4deSHong Zhang   PetscInt       ii;
354236db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3543cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3544cbcfb4deSHong Zhang   PetscInt       jj;
3545cbcfb4deSHong Zhang #endif
354636db0b34SBarry Smith 
354736db0b34SBarry Smith   PetscFunctionBegin;
3548a96a251dSBarry Smith   if (i[0]) {
3549634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
355036db0b34SBarry Smith   }
3551f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3552f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3553ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3554ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3555ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3556ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3557ab93d7beSBarry Smith 
355836db0b34SBarry Smith   aij->i = i;
355936db0b34SBarry Smith   aij->j = j;
356036db0b34SBarry Smith   aij->a = a;
356136db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
356236db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3563e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3564e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
356536db0b34SBarry Smith 
356636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
356736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
35682515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
356979a5c55eSBarry 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]);
35709985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
35719985e31cSBarry 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);
35729985e31cSBarry 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);
35739985e31cSBarry Smith     }
357436db0b34SBarry Smith #endif
357536db0b34SBarry Smith   }
35762515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
357736db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
357879a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
357979a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
358036db0b34SBarry Smith   }
358136db0b34SBarry Smith #endif
358236db0b34SBarry Smith 
3583b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3584b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
358536db0b34SBarry Smith   PetscFunctionReturn(0);
358636db0b34SBarry Smith }
358736db0b34SBarry Smith 
3588cc8ba8e1SBarry Smith #undef __FUNCT__
3589ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3590dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3591cc8ba8e1SBarry Smith {
3592dfbe8321SBarry Smith   PetscErrorCode ierr;
3593cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
359436db0b34SBarry Smith 
3595cc8ba8e1SBarry Smith   PetscFunctionBegin;
35968ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3597cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3598cc8ba8e1SBarry Smith     a->coloring = coloring;
359912c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
360097f1f81fSBarry Smith     PetscInt             i,*larray;
360112c595b3SBarry Smith     ISColoring      ocoloring;
360208b6dcc0SBarry Smith     ISColoringValue *colors;
360312c595b3SBarry Smith 
360412c595b3SBarry Smith     /* set coloring for diagonal portion */
3605d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3606d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
360712c595b3SBarry Smith       larray[i] = i;
360812c595b3SBarry Smith     }
3609d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3610d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3611d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
361212c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
361312c595b3SBarry Smith     }
361412c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3615d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
361612c595b3SBarry Smith     a->coloring = ocoloring;
361712c595b3SBarry Smith   }
3618cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3619cc8ba8e1SBarry Smith }
3620cc8ba8e1SBarry Smith 
3621dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3622ee4f033dSBarry Smith EXTERN_C_BEGIN
362329c1e371SBarry Smith #include "adic/ad_utils.h"
3624ee4f033dSBarry Smith EXTERN_C_END
3625cc8ba8e1SBarry Smith 
3626cc8ba8e1SBarry Smith #undef __FUNCT__
3627ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3628dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3629cc8ba8e1SBarry Smith {
3630cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3631d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
36324440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
363308b6dcc0SBarry Smith   ISColoringValue *color;
3634cc8ba8e1SBarry Smith 
3635cc8ba8e1SBarry Smith   PetscFunctionBegin;
3636e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
36374440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3638cc8ba8e1SBarry Smith   color = a->coloring->colors;
3639cc8ba8e1SBarry Smith   /* loop over rows */
3640cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3641cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3642cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3643cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3644cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3645cc8ba8e1SBarry Smith     }
36464440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3647ee4f033dSBarry Smith   }
3648ee4f033dSBarry Smith   PetscFunctionReturn(0);
3649ee4f033dSBarry Smith }
3650ee4f033dSBarry Smith #endif
3651ee4f033dSBarry Smith 
3652ee4f033dSBarry Smith #undef __FUNCT__
3653ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
365497f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3655ee4f033dSBarry Smith {
3656ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3657d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
365854f21887SBarry Smith   MatScalar       *v = a->a;
365954f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
366008b6dcc0SBarry Smith   ISColoringValue *color;
3661ee4f033dSBarry Smith 
3662ee4f033dSBarry Smith   PetscFunctionBegin;
3663e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3664ee4f033dSBarry Smith   color = a->coloring->colors;
3665ee4f033dSBarry Smith   /* loop over rows */
3666ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3667ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3668ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3669ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3670ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3671ee4f033dSBarry Smith     }
3672ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3673cc8ba8e1SBarry Smith   }
3674cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3675cc8ba8e1SBarry Smith }
367636db0b34SBarry Smith 
367781824310SBarry Smith /*
367881824310SBarry Smith     Special version for direct calls from Fortran
367981824310SBarry Smith */
368081824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
368181824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
368281824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
368381824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
368481824310SBarry Smith #endif
368581824310SBarry Smith 
368681824310SBarry Smith /* Change these macros so can be used in void function */
368781824310SBarry Smith #undef CHKERRQ
36887adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
368981824310SBarry Smith #undef SETERRQ2
36907adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
369181824310SBarry Smith 
369281824310SBarry Smith EXTERN_C_BEGIN
369381824310SBarry Smith #undef __FUNCT__
369481824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
36951f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
369681824310SBarry Smith {
369781824310SBarry Smith   Mat            A = *AA;
369881824310SBarry Smith   PetscInt       m = *mm, n = *nn;
369981824310SBarry Smith   InsertMode     is = *isis;
370081824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
370181824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
370281824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
370381824310SBarry Smith   PetscErrorCode ierr;
370481824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
370554f21887SBarry Smith   MatScalar      *ap,value,*aa;
3706edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
370781824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
370881824310SBarry Smith 
370981824310SBarry Smith   PetscFunctionBegin;
371081824310SBarry Smith   MatPreallocated(A);
371181824310SBarry Smith   imax = a->imax;
371281824310SBarry Smith   ai = a->i;
371381824310SBarry Smith   ailen = a->ilen;
371481824310SBarry Smith   aj = a->j;
371581824310SBarry Smith   aa = a->a;
371681824310SBarry Smith 
371781824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
371881824310SBarry Smith     row  = im[k];
371981824310SBarry Smith     if (row < 0) continue;
372081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3721d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
372281824310SBarry Smith #endif
372381824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
372481824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
372581824310SBarry Smith     low  = 0;
372681824310SBarry Smith     high = nrow;
372781824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
372881824310SBarry Smith       if (in[l] < 0) continue;
372981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3730d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
373181824310SBarry Smith #endif
373281824310SBarry Smith       col = in[l];
373381824310SBarry Smith       if (roworiented) {
373481824310SBarry Smith         value = v[l + k*n];
373581824310SBarry Smith       } else {
373681824310SBarry Smith         value = v[k + l*m];
373781824310SBarry Smith       }
373881824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
373981824310SBarry Smith 
374081824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
374181824310SBarry Smith       lastcol = col;
374281824310SBarry Smith       while (high-low > 5) {
374381824310SBarry Smith         t = (low+high)/2;
374481824310SBarry Smith         if (rp[t] > col) high = t;
374581824310SBarry Smith         else             low  = t;
374681824310SBarry Smith       }
374781824310SBarry Smith       for (i=low; i<high; i++) {
374881824310SBarry Smith         if (rp[i] > col) break;
374981824310SBarry Smith         if (rp[i] == col) {
375081824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
375181824310SBarry Smith           else                  ap[i] = value;
375281824310SBarry Smith           goto noinsert;
375381824310SBarry Smith         }
375481824310SBarry Smith       }
375581824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
375681824310SBarry Smith       if (nonew == 1) goto noinsert;
37577adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3758d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
375981824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
376081824310SBarry Smith       /* shift up all the later entries in this row */
376181824310SBarry Smith       for (ii=N; ii>=i; ii--) {
376281824310SBarry Smith         rp[ii+1] = rp[ii];
376381824310SBarry Smith         ap[ii+1] = ap[ii];
376481824310SBarry Smith       }
376581824310SBarry Smith       rp[i] = col;
376681824310SBarry Smith       ap[i] = value;
376781824310SBarry Smith       noinsert:;
376881824310SBarry Smith       low = i + 1;
376981824310SBarry Smith     }
377081824310SBarry Smith     ailen[row] = nrow;
377181824310SBarry Smith   }
377281824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
377381824310SBarry Smith   PetscFunctionReturnVoid();
377481824310SBarry Smith }
377581824310SBarry Smith EXTERN_C_END
3776