xref: /petsc/src/mat/impls/aij/seq/aij.c (revision f69a0ea3504314d029ee423e0de2950ece2dab86)
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__
144a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
1597f1f81fSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1617ab2063SBarry Smith {
17416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18dfbe8321SBarry Smith   PetscErrorCode ierr;
1997f1f81fSBarry Smith   PetscInt       i,ishift;
2017ab2063SBarry Smith 
213a40ed3dSBarry Smith   PetscFunctionBegin;
2231625ec5SSatish Balay   *m     = A->m;
233a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
24bfeeae90SHong Zhang   ishift = 0;
2553e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
26273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
27bfeeae90SHong Zhang   } else if (oshift == 1) {
2897f1f81fSBarry Smith     PetscInt nz = a->i[A->m];
293b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
3097f1f81fSBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
3197f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
323b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
33273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
346945ee14SBarry Smith   } else {
356945ee14SBarry Smith     *ia = a->i; *ja = a->j;
36a2ce50c7SBarry Smith   }
373a40ed3dSBarry Smith   PetscFunctionReturn(0);
38a2744918SBarry Smith }
39a2744918SBarry Smith 
404a2ae208SSatish Balay #undef __FUNCT__
414a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
4297f1f81fSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
436945ee14SBarry Smith {
44dfbe8321SBarry Smith   PetscErrorCode ierr;
456945ee14SBarry Smith 
463a40ed3dSBarry Smith   PetscFunctionBegin;
473a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
48bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
49606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
50606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
51bcd2baecSBarry Smith   }
523a40ed3dSBarry Smith   PetscFunctionReturn(0);
5317ab2063SBarry Smith }
5417ab2063SBarry Smith 
554a2ae208SSatish Balay #undef __FUNCT__
564a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
5797f1f81fSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
583b2fbd54SBarry Smith {
593b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
60dfbe8321SBarry Smith   PetscErrorCode ierr;
6197f1f81fSBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->n,m = A->m;
6297f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
633b2fbd54SBarry Smith 
643a40ed3dSBarry Smith   PetscFunctionBegin;
653b2fbd54SBarry Smith   *nn     = A->n;
663a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
673b2fbd54SBarry Smith   if (symmetric) {
68bfeeae90SHong Zhang     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
693b2fbd54SBarry Smith   } else {
7097f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
7197f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
7297f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
7397f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
743b2fbd54SBarry Smith     jj = a->j;
753b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
76bfeeae90SHong Zhang       collengths[jj[i]]++;
773b2fbd54SBarry Smith     }
783b2fbd54SBarry Smith     cia[0] = oshift;
793b2fbd54SBarry Smith     for (i=0; i<n; i++) {
803b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
813b2fbd54SBarry Smith     }
8297f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
833b2fbd54SBarry Smith     jj   = a->j;
84a93ec695SBarry Smith     for (row=0; row<m; row++) {
85a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
86a93ec695SBarry Smith       for (i=0; i<mr; i++) {
87bfeeae90SHong Zhang         col = *jj++;
883b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
893b2fbd54SBarry Smith       }
903b2fbd54SBarry Smith     }
91606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
923b2fbd54SBarry Smith     *ia = cia; *ja = cja;
933b2fbd54SBarry Smith   }
943a40ed3dSBarry Smith   PetscFunctionReturn(0);
953b2fbd54SBarry Smith }
963b2fbd54SBarry Smith 
974a2ae208SSatish Balay #undef __FUNCT__
984a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
9997f1f81fSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1003b2fbd54SBarry Smith {
101dfbe8321SBarry Smith   PetscErrorCode ierr;
102606d414cSSatish Balay 
1033a40ed3dSBarry Smith   PetscFunctionBegin;
1043a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1053b2fbd54SBarry Smith 
106606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
107606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1083b2fbd54SBarry Smith 
1093a40ed3dSBarry Smith   PetscFunctionReturn(0);
1103b2fbd54SBarry Smith }
1113b2fbd54SBarry Smith 
112227d817aSBarry Smith #define CHUNKSIZE   15
11317ab2063SBarry Smith 
1144a2ae208SSatish Balay #undef __FUNCT__
1154a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
11697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
11717ab2063SBarry Smith {
118416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
119e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
12097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1216849ba73SBarry Smith   PetscErrorCode ierr;
122e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
12387828ca2SBarry Smith   PetscScalar    *ap,value,*aa = a->a;
12436db0b34SBarry Smith   PetscTruth     ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
125273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
12617ab2063SBarry Smith 
1273a40ed3dSBarry Smith   PetscFunctionBegin;
12817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
129416022c9SBarry Smith     row  = im[k];
1305ef9f2a5SBarry Smith     if (row < 0) continue;
1312515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
13277431f27SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1);
1333b2fbd54SBarry Smith #endif
134bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
13517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
136416022c9SBarry Smith     low  = 0;
137c71e6ed7SBarry Smith     high = nrow;
13817ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1395ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1402515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
14177431f27SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1);
1423b2fbd54SBarry Smith #endif
143bfeeae90SHong Zhang       col = in[l];
1444b0e389bSBarry Smith       if (roworiented) {
1455ef9f2a5SBarry Smith         value = v[l + k*n];
146bef8e0ddSBarry Smith       } else {
1474b0e389bSBarry Smith         value = v[k + l*m];
1484b0e389bSBarry Smith       }
149abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
15036db0b34SBarry Smith 
151c71e6ed7SBarry Smith       if (col < lastcol) low = 0; else high = nrow;
152e2ee6c50SBarry Smith       lastcol = col;
153416022c9SBarry Smith       while (high-low > 5) {
154416022c9SBarry Smith         t = (low+high)/2;
155416022c9SBarry Smith         if (rp[t] > col) high = t;
156416022c9SBarry Smith         else             low  = t;
15717ab2063SBarry Smith       }
158416022c9SBarry Smith       for (i=low; i<high; i++) {
15917ab2063SBarry Smith         if (rp[i] > col) break;
16017ab2063SBarry Smith         if (rp[i] == col) {
161416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
16217ab2063SBarry Smith           else                  ap[i] = value;
16317ab2063SBarry Smith           goto noinsert;
16417ab2063SBarry Smith         }
16517ab2063SBarry Smith       }
166abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
167c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
168cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
169ed1caa07SMatthew Knepley       MatSeqXAIJReallocateAIJ(a,1,nrow,row,col,rmax,aa,ai,aj,A->m,rp,ap,imax,nonew);
170416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
171416022c9SBarry Smith       /* shift up all the later entries in this row */
172416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
17317ab2063SBarry Smith         rp[ii+1] = rp[ii];
17417ab2063SBarry Smith         ap[ii+1] = ap[ii];
17517ab2063SBarry Smith       }
17617ab2063SBarry Smith       rp[i] = col;
17717ab2063SBarry Smith       ap[i] = value;
17817ab2063SBarry Smith       noinsert:;
179416022c9SBarry Smith       low = i + 1;
18017ab2063SBarry Smith     }
18117ab2063SBarry Smith     ailen[row] = nrow;
18217ab2063SBarry Smith   }
18388e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
1843a40ed3dSBarry Smith   PetscFunctionReturn(0);
18517ab2063SBarry Smith }
18617ab2063SBarry Smith 
1874a2ae208SSatish Balay #undef __FUNCT__
1884a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
18997f1f81fSBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
1907eb43aa7SLois Curfman McInnes {
1917eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
19297f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
19397f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
19487828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
1957eb43aa7SLois Curfman McInnes 
1963a40ed3dSBarry Smith   PetscFunctionBegin;
1977eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
1987eb43aa7SLois Curfman McInnes     row  = im[k];
19977431f27SBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row);
20077431f27SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1);
201bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2027eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2037eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
20477431f27SBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]);
20577431f27SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1);
206bfeeae90SHong Zhang       col = in[l] ;
2077eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2087eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2097eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2107eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2117eb43aa7SLois Curfman McInnes         else             low  = t;
2127eb43aa7SLois Curfman McInnes       }
2137eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2147eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2157eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
216b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2177eb43aa7SLois Curfman McInnes           goto finished;
2187eb43aa7SLois Curfman McInnes         }
2197eb43aa7SLois Curfman McInnes       }
220b49de8d1SLois Curfman McInnes       *v++ = zero;
2217eb43aa7SLois Curfman McInnes       finished:;
2227eb43aa7SLois Curfman McInnes     }
2237eb43aa7SLois Curfman McInnes   }
2243a40ed3dSBarry Smith   PetscFunctionReturn(0);
2257eb43aa7SLois Curfman McInnes }
2267eb43aa7SLois Curfman McInnes 
22717ab2063SBarry Smith 
2284a2ae208SSatish Balay #undef __FUNCT__
2294a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
230dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
23117ab2063SBarry Smith {
232416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2336849ba73SBarry Smith   PetscErrorCode ierr;
2346f69ff64SBarry Smith   PetscInt       i,*col_lens;
2356f69ff64SBarry Smith   int            fd;
23617ab2063SBarry Smith 
2373a40ed3dSBarry Smith   PetscFunctionBegin;
238b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
23997f1f81fSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
240552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
241273d9f13SBarry Smith   col_lens[1] = A->m;
242273d9f13SBarry Smith   col_lens[2] = A->n;
243416022c9SBarry Smith   col_lens[3] = a->nz;
244416022c9SBarry Smith 
245416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
246273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
247416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
24817ab2063SBarry Smith   }
2496f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
250606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
251416022c9SBarry Smith 
252416022c9SBarry Smith   /* store column indices (zero start index) */
2536f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
254416022c9SBarry Smith 
255416022c9SBarry Smith   /* store nonzero values */
2566f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
2573a40ed3dSBarry Smith   PetscFunctionReturn(0);
25817ab2063SBarry Smith }
259416022c9SBarry Smith 
260dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
261cd155464SBarry Smith 
2624a2ae208SSatish Balay #undef __FUNCT__
2634a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
264dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
265416022c9SBarry Smith {
266416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
267dfbe8321SBarry Smith   PetscErrorCode    ierr;
26897f1f81fSBarry Smith   PetscInt          i,j,m = A->m,shift=0;
2692dcb1b2aSMatthew Knepley   const char        *name;
270f3ef73ceSBarry Smith   PetscViewerFormat format;
27117ab2063SBarry Smith 
2723a40ed3dSBarry Smith   PetscFunctionBegin;
273435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
274b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
27571c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
27697f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
277273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
278d00d2cf4SBarry Smith       nofinalvalue = 1;
279d00d2cf4SBarry Smith     }
280b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
28177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->n);CHKERRQ(ierr);
28277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
28377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
284b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
28517ab2063SBarry Smith 
28617ab2063SBarry Smith     for (i=0; i<m; i++) {
287416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
288aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
28977431f27SBarry 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);
29017ab2063SBarry Smith #else
29177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
29217ab2063SBarry Smith #endif
29317ab2063SBarry Smith       }
29417ab2063SBarry Smith     }
295d00d2cf4SBarry Smith     if (nofinalvalue) {
29677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
297d00d2cf4SBarry Smith     }
298fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
299b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
30068369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
301cd155464SBarry Smith      PetscFunctionReturn(0);
302fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
303b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
30444cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
30577431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
30644cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
307aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
30836db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
30977431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
31036db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
31177431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
31236db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
31377431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3146831982aSBarry Smith         }
31544cd7ae7SLois Curfman McInnes #else
31677431f27SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
31744cd7ae7SLois Curfman McInnes #endif
31844cd7ae7SLois Curfman McInnes       }
319b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
32044cd7ae7SLois Curfman McInnes     }
321b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
322fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
32397f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
324b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
32597f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
326496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
327496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
328496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
329496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
330aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
33136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
332496be53dSLois Curfman McInnes #else
333496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
334496be53dSLois Curfman McInnes #endif
335496be53dSLois Curfman McInnes         }
336496be53dSLois Curfman McInnes       }
337496be53dSLois Curfman McInnes     }
3382e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
33977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3402e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
34177431f27SBarry 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);}
34277431f27SBarry 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);}
34377431f27SBarry 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);}
34477431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
34577431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
34677431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
347496be53dSLois Curfman McInnes     }
348b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
349606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
350496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
351496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
35277431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
353496be53dSLois Curfman McInnes       }
354b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
355496be53dSLois Curfman McInnes     }
356b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
357496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
358496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
359496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
360aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
362b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
3636831982aSBarry Smith           }
364496be53dSLois Curfman McInnes #else
365b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
366496be53dSLois Curfman McInnes #endif
367496be53dSLois Curfman McInnes         }
368496be53dSLois Curfman McInnes       }
369b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
370496be53dSLois Curfman McInnes     }
371b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
372fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
37397f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
37487828ca2SBarry Smith     PetscScalar value;
37502594712SBarry Smith 
376b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
37702594712SBarry Smith     for (i=0; i<m; i++) {
37802594712SBarry Smith       jcnt = 0;
379273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
380e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
38102594712SBarry Smith           value = a->a[cnt++];
382e24b481bSBarry Smith           jcnt++;
38302594712SBarry Smith         } else {
38402594712SBarry Smith           value = 0.0;
38502594712SBarry Smith         }
386aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
387b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
38802594712SBarry Smith #else
389b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
39002594712SBarry Smith #endif
39102594712SBarry Smith       }
392b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
39302594712SBarry Smith     }
394b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
3953a40ed3dSBarry Smith   } else {
396b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
39717ab2063SBarry Smith     for (i=0; i<m; i++) {
39877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
399416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
400aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
40136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
40277431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
40336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
40477431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4053a40ed3dSBarry Smith         } else {
40677431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
40717ab2063SBarry Smith         }
40817ab2063SBarry Smith #else
40977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
41017ab2063SBarry Smith #endif
41117ab2063SBarry Smith       }
412b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
41317ab2063SBarry Smith     }
414b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
41517ab2063SBarry Smith   }
416b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4173a40ed3dSBarry Smith   PetscFunctionReturn(0);
418416022c9SBarry Smith }
419416022c9SBarry Smith 
4204a2ae208SSatish Balay #undef __FUNCT__
4214a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
422dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
423416022c9SBarry Smith {
424480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
425416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
426dfbe8321SBarry Smith   PetscErrorCode    ierr;
42797f1f81fSBarry Smith   PetscInt          i,j,m = A->m,color;
42836db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
429b0a32e0cSBarry Smith   PetscViewer       viewer;
430f3ef73ceSBarry Smith   PetscViewerFormat format;
431cddf8d76SBarry Smith 
4323a40ed3dSBarry Smith   PetscFunctionBegin;
433480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
434b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
43519bcc07fSBarry Smith 
436b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
437416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4380513a670SBarry Smith 
439fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4400513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
441b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
442416022c9SBarry Smith     for (i=0; i<m; i++) {
443cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
444bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
445bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
446aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
448cddf8d76SBarry Smith #else
449cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
450cddf8d76SBarry Smith #endif
451b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
452cddf8d76SBarry Smith       }
453cddf8d76SBarry Smith     }
454b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
455cddf8d76SBarry Smith     for (i=0; i<m; i++) {
456cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
457bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
458bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
459cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
460b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
461cddf8d76SBarry Smith       }
462cddf8d76SBarry Smith     }
463b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
464cddf8d76SBarry Smith     for (i=0; i<m; i++) {
465cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
466bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
467bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
468aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
46936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
470cddf8d76SBarry Smith #else
471cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
472cddf8d76SBarry Smith #endif
473b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
474416022c9SBarry Smith       }
475416022c9SBarry Smith     }
4760513a670SBarry Smith   } else {
4770513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
4780513a670SBarry Smith     /* first determine max of all nonzero values */
47997f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
480b0a32e0cSBarry Smith     PetscDraw   popup;
48136db0b34SBarry Smith     PetscReal scale;
4820513a670SBarry Smith 
4830513a670SBarry Smith     for (i=0; i<nz; i++) {
4840513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
4850513a670SBarry Smith     }
486b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
487b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
488b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
4890513a670SBarry Smith     count = 0;
4900513a670SBarry Smith     for (i=0; i<m; i++) {
4910513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
492bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
493bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
49497f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
495b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
4960513a670SBarry Smith         count++;
4970513a670SBarry Smith       }
4980513a670SBarry Smith     }
4990513a670SBarry Smith   }
500480ef9eaSBarry Smith   PetscFunctionReturn(0);
501480ef9eaSBarry Smith }
502cddf8d76SBarry Smith 
5034a2ae208SSatish Balay #undef __FUNCT__
5044a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
505dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
506480ef9eaSBarry Smith {
507dfbe8321SBarry Smith   PetscErrorCode ierr;
508b0a32e0cSBarry Smith   PetscDraw      draw;
50936db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
510480ef9eaSBarry Smith   PetscTruth     isnull;
511480ef9eaSBarry Smith 
512480ef9eaSBarry Smith   PetscFunctionBegin;
513b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
514b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
515480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
516480ef9eaSBarry Smith 
517480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
518273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
519480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
520b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
521b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
522480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5233a40ed3dSBarry Smith   PetscFunctionReturn(0);
524416022c9SBarry Smith }
525416022c9SBarry Smith 
5264a2ae208SSatish Balay #undef __FUNCT__
5274a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
528dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
529416022c9SBarry Smith {
530dfbe8321SBarry Smith   PetscErrorCode ierr;
53132077d6dSBarry Smith   PetscTruth     issocket,iascii,isbinary,isdraw;
532416022c9SBarry Smith 
5333a40ed3dSBarry Smith   PetscFunctionBegin;
534b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
53532077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
536fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
537fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
538c45a1595SBarry Smith   if (iascii) {
5393a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5404cf18111SSatish Balay #if defined(PETSC_USE_SOCKET_VIEWER)
541c45a1595SBarry Smith   } else if (issocket) {
5424cf18111SSatish Balay     Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
543c45a1595SBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
544c45a1595SBarry Smith #endif
5450f5bd95cSBarry Smith   } else if (isbinary) {
5463a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5470f5bd95cSBarry Smith   } else if (isdraw) {
5483a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5495cd90555SBarry Smith   } else {
550958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
55117ab2063SBarry Smith   }
5524846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
5533a40ed3dSBarry Smith   PetscFunctionReturn(0);
55417ab2063SBarry Smith }
55519bcc07fSBarry Smith 
5564a2ae208SSatish Balay #undef __FUNCT__
5574a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
558dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
55917ab2063SBarry Smith {
560416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5616849ba73SBarry Smith   PetscErrorCode ierr;
56297f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
56397f1f81fSBarry Smith   PetscInt       m = A->m,*ip,N,*ailen = a->ilen,rmax = 0;
56487828ca2SBarry Smith   PetscScalar    *aa = a->a,*ap;
5653447b6efSHong Zhang   PetscReal      ratio=0.6;
56617ab2063SBarry Smith 
5673a40ed3dSBarry Smith   PetscFunctionBegin;
5683a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
56917ab2063SBarry Smith 
57043ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
57117ab2063SBarry Smith   for (i=1; i<m; i++) {
572416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
57317ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
57494a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
57517ab2063SBarry Smith     if (fshift) {
576bfeeae90SHong Zhang       ip = aj + ai[i] ;
577bfeeae90SHong Zhang       ap = aa + ai[i] ;
57817ab2063SBarry Smith       N  = ailen[i];
57917ab2063SBarry Smith       for (j=0; j<N; j++) {
58017ab2063SBarry Smith         ip[j-fshift] = ip[j];
58117ab2063SBarry Smith         ap[j-fshift] = ap[j];
58217ab2063SBarry Smith       }
58317ab2063SBarry Smith     }
58417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
58517ab2063SBarry Smith   }
58617ab2063SBarry Smith   if (m) {
58717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
58817ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
58917ab2063SBarry Smith   }
59017ab2063SBarry Smith   /* reset ilen and imax for each row */
59117ab2063SBarry Smith   for (i=0; i<m; i++) {
59217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
59317ab2063SBarry Smith   }
594bfeeae90SHong Zhang   a->nz = ai[m];
59517ab2063SBarry Smith 
59617ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
597416022c9SBarry Smith   if (fshift && a->diag) {
598606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
59952e6d16bSBarry Smith     ierr = PetscLogObjectMemory(A,-(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
600416022c9SBarry Smith     a->diag = 0;
60117ab2063SBarry Smith   }
60263ba0a88SBarry Smith   ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->n,fshift,a->nz));CHKERRQ(ierr);
60363ba0a88SBarry Smith   ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %D\n",a->reallocs));CHKERRQ(ierr);
60463ba0a88SBarry Smith   ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Maximum nonzeros in any row is %D\n",rmax));CHKERRQ(ierr);
605dd5f02e7SSatish Balay   a->reallocs          = 0;
6064e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
60736db0b34SBarry Smith   a->rmax              = rmax;
6084e220ebcSLois Curfman McInnes 
609cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
610317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
61188e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
61271c2f376SKris Buschelman 
6134846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
6143a40ed3dSBarry Smith   PetscFunctionReturn(0);
61517ab2063SBarry Smith }
61617ab2063SBarry Smith 
6174a2ae208SSatish Balay #undef __FUNCT__
6184a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
619dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
62017ab2063SBarry Smith {
621416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
622dfbe8321SBarry Smith   PetscErrorCode ierr;
6233a40ed3dSBarry Smith 
6243a40ed3dSBarry Smith   PetscFunctionBegin;
625bfeeae90SHong Zhang   ierr = PetscMemzero(a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
6263a40ed3dSBarry Smith   PetscFunctionReturn(0);
62717ab2063SBarry Smith }
628416022c9SBarry Smith 
6294a2ae208SSatish Balay #undef __FUNCT__
6304a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
631dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
63217ab2063SBarry Smith {
633416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
634dfbe8321SBarry Smith   PetscErrorCode ierr;
635d5d45c9bSBarry Smith 
6363a40ed3dSBarry Smith   PetscFunctionBegin;
637aa482453SBarry Smith #if defined(PETSC_USE_LOG)
63877431f27SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->m,A->n,a->nz);
63917ab2063SBarry Smith #endif
6403cbb7e54SHong Zhang   if (a->freedata){
641085a36d4SBarry Smith     ierr = MatSeqXAIJFreeAIJ(a->singlemalloc,&a->a,&a->j,&a->i);CHKERRQ(ierr);
6423cbb7e54SHong Zhang   }
643c38d4ed2SBarry Smith   if (a->row) {
644c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
645c38d4ed2SBarry Smith   }
646c38d4ed2SBarry Smith   if (a->col) {
647c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
648c38d4ed2SBarry Smith   }
649606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
650ab93d7beSBarry Smith   if (a->ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
651273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
652606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
65382bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
654606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
655cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
656a30b2313SHong Zhang   if (a->xtoy) {ierr = PetscFree(a->xtoy);CHKERRQ(ierr);}
657d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
658a30b2313SHong Zhang 
6594846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
6604846f1f5SKris Buschelman 
661606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
662901853e0SKris Buschelman 
663901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
664901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
665901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
666901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
667901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
668901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
669901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
670901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
6713a40ed3dSBarry Smith   PetscFunctionReturn(0);
67217ab2063SBarry Smith }
67317ab2063SBarry Smith 
6744a2ae208SSatish Balay #undef __FUNCT__
6754a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
676dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
67717ab2063SBarry Smith {
6783a40ed3dSBarry Smith   PetscFunctionBegin;
6793a40ed3dSBarry Smith   PetscFunctionReturn(0);
68017ab2063SBarry Smith }
68117ab2063SBarry Smith 
6824a2ae208SSatish Balay #undef __FUNCT__
6834a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
684dfbe8321SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op)
68517ab2063SBarry Smith {
686416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6874846f1f5SKris Buschelman   PetscErrorCode ierr;
6883a40ed3dSBarry Smith 
6893a40ed3dSBarry Smith   PetscFunctionBegin;
690a65d3064SKris Buschelman   switch (op) {
691a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
692a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
693a65d3064SKris Buschelman       break;
694a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
695a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
696a65d3064SKris Buschelman       break;
697a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
698a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
699a65d3064SKris Buschelman       break;
700a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
701a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
702a65d3064SKris Buschelman       break;
703a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
704a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
705a65d3064SKris Buschelman       break;
706a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
707a65d3064SKris Buschelman       a->nonew             = 1;
708a65d3064SKris Buschelman       break;
709a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
710a65d3064SKris Buschelman       a->nonew             = -1;
711a65d3064SKris Buschelman       break;
712a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
713a65d3064SKris Buschelman       a->nonew             = -2;
714a65d3064SKris Buschelman       break;
715a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
716a65d3064SKris Buschelman       a->nonew             = 0;
717a65d3064SKris Buschelman       break;
718a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
719a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
720a65d3064SKris Buschelman       break;
721d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
722d487561eSHong Zhang       a->compressedrow.use = PETSC_TRUE;
723d487561eSHong Zhang       break;
724d487561eSHong Zhang     case MAT_DO_NOT_USE_COMPRESSEDROW:
725d487561eSHong Zhang       a->compressedrow.use = PETSC_FALSE;
726d487561eSHong Zhang       break;
727a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
728a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
729a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
730a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
731a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
73263ba0a88SBarry Smith       ierr = PetscLogInfo((A,"MatSetOption_SeqAIJ:Option ignored\n"));CHKERRQ(ierr);
733a65d3064SKris Buschelman       break;
734a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
73529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
736a65d3064SKris Buschelman     default:
73771c2f376SKris Buschelman       break;
738a65d3064SKris Buschelman   }
7394846f1f5SKris Buschelman   ierr = MatSetOption_Inode(A,op);CHKERRQ(ierr);
7403a40ed3dSBarry Smith   PetscFunctionReturn(0);
74117ab2063SBarry Smith }
74217ab2063SBarry Smith 
7434a2ae208SSatish Balay #undef __FUNCT__
7444a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
745dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
74617ab2063SBarry Smith {
747416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7486849ba73SBarry Smith   PetscErrorCode ierr;
74997f1f81fSBarry Smith   PetscInt       i,j,n;
75087828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
75117ab2063SBarry Smith 
7523a40ed3dSBarry Smith   PetscFunctionBegin;
7532dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
7541ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
75536db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
756273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
757273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
758bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
759bfeeae90SHong Zhang       if (a->j[j] == i) {
760416022c9SBarry Smith         x[i] = a->a[j];
76117ab2063SBarry Smith         break;
76217ab2063SBarry Smith       }
76317ab2063SBarry Smith     }
76417ab2063SBarry Smith   }
7651ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
7663a40ed3dSBarry Smith   PetscFunctionReturn(0);
76717ab2063SBarry Smith }
76817ab2063SBarry Smith 
7694a2ae208SSatish Balay #undef __FUNCT__
7704a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
771dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
77217ab2063SBarry Smith {
773416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
7745c897100SBarry Smith   PetscScalar       *x,*y;
775dfbe8321SBarry Smith   PetscErrorCode    ierr;
77697f1f81fSBarry Smith   PetscInt          m = A->m;
7775c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
7785c897100SBarry Smith   PetscScalar       *v,alpha;
7797b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
7803447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
7814eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
7825c897100SBarry Smith #endif
78317ab2063SBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
7852e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
7861ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
7871ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
7885c897100SBarry Smith 
7895c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
790bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
7915c897100SBarry Smith #else
7923447b6efSHong Zhang   if (usecprow){
7933447b6efSHong Zhang     m    = cprow.nrows;
7943447b6efSHong Zhang     ii   = cprow.i;
7957b2bb3b9SHong Zhang     ridx = cprow.rindex;
7963447b6efSHong Zhang   } else {
7973447b6efSHong Zhang     ii = a->i;
7983447b6efSHong Zhang   }
79917ab2063SBarry Smith   for (i=0; i<m; i++) {
8003447b6efSHong Zhang     idx   = a->j + ii[i] ;
8013447b6efSHong Zhang     v     = a->a + ii[i] ;
8023447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8033447b6efSHong Zhang     if (usecprow){
8047b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8053447b6efSHong Zhang     } else {
80617ab2063SBarry Smith       alpha = x[i];
8073447b6efSHong Zhang     }
80817ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
80917ab2063SBarry Smith   }
8105c897100SBarry Smith #endif
811efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
8121ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8131ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8143a40ed3dSBarry Smith   PetscFunctionReturn(0);
81517ab2063SBarry Smith }
81617ab2063SBarry Smith 
8174a2ae208SSatish Balay #undef __FUNCT__
8185c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
819dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8205c897100SBarry Smith {
8218d5b0100SBarry Smith   PetscScalar    zero = 0.0;
822dfbe8321SBarry Smith   PetscErrorCode ierr;
8235c897100SBarry Smith 
8245c897100SBarry Smith   PetscFunctionBegin;
8252dcb1b2aSMatthew Knepley   ierr = VecSet(yy,zero);CHKERRQ(ierr);
8265c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8275c897100SBarry Smith   PetscFunctionReturn(0);
8285c897100SBarry Smith }
8295c897100SBarry Smith 
8305c897100SBarry Smith 
8315c897100SBarry Smith #undef __FUNCT__
8324a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
833dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
83417ab2063SBarry Smith {
835416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
83697952fefSHong Zhang   PetscScalar    *x,*y,*aa;
837dfbe8321SBarry Smith   PetscErrorCode ierr;
83897952fefSHong Zhang   PetscInt       m=A->m,*aj,*ii;
839aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
84097952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
841362ced78SSatish Balay   PetscScalar    sum;
84297952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
843e36a17ebSSatish Balay #endif
84417ab2063SBarry Smith 
845b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
84697952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
847fee21e36SBarry Smith #endif
848fee21e36SBarry Smith 
8493a40ed3dSBarry Smith   PetscFunctionBegin;
8501ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8511ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
85297952fefSHong Zhang   aj  = a->j;
85397952fefSHong Zhang   aa    = a->a;
854416022c9SBarry Smith   ii   = a->i;
855aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
85697952fefSHong Zhang   fortranmultaij_(&m,x,ii,aj,aa,y);
8578d195f9aSBarry Smith #else
8584eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
85997952fefSHong Zhang     m    = a->compressedrow.nrows;
86097952fefSHong Zhang     ii   = a->compressedrow.i;
86197952fefSHong Zhang     ridx = a->compressedrow.rindex;
86297952fefSHong Zhang     for (i=0; i<m; i++){
86397952fefSHong Zhang       n   = ii[i+1] - ii[i];
86497952fefSHong Zhang       aj  = a->j + ii[i];
86597952fefSHong Zhang       aa  = a->a + ii[i];
86697952fefSHong Zhang       sum = 0.0;
86797952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
86897952fefSHong Zhang       y[*ridx++] = sum;
86997952fefSHong Zhang     }
87097952fefSHong Zhang   } else { /* do not use compressed row format */
87117ab2063SBarry Smith     for (i=0; i<m; i++) {
8729ea0dfa2SSatish Balay       jrow = ii[i];
8739ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
87417ab2063SBarry Smith       sum  = 0.0;
8759ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
87697952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
8779ea0dfa2SSatish Balay       }
87817ab2063SBarry Smith       y[i] = sum;
87917ab2063SBarry Smith     }
88097952fefSHong Zhang   }
8818d195f9aSBarry Smith #endif
882efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz - m);CHKERRQ(ierr);
8831ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8841ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8853a40ed3dSBarry Smith   PetscFunctionReturn(0);
88617ab2063SBarry Smith }
88717ab2063SBarry Smith 
8884a2ae208SSatish Balay #undef __FUNCT__
8894a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
890dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
89117ab2063SBarry Smith {
892416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
89397952fefSHong Zhang   PetscScalar    *x,*y,*z,*aa;
894dfbe8321SBarry Smith   PetscErrorCode ierr;
89597952fefSHong Zhang   PetscInt       m = A->m,*aj,*ii;
896aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
89797952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
898362ced78SSatish Balay   PetscScalar    sum;
89997952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
900e36a17ebSSatish Balay #endif
9019ea0dfa2SSatish Balay 
9023a40ed3dSBarry Smith   PetscFunctionBegin;
9031ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9041ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9052e8a6d31SBarry Smith   if (zz != yy) {
9061ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9072e8a6d31SBarry Smith   } else {
9082e8a6d31SBarry Smith     z = y;
9092e8a6d31SBarry Smith   }
910bfeeae90SHong Zhang 
91197952fefSHong Zhang   aj  = a->j;
91297952fefSHong Zhang   aa  = a->a;
913cddf8d76SBarry Smith   ii  = a->i;
914aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
91597952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
91602ab625aSSatish Balay #else
9174eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
9184eb6d288SHong Zhang     if (zz != yy){
9194eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
9204eb6d288SHong Zhang     }
92197952fefSHong Zhang     m    = a->compressedrow.nrows;
92297952fefSHong Zhang     ii   = a->compressedrow.i;
92397952fefSHong Zhang     ridx = a->compressedrow.rindex;
92497952fefSHong Zhang     for (i=0; i<m; i++){
92597952fefSHong Zhang       n  = ii[i+1] - ii[i];
92697952fefSHong Zhang       aj  = a->j + ii[i];
92797952fefSHong Zhang       aa  = a->a + ii[i];
92897952fefSHong Zhang       sum = y[*ridx];
92997952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
93097952fefSHong Zhang       z[*ridx++] = sum;
93197952fefSHong Zhang     }
93297952fefSHong Zhang   } else { /* do not use compressed row format */
93317ab2063SBarry Smith     for (i=0; i<m; i++) {
9349ea0dfa2SSatish Balay       jrow = ii[i];
9359ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
93617ab2063SBarry Smith       sum  = y[i];
9379ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
93897952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9399ea0dfa2SSatish Balay       }
94017ab2063SBarry Smith       z[i] = sum;
94117ab2063SBarry Smith     }
94297952fefSHong Zhang   }
94302ab625aSSatish Balay #endif
944efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
9451ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9461ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9472e8a6d31SBarry Smith   if (zz != yy) {
9481ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9492e8a6d31SBarry Smith   }
9503a40ed3dSBarry Smith   PetscFunctionReturn(0);
95117ab2063SBarry Smith }
95217ab2063SBarry Smith 
95317ab2063SBarry Smith /*
95417ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
95517ab2063SBarry Smith */
9564a2ae208SSatish Balay #undef __FUNCT__
9574a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
958dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
95917ab2063SBarry Smith {
960416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9616849ba73SBarry Smith   PetscErrorCode ierr;
96297f1f81fSBarry Smith   PetscInt       i,j,*diag,m = A->m;
96317ab2063SBarry Smith 
9643a40ed3dSBarry Smith   PetscFunctionBegin;
965f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
966f1e2ffcdSBarry Smith 
96797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&diag);CHKERRQ(ierr);
96852e6d16bSBarry Smith   ierr = PetscLogObjectMemory(A,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
969273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
97035b0346bSBarry Smith     diag[i] = a->i[i+1];
971bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
972bfeeae90SHong Zhang       if (a->j[j] == i) {
973bfeeae90SHong Zhang         diag[i] = j;
97417ab2063SBarry Smith         break;
97517ab2063SBarry Smith       }
97617ab2063SBarry Smith     }
97717ab2063SBarry Smith   }
978416022c9SBarry Smith   a->diag = diag;
9793a40ed3dSBarry Smith   PetscFunctionReturn(0);
98017ab2063SBarry Smith }
98117ab2063SBarry Smith 
982be5855fcSBarry Smith /*
983be5855fcSBarry Smith      Checks for missing diagonals
984be5855fcSBarry Smith */
9854a2ae208SSatish Balay #undef __FUNCT__
9864a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
987dfbe8321SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A)
988be5855fcSBarry Smith {
989be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9906849ba73SBarry Smith   PetscErrorCode ierr;
99197f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
992be5855fcSBarry Smith 
993be5855fcSBarry Smith   PetscFunctionBegin;
994f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
995f1e2ffcdSBarry Smith   diag = a->diag;
996273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
997bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
99877431f27SBarry Smith       SETERRQ1(PETSC_ERR_PLIB,"Matrix is missing diagonal number %D",i);
999be5855fcSBarry Smith     }
1000be5855fcSBarry Smith   }
1001be5855fcSBarry Smith   PetscFunctionReturn(0);
1002be5855fcSBarry Smith }
1003be5855fcSBarry Smith 
10044a2ae208SSatish Balay #undef __FUNCT__
10054a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
100697f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
100717ab2063SBarry Smith {
1008416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1009beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1010beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1011dfbe8321SBarry Smith   PetscErrorCode     ierr;
101297f1f81fSBarry Smith   PetscInt           n = A->n,m = A->m,i;
101397f1f81fSBarry Smith   const PetscInt     *idx,*diag;
101417ab2063SBarry Smith 
10153a40ed3dSBarry Smith   PetscFunctionBegin;
1016b965ef7fSBarry Smith   its = its*lits;
101777431f27SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
101891723122SBarry Smith 
1019ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1020ed480e8bSBarry Smith   diag = a->diag;
1021ed480e8bSBarry Smith   if (!a->idiag) {
1022ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1023ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1024ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1025ed480e8bSBarry Smith 
1026ed480e8bSBarry Smith     v        = a->a;
1027ed480e8bSBarry Smith 
1028ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1029958c9bccSBarry Smith     if (omega == 1.0 && !fshift) {
1030ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1031ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1032ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1033ed480e8bSBarry Smith       }
1034efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
1035ed480e8bSBarry Smith     } else {
1036ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1037ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1038beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1039ed480e8bSBarry Smith       }
1040efee365bSSatish Balay       ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
1041beeb8507SBarry Smith     }
1042ed480e8bSBarry Smith   }
1043ed480e8bSBarry Smith   t     = a->ssor;
1044ed480e8bSBarry Smith   idiag = a->idiag;
1045ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1046ed480e8bSBarry Smith 
10471ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1048fb2e594dSBarry Smith   if (xx != bb) {
10491ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1050fb2e594dSBarry Smith   } else {
1051fb2e594dSBarry Smith     b = x;
1052fb2e594dSBarry Smith   }
1053fb2e594dSBarry Smith 
1054ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1055ed480e8bSBarry Smith   xs   = x;
105617ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
105717ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1058ed480e8bSBarry Smith     bs = b;
105917ab2063SBarry Smith     for (i=0; i<m; i++) {
1060ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1061416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1062ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1063ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
106417ab2063SBarry Smith         sum  = b[i]*d/omega;
106517ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
106617ab2063SBarry Smith         x[i] = sum;
106717ab2063SBarry Smith     }
10681ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10691ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1070efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
10713a40ed3dSBarry Smith     PetscFunctionReturn(0);
107217ab2063SBarry Smith   }
1073c783ea89SBarry Smith 
1074ed480e8bSBarry Smith 
1075fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1076fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1077fc3d8934SBarry Smith 
1078fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1079fc3d8934SBarry Smith 
1080fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1081fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
108248af12d7SBarry Smith     is the relaxation factor.
1083fc3d8934SBarry Smith     */
1084fc3d8934SBarry Smith 
108548af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
108629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
10873a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
108817ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
108917ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
109017ab2063SBarry Smith 
109117ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
109217ab2063SBarry Smith 
109317ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
109417ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
109517ab2063SBarry Smith     is the relaxation factor.
109617ab2063SBarry Smith     */
109717ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
109817ab2063SBarry Smith 
109917ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
110017ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1101416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1102ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1103ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
110417ab2063SBarry Smith       sum  = b[i];
110517ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1106ed480e8bSBarry Smith       x[i] = sum*idiag[i];
110717ab2063SBarry Smith     }
110817ab2063SBarry Smith 
110917ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1110416022c9SBarry Smith     v = a->a;
1111ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
111217ab2063SBarry Smith 
111317ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1114ed480e8bSBarry Smith     ts = t;
1115416022c9SBarry Smith     diag = a->diag;
111617ab2063SBarry Smith     for (i=0; i<m; i++) {
1117416022c9SBarry Smith       n    = diag[i] - a->i[i];
1118ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1119ed480e8bSBarry Smith       v    = a->a + a->i[i];
112017ab2063SBarry Smith       sum  = t[i];
112117ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1122ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1123733d66baSBarry Smith       /*  x = x + t */
1124733d66baSBarry Smith       x[i] += t[i];
112517ab2063SBarry Smith     }
112617ab2063SBarry Smith 
1127efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
11281ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11291ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11303a40ed3dSBarry Smith     PetscFunctionReturn(0);
113117ab2063SBarry Smith   }
113217ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
113317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
113477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
113597f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
113677d8c4bbSBarry Smith #else
113717ab2063SBarry Smith       for (i=0; i<m; i++) {
1138416022c9SBarry Smith         n    = diag[i] - a->i[i];
1139ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1140ed480e8bSBarry Smith         v    = a->a + a->i[i];
114117ab2063SBarry Smith         sum  = b[i];
114217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1143ed480e8bSBarry Smith         x[i] = sum*idiag[i];
114417ab2063SBarry Smith       }
114577d8c4bbSBarry Smith #endif
114617ab2063SBarry Smith       xb = x;
1147efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11483a40ed3dSBarry Smith     } else xb = b;
114917ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
115017ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
115117ab2063SBarry Smith       for (i=0; i<m; i++) {
1152ed480e8bSBarry Smith         x[i] *= mdiag[i];
115317ab2063SBarry Smith       }
1154efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
115517ab2063SBarry Smith     }
115617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
115777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
115897f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
115977d8c4bbSBarry Smith #else
116017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1161416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1162ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1163ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
116417ab2063SBarry Smith         sum  = xb[i];
116517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1166ed480e8bSBarry Smith         x[i] = sum*idiag[i];
116717ab2063SBarry Smith       }
116877d8c4bbSBarry Smith #endif
1169efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
117017ab2063SBarry Smith     }
117117ab2063SBarry Smith     its--;
117217ab2063SBarry Smith   }
117317ab2063SBarry Smith   while (its--) {
117417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
117577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
117697f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
117777d8c4bbSBarry Smith #else
117817ab2063SBarry Smith       for (i=0; i<m; i++) {
1179ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1180416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1181ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1182ed480e8bSBarry Smith         v    = a->a + a->i[i];
118317ab2063SBarry Smith         sum  = b[i];
118417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1185ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
118617ab2063SBarry Smith       }
118777d8c4bbSBarry Smith #endif
1188efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
118917ab2063SBarry Smith     }
119017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
119177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
119297f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
119377d8c4bbSBarry Smith #else
119417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1195ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1196416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1197ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1198ed480e8bSBarry Smith         v    = a->a + a->i[i];
119917ab2063SBarry Smith         sum  = b[i];
120017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1201ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
120217ab2063SBarry Smith       }
120377d8c4bbSBarry Smith #endif
1204efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
120517ab2063SBarry Smith     }
120617ab2063SBarry Smith   }
12071ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12081ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12093a40ed3dSBarry Smith   PetscFunctionReturn(0);
121017ab2063SBarry Smith }
121117ab2063SBarry Smith 
12124a2ae208SSatish Balay #undef __FUNCT__
12134a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1214dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
121517ab2063SBarry Smith {
1216416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12174e220ebcSLois Curfman McInnes 
12183a40ed3dSBarry Smith   PetscFunctionBegin;
1219273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1220273d9f13SBarry Smith   info->columns_global = (double)A->n;
1221273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1222273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12234e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12244e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12254e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12264e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12274e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12284e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12294e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12304e220ebcSLois Curfman McInnes   if (A->factor) {
12314e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12324e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12334e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12344e220ebcSLois Curfman McInnes   } else {
12354e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12364e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12374e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12384e220ebcSLois Curfman McInnes   }
12393a40ed3dSBarry Smith   PetscFunctionReturn(0);
124017ab2063SBarry Smith }
124117ab2063SBarry Smith 
12424a2ae208SSatish Balay #undef __FUNCT__
12434a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1244dfbe8321SBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,IS is,const PetscScalar *diag)
124517ab2063SBarry Smith {
1246416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12476849ba73SBarry Smith   PetscErrorCode ierr;
124897f1f81fSBarry Smith   PetscInt       i,N,*rows,m = A->m - 1;
124917ab2063SBarry Smith 
12503a40ed3dSBarry Smith   PetscFunctionBegin;
1251b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
125217ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1253f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1254f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
125577431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1256bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1257f1e2ffcdSBarry Smith     }
1258f1e2ffcdSBarry Smith     if (diag) {
1259f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1260f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1261f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1262f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1263f1e2ffcdSBarry Smith       }
1264f1e2ffcdSBarry Smith     }
126588e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1266f1e2ffcdSBarry Smith   } else {
126717ab2063SBarry Smith     if (diag) {
126817ab2063SBarry Smith       for (i=0; i<N; i++) {
126977431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
12707ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1271416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1272bfeeae90SHong Zhang           a->a[a->i[rows[i]]] = *diag;
1273bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
12747ae801bdSBarry Smith         } else { /* in case row was completely empty */
1275d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
127617ab2063SBarry Smith         }
127717ab2063SBarry Smith       }
12783a40ed3dSBarry Smith     } else {
127917ab2063SBarry Smith       for (i=0; i<N; i++) {
128077431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1281416022c9SBarry Smith         a->ilen[rows[i]] = 0;
128217ab2063SBarry Smith       }
128317ab2063SBarry Smith     }
128488e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1285f1e2ffcdSBarry Smith   }
12867ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
128743a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12883a40ed3dSBarry Smith   PetscFunctionReturn(0);
128917ab2063SBarry Smith }
129017ab2063SBarry Smith 
12914a2ae208SSatish Balay #undef __FUNCT__
12924a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
129397f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
129417ab2063SBarry Smith {
1295416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
129697f1f81fSBarry Smith   PetscInt   *itmp;
129717ab2063SBarry Smith 
12983a40ed3dSBarry Smith   PetscFunctionBegin;
129977431f27SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
130017ab2063SBarry Smith 
1301416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1302bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
130317ab2063SBarry Smith   if (idx) {
1304bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1305bfeeae90SHong Zhang     if (*nz) {
13064e093b46SBarry Smith       *idx = itmp;
130717ab2063SBarry Smith     }
130817ab2063SBarry Smith     else *idx = 0;
130917ab2063SBarry Smith   }
13103a40ed3dSBarry Smith   PetscFunctionReturn(0);
131117ab2063SBarry Smith }
131217ab2063SBarry Smith 
1313bfeeae90SHong Zhang /* remove this function? */
13144a2ae208SSatish Balay #undef __FUNCT__
13154a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
131697f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
131717ab2063SBarry Smith {
13183a40ed3dSBarry Smith   PetscFunctionBegin;
13193a40ed3dSBarry Smith   PetscFunctionReturn(0);
132017ab2063SBarry Smith }
132117ab2063SBarry Smith 
13224a2ae208SSatish Balay #undef __FUNCT__
13234a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1324dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
132517ab2063SBarry Smith {
1326416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
132787828ca2SBarry Smith   PetscScalar    *v = a->a;
132836db0b34SBarry Smith   PetscReal      sum = 0.0;
13296849ba73SBarry Smith   PetscErrorCode ierr;
133097f1f81fSBarry Smith   PetscInt       i,j;
133117ab2063SBarry Smith 
13323a40ed3dSBarry Smith   PetscFunctionBegin;
133317ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1334416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1335aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
133636db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
133717ab2063SBarry Smith #else
133817ab2063SBarry Smith       sum += (*v)*(*v); v++;
133917ab2063SBarry Smith #endif
134017ab2063SBarry Smith     }
1341064f8208SBarry Smith     *nrm = sqrt(sum);
13423a40ed3dSBarry Smith   } else if (type == NORM_1) {
134336db0b34SBarry Smith     PetscReal *tmp;
134497f1f81fSBarry Smith     PetscInt    *jj = a->j;
1345b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1346273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1347064f8208SBarry Smith     *nrm = 0.0;
1348416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1349bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
135017ab2063SBarry Smith     }
1351273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1352064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
135317ab2063SBarry Smith     }
1354606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13553a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1356064f8208SBarry Smith     *nrm = 0.0;
1357273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1358bfeeae90SHong Zhang       v = a->a + a->i[j];
135917ab2063SBarry Smith       sum = 0.0;
1360416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1361cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
136217ab2063SBarry Smith       }
1363064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
136417ab2063SBarry Smith     }
13653a40ed3dSBarry Smith   } else {
136629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
136717ab2063SBarry Smith   }
13683a40ed3dSBarry Smith   PetscFunctionReturn(0);
136917ab2063SBarry Smith }
137017ab2063SBarry Smith 
13714a2ae208SSatish Balay #undef __FUNCT__
13724a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1373dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B)
137417ab2063SBarry Smith {
1375416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1376416022c9SBarry Smith   Mat            C;
13776849ba73SBarry Smith   PetscErrorCode ierr;
137897f1f81fSBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->m,len,*col;
137987828ca2SBarry Smith   PetscScalar    *array = a->a;
138017ab2063SBarry Smith 
13813a40ed3dSBarry Smith   PetscFunctionBegin;
1382273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
138397f1f81fSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
138497f1f81fSBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(PetscInt));CHKERRQ(ierr);
1385bfeeae90SHong Zhang 
1386bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1387*f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1388*f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->n,m,A->n,m);CHKERRQ(ierr);
1389f204ca49SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1390ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1391606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
139217ab2063SBarry Smith   for (i=0; i<m; i++) {
139317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1394416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1395b9b97703SBarry Smith     array += len;
1396b9b97703SBarry Smith     aj    += len;
139717ab2063SBarry Smith   }
139817ab2063SBarry Smith 
13996d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14006d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
140117ab2063SBarry Smith 
1402f1e2ffcdSBarry Smith   if (B) {
1403416022c9SBarry Smith     *B = C;
140417ab2063SBarry Smith   } else {
1405273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
140617ab2063SBarry Smith   }
14073a40ed3dSBarry Smith   PetscFunctionReturn(0);
140817ab2063SBarry Smith }
140917ab2063SBarry Smith 
1410cd0d46ebSvictorle EXTERN_C_BEGIN
1411cd0d46ebSvictorle #undef __FUNCT__
14125fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1413be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1414cd0d46ebSvictorle {
1415cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
141697f1f81fSBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
14176849ba73SBarry Smith   PetscErrorCode ierr;
141897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1419cd0d46ebSvictorle 
1420cd0d46ebSvictorle   PetscFunctionBegin;
1421cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1422cd0d46ebSvictorle 
1423cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1424cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
14255485867bSBarry Smith   if (ma!=nb || na!=mb){
14265485867bSBarry Smith     *f = PETSC_FALSE;
14275485867bSBarry Smith     PetscFunctionReturn(0);
14285485867bSBarry Smith   }
1429cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1430cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1431cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
143297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
143397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1434cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1435cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1436cd0d46ebSvictorle 
1437cd0d46ebSvictorle   *f = PETSC_TRUE;
1438cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1439cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
144097f1f81fSBarry Smith       PetscInt         idc,idr;
14415485867bSBarry Smith       PetscScalar vc,vr;
1442cd0d46ebSvictorle       /* column/row index/value */
14435485867bSBarry Smith       idc = adx[aptr[i]];
14445485867bSBarry Smith       idr = bdx[bptr[idc]];
14455485867bSBarry Smith       vc  = va[aptr[i]];
14465485867bSBarry Smith       vr  = vb[bptr[idc]];
14475485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
14485485867bSBarry Smith 	*f = PETSC_FALSE;
14495485867bSBarry Smith         goto done;
1450cd0d46ebSvictorle       } else {
14515485867bSBarry Smith 	aptr[i]++;
14525485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1453cd0d46ebSvictorle       }
1454cd0d46ebSvictorle     }
1455cd0d46ebSvictorle   }
1456cd0d46ebSvictorle  done:
1457cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
14583aeef889SHong Zhang   if (B) {
14593aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
14603aeef889SHong Zhang   }
1461cd0d46ebSvictorle   PetscFunctionReturn(0);
1462cd0d46ebSvictorle }
1463cd0d46ebSvictorle EXTERN_C_END
1464cd0d46ebSvictorle 
14659e29f15eSvictorle #undef __FUNCT__
14669e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1467dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
14689e29f15eSvictorle {
1469dfbe8321SBarry Smith   PetscErrorCode ierr;
14709e29f15eSvictorle   PetscFunctionBegin;
14715485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
14729e29f15eSvictorle   PetscFunctionReturn(0);
14739e29f15eSvictorle }
14749e29f15eSvictorle 
14754a2ae208SSatish Balay #undef __FUNCT__
14764a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1477dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
147817ab2063SBarry Smith {
1479416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
148087828ca2SBarry Smith   PetscScalar    *l,*r,x,*v;
1481dfbe8321SBarry Smith   PetscErrorCode ierr;
148297f1f81fSBarry Smith   PetscInt       i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
148317ab2063SBarry Smith 
14843a40ed3dSBarry Smith   PetscFunctionBegin;
148517ab2063SBarry Smith   if (ll) {
14863ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14873ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1488e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1489273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
14901ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1491416022c9SBarry Smith     v = a->a;
149217ab2063SBarry Smith     for (i=0; i<m; i++) {
149317ab2063SBarry Smith       x = l[i];
1494416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
149517ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
149617ab2063SBarry Smith     }
14971ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1498efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
149917ab2063SBarry Smith   }
150017ab2063SBarry Smith   if (rr) {
1501e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1502273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
15031ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1504416022c9SBarry Smith     v = a->a; jj = a->j;
150517ab2063SBarry Smith     for (i=0; i<nz; i++) {
1506bfeeae90SHong Zhang       (*v++) *= r[*jj++];
150717ab2063SBarry Smith     }
15081ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1509efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
151017ab2063SBarry Smith   }
15113a40ed3dSBarry Smith   PetscFunctionReturn(0);
151217ab2063SBarry Smith }
151317ab2063SBarry Smith 
15144a2ae208SSatish Balay #undef __FUNCT__
15154a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
151697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
151717ab2063SBarry Smith {
1518db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
15196849ba73SBarry Smith   PetscErrorCode ierr;
152097f1f81fSBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->n,*lens;
152197f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
152297f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
152397f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
152487828ca2SBarry Smith   PetscScalar    *a_new,*mat_a;
1525416022c9SBarry Smith   Mat            C;
1526fee21e36SBarry Smith   PetscTruth     stride;
152717ab2063SBarry Smith 
15283a40ed3dSBarry Smith   PetscFunctionBegin;
1529d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
153029bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1531d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
153229bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
153399141d43SSatish Balay 
153417ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1535b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1536b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
153717ab2063SBarry Smith 
1538fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1539fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1540fee21e36SBarry Smith   if (stride && step == 1) {
154102834360SBarry Smith     /* special case of contiguous rows */
154297f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
154331ebf83bSSatish Balay     starts = lens + nrows;
154402834360SBarry Smith     /* loop over new rows determining lens and starting points */
154502834360SBarry Smith     for (i=0; i<nrows; i++) {
1546bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1547a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
154802834360SBarry Smith       for (k=kstart; k<kend; k++) {
1549bfeeae90SHong Zhang         if (aj[k] >= first) {
155002834360SBarry Smith           starts[i] = k;
155102834360SBarry Smith           break;
155202834360SBarry Smith 	}
155302834360SBarry Smith       }
1554a2744918SBarry Smith       sum = 0;
155502834360SBarry Smith       while (k < kend) {
1556bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1557a2744918SBarry Smith         sum++;
155802834360SBarry Smith       }
1559a2744918SBarry Smith       lens[i] = sum;
156002834360SBarry Smith     }
156102834360SBarry Smith     /* create submatrix */
1562cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
156397f1f81fSBarry Smith       PetscInt n_cols,n_rows;
156408480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
156529bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1566d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
156708480c60SBarry Smith       C = *B;
15683a40ed3dSBarry Smith     } else {
1569*f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1570*f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1571e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1572ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
157308480c60SBarry Smith     }
1574db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1575db02288aSLois Curfman McInnes 
157602834360SBarry Smith     /* loop over rows inserting into submatrix */
1577db02288aSLois Curfman McInnes     a_new    = c->a;
1578db02288aSLois Curfman McInnes     j_new    = c->j;
1579db02288aSLois Curfman McInnes     i_new    = c->i;
1580bfeeae90SHong Zhang 
158102834360SBarry Smith     for (i=0; i<nrows; i++) {
1582a2744918SBarry Smith       ii    = starts[i];
1583a2744918SBarry Smith       lensi = lens[i];
1584a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1585a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
158602834360SBarry Smith       }
158787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1588a2744918SBarry Smith       a_new      += lensi;
1589a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1590a2744918SBarry Smith       c->ilen[i]  = lensi;
159102834360SBarry Smith     }
1592606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15933a40ed3dSBarry Smith   } else {
159402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
159597f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1596bfeeae90SHong Zhang 
159797f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
159897f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
159917ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
160002834360SBarry Smith     /* determine lens of each row */
160102834360SBarry Smith     for (i=0; i<nrows; i++) {
1602bfeeae90SHong Zhang       kstart  = ai[irow[i]];
160302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
160402834360SBarry Smith       lens[i] = 0;
160502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1606bfeeae90SHong Zhang         if (smap[aj[k]]) {
160702834360SBarry Smith           lens[i]++;
160802834360SBarry Smith         }
160902834360SBarry Smith       }
161002834360SBarry Smith     }
161117ab2063SBarry Smith     /* Create and fill new matrix */
1612a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16130f5bd95cSBarry Smith       PetscTruth equal;
16140f5bd95cSBarry Smith 
161599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1616273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
161797f1f81fSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(PetscInt),&equal);CHKERRQ(ierr);
16180f5bd95cSBarry Smith       if (!equal) {
161929bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
162099141d43SSatish Balay       }
162197f1f81fSBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(PetscInt));CHKERRQ(ierr);
162208480c60SBarry Smith       C = *B;
16233a40ed3dSBarry Smith     } else {
1624*f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1625*f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1626e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1627ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
162808480c60SBarry Smith     }
162999141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
163017ab2063SBarry Smith     for (i=0; i<nrows; i++) {
163199141d43SSatish Balay       row    = irow[i];
1632bfeeae90SHong Zhang       kstart = ai[row];
163399141d43SSatish Balay       kend   = kstart + a->ilen[row];
1634bfeeae90SHong Zhang       mat_i  = c->i[i];
163599141d43SSatish Balay       mat_j  = c->j + mat_i;
163699141d43SSatish Balay       mat_a  = c->a + mat_i;
163799141d43SSatish Balay       mat_ilen = c->ilen + i;
163817ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1639bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1640ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
164199141d43SSatish Balay           *mat_a++ = a->a[k];
164299141d43SSatish Balay           (*mat_ilen)++;
164399141d43SSatish Balay 
164417ab2063SBarry Smith         }
164517ab2063SBarry Smith       }
164617ab2063SBarry Smith     }
164702834360SBarry Smith     /* Free work space */
164802834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1649606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1650606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
165102834360SBarry Smith   }
16526d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16536d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
165417ab2063SBarry Smith 
165517ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1656416022c9SBarry Smith   *B = C;
16573a40ed3dSBarry Smith   PetscFunctionReturn(0);
165817ab2063SBarry Smith }
165917ab2063SBarry Smith 
1660a871dcd8SBarry Smith /*
1661a871dcd8SBarry Smith */
16624a2ae208SSatish Balay #undef __FUNCT__
16634a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1664dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1665a871dcd8SBarry Smith {
166663b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1667dfbe8321SBarry Smith   PetscErrorCode ierr;
166863b91edcSBarry Smith   Mat            outA;
1669b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
167063b91edcSBarry Smith 
16713a40ed3dSBarry Smith   PetscFunctionBegin;
1672d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1673b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1674b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1675b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1676634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
1677b8a78c4aSBarry Smith   }
1678a871dcd8SBarry Smith 
167963b91edcSBarry Smith   outA          = inA;
168063b91edcSBarry Smith   inA->factor   = FACTOR_LU;
168163b91edcSBarry Smith   a->row        = row;
168263b91edcSBarry Smith   a->col        = col;
1683c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1684c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
168563b91edcSBarry Smith 
168636db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1687b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16884c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
168952e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1690f0ec6fceSSatish Balay 
169194a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
169287828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
169394a9d846SBarry Smith   }
169463b91edcSBarry Smith 
169508480c60SBarry Smith   if (!a->diag) {
1696f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
169763b91edcSBarry Smith   }
1698af281ebdSHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
16993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1700a871dcd8SBarry Smith }
1701a871dcd8SBarry Smith 
1702d9eff348SSatish Balay #include "petscblaslapack.h"
17034a2ae208SSatish Balay #undef __FUNCT__
17044a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1705dfbe8321SBarry Smith PetscErrorCode MatScale_SeqAIJ(const PetscScalar *alpha,Mat inA)
1706f0b747eeSBarry Smith {
1707f0b747eeSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)inA->data;
17084ce68768SBarry Smith   PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1;
1709efee365bSSatish Balay   PetscErrorCode ierr;
1710efee365bSSatish Balay 
17113a40ed3dSBarry Smith 
17123a40ed3dSBarry Smith   PetscFunctionBegin;
171371044d3cSBarry Smith   BLASscal_(&bnz,(PetscScalar*)alpha,a->a,&one);
1714efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17153a40ed3dSBarry Smith   PetscFunctionReturn(0);
1716f0b747eeSBarry Smith }
1717f0b747eeSBarry Smith 
17184a2ae208SSatish Balay #undef __FUNCT__
17194a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
172097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1721cddf8d76SBarry Smith {
1722dfbe8321SBarry Smith   PetscErrorCode ierr;
172397f1f81fSBarry Smith   PetscInt       i;
1724cddf8d76SBarry Smith 
17253a40ed3dSBarry Smith   PetscFunctionBegin;
1726cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1727b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1728cddf8d76SBarry Smith   }
1729cddf8d76SBarry Smith 
1730cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17316a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1732cddf8d76SBarry Smith   }
17333a40ed3dSBarry Smith   PetscFunctionReturn(0);
1734cddf8d76SBarry Smith }
1735cddf8d76SBarry Smith 
17364a2ae208SSatish Balay #undef __FUNCT__
17374a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
173897f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
17394dcbc457SBarry Smith {
1740e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17416849ba73SBarry Smith   PetscErrorCode ierr;
174297f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
174397f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1744f1af5d2fSBarry Smith   PetscBT        table;
1745bbd702dbSSatish Balay 
17463a40ed3dSBarry Smith   PetscFunctionBegin;
1747273d9f13SBarry Smith   m     = A->m;
1748e4d965acSSatish Balay   ai    = a->i;
1749bfeeae90SHong Zhang   aj    = a->j;
17508a047759SSatish Balay 
1751a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
175206763907SSatish Balay 
175397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
17546831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
175506763907SSatish Balay 
1756e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1757b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1758e4d965acSSatish Balay     isz  = 0;
17596831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1760e4d965acSSatish Balay 
1761e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17624dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1763b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1764e4d965acSSatish Balay 
1765dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1766e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1767f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17684dcbc457SBarry Smith     }
176906763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
177006763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1771e4d965acSSatish Balay 
177204a348a9SBarry Smith     k = 0;
177304a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
177404a348a9SBarry Smith       n = isz;
177506763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1776e4d965acSSatish Balay         row   = nidx[k];
1777e4d965acSSatish Balay         start = ai[row];
1778e4d965acSSatish Balay         end   = ai[row+1];
177904a348a9SBarry Smith         for (l = start; l<end ; l++){
1780efb16452SHong Zhang           val = aj[l] ;
1781f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1782e4d965acSSatish Balay         }
1783e4d965acSSatish Balay       }
1784e4d965acSSatish Balay     }
1785029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1786e4d965acSSatish Balay   }
17876831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1788606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17893a40ed3dSBarry Smith   PetscFunctionReturn(0);
17904dcbc457SBarry Smith }
179117ab2063SBarry Smith 
17920513a670SBarry Smith /* -------------------------------------------------------------- */
17934a2ae208SSatish Balay #undef __FUNCT__
17944a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1795dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17960513a670SBarry Smith {
17970513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17986849ba73SBarry Smith   PetscErrorCode ierr;
179997f1f81fSBarry Smith   PetscInt       i,nz,m = A->m,n = A->n,*col;
180097f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
180156cd22aeSBarry Smith   IS             icolp,irowp;
180297f1f81fSBarry Smith   PetscInt       *cwork;
180332ec9ce4SBarry Smith   PetscScalar    *vwork;
18040513a670SBarry Smith 
18053a40ed3dSBarry Smith   PetscFunctionBegin;
18064c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
180756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18084c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
180956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18100513a670SBarry Smith 
18110513a670SBarry Smith   /* determine lengths of permuted rows */
181297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
18130513a670SBarry Smith   for (i=0; i<m; i++) {
18140513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18150513a670SBarry Smith   }
1816*f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,B);CHKERRQ(ierr);
1817*f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
1818f204ca49SKris Buschelman   ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr);
1819ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1820606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18210513a670SBarry Smith 
182297f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
18230513a670SBarry Smith   for (i=0; i<m; i++) {
182432ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18250513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1826cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
182732ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18280513a670SBarry Smith   }
1829606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18303c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
18310513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18320513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
183356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
183456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
183556cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
183656cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18373a40ed3dSBarry Smith   PetscFunctionReturn(0);
18380513a670SBarry Smith }
18390513a670SBarry Smith 
18404a2ae208SSatish Balay #undef __FUNCT__
18414a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1842dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_SeqAIJ(Mat A)
1843682d7d0cSBarry Smith {
1844c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1845682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1846dfbe8321SBarry Smith   PetscErrorCode    ierr;
1847682d7d0cSBarry Smith 
18483a40ed3dSBarry Smith   PetscFunctionBegin;
18494846f1f5SKris Buschelman   ierr = MatPrintHelp_Inode(A);CHKERRQ(ierr);
1850c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1851d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1852d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1853d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
185473e7a558SHong Zhang   ierr = (*PetscHelpPrintf)(comm,"  -mat_no_compressedrow: Do not use compressedrow\n");CHKERRQ(ierr);
18553a40ed3dSBarry Smith   PetscFunctionReturn(0);
1856682d7d0cSBarry Smith }
185797304618SKris Buschelman 
18584a2ae208SSatish Balay #undef __FUNCT__
18594a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1860dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1861cb5b572fSBarry Smith {
1862dfbe8321SBarry Smith   PetscErrorCode ierr;
1863cb5b572fSBarry Smith 
1864cb5b572fSBarry Smith   PetscFunctionBegin;
186533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
186633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1867be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1868be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1869be6bf707SBarry Smith 
1870bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
1871634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1872cb5b572fSBarry Smith     }
1873bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1874cb5b572fSBarry Smith   } else {
1875cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1876cb5b572fSBarry Smith   }
1877cb5b572fSBarry Smith   PetscFunctionReturn(0);
1878cb5b572fSBarry Smith }
1879cb5b572fSBarry Smith 
18804a2ae208SSatish Balay #undef __FUNCT__
18814a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1882dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
1883273d9f13SBarry Smith {
1884dfbe8321SBarry Smith   PetscErrorCode ierr;
1885273d9f13SBarry Smith 
1886273d9f13SBarry Smith   PetscFunctionBegin;
1887ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1888273d9f13SBarry Smith   PetscFunctionReturn(0);
1889273d9f13SBarry Smith }
1890273d9f13SBarry Smith 
18914a2ae208SSatish Balay #undef __FUNCT__
18924a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
1893dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
18946c0721eeSBarry Smith {
18956c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18966c0721eeSBarry Smith   PetscFunctionBegin;
18976c0721eeSBarry Smith   *array = a->a;
18986c0721eeSBarry Smith   PetscFunctionReturn(0);
18996c0721eeSBarry Smith }
19006c0721eeSBarry Smith 
19014a2ae208SSatish Balay #undef __FUNCT__
19024a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
1903dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19046c0721eeSBarry Smith {
19056c0721eeSBarry Smith   PetscFunctionBegin;
19066c0721eeSBarry Smith   PetscFunctionReturn(0);
19076c0721eeSBarry Smith }
1908273d9f13SBarry Smith 
1909ee4f033dSBarry Smith #undef __FUNCT__
1910ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1911dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1912ee4f033dSBarry Smith {
19136849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
19146849ba73SBarry Smith   PetscErrorCode ierr;
191597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
191687828ca2SBarry Smith   PetscScalar    dx,mone = -1.0,*y,*xx,*w3_array;
191787828ca2SBarry Smith   PetscScalar    *vscale_array;
1918ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
1919ee4f033dSBarry Smith   Vec            w1,w2,w3;
1920ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
1921ee4f033dSBarry Smith   PetscTruth     flg;
1922ee4f033dSBarry Smith 
1923ee4f033dSBarry Smith   PetscFunctionBegin;
1924ee4f033dSBarry Smith   if (!coloring->w1) {
1925ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
192652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
1927ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
192852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
1929ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
193052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
1931ee4f033dSBarry Smith   }
1932ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1933ee4f033dSBarry Smith 
1934ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1935e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1936ee4f033dSBarry Smith   if (flg) {
193763ba0a88SBarry Smith     ierr = PetscLogInfo((coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"));CHKERRQ(ierr);
1938ee4f033dSBarry Smith   } else {
19390b9b6f31SBarry Smith     PetscTruth assembled;
19400b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
19410b9b6f31SBarry Smith     if (assembled) {
1942ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
1943ee4f033dSBarry Smith     }
19440b9b6f31SBarry Smith   }
1945ee4f033dSBarry Smith 
1946ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1947ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1948ee4f033dSBarry Smith 
1949ee4f033dSBarry Smith   /*
1950ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1951ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1952ee4f033dSBarry Smith   */
1953ee4f033dSBarry Smith   if (coloring->F) {
1954ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1955ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1956ee4f033dSBarry Smith     if (m1 != m2) {
1957ee4f033dSBarry Smith       coloring->F = 0;
1958ee4f033dSBarry Smith     }
1959ee4f033dSBarry Smith   }
1960ee4f033dSBarry Smith 
1961ee4f033dSBarry Smith   if (coloring->F) {
1962ee4f033dSBarry Smith     w1          = coloring->F;
1963ee4f033dSBarry Smith     coloring->F = 0;
1964ee4f033dSBarry Smith   } else {
196566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1966ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
196766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1968ee4f033dSBarry Smith   }
1969ee4f033dSBarry Smith 
1970ee4f033dSBarry Smith   /*
1971ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1972ee4f033dSBarry Smith   */
19731ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
19741ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1975ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1976ee4f033dSBarry Smith     /*
1977ee4f033dSBarry Smith        Loop over each column associated with color adding the
1978ee4f033dSBarry Smith        perturbation to the vector w3.
1979ee4f033dSBarry Smith     */
1980ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1981ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1982ee4f033dSBarry Smith       dx  = xx[col];
1983ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1984ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1985ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1986ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1987ee4f033dSBarry Smith #else
1988ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1989ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1990ee4f033dSBarry Smith #endif
1991ee4f033dSBarry Smith       dx                *= epsilon;
1992ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1993ee4f033dSBarry Smith     }
1994ee4f033dSBarry Smith   }
19951ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1996ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1997ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1998ee4f033dSBarry Smith 
1999ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2000ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2001ee4f033dSBarry Smith 
2002ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2003ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2004ee4f033dSBarry Smith 
20051ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2006ee4f033dSBarry Smith   /*
2007ee4f033dSBarry Smith       Loop over each color
2008ee4f033dSBarry Smith   */
2009ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
201049b058dcSBarry Smith     coloring->currentcolor = k;
2011ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
20121ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2013ee4f033dSBarry Smith     /*
2014ee4f033dSBarry Smith        Loop over each column associated with color adding the
2015ee4f033dSBarry Smith        perturbation to the vector w3.
2016ee4f033dSBarry Smith     */
2017ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2018ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2019ee4f033dSBarry Smith       dx  = xx[col];
20205b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2021ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2022ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2023ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2024ee4f033dSBarry Smith #else
2025ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2026ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2027ee4f033dSBarry Smith #endif
2028ee4f033dSBarry Smith       dx            *= epsilon;
2029634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2030ee4f033dSBarry Smith       w3_array[col] += dx;
2031ee4f033dSBarry Smith     }
20321ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2033ee4f033dSBarry Smith 
2034ee4f033dSBarry Smith     /*
2035ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2036ee4f033dSBarry Smith     */
2037ee4f033dSBarry Smith 
203866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2039ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
204066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
20412dcb1b2aSMatthew Knepley     ierr = VecAXPY(w2,mone,w1);CHKERRQ(ierr);
2042ee4f033dSBarry Smith 
2043ee4f033dSBarry Smith     /*
2044ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2045ee4f033dSBarry Smith     */
20461ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2047ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2048ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2049ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2050ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2051ee4f033dSBarry Smith       srow   = row + start;
2052ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2053ee4f033dSBarry Smith     }
20541ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2055ee4f033dSBarry Smith   }
205649b058dcSBarry Smith   coloring->currentcolor = k;
20571ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
20581ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2059ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2060ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2061ee4f033dSBarry Smith   PetscFunctionReturn(0);
2062ee4f033dSBarry Smith }
2063ee4f033dSBarry Smith 
2064ac90fabeSBarry Smith #include "petscblaslapack.h"
2065ac90fabeSBarry Smith #undef __FUNCT__
2066ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2067dfbe8321SBarry Smith PetscErrorCode MatAXPY_SeqAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str)
2068ac90fabeSBarry Smith {
2069dfbe8321SBarry Smith   PetscErrorCode ierr;
207097f1f81fSBarry Smith   PetscInt       i;
2071ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
20724ce68768SBarry Smith   PetscBLASInt   one=1,bnz = (PetscBLASInt)x->nz;
2073ac90fabeSBarry Smith 
2074ac90fabeSBarry Smith   PetscFunctionBegin;
2075ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
207671044d3cSBarry Smith     BLASaxpy_(&bnz,(PetscScalar*)a,x->a,&one,y->a,&one);
2077c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2078a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2079a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2080a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2081a30b2313SHong Zhang     }
2082a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
208324f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2084a30b2313SHong Zhang       y->XtoY = X;
2085c537a176SHong Zhang     }
2086a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
208763ba0a88SBarry Smith     ierr = PetscLogInfo((0,"MatAXPY_SeqAIJ: ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz));CHKERRQ(ierr);
2088ac90fabeSBarry Smith   } else {
2089ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
2090ac90fabeSBarry Smith   }
2091ac90fabeSBarry Smith   PetscFunctionReturn(0);
2092ac90fabeSBarry Smith }
2093ac90fabeSBarry Smith 
2094521d7252SBarry Smith #undef __FUNCT__
2095521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2096521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2097521d7252SBarry Smith {
2098521d7252SBarry Smith   PetscFunctionBegin;
2099521d7252SBarry Smith   PetscFunctionReturn(0);
2100521d7252SBarry Smith }
2101521d7252SBarry Smith 
2102354c94deSBarry Smith #undef __FUNCT__
2103354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2104354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2105354c94deSBarry Smith {
2106354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2107354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2108354c94deSBarry Smith   PetscInt    i,nz;
2109354c94deSBarry Smith   PetscScalar *a;
2110354c94deSBarry Smith 
2111354c94deSBarry Smith   PetscFunctionBegin;
2112354c94deSBarry Smith   nz = aij->nz;
2113354c94deSBarry Smith   a  = aij->a;
2114354c94deSBarry Smith   for (i=0; i<nz; i++) {
2115354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2116354c94deSBarry Smith   }
2117354c94deSBarry Smith #else
2118354c94deSBarry Smith   PetscFunctionBegin;
2119354c94deSBarry Smith #endif
2120354c94deSBarry Smith   PetscFunctionReturn(0);
2121354c94deSBarry Smith }
2122354c94deSBarry Smith 
2123682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
21240a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2125cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2126cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2127cb5b572fSBarry Smith        MatMult_SeqAIJ,
212897304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
21297c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
21307c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2131cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2132cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
21337c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
213497304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2135cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2136cb5b572fSBarry Smith        0,
213717ab2063SBarry Smith        MatRelax_SeqAIJ,
213817ab2063SBarry Smith        MatTranspose_SeqAIJ,
213997304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2140cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2141cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2142cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2143cb5b572fSBarry Smith        MatNorm_SeqAIJ,
214497304618SKris Buschelman /*20*/ 0,
2145cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
214617ab2063SBarry Smith        MatCompress_SeqAIJ,
2147cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2148cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
214997304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2150cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2151cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2152f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2153a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
215497304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2155cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2156861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
21576c0721eeSBarry Smith        MatGetArray_SeqAIJ,
21586c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
215997304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2160cb5b572fSBarry Smith        0,
2161cb5b572fSBarry Smith        0,
2162cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2163cb5b572fSBarry Smith        0,
216497304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2165cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2166cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2167cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2168cb5b572fSBarry Smith        MatCopy_SeqAIJ,
216997304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ,
2170cb5b572fSBarry Smith        MatScale_SeqAIJ,
2171cb5b572fSBarry Smith        0,
2172cb5b572fSBarry Smith        0,
21736945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2174521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
21753b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
21763b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
21773b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2178a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
217997304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2180b9617806SBarry Smith        0,
21810513a670SBarry Smith        0,
2182cda55fadSBarry Smith        MatPermute_SeqAIJ,
2183cda55fadSBarry Smith        0,
218497304618SKris Buschelman /*60*/ 0,
2185b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2186b9b97703SBarry Smith        MatView_SeqAIJ,
21878a124369SBarry Smith        MatGetPetscMaps_Petsc,
2188ee4f033dSBarry Smith        0,
218997304618SKris Buschelman /*65*/ 0,
2190ee4f033dSBarry Smith        0,
2191ee4f033dSBarry Smith        0,
2192ee4f033dSBarry Smith        0,
2193ee4f033dSBarry Smith        0,
219497304618SKris Buschelman /*70*/ 0,
2195ee4f033dSBarry Smith        0,
2196ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2197dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2198ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2199dcf5cc72SBarry Smith #else
2200dcf5cc72SBarry Smith        0,
2201dcf5cc72SBarry Smith #endif
2202ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
220397304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ,
220497304618SKris Buschelman        0,
220597304618SKris Buschelman        0,
220697304618SKris Buschelman        0,
220797304618SKris Buschelman        0,
220897304618SKris Buschelman /*80*/ 0,
220997304618SKris Buschelman        0,
221097304618SKris Buschelman        0,
221197304618SKris Buschelman        0,
2212bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2213bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
22146284ec50SHong Zhang        0,
22156284ec50SHong Zhang        0,
22166284ec50SHong Zhang        0,
2217bc011b1eSHong Zhang        0,
2218bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
221926be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
222026be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2221d439da42SKris Buschelman        MatPtAP_Basic,
22227ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
22237ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2224bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2225bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2226bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
22277ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
22287ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2229609c6c4dSKris Buschelman        0,
2230609c6c4dSKris Buschelman        0,
2231354c94deSBarry Smith        MatConjugate_SeqAIJ
22329e29f15eSvictorle };
223317ab2063SBarry Smith 
2234fb2e594dSBarry Smith EXTERN_C_BEGIN
22354a2ae208SSatish Balay #undef __FUNCT__
22364a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2237be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2238bef8e0ddSBarry Smith {
2239bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
224097f1f81fSBarry Smith   PetscInt   i,nz,n;
2241bef8e0ddSBarry Smith 
2242bef8e0ddSBarry Smith   PetscFunctionBegin;
2243bef8e0ddSBarry Smith 
2244bef8e0ddSBarry Smith   nz = aij->maxnz;
2245273d9f13SBarry Smith   n  = mat->n;
2246bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2247bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2248bef8e0ddSBarry Smith   }
2249bef8e0ddSBarry Smith   aij->nz = nz;
2250bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2251bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2252bef8e0ddSBarry Smith   }
2253bef8e0ddSBarry Smith 
2254bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2255bef8e0ddSBarry Smith }
2256fb2e594dSBarry Smith EXTERN_C_END
2257bef8e0ddSBarry Smith 
22584a2ae208SSatish Balay #undef __FUNCT__
22594a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2260bef8e0ddSBarry Smith /*@
2261bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2262bef8e0ddSBarry Smith        in the matrix.
2263bef8e0ddSBarry Smith 
2264bef8e0ddSBarry Smith   Input Parameters:
2265bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2266bef8e0ddSBarry Smith -  indices - the column indices
2267bef8e0ddSBarry Smith 
226815091d37SBarry Smith   Level: advanced
226915091d37SBarry Smith 
2270bef8e0ddSBarry Smith   Notes:
2271bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2272bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2273bef8e0ddSBarry Smith   of the MatSetValues() operation.
2274bef8e0ddSBarry Smith 
2275bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2276d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2277bef8e0ddSBarry Smith 
2278bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2279bef8e0ddSBarry Smith 
2280b9617806SBarry Smith     The indices should start with zero, not one.
2281b9617806SBarry Smith 
2282bef8e0ddSBarry Smith @*/
2283be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2284bef8e0ddSBarry Smith {
228597f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2286bef8e0ddSBarry Smith 
2287bef8e0ddSBarry Smith   PetscFunctionBegin;
22884482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
22894482741eSBarry Smith   PetscValidPointer(indices,2);
2290c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2291bef8e0ddSBarry Smith   if (f) {
2292bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2293bef8e0ddSBarry Smith   } else {
2294634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2295bef8e0ddSBarry Smith   }
2296bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2297bef8e0ddSBarry Smith }
2298bef8e0ddSBarry Smith 
2299be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2300be6bf707SBarry Smith 
2301fb2e594dSBarry Smith EXTERN_C_BEGIN
23024a2ae208SSatish Balay #undef __FUNCT__
23034a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2304be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2305be6bf707SBarry Smith {
2306be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23076849ba73SBarry Smith   PetscErrorCode ierr;
23086849ba73SBarry Smith   size_t         nz = aij->i[mat->m];
2309be6bf707SBarry Smith 
2310be6bf707SBarry Smith   PetscFunctionBegin;
2311be6bf707SBarry Smith   if (aij->nonew != 1) {
2312634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2313be6bf707SBarry Smith   }
2314be6bf707SBarry Smith 
2315be6bf707SBarry Smith   /* allocate space for values if not already there */
2316be6bf707SBarry Smith   if (!aij->saved_values) {
231787828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2318be6bf707SBarry Smith   }
2319be6bf707SBarry Smith 
2320be6bf707SBarry Smith   /* copy values over */
232187828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2322be6bf707SBarry Smith   PetscFunctionReturn(0);
2323be6bf707SBarry Smith }
2324fb2e594dSBarry Smith EXTERN_C_END
2325be6bf707SBarry Smith 
23264a2ae208SSatish Balay #undef __FUNCT__
2327b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2328be6bf707SBarry Smith /*@
2329be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2330be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2331be6bf707SBarry Smith        nonlinear portion.
2332be6bf707SBarry Smith 
2333be6bf707SBarry Smith    Collect on Mat
2334be6bf707SBarry Smith 
2335be6bf707SBarry Smith   Input Parameters:
23360e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2337be6bf707SBarry Smith 
233815091d37SBarry Smith   Level: advanced
233915091d37SBarry Smith 
2340be6bf707SBarry Smith   Common Usage, with SNESSolve():
2341be6bf707SBarry Smith $    Create Jacobian matrix
2342be6bf707SBarry Smith $    Set linear terms into matrix
2343be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2344be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2345be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2346be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2347be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2348be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2349be6bf707SBarry Smith $    In your Jacobian routine
2350be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2351be6bf707SBarry Smith $      Set nonlinear terms in matrix
2352be6bf707SBarry Smith 
2353be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2354be6bf707SBarry Smith $    // build linear portion of Jacobian
2355be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2356be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2357be6bf707SBarry Smith $    loop over nonlinear iterations
2358be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2359be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2360be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2361be6bf707SBarry Smith $       Solve linear system with Jacobian
2362be6bf707SBarry Smith $    endloop
2363be6bf707SBarry Smith 
2364be6bf707SBarry Smith   Notes:
2365be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2366be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2367be6bf707SBarry Smith     calling this routine.
2368be6bf707SBarry Smith 
23690c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
23700c468ba9SBarry Smith     and does not allocated additional space.
23710c468ba9SBarry Smith 
2372be6bf707SBarry Smith .seealso: MatRetrieveValues()
2373be6bf707SBarry Smith 
2374be6bf707SBarry Smith @*/
2375be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2376be6bf707SBarry Smith {
2377dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2378be6bf707SBarry Smith 
2379be6bf707SBarry Smith   PetscFunctionBegin;
23804482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
238129bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
238229bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2383be6bf707SBarry Smith 
2384c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2385be6bf707SBarry Smith   if (f) {
2386be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2387be6bf707SBarry Smith   } else {
2388634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2389be6bf707SBarry Smith   }
2390be6bf707SBarry Smith   PetscFunctionReturn(0);
2391be6bf707SBarry Smith }
2392be6bf707SBarry Smith 
2393fb2e594dSBarry Smith EXTERN_C_BEGIN
23944a2ae208SSatish Balay #undef __FUNCT__
23954a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2396be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2397be6bf707SBarry Smith {
2398be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23996849ba73SBarry Smith   PetscErrorCode ierr;
240097f1f81fSBarry Smith   PetscInt       nz = aij->i[mat->m];
2401be6bf707SBarry Smith 
2402be6bf707SBarry Smith   PetscFunctionBegin;
2403be6bf707SBarry Smith   if (aij->nonew != 1) {
2404634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2405be6bf707SBarry Smith   }
2406be6bf707SBarry Smith   if (!aij->saved_values) {
2407634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2408be6bf707SBarry Smith   }
2409be6bf707SBarry Smith   /* copy values over */
241087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2411be6bf707SBarry Smith   PetscFunctionReturn(0);
2412be6bf707SBarry Smith }
2413fb2e594dSBarry Smith EXTERN_C_END
2414be6bf707SBarry Smith 
24154a2ae208SSatish Balay #undef __FUNCT__
24164a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2417be6bf707SBarry Smith /*@
2418be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2419be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2420be6bf707SBarry Smith        nonlinear portion.
2421be6bf707SBarry Smith 
2422be6bf707SBarry Smith    Collect on Mat
2423be6bf707SBarry Smith 
2424be6bf707SBarry Smith   Input Parameters:
2425be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2426be6bf707SBarry Smith 
242715091d37SBarry Smith   Level: advanced
242815091d37SBarry Smith 
2429be6bf707SBarry Smith .seealso: MatStoreValues()
2430be6bf707SBarry Smith 
2431be6bf707SBarry Smith @*/
2432be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2433be6bf707SBarry Smith {
2434dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2435be6bf707SBarry Smith 
2436be6bf707SBarry Smith   PetscFunctionBegin;
24374482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
243829bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
243929bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2440be6bf707SBarry Smith 
2441c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2442be6bf707SBarry Smith   if (f) {
2443be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2444be6bf707SBarry Smith   } else {
2445634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2446be6bf707SBarry Smith   }
2447be6bf707SBarry Smith   PetscFunctionReturn(0);
2448be6bf707SBarry Smith }
2449be6bf707SBarry Smith 
2450f83d6046SBarry Smith 
2451be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24524a2ae208SSatish Balay #undef __FUNCT__
24534a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
245417ab2063SBarry Smith /*@C
2455682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24560d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24576e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
245851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24592bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
246017ab2063SBarry Smith 
2461db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2462db81eaa0SLois Curfman McInnes 
246317ab2063SBarry Smith    Input Parameters:
2464db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
246517ab2063SBarry Smith .  m - number of rows
246617ab2063SBarry Smith .  n - number of columns
246717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
246851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
24692bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
247017ab2063SBarry Smith 
247117ab2063SBarry Smith    Output Parameter:
2472416022c9SBarry Smith .  A - the matrix
247317ab2063SBarry Smith 
2474b259b22eSLois Curfman McInnes    Notes:
247549a6f317SBarry Smith    If nnz is given then nz is ignored
247649a6f317SBarry Smith 
247717ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
247817ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
24790002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
248044cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
248117ab2063SBarry Smith 
248217ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2483a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
24843d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
24856da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
248617ab2063SBarry Smith 
2487682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
24884fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2489682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
24906c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
24916c7ebb05SLois Curfman McInnes 
24926c7ebb05SLois Curfman McInnes    Options Database Keys:
2493698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2494698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2495db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2496db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2497db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
249817ab2063SBarry Smith 
2499027ccd11SLois Curfman McInnes    Level: intermediate
2500027ccd11SLois Curfman McInnes 
250136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
250236db0b34SBarry Smith 
250317ab2063SBarry Smith @*/
2504be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
250517ab2063SBarry Smith {
2506dfbe8321SBarry Smith   PetscErrorCode ierr;
25076945ee14SBarry Smith 
25083a40ed3dSBarry Smith   PetscFunctionBegin;
2509*f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2510*f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2511273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2512ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2513273d9f13SBarry Smith   PetscFunctionReturn(0);
2514273d9f13SBarry Smith }
2515273d9f13SBarry Smith 
25164a2ae208SSatish Balay #undef __FUNCT__
25174a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2518273d9f13SBarry Smith /*@C
2519273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2520273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2521273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2522273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2523273d9f13SBarry Smith 
2524273d9f13SBarry Smith    Collective on MPI_Comm
2525273d9f13SBarry Smith 
2526273d9f13SBarry Smith    Input Parameters:
252745bfc511SMatthew Knepley +  B - The matrix
2528273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2529273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2530273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2531273d9f13SBarry Smith 
2532273d9f13SBarry Smith    Notes:
253349a6f317SBarry Smith      If nnz is given then nz is ignored
253449a6f317SBarry Smith 
2535273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2536273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2537273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2538273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2539273d9f13SBarry Smith 
2540273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2541273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2542273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2543273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2544273d9f13SBarry Smith 
2545a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2546a96a251dSBarry Smith    entries or columns indices
2547a96a251dSBarry Smith 
2548273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2549273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2550273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2551273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2552273d9f13SBarry Smith 
2553273d9f13SBarry Smith    Options Database Keys:
2554698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2555698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2556273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2557273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2558273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2559273d9f13SBarry Smith 
2560273d9f13SBarry Smith    Level: intermediate
2561273d9f13SBarry Smith 
2562273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2563273d9f13SBarry Smith 
2564273d9f13SBarry Smith @*/
2565be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2566273d9f13SBarry Smith {
256797f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2568a23d5eceSKris Buschelman 
2569a23d5eceSKris Buschelman   PetscFunctionBegin;
2570a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2571a23d5eceSKris Buschelman   if (f) {
2572a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2573a23d5eceSKris Buschelman   }
2574a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2575a23d5eceSKris Buschelman }
2576a23d5eceSKris Buschelman 
2577a23d5eceSKris Buschelman EXTERN_C_BEGIN
2578a23d5eceSKris Buschelman #undef __FUNCT__
2579a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2580be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2581a23d5eceSKris Buschelman {
2582273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2583a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
25846849ba73SBarry Smith   PetscErrorCode ierr;
258597f1f81fSBarry Smith   PetscInt       i;
2586273d9f13SBarry Smith 
2587273d9f13SBarry Smith   PetscFunctionBegin;
2588d5d45c9bSBarry Smith 
2589a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2590c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2591c461c341SBarry Smith     nz             = 0;
2592c461c341SBarry Smith   }
2593c461c341SBarry Smith 
2594435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2595435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2596b73539f3SBarry Smith   if (nnz) {
2597273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
259829bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
25993a7fca6bSBarry Smith       if (nnz[i] > B->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->n);
2600b73539f3SBarry Smith     }
2601b73539f3SBarry Smith   }
2602b73539f3SBarry Smith 
2603273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2604273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2605273d9f13SBarry Smith 
2606ab93d7beSBarry Smith   if (!skipallocation) {
2607ab93d7beSBarry Smith     ierr = PetscMalloc2(B->m,PetscInt,&b->imax,B->m,PetscInt,&b->ilen);CHKERRQ(ierr);
2608273d9f13SBarry Smith     if (!nnz) {
2609435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2610273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2611273d9f13SBarry Smith       for (i=0; i<B->m; i++) b->imax[i] = nz;
2612273d9f13SBarry Smith       nz = nz*B->m;
2613273d9f13SBarry Smith     } else {
2614273d9f13SBarry Smith       nz = 0;
2615273d9f13SBarry Smith       for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2616273d9f13SBarry Smith     }
2617273d9f13SBarry Smith 
2618ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2619ab93d7beSBarry Smith     for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2620ab93d7beSBarry Smith 
2621273d9f13SBarry Smith     /* allocate the matrix space */
2622a96a251dSBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->m+1,PetscInt,&b->i);CHKERRQ(ierr);
2623bfeeae90SHong Zhang     b->i[0] = 0;
26245da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
26255da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
26265da197adSKris Buschelman     }
2627273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2628273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2629c461c341SBarry Smith   } else {
2630c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2631c461c341SBarry Smith   }
2632273d9f13SBarry Smith 
2633273d9f13SBarry Smith   b->nz                = 0;
2634273d9f13SBarry Smith   b->maxnz             = nz;
2635273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2636273d9f13SBarry Smith   PetscFunctionReturn(0);
2637273d9f13SBarry Smith }
2638a23d5eceSKris Buschelman EXTERN_C_END
2639273d9f13SBarry Smith 
26400bad9183SKris Buschelman /*MC
2641fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
26420bad9183SKris Buschelman    based on compressed sparse row format.
26430bad9183SKris Buschelman 
26440bad9183SKris Buschelman    Options Database Keys:
26450bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
26460bad9183SKris Buschelman 
26470bad9183SKris Buschelman   Level: beginner
26480bad9183SKris Buschelman 
2649f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
26500bad9183SKris Buschelman M*/
26510bad9183SKris Buschelman 
2652a6175056SHong Zhang EXTERN_C_BEGIN
26534a2ae208SSatish Balay #undef __FUNCT__
26544a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2655be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
2656273d9f13SBarry Smith {
2657273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2658dfbe8321SBarry Smith   PetscErrorCode ierr;
265938baddfdSBarry Smith   PetscMPIInt    size;
2660273d9f13SBarry Smith 
2661273d9f13SBarry Smith   PetscFunctionBegin;
2662273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2663273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2664273d9f13SBarry Smith 
2665273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2666273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2667273d9f13SBarry Smith 
2668b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2669b0a32e0cSBarry Smith   B->data             = (void*)b;
2670549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2671416022c9SBarry Smith   B->factor           = 0;
267290f02eecSBarry Smith   B->mapping          = 0;
2673416022c9SBarry Smith   b->row              = 0;
2674416022c9SBarry Smith   b->col              = 0;
267582bf6240SBarry Smith   b->icol             = 0;
2676b810aeb4SBarry Smith   b->reallocs         = 0;
267717ab2063SBarry Smith 
26788a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
26798a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2680a5ae1ecdSBarry Smith 
2681f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
268236db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2683f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2684416022c9SBarry Smith   b->nonew             = 0;
2685416022c9SBarry Smith   b->diag              = 0;
2686416022c9SBarry Smith   b->solve_work        = 0;
26872a1b7f2aSHong Zhang   B->spptr             = 0;
2688be6bf707SBarry Smith   b->saved_values      = 0;
2689d7f994e1SBarry Smith   b->idiag             = 0;
2690d7f994e1SBarry Smith   b->ssor              = 0;
2691f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2692a30b2313SHong Zhang   b->xtoy              = 0;
2693a30b2313SHong Zhang   b->XtoY              = 0;
269473e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
2695d487561eSHong Zhang   b->compressedrow.nrows   = B->m;
2696d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
2697d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
2698d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
269988e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
270017ab2063SBarry Smith 
270135d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
270235d8aa7fSBarry Smith 
2703f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2704bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2705bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2706f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2707be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2708bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2709f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2710be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2711bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2712a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2713a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2714a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
271585fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
271685fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
271785fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
27185fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
27195fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
27205fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2721a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2722a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2723a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
272405b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
272505b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
272605b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
27274846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
27283a40ed3dSBarry Smith   PetscFunctionReturn(0);
272917ab2063SBarry Smith }
2730273d9f13SBarry Smith EXTERN_C_END
273117ab2063SBarry Smith 
27324a2ae208SSatish Balay #undef __FUNCT__
27334a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2734dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
273517ab2063SBarry Smith {
2736416022c9SBarry Smith   Mat            C;
2737416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
27386849ba73SBarry Smith   PetscErrorCode ierr;
273997f1f81fSBarry Smith   PetscInt       i,m = A->m;
274017ab2063SBarry Smith 
27413a40ed3dSBarry Smith   PetscFunctionBegin;
27424043dd9cSLois Curfman McInnes   *B = 0;
2743*f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
2744*f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->m,A->n,A->m,A->n);CHKERRQ(ierr);
2745be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
27461d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
27471d5dac46SHong Zhang 
2748273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
2749273d9f13SBarry Smith 
2750416022c9SBarry Smith   C->factor           = A->factor;
27516ad4291fSHong Zhang 
2752416022c9SBarry Smith   c->row            = 0;
2753416022c9SBarry Smith   c->col            = 0;
275482bf6240SBarry Smith   c->icol           = 0;
27556ad4291fSHong Zhang   c->reallocs       = 0;
275617ab2063SBarry Smith 
27576ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
275817ab2063SBarry Smith 
275997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->imax);CHKERRQ(ierr);
276097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->ilen);CHKERRQ(ierr);
276117ab2063SBarry Smith   for (i=0; i<m; i++) {
2762416022c9SBarry Smith     c->imax[i] = a->imax[i];
2763416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
276417ab2063SBarry Smith   }
276517ab2063SBarry Smith 
276617ab2063SBarry Smith   /* allocate the matrix space */
2767a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
2768f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
276997f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
277017ab2063SBarry Smith   if (m > 0) {
277197f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
2772be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2773bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2774be6bf707SBarry Smith     } else {
2775bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
277617ab2063SBarry Smith     }
277708480c60SBarry Smith   }
277817ab2063SBarry Smith 
2779416022c9SBarry Smith   c->sorted            = a->sorted;
27806ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
2781416022c9SBarry Smith   c->roworiented       = a->roworiented;
2782416022c9SBarry Smith   c->nonew             = a->nonew;
2783416022c9SBarry Smith   if (a->diag) {
278497f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
278552e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
278617ab2063SBarry Smith     for (i=0; i<m; i++) {
2787416022c9SBarry Smith       c->diag[i] = a->diag[i];
278817ab2063SBarry Smith     }
27893a40ed3dSBarry Smith   } else c->diag        = 0;
27906ad4291fSHong Zhang   c->solve_work         = 0;
27916ad4291fSHong Zhang   c->saved_values          = 0;
27926ad4291fSHong Zhang   c->idiag                 = 0;
27936ad4291fSHong Zhang   c->ssor                  = 0;
27946ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
27956ad4291fSHong Zhang   c->freedata              = PETSC_TRUE;
27966ad4291fSHong Zhang   c->xtoy                  = 0;
27976ad4291fSHong Zhang   c->XtoY                  = 0;
27986ad4291fSHong Zhang 
2799416022c9SBarry Smith   c->nz                 = a->nz;
2800416022c9SBarry Smith   c->maxnz              = a->maxnz;
2801273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2802754ec7b1SSatish Balay 
28036ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
28046ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
28056ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
28066ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
28076ad4291fSHong Zhang     i = a->compressedrow.nrows;
28086ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
28096ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
28106ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
28116ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
281227ea64f8SHong Zhang   } else {
281327ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
281427ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
281527ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
28166ad4291fSHong Zhang   }
281788e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
28186ad4291fSHong Zhang 
28194846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
28204846f1f5SKris Buschelman 
2821416022c9SBarry Smith   *B = C;
2822b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
28233a40ed3dSBarry Smith   PetscFunctionReturn(0);
282417ab2063SBarry Smith }
282517ab2063SBarry Smith 
28264a2ae208SSatish Balay #undef __FUNCT__
28274a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2828*f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A)
282917ab2063SBarry Smith {
2830416022c9SBarry Smith   Mat_SeqAIJ     *a;
2831416022c9SBarry Smith   Mat            B;
28326849ba73SBarry Smith   PetscErrorCode ierr;
28333c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
283438baddfdSBarry Smith   int            fd;
283538baddfdSBarry Smith   PetscMPIInt    size;
2836bcd2baecSBarry Smith   MPI_Comm       comm;
283717ab2063SBarry Smith 
28383a40ed3dSBarry Smith   PetscFunctionBegin;
2839e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2840d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
284129bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2842b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
28430752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2844552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
284517ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
284617ab2063SBarry Smith 
2847d64ed03dSBarry Smith   if (nz < 0) {
284829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2849d64ed03dSBarry Smith   }
2850d64ed03dSBarry Smith 
285117ab2063SBarry Smith   /* read in row lengths */
285297f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
28530752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
285417ab2063SBarry Smith 
28553c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
28563c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
28573c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
28583c601197SSatish Balay 
285917ab2063SBarry Smith   /* create our matrix */
2860*f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
2861*f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
2862b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2863ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
2864416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
286517ab2063SBarry Smith 
286617ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
28670752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
286817ab2063SBarry Smith 
286917ab2063SBarry Smith   /* read in nonzero values */
28700752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
287117ab2063SBarry Smith 
287217ab2063SBarry Smith   /* set matrix "i" values */
2873efb16452SHong Zhang   a->i[0] = 0;
287417ab2063SBarry Smith   for (i=1; i<= M; i++) {
2875416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2876416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
287717ab2063SBarry Smith   }
2878606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
287917ab2063SBarry Smith 
28806d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28816d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2882b3a1e11cSKris Buschelman   *A = B;
28833a40ed3dSBarry Smith   PetscFunctionReturn(0);
288417ab2063SBarry Smith }
288517ab2063SBarry Smith 
28864a2ae208SSatish Balay #undef __FUNCT__
2887b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
2888dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
28897264ac53SSatish Balay {
28907264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
2891dfbe8321SBarry Smith   PetscErrorCode ierr;
28927264ac53SSatish Balay 
28933a40ed3dSBarry Smith   PetscFunctionBegin;
2894bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
2895bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
2896ca44d042SBarry Smith     *flg = PETSC_FALSE;
2897ca44d042SBarry Smith     PetscFunctionReturn(0);
2898bcd2baecSBarry Smith   }
28997264ac53SSatish Balay 
29007264ac53SSatish Balay   /* if the a->i are the same */
290197f1f81fSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
2902abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
29037264ac53SSatish Balay 
29047264ac53SSatish Balay   /* if a->j are the same */
290597f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
2906abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
2907bcd2baecSBarry Smith 
2908bcd2baecSBarry Smith   /* if a->a are the same */
290987828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
29100f5bd95cSBarry Smith 
29113a40ed3dSBarry Smith   PetscFunctionReturn(0);
29127264ac53SSatish Balay 
29137264ac53SSatish Balay }
291436db0b34SBarry Smith 
29154a2ae208SSatish Balay #undef __FUNCT__
29164a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
291736db0b34SBarry Smith /*@C
291836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
291936db0b34SBarry Smith               provided by the user.
292036db0b34SBarry Smith 
292136db0b34SBarry Smith       Coolective on MPI_Comm
292236db0b34SBarry Smith 
292336db0b34SBarry Smith    Input Parameters:
292436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
292536db0b34SBarry Smith .   m - number of rows
292636db0b34SBarry Smith .   n - number of columns
292736db0b34SBarry Smith .   i - row indices
292836db0b34SBarry Smith .   j - column indices
292936db0b34SBarry Smith -   a - matrix values
293036db0b34SBarry Smith 
293136db0b34SBarry Smith    Output Parameter:
293236db0b34SBarry Smith .   mat - the matrix
293336db0b34SBarry Smith 
293436db0b34SBarry Smith    Level: intermediate
293536db0b34SBarry Smith 
293636db0b34SBarry Smith    Notes:
29370551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
293836db0b34SBarry Smith     once the matrix is destroyed
293936db0b34SBarry Smith 
294036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
294136db0b34SBarry Smith 
2942bfeeae90SHong Zhang        The i and j indices are 0 based
294336db0b34SBarry Smith 
294436db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
294536db0b34SBarry Smith 
294636db0b34SBarry Smith @*/
2947be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
294836db0b34SBarry Smith {
2949dfbe8321SBarry Smith   PetscErrorCode ierr;
295097f1f81fSBarry Smith   PetscInt       ii;
295136db0b34SBarry Smith   Mat_SeqAIJ     *aij;
295236db0b34SBarry Smith 
295336db0b34SBarry Smith   PetscFunctionBegin;
2954a96a251dSBarry Smith   if (i[0]) {
2955634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
295636db0b34SBarry Smith   }
2957*f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2958*f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2959ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
2960ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2961ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
2962ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
2963ab93d7beSBarry Smith 
296436db0b34SBarry Smith   aij->i = i;
296536db0b34SBarry Smith   aij->j = j;
296636db0b34SBarry Smith   aij->a = a;
296736db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
296836db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
296936db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
297036db0b34SBarry Smith 
297136db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
297236db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
29732515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
297479a5c55eSBarry 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]);
297536db0b34SBarry Smith #endif
297636db0b34SBarry Smith   }
29772515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
297836db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
297979a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
298079a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
298136db0b34SBarry Smith   }
298236db0b34SBarry Smith #endif
298336db0b34SBarry Smith 
2984b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2985b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
298636db0b34SBarry Smith   PetscFunctionReturn(0);
298736db0b34SBarry Smith }
298836db0b34SBarry Smith 
2989cc8ba8e1SBarry Smith #undef __FUNCT__
2990ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
2991dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
2992cc8ba8e1SBarry Smith {
2993dfbe8321SBarry Smith   PetscErrorCode ierr;
2994cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
299536db0b34SBarry Smith 
2996cc8ba8e1SBarry Smith   PetscFunctionBegin;
299712c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
2998cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
2999cc8ba8e1SBarry Smith     a->coloring = coloring;
300012c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
300197f1f81fSBarry Smith     PetscInt             i,*larray;
300212c595b3SBarry Smith     ISColoring      ocoloring;
300308b6dcc0SBarry Smith     ISColoringValue *colors;
300412c595b3SBarry Smith 
300512c595b3SBarry Smith     /* set coloring for diagonal portion */
300697f1f81fSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
300712c595b3SBarry Smith     for (i=0; i<A->n; i++) {
300812c595b3SBarry Smith       larray[i] = i;
300912c595b3SBarry Smith     }
301012c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
301108b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
301212c595b3SBarry Smith     for (i=0; i<A->n; i++) {
301312c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
301412c595b3SBarry Smith     }
301512c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
301612c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
301712c595b3SBarry Smith     a->coloring = ocoloring;
301812c595b3SBarry Smith   }
3019cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3020cc8ba8e1SBarry Smith }
3021cc8ba8e1SBarry Smith 
3022dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3023ee4f033dSBarry Smith EXTERN_C_BEGIN
302429c1e371SBarry Smith #include "adic/ad_utils.h"
3025ee4f033dSBarry Smith EXTERN_C_END
3026cc8ba8e1SBarry Smith 
3027cc8ba8e1SBarry Smith #undef __FUNCT__
3028ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3029dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3030cc8ba8e1SBarry Smith {
3031cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
303297f1f81fSBarry Smith   PetscInt        m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
30334440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
303408b6dcc0SBarry Smith   ISColoringValue *color;
3035cc8ba8e1SBarry Smith 
3036cc8ba8e1SBarry Smith   PetscFunctionBegin;
3037e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
30384440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3039cc8ba8e1SBarry Smith   color = a->coloring->colors;
3040cc8ba8e1SBarry Smith   /* loop over rows */
3041cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3042cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3043cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3044cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3045cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3046cc8ba8e1SBarry Smith     }
30474440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3048ee4f033dSBarry Smith   }
3049ee4f033dSBarry Smith   PetscFunctionReturn(0);
3050ee4f033dSBarry Smith }
3051ee4f033dSBarry Smith #endif
3052ee4f033dSBarry Smith 
3053ee4f033dSBarry Smith #undef __FUNCT__
3054ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
305597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3056ee4f033dSBarry Smith {
3057ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
305897f1f81fSBarry Smith   PetscInt             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
305987828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
306008b6dcc0SBarry Smith   ISColoringValue *color;
3061ee4f033dSBarry Smith 
3062ee4f033dSBarry Smith   PetscFunctionBegin;
3063e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3064ee4f033dSBarry Smith   color = a->coloring->colors;
3065ee4f033dSBarry Smith   /* loop over rows */
3066ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3067ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3068ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3069ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3070ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3071ee4f033dSBarry Smith     }
3072ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3073cc8ba8e1SBarry Smith   }
3074cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3075cc8ba8e1SBarry Smith }
307636db0b34SBarry Smith 
3077