xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 7cd84e0492204f628c8ebffb5adf024f337e33c4)
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 
151*7cd84e04SBarry 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;
269e060cb09SBarry Smith   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"
1244f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
124517ab2063SBarry Smith {
1246416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1247f4df32b1SMatthew Knepley   PetscInt       i,m = A->m - 1;
12486849ba73SBarry Smith   PetscErrorCode ierr;
124917ab2063SBarry Smith 
12503a40ed3dSBarry Smith   PetscFunctionBegin;
1251f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1252f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
125377431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1254bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1255f1e2ffcdSBarry Smith     }
1256f4df32b1SMatthew Knepley     if (diag != 0.0) {
1257f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1258f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1259f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1260f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1261f1e2ffcdSBarry Smith       }
1262f1e2ffcdSBarry Smith     }
126388e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1264f1e2ffcdSBarry Smith   } else {
1265f4df32b1SMatthew Knepley     if (diag != 0.0) {
126617ab2063SBarry Smith       for (i=0; i<N; i++) {
126777431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
12687ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1269416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1270f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1271bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
12727ae801bdSBarry Smith         } else { /* in case row was completely empty */
1273f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
127417ab2063SBarry Smith         }
127517ab2063SBarry Smith       }
12763a40ed3dSBarry Smith     } else {
127717ab2063SBarry Smith       for (i=0; i<N; i++) {
127877431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1279416022c9SBarry Smith         a->ilen[rows[i]] = 0;
128017ab2063SBarry Smith       }
128117ab2063SBarry Smith     }
128288e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1283f1e2ffcdSBarry Smith   }
128443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12853a40ed3dSBarry Smith   PetscFunctionReturn(0);
128617ab2063SBarry Smith }
128717ab2063SBarry Smith 
12884a2ae208SSatish Balay #undef __FUNCT__
12894a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
129097f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
129117ab2063SBarry Smith {
1292416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
129397f1f81fSBarry Smith   PetscInt   *itmp;
129417ab2063SBarry Smith 
12953a40ed3dSBarry Smith   PetscFunctionBegin;
129677431f27SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
129717ab2063SBarry Smith 
1298416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1299bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
130017ab2063SBarry Smith   if (idx) {
1301bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1302bfeeae90SHong Zhang     if (*nz) {
13034e093b46SBarry Smith       *idx = itmp;
130417ab2063SBarry Smith     }
130517ab2063SBarry Smith     else *idx = 0;
130617ab2063SBarry Smith   }
13073a40ed3dSBarry Smith   PetscFunctionReturn(0);
130817ab2063SBarry Smith }
130917ab2063SBarry Smith 
1310bfeeae90SHong Zhang /* remove this function? */
13114a2ae208SSatish Balay #undef __FUNCT__
13124a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
131397f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
131417ab2063SBarry Smith {
13153a40ed3dSBarry Smith   PetscFunctionBegin;
13163a40ed3dSBarry Smith   PetscFunctionReturn(0);
131717ab2063SBarry Smith }
131817ab2063SBarry Smith 
13194a2ae208SSatish Balay #undef __FUNCT__
13204a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1321dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
132217ab2063SBarry Smith {
1323416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
132487828ca2SBarry Smith   PetscScalar    *v = a->a;
132536db0b34SBarry Smith   PetscReal      sum = 0.0;
13266849ba73SBarry Smith   PetscErrorCode ierr;
132797f1f81fSBarry Smith   PetscInt       i,j;
132817ab2063SBarry Smith 
13293a40ed3dSBarry Smith   PetscFunctionBegin;
133017ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1331416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1332aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
133336db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
133417ab2063SBarry Smith #else
133517ab2063SBarry Smith       sum += (*v)*(*v); v++;
133617ab2063SBarry Smith #endif
133717ab2063SBarry Smith     }
1338064f8208SBarry Smith     *nrm = sqrt(sum);
13393a40ed3dSBarry Smith   } else if (type == NORM_1) {
134036db0b34SBarry Smith     PetscReal *tmp;
134197f1f81fSBarry Smith     PetscInt    *jj = a->j;
1342b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1343273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1344064f8208SBarry Smith     *nrm = 0.0;
1345416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1346bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
134717ab2063SBarry Smith     }
1348273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1349064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
135017ab2063SBarry Smith     }
1351606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13523a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1353064f8208SBarry Smith     *nrm = 0.0;
1354273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1355bfeeae90SHong Zhang       v = a->a + a->i[j];
135617ab2063SBarry Smith       sum = 0.0;
1357416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1358cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
135917ab2063SBarry Smith       }
1360064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
136117ab2063SBarry Smith     }
13623a40ed3dSBarry Smith   } else {
136329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
136417ab2063SBarry Smith   }
13653a40ed3dSBarry Smith   PetscFunctionReturn(0);
136617ab2063SBarry Smith }
136717ab2063SBarry Smith 
13684a2ae208SSatish Balay #undef __FUNCT__
13694a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1370dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B)
137117ab2063SBarry Smith {
1372416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1373416022c9SBarry Smith   Mat            C;
13746849ba73SBarry Smith   PetscErrorCode ierr;
137597f1f81fSBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->m,len,*col;
137687828ca2SBarry Smith   PetscScalar    *array = a->a;
137717ab2063SBarry Smith 
13783a40ed3dSBarry Smith   PetscFunctionBegin;
1379273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
138097f1f81fSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
138197f1f81fSBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(PetscInt));CHKERRQ(ierr);
1382bfeeae90SHong Zhang 
1383bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1384f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1385f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->n,m,A->n,m);CHKERRQ(ierr);
1386f204ca49SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1387ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1388606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
138917ab2063SBarry Smith   for (i=0; i<m; i++) {
139017ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1391416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1392b9b97703SBarry Smith     array += len;
1393b9b97703SBarry Smith     aj    += len;
139417ab2063SBarry Smith   }
139517ab2063SBarry Smith 
13966d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13976d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
139817ab2063SBarry Smith 
1399f1e2ffcdSBarry Smith   if (B) {
1400416022c9SBarry Smith     *B = C;
140117ab2063SBarry Smith   } else {
1402273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
140317ab2063SBarry Smith   }
14043a40ed3dSBarry Smith   PetscFunctionReturn(0);
140517ab2063SBarry Smith }
140617ab2063SBarry Smith 
1407cd0d46ebSvictorle EXTERN_C_BEGIN
1408cd0d46ebSvictorle #undef __FUNCT__
14095fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1410be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1411cd0d46ebSvictorle {
1412cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
141397f1f81fSBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
14146849ba73SBarry Smith   PetscErrorCode ierr;
141597f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1416cd0d46ebSvictorle 
1417cd0d46ebSvictorle   PetscFunctionBegin;
1418cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1419cd0d46ebSvictorle 
1420cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1421cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
14225485867bSBarry Smith   if (ma!=nb || na!=mb){
14235485867bSBarry Smith     *f = PETSC_FALSE;
14245485867bSBarry Smith     PetscFunctionReturn(0);
14255485867bSBarry Smith   }
1426cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1427cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1428cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
142997f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
143097f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1431cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1432cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1433cd0d46ebSvictorle 
1434cd0d46ebSvictorle   *f = PETSC_TRUE;
1435cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1436cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
143797f1f81fSBarry Smith       PetscInt         idc,idr;
14385485867bSBarry Smith       PetscScalar vc,vr;
1439cd0d46ebSvictorle       /* column/row index/value */
14405485867bSBarry Smith       idc = adx[aptr[i]];
14415485867bSBarry Smith       idr = bdx[bptr[idc]];
14425485867bSBarry Smith       vc  = va[aptr[i]];
14435485867bSBarry Smith       vr  = vb[bptr[idc]];
14445485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
14455485867bSBarry Smith 	*f = PETSC_FALSE;
14465485867bSBarry Smith         goto done;
1447cd0d46ebSvictorle       } else {
14485485867bSBarry Smith 	aptr[i]++;
14495485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1450cd0d46ebSvictorle       }
1451cd0d46ebSvictorle     }
1452cd0d46ebSvictorle   }
1453cd0d46ebSvictorle  done:
1454cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
14553aeef889SHong Zhang   if (B) {
14563aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
14573aeef889SHong Zhang   }
1458cd0d46ebSvictorle   PetscFunctionReturn(0);
1459cd0d46ebSvictorle }
1460cd0d46ebSvictorle EXTERN_C_END
1461cd0d46ebSvictorle 
14629e29f15eSvictorle #undef __FUNCT__
14639e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1464dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
14659e29f15eSvictorle {
1466dfbe8321SBarry Smith   PetscErrorCode ierr;
14679e29f15eSvictorle   PetscFunctionBegin;
14685485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
14699e29f15eSvictorle   PetscFunctionReturn(0);
14709e29f15eSvictorle }
14719e29f15eSvictorle 
14724a2ae208SSatish Balay #undef __FUNCT__
14734a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1474dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
147517ab2063SBarry Smith {
1476416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
147787828ca2SBarry Smith   PetscScalar    *l,*r,x,*v;
1478dfbe8321SBarry Smith   PetscErrorCode ierr;
147997f1f81fSBarry Smith   PetscInt       i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
148017ab2063SBarry Smith 
14813a40ed3dSBarry Smith   PetscFunctionBegin;
148217ab2063SBarry Smith   if (ll) {
14833ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14843ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1485e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1486273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
14871ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1488416022c9SBarry Smith     v = a->a;
148917ab2063SBarry Smith     for (i=0; i<m; i++) {
149017ab2063SBarry Smith       x = l[i];
1491416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
149217ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
149317ab2063SBarry Smith     }
14941ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1495efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
149617ab2063SBarry Smith   }
149717ab2063SBarry Smith   if (rr) {
1498e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1499273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
15001ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1501416022c9SBarry Smith     v = a->a; jj = a->j;
150217ab2063SBarry Smith     for (i=0; i<nz; i++) {
1503bfeeae90SHong Zhang       (*v++) *= r[*jj++];
150417ab2063SBarry Smith     }
15051ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1506efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
150717ab2063SBarry Smith   }
15083a40ed3dSBarry Smith   PetscFunctionReturn(0);
150917ab2063SBarry Smith }
151017ab2063SBarry Smith 
15114a2ae208SSatish Balay #undef __FUNCT__
15124a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
151397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
151417ab2063SBarry Smith {
1515db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
15166849ba73SBarry Smith   PetscErrorCode ierr;
151797f1f81fSBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->n,*lens;
151897f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
151997f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
152097f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
152187828ca2SBarry Smith   PetscScalar    *a_new,*mat_a;
1522416022c9SBarry Smith   Mat            C;
1523fee21e36SBarry Smith   PetscTruth     stride;
152417ab2063SBarry Smith 
15253a40ed3dSBarry Smith   PetscFunctionBegin;
1526d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
152729bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1528d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
152929bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
153099141d43SSatish Balay 
153117ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1532b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1533b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
153417ab2063SBarry Smith 
1535fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1536fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1537fee21e36SBarry Smith   if (stride && step == 1) {
153802834360SBarry Smith     /* special case of contiguous rows */
153997f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
154031ebf83bSSatish Balay     starts = lens + nrows;
154102834360SBarry Smith     /* loop over new rows determining lens and starting points */
154202834360SBarry Smith     for (i=0; i<nrows; i++) {
1543bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1544a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
154502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1546bfeeae90SHong Zhang         if (aj[k] >= first) {
154702834360SBarry Smith           starts[i] = k;
154802834360SBarry Smith           break;
154902834360SBarry Smith 	}
155002834360SBarry Smith       }
1551a2744918SBarry Smith       sum = 0;
155202834360SBarry Smith       while (k < kend) {
1553bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1554a2744918SBarry Smith         sum++;
155502834360SBarry Smith       }
1556a2744918SBarry Smith       lens[i] = sum;
155702834360SBarry Smith     }
155802834360SBarry Smith     /* create submatrix */
1559cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
156097f1f81fSBarry Smith       PetscInt n_cols,n_rows;
156108480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
156229bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1563d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
156408480c60SBarry Smith       C = *B;
15653a40ed3dSBarry Smith     } else {
1566f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1567f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1568e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1569ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
157008480c60SBarry Smith     }
1571db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1572db02288aSLois Curfman McInnes 
157302834360SBarry Smith     /* loop over rows inserting into submatrix */
1574db02288aSLois Curfman McInnes     a_new    = c->a;
1575db02288aSLois Curfman McInnes     j_new    = c->j;
1576db02288aSLois Curfman McInnes     i_new    = c->i;
1577bfeeae90SHong Zhang 
157802834360SBarry Smith     for (i=0; i<nrows; i++) {
1579a2744918SBarry Smith       ii    = starts[i];
1580a2744918SBarry Smith       lensi = lens[i];
1581a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1582a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
158302834360SBarry Smith       }
158487828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1585a2744918SBarry Smith       a_new      += lensi;
1586a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1587a2744918SBarry Smith       c->ilen[i]  = lensi;
158802834360SBarry Smith     }
1589606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15903a40ed3dSBarry Smith   } else {
159102834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
159297f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1593bfeeae90SHong Zhang 
159497f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
159597f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
159617ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
159702834360SBarry Smith     /* determine lens of each row */
159802834360SBarry Smith     for (i=0; i<nrows; i++) {
1599bfeeae90SHong Zhang       kstart  = ai[irow[i]];
160002834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
160102834360SBarry Smith       lens[i] = 0;
160202834360SBarry Smith       for (k=kstart; k<kend; k++) {
1603bfeeae90SHong Zhang         if (smap[aj[k]]) {
160402834360SBarry Smith           lens[i]++;
160502834360SBarry Smith         }
160602834360SBarry Smith       }
160702834360SBarry Smith     }
160817ab2063SBarry Smith     /* Create and fill new matrix */
1609a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16100f5bd95cSBarry Smith       PetscTruth equal;
16110f5bd95cSBarry Smith 
161299141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1613273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
161497f1f81fSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(PetscInt),&equal);CHKERRQ(ierr);
16150f5bd95cSBarry Smith       if (!equal) {
161629bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
161799141d43SSatish Balay       }
161897f1f81fSBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(PetscInt));CHKERRQ(ierr);
161908480c60SBarry Smith       C = *B;
16203a40ed3dSBarry Smith     } else {
1621f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1622f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1623e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1624ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
162508480c60SBarry Smith     }
162699141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
162717ab2063SBarry Smith     for (i=0; i<nrows; i++) {
162899141d43SSatish Balay       row    = irow[i];
1629bfeeae90SHong Zhang       kstart = ai[row];
163099141d43SSatish Balay       kend   = kstart + a->ilen[row];
1631bfeeae90SHong Zhang       mat_i  = c->i[i];
163299141d43SSatish Balay       mat_j  = c->j + mat_i;
163399141d43SSatish Balay       mat_a  = c->a + mat_i;
163499141d43SSatish Balay       mat_ilen = c->ilen + i;
163517ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1636bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1637ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
163899141d43SSatish Balay           *mat_a++ = a->a[k];
163999141d43SSatish Balay           (*mat_ilen)++;
164099141d43SSatish Balay 
164117ab2063SBarry Smith         }
164217ab2063SBarry Smith       }
164317ab2063SBarry Smith     }
164402834360SBarry Smith     /* Free work space */
164502834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1646606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1647606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
164802834360SBarry Smith   }
16496d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16506d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
165117ab2063SBarry Smith 
165217ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1653416022c9SBarry Smith   *B = C;
16543a40ed3dSBarry Smith   PetscFunctionReturn(0);
165517ab2063SBarry Smith }
165617ab2063SBarry Smith 
1657a871dcd8SBarry Smith /*
1658a871dcd8SBarry Smith */
16594a2ae208SSatish Balay #undef __FUNCT__
16604a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1661dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1662a871dcd8SBarry Smith {
166363b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1664dfbe8321SBarry Smith   PetscErrorCode ierr;
166563b91edcSBarry Smith   Mat            outA;
1666b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
166763b91edcSBarry Smith 
16683a40ed3dSBarry Smith   PetscFunctionBegin;
1669d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1670b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1671b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1672b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1673634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
1674b8a78c4aSBarry Smith   }
1675a871dcd8SBarry Smith 
167663b91edcSBarry Smith   outA          = inA;
167763b91edcSBarry Smith   inA->factor   = FACTOR_LU;
167863b91edcSBarry Smith   a->row        = row;
167963b91edcSBarry Smith   a->col        = col;
1680c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1681c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
168263b91edcSBarry Smith 
168336db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1684b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16854c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
168652e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1687f0ec6fceSSatish Balay 
168894a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
168987828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
169094a9d846SBarry Smith   }
169163b91edcSBarry Smith 
169208480c60SBarry Smith   if (!a->diag) {
1693f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
169463b91edcSBarry Smith   }
1695af281ebdSHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
16963a40ed3dSBarry Smith   PetscFunctionReturn(0);
1697a871dcd8SBarry Smith }
1698a871dcd8SBarry Smith 
1699d9eff348SSatish Balay #include "petscblaslapack.h"
17004a2ae208SSatish Balay #undef __FUNCT__
17014a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1702f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1703f0b747eeSBarry Smith {
1704f0b747eeSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)inA->data;
17054ce68768SBarry Smith   PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1;
1706f4df32b1SMatthew Knepley   PetscScalar oalpha = alpha;
1707efee365bSSatish Balay   PetscErrorCode ierr;
1708efee365bSSatish Balay 
17093a40ed3dSBarry Smith 
17103a40ed3dSBarry Smith   PetscFunctionBegin;
1711f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1712efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17133a40ed3dSBarry Smith   PetscFunctionReturn(0);
1714f0b747eeSBarry Smith }
1715f0b747eeSBarry Smith 
17164a2ae208SSatish Balay #undef __FUNCT__
17174a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
171897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1719cddf8d76SBarry Smith {
1720dfbe8321SBarry Smith   PetscErrorCode ierr;
172197f1f81fSBarry Smith   PetscInt       i;
1722cddf8d76SBarry Smith 
17233a40ed3dSBarry Smith   PetscFunctionBegin;
1724cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1725b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1726cddf8d76SBarry Smith   }
1727cddf8d76SBarry Smith 
1728cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17296a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1730cddf8d76SBarry Smith   }
17313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1732cddf8d76SBarry Smith }
1733cddf8d76SBarry Smith 
17344a2ae208SSatish Balay #undef __FUNCT__
17354a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
173697f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
17374dcbc457SBarry Smith {
1738e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17396849ba73SBarry Smith   PetscErrorCode ierr;
174097f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
174197f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1742f1af5d2fSBarry Smith   PetscBT        table;
1743bbd702dbSSatish Balay 
17443a40ed3dSBarry Smith   PetscFunctionBegin;
1745273d9f13SBarry Smith   m     = A->m;
1746e4d965acSSatish Balay   ai    = a->i;
1747bfeeae90SHong Zhang   aj    = a->j;
17488a047759SSatish Balay 
1749a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
175006763907SSatish Balay 
175197f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
17526831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
175306763907SSatish Balay 
1754e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1755b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1756e4d965acSSatish Balay     isz  = 0;
17576831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1758e4d965acSSatish Balay 
1759e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17604dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1761b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1762e4d965acSSatish Balay 
1763dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1764e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1765f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17664dcbc457SBarry Smith     }
176706763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
176806763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1769e4d965acSSatish Balay 
177004a348a9SBarry Smith     k = 0;
177104a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
177204a348a9SBarry Smith       n = isz;
177306763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1774e4d965acSSatish Balay         row   = nidx[k];
1775e4d965acSSatish Balay         start = ai[row];
1776e4d965acSSatish Balay         end   = ai[row+1];
177704a348a9SBarry Smith         for (l = start; l<end ; l++){
1778efb16452SHong Zhang           val = aj[l] ;
1779f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1780e4d965acSSatish Balay         }
1781e4d965acSSatish Balay       }
1782e4d965acSSatish Balay     }
1783029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1784e4d965acSSatish Balay   }
17856831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1786606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17873a40ed3dSBarry Smith   PetscFunctionReturn(0);
17884dcbc457SBarry Smith }
178917ab2063SBarry Smith 
17900513a670SBarry Smith /* -------------------------------------------------------------- */
17914a2ae208SSatish Balay #undef __FUNCT__
17924a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1793dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17940513a670SBarry Smith {
17950513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17966849ba73SBarry Smith   PetscErrorCode ierr;
179797f1f81fSBarry Smith   PetscInt       i,nz,m = A->m,n = A->n,*col;
179897f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
179956cd22aeSBarry Smith   IS             icolp,irowp;
180097f1f81fSBarry Smith   PetscInt       *cwork;
180132ec9ce4SBarry Smith   PetscScalar    *vwork;
18020513a670SBarry Smith 
18033a40ed3dSBarry Smith   PetscFunctionBegin;
18044c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
180556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18064c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
180756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18080513a670SBarry Smith 
18090513a670SBarry Smith   /* determine lengths of permuted rows */
181097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
18110513a670SBarry Smith   for (i=0; i<m; i++) {
18120513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18130513a670SBarry Smith   }
1814f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,B);CHKERRQ(ierr);
1815f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
1816f204ca49SKris Buschelman   ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr);
1817ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1818606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18190513a670SBarry Smith 
182097f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
18210513a670SBarry Smith   for (i=0; i<m; i++) {
182232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18230513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1824cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
182532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18260513a670SBarry Smith   }
1827606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18283c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
18290513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18300513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
183156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
183256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
183356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
183456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18353a40ed3dSBarry Smith   PetscFunctionReturn(0);
18360513a670SBarry Smith }
18370513a670SBarry Smith 
18384a2ae208SSatish Balay #undef __FUNCT__
18394a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1840dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_SeqAIJ(Mat A)
1841682d7d0cSBarry Smith {
1842c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1843682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1844dfbe8321SBarry Smith   PetscErrorCode    ierr;
1845682d7d0cSBarry Smith 
18463a40ed3dSBarry Smith   PetscFunctionBegin;
18474846f1f5SKris Buschelman   ierr = MatPrintHelp_Inode(A);CHKERRQ(ierr);
1848c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1849d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1850d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1851d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
185273e7a558SHong Zhang   ierr = (*PetscHelpPrintf)(comm,"  -mat_no_compressedrow: Do not use compressedrow\n");CHKERRQ(ierr);
18533a40ed3dSBarry Smith   PetscFunctionReturn(0);
1854682d7d0cSBarry Smith }
185597304618SKris Buschelman 
18564a2ae208SSatish Balay #undef __FUNCT__
18574a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1858dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1859cb5b572fSBarry Smith {
1860dfbe8321SBarry Smith   PetscErrorCode ierr;
1861cb5b572fSBarry Smith 
1862cb5b572fSBarry Smith   PetscFunctionBegin;
186333f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
186433f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1865be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1866be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1867be6bf707SBarry Smith 
1868bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
1869634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1870cb5b572fSBarry Smith     }
1871bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1872cb5b572fSBarry Smith   } else {
1873cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1874cb5b572fSBarry Smith   }
1875cb5b572fSBarry Smith   PetscFunctionReturn(0);
1876cb5b572fSBarry Smith }
1877cb5b572fSBarry Smith 
18784a2ae208SSatish Balay #undef __FUNCT__
18794a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1880dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
1881273d9f13SBarry Smith {
1882dfbe8321SBarry Smith   PetscErrorCode ierr;
1883273d9f13SBarry Smith 
1884273d9f13SBarry Smith   PetscFunctionBegin;
1885ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1886273d9f13SBarry Smith   PetscFunctionReturn(0);
1887273d9f13SBarry Smith }
1888273d9f13SBarry Smith 
18894a2ae208SSatish Balay #undef __FUNCT__
18904a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
1891dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
18926c0721eeSBarry Smith {
18936c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18946c0721eeSBarry Smith   PetscFunctionBegin;
18956c0721eeSBarry Smith   *array = a->a;
18966c0721eeSBarry Smith   PetscFunctionReturn(0);
18976c0721eeSBarry Smith }
18986c0721eeSBarry Smith 
18994a2ae208SSatish Balay #undef __FUNCT__
19004a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
1901dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19026c0721eeSBarry Smith {
19036c0721eeSBarry Smith   PetscFunctionBegin;
19046c0721eeSBarry Smith   PetscFunctionReturn(0);
19056c0721eeSBarry Smith }
1906273d9f13SBarry Smith 
1907ee4f033dSBarry Smith #undef __FUNCT__
1908ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1909dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1910ee4f033dSBarry Smith {
19116849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
19126849ba73SBarry Smith   PetscErrorCode ierr;
191397f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
191487828ca2SBarry Smith   PetscScalar    dx,mone = -1.0,*y,*xx,*w3_array;
191587828ca2SBarry Smith   PetscScalar    *vscale_array;
1916ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
1917ee4f033dSBarry Smith   Vec            w1,w2,w3;
1918ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
1919ee4f033dSBarry Smith   PetscTruth     flg;
1920ee4f033dSBarry Smith 
1921ee4f033dSBarry Smith   PetscFunctionBegin;
1922ee4f033dSBarry Smith   if (!coloring->w1) {
1923ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
192452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
1925ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
192652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
1927ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
192852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
1929ee4f033dSBarry Smith   }
1930ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1931ee4f033dSBarry Smith 
1932ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1933e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1934ee4f033dSBarry Smith   if (flg) {
193563ba0a88SBarry Smith     ierr = PetscLogInfo((coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"));CHKERRQ(ierr);
1936ee4f033dSBarry Smith   } else {
19370b9b6f31SBarry Smith     PetscTruth assembled;
19380b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
19390b9b6f31SBarry Smith     if (assembled) {
1940ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
1941ee4f033dSBarry Smith     }
19420b9b6f31SBarry Smith   }
1943ee4f033dSBarry Smith 
1944ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1945ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1946ee4f033dSBarry Smith 
1947ee4f033dSBarry Smith   /*
1948ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1949ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1950ee4f033dSBarry Smith   */
1951ee4f033dSBarry Smith   if (coloring->F) {
1952ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1953ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1954ee4f033dSBarry Smith     if (m1 != m2) {
1955ee4f033dSBarry Smith       coloring->F = 0;
1956ee4f033dSBarry Smith     }
1957ee4f033dSBarry Smith   }
1958ee4f033dSBarry Smith 
1959ee4f033dSBarry Smith   if (coloring->F) {
1960ee4f033dSBarry Smith     w1          = coloring->F;
1961ee4f033dSBarry Smith     coloring->F = 0;
1962ee4f033dSBarry Smith   } else {
196366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1964ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
196566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1966ee4f033dSBarry Smith   }
1967ee4f033dSBarry Smith 
1968ee4f033dSBarry Smith   /*
1969ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1970ee4f033dSBarry Smith   */
19711ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
19721ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1973ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1974ee4f033dSBarry Smith     /*
1975ee4f033dSBarry Smith        Loop over each column associated with color adding the
1976ee4f033dSBarry Smith        perturbation to the vector w3.
1977ee4f033dSBarry Smith     */
1978ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1979ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1980ee4f033dSBarry Smith       dx  = xx[col];
1981ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1982ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1983ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1984ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1985ee4f033dSBarry Smith #else
1986ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1987ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1988ee4f033dSBarry Smith #endif
1989ee4f033dSBarry Smith       dx                *= epsilon;
1990ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1991ee4f033dSBarry Smith     }
1992ee4f033dSBarry Smith   }
19931ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1994ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1995ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1996ee4f033dSBarry Smith 
1997ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
1998ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
1999ee4f033dSBarry Smith 
2000ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2001ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2002ee4f033dSBarry Smith 
20031ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2004ee4f033dSBarry Smith   /*
2005ee4f033dSBarry Smith       Loop over each color
2006ee4f033dSBarry Smith   */
2007ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
200849b058dcSBarry Smith     coloring->currentcolor = k;
2009ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
20101ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2011ee4f033dSBarry Smith     /*
2012ee4f033dSBarry Smith        Loop over each column associated with color adding the
2013ee4f033dSBarry Smith        perturbation to the vector w3.
2014ee4f033dSBarry Smith     */
2015ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2016ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2017ee4f033dSBarry Smith       dx  = xx[col];
20185b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2019ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2020ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2021ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2022ee4f033dSBarry Smith #else
2023ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2024ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2025ee4f033dSBarry Smith #endif
2026ee4f033dSBarry Smith       dx            *= epsilon;
2027634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2028ee4f033dSBarry Smith       w3_array[col] += dx;
2029ee4f033dSBarry Smith     }
20301ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2031ee4f033dSBarry Smith 
2032ee4f033dSBarry Smith     /*
2033ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2034ee4f033dSBarry Smith     */
2035ee4f033dSBarry Smith 
203666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2037ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
203866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
20392dcb1b2aSMatthew Knepley     ierr = VecAXPY(w2,mone,w1);CHKERRQ(ierr);
2040ee4f033dSBarry Smith 
2041ee4f033dSBarry Smith     /*
2042ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2043ee4f033dSBarry Smith     */
20441ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2045ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2046ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2047ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2048ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2049ee4f033dSBarry Smith       srow   = row + start;
2050ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2051ee4f033dSBarry Smith     }
20521ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2053ee4f033dSBarry Smith   }
205449b058dcSBarry Smith   coloring->currentcolor = k;
20551ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
20561ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2057ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2058ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2059ee4f033dSBarry Smith   PetscFunctionReturn(0);
2060ee4f033dSBarry Smith }
2061ee4f033dSBarry Smith 
2062ac90fabeSBarry Smith #include "petscblaslapack.h"
2063ac90fabeSBarry Smith #undef __FUNCT__
2064ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2065f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2066ac90fabeSBarry Smith {
2067dfbe8321SBarry Smith   PetscErrorCode ierr;
206897f1f81fSBarry Smith   PetscInt       i;
2069ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
20704ce68768SBarry Smith   PetscBLASInt   one=1,bnz = (PetscBLASInt)x->nz;
2071ac90fabeSBarry Smith 
2072ac90fabeSBarry Smith   PetscFunctionBegin;
2073ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2074f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2075f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2076c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2077a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2078a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2079a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2080a30b2313SHong Zhang     }
2081a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
208224f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2083a30b2313SHong Zhang       y->XtoY = X;
2084c537a176SHong Zhang     }
2085f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
208663ba0a88SBarry 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);
2087ac90fabeSBarry Smith   } else {
2088f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2089ac90fabeSBarry Smith   }
2090ac90fabeSBarry Smith   PetscFunctionReturn(0);
2091ac90fabeSBarry Smith }
2092ac90fabeSBarry Smith 
2093521d7252SBarry Smith #undef __FUNCT__
2094521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2095521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2096521d7252SBarry Smith {
2097521d7252SBarry Smith   PetscFunctionBegin;
2098521d7252SBarry Smith   PetscFunctionReturn(0);
2099521d7252SBarry Smith }
2100521d7252SBarry Smith 
2101354c94deSBarry Smith #undef __FUNCT__
2102354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2103354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2104354c94deSBarry Smith {
2105354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2106354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2107354c94deSBarry Smith   PetscInt    i,nz;
2108354c94deSBarry Smith   PetscScalar *a;
2109354c94deSBarry Smith 
2110354c94deSBarry Smith   PetscFunctionBegin;
2111354c94deSBarry Smith   nz = aij->nz;
2112354c94deSBarry Smith   a  = aij->a;
2113354c94deSBarry Smith   for (i=0; i<nz; i++) {
2114354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2115354c94deSBarry Smith   }
2116354c94deSBarry Smith #else
2117354c94deSBarry Smith   PetscFunctionBegin;
2118354c94deSBarry Smith #endif
2119354c94deSBarry Smith   PetscFunctionReturn(0);
2120354c94deSBarry Smith }
2121354c94deSBarry Smith 
2122e34fafa9SBarry Smith #undef __FUNCT__
2123e34fafa9SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2124e34fafa9SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v)
2125e34fafa9SBarry Smith {
2126e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2127e34fafa9SBarry Smith   PetscErrorCode ierr;
2128e34fafa9SBarry Smith   PetscInt       i,j,m = A->m,*ai,*aj,ncols,n;
2129e34fafa9SBarry Smith   PetscReal      atmp;
2130e34fafa9SBarry Smith   PetscScalar    *x,zero = 0.0;
2131e34fafa9SBarry Smith   MatScalar      *aa;
2132e34fafa9SBarry Smith 
2133e34fafa9SBarry Smith   PetscFunctionBegin;
2134e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2135e34fafa9SBarry Smith   aa   = a->a;
2136e34fafa9SBarry Smith   ai   = a->i;
2137e34fafa9SBarry Smith   aj   = a->j;
2138e34fafa9SBarry Smith 
213943e18e04SHong Zhang   ierr = VecSet(v,zero);CHKERRQ(ierr);
2140e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2141e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2142e34fafa9SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2143e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2144e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2145e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2146e34fafa9SBarry Smith       atmp = PetscAbsScalar(*aa); aa++;
2147e34fafa9SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) x[i] = atmp;
2148e34fafa9SBarry Smith       aj++;
2149e34fafa9SBarry Smith     }
2150e34fafa9SBarry Smith   }
2151e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2152e34fafa9SBarry Smith   PetscFunctionReturn(0);
2153e34fafa9SBarry Smith }
2154e34fafa9SBarry Smith 
2155682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
21560a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2157cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2158cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2159cb5b572fSBarry Smith        MatMult_SeqAIJ,
216097304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
21617c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
21627c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2163cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2164cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
21657c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
216697304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2167cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2168cb5b572fSBarry Smith        0,
216917ab2063SBarry Smith        MatRelax_SeqAIJ,
217017ab2063SBarry Smith        MatTranspose_SeqAIJ,
217197304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2172cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2173cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2174cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2175cb5b572fSBarry Smith        MatNorm_SeqAIJ,
217697304618SKris Buschelman /*20*/ 0,
2177cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
217817ab2063SBarry Smith        MatCompress_SeqAIJ,
2179cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2180cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
218197304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2182cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2183cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2184f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2185a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
218697304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2187cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2188861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
21896c0721eeSBarry Smith        MatGetArray_SeqAIJ,
21906c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
219197304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2192cb5b572fSBarry Smith        0,
2193cb5b572fSBarry Smith        0,
2194cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2195cb5b572fSBarry Smith        0,
219697304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2197cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2198cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2199cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2200cb5b572fSBarry Smith        MatCopy_SeqAIJ,
220197304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ,
2202cb5b572fSBarry Smith        MatScale_SeqAIJ,
2203cb5b572fSBarry Smith        0,
2204cb5b572fSBarry Smith        0,
22056945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2206521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
22073b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
22083b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
22093b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2210a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
221197304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2212b9617806SBarry Smith        0,
22130513a670SBarry Smith        0,
2214cda55fadSBarry Smith        MatPermute_SeqAIJ,
2215cda55fadSBarry Smith        0,
221697304618SKris Buschelman /*60*/ 0,
2217b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2218b9b97703SBarry Smith        MatView_SeqAIJ,
22198a124369SBarry Smith        MatGetPetscMaps_Petsc,
2220ee4f033dSBarry Smith        0,
222197304618SKris Buschelman /*65*/ 0,
2222ee4f033dSBarry Smith        0,
2223ee4f033dSBarry Smith        0,
2224ee4f033dSBarry Smith        0,
2225ee4f033dSBarry Smith        0,
2226e34fafa9SBarry Smith /*70*/ MatGetRowMax_SeqAIJ,
2227ee4f033dSBarry Smith        0,
2228ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2229dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2230ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2231dcf5cc72SBarry Smith #else
2232dcf5cc72SBarry Smith        0,
2233dcf5cc72SBarry Smith #endif
2234ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
223597304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ,
223697304618SKris Buschelman        0,
223797304618SKris Buschelman        0,
223897304618SKris Buschelman        0,
223997304618SKris Buschelman        0,
224097304618SKris Buschelman /*80*/ 0,
224197304618SKris Buschelman        0,
224297304618SKris Buschelman        0,
224397304618SKris Buschelman        0,
2244bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2245bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
22466284ec50SHong Zhang        0,
22476284ec50SHong Zhang        0,
22486284ec50SHong Zhang        0,
2249bc011b1eSHong Zhang        0,
2250bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
225126be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
225226be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2253d439da42SKris Buschelman        MatPtAP_Basic,
22547ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
22557ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2256bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2257bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2258bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
22597ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
22607ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2261609c6c4dSKris Buschelman        0,
2262609c6c4dSKris Buschelman        0,
2263354c94deSBarry Smith        MatConjugate_SeqAIJ
22649e29f15eSvictorle };
226517ab2063SBarry Smith 
2266fb2e594dSBarry Smith EXTERN_C_BEGIN
22674a2ae208SSatish Balay #undef __FUNCT__
22684a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2269be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2270bef8e0ddSBarry Smith {
2271bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
227297f1f81fSBarry Smith   PetscInt   i,nz,n;
2273bef8e0ddSBarry Smith 
2274bef8e0ddSBarry Smith   PetscFunctionBegin;
2275bef8e0ddSBarry Smith 
2276bef8e0ddSBarry Smith   nz = aij->maxnz;
2277273d9f13SBarry Smith   n  = mat->n;
2278bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2279bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2280bef8e0ddSBarry Smith   }
2281bef8e0ddSBarry Smith   aij->nz = nz;
2282bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2283bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2284bef8e0ddSBarry Smith   }
2285bef8e0ddSBarry Smith 
2286bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2287bef8e0ddSBarry Smith }
2288fb2e594dSBarry Smith EXTERN_C_END
2289bef8e0ddSBarry Smith 
22904a2ae208SSatish Balay #undef __FUNCT__
22914a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2292bef8e0ddSBarry Smith /*@
2293bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2294bef8e0ddSBarry Smith        in the matrix.
2295bef8e0ddSBarry Smith 
2296bef8e0ddSBarry Smith   Input Parameters:
2297bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2298bef8e0ddSBarry Smith -  indices - the column indices
2299bef8e0ddSBarry Smith 
230015091d37SBarry Smith   Level: advanced
230115091d37SBarry Smith 
2302bef8e0ddSBarry Smith   Notes:
2303bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2304bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2305bef8e0ddSBarry Smith   of the MatSetValues() operation.
2306bef8e0ddSBarry Smith 
2307bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2308d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2309bef8e0ddSBarry Smith 
2310bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2311bef8e0ddSBarry Smith 
2312b9617806SBarry Smith     The indices should start with zero, not one.
2313b9617806SBarry Smith 
2314bef8e0ddSBarry Smith @*/
2315be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2316bef8e0ddSBarry Smith {
231797f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2318bef8e0ddSBarry Smith 
2319bef8e0ddSBarry Smith   PetscFunctionBegin;
23204482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
23214482741eSBarry Smith   PetscValidPointer(indices,2);
2322c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2323bef8e0ddSBarry Smith   if (f) {
2324bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2325bef8e0ddSBarry Smith   } else {
2326634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2327bef8e0ddSBarry Smith   }
2328bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2329bef8e0ddSBarry Smith }
2330bef8e0ddSBarry Smith 
2331be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2332be6bf707SBarry Smith 
2333fb2e594dSBarry Smith EXTERN_C_BEGIN
23344a2ae208SSatish Balay #undef __FUNCT__
23354a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2336be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2337be6bf707SBarry Smith {
2338be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23396849ba73SBarry Smith   PetscErrorCode ierr;
23406849ba73SBarry Smith   size_t         nz = aij->i[mat->m];
2341be6bf707SBarry Smith 
2342be6bf707SBarry Smith   PetscFunctionBegin;
2343be6bf707SBarry Smith   if (aij->nonew != 1) {
2344634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2345be6bf707SBarry Smith   }
2346be6bf707SBarry Smith 
2347be6bf707SBarry Smith   /* allocate space for values if not already there */
2348be6bf707SBarry Smith   if (!aij->saved_values) {
234987828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2350be6bf707SBarry Smith   }
2351be6bf707SBarry Smith 
2352be6bf707SBarry Smith   /* copy values over */
235387828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2354be6bf707SBarry Smith   PetscFunctionReturn(0);
2355be6bf707SBarry Smith }
2356fb2e594dSBarry Smith EXTERN_C_END
2357be6bf707SBarry Smith 
23584a2ae208SSatish Balay #undef __FUNCT__
2359b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2360be6bf707SBarry Smith /*@
2361be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2362be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2363be6bf707SBarry Smith        nonlinear portion.
2364be6bf707SBarry Smith 
2365be6bf707SBarry Smith    Collect on Mat
2366be6bf707SBarry Smith 
2367be6bf707SBarry Smith   Input Parameters:
23680e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2369be6bf707SBarry Smith 
237015091d37SBarry Smith   Level: advanced
237115091d37SBarry Smith 
2372be6bf707SBarry Smith   Common Usage, with SNESSolve():
2373be6bf707SBarry Smith $    Create Jacobian matrix
2374be6bf707SBarry Smith $    Set linear terms into matrix
2375be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2376be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2377be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2378be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2379be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2380be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2381be6bf707SBarry Smith $    In your Jacobian routine
2382be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2383be6bf707SBarry Smith $      Set nonlinear terms in matrix
2384be6bf707SBarry Smith 
2385be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2386be6bf707SBarry Smith $    // build linear portion of Jacobian
2387be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2388be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2389be6bf707SBarry Smith $    loop over nonlinear iterations
2390be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2391be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2392be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2393be6bf707SBarry Smith $       Solve linear system with Jacobian
2394be6bf707SBarry Smith $    endloop
2395be6bf707SBarry Smith 
2396be6bf707SBarry Smith   Notes:
2397be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2398be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2399be6bf707SBarry Smith     calling this routine.
2400be6bf707SBarry Smith 
24010c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
24020c468ba9SBarry Smith     and does not allocated additional space.
24030c468ba9SBarry Smith 
2404be6bf707SBarry Smith .seealso: MatRetrieveValues()
2405be6bf707SBarry Smith 
2406be6bf707SBarry Smith @*/
2407be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2408be6bf707SBarry Smith {
2409dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2410be6bf707SBarry Smith 
2411be6bf707SBarry Smith   PetscFunctionBegin;
24124482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
241329bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
241429bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2415be6bf707SBarry Smith 
2416c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2417be6bf707SBarry Smith   if (f) {
2418be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2419be6bf707SBarry Smith   } else {
2420634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2421be6bf707SBarry Smith   }
2422be6bf707SBarry Smith   PetscFunctionReturn(0);
2423be6bf707SBarry Smith }
2424be6bf707SBarry Smith 
2425fb2e594dSBarry Smith EXTERN_C_BEGIN
24264a2ae208SSatish Balay #undef __FUNCT__
24274a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2428be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2429be6bf707SBarry Smith {
2430be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
24316849ba73SBarry Smith   PetscErrorCode ierr;
243297f1f81fSBarry Smith   PetscInt       nz = aij->i[mat->m];
2433be6bf707SBarry Smith 
2434be6bf707SBarry Smith   PetscFunctionBegin;
2435be6bf707SBarry Smith   if (aij->nonew != 1) {
2436634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2437be6bf707SBarry Smith   }
2438be6bf707SBarry Smith   if (!aij->saved_values) {
2439634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2440be6bf707SBarry Smith   }
2441be6bf707SBarry Smith   /* copy values over */
244287828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2443be6bf707SBarry Smith   PetscFunctionReturn(0);
2444be6bf707SBarry Smith }
2445fb2e594dSBarry Smith EXTERN_C_END
2446be6bf707SBarry Smith 
24474a2ae208SSatish Balay #undef __FUNCT__
24484a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2449be6bf707SBarry Smith /*@
2450be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2451be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2452be6bf707SBarry Smith        nonlinear portion.
2453be6bf707SBarry Smith 
2454be6bf707SBarry Smith    Collect on Mat
2455be6bf707SBarry Smith 
2456be6bf707SBarry Smith   Input Parameters:
2457be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2458be6bf707SBarry Smith 
245915091d37SBarry Smith   Level: advanced
246015091d37SBarry Smith 
2461be6bf707SBarry Smith .seealso: MatStoreValues()
2462be6bf707SBarry Smith 
2463be6bf707SBarry Smith @*/
2464be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2465be6bf707SBarry Smith {
2466dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2467be6bf707SBarry Smith 
2468be6bf707SBarry Smith   PetscFunctionBegin;
24694482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
247029bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
247129bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2472be6bf707SBarry Smith 
2473c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2474be6bf707SBarry Smith   if (f) {
2475be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2476be6bf707SBarry Smith   } else {
2477634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2478be6bf707SBarry Smith   }
2479be6bf707SBarry Smith   PetscFunctionReturn(0);
2480be6bf707SBarry Smith }
2481be6bf707SBarry Smith 
2482f83d6046SBarry Smith 
2483be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24844a2ae208SSatish Balay #undef __FUNCT__
24854a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
248617ab2063SBarry Smith /*@C
2487682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24880d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24896e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
249051c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24912bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
249217ab2063SBarry Smith 
2493db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2494db81eaa0SLois Curfman McInnes 
249517ab2063SBarry Smith    Input Parameters:
2496db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
249717ab2063SBarry Smith .  m - number of rows
249817ab2063SBarry Smith .  n - number of columns
249917ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
250051c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
25012bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
250217ab2063SBarry Smith 
250317ab2063SBarry Smith    Output Parameter:
2504416022c9SBarry Smith .  A - the matrix
250517ab2063SBarry Smith 
2506b259b22eSLois Curfman McInnes    Notes:
250749a6f317SBarry Smith    If nnz is given then nz is ignored
250849a6f317SBarry Smith 
250917ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
251017ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
25110002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
251244cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
251317ab2063SBarry Smith 
251417ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2515a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
25163d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
25176da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
251817ab2063SBarry Smith 
2519682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
25204fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2521682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
25226c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
25236c7ebb05SLois Curfman McInnes 
25246c7ebb05SLois Curfman McInnes    Options Database Keys:
2525698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2526698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2527db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2528db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2529db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
253017ab2063SBarry Smith 
2531027ccd11SLois Curfman McInnes    Level: intermediate
2532027ccd11SLois Curfman McInnes 
253336db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
253436db0b34SBarry Smith 
253517ab2063SBarry Smith @*/
2536be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
253717ab2063SBarry Smith {
2538dfbe8321SBarry Smith   PetscErrorCode ierr;
25396945ee14SBarry Smith 
25403a40ed3dSBarry Smith   PetscFunctionBegin;
2541f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2542f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2543273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2544ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2545273d9f13SBarry Smith   PetscFunctionReturn(0);
2546273d9f13SBarry Smith }
2547273d9f13SBarry Smith 
25484a2ae208SSatish Balay #undef __FUNCT__
25494a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2550273d9f13SBarry Smith /*@C
2551273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2552273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2553273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2554273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2555273d9f13SBarry Smith 
2556273d9f13SBarry Smith    Collective on MPI_Comm
2557273d9f13SBarry Smith 
2558273d9f13SBarry Smith    Input Parameters:
255945bfc511SMatthew Knepley +  B - The matrix
2560273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2561273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2562273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2563273d9f13SBarry Smith 
2564273d9f13SBarry Smith    Notes:
256549a6f317SBarry Smith      If nnz is given then nz is ignored
256649a6f317SBarry Smith 
2567273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2568273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2569273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2570273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2571273d9f13SBarry Smith 
2572273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2573273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2574273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2575273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2576273d9f13SBarry Smith 
2577a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2578a96a251dSBarry Smith    entries or columns indices
2579a96a251dSBarry Smith 
2580273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2581273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2582273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2583273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2584273d9f13SBarry Smith 
2585273d9f13SBarry Smith    Options Database Keys:
2586698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2587698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2588273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2589273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2590273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2591273d9f13SBarry Smith 
2592273d9f13SBarry Smith    Level: intermediate
2593273d9f13SBarry Smith 
2594273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2595273d9f13SBarry Smith 
2596273d9f13SBarry Smith @*/
2597be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2598273d9f13SBarry Smith {
259997f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2600a23d5eceSKris Buschelman 
2601a23d5eceSKris Buschelman   PetscFunctionBegin;
2602a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2603a23d5eceSKris Buschelman   if (f) {
2604a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2605a23d5eceSKris Buschelman   }
2606a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2607a23d5eceSKris Buschelman }
2608a23d5eceSKris Buschelman 
2609a23d5eceSKris Buschelman EXTERN_C_BEGIN
2610a23d5eceSKris Buschelman #undef __FUNCT__
2611a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2612be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2613a23d5eceSKris Buschelman {
2614273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2615a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
26166849ba73SBarry Smith   PetscErrorCode ierr;
261797f1f81fSBarry Smith   PetscInt       i;
2618273d9f13SBarry Smith 
2619273d9f13SBarry Smith   PetscFunctionBegin;
2620d5d45c9bSBarry Smith 
2621a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2622c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2623c461c341SBarry Smith     nz             = 0;
2624c461c341SBarry Smith   }
2625c461c341SBarry Smith 
2626435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2627435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2628b73539f3SBarry Smith   if (nnz) {
2629273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
263029bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
26313a7fca6bSBarry 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);
2632b73539f3SBarry Smith     }
2633b73539f3SBarry Smith   }
2634b73539f3SBarry Smith 
2635273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2636273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2637273d9f13SBarry Smith 
2638ab93d7beSBarry Smith   if (!skipallocation) {
2639ab93d7beSBarry Smith     ierr = PetscMalloc2(B->m,PetscInt,&b->imax,B->m,PetscInt,&b->ilen);CHKERRQ(ierr);
2640273d9f13SBarry Smith     if (!nnz) {
2641435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2642273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2643273d9f13SBarry Smith       for (i=0; i<B->m; i++) b->imax[i] = nz;
2644273d9f13SBarry Smith       nz = nz*B->m;
2645273d9f13SBarry Smith     } else {
2646273d9f13SBarry Smith       nz = 0;
2647273d9f13SBarry Smith       for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2648273d9f13SBarry Smith     }
2649273d9f13SBarry Smith 
2650ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2651ab93d7beSBarry Smith     for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2652ab93d7beSBarry Smith 
2653273d9f13SBarry Smith     /* allocate the matrix space */
2654a96a251dSBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->m+1,PetscInt,&b->i);CHKERRQ(ierr);
2655bfeeae90SHong Zhang     b->i[0] = 0;
26565da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
26575da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
26585da197adSKris Buschelman     }
2659273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2660273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2661c461c341SBarry Smith   } else {
2662c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2663c461c341SBarry Smith   }
2664273d9f13SBarry Smith 
2665273d9f13SBarry Smith   b->nz                = 0;
2666273d9f13SBarry Smith   b->maxnz             = nz;
2667273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2668273d9f13SBarry Smith   PetscFunctionReturn(0);
2669273d9f13SBarry Smith }
2670a23d5eceSKris Buschelman EXTERN_C_END
2671273d9f13SBarry Smith 
26720bad9183SKris Buschelman /*MC
2673fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
26740bad9183SKris Buschelman    based on compressed sparse row format.
26750bad9183SKris Buschelman 
26760bad9183SKris Buschelman    Options Database Keys:
26770bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
26780bad9183SKris Buschelman 
26790bad9183SKris Buschelman   Level: beginner
26800bad9183SKris Buschelman 
2681f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
26820bad9183SKris Buschelman M*/
26830bad9183SKris Buschelman 
2684a6175056SHong Zhang EXTERN_C_BEGIN
26854a2ae208SSatish Balay #undef __FUNCT__
26864a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2687be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
2688273d9f13SBarry Smith {
2689273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2690dfbe8321SBarry Smith   PetscErrorCode ierr;
269138baddfdSBarry Smith   PetscMPIInt    size;
2692273d9f13SBarry Smith 
2693273d9f13SBarry Smith   PetscFunctionBegin;
2694273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2695273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2696273d9f13SBarry Smith 
2697273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2698273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2699273d9f13SBarry Smith 
2700b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2701b0a32e0cSBarry Smith   B->data             = (void*)b;
2702549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2703416022c9SBarry Smith   B->factor           = 0;
270490f02eecSBarry Smith   B->mapping          = 0;
2705416022c9SBarry Smith   b->row              = 0;
2706416022c9SBarry Smith   b->col              = 0;
270782bf6240SBarry Smith   b->icol             = 0;
2708b810aeb4SBarry Smith   b->reallocs         = 0;
270917ab2063SBarry Smith 
27108a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
27118a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2712a5ae1ecdSBarry Smith 
2713f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
271436db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2715f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2716416022c9SBarry Smith   b->nonew             = 0;
2717416022c9SBarry Smith   b->diag              = 0;
2718416022c9SBarry Smith   b->solve_work        = 0;
27192a1b7f2aSHong Zhang   B->spptr             = 0;
2720be6bf707SBarry Smith   b->saved_values      = 0;
2721d7f994e1SBarry Smith   b->idiag             = 0;
2722d7f994e1SBarry Smith   b->ssor              = 0;
2723f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2724a30b2313SHong Zhang   b->xtoy              = 0;
2725a30b2313SHong Zhang   b->XtoY              = 0;
272673e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
2727d487561eSHong Zhang   b->compressedrow.nrows   = B->m;
2728d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
2729d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
2730d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
273188e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
273217ab2063SBarry Smith 
273335d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
273435d8aa7fSBarry Smith 
2735f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2736bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2737bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2738f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2739be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2740bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2741f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2742be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2743bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2744a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2745a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2746a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
274785fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
274885fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
274985fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
27505fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
27515fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
27525fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2753a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2754a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2755a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
275605b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
275705b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
275805b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
27594846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
27603a40ed3dSBarry Smith   PetscFunctionReturn(0);
276117ab2063SBarry Smith }
2762273d9f13SBarry Smith EXTERN_C_END
276317ab2063SBarry Smith 
27644a2ae208SSatish Balay #undef __FUNCT__
27654a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2766dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
276717ab2063SBarry Smith {
2768416022c9SBarry Smith   Mat            C;
2769416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
27706849ba73SBarry Smith   PetscErrorCode ierr;
277197f1f81fSBarry Smith   PetscInt       i,m = A->m;
277217ab2063SBarry Smith 
27733a40ed3dSBarry Smith   PetscFunctionBegin;
27744043dd9cSLois Curfman McInnes   *B = 0;
2775f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
2776f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->m,A->n,A->m,A->n);CHKERRQ(ierr);
2777be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
27781d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
27791d5dac46SHong Zhang 
2780273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
2781273d9f13SBarry Smith 
2782416022c9SBarry Smith   C->factor           = A->factor;
27836ad4291fSHong Zhang 
2784416022c9SBarry Smith   c->row            = 0;
2785416022c9SBarry Smith   c->col            = 0;
278682bf6240SBarry Smith   c->icol           = 0;
27876ad4291fSHong Zhang   c->reallocs       = 0;
278817ab2063SBarry Smith 
27896ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
279017ab2063SBarry Smith 
279133b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
279217ab2063SBarry Smith   for (i=0; i<m; i++) {
2793416022c9SBarry Smith     c->imax[i] = a->imax[i];
2794416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
279517ab2063SBarry Smith   }
279617ab2063SBarry Smith 
279717ab2063SBarry Smith   /* allocate the matrix space */
2798a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
2799f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
280097f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
280117ab2063SBarry Smith   if (m > 0) {
280297f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
2803be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2804bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2805be6bf707SBarry Smith     } else {
2806bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
280717ab2063SBarry Smith     }
280808480c60SBarry Smith   }
280917ab2063SBarry Smith 
2810416022c9SBarry Smith   c->sorted            = a->sorted;
28116ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
2812416022c9SBarry Smith   c->roworiented       = a->roworiented;
2813416022c9SBarry Smith   c->nonew             = a->nonew;
2814416022c9SBarry Smith   if (a->diag) {
281597f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
281652e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
281717ab2063SBarry Smith     for (i=0; i<m; i++) {
2818416022c9SBarry Smith       c->diag[i] = a->diag[i];
281917ab2063SBarry Smith     }
28203a40ed3dSBarry Smith   } else c->diag        = 0;
28216ad4291fSHong Zhang   c->solve_work         = 0;
28226ad4291fSHong Zhang   c->saved_values          = 0;
28236ad4291fSHong Zhang   c->idiag                 = 0;
28246ad4291fSHong Zhang   c->ssor                  = 0;
28256ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
28266ad4291fSHong Zhang   c->freedata              = PETSC_TRUE;
28276ad4291fSHong Zhang   c->xtoy                  = 0;
28286ad4291fSHong Zhang   c->XtoY                  = 0;
28296ad4291fSHong Zhang 
2830416022c9SBarry Smith   c->nz                 = a->nz;
2831416022c9SBarry Smith   c->maxnz              = a->maxnz;
2832273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2833754ec7b1SSatish Balay 
28346ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
28356ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
28366ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
28376ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
28386ad4291fSHong Zhang     i = a->compressedrow.nrows;
28396ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
28406ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
28416ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
28426ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
284327ea64f8SHong Zhang   } else {
284427ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
284527ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
284627ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
28476ad4291fSHong Zhang   }
284888e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
28496ad4291fSHong Zhang 
28504846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
28514846f1f5SKris Buschelman 
2852416022c9SBarry Smith   *B = C;
2853b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
28543a40ed3dSBarry Smith   PetscFunctionReturn(0);
285517ab2063SBarry Smith }
285617ab2063SBarry Smith 
28574a2ae208SSatish Balay #undef __FUNCT__
28584a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2859f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A)
286017ab2063SBarry Smith {
2861416022c9SBarry Smith   Mat_SeqAIJ     *a;
2862416022c9SBarry Smith   Mat            B;
28636849ba73SBarry Smith   PetscErrorCode ierr;
28643c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
286538baddfdSBarry Smith   int            fd;
286638baddfdSBarry Smith   PetscMPIInt    size;
2867bcd2baecSBarry Smith   MPI_Comm       comm;
286817ab2063SBarry Smith 
28693a40ed3dSBarry Smith   PetscFunctionBegin;
2870e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2871d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
287229bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2873b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
28740752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2875552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
287617ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
287717ab2063SBarry Smith 
2878d64ed03dSBarry Smith   if (nz < 0) {
287929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2880d64ed03dSBarry Smith   }
2881d64ed03dSBarry Smith 
288217ab2063SBarry Smith   /* read in row lengths */
288397f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
28840752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
288517ab2063SBarry Smith 
28863c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
28873c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
28883c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
28893c601197SSatish Balay 
289017ab2063SBarry Smith   /* create our matrix */
2891f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
2892f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
2893b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2894ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
2895416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
289617ab2063SBarry Smith 
289717ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
28980752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
289917ab2063SBarry Smith 
290017ab2063SBarry Smith   /* read in nonzero values */
29010752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
290217ab2063SBarry Smith 
290317ab2063SBarry Smith   /* set matrix "i" values */
2904efb16452SHong Zhang   a->i[0] = 0;
290517ab2063SBarry Smith   for (i=1; i<= M; i++) {
2906416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2907416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
290817ab2063SBarry Smith   }
2909606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
291017ab2063SBarry Smith 
29116d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
29126d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2913b3a1e11cSKris Buschelman   *A = B;
29143a40ed3dSBarry Smith   PetscFunctionReturn(0);
291517ab2063SBarry Smith }
291617ab2063SBarry Smith 
29174a2ae208SSatish Balay #undef __FUNCT__
2918b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
2919dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
29207264ac53SSatish Balay {
29217264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
2922dfbe8321SBarry Smith   PetscErrorCode ierr;
29237264ac53SSatish Balay 
29243a40ed3dSBarry Smith   PetscFunctionBegin;
2925bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
2926bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
2927ca44d042SBarry Smith     *flg = PETSC_FALSE;
2928ca44d042SBarry Smith     PetscFunctionReturn(0);
2929bcd2baecSBarry Smith   }
29307264ac53SSatish Balay 
29317264ac53SSatish Balay   /* if the a->i are the same */
293297f1f81fSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
2933abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
29347264ac53SSatish Balay 
29357264ac53SSatish Balay   /* if a->j are the same */
293697f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
2937abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
2938bcd2baecSBarry Smith 
2939bcd2baecSBarry Smith   /* if a->a are the same */
294087828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
29410f5bd95cSBarry Smith 
29423a40ed3dSBarry Smith   PetscFunctionReturn(0);
29437264ac53SSatish Balay 
29447264ac53SSatish Balay }
294536db0b34SBarry Smith 
29464a2ae208SSatish Balay #undef __FUNCT__
29474a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
294836db0b34SBarry Smith /*@C
294936db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
295036db0b34SBarry Smith               provided by the user.
295136db0b34SBarry Smith 
295236db0b34SBarry Smith       Coolective on MPI_Comm
295336db0b34SBarry Smith 
295436db0b34SBarry Smith    Input Parameters:
295536db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
295636db0b34SBarry Smith .   m - number of rows
295736db0b34SBarry Smith .   n - number of columns
295836db0b34SBarry Smith .   i - row indices
295936db0b34SBarry Smith .   j - column indices
296036db0b34SBarry Smith -   a - matrix values
296136db0b34SBarry Smith 
296236db0b34SBarry Smith    Output Parameter:
296336db0b34SBarry Smith .   mat - the matrix
296436db0b34SBarry Smith 
296536db0b34SBarry Smith    Level: intermediate
296636db0b34SBarry Smith 
296736db0b34SBarry Smith    Notes:
29680551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
296936db0b34SBarry Smith     once the matrix is destroyed
297036db0b34SBarry Smith 
297136db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
297236db0b34SBarry Smith 
2973bfeeae90SHong Zhang        The i and j indices are 0 based
297436db0b34SBarry Smith 
297536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
297636db0b34SBarry Smith 
297736db0b34SBarry Smith @*/
2978be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
297936db0b34SBarry Smith {
2980dfbe8321SBarry Smith   PetscErrorCode ierr;
298197f1f81fSBarry Smith   PetscInt       ii;
298236db0b34SBarry Smith   Mat_SeqAIJ     *aij;
298336db0b34SBarry Smith 
298436db0b34SBarry Smith   PetscFunctionBegin;
2985a96a251dSBarry Smith   if (i[0]) {
2986634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
298736db0b34SBarry Smith   }
2988f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2989f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2990ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
2991ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2992ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
2993ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
2994ab93d7beSBarry Smith 
299536db0b34SBarry Smith   aij->i = i;
299636db0b34SBarry Smith   aij->j = j;
299736db0b34SBarry Smith   aij->a = a;
299836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
299936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
300036db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
300136db0b34SBarry Smith 
300236db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
300336db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
30042515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
300579a5c55eSBarry 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]);
300636db0b34SBarry Smith #endif
300736db0b34SBarry Smith   }
30082515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
300936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
301079a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
301179a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
301236db0b34SBarry Smith   }
301336db0b34SBarry Smith #endif
301436db0b34SBarry Smith 
3015b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3016b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
301736db0b34SBarry Smith   PetscFunctionReturn(0);
301836db0b34SBarry Smith }
301936db0b34SBarry Smith 
3020cc8ba8e1SBarry Smith #undef __FUNCT__
3021ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3022dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3023cc8ba8e1SBarry Smith {
3024dfbe8321SBarry Smith   PetscErrorCode ierr;
3025cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
302636db0b34SBarry Smith 
3027cc8ba8e1SBarry Smith   PetscFunctionBegin;
302812c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
3029cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3030cc8ba8e1SBarry Smith     a->coloring = coloring;
303112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
303297f1f81fSBarry Smith     PetscInt             i,*larray;
303312c595b3SBarry Smith     ISColoring      ocoloring;
303408b6dcc0SBarry Smith     ISColoringValue *colors;
303512c595b3SBarry Smith 
303612c595b3SBarry Smith     /* set coloring for diagonal portion */
303797f1f81fSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
303812c595b3SBarry Smith     for (i=0; i<A->n; i++) {
303912c595b3SBarry Smith       larray[i] = i;
304012c595b3SBarry Smith     }
304112c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
304208b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
304312c595b3SBarry Smith     for (i=0; i<A->n; i++) {
304412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
304512c595b3SBarry Smith     }
304612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
304712c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
304812c595b3SBarry Smith     a->coloring = ocoloring;
304912c595b3SBarry Smith   }
3050cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3051cc8ba8e1SBarry Smith }
3052cc8ba8e1SBarry Smith 
3053dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3054ee4f033dSBarry Smith EXTERN_C_BEGIN
305529c1e371SBarry Smith #include "adic/ad_utils.h"
3056ee4f033dSBarry Smith EXTERN_C_END
3057cc8ba8e1SBarry Smith 
3058cc8ba8e1SBarry Smith #undef __FUNCT__
3059ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3060dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3061cc8ba8e1SBarry Smith {
3062cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
306397f1f81fSBarry Smith   PetscInt        m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
30644440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
306508b6dcc0SBarry Smith   ISColoringValue *color;
3066cc8ba8e1SBarry Smith 
3067cc8ba8e1SBarry Smith   PetscFunctionBegin;
3068e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
30694440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3070cc8ba8e1SBarry Smith   color = a->coloring->colors;
3071cc8ba8e1SBarry Smith   /* loop over rows */
3072cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3073cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3074cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3075cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3076cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3077cc8ba8e1SBarry Smith     }
30784440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3079ee4f033dSBarry Smith   }
3080ee4f033dSBarry Smith   PetscFunctionReturn(0);
3081ee4f033dSBarry Smith }
3082ee4f033dSBarry Smith #endif
3083ee4f033dSBarry Smith 
3084ee4f033dSBarry Smith #undef __FUNCT__
3085ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
308697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3087ee4f033dSBarry Smith {
3088ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
308997f1f81fSBarry Smith   PetscInt             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
309087828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
309108b6dcc0SBarry Smith   ISColoringValue *color;
3092ee4f033dSBarry Smith 
3093ee4f033dSBarry Smith   PetscFunctionBegin;
3094e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3095ee4f033dSBarry Smith   color = a->coloring->colors;
3096ee4f033dSBarry Smith   /* loop over rows */
3097ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3098ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3099ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3100ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3101ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3102ee4f033dSBarry Smith     }
3103ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3104cc8ba8e1SBarry Smith   }
3105cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3106cc8ba8e1SBarry Smith }
310736db0b34SBarry Smith 
3108