xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 5c9eb25f753630cfd361293e05e7358a00a954ac)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
89e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
9f5eb4b81SSatish Balay #include "src/inline/spops.h"
108d195f9aSBarry Smith #include "src/inline/dot.h"
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
134a2ae208SSatish Balay #undef __FUNCT__
1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1579299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1679299369SBarry Smith {
1779299369SBarry Smith   PetscErrorCode ierr;
1879299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
19899cda47SBarry Smith   PetscInt       i,*diag, m = Y->rmap.n;
2054f21887SBarry Smith   MatScalar      *aa = aij->a;
2154f21887SBarry Smith   PetscScalar    *v;
2209f38230SBarry Smith   PetscTruth     missing;
2379299369SBarry Smith 
2479299369SBarry Smith   PetscFunctionBegin;
2509f38230SBarry Smith   if (Y->assembled) {
2609f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2709f38230SBarry Smith     if (!missing) {
2879299369SBarry Smith       diag = aij->diag;
2979299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3079299369SBarry Smith       if (is == INSERT_VALUES) {
3179299369SBarry Smith 	for (i=0; i<m; i++) {
3279299369SBarry Smith 	  aa[diag[i]] = v[i];
3379299369SBarry Smith 	}
3479299369SBarry Smith       } else {
3579299369SBarry Smith 	for (i=0; i<m; i++) {
3679299369SBarry Smith 	  aa[diag[i]] += v[i];
3779299369SBarry Smith 	}
3879299369SBarry Smith       }
3979299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4079299369SBarry Smith       PetscFunctionReturn(0);
4179299369SBarry Smith     }
4209f38230SBarry Smith   }
4309f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4409f38230SBarry Smith   PetscFunctionReturn(0);
4509f38230SBarry Smith }
4679299369SBarry Smith 
4779299369SBarry Smith #undef __FUNCT__
484a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
498f7157efSSatish Balay PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5017ab2063SBarry Smith {
51416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
52dfbe8321SBarry Smith   PetscErrorCode ierr;
5397f1f81fSBarry Smith   PetscInt       i,ishift;
5417ab2063SBarry Smith 
553a40ed3dSBarry Smith   PetscFunctionBegin;
56899cda47SBarry Smith   *m     = A->rmap.n;
573a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
58bfeeae90SHong Zhang   ishift = 0;
5953e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
60899cda47SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
61bfeeae90SHong Zhang   } else if (oshift == 1) {
62899cda47SBarry Smith     PetscInt nz = a->i[A->rmap.n];
633b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
64899cda47SBarry Smith     ierr = PetscMalloc((A->rmap.n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
65ecc77c7aSBarry Smith     for (i=0; i<A->rmap.n+1; i++) (*ia)[i] = a->i[i] + 1;
66ecc77c7aSBarry Smith     if (ja) {
6797f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
683b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
69ecc77c7aSBarry Smith     }
706945ee14SBarry Smith   } else {
71ecc77c7aSBarry Smith     *ia = a->i;
72ecc77c7aSBarry Smith     if (ja) *ja = a->j;
73a2ce50c7SBarry Smith   }
743a40ed3dSBarry Smith   PetscFunctionReturn(0);
75a2744918SBarry Smith }
76a2744918SBarry Smith 
774a2ae208SSatish Balay #undef __FUNCT__
784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
798f7157efSSatish Balay PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
806945ee14SBarry Smith {
81dfbe8321SBarry Smith   PetscErrorCode ierr;
826945ee14SBarry Smith 
833a40ed3dSBarry Smith   PetscFunctionBegin;
843a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
85bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
86606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
87ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
88bcd2baecSBarry Smith   }
893a40ed3dSBarry Smith   PetscFunctionReturn(0);
9017ab2063SBarry Smith }
9117ab2063SBarry Smith 
924a2ae208SSatish Balay #undef __FUNCT__
934a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
948f7157efSSatish Balay PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
953b2fbd54SBarry Smith {
963b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
97dfbe8321SBarry Smith   PetscErrorCode ierr;
98899cda47SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap.n,m = A->rmap.n;
9997f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1003b2fbd54SBarry Smith 
1013a40ed3dSBarry Smith   PetscFunctionBegin;
102899cda47SBarry Smith   *nn = n;
1033a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1043b2fbd54SBarry Smith   if (symmetric) {
105899cda47SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1063b2fbd54SBarry Smith   } else {
10797f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1113b2fbd54SBarry Smith     jj = a->j;
1123b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
113bfeeae90SHong Zhang       collengths[jj[i]]++;
1143b2fbd54SBarry Smith     }
1153b2fbd54SBarry Smith     cia[0] = oshift;
1163b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1173b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1183b2fbd54SBarry Smith     }
11997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1203b2fbd54SBarry Smith     jj   = a->j;
121a93ec695SBarry Smith     for (row=0; row<m; row++) {
122a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
123a93ec695SBarry Smith       for (i=0; i<mr; i++) {
124bfeeae90SHong Zhang         col = *jj++;
1253b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1263b2fbd54SBarry Smith       }
1273b2fbd54SBarry Smith     }
128606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1293b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1303b2fbd54SBarry Smith   }
1313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1323b2fbd54SBarry Smith }
1333b2fbd54SBarry Smith 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1368f7157efSSatish Balay PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1373b2fbd54SBarry Smith {
138dfbe8321SBarry Smith   PetscErrorCode ierr;
139606d414cSSatish Balay 
1403a40ed3dSBarry Smith   PetscFunctionBegin;
1413a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1423b2fbd54SBarry Smith 
143606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
144606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1453b2fbd54SBarry Smith 
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1473b2fbd54SBarry Smith }
1483b2fbd54SBarry Smith 
14987d4246cSBarry Smith #undef __FUNCT__
15087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15287d4246cSBarry Smith {
15387d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15487d4246cSBarry Smith   PetscInt       *ai = a->i;
15587d4246cSBarry Smith   PetscErrorCode ierr;
15687d4246cSBarry Smith 
15787d4246cSBarry Smith   PetscFunctionBegin;
15887d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
15987d4246cSBarry Smith   PetscFunctionReturn(0);
16087d4246cSBarry Smith }
16187d4246cSBarry Smith 
162227d817aSBarry Smith #define CHUNKSIZE   15
16317ab2063SBarry Smith 
1644a2ae208SSatish Balay #undef __FUNCT__
1654a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16717ab2063SBarry Smith {
168416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
169e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
17097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1716849ba73SBarry Smith   PetscErrorCode ierr;
172e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17354f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
174edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
175273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17617ab2063SBarry Smith 
1773a40ed3dSBarry Smith   PetscFunctionBegin;
17817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
179416022c9SBarry Smith     row  = im[k];
1805ef9f2a5SBarry Smith     if (row < 0) continue;
1812515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
182899cda47SBarry Smith     if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1);
1833b2fbd54SBarry Smith #endif
184bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
186416022c9SBarry Smith     low  = 0;
187c71e6ed7SBarry Smith     high = nrow;
18817ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1895ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1902515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
191899cda47SBarry Smith       if (in[l] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap.n-1);
1923b2fbd54SBarry Smith #endif
193bfeeae90SHong Zhang       col = in[l];
1944b0e389bSBarry Smith       if (roworiented) {
1955ef9f2a5SBarry Smith         value = v[l + k*n];
196bef8e0ddSBarry Smith       } else {
1974b0e389bSBarry Smith         value = v[k + l*m];
1984b0e389bSBarry Smith       }
199abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
20036db0b34SBarry Smith 
2017cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
202e2ee6c50SBarry Smith       lastcol = col;
203416022c9SBarry Smith       while (high-low > 5) {
204416022c9SBarry Smith         t = (low+high)/2;
205416022c9SBarry Smith         if (rp[t] > col) high = t;
206416022c9SBarry Smith         else             low  = t;
20717ab2063SBarry Smith       }
208416022c9SBarry Smith       for (i=low; i<high; i++) {
20917ab2063SBarry Smith         if (rp[i] > col) break;
21017ab2063SBarry Smith         if (rp[i] == col) {
211416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
21217ab2063SBarry Smith           else                  ap[i] = value;
213e44c0bd4SBarry Smith           low = i + 1;
21417ab2063SBarry Smith           goto noinsert;
21517ab2063SBarry Smith         }
21617ab2063SBarry Smith       }
217abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
218c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
219cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
220421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
221c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
222416022c9SBarry Smith       /* shift up all the later entries in this row */
223416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
22417ab2063SBarry Smith         rp[ii+1] = rp[ii];
22517ab2063SBarry Smith         ap[ii+1] = ap[ii];
22617ab2063SBarry Smith       }
22717ab2063SBarry Smith       rp[i] = col;
22817ab2063SBarry Smith       ap[i] = value;
229416022c9SBarry Smith       low   = i + 1;
230e44c0bd4SBarry Smith       noinsert:;
23117ab2063SBarry Smith     }
23217ab2063SBarry Smith     ailen[row] = nrow;
23317ab2063SBarry Smith   }
23488e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2353a40ed3dSBarry Smith   PetscFunctionReturn(0);
23617ab2063SBarry Smith }
23717ab2063SBarry Smith 
23881824310SBarry Smith 
2394a2ae208SSatish Balay #undef __FUNCT__
2404a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
241a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2427eb43aa7SLois Curfman McInnes {
2437eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
24497f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
24597f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
24654f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2477eb43aa7SLois Curfman McInnes 
2483a40ed3dSBarry Smith   PetscFunctionBegin;
2497eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2507eb43aa7SLois Curfman McInnes     row  = im[k];
25197e567efSBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
252899cda47SBarry Smith     if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1);
253bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2547eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2557eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
25697e567efSBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
257899cda47SBarry 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);
258bfeeae90SHong Zhang       col = in[l] ;
2597eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2607eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2617eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2627eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2637eb43aa7SLois Curfman McInnes         else             low  = t;
2647eb43aa7SLois Curfman McInnes       }
2657eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2667eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2677eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
268b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2697eb43aa7SLois Curfman McInnes           goto finished;
2707eb43aa7SLois Curfman McInnes         }
2717eb43aa7SLois Curfman McInnes       }
27297e567efSBarry Smith       *v++ = 0.0;
2737eb43aa7SLois Curfman McInnes       finished:;
2747eb43aa7SLois Curfman McInnes     }
2757eb43aa7SLois Curfman McInnes   }
2763a40ed3dSBarry Smith   PetscFunctionReturn(0);
2777eb43aa7SLois Curfman McInnes }
2787eb43aa7SLois Curfman McInnes 
27917ab2063SBarry Smith 
2804a2ae208SSatish Balay #undef __FUNCT__
2814a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
282dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28317ab2063SBarry Smith {
284416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2856849ba73SBarry Smith   PetscErrorCode ierr;
2866f69ff64SBarry Smith   PetscInt       i,*col_lens;
2876f69ff64SBarry Smith   int            fd;
28817ab2063SBarry Smith 
2893a40ed3dSBarry Smith   PetscFunctionBegin;
290b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
291899cda47SBarry Smith   ierr = PetscMalloc((4+A->rmap.n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
292552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
293899cda47SBarry Smith   col_lens[1] = A->rmap.n;
294899cda47SBarry Smith   col_lens[2] = A->cmap.n;
295416022c9SBarry Smith   col_lens[3] = a->nz;
296416022c9SBarry Smith 
297416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
298899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
299416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30017ab2063SBarry Smith   }
301899cda47SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap.n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
302606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
303416022c9SBarry Smith 
304416022c9SBarry Smith   /* store column indices (zero start index) */
3056f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
306416022c9SBarry Smith 
307416022c9SBarry Smith   /* store nonzero values */
3086f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3093a40ed3dSBarry Smith   PetscFunctionReturn(0);
31017ab2063SBarry Smith }
311416022c9SBarry Smith 
312dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
313cd155464SBarry Smith 
3144a2ae208SSatish Balay #undef __FUNCT__
3154a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
316dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
317416022c9SBarry Smith {
318416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
319dfbe8321SBarry Smith   PetscErrorCode    ierr;
320899cda47SBarry Smith   PetscInt          i,j,m = A->rmap.n,shift=0;
321e060cb09SBarry Smith   const char        *name;
322f3ef73ceSBarry Smith   PetscViewerFormat format;
32317ab2063SBarry Smith 
3243a40ed3dSBarry Smith   PetscFunctionBegin;
325435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
326b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
32771c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
32897f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
329899cda47SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap.n-!shift)) {
330d00d2cf4SBarry Smith       nofinalvalue = 1;
331d00d2cf4SBarry Smith     }
332b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
333899cda47SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap.n);CHKERRQ(ierr);
33477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
33577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
336b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
33717ab2063SBarry Smith 
33817ab2063SBarry Smith     for (i=0; i<m; i++) {
339416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
340aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34177431f27SBarry 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);
34217ab2063SBarry Smith #else
34377431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34417ab2063SBarry Smith #endif
34517ab2063SBarry Smith       }
34617ab2063SBarry Smith     }
347d00d2cf4SBarry Smith     if (nofinalvalue) {
348899cda47SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap.n,0.0);CHKERRQ(ierr);
349d00d2cf4SBarry Smith     }
350fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
351b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35268369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
353cd155464SBarry Smith      PetscFunctionReturn(0);
354fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
355b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
35644cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
35777431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
35844cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
359aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36036db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
361a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36236db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
363a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36436db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
365a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3666831982aSBarry Smith         }
36744cd7ae7SLois Curfman McInnes #else
368a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
36944cd7ae7SLois Curfman McInnes #endif
37044cd7ae7SLois Curfman McInnes       }
371b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37244cd7ae7SLois Curfman McInnes     }
373b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
374fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
37597f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
376b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
37797f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
378496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
379496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
380496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
381496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
382aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38336db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
384496be53dSLois Curfman McInnes #else
385496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
386496be53dSLois Curfman McInnes #endif
387496be53dSLois Curfman McInnes         }
388496be53dSLois Curfman McInnes       }
389496be53dSLois Curfman McInnes     }
3902e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3922e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39377431f27SBarry 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);}
39477431f27SBarry 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);}
39577431f27SBarry 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);}
39677431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
39777431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
39877431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
399496be53dSLois Curfman McInnes     }
400b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
401606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
402496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
403496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
40477431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
405496be53dSLois Curfman McInnes       }
406b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
407496be53dSLois Curfman McInnes     }
408b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
409496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
410496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
411496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
412aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41336db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
414b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4156831982aSBarry Smith           }
416496be53dSLois Curfman McInnes #else
417b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
418496be53dSLois Curfman McInnes #endif
419496be53dSLois Curfman McInnes         }
420496be53dSLois Curfman McInnes       }
421b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
422496be53dSLois Curfman McInnes     }
423b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
424fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
42597f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
42687828ca2SBarry Smith     PetscScalar value;
42702594712SBarry Smith 
428b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
42902594712SBarry Smith     for (i=0; i<m; i++) {
43002594712SBarry Smith       jcnt = 0;
431899cda47SBarry Smith       for (j=0; j<A->cmap.n; j++) {
432e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43302594712SBarry Smith           value = a->a[cnt++];
434e24b481bSBarry Smith           jcnt++;
43502594712SBarry Smith         } else {
43602594712SBarry Smith           value = 0.0;
43702594712SBarry Smith         }
438aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
439b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44002594712SBarry Smith #else
441b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44202594712SBarry Smith #endif
44302594712SBarry Smith       }
444b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44502594712SBarry Smith     }
446b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4473a40ed3dSBarry Smith   } else {
448b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44917ab2063SBarry Smith     for (i=0; i<m; i++) {
45077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
451416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
452aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
45336db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
454a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
45536db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
456a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4573a40ed3dSBarry Smith         } else {
458a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
45917ab2063SBarry Smith         }
46017ab2063SBarry Smith #else
461a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
46217ab2063SBarry Smith #endif
46317ab2063SBarry Smith       }
464b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
46517ab2063SBarry Smith     }
466b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
46717ab2063SBarry Smith   }
468b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4693a40ed3dSBarry Smith   PetscFunctionReturn(0);
470416022c9SBarry Smith }
471416022c9SBarry Smith 
4724a2ae208SSatish Balay #undef __FUNCT__
4734a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
474dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
475416022c9SBarry Smith {
476480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
477416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
478dfbe8321SBarry Smith   PetscErrorCode    ierr;
479899cda47SBarry Smith   PetscInt          i,j,m = A->rmap.n,color;
48036db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
481b0a32e0cSBarry Smith   PetscViewer       viewer;
482f3ef73ceSBarry Smith   PetscViewerFormat format;
483cddf8d76SBarry Smith 
4843a40ed3dSBarry Smith   PetscFunctionBegin;
485480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
486b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
48719bcc07fSBarry Smith 
488b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
489416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4900513a670SBarry Smith 
491fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4920513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
493b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
494416022c9SBarry Smith     for (i=0; i<m; i++) {
495cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
496bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
497bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
498aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
500cddf8d76SBarry Smith #else
501cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
502cddf8d76SBarry Smith #endif
503b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
504cddf8d76SBarry Smith       }
505cddf8d76SBarry Smith     }
506b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
507cddf8d76SBarry Smith     for (i=0; i<m; i++) {
508cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
509bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
510bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
511cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
512b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
513cddf8d76SBarry Smith       }
514cddf8d76SBarry Smith     }
515b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
516cddf8d76SBarry Smith     for (i=0; i<m; i++) {
517cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
518bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
519bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
520aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52136db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
522cddf8d76SBarry Smith #else
523cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
524cddf8d76SBarry Smith #endif
525b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
526416022c9SBarry Smith       }
527416022c9SBarry Smith     }
5280513a670SBarry Smith   } else {
5290513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5300513a670SBarry Smith     /* first determine max of all nonzero values */
53197f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
532b0a32e0cSBarry Smith     PetscDraw   popup;
53336db0b34SBarry Smith     PetscReal scale;
5340513a670SBarry Smith 
5350513a670SBarry Smith     for (i=0; i<nz; i++) {
5360513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5370513a670SBarry Smith     }
538b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
539b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
540b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5410513a670SBarry Smith     count = 0;
5420513a670SBarry Smith     for (i=0; i<m; i++) {
5430513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
544bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
545bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
54697f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
547b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5480513a670SBarry Smith         count++;
5490513a670SBarry Smith       }
5500513a670SBarry Smith     }
5510513a670SBarry Smith   }
552480ef9eaSBarry Smith   PetscFunctionReturn(0);
553480ef9eaSBarry Smith }
554cddf8d76SBarry Smith 
5554a2ae208SSatish Balay #undef __FUNCT__
5564a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
557dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
558480ef9eaSBarry Smith {
559dfbe8321SBarry Smith   PetscErrorCode ierr;
560b0a32e0cSBarry Smith   PetscDraw      draw;
56136db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
562480ef9eaSBarry Smith   PetscTruth     isnull;
563480ef9eaSBarry Smith 
564480ef9eaSBarry Smith   PetscFunctionBegin;
565b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
566b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
567480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
568480ef9eaSBarry Smith 
569480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
570899cda47SBarry Smith   xr  = A->cmap.n; yr = A->rmap.n; h = yr/10.0; w = xr/10.0;
571480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
572b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
573b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
574480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5753a40ed3dSBarry Smith   PetscFunctionReturn(0);
576416022c9SBarry Smith }
577416022c9SBarry Smith 
5784a2ae208SSatish Balay #undef __FUNCT__
5794a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
580dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
581416022c9SBarry Smith {
582dfbe8321SBarry Smith   PetscErrorCode ierr;
5836805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
584416022c9SBarry Smith 
5853a40ed3dSBarry Smith   PetscFunctionBegin;
58632077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
587fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
588fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
589c45a1595SBarry Smith   if (iascii) {
5903a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5910f5bd95cSBarry Smith   } else if (isbinary) {
5923a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5930f5bd95cSBarry Smith   } else if (isdraw) {
5943a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5955cd90555SBarry Smith   } else {
596958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
59717ab2063SBarry Smith   }
5984846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
5993a40ed3dSBarry Smith   PetscFunctionReturn(0);
60017ab2063SBarry Smith }
60119bcc07fSBarry Smith 
6024a2ae208SSatish Balay #undef __FUNCT__
6034a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
604dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
60517ab2063SBarry Smith {
606416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6076849ba73SBarry Smith   PetscErrorCode ierr;
60897f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
609899cda47SBarry Smith   PetscInt       m = A->rmap.n,*ip,N,*ailen = a->ilen,rmax = 0;
61054f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6113447b6efSHong Zhang   PetscReal      ratio=0.6;
61217ab2063SBarry Smith 
6133a40ed3dSBarry Smith   PetscFunctionBegin;
6143a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
61517ab2063SBarry Smith 
61643ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
61717ab2063SBarry Smith   for (i=1; i<m; i++) {
618416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
61917ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
62094a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
62117ab2063SBarry Smith     if (fshift) {
622bfeeae90SHong Zhang       ip = aj + ai[i] ;
623bfeeae90SHong Zhang       ap = aa + ai[i] ;
62417ab2063SBarry Smith       N  = ailen[i];
62517ab2063SBarry Smith       for (j=0; j<N; j++) {
62617ab2063SBarry Smith         ip[j-fshift] = ip[j];
62717ab2063SBarry Smith         ap[j-fshift] = ap[j];
62817ab2063SBarry Smith       }
62917ab2063SBarry Smith     }
63017ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
63117ab2063SBarry Smith   }
63217ab2063SBarry Smith   if (m) {
63317ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
63417ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
63517ab2063SBarry Smith   }
63617ab2063SBarry Smith   /* reset ilen and imax for each row */
63717ab2063SBarry Smith   for (i=0; i<m; i++) {
63817ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
63917ab2063SBarry Smith   }
640bfeeae90SHong Zhang   a->nz = ai[m];
64117ab2063SBarry Smith 
64209f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
643899cda47SBarry 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);
644ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
645ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
646a0e1a203SBarry Smith 
647dd5f02e7SSatish Balay   a->reallocs          = 0;
6484e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
64936db0b34SBarry Smith   a->rmax              = rmax;
6504e220ebcSLois Curfman McInnes 
651cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
652317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
65388e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
65471c2f376SKris Buschelman 
6554846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
65671f1c65dSBarry Smith 
65771f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6583a40ed3dSBarry Smith   PetscFunctionReturn(0);
65917ab2063SBarry Smith }
66017ab2063SBarry Smith 
6614a2ae208SSatish Balay #undef __FUNCT__
66299cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
66399cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
66499cafbc1SBarry Smith {
66599cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
66699cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
66754f21887SBarry Smith   MatScalar      *aa = a->a;
66899cafbc1SBarry Smith 
66999cafbc1SBarry Smith   PetscFunctionBegin;
67099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
67199cafbc1SBarry Smith   PetscFunctionReturn(0);
67299cafbc1SBarry Smith }
67399cafbc1SBarry Smith 
67499cafbc1SBarry Smith #undef __FUNCT__
67599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
67699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
67799cafbc1SBarry Smith {
67899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
67999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
68054f21887SBarry Smith   MatScalar      *aa = a->a;
68199cafbc1SBarry Smith 
68299cafbc1SBarry Smith   PetscFunctionBegin;
68399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
68499cafbc1SBarry Smith   PetscFunctionReturn(0);
68599cafbc1SBarry Smith }
68699cafbc1SBarry Smith 
68799cafbc1SBarry Smith #undef __FUNCT__
6884a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
689dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
69017ab2063SBarry Smith {
691416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
692dfbe8321SBarry Smith   PetscErrorCode ierr;
6933a40ed3dSBarry Smith 
6943a40ed3dSBarry Smith   PetscFunctionBegin;
695899cda47SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr);
6963a40ed3dSBarry Smith   PetscFunctionReturn(0);
69717ab2063SBarry Smith }
698416022c9SBarry Smith 
6994a2ae208SSatish Balay #undef __FUNCT__
7004a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
701dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
70217ab2063SBarry Smith {
703416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
704dfbe8321SBarry Smith   PetscErrorCode ierr;
705d5d45c9bSBarry Smith 
7063a40ed3dSBarry Smith   PetscFunctionBegin;
707aa482453SBarry Smith #if defined(PETSC_USE_LOG)
708899cda47SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap.n,A->cmap.n,a->nz);
70917ab2063SBarry Smith #endif
710e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
711c38d4ed2SBarry Smith   if (a->row) {
712c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
713c38d4ed2SBarry Smith   }
714c38d4ed2SBarry Smith   if (a->col) {
715c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
716c38d4ed2SBarry Smith   }
71705b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
71805b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
71971f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
72005b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
72182bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
72205b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
723cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
72405b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
725407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
726d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
727a30b2313SHong Zhang 
7284846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7294846f1f5SKris Buschelman 
730606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
731901853e0SKris Buschelman 
732dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
733901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
734901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
735901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
736901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
737901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
738ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
739901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
740901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
741a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
742901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7433a40ed3dSBarry Smith   PetscFunctionReturn(0);
74417ab2063SBarry Smith }
74517ab2063SBarry Smith 
7464a2ae208SSatish Balay #undef __FUNCT__
7474a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
748dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
74917ab2063SBarry Smith {
7503a40ed3dSBarry Smith   PetscFunctionBegin;
7513a40ed3dSBarry Smith   PetscFunctionReturn(0);
75217ab2063SBarry Smith }
75317ab2063SBarry Smith 
7544a2ae208SSatish Balay #undef __FUNCT__
7554a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7564e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
75717ab2063SBarry Smith {
758416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7594846f1f5SKris Buschelman   PetscErrorCode ierr;
7603a40ed3dSBarry Smith 
7613a40ed3dSBarry Smith   PetscFunctionBegin;
762a65d3064SKris Buschelman   switch (op) {
763a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7644e0d8c25SBarry Smith       a->roworiented       = flg;
765a65d3064SKris Buschelman       break;
766a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
7674e0d8c25SBarry Smith       a->keepzeroedrows    = flg;
768a65d3064SKris Buschelman       break;
769512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
770512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
771a65d3064SKris Buschelman       break;
772a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
7734e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
774a65d3064SKris Buschelman       break;
775a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
7764e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
777a65d3064SKris Buschelman       break;
778a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
7794e0d8c25SBarry Smith       a->ignorezeroentries = flg;
7800df259c2SBarry Smith       break;
781d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
7824e0d8c25SBarry Smith       a->compressedrow.use = flg;
783d487561eSHong Zhang       break;
7844e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
785a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
786a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
787290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
788a65d3064SKris Buschelman       break;
789a65d3064SKris Buschelman     default:
79071c2f376SKris Buschelman       break;
791a65d3064SKris Buschelman   }
7924e0d8c25SBarry Smith   ierr = MatSetOption_Inode(A,op,flg);CHKERRQ(ierr);
7933a40ed3dSBarry Smith   PetscFunctionReturn(0);
79417ab2063SBarry Smith }
79517ab2063SBarry Smith 
7964a2ae208SSatish Balay #undef __FUNCT__
7974a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
798dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
79917ab2063SBarry Smith {
800416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8016849ba73SBarry Smith   PetscErrorCode ierr;
80297f1f81fSBarry Smith   PetscInt       i,j,n;
80387828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
80417ab2063SBarry Smith 
8053a40ed3dSBarry Smith   PetscFunctionBegin;
8062dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8071ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
80836db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
809899cda47SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
810899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
811bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
812bfeeae90SHong Zhang       if (a->j[j] == i) {
813416022c9SBarry Smith         x[i] = a->a[j];
81417ab2063SBarry Smith         break;
81517ab2063SBarry Smith       }
81617ab2063SBarry Smith     }
81717ab2063SBarry Smith   }
8181ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8193a40ed3dSBarry Smith   PetscFunctionReturn(0);
82017ab2063SBarry Smith }
82117ab2063SBarry Smith 
8224a2ae208SSatish Balay #undef __FUNCT__
8234a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
824dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
82517ab2063SBarry Smith {
826416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8275c897100SBarry Smith   PetscScalar       *x,*y;
828dfbe8321SBarry Smith   PetscErrorCode    ierr;
829899cda47SBarry Smith   PetscInt          m = A->rmap.n;
8305c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
831a77337e4SBarry Smith   MatScalar         *v;
832a77337e4SBarry Smith   PetscScalar       alpha;
8337b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8343447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8354eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8365c897100SBarry Smith #endif
83717ab2063SBarry Smith 
8383a40ed3dSBarry Smith   PetscFunctionBegin;
8392e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8401ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8411ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8425c897100SBarry Smith 
8435c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
844bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8455c897100SBarry Smith #else
8463447b6efSHong Zhang   if (usecprow){
8473447b6efSHong Zhang     m    = cprow.nrows;
8483447b6efSHong Zhang     ii   = cprow.i;
8497b2bb3b9SHong Zhang     ridx = cprow.rindex;
8503447b6efSHong Zhang   } else {
8513447b6efSHong Zhang     ii = a->i;
8523447b6efSHong Zhang   }
85317ab2063SBarry Smith   for (i=0; i<m; i++) {
8543447b6efSHong Zhang     idx   = a->j + ii[i] ;
8553447b6efSHong Zhang     v     = a->a + ii[i] ;
8563447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8573447b6efSHong Zhang     if (usecprow){
8587b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8593447b6efSHong Zhang     } else {
86017ab2063SBarry Smith       alpha = x[i];
8613447b6efSHong Zhang     }
86217ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
86317ab2063SBarry Smith   }
8645c897100SBarry Smith #endif
865efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
8661ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8671ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8683a40ed3dSBarry Smith   PetscFunctionReturn(0);
86917ab2063SBarry Smith }
87017ab2063SBarry Smith 
8714a2ae208SSatish Balay #undef __FUNCT__
8725c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
873dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8745c897100SBarry Smith {
875dfbe8321SBarry Smith   PetscErrorCode ierr;
8765c897100SBarry Smith 
8775c897100SBarry Smith   PetscFunctionBegin;
878170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
8795c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8805c897100SBarry Smith   PetscFunctionReturn(0);
8815c897100SBarry Smith }
8825c897100SBarry Smith 
8835c897100SBarry Smith 
8845c897100SBarry Smith #undef __FUNCT__
8854a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
886dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
88717ab2063SBarry Smith {
888416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
889d9fead3dSBarry Smith   PetscScalar       *y;
89054f21887SBarry Smith   const PetscScalar *x;
89154f21887SBarry Smith   const MatScalar   *aa;
892dfbe8321SBarry Smith   PetscErrorCode    ierr;
893899cda47SBarry Smith   PetscInt          m=A->rmap.n,*aj,*ii;
894a46b3154SVictor Eijkhout   PetscInt          n,i,j,nonzerorow=0,*ridx=PETSC_NULL;
895362ced78SSatish Balay   PetscScalar       sum;
89697952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
897f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
898f2e71c43SSatish Balay   PetscInt         jrow;
899e36a17ebSSatish Balay #endif
90017ab2063SBarry Smith 
901b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
90297952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
903fee21e36SBarry Smith #endif
904fee21e36SBarry Smith 
9053a40ed3dSBarry Smith   PetscFunctionBegin;
906d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9071ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
90897952fefSHong Zhang   aj  = a->j;
90997952fefSHong Zhang   aa  = a->a;
910416022c9SBarry Smith   ii  = a->i;
9114eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
91297952fefSHong Zhang     m    = a->compressedrow.nrows;
91397952fefSHong Zhang     ii   = a->compressedrow.i;
91497952fefSHong Zhang     ridx = a->compressedrow.rindex;
91597952fefSHong Zhang     for (i=0; i<m; i++){
91697952fefSHong Zhang       n   = ii[i+1] - ii[i];
91797952fefSHong Zhang       aj  = a->j + ii[i];
91897952fefSHong Zhang       aa  = a->a + ii[i];
91997952fefSHong Zhang       sum = 0.0;
920a46b3154SVictor Eijkhout       nonzerorow += (n>0);
92197952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
92297952fefSHong Zhang       y[*ridx++] = sum;
92397952fefSHong Zhang     }
92497952fefSHong Zhang   } else { /* do not use compressed row format */
925b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
926b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
927b05257ddSBarry Smith #else
92817ab2063SBarry Smith     for (i=0; i<m; i++) {
9299ea0dfa2SSatish Balay       jrow = ii[i];
9309ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
93117ab2063SBarry Smith       sum  = 0.0;
932a46b3154SVictor Eijkhout       nonzerorow += (n>0);
9339ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
93497952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9359ea0dfa2SSatish Balay       }
93617ab2063SBarry Smith       y[i] = sum;
93717ab2063SBarry Smith     }
9388d195f9aSBarry Smith #endif
939b05257ddSBarry Smith   }
940a46b3154SVictor Eijkhout   ierr = PetscLogFlops(2*a->nz - nonzerorow);CHKERRQ(ierr);
941d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9421ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9433a40ed3dSBarry Smith   PetscFunctionReturn(0);
94417ab2063SBarry Smith }
94517ab2063SBarry Smith 
9464a2ae208SSatish Balay #undef __FUNCT__
9474a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
948dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
94917ab2063SBarry Smith {
950416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
95154f21887SBarry Smith   PetscScalar     *x,*y,*z;
95254f21887SBarry Smith   const MatScalar *aa;
953dfbe8321SBarry Smith   PetscErrorCode  ierr;
954899cda47SBarry Smith   PetscInt        m = A->rmap.n,*aj,*ii;
955aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
95697952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
957362ced78SSatish Balay   PetscScalar     sum;
95897952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
959e36a17ebSSatish Balay #endif
9609ea0dfa2SSatish Balay 
9613a40ed3dSBarry Smith   PetscFunctionBegin;
9621ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9631ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9642e8a6d31SBarry Smith   if (zz != yy) {
9651ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9662e8a6d31SBarry Smith   } else {
9672e8a6d31SBarry Smith     z = y;
9682e8a6d31SBarry Smith   }
969bfeeae90SHong Zhang 
97097952fefSHong Zhang   aj  = a->j;
97197952fefSHong Zhang   aa  = a->a;
972cddf8d76SBarry Smith   ii  = a->i;
973aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
97497952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
97502ab625aSSatish Balay #else
9764eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
9774eb6d288SHong Zhang     if (zz != yy){
9784eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
9794eb6d288SHong Zhang     }
98097952fefSHong Zhang     m    = a->compressedrow.nrows;
98197952fefSHong Zhang     ii   = a->compressedrow.i;
98297952fefSHong Zhang     ridx = a->compressedrow.rindex;
98397952fefSHong Zhang     for (i=0; i<m; i++){
98497952fefSHong Zhang       n  = ii[i+1] - ii[i];
98597952fefSHong Zhang       aj  = a->j + ii[i];
98697952fefSHong Zhang       aa  = a->a + ii[i];
98797952fefSHong Zhang       sum = y[*ridx];
98897952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
98997952fefSHong Zhang       z[*ridx++] = sum;
99097952fefSHong Zhang     }
99197952fefSHong Zhang   } else { /* do not use compressed row format */
99217ab2063SBarry Smith     for (i=0; i<m; i++) {
9939ea0dfa2SSatish Balay       jrow = ii[i];
9949ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
99517ab2063SBarry Smith       sum  = y[i];
9969ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
99797952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9989ea0dfa2SSatish Balay       }
99917ab2063SBarry Smith       z[i] = sum;
100017ab2063SBarry Smith     }
100197952fefSHong Zhang   }
100202ab625aSSatish Balay #endif
1003efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
10041ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10051ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10062e8a6d31SBarry Smith   if (zz != yy) {
10071ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10082e8a6d31SBarry Smith   }
10093a40ed3dSBarry Smith   PetscFunctionReturn(0);
101017ab2063SBarry Smith }
101117ab2063SBarry Smith 
101217ab2063SBarry Smith /*
101317ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
101417ab2063SBarry Smith */
10154a2ae208SSatish Balay #undef __FUNCT__
10164a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1017dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
101817ab2063SBarry Smith {
1019416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10206849ba73SBarry Smith   PetscErrorCode ierr;
102109f38230SBarry Smith   PetscInt       i,j,m = A->rmap.n;
102217ab2063SBarry Smith 
10233a40ed3dSBarry Smith   PetscFunctionBegin;
102409f38230SBarry Smith   if (!a->diag) {
102509f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10269518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
102709f38230SBarry Smith   }
1028899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
102909f38230SBarry Smith     a->diag[i] = a->i[i+1];
1030bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1031bfeeae90SHong Zhang       if (a->j[j] == i) {
103209f38230SBarry Smith         a->diag[i] = j;
103317ab2063SBarry Smith         break;
103417ab2063SBarry Smith       }
103517ab2063SBarry Smith     }
103617ab2063SBarry Smith   }
10373a40ed3dSBarry Smith   PetscFunctionReturn(0);
103817ab2063SBarry Smith }
103917ab2063SBarry Smith 
1040be5855fcSBarry Smith /*
1041be5855fcSBarry Smith      Checks for missing diagonals
1042be5855fcSBarry Smith */
10434a2ae208SSatish Balay #undef __FUNCT__
10444a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
104509f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1046be5855fcSBarry Smith {
1047be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
104897f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1049be5855fcSBarry Smith 
1050be5855fcSBarry Smith   PetscFunctionBegin;
105109f38230SBarry Smith   *missing = PETSC_FALSE;
105209f38230SBarry Smith   if (A->rmap.n > 0 && !jj) {
105309f38230SBarry Smith     *missing  = PETSC_TRUE;
105409f38230SBarry Smith     if (d) *d = 0;
105509f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
105609f38230SBarry Smith   } else {
1057f1e2ffcdSBarry Smith     diag = a->diag;
1058899cda47SBarry Smith     for (i=0; i<A->rmap.n; i++) {
1059bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
106009f38230SBarry Smith 	*missing = PETSC_TRUE;
106109f38230SBarry Smith 	if (d) *d = i;
106209f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
106309f38230SBarry Smith       }
1064be5855fcSBarry Smith     }
1065be5855fcSBarry Smith   }
1066be5855fcSBarry Smith   PetscFunctionReturn(0);
1067be5855fcSBarry Smith }
1068be5855fcSBarry Smith 
106971f1c65dSBarry Smith EXTERN_C_BEGIN
107071f1c65dSBarry Smith #undef __FUNCT__
107171f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
107271f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
107371f1c65dSBarry Smith {
107471f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
107571f1c65dSBarry Smith   PetscErrorCode ierr;
107671f1c65dSBarry Smith   PetscInt       i,*diag,m = A->rmap.n;
107754f21887SBarry Smith   MatScalar      *v = a->a;
107854f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
107971f1c65dSBarry Smith 
108071f1c65dSBarry Smith   PetscFunctionBegin;
108171f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
108271f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
108371f1c65dSBarry Smith   diag = a->diag;
108471f1c65dSBarry Smith   if (!a->idiag) {
108571f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
108671f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
108771f1c65dSBarry Smith     v        = a->a;
108871f1c65dSBarry Smith   }
108971f1c65dSBarry Smith   mdiag = a->mdiag;
109071f1c65dSBarry Smith   idiag = a->idiag;
109171f1c65dSBarry Smith 
1092028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
109371f1c65dSBarry Smith     for (i=0; i<m; i++) {
109471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
109571f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
109671f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
109771f1c65dSBarry Smith     }
109871f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
109971f1c65dSBarry Smith   } else {
110071f1c65dSBarry Smith     for (i=0; i<m; i++) {
110171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
110271f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
110371f1c65dSBarry Smith     }
110471f1c65dSBarry Smith     ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
110571f1c65dSBarry Smith   }
110671f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
110771f1c65dSBarry Smith   PetscFunctionReturn(0);
110871f1c65dSBarry Smith }
11095a9745a3SMatthew Knepley EXTERN_C_END
111071f1c65dSBarry Smith 
11114a2ae208SSatish Balay #undef __FUNCT__
11124a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
111397f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
111417ab2063SBarry Smith {
1115416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1116beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
111754f21887SBarry Smith   const MatScalar    *v = a->a;
111854f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1119dfbe8321SBarry Smith   PetscErrorCode     ierr;
1120899cda47SBarry Smith   PetscInt           n = A->cmap.n,m = A->rmap.n,i;
112197f1f81fSBarry Smith   const PetscInt     *idx,*diag;
112217ab2063SBarry Smith 
11233a40ed3dSBarry Smith   PetscFunctionBegin;
1124b965ef7fSBarry Smith   its = its*lits;
112591723122SBarry Smith 
112671f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
112771f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
112871f1c65dSBarry Smith   a->fshift = fshift;
112971f1c65dSBarry Smith   a->omega  = omega;
1130ed480e8bSBarry Smith 
113171f1c65dSBarry Smith   diag = a->diag;
113271f1c65dSBarry Smith   t     = a->ssor_work;
1133ed480e8bSBarry Smith   idiag = a->idiag;
113471f1c65dSBarry Smith   mdiag = a->mdiag;
1135ed480e8bSBarry Smith 
11361ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1137fb2e594dSBarry Smith   if (xx != bb) {
11381ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1139fb2e594dSBarry Smith   } else {
1140fb2e594dSBarry Smith     b = x;
1141fb2e594dSBarry Smith   }
114271f1c65dSBarry Smith   CHKMEMQ;
1143ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1144ed480e8bSBarry Smith   xs   = x;
114517ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
114617ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1147ed480e8bSBarry Smith     bs = b;
114817ab2063SBarry Smith     for (i=0; i<m; i++) {
114971f1c65dSBarry Smith         d    = fshift + mdiag[i];
1150416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1151ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1152ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
115317ab2063SBarry Smith         sum  = b[i]*d/omega;
115417ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
115517ab2063SBarry Smith         x[i] = sum;
115617ab2063SBarry Smith     }
11571ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11581ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1159efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11603a40ed3dSBarry Smith     PetscFunctionReturn(0);
116117ab2063SBarry Smith   }
1162c783ea89SBarry Smith 
116348af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
116429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11653a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
116617ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
116717ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
116817ab2063SBarry Smith 
116917ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
117017ab2063SBarry Smith 
117117ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
117217ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
117317ab2063SBarry Smith     is the relaxation factor.
117417ab2063SBarry Smith     */
117517ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
117617ab2063SBarry Smith 
117717ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
117817ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1179416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1180ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1181ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
118217ab2063SBarry Smith       sum  = b[i];
118317ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1184ed480e8bSBarry Smith       x[i] = sum*idiag[i];
118517ab2063SBarry Smith     }
118617ab2063SBarry Smith 
118717ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1188416022c9SBarry Smith     v = a->a;
1189ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
119017ab2063SBarry Smith 
119117ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1192ed480e8bSBarry Smith     ts = t;
1193416022c9SBarry Smith     diag = a->diag;
119417ab2063SBarry Smith     for (i=0; i<m; i++) {
1195416022c9SBarry Smith       n    = diag[i] - a->i[i];
1196ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1197ed480e8bSBarry Smith       v    = a->a + a->i[i];
119817ab2063SBarry Smith       sum  = t[i];
119917ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1200ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1201733d66baSBarry Smith       /*  x = x + t */
1202733d66baSBarry Smith       x[i] += t[i];
120317ab2063SBarry Smith     }
120417ab2063SBarry Smith 
1205efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
12061ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12071ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12083a40ed3dSBarry Smith     PetscFunctionReturn(0);
120917ab2063SBarry Smith   }
121017ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
121117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
121277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
121397f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
121477d8c4bbSBarry Smith #else
121517ab2063SBarry Smith       for (i=0; i<m; i++) {
1216416022c9SBarry Smith         n    = diag[i] - a->i[i];
1217ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1218ed480e8bSBarry Smith         v    = a->a + a->i[i];
121917ab2063SBarry Smith         sum  = b[i];
122017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1221ed480e8bSBarry Smith         x[i] = sum*idiag[i];
122217ab2063SBarry Smith       }
122377d8c4bbSBarry Smith #endif
122417ab2063SBarry Smith       xb = x;
1225efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12263a40ed3dSBarry Smith     } else xb = b;
122717ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
122817ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
122917ab2063SBarry Smith       for (i=0; i<m; i++) {
1230ed480e8bSBarry Smith         x[i] *= mdiag[i];
123117ab2063SBarry Smith       }
1232efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
123317ab2063SBarry Smith     }
123417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
123577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
123697f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
123777d8c4bbSBarry Smith #else
123817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1239416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1240ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1241ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
124217ab2063SBarry Smith         sum  = xb[i];
124317ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1244ed480e8bSBarry Smith         x[i] = sum*idiag[i];
124517ab2063SBarry Smith       }
124677d8c4bbSBarry Smith #endif
1247efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
124817ab2063SBarry Smith     }
124917ab2063SBarry Smith     its--;
125017ab2063SBarry Smith   }
125117ab2063SBarry Smith   while (its--) {
125217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
125377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
125497f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
125577d8c4bbSBarry Smith #else
125617ab2063SBarry Smith       for (i=0; i<m; i++) {
1257416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1258ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1259ed480e8bSBarry Smith         v    = a->a + a->i[i];
126017ab2063SBarry Smith         sum  = b[i];
126117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1262ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
126317ab2063SBarry Smith       }
126477d8c4bbSBarry Smith #endif
1265efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
126617ab2063SBarry Smith     }
126717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
126877d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
126997f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
127077d8c4bbSBarry Smith #else
127117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1272416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1273ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1274ed480e8bSBarry Smith         v    = a->a + a->i[i];
127517ab2063SBarry Smith         sum  = b[i];
127617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1277ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
127817ab2063SBarry Smith       }
127977d8c4bbSBarry Smith #endif
1280efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
128117ab2063SBarry Smith     }
128217ab2063SBarry Smith   }
12831ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12841ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
128571f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
128617ab2063SBarry Smith }
128717ab2063SBarry Smith 
12882af78befSBarry Smith 
12894a2ae208SSatish Balay #undef __FUNCT__
12904a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1291dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
129217ab2063SBarry Smith {
1293416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12944e220ebcSLois Curfman McInnes 
12953a40ed3dSBarry Smith   PetscFunctionBegin;
1296899cda47SBarry Smith   info->rows_global    = (double)A->rmap.n;
1297899cda47SBarry Smith   info->columns_global = (double)A->cmap.n;
1298899cda47SBarry Smith   info->rows_local     = (double)A->rmap.n;
1299899cda47SBarry Smith   info->columns_local  = (double)A->cmap.n;
13004e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13014e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13024e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13034e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13044e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13054e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13067adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13074e220ebcSLois Curfman McInnes   if (A->factor) {
13084e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13094e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13104e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13114e220ebcSLois Curfman McInnes   } else {
13124e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13134e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13144e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13154e220ebcSLois Curfman McInnes   }
13163a40ed3dSBarry Smith   PetscFunctionReturn(0);
131717ab2063SBarry Smith }
131817ab2063SBarry Smith 
13194a2ae208SSatish Balay #undef __FUNCT__
13204a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1321f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
132217ab2063SBarry Smith {
1323416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
132409f38230SBarry Smith   PetscInt       i,m = A->rmap.n - 1,d;
13256849ba73SBarry Smith   PetscErrorCode ierr;
132609f38230SBarry Smith   PetscTruth     missing;
132717ab2063SBarry Smith 
13283a40ed3dSBarry Smith   PetscFunctionBegin;
1329f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1330f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
133177431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1332bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1333f1e2ffcdSBarry Smith     }
1334f4df32b1SMatthew Knepley     if (diag != 0.0) {
133509f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
133609f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1337f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1338f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1339f1e2ffcdSBarry Smith       }
1340f1e2ffcdSBarry Smith     }
134188e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1342f1e2ffcdSBarry Smith   } else {
1343f4df32b1SMatthew Knepley     if (diag != 0.0) {
134417ab2063SBarry Smith       for (i=0; i<N; i++) {
134577431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13467ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1347416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1348f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1349bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13507ae801bdSBarry Smith         } else { /* in case row was completely empty */
1351f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
135217ab2063SBarry Smith         }
135317ab2063SBarry Smith       }
13543a40ed3dSBarry Smith     } else {
135517ab2063SBarry Smith       for (i=0; i<N; i++) {
135677431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1357416022c9SBarry Smith         a->ilen[rows[i]] = 0;
135817ab2063SBarry Smith       }
135917ab2063SBarry Smith     }
136088e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1361f1e2ffcdSBarry Smith   }
136243a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13633a40ed3dSBarry Smith   PetscFunctionReturn(0);
136417ab2063SBarry Smith }
136517ab2063SBarry Smith 
13664a2ae208SSatish Balay #undef __FUNCT__
13674a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1368a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
136917ab2063SBarry Smith {
1370416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
137197f1f81fSBarry Smith   PetscInt   *itmp;
137217ab2063SBarry Smith 
13733a40ed3dSBarry Smith   PetscFunctionBegin;
1374899cda47SBarry Smith   if (row < 0 || row >= A->rmap.n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
137517ab2063SBarry Smith 
1376416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1377bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
137817ab2063SBarry Smith   if (idx) {
1379bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1380bfeeae90SHong Zhang     if (*nz) {
13814e093b46SBarry Smith       *idx = itmp;
138217ab2063SBarry Smith     }
138317ab2063SBarry Smith     else *idx = 0;
138417ab2063SBarry Smith   }
13853a40ed3dSBarry Smith   PetscFunctionReturn(0);
138617ab2063SBarry Smith }
138717ab2063SBarry Smith 
1388bfeeae90SHong Zhang /* remove this function? */
13894a2ae208SSatish Balay #undef __FUNCT__
13904a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1391a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
139217ab2063SBarry Smith {
13933a40ed3dSBarry Smith   PetscFunctionBegin;
13943a40ed3dSBarry Smith   PetscFunctionReturn(0);
139517ab2063SBarry Smith }
139617ab2063SBarry Smith 
13974a2ae208SSatish Balay #undef __FUNCT__
13984a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1399dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
140017ab2063SBarry Smith {
1401416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
140254f21887SBarry Smith   MatScalar      *v = a->a;
140336db0b34SBarry Smith   PetscReal      sum = 0.0;
14046849ba73SBarry Smith   PetscErrorCode ierr;
140597f1f81fSBarry Smith   PetscInt       i,j;
140617ab2063SBarry Smith 
14073a40ed3dSBarry Smith   PetscFunctionBegin;
140817ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1409416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1410aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
141136db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
141217ab2063SBarry Smith #else
141317ab2063SBarry Smith       sum += (*v)*(*v); v++;
141417ab2063SBarry Smith #endif
141517ab2063SBarry Smith     }
1416064f8208SBarry Smith     *nrm = sqrt(sum);
14173a40ed3dSBarry Smith   } else if (type == NORM_1) {
141836db0b34SBarry Smith     PetscReal *tmp;
141997f1f81fSBarry Smith     PetscInt    *jj = a->j;
1420899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1421899cda47SBarry Smith     ierr = PetscMemzero(tmp,A->cmap.n*sizeof(PetscReal));CHKERRQ(ierr);
1422064f8208SBarry Smith     *nrm = 0.0;
1423416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1424bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
142517ab2063SBarry Smith     }
1426899cda47SBarry Smith     for (j=0; j<A->cmap.n; j++) {
1427064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
142817ab2063SBarry Smith     }
1429606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14303a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1431064f8208SBarry Smith     *nrm = 0.0;
1432899cda47SBarry Smith     for (j=0; j<A->rmap.n; j++) {
1433bfeeae90SHong Zhang       v = a->a + a->i[j];
143417ab2063SBarry Smith       sum = 0.0;
1435416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1436cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
143717ab2063SBarry Smith       }
1438064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
143917ab2063SBarry Smith     }
14403a40ed3dSBarry Smith   } else {
144129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
144217ab2063SBarry Smith   }
14433a40ed3dSBarry Smith   PetscFunctionReturn(0);
144417ab2063SBarry Smith }
144517ab2063SBarry Smith 
14464a2ae208SSatish Balay #undef __FUNCT__
14474a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1448fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
144917ab2063SBarry Smith {
1450416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1451416022c9SBarry Smith   Mat            C;
14526849ba73SBarry Smith   PetscErrorCode ierr;
1453899cda47SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap.n,len,*col;
145454f21887SBarry Smith   MatScalar      *array = a->a;
145517ab2063SBarry Smith 
14563a40ed3dSBarry Smith   PetscFunctionBegin;
1457e9695a30SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1458fc4dec0aSBarry Smith 
1459fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1460899cda47SBarry Smith     ierr = PetscMalloc((1+A->cmap.n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1461899cda47SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap.n)*sizeof(PetscInt));CHKERRQ(ierr);
1462bfeeae90SHong Zhang 
1463bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14647adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1465899cda47SBarry Smith     ierr = MatSetSizes(C,A->cmap.n,m,A->cmap.n,m);CHKERRQ(ierr);
14667adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1467ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1468606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
146917ab2063SBarry Smith     for (i=0; i<m; i++) {
147017ab2063SBarry Smith       len    = ai[i+1]-ai[i];
147187d4246cSBarry Smith       ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1472b9b97703SBarry Smith       array += len;
1473b9b97703SBarry Smith       aj    += len;
147417ab2063SBarry Smith     }
1475fc4dec0aSBarry Smith   } else {
1476fc4dec0aSBarry Smith     C = *B;
1477fc4dec0aSBarry Smith   }
147817ab2063SBarry Smith 
14796d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14806d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148117ab2063SBarry Smith 
1482815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1483416022c9SBarry Smith     *B = C;
148417ab2063SBarry Smith   } else {
1485273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
148617ab2063SBarry Smith   }
14873a40ed3dSBarry Smith   PetscFunctionReturn(0);
148817ab2063SBarry Smith }
148917ab2063SBarry Smith 
1490cd0d46ebSvictorle EXTERN_C_BEGIN
1491cd0d46ebSvictorle #undef __FUNCT__
14925fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1493be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1494cd0d46ebSvictorle {
1495cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
149654f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
149754f21887SBarry Smith   MatScalar      *va,*vb;
14986849ba73SBarry Smith   PetscErrorCode ierr;
149997f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1500cd0d46ebSvictorle 
1501cd0d46ebSvictorle   PetscFunctionBegin;
1502cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1503cd0d46ebSvictorle 
1504cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1505cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15065485867bSBarry Smith   if (ma!=nb || na!=mb){
15075485867bSBarry Smith     *f = PETSC_FALSE;
15085485867bSBarry Smith     PetscFunctionReturn(0);
15095485867bSBarry Smith   }
1510cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1511cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1512cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
151397f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
151497f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1515cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1516cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1517cd0d46ebSvictorle 
1518cd0d46ebSvictorle   *f = PETSC_TRUE;
1519cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1520cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
152197f1f81fSBarry Smith       PetscInt         idc,idr;
15225485867bSBarry Smith       PetscScalar vc,vr;
1523cd0d46ebSvictorle       /* column/row index/value */
15245485867bSBarry Smith       idc = adx[aptr[i]];
15255485867bSBarry Smith       idr = bdx[bptr[idc]];
15265485867bSBarry Smith       vc  = va[aptr[i]];
15275485867bSBarry Smith       vr  = vb[bptr[idc]];
15285485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15295485867bSBarry Smith 	*f = PETSC_FALSE;
15305485867bSBarry Smith         goto done;
1531cd0d46ebSvictorle       } else {
15325485867bSBarry Smith 	aptr[i]++;
15335485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1534cd0d46ebSvictorle       }
1535cd0d46ebSvictorle     }
1536cd0d46ebSvictorle   }
1537cd0d46ebSvictorle  done:
1538cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15393aeef889SHong Zhang   if (B) {
15403aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15413aeef889SHong Zhang   }
1542cd0d46ebSvictorle   PetscFunctionReturn(0);
1543cd0d46ebSvictorle }
1544cd0d46ebSvictorle EXTERN_C_END
1545cd0d46ebSvictorle 
15461cbb95d3SBarry Smith EXTERN_C_BEGIN
15471cbb95d3SBarry Smith #undef __FUNCT__
15481cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15491cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15501cbb95d3SBarry Smith {
15511cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
155254f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
155354f21887SBarry Smith   MatScalar      *va,*vb;
15541cbb95d3SBarry Smith   PetscErrorCode ierr;
15551cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15561cbb95d3SBarry Smith 
15571cbb95d3SBarry Smith   PetscFunctionBegin;
15581cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15591cbb95d3SBarry Smith 
15601cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15611cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15621cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15631cbb95d3SBarry Smith     *f = PETSC_FALSE;
15641cbb95d3SBarry Smith     PetscFunctionReturn(0);
15651cbb95d3SBarry Smith   }
15661cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15671cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15681cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
15691cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
15701cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
15711cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
15721cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
15731cbb95d3SBarry Smith 
15741cbb95d3SBarry Smith   *f = PETSC_TRUE;
15751cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
15761cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
15771cbb95d3SBarry Smith       PetscInt         idc,idr;
15781cbb95d3SBarry Smith       PetscScalar vc,vr;
15791cbb95d3SBarry Smith       /* column/row index/value */
15801cbb95d3SBarry Smith       idc = adx[aptr[i]];
15811cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
15821cbb95d3SBarry Smith       vc  = va[aptr[i]];
15831cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
15841cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
15851cbb95d3SBarry Smith 	*f = PETSC_FALSE;
15861cbb95d3SBarry Smith         goto done;
15871cbb95d3SBarry Smith       } else {
15881cbb95d3SBarry Smith 	aptr[i]++;
15891cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
15901cbb95d3SBarry Smith       }
15911cbb95d3SBarry Smith     }
15921cbb95d3SBarry Smith   }
15931cbb95d3SBarry Smith  done:
15941cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
15951cbb95d3SBarry Smith   if (B) {
15961cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
15971cbb95d3SBarry Smith   }
15981cbb95d3SBarry Smith   PetscFunctionReturn(0);
15991cbb95d3SBarry Smith }
16001cbb95d3SBarry Smith EXTERN_C_END
16011cbb95d3SBarry Smith 
16029e29f15eSvictorle #undef __FUNCT__
16039e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1604dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16059e29f15eSvictorle {
1606dfbe8321SBarry Smith   PetscErrorCode ierr;
16079e29f15eSvictorle   PetscFunctionBegin;
16085485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16099e29f15eSvictorle   PetscFunctionReturn(0);
16109e29f15eSvictorle }
16119e29f15eSvictorle 
16124a2ae208SSatish Balay #undef __FUNCT__
16131cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16141cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16151cbb95d3SBarry Smith {
16161cbb95d3SBarry Smith   PetscErrorCode ierr;
16171cbb95d3SBarry Smith   PetscFunctionBegin;
16181cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16191cbb95d3SBarry Smith   PetscFunctionReturn(0);
16201cbb95d3SBarry Smith }
16211cbb95d3SBarry Smith 
16221cbb95d3SBarry Smith #undef __FUNCT__
16234a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1624dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
162517ab2063SBarry Smith {
1626416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
162754f21887SBarry Smith   PetscScalar    *l,*r,x;
162854f21887SBarry Smith   MatScalar      *v;
1629dfbe8321SBarry Smith   PetscErrorCode ierr;
1630899cda47SBarry Smith   PetscInt       i,j,m = A->rmap.n,n = A->cmap.n,M,nz = a->nz,*jj;
163117ab2063SBarry Smith 
16323a40ed3dSBarry Smith   PetscFunctionBegin;
163317ab2063SBarry Smith   if (ll) {
16343ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16353ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1636e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1637899cda47SBarry Smith     if (m != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16381ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1639416022c9SBarry Smith     v = a->a;
164017ab2063SBarry Smith     for (i=0; i<m; i++) {
164117ab2063SBarry Smith       x = l[i];
1642416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
164317ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
164417ab2063SBarry Smith     }
16451ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1646efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
164717ab2063SBarry Smith   }
164817ab2063SBarry Smith   if (rr) {
1649e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1650899cda47SBarry Smith     if (n != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16511ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1652416022c9SBarry Smith     v = a->a; jj = a->j;
165317ab2063SBarry Smith     for (i=0; i<nz; i++) {
1654bfeeae90SHong Zhang       (*v++) *= r[*jj++];
165517ab2063SBarry Smith     }
16561ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1657efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
165817ab2063SBarry Smith   }
16593a40ed3dSBarry Smith   PetscFunctionReturn(0);
166017ab2063SBarry Smith }
166117ab2063SBarry Smith 
16624a2ae208SSatish Balay #undef __FUNCT__
16634a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
166497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
166517ab2063SBarry Smith {
1666db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16676849ba73SBarry Smith   PetscErrorCode ierr;
1668899cda47SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap.n,*lens;
166997f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
167097f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
167197f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
167254f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1673416022c9SBarry Smith   Mat            C;
1674fee21e36SBarry Smith   PetscTruth     stride;
167517ab2063SBarry Smith 
16763a40ed3dSBarry Smith   PetscFunctionBegin;
1677d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
167829bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1679d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
168029bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
168199141d43SSatish Balay 
168217ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1683b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1684b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
168517ab2063SBarry Smith 
1686fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1687fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1688fee21e36SBarry Smith   if (stride && step == 1) {
168902834360SBarry Smith     /* special case of contiguous rows */
169097f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
169131ebf83bSSatish Balay     starts = lens + nrows;
169202834360SBarry Smith     /* loop over new rows determining lens and starting points */
169302834360SBarry Smith     for (i=0; i<nrows; i++) {
1694bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1695a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
169602834360SBarry Smith       for (k=kstart; k<kend; k++) {
1697bfeeae90SHong Zhang         if (aj[k] >= first) {
169802834360SBarry Smith           starts[i] = k;
169902834360SBarry Smith           break;
170002834360SBarry Smith 	}
170102834360SBarry Smith       }
1702a2744918SBarry Smith       sum = 0;
170302834360SBarry Smith       while (k < kend) {
1704bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1705a2744918SBarry Smith         sum++;
170602834360SBarry Smith       }
1707a2744918SBarry Smith       lens[i] = sum;
170802834360SBarry Smith     }
170902834360SBarry Smith     /* create submatrix */
1710cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
171197f1f81fSBarry Smith       PetscInt n_cols,n_rows;
171208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
171329bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1714d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
171508480c60SBarry Smith       C = *B;
17163a40ed3dSBarry Smith     } else {
17177adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1718f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17197adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1720ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
172108480c60SBarry Smith     }
1722db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1723db02288aSLois Curfman McInnes 
172402834360SBarry Smith     /* loop over rows inserting into submatrix */
1725db02288aSLois Curfman McInnes     a_new    = c->a;
1726db02288aSLois Curfman McInnes     j_new    = c->j;
1727db02288aSLois Curfman McInnes     i_new    = c->i;
1728bfeeae90SHong Zhang 
172902834360SBarry Smith     for (i=0; i<nrows; i++) {
1730a2744918SBarry Smith       ii    = starts[i];
1731a2744918SBarry Smith       lensi = lens[i];
1732a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1733a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
173402834360SBarry Smith       }
173587828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1736a2744918SBarry Smith       a_new      += lensi;
1737a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1738a2744918SBarry Smith       c->ilen[i]  = lensi;
173902834360SBarry Smith     }
1740606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17413a40ed3dSBarry Smith   } else {
174202834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
174397f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1744bfeeae90SHong Zhang 
174597f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
174697f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
174717ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
174802834360SBarry Smith     /* determine lens of each row */
174902834360SBarry Smith     for (i=0; i<nrows; i++) {
1750bfeeae90SHong Zhang       kstart  = ai[irow[i]];
175102834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
175202834360SBarry Smith       lens[i] = 0;
175302834360SBarry Smith       for (k=kstart; k<kend; k++) {
1754bfeeae90SHong Zhang         if (smap[aj[k]]) {
175502834360SBarry Smith           lens[i]++;
175602834360SBarry Smith         }
175702834360SBarry Smith       }
175802834360SBarry Smith     }
175917ab2063SBarry Smith     /* Create and fill new matrix */
1760a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17610f5bd95cSBarry Smith       PetscTruth equal;
17620f5bd95cSBarry Smith 
176399141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1764899cda47SBarry Smith       if ((*B)->rmap.n  != nrows || (*B)->cmap.n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1765899cda47SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap.n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17660f5bd95cSBarry Smith       if (!equal) {
176729bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
176899141d43SSatish Balay       }
1769899cda47SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap.n*sizeof(PetscInt));CHKERRQ(ierr);
177008480c60SBarry Smith       C = *B;
17713a40ed3dSBarry Smith     } else {
17727adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1773f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17747adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1775ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
177608480c60SBarry Smith     }
177799141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
177817ab2063SBarry Smith     for (i=0; i<nrows; i++) {
177999141d43SSatish Balay       row    = irow[i];
1780bfeeae90SHong Zhang       kstart = ai[row];
178199141d43SSatish Balay       kend   = kstart + a->ilen[row];
1782bfeeae90SHong Zhang       mat_i  = c->i[i];
178399141d43SSatish Balay       mat_j  = c->j + mat_i;
178499141d43SSatish Balay       mat_a  = c->a + mat_i;
178599141d43SSatish Balay       mat_ilen = c->ilen + i;
178617ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1787bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1788ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
178999141d43SSatish Balay           *mat_a++ = a->a[k];
179099141d43SSatish Balay           (*mat_ilen)++;
179199141d43SSatish Balay 
179217ab2063SBarry Smith         }
179317ab2063SBarry Smith       }
179417ab2063SBarry Smith     }
179502834360SBarry Smith     /* Free work space */
179602834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1797606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1798606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
179902834360SBarry Smith   }
18006d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18016d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
180217ab2063SBarry Smith 
180317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1804416022c9SBarry Smith   *B = C;
18053a40ed3dSBarry Smith   PetscFunctionReturn(0);
180617ab2063SBarry Smith }
180717ab2063SBarry Smith 
1808a871dcd8SBarry Smith /*
1809a871dcd8SBarry Smith */
18104a2ae208SSatish Balay #undef __FUNCT__
18114a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1812dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1813a871dcd8SBarry Smith {
181463b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1815dfbe8321SBarry Smith   PetscErrorCode ierr;
181663b91edcSBarry Smith   Mat            outA;
1817b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
181863b91edcSBarry Smith 
18193a40ed3dSBarry Smith   PetscFunctionBegin;
1820d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1821b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1822b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1823a871dcd8SBarry Smith 
182463b91edcSBarry Smith   outA          = inA;
1825*5c9eb25fSBarry Smith   inA->factor   = MAT_FACTOR_LU;
1826c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1827c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row); CHKERRQ(ierr);}
1828c3122656SLisandro Dalcin   a->row = row;
1829c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1830c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col); CHKERRQ(ierr);}
1831c3122656SLisandro Dalcin   a->col = col;
183263b91edcSBarry Smith 
183336db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1834b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18354c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
183652e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1837f0ec6fceSSatish Balay 
183894a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1839899cda47SBarry Smith      ierr = PetscMalloc((inA->rmap.n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
18409518dbb4SMatthew Knepley      ierr = PetscLogObjectMemory(inA, (inA->rmap.n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
184194a9d846SBarry Smith   }
184263b91edcSBarry Smith 
1843f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1844137fb511SHong Zhang   if (row_identity && col_identity) {
1845af281ebdSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
1846137fb511SHong Zhang   } else {
1847137fb511SHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(inA,info,&outA);CHKERRQ(ierr);
1848137fb511SHong Zhang   }
18493a40ed3dSBarry Smith   PetscFunctionReturn(0);
1850a871dcd8SBarry Smith }
1851a871dcd8SBarry Smith 
1852d9eff348SSatish Balay #include "petscblaslapack.h"
18534a2ae208SSatish Balay #undef __FUNCT__
18544a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1855f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1856f0b747eeSBarry Smith {
1857f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1858f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1859efee365bSSatish Balay   PetscErrorCode ierr;
18600805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18613a40ed3dSBarry Smith 
18623a40ed3dSBarry Smith   PetscFunctionBegin;
1863f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1864efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18653a40ed3dSBarry Smith   PetscFunctionReturn(0);
1866f0b747eeSBarry Smith }
1867f0b747eeSBarry Smith 
18684a2ae208SSatish Balay #undef __FUNCT__
18694a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
187097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1871cddf8d76SBarry Smith {
1872dfbe8321SBarry Smith   PetscErrorCode ierr;
187397f1f81fSBarry Smith   PetscInt       i;
1874cddf8d76SBarry Smith 
18753a40ed3dSBarry Smith   PetscFunctionBegin;
1876cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1877b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1878cddf8d76SBarry Smith   }
1879cddf8d76SBarry Smith 
1880cddf8d76SBarry Smith   for (i=0; i<n; i++) {
18816a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1882cddf8d76SBarry Smith   }
18833a40ed3dSBarry Smith   PetscFunctionReturn(0);
1884cddf8d76SBarry Smith }
1885cddf8d76SBarry Smith 
18864a2ae208SSatish Balay #undef __FUNCT__
18874a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
188897f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
18894dcbc457SBarry Smith {
1890e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18916849ba73SBarry Smith   PetscErrorCode ierr;
189297f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
189397f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1894f1af5d2fSBarry Smith   PetscBT        table;
1895bbd702dbSSatish Balay 
18963a40ed3dSBarry Smith   PetscFunctionBegin;
1897899cda47SBarry Smith   m     = A->rmap.n;
1898e4d965acSSatish Balay   ai    = a->i;
1899bfeeae90SHong Zhang   aj    = a->j;
19008a047759SSatish Balay 
1901a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
190206763907SSatish Balay 
190397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19046831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
190506763907SSatish Balay 
1906e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1907b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1908e4d965acSSatish Balay     isz  = 0;
19096831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1910e4d965acSSatish Balay 
1911e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19124dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1913b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1914e4d965acSSatish Balay 
1915dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1916e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1917f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19184dcbc457SBarry Smith     }
191906763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
192006763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1921e4d965acSSatish Balay 
192204a348a9SBarry Smith     k = 0;
192304a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
192404a348a9SBarry Smith       n = isz;
192506763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1926e4d965acSSatish Balay         row   = nidx[k];
1927e4d965acSSatish Balay         start = ai[row];
1928e4d965acSSatish Balay         end   = ai[row+1];
192904a348a9SBarry Smith         for (l = start; l<end ; l++){
1930efb16452SHong Zhang           val = aj[l] ;
1931f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1932e4d965acSSatish Balay         }
1933e4d965acSSatish Balay       }
1934e4d965acSSatish Balay     }
1935029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1936e4d965acSSatish Balay   }
19376831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1938606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19393a40ed3dSBarry Smith   PetscFunctionReturn(0);
19404dcbc457SBarry Smith }
194117ab2063SBarry Smith 
19420513a670SBarry Smith /* -------------------------------------------------------------- */
19434a2ae208SSatish Balay #undef __FUNCT__
19444a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1945dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19460513a670SBarry Smith {
19470513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19486849ba73SBarry Smith   PetscErrorCode ierr;
1949899cda47SBarry Smith   PetscInt       i,nz,m = A->rmap.n,n = A->cmap.n,*col;
195097f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
195156cd22aeSBarry Smith   IS             icolp,irowp;
195297f1f81fSBarry Smith   PetscInt       *cwork;
195332ec9ce4SBarry Smith   PetscScalar    *vwork;
19540513a670SBarry Smith 
19553a40ed3dSBarry Smith   PetscFunctionBegin;
19564c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
195756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19584c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
195956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19600513a670SBarry Smith 
19610513a670SBarry Smith   /* determine lengths of permuted rows */
196297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19630513a670SBarry Smith   for (i=0; i<m; i++) {
19640513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19650513a670SBarry Smith   }
19667adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
1967f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
19687adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1969ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1970606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
19710513a670SBarry Smith 
197297f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
19730513a670SBarry Smith   for (i=0; i<m; i++) {
197432ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19750513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1976cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
197732ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19780513a670SBarry Smith   }
1979606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
19803c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
19810513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19820513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
198356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
198456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
198556cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
198656cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
19873a40ed3dSBarry Smith   PetscFunctionReturn(0);
19880513a670SBarry Smith }
19890513a670SBarry Smith 
19904a2ae208SSatish Balay #undef __FUNCT__
19914a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1992dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1993cb5b572fSBarry Smith {
1994dfbe8321SBarry Smith   PetscErrorCode ierr;
1995cb5b572fSBarry Smith 
1996cb5b572fSBarry Smith   PetscFunctionBegin;
199733f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
199833f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1999be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2000be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2001be6bf707SBarry Smith 
2002899cda47SBarry Smith     if (a->i[A->rmap.n] != b->i[B->rmap.n]) {
2003634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2004cb5b572fSBarry Smith     }
2005899cda47SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr);
2006cb5b572fSBarry Smith   } else {
2007cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2008cb5b572fSBarry Smith   }
2009cb5b572fSBarry Smith   PetscFunctionReturn(0);
2010cb5b572fSBarry Smith }
2011cb5b572fSBarry Smith 
20124a2ae208SSatish Balay #undef __FUNCT__
20134a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2014dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2015273d9f13SBarry Smith {
2016dfbe8321SBarry Smith   PetscErrorCode ierr;
2017273d9f13SBarry Smith 
2018273d9f13SBarry Smith   PetscFunctionBegin;
2019ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2020273d9f13SBarry Smith   PetscFunctionReturn(0);
2021273d9f13SBarry Smith }
2022273d9f13SBarry Smith 
20234a2ae208SSatish Balay #undef __FUNCT__
20244a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2025a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20266c0721eeSBarry Smith {
20276c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20286c0721eeSBarry Smith   PetscFunctionBegin;
20296c0721eeSBarry Smith   *array = a->a;
20306c0721eeSBarry Smith   PetscFunctionReturn(0);
20316c0721eeSBarry Smith }
20326c0721eeSBarry Smith 
20334a2ae208SSatish Balay #undef __FUNCT__
20344a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2035dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20366c0721eeSBarry Smith {
20376c0721eeSBarry Smith   PetscFunctionBegin;
20386c0721eeSBarry Smith   PetscFunctionReturn(0);
20396c0721eeSBarry Smith }
2040273d9f13SBarry Smith 
2041ee4f033dSBarry Smith #undef __FUNCT__
2042ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2043dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2044ee4f033dSBarry Smith {
20456849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20466849ba73SBarry Smith   PetscErrorCode ierr;
204797f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2048efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
204987828ca2SBarry Smith   PetscScalar    *vscale_array;
2050ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2051ee4f033dSBarry Smith   Vec            w1,w2,w3;
2052ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2053ee4f033dSBarry Smith   PetscTruth     flg;
2054ee4f033dSBarry Smith 
2055ee4f033dSBarry Smith   PetscFunctionBegin;
2056ee4f033dSBarry Smith   if (!coloring->w1) {
2057ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
205852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2059ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
206052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2061ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
206252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2063ee4f033dSBarry Smith   }
2064ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2065ee4f033dSBarry Smith 
2066ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
20677adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
2068ee4f033dSBarry Smith   if (flg) {
2069ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2070ee4f033dSBarry Smith   } else {
20710b9b6f31SBarry Smith     PetscTruth assembled;
20720b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
20730b9b6f31SBarry Smith     if (assembled) {
2074ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2075ee4f033dSBarry Smith     }
20760b9b6f31SBarry Smith   }
2077ee4f033dSBarry Smith 
2078ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2079ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2080ee4f033dSBarry Smith 
2081ee4f033dSBarry Smith   /*
2082ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2083ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2084ee4f033dSBarry Smith   */
2085ee4f033dSBarry Smith   if (coloring->F) {
2086ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2087ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2088ee4f033dSBarry Smith     if (m1 != m2) {
2089ee4f033dSBarry Smith       coloring->F = 0;
2090ee4f033dSBarry Smith     }
2091ee4f033dSBarry Smith   }
2092ee4f033dSBarry Smith 
2093ee4f033dSBarry Smith   if (coloring->F) {
2094ee4f033dSBarry Smith     w1          = coloring->F;
2095ee4f033dSBarry Smith     coloring->F = 0;
2096ee4f033dSBarry Smith   } else {
209766f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2098ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
209966f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2100ee4f033dSBarry Smith   }
2101ee4f033dSBarry Smith 
2102ee4f033dSBarry Smith   /*
2103ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2104ee4f033dSBarry Smith   */
21051ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21061ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2107ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2108ee4f033dSBarry Smith     /*
2109ee4f033dSBarry Smith        Loop over each column associated with color adding the
2110ee4f033dSBarry Smith        perturbation to the vector w3.
2111ee4f033dSBarry Smith     */
2112ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2113ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2114ee4f033dSBarry Smith       dx  = xx[col];
2115ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2116ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2117ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2118ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2119ee4f033dSBarry Smith #else
2120ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2121ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2122ee4f033dSBarry Smith #endif
2123ee4f033dSBarry Smith       dx                *= epsilon;
2124ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2125ee4f033dSBarry Smith     }
2126ee4f033dSBarry Smith   }
21271ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2128ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2129ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2130ee4f033dSBarry Smith 
2131ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2132ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2133ee4f033dSBarry Smith 
2134ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2135ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2136ee4f033dSBarry Smith 
21371ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2138ee4f033dSBarry Smith   /*
2139ee4f033dSBarry Smith       Loop over each color
2140ee4f033dSBarry Smith   */
2141ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
214249b058dcSBarry Smith     coloring->currentcolor = k;
2143ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21441ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2145ee4f033dSBarry Smith     /*
2146ee4f033dSBarry Smith        Loop over each column associated with color adding the
2147ee4f033dSBarry Smith        perturbation to the vector w3.
2148ee4f033dSBarry Smith     */
2149ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2150ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2151ee4f033dSBarry Smith       dx  = xx[col];
21525b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2153ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2154ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2155ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2156ee4f033dSBarry Smith #else
2157ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2158ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2159ee4f033dSBarry Smith #endif
2160ee4f033dSBarry Smith       dx            *= epsilon;
2161634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2162ee4f033dSBarry Smith       w3_array[col] += dx;
2163ee4f033dSBarry Smith     }
21641ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2165ee4f033dSBarry Smith 
2166ee4f033dSBarry Smith     /*
2167ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2168ee4f033dSBarry Smith     */
2169ee4f033dSBarry Smith 
217066f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2171ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
217266f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2173efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2174ee4f033dSBarry Smith 
2175ee4f033dSBarry Smith     /*
2176ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2177ee4f033dSBarry Smith     */
21781ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2179ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2180ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2181ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2182ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2183ee4f033dSBarry Smith       srow   = row + start;
2184ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2185ee4f033dSBarry Smith     }
21861ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2187ee4f033dSBarry Smith   }
218849b058dcSBarry Smith   coloring->currentcolor = k;
21891ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
21901ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2191ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2192ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2193ee4f033dSBarry Smith   PetscFunctionReturn(0);
2194ee4f033dSBarry Smith }
2195ee4f033dSBarry Smith 
2196ac90fabeSBarry Smith #include "petscblaslapack.h"
2197ac90fabeSBarry Smith #undef __FUNCT__
2198ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2199f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2200ac90fabeSBarry Smith {
2201dfbe8321SBarry Smith   PetscErrorCode ierr;
220297f1f81fSBarry Smith   PetscInt       i;
2203ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22040805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2205ac90fabeSBarry Smith 
2206ac90fabeSBarry Smith   PetscFunctionBegin;
2207ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2208f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2209f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2210c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2211a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2212a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2213a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2214a30b2313SHong Zhang     }
2215a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2216899cda47SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap.n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2217a30b2313SHong Zhang       y->XtoY = X;
2218407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2219c537a176SHong Zhang     }
2220f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22211e2582c4SBarry 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);
2222ac90fabeSBarry Smith   } else {
2223f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2224ac90fabeSBarry Smith   }
2225ac90fabeSBarry Smith   PetscFunctionReturn(0);
2226ac90fabeSBarry Smith }
2227ac90fabeSBarry Smith 
2228521d7252SBarry Smith #undef __FUNCT__
2229521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2230521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2231521d7252SBarry Smith {
2232521d7252SBarry Smith   PetscFunctionBegin;
2233521d7252SBarry Smith   PetscFunctionReturn(0);
2234521d7252SBarry Smith }
2235521d7252SBarry Smith 
2236354c94deSBarry Smith #undef __FUNCT__
2237354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2238354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2239354c94deSBarry Smith {
2240354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2241354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2242354c94deSBarry Smith   PetscInt    i,nz;
2243354c94deSBarry Smith   PetscScalar *a;
2244354c94deSBarry Smith 
2245354c94deSBarry Smith   PetscFunctionBegin;
2246354c94deSBarry Smith   nz = aij->nz;
2247354c94deSBarry Smith   a  = aij->a;
2248354c94deSBarry Smith   for (i=0; i<nz; i++) {
2249354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2250354c94deSBarry Smith   }
2251354c94deSBarry Smith #else
2252354c94deSBarry Smith   PetscFunctionBegin;
2253354c94deSBarry Smith #endif
2254354c94deSBarry Smith   PetscFunctionReturn(0);
2255354c94deSBarry Smith }
2256354c94deSBarry Smith 
2257e34fafa9SBarry Smith #undef __FUNCT__
2258985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2259985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2260e34fafa9SBarry Smith {
2261e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2262e34fafa9SBarry Smith   PetscErrorCode ierr;
2263899cda47SBarry Smith   PetscInt       i,j,m = A->rmap.n,*ai,*aj,ncols,n;
2264e34fafa9SBarry Smith   PetscReal      atmp;
2265985db425SBarry Smith   PetscScalar    *x;
2266e34fafa9SBarry Smith   MatScalar      *aa;
2267e34fafa9SBarry Smith 
2268e34fafa9SBarry Smith   PetscFunctionBegin;
2269e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2270e34fafa9SBarry Smith   aa   = a->a;
2271e34fafa9SBarry Smith   ai   = a->i;
2272e34fafa9SBarry Smith   aj   = a->j;
2273e34fafa9SBarry Smith 
2274985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2275e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2276e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2277899cda47SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2278e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2279e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
22809189402eSHong Zhang     x[i] = 0.0;
2281e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2282985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2283985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2284985db425SBarry Smith       aa++; aj++;
2285985db425SBarry Smith     }
2286985db425SBarry Smith   }
2287985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2288985db425SBarry Smith   PetscFunctionReturn(0);
2289985db425SBarry Smith }
2290985db425SBarry Smith 
2291985db425SBarry Smith #undef __FUNCT__
2292985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2293985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2294985db425SBarry Smith {
2295985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2296985db425SBarry Smith   PetscErrorCode ierr;
2297985db425SBarry Smith   PetscInt       i,j,m = A->rmap.n,*ai,*aj,ncols,n;
2298985db425SBarry Smith   PetscScalar    *x;
2299985db425SBarry Smith   MatScalar      *aa;
2300985db425SBarry Smith 
2301985db425SBarry Smith   PetscFunctionBegin;
2302985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2303985db425SBarry Smith   aa   = a->a;
2304985db425SBarry Smith   ai   = a->i;
2305985db425SBarry Smith   aj   = a->j;
2306985db425SBarry Smith 
2307985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2308985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2309985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2310985db425SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2311985db425SBarry Smith   for (i=0; i<m; i++) {
2312985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2313985db425SBarry Smith     if (ncols == A->cmap.n) { /* row is dense */
2314985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2315985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2316985db425SBarry Smith       x[i] = 0.0;
2317985db425SBarry Smith       if (idx) {
2318985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2319985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2320985db425SBarry Smith           if (aj[j] > j) {
2321985db425SBarry Smith             idx[i] = j;
2322985db425SBarry Smith             break;
2323985db425SBarry Smith           }
2324985db425SBarry Smith         }
2325985db425SBarry Smith       }
2326985db425SBarry Smith     }
2327985db425SBarry Smith     for (j=0; j<ncols; j++){
2328985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2329985db425SBarry Smith       aa++; aj++;
2330985db425SBarry Smith     }
2331985db425SBarry Smith   }
2332985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2333985db425SBarry Smith   PetscFunctionReturn(0);
2334985db425SBarry Smith }
2335985db425SBarry Smith 
2336985db425SBarry Smith #undef __FUNCT__
2337985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2338985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2339985db425SBarry Smith {
2340985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2341985db425SBarry Smith   PetscErrorCode ierr;
2342985db425SBarry Smith   PetscInt       i,j,m = A->rmap.n,*ai,*aj,ncols,n;
2343985db425SBarry Smith   PetscScalar    *x;
2344985db425SBarry Smith   MatScalar      *aa;
2345985db425SBarry Smith 
2346985db425SBarry Smith   PetscFunctionBegin;
2347985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2348985db425SBarry Smith   aa   = a->a;
2349985db425SBarry Smith   ai   = a->i;
2350985db425SBarry Smith   aj   = a->j;
2351985db425SBarry Smith 
2352985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2353985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2354985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2355985db425SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2356985db425SBarry Smith   for (i=0; i<m; i++) {
2357985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2358985db425SBarry Smith     if (ncols == A->cmap.n) { /* row is dense */
2359985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2360985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2361985db425SBarry Smith       x[i] = 0.0;
2362985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2363985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2364985db425SBarry Smith         for (j=0;j<ncols;j++) {
2365985db425SBarry Smith           if (aj[j] > j) {
2366985db425SBarry Smith             idx[i] = j;
2367985db425SBarry Smith             break;
2368985db425SBarry Smith           }
2369985db425SBarry Smith         }
2370985db425SBarry Smith       }
2371985db425SBarry Smith     }
2372985db425SBarry Smith     for (j=0; j<ncols; j++){
2373985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2374985db425SBarry Smith       aa++; aj++;
2375e34fafa9SBarry Smith     }
2376e34fafa9SBarry Smith   }
2377e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2378e34fafa9SBarry Smith   PetscFunctionReturn(0);
2379e34fafa9SBarry Smith }
2380e34fafa9SBarry Smith 
2381682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
23820a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2383cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2384cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2385cb5b572fSBarry Smith        MatMult_SeqAIJ,
238697304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
23877c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
23887c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2389cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2390cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
23917c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
239297304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2393cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2394cb5b572fSBarry Smith        0,
239517ab2063SBarry Smith        MatRelax_SeqAIJ,
239617ab2063SBarry Smith        MatTranspose_SeqAIJ,
239797304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2398cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2399cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2400cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2401cb5b572fSBarry Smith        MatNorm_SeqAIJ,
240297304618SKris Buschelman /*20*/ 0,
2403cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
240417ab2063SBarry Smith        MatCompress_SeqAIJ,
2405cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2406cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
240797304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2408cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2409cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2410f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2411a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
241297304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2413cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2414861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
24156c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24166c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
241797304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2418cb5b572fSBarry Smith        0,
2419cb5b572fSBarry Smith        0,
2420cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2421cb5b572fSBarry Smith        0,
242297304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2423cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2424cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2425cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2426cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2427985db425SBarry Smith /*45*/ MatGetRowMax_SeqAIJ,
2428cb5b572fSBarry Smith        MatScale_SeqAIJ,
2429cb5b572fSBarry Smith        0,
243079299369SBarry Smith        MatDiagonalSet_SeqAIJ,
24316945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2432521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
24333b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
24343b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
24353b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2436a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
243797304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2438b9617806SBarry Smith        0,
24390513a670SBarry Smith        0,
2440cda55fadSBarry Smith        MatPermute_SeqAIJ,
2441cda55fadSBarry Smith        0,
244297304618SKris Buschelman /*60*/ 0,
2443b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2444b9b97703SBarry Smith        MatView_SeqAIJ,
2445357abbc8SBarry Smith        0,
2446ee4f033dSBarry Smith        0,
244797304618SKris Buschelman /*65*/ 0,
2448ee4f033dSBarry Smith        0,
2449ee4f033dSBarry Smith        0,
2450ee4f033dSBarry Smith        0,
2451ee4f033dSBarry Smith        0,
2452985db425SBarry Smith /*70*/ MatGetRowMaxAbs_SeqAIJ,
2453ee4f033dSBarry Smith        0,
2454ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2455dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2456ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2457dcf5cc72SBarry Smith #else
2458dcf5cc72SBarry Smith        0,
2459dcf5cc72SBarry Smith #endif
2460ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2461b8f8c88eSHong Zhang /*75*/ 0,
246297304618SKris Buschelman        0,
246397304618SKris Buschelman        0,
246497304618SKris Buschelman        0,
246597304618SKris Buschelman        0,
246697304618SKris Buschelman /*80*/ 0,
246797304618SKris Buschelman        0,
246897304618SKris Buschelman        0,
246997304618SKris Buschelman        0,
2470bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2471bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
24721cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
24736284ec50SHong Zhang        0,
24746284ec50SHong Zhang        0,
2475bc011b1eSHong Zhang        0,
2476bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
247726be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
247826be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2479d439da42SKris Buschelman        MatPtAP_Basic,
24807ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
24817ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2482bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2483bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2484bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
24857ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
24867ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2487609c6c4dSKris Buschelman        0,
2488609c6c4dSKris Buschelman        0,
248987d4246cSBarry Smith        MatConjugate_SeqAIJ,
249087d4246cSBarry Smith        0,
249199cafbc1SBarry Smith /*105*/MatSetValuesRow_SeqAIJ,
249299cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2493f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2494f5edf698SHong Zhang        0,
24952bebee5dSHong Zhang        0,
2496985db425SBarry Smith /*110*/MatMatSolve_SeqAIJ,
2497985db425SBarry Smith        0,
24982af78befSBarry Smith        MatGetRowMin_SeqAIJ,
24992af78befSBarry Smith        0,
25002af78befSBarry Smith        MatMissingDiagonal_SeqAIJ
25019e29f15eSvictorle };
250217ab2063SBarry Smith 
2503fb2e594dSBarry Smith EXTERN_C_BEGIN
25044a2ae208SSatish Balay #undef __FUNCT__
25054a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2506be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2507bef8e0ddSBarry Smith {
2508bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
250997f1f81fSBarry Smith   PetscInt   i,nz,n;
2510bef8e0ddSBarry Smith 
2511bef8e0ddSBarry Smith   PetscFunctionBegin;
2512bef8e0ddSBarry Smith 
2513bef8e0ddSBarry Smith   nz = aij->maxnz;
251411c9b30cSMatthew Knepley   n  = mat->rmap.n;
2515bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2516bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2517bef8e0ddSBarry Smith   }
2518bef8e0ddSBarry Smith   aij->nz = nz;
2519bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2520bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2521bef8e0ddSBarry Smith   }
2522bef8e0ddSBarry Smith 
2523bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2524bef8e0ddSBarry Smith }
2525fb2e594dSBarry Smith EXTERN_C_END
2526bef8e0ddSBarry Smith 
25274a2ae208SSatish Balay #undef __FUNCT__
25284a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2529bef8e0ddSBarry Smith /*@
2530bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2531bef8e0ddSBarry Smith        in the matrix.
2532bef8e0ddSBarry Smith 
2533bef8e0ddSBarry Smith   Input Parameters:
2534bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2535bef8e0ddSBarry Smith -  indices - the column indices
2536bef8e0ddSBarry Smith 
253715091d37SBarry Smith   Level: advanced
253815091d37SBarry Smith 
2539bef8e0ddSBarry Smith   Notes:
2540bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2541bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2542bef8e0ddSBarry Smith   of the MatSetValues() operation.
2543bef8e0ddSBarry Smith 
2544bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2545d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2546bef8e0ddSBarry Smith 
2547bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2548bef8e0ddSBarry Smith 
2549b9617806SBarry Smith     The indices should start with zero, not one.
2550b9617806SBarry Smith 
2551bef8e0ddSBarry Smith @*/
2552be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2553bef8e0ddSBarry Smith {
255497f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2555bef8e0ddSBarry Smith 
2556bef8e0ddSBarry Smith   PetscFunctionBegin;
25574482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
25584482741eSBarry Smith   PetscValidPointer(indices,2);
2559c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2560bef8e0ddSBarry Smith   if (f) {
2561bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2562bef8e0ddSBarry Smith   } else {
2563634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2564bef8e0ddSBarry Smith   }
2565bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2566bef8e0ddSBarry Smith }
2567bef8e0ddSBarry Smith 
2568be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2569be6bf707SBarry Smith 
2570fb2e594dSBarry Smith EXTERN_C_BEGIN
25714a2ae208SSatish Balay #undef __FUNCT__
25724a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2573be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2574be6bf707SBarry Smith {
2575be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
25766849ba73SBarry Smith   PetscErrorCode ierr;
2577899cda47SBarry Smith   size_t         nz = aij->i[mat->rmap.n];
2578be6bf707SBarry Smith 
2579be6bf707SBarry Smith   PetscFunctionBegin;
2580be6bf707SBarry Smith   if (aij->nonew != 1) {
2581512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2582be6bf707SBarry Smith   }
2583be6bf707SBarry Smith 
2584be6bf707SBarry Smith   /* allocate space for values if not already there */
2585be6bf707SBarry Smith   if (!aij->saved_values) {
258687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
25879518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2588be6bf707SBarry Smith   }
2589be6bf707SBarry Smith 
2590be6bf707SBarry Smith   /* copy values over */
259187828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2592be6bf707SBarry Smith   PetscFunctionReturn(0);
2593be6bf707SBarry Smith }
2594fb2e594dSBarry Smith EXTERN_C_END
2595be6bf707SBarry Smith 
25964a2ae208SSatish Balay #undef __FUNCT__
2597b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2598be6bf707SBarry Smith /*@
2599be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2600be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2601be6bf707SBarry Smith        nonlinear portion.
2602be6bf707SBarry Smith 
2603be6bf707SBarry Smith    Collect on Mat
2604be6bf707SBarry Smith 
2605be6bf707SBarry Smith   Input Parameters:
26060e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2607be6bf707SBarry Smith 
260815091d37SBarry Smith   Level: advanced
260915091d37SBarry Smith 
2610be6bf707SBarry Smith   Common Usage, with SNESSolve():
2611be6bf707SBarry Smith $    Create Jacobian matrix
2612be6bf707SBarry Smith $    Set linear terms into matrix
2613be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2614be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2615be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2616512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2617be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2618be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2619be6bf707SBarry Smith $    In your Jacobian routine
2620be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2621be6bf707SBarry Smith $      Set nonlinear terms in matrix
2622be6bf707SBarry Smith 
2623be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2624be6bf707SBarry Smith $    // build linear portion of Jacobian
2625512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2626be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2627be6bf707SBarry Smith $    loop over nonlinear iterations
2628be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2629be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2630be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2631be6bf707SBarry Smith $       Solve linear system with Jacobian
2632be6bf707SBarry Smith $    endloop
2633be6bf707SBarry Smith 
2634be6bf707SBarry Smith   Notes:
2635be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2636512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2637be6bf707SBarry Smith     calling this routine.
2638be6bf707SBarry Smith 
26390c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
26400c468ba9SBarry Smith     and does not allocated additional space.
26410c468ba9SBarry Smith 
2642be6bf707SBarry Smith .seealso: MatRetrieveValues()
2643be6bf707SBarry Smith 
2644be6bf707SBarry Smith @*/
2645be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2646be6bf707SBarry Smith {
2647dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2648be6bf707SBarry Smith 
2649be6bf707SBarry Smith   PetscFunctionBegin;
26504482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
265129bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
265229bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2653be6bf707SBarry Smith 
2654c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2655be6bf707SBarry Smith   if (f) {
2656be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2657be6bf707SBarry Smith   } else {
2658634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2659be6bf707SBarry Smith   }
2660be6bf707SBarry Smith   PetscFunctionReturn(0);
2661be6bf707SBarry Smith }
2662be6bf707SBarry Smith 
2663fb2e594dSBarry Smith EXTERN_C_BEGIN
26644a2ae208SSatish Balay #undef __FUNCT__
26654a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2666be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2667be6bf707SBarry Smith {
2668be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26696849ba73SBarry Smith   PetscErrorCode ierr;
2670899cda47SBarry Smith   PetscInt       nz = aij->i[mat->rmap.n];
2671be6bf707SBarry Smith 
2672be6bf707SBarry Smith   PetscFunctionBegin;
2673be6bf707SBarry Smith   if (aij->nonew != 1) {
2674512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2675be6bf707SBarry Smith   }
2676be6bf707SBarry Smith   if (!aij->saved_values) {
2677634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2678be6bf707SBarry Smith   }
2679be6bf707SBarry Smith   /* copy values over */
268087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2681be6bf707SBarry Smith   PetscFunctionReturn(0);
2682be6bf707SBarry Smith }
2683fb2e594dSBarry Smith EXTERN_C_END
2684be6bf707SBarry Smith 
26854a2ae208SSatish Balay #undef __FUNCT__
26864a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2687be6bf707SBarry Smith /*@
2688be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2689be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2690be6bf707SBarry Smith        nonlinear portion.
2691be6bf707SBarry Smith 
2692be6bf707SBarry Smith    Collect on Mat
2693be6bf707SBarry Smith 
2694be6bf707SBarry Smith   Input Parameters:
2695be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2696be6bf707SBarry Smith 
269715091d37SBarry Smith   Level: advanced
269815091d37SBarry Smith 
2699be6bf707SBarry Smith .seealso: MatStoreValues()
2700be6bf707SBarry Smith 
2701be6bf707SBarry Smith @*/
2702be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2703be6bf707SBarry Smith {
2704dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2705be6bf707SBarry Smith 
2706be6bf707SBarry Smith   PetscFunctionBegin;
27074482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
270829bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
270929bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2710be6bf707SBarry Smith 
2711c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2712be6bf707SBarry Smith   if (f) {
2713be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2714be6bf707SBarry Smith   } else {
2715634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2716be6bf707SBarry Smith   }
2717be6bf707SBarry Smith   PetscFunctionReturn(0);
2718be6bf707SBarry Smith }
2719be6bf707SBarry Smith 
2720f83d6046SBarry Smith 
2721be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
27224a2ae208SSatish Balay #undef __FUNCT__
27234a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
272417ab2063SBarry Smith /*@C
2725682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
27260d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
27276e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
272851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
27292bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
273017ab2063SBarry Smith 
2731db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2732db81eaa0SLois Curfman McInnes 
273317ab2063SBarry Smith    Input Parameters:
2734db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
273517ab2063SBarry Smith .  m - number of rows
273617ab2063SBarry Smith .  n - number of columns
273717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
273851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
27392bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
274017ab2063SBarry Smith 
274117ab2063SBarry Smith    Output Parameter:
2742416022c9SBarry Smith .  A - the matrix
274317ab2063SBarry Smith 
2744175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2745175b88e8SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly. This is definitely
2746175b88e8SBarry Smith    true if you plan to use the external direct solvers such as SuperLU, MUMPS or Spooles.
2747175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2748175b88e8SBarry Smith 
2749b259b22eSLois Curfman McInnes    Notes:
275049a6f317SBarry Smith    If nnz is given then nz is ignored
275149a6f317SBarry Smith 
275217ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
275317ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
27540002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
275544cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
275617ab2063SBarry Smith 
275717ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2758a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
27593d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
27606da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
276117ab2063SBarry Smith 
2762682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
27634fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2764682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
27656c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
27666c7ebb05SLois Curfman McInnes 
27676c7ebb05SLois Curfman McInnes    Options Database Keys:
2768698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2769698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2770db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2771db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2772db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
277317ab2063SBarry Smith 
2774027ccd11SLois Curfman McInnes    Level: intermediate
2775027ccd11SLois Curfman McInnes 
277636db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
277736db0b34SBarry Smith 
277817ab2063SBarry Smith @*/
2779be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
278017ab2063SBarry Smith {
2781dfbe8321SBarry Smith   PetscErrorCode ierr;
27826945ee14SBarry Smith 
27833a40ed3dSBarry Smith   PetscFunctionBegin;
2784f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2785f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2786273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2787ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2788273d9f13SBarry Smith   PetscFunctionReturn(0);
2789273d9f13SBarry Smith }
2790273d9f13SBarry Smith 
27914a2ae208SSatish Balay #undef __FUNCT__
27924a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2793273d9f13SBarry Smith /*@C
2794273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2795273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2796273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2797273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2798273d9f13SBarry Smith 
2799273d9f13SBarry Smith    Collective on MPI_Comm
2800273d9f13SBarry Smith 
2801273d9f13SBarry Smith    Input Parameters:
280245bfc511SMatthew Knepley +  B - The matrix
2803273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2804273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2805273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2806273d9f13SBarry Smith 
2807273d9f13SBarry Smith    Notes:
280849a6f317SBarry Smith      If nnz is given then nz is ignored
280949a6f317SBarry Smith 
2810273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2811273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2812273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2813273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2814273d9f13SBarry Smith 
2815273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2816273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2817273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2818273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2819273d9f13SBarry Smith 
2820aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2821aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2822aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2823aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2824aa95bbe8SBarry Smith 
2825a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2826a96a251dSBarry Smith    entries or columns indices
2827a96a251dSBarry Smith 
2828273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2829273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2830273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2831273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2832273d9f13SBarry Smith 
2833273d9f13SBarry Smith    Options Database Keys:
2834698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2835698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2836273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2837273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2838273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2839273d9f13SBarry Smith 
2840273d9f13SBarry Smith    Level: intermediate
2841273d9f13SBarry Smith 
2842aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2843273d9f13SBarry Smith 
2844273d9f13SBarry Smith @*/
2845be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2846273d9f13SBarry Smith {
284797f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2848a23d5eceSKris Buschelman 
2849a23d5eceSKris Buschelman   PetscFunctionBegin;
2850a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2851a23d5eceSKris Buschelman   if (f) {
2852a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2853a23d5eceSKris Buschelman   }
2854a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2855a23d5eceSKris Buschelman }
2856a23d5eceSKris Buschelman 
2857a23d5eceSKris Buschelman EXTERN_C_BEGIN
2858a23d5eceSKris Buschelman #undef __FUNCT__
2859a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2860be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2861a23d5eceSKris Buschelman {
2862273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2863a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
28646849ba73SBarry Smith   PetscErrorCode ierr;
286597f1f81fSBarry Smith   PetscInt       i;
2866273d9f13SBarry Smith 
2867273d9f13SBarry Smith   PetscFunctionBegin;
2868d5d45c9bSBarry Smith 
2869a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2870c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2871c461c341SBarry Smith     nz             = 0;
2872c461c341SBarry Smith   }
2873c461c341SBarry Smith 
2874899cda47SBarry Smith   B->rmap.bs = B->cmap.bs = 1;
28756148ca0dSBarry Smith   ierr = PetscMapSetUp(&B->rmap);CHKERRQ(ierr);
28766148ca0dSBarry Smith   ierr = PetscMapSetUp(&B->cmap);CHKERRQ(ierr);
2877899cda47SBarry Smith 
2878435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2879435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2880b73539f3SBarry Smith   if (nnz) {
2881899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) {
288229bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2883899cda47SBarry 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);
2884b73539f3SBarry Smith     }
2885b73539f3SBarry Smith   }
2886b73539f3SBarry Smith 
2887273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2888273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2889273d9f13SBarry Smith 
2890ab93d7beSBarry Smith   if (!skipallocation) {
28912ee49352SLisandro Dalcin     if (!b->imax) {
2892899cda47SBarry Smith       ierr = PetscMalloc2(B->rmap.n,PetscInt,&b->imax,B->rmap.n,PetscInt,&b->ilen);CHKERRQ(ierr);
28939518dbb4SMatthew Knepley       ierr = PetscLogObjectMemory(B,2*B->rmap.n*sizeof(PetscInt));CHKERRQ(ierr);
28942ee49352SLisandro Dalcin     }
2895273d9f13SBarry Smith     if (!nnz) {
2896435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2897273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2898899cda47SBarry Smith       for (i=0; i<B->rmap.n; i++) b->imax[i] = nz;
2899899cda47SBarry Smith       nz = nz*B->rmap.n;
2900273d9f13SBarry Smith     } else {
2901273d9f13SBarry Smith       nz = 0;
2902899cda47SBarry Smith       for (i=0; i<B->rmap.n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2903273d9f13SBarry Smith     }
2904ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2905899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) { b->ilen[i] = 0; }
2906ab93d7beSBarry Smith 
2907273d9f13SBarry Smith     /* allocate the matrix space */
29082ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2909899cda47SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap.n+1,PetscInt,&b->i);CHKERRQ(ierr);
29109518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(B,(B->rmap.n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2911bfeeae90SHong Zhang     b->i[0] = 0;
2912899cda47SBarry Smith     for (i=1; i<B->rmap.n+1; i++) {
29135da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29145da197adSKris Buschelman     }
2915273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2916e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2917e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2918c461c341SBarry Smith   } else {
2919e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2920e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2921c461c341SBarry Smith   }
2922273d9f13SBarry Smith 
2923273d9f13SBarry Smith   b->nz                = 0;
2924273d9f13SBarry Smith   b->maxnz             = nz;
2925273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2926273d9f13SBarry Smith   PetscFunctionReturn(0);
2927273d9f13SBarry Smith }
2928a23d5eceSKris Buschelman EXTERN_C_END
2929273d9f13SBarry Smith 
2930a1661176SMatthew Knepley #undef  __FUNCT__
2931a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
293258d36128SBarry Smith /*@
2933a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2934a1661176SMatthew Knepley 
2935a1661176SMatthew Knepley    Input Parameters:
2936a1661176SMatthew Knepley +  B - the matrix
2937a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2938a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2939a1661176SMatthew Knepley -  v - optional values in the matrix
2940a1661176SMatthew Knepley 
2941d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
2942d58b2322SMatthew Knepley 
2943a1661176SMatthew Knepley    Level: developer
2944a1661176SMatthew Knepley 
294558d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
294658d36128SBarry Smith 
2947a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2948a1661176SMatthew Knepley 
2949a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
2950a1661176SMatthew Knepley @*/
2951a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
2952a1661176SMatthew Knepley {
2953a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2954a1661176SMatthew Knepley   PetscErrorCode ierr;
2955a1661176SMatthew Knepley 
2956a1661176SMatthew Knepley   PetscFunctionBegin;
2957a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
2958a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2959a1661176SMatthew Knepley   if (f) {
2960a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2961a1661176SMatthew Knepley   }
2962a1661176SMatthew Knepley   PetscFunctionReturn(0);
2963a1661176SMatthew Knepley }
2964a1661176SMatthew Knepley 
2965a1661176SMatthew Knepley EXTERN_C_BEGIN
2966a1661176SMatthew Knepley #undef  __FUNCT__
2967a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
2968b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
2969a1661176SMatthew Knepley {
2970a1661176SMatthew Knepley   PetscInt       i;
2971a1661176SMatthew Knepley   PetscInt       m,n;
2972a1661176SMatthew Knepley   PetscInt       nz;
2973a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
2974a1661176SMatthew Knepley   PetscScalar    *values;
2975a1661176SMatthew Knepley   PetscErrorCode ierr;
2976a1661176SMatthew Knepley 
2977a1661176SMatthew Knepley   PetscFunctionBegin;
2978a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
2979a1661176SMatthew Knepley 
2980b7940d39SSatish Balay   if (Ii[0]) {
2981b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
2982a1661176SMatthew Knepley   }
2983a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
2984a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2985b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
2986a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
2987a1661176SMatthew Knepley     if (nz < 0) {
2988a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
2989a1661176SMatthew Knepley     }
2990a1661176SMatthew Knepley     nnz[i] = nz;
2991a1661176SMatthew Knepley   }
2992a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
2993a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
2994a1661176SMatthew Knepley 
2995a1661176SMatthew Knepley   if (v) {
2996a1661176SMatthew Knepley     values = (PetscScalar*) v;
2997a1661176SMatthew Knepley   } else {
2998a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
2999a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3000a1661176SMatthew Knepley   }
3001a1661176SMatthew Knepley 
3002a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3003b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3004b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3005a1661176SMatthew Knepley   }
3006a1661176SMatthew Knepley 
3007a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3008a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3009a1661176SMatthew Knepley 
3010a1661176SMatthew Knepley   if (!v) {
3011a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3012a1661176SMatthew Knepley   }
3013a1661176SMatthew Knepley   PetscFunctionReturn(0);
3014a1661176SMatthew Knepley }
3015a1661176SMatthew Knepley EXTERN_C_END
3016a1661176SMatthew Knepley 
3017170fe5c8SBarry Smith #include "src/mat/impls/dense/seq/dense.h"
30181de00fd4SBarry Smith #include "src/inline/axpy.h"
3019170fe5c8SBarry Smith 
3020170fe5c8SBarry Smith #undef __FUNCT__
3021170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3022170fe5c8SBarry Smith /*
3023170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3024170fe5c8SBarry Smith 
3025170fe5c8SBarry Smith                n                       p                          p
3026170fe5c8SBarry Smith         (              )       (              )         (                  )
3027170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3028170fe5c8SBarry Smith         (              )       (              )         (                  )
3029170fe5c8SBarry Smith 
3030170fe5c8SBarry Smith */
3031170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3032170fe5c8SBarry Smith {
3033170fe5c8SBarry Smith   PetscErrorCode     ierr;
3034170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3035170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3036170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
30371de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3038170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3039170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3040170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3041170fe5c8SBarry Smith 
3042170fe5c8SBarry Smith   PetscFunctionBegin;
3043170fe5c8SBarry Smith   m = A->rmap.n;
3044170fe5c8SBarry Smith   n = A->cmap.n;
3045170fe5c8SBarry Smith   p = B->cmap.n;
3046170fe5c8SBarry Smith   a = sub_a->v;
3047170fe5c8SBarry Smith   b = sub_b->a;
3048170fe5c8SBarry Smith   c = sub_c->v;
3049170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3050170fe5c8SBarry Smith 
3051170fe5c8SBarry Smith   ii  = sub_b->i;
3052170fe5c8SBarry Smith   idx = sub_b->j;
3053170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3054170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3055170fe5c8SBarry Smith     while (q-->0) {
3056170fe5c8SBarry Smith       c_q = c + m*(*idx);
3057170fe5c8SBarry Smith       a_q = a + m*i;
30581de00fd4SBarry Smith       APXY(c_q,*b,a_q,m);
3059170fe5c8SBarry Smith       idx++;
3060170fe5c8SBarry Smith       b++;
3061170fe5c8SBarry Smith     }
3062170fe5c8SBarry Smith   }
3063170fe5c8SBarry Smith   PetscFunctionReturn(0);
3064170fe5c8SBarry Smith }
3065170fe5c8SBarry Smith 
3066170fe5c8SBarry Smith #undef __FUNCT__
3067170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3068170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3069170fe5c8SBarry Smith {
3070170fe5c8SBarry Smith   PetscErrorCode ierr;
3071170fe5c8SBarry Smith   PetscInt       m=A->rmap.n,n=B->cmap.n;
3072170fe5c8SBarry Smith   Mat            Cmat;
3073170fe5c8SBarry Smith 
3074170fe5c8SBarry Smith   PetscFunctionBegin;
3075170fe5c8SBarry 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);
3076170fe5c8SBarry Smith   ierr = MatCreate(A->hdr.comm,&Cmat);CHKERRQ(ierr);
3077170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3078170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3079170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3080170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3081170fe5c8SBarry Smith   *C = Cmat;
3082170fe5c8SBarry Smith   PetscFunctionReturn(0);
3083170fe5c8SBarry Smith }
3084170fe5c8SBarry Smith 
3085170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3086170fe5c8SBarry Smith #undef __FUNCT__
3087170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3088170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3089170fe5c8SBarry Smith {
3090170fe5c8SBarry Smith   PetscErrorCode ierr;
3091170fe5c8SBarry Smith 
3092170fe5c8SBarry Smith   PetscFunctionBegin;
3093170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3094170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3095170fe5c8SBarry Smith   }
3096170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3097170fe5c8SBarry Smith   PetscFunctionReturn(0);
3098170fe5c8SBarry Smith }
3099170fe5c8SBarry Smith 
3100170fe5c8SBarry Smith 
31010bad9183SKris Buschelman /*MC
3102fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31030bad9183SKris Buschelman    based on compressed sparse row format.
31040bad9183SKris Buschelman 
31050bad9183SKris Buschelman    Options Database Keys:
31060bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31070bad9183SKris Buschelman 
31080bad9183SKris Buschelman   Level: beginner
31090bad9183SKris Buschelman 
3110f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31110bad9183SKris Buschelman M*/
31120bad9183SKris Buschelman 
3113a6175056SHong Zhang EXTERN_C_BEGIN
311417667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3115b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3116*5c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3117*5c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3118*5c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
311917667f90SBarry Smith EXTERN_C_END
312017667f90SBarry Smith 
3121b24902e0SBarry Smith 
312217667f90SBarry Smith EXTERN_C_BEGIN
31234a2ae208SSatish Balay #undef __FUNCT__
31244a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3125be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3126273d9f13SBarry Smith {
3127273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3128dfbe8321SBarry Smith   PetscErrorCode ierr;
312938baddfdSBarry Smith   PetscMPIInt    size;
3130273d9f13SBarry Smith 
3131273d9f13SBarry Smith   PetscFunctionBegin;
31327adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3133273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3134273d9f13SBarry Smith 
313538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3136b0a32e0cSBarry Smith   B->data             = (void*)b;
3137549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
313890f02eecSBarry Smith   B->mapping          = 0;
3139416022c9SBarry Smith   b->row              = 0;
3140416022c9SBarry Smith   b->col              = 0;
314182bf6240SBarry Smith   b->icol             = 0;
3142b810aeb4SBarry Smith   b->reallocs         = 0;
314336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3144f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3145416022c9SBarry Smith   b->nonew             = 0;
3146416022c9SBarry Smith   b->diag              = 0;
3147416022c9SBarry Smith   b->solve_work        = 0;
31482a1b7f2aSHong Zhang   B->spptr             = 0;
3149be6bf707SBarry Smith   b->saved_values      = 0;
3150d7f994e1SBarry Smith   b->idiag             = 0;
315171f1c65dSBarry Smith   b->mdiag             = 0;
315271f1c65dSBarry Smith   b->ssor_work         = 0;
315371f1c65dSBarry Smith   b->omega             = 1.0;
315471f1c65dSBarry Smith   b->fshift            = 0.0;
315571f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3156f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
3157a30b2313SHong Zhang   b->xtoy              = 0;
3158a30b2313SHong Zhang   b->XtoY              = 0;
315973e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3160899cda47SBarry Smith   b->compressedrow.nrows   = B->rmap.n;
3161d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3162d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3163d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
316488e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
316517ab2063SBarry Smith 
316635d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3167*5c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_C",
3168*5c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
3169*5c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3170*5c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_spooles_C",
3171*5c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
3172*5c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3173*5c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_mumps_C",
3174*5c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
3175*5c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3176b24902e0SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_petsc_C",
3177b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3178b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3179f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3180bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3181bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3182f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3183be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3184bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3185f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3186be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3187bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3188a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3189a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3190a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
319185fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
319285fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
319385fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3194ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3195ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3196ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
319717667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
319817667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
319917667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
32005fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
32015fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
32025fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
32031cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
32041cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
32051cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3206a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3207a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3208a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3209a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3210a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3211a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
321205b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
321305b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
321405b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3215170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3216170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3217170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3218170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3219170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3220170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3221170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3222170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3223170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
32244846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
322517667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
32263a40ed3dSBarry Smith   PetscFunctionReturn(0);
322717ab2063SBarry Smith }
3228273d9f13SBarry Smith EXTERN_C_END
322917ab2063SBarry Smith 
32304a2ae208SSatish Balay #undef __FUNCT__
3231b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3232b24902e0SBarry Smith /*
3233b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3234b24902e0SBarry Smith */
3235b24902e0SBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
323617ab2063SBarry Smith {
3237b24902e0SBarry Smith   Mat            C = *B;
3238416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
32396849ba73SBarry Smith   PetscErrorCode ierr;
3240899cda47SBarry Smith   PetscInt       i,m = A->rmap.n;
324117ab2063SBarry Smith 
32423a40ed3dSBarry Smith   PetscFunctionBegin;
32431d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
32441d5dac46SHong Zhang 
3245273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3246273d9f13SBarry Smith 
3247416022c9SBarry Smith   C->factor           = A->factor;
32486ad4291fSHong Zhang 
3249416022c9SBarry Smith   c->row            = 0;
3250416022c9SBarry Smith   c->col            = 0;
325182bf6240SBarry Smith   c->icol           = 0;
32526ad4291fSHong Zhang   c->reallocs       = 0;
325317ab2063SBarry Smith 
32546ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
325517ab2063SBarry Smith 
325633b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
32579518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
325817ab2063SBarry Smith   for (i=0; i<m; i++) {
3259416022c9SBarry Smith     c->imax[i] = a->imax[i];
3260416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
326117ab2063SBarry Smith   }
326217ab2063SBarry Smith 
326317ab2063SBarry Smith   /* allocate the matrix space */
3264a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
32659518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3266f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
326797f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
326817ab2063SBarry Smith   if (m > 0) {
326997f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3270be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
3271bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3272be6bf707SBarry Smith     } else {
3273bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
327417ab2063SBarry Smith     }
327508480c60SBarry Smith   }
327617ab2063SBarry Smith 
32776ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3278416022c9SBarry Smith   c->roworiented       = a->roworiented;
3279416022c9SBarry Smith   c->nonew             = a->nonew;
3280416022c9SBarry Smith   if (a->diag) {
328197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
328252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
328317ab2063SBarry Smith     for (i=0; i<m; i++) {
3284416022c9SBarry Smith       c->diag[i] = a->diag[i];
328517ab2063SBarry Smith     }
32863a40ed3dSBarry Smith   } else c->diag           = 0;
32876ad4291fSHong Zhang   c->solve_work            = 0;
32886ad4291fSHong Zhang   c->saved_values          = 0;
32896ad4291fSHong Zhang   c->idiag                 = 0;
329071f1c65dSBarry Smith   c->ssor_work             = 0;
32916ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
3292e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3293e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
32946ad4291fSHong Zhang   c->xtoy                  = 0;
32956ad4291fSHong Zhang   c->XtoY                  = 0;
32966ad4291fSHong Zhang 
3297416022c9SBarry Smith   c->nz                 = a->nz;
3298416022c9SBarry Smith   c->maxnz              = a->maxnz;
3299273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3300754ec7b1SSatish Balay 
33016ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
33026ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
33036ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
33046ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
33056ad4291fSHong Zhang     i = a->compressedrow.nrows;
33066ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
33076ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
33086ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
33096ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
331027ea64f8SHong Zhang   } else {
331127ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
331227ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
331327ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
33146ad4291fSHong Zhang   }
331588e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
33164846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
33174846f1f5SKris Buschelman 
33187adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
33193a40ed3dSBarry Smith   PetscFunctionReturn(0);
332017ab2063SBarry Smith }
332117ab2063SBarry Smith 
33224a2ae208SSatish Balay #undef __FUNCT__
3323b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3324b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3325b24902e0SBarry Smith {
3326b24902e0SBarry Smith   PetscErrorCode ierr;
3327b24902e0SBarry Smith   PetscInt       n = A->rmap.n;
3328b24902e0SBarry Smith 
3329b24902e0SBarry Smith   PetscFunctionBegin;
3330b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3331b24902e0SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
3332b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3333b24902e0SBarry Smith   ierr = MatDuplicateNoCreate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr);
3334b24902e0SBarry Smith   PetscFunctionReturn(0);
3335b24902e0SBarry Smith }
3336b24902e0SBarry Smith 
3337b24902e0SBarry Smith #undef __FUNCT__
33384a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3339f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A)
334017ab2063SBarry Smith {
3341416022c9SBarry Smith   Mat_SeqAIJ     *a;
3342416022c9SBarry Smith   Mat            B;
33436849ba73SBarry Smith   PetscErrorCode ierr;
33443c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
334538baddfdSBarry Smith   int            fd;
334638baddfdSBarry Smith   PetscMPIInt    size;
3347bcd2baecSBarry Smith   MPI_Comm       comm;
334817ab2063SBarry Smith 
33493a40ed3dSBarry Smith   PetscFunctionBegin;
3350e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3351d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
335229bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3353b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
33540752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3355552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
335617ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
335717ab2063SBarry Smith 
3358d64ed03dSBarry Smith   if (nz < 0) {
335929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3360d64ed03dSBarry Smith   }
3361d64ed03dSBarry Smith 
336217ab2063SBarry Smith   /* read in row lengths */
336397f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
33640752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
336517ab2063SBarry Smith 
33663c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
33673c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
33683c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
33693c601197SSatish Balay 
337017ab2063SBarry Smith   /* create our matrix */
3371f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3372f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3373b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3374ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3375416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
337617ab2063SBarry Smith 
33770752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
337817ab2063SBarry Smith 
337917ab2063SBarry Smith   /* read in nonzero values */
33800752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
338117ab2063SBarry Smith 
338217ab2063SBarry Smith   /* set matrix "i" values */
3383efb16452SHong Zhang   a->i[0] = 0;
338417ab2063SBarry Smith   for (i=1; i<= M; i++) {
3385416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3386416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
338717ab2063SBarry Smith   }
3388606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
338917ab2063SBarry Smith 
33906d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
33916d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3392b3a1e11cSKris Buschelman   *A = B;
33933a40ed3dSBarry Smith   PetscFunctionReturn(0);
339417ab2063SBarry Smith }
339517ab2063SBarry Smith 
33964a2ae208SSatish Balay #undef __FUNCT__
3397b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3398dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
33997264ac53SSatish Balay {
34007264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3401dfbe8321SBarry Smith   PetscErrorCode ierr;
34027264ac53SSatish Balay 
34033a40ed3dSBarry Smith   PetscFunctionBegin;
3404bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3405899cda47SBarry Smith   if ((A->rmap.n != B->rmap.n) || (A->cmap.n != B->cmap.n) ||(a->nz != b->nz)) {
3406ca44d042SBarry Smith     *flg = PETSC_FALSE;
3407ca44d042SBarry Smith     PetscFunctionReturn(0);
3408bcd2baecSBarry Smith   }
34097264ac53SSatish Balay 
34107264ac53SSatish Balay   /* if the a->i are the same */
3411899cda47SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap.n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3412abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
34137264ac53SSatish Balay 
34147264ac53SSatish Balay   /* if a->j are the same */
341597f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3416abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3417bcd2baecSBarry Smith 
3418bcd2baecSBarry Smith   /* if a->a are the same */
341987828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
34200f5bd95cSBarry Smith 
34213a40ed3dSBarry Smith   PetscFunctionReturn(0);
34227264ac53SSatish Balay 
34237264ac53SSatish Balay }
342436db0b34SBarry Smith 
34254a2ae208SSatish Balay #undef __FUNCT__
34264a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
342705869f15SSatish Balay /*@
342836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
342936db0b34SBarry Smith               provided by the user.
343036db0b34SBarry Smith 
3431c75a6043SHong Zhang       Collective on MPI_Comm
343236db0b34SBarry Smith 
343336db0b34SBarry Smith    Input Parameters:
343436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
343536db0b34SBarry Smith .   m - number of rows
343636db0b34SBarry Smith .   n - number of columns
343736db0b34SBarry Smith .   i - row indices
343836db0b34SBarry Smith .   j - column indices
343936db0b34SBarry Smith -   a - matrix values
344036db0b34SBarry Smith 
344136db0b34SBarry Smith    Output Parameter:
344236db0b34SBarry Smith .   mat - the matrix
344336db0b34SBarry Smith 
344436db0b34SBarry Smith    Level: intermediate
344536db0b34SBarry Smith 
344636db0b34SBarry Smith    Notes:
34470551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
344836db0b34SBarry Smith     once the matrix is destroyed
344936db0b34SBarry Smith 
345036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
345136db0b34SBarry Smith 
3452bfeeae90SHong Zhang        The i and j indices are 0 based
345336db0b34SBarry Smith 
3454a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3455a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3456a4552177SSatish Balay     as shown:
3457a4552177SSatish Balay 
3458a4552177SSatish Balay         1 0 0
3459a4552177SSatish Balay         2 0 3
3460a4552177SSatish Balay         4 5 6
3461a4552177SSatish Balay 
3462a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
34639985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3464a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3465a4552177SSatish Balay 
34669985e31cSBarry Smith 
34672fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
346836db0b34SBarry Smith 
346936db0b34SBarry Smith @*/
3470a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
347136db0b34SBarry Smith {
3472dfbe8321SBarry Smith   PetscErrorCode ierr;
3473cbcfb4deSHong Zhang   PetscInt       ii;
347436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3475cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3476cbcfb4deSHong Zhang   PetscInt       jj;
3477cbcfb4deSHong Zhang #endif
347836db0b34SBarry Smith 
347936db0b34SBarry Smith   PetscFunctionBegin;
3480a96a251dSBarry Smith   if (i[0]) {
3481634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
348236db0b34SBarry Smith   }
3483f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3484f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3485ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3486ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3487ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3488ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3489ab93d7beSBarry Smith 
349036db0b34SBarry Smith   aij->i = i;
349136db0b34SBarry Smith   aij->j = j;
349236db0b34SBarry Smith   aij->a = a;
349336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
349436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3495e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3496e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
349736db0b34SBarry Smith 
349836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
349936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
35002515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
350179a5c55eSBarry 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]);
35029985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
35039985e31cSBarry 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);
35049985e31cSBarry 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);
35059985e31cSBarry Smith     }
350636db0b34SBarry Smith #endif
350736db0b34SBarry Smith   }
35082515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
350936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
351079a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
351179a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
351236db0b34SBarry Smith   }
351336db0b34SBarry Smith #endif
351436db0b34SBarry Smith 
3515b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3516b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
351736db0b34SBarry Smith   PetscFunctionReturn(0);
351836db0b34SBarry Smith }
351936db0b34SBarry Smith 
3520cc8ba8e1SBarry Smith #undef __FUNCT__
3521ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3522dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3523cc8ba8e1SBarry Smith {
3524dfbe8321SBarry Smith   PetscErrorCode ierr;
3525cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
352636db0b34SBarry Smith 
3527cc8ba8e1SBarry Smith   PetscFunctionBegin;
35288ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3529cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3530cc8ba8e1SBarry Smith     a->coloring = coloring;
353112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
353297f1f81fSBarry Smith     PetscInt             i,*larray;
353312c595b3SBarry Smith     ISColoring      ocoloring;
353408b6dcc0SBarry Smith     ISColoringValue *colors;
353512c595b3SBarry Smith 
353612c595b3SBarry Smith     /* set coloring for diagonal portion */
3537899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3538899cda47SBarry Smith     for (i=0; i<A->cmap.n; i++) {
353912c595b3SBarry Smith       larray[i] = i;
354012c595b3SBarry Smith     }
3541899cda47SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap.n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3542899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3543899cda47SBarry Smith     for (i=0; i<A->cmap.n; i++) {
354412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
354512c595b3SBarry Smith     }
354612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
354795a80f87SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap.n,colors,&ocoloring);CHKERRQ(ierr);
354812c595b3SBarry Smith     a->coloring = ocoloring;
354912c595b3SBarry Smith   }
3550cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3551cc8ba8e1SBarry Smith }
3552cc8ba8e1SBarry Smith 
3553dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3554ee4f033dSBarry Smith EXTERN_C_BEGIN
355529c1e371SBarry Smith #include "adic/ad_utils.h"
3556ee4f033dSBarry Smith EXTERN_C_END
3557cc8ba8e1SBarry Smith 
3558cc8ba8e1SBarry Smith #undef __FUNCT__
3559ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3560dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3561cc8ba8e1SBarry Smith {
3562cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3563899cda47SBarry Smith   PetscInt        m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
35644440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
356508b6dcc0SBarry Smith   ISColoringValue *color;
3566cc8ba8e1SBarry Smith 
3567cc8ba8e1SBarry Smith   PetscFunctionBegin;
3568e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
35694440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3570cc8ba8e1SBarry Smith   color = a->coloring->colors;
3571cc8ba8e1SBarry Smith   /* loop over rows */
3572cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3573cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3574cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3575cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3576cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3577cc8ba8e1SBarry Smith     }
35784440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3579ee4f033dSBarry Smith   }
3580ee4f033dSBarry Smith   PetscFunctionReturn(0);
3581ee4f033dSBarry Smith }
3582ee4f033dSBarry Smith #endif
3583ee4f033dSBarry Smith 
3584ee4f033dSBarry Smith #undef __FUNCT__
3585ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
358697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3587ee4f033dSBarry Smith {
3588ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3589899cda47SBarry Smith   PetscInt         m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j;
359054f21887SBarry Smith   MatScalar       *v = a->a;
359154f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
359208b6dcc0SBarry Smith   ISColoringValue *color;
3593ee4f033dSBarry Smith 
3594ee4f033dSBarry Smith   PetscFunctionBegin;
3595e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3596ee4f033dSBarry Smith   color = a->coloring->colors;
3597ee4f033dSBarry Smith   /* loop over rows */
3598ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3599ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3600ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3601ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3602ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3603ee4f033dSBarry Smith     }
3604ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3605cc8ba8e1SBarry Smith   }
3606cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3607cc8ba8e1SBarry Smith }
360836db0b34SBarry Smith 
360981824310SBarry Smith /*
361081824310SBarry Smith     Special version for direct calls from Fortran
361181824310SBarry Smith */
361281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
361381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
361481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
361581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
361681824310SBarry Smith #endif
361781824310SBarry Smith 
361881824310SBarry Smith /* Change these macros so can be used in void function */
361981824310SBarry Smith #undef CHKERRQ
36207adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
362181824310SBarry Smith #undef SETERRQ2
36227adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
362381824310SBarry Smith 
362481824310SBarry Smith EXTERN_C_BEGIN
362581824310SBarry Smith #undef __FUNCT__
362681824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
36271f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
362881824310SBarry Smith {
362981824310SBarry Smith   Mat            A = *AA;
363081824310SBarry Smith   PetscInt       m = *mm, n = *nn;
363181824310SBarry Smith   InsertMode     is = *isis;
363281824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
363381824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
363481824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
363581824310SBarry Smith   PetscErrorCode ierr;
363681824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
363754f21887SBarry Smith   MatScalar      *ap,value,*aa;
3638edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
363981824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
364081824310SBarry Smith 
364181824310SBarry Smith   PetscFunctionBegin;
364281824310SBarry Smith   MatPreallocated(A);
364381824310SBarry Smith   imax = a->imax;
364481824310SBarry Smith   ai = a->i;
364581824310SBarry Smith   ailen = a->ilen;
364681824310SBarry Smith   aj = a->j;
364781824310SBarry Smith   aa = a->a;
364881824310SBarry Smith 
364981824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
365081824310SBarry Smith     row  = im[k];
365181824310SBarry Smith     if (row < 0) continue;
365281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
36537adad957SLisandro Dalcin     if (row >= A->rmap.n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
365481824310SBarry Smith #endif
365581824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
365681824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
365781824310SBarry Smith     low  = 0;
365881824310SBarry Smith     high = nrow;
365981824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
366081824310SBarry Smith       if (in[l] < 0) continue;
366181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
36627adad957SLisandro Dalcin       if (in[l] >= A->cmap.n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
366381824310SBarry Smith #endif
366481824310SBarry Smith       col = in[l];
366581824310SBarry Smith       if (roworiented) {
366681824310SBarry Smith         value = v[l + k*n];
366781824310SBarry Smith       } else {
366881824310SBarry Smith         value = v[k + l*m];
366981824310SBarry Smith       }
367081824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
367181824310SBarry Smith 
367281824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
367381824310SBarry Smith       lastcol = col;
367481824310SBarry Smith       while (high-low > 5) {
367581824310SBarry Smith         t = (low+high)/2;
367681824310SBarry Smith         if (rp[t] > col) high = t;
367781824310SBarry Smith         else             low  = t;
367881824310SBarry Smith       }
367981824310SBarry Smith       for (i=low; i<high; i++) {
368081824310SBarry Smith         if (rp[i] > col) break;
368181824310SBarry Smith         if (rp[i] == col) {
368281824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
368381824310SBarry Smith           else                  ap[i] = value;
368481824310SBarry Smith           goto noinsert;
368581824310SBarry Smith         }
368681824310SBarry Smith       }
368781824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
368881824310SBarry Smith       if (nonew == 1) goto noinsert;
36897adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3690421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
369181824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
369281824310SBarry Smith       /* shift up all the later entries in this row */
369381824310SBarry Smith       for (ii=N; ii>=i; ii--) {
369481824310SBarry Smith         rp[ii+1] = rp[ii];
369581824310SBarry Smith         ap[ii+1] = ap[ii];
369681824310SBarry Smith       }
369781824310SBarry Smith       rp[i] = col;
369881824310SBarry Smith       ap[i] = value;
369981824310SBarry Smith       noinsert:;
370081824310SBarry Smith       low = i + 1;
370181824310SBarry Smith     }
370281824310SBarry Smith     ailen[row] = nrow;
370381824310SBarry Smith   }
370481824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
370581824310SBarry Smith   PetscFunctionReturnVoid();
370681824310SBarry Smith }
370781824310SBarry Smith EXTERN_C_END
3708