xref: /petsc/src/mat/impls/aij/seq/aij.c (revision efb308899f58eff35e31f4136d1431f0c81bf4bc)
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 
1517cd84e04SBarry 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);
670a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
671901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
6723a40ed3dSBarry Smith   PetscFunctionReturn(0);
67317ab2063SBarry Smith }
67417ab2063SBarry Smith 
6754a2ae208SSatish Balay #undef __FUNCT__
6764a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
677dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
67817ab2063SBarry Smith {
6793a40ed3dSBarry Smith   PetscFunctionBegin;
6803a40ed3dSBarry Smith   PetscFunctionReturn(0);
68117ab2063SBarry Smith }
68217ab2063SBarry Smith 
6834a2ae208SSatish Balay #undef __FUNCT__
6844a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
685dfbe8321SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op)
68617ab2063SBarry Smith {
687416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6884846f1f5SKris Buschelman   PetscErrorCode ierr;
6893a40ed3dSBarry Smith 
6903a40ed3dSBarry Smith   PetscFunctionBegin;
691a65d3064SKris Buschelman   switch (op) {
692a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
693a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
694a65d3064SKris Buschelman       break;
695a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
696a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
697a65d3064SKris Buschelman       break;
698a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
699a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
700a65d3064SKris Buschelman       break;
701a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
702a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
703a65d3064SKris Buschelman       break;
704a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
705a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
706a65d3064SKris Buschelman       break;
707a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
708a65d3064SKris Buschelman       a->nonew             = 1;
709a65d3064SKris Buschelman       break;
710a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
711a65d3064SKris Buschelman       a->nonew             = -1;
712a65d3064SKris Buschelman       break;
713a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
714a65d3064SKris Buschelman       a->nonew             = -2;
715a65d3064SKris Buschelman       break;
716a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
717a65d3064SKris Buschelman       a->nonew             = 0;
718a65d3064SKris Buschelman       break;
719a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
720a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
721a65d3064SKris Buschelman       break;
722d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
723d487561eSHong Zhang       a->compressedrow.use = PETSC_TRUE;
724d487561eSHong Zhang       break;
725d487561eSHong Zhang     case MAT_DO_NOT_USE_COMPRESSEDROW:
726d487561eSHong Zhang       a->compressedrow.use = PETSC_FALSE;
727d487561eSHong Zhang       break;
728a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
729a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
730a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
731a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
732a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
73363ba0a88SBarry Smith       ierr = PetscLogInfo((A,"MatSetOption_SeqAIJ:Option ignored\n"));CHKERRQ(ierr);
734a65d3064SKris Buschelman       break;
735a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
73629bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
737a65d3064SKris Buschelman     default:
73871c2f376SKris Buschelman       break;
739a65d3064SKris Buschelman   }
7404846f1f5SKris Buschelman   ierr = MatSetOption_Inode(A,op);CHKERRQ(ierr);
7413a40ed3dSBarry Smith   PetscFunctionReturn(0);
74217ab2063SBarry Smith }
74317ab2063SBarry Smith 
7444a2ae208SSatish Balay #undef __FUNCT__
7454a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
746dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
74717ab2063SBarry Smith {
748416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7496849ba73SBarry Smith   PetscErrorCode ierr;
75097f1f81fSBarry Smith   PetscInt       i,j,n;
75187828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
75217ab2063SBarry Smith 
7533a40ed3dSBarry Smith   PetscFunctionBegin;
7542dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
7551ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
75636db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
757273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
758273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
759bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
760bfeeae90SHong Zhang       if (a->j[j] == i) {
761416022c9SBarry Smith         x[i] = a->a[j];
76217ab2063SBarry Smith         break;
76317ab2063SBarry Smith       }
76417ab2063SBarry Smith     }
76517ab2063SBarry Smith   }
7661ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
7673a40ed3dSBarry Smith   PetscFunctionReturn(0);
76817ab2063SBarry Smith }
76917ab2063SBarry Smith 
7704a2ae208SSatish Balay #undef __FUNCT__
7714a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
772dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
77317ab2063SBarry Smith {
774416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
7755c897100SBarry Smith   PetscScalar       *x,*y;
776dfbe8321SBarry Smith   PetscErrorCode    ierr;
77797f1f81fSBarry Smith   PetscInt          m = A->m;
7785c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
7795c897100SBarry Smith   PetscScalar       *v,alpha;
7807b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
7813447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
7824eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
7835c897100SBarry Smith #endif
78417ab2063SBarry Smith 
7853a40ed3dSBarry Smith   PetscFunctionBegin;
7862e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
7871ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
7881ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
7895c897100SBarry Smith 
7905c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
791bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
7925c897100SBarry Smith #else
7933447b6efSHong Zhang   if (usecprow){
7943447b6efSHong Zhang     m    = cprow.nrows;
7953447b6efSHong Zhang     ii   = cprow.i;
7967b2bb3b9SHong Zhang     ridx = cprow.rindex;
7973447b6efSHong Zhang   } else {
7983447b6efSHong Zhang     ii = a->i;
7993447b6efSHong Zhang   }
80017ab2063SBarry Smith   for (i=0; i<m; i++) {
8013447b6efSHong Zhang     idx   = a->j + ii[i] ;
8023447b6efSHong Zhang     v     = a->a + ii[i] ;
8033447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8043447b6efSHong Zhang     if (usecprow){
8057b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8063447b6efSHong Zhang     } else {
80717ab2063SBarry Smith       alpha = x[i];
8083447b6efSHong Zhang     }
80917ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
81017ab2063SBarry Smith   }
8115c897100SBarry Smith #endif
812efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
8131ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8141ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8153a40ed3dSBarry Smith   PetscFunctionReturn(0);
81617ab2063SBarry Smith }
81717ab2063SBarry Smith 
8184a2ae208SSatish Balay #undef __FUNCT__
8195c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
820dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8215c897100SBarry Smith {
8228d5b0100SBarry Smith   PetscScalar    zero = 0.0;
823dfbe8321SBarry Smith   PetscErrorCode ierr;
8245c897100SBarry Smith 
8255c897100SBarry Smith   PetscFunctionBegin;
8262dcb1b2aSMatthew Knepley   ierr = VecSet(yy,zero);CHKERRQ(ierr);
8275c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8285c897100SBarry Smith   PetscFunctionReturn(0);
8295c897100SBarry Smith }
8305c897100SBarry Smith 
8315c897100SBarry Smith 
8325c897100SBarry Smith #undef __FUNCT__
8334a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
834dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
83517ab2063SBarry Smith {
836416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
83797952fefSHong Zhang   PetscScalar    *x,*y,*aa;
838dfbe8321SBarry Smith   PetscErrorCode ierr;
83997952fefSHong Zhang   PetscInt       m=A->m,*aj,*ii;
840aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
84197952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
842362ced78SSatish Balay   PetscScalar    sum;
84397952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
844e36a17ebSSatish Balay #endif
84517ab2063SBarry Smith 
846b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
84797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
848fee21e36SBarry Smith #endif
849fee21e36SBarry Smith 
8503a40ed3dSBarry Smith   PetscFunctionBegin;
8511ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8521ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
85397952fefSHong Zhang   aj  = a->j;
85497952fefSHong Zhang   aa    = a->a;
855416022c9SBarry Smith   ii   = a->i;
856aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
85797952fefSHong Zhang   fortranmultaij_(&m,x,ii,aj,aa,y);
8588d195f9aSBarry Smith #else
8594eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
86097952fefSHong Zhang     m    = a->compressedrow.nrows;
86197952fefSHong Zhang     ii   = a->compressedrow.i;
86297952fefSHong Zhang     ridx = a->compressedrow.rindex;
86397952fefSHong Zhang     for (i=0; i<m; i++){
86497952fefSHong Zhang       n   = ii[i+1] - ii[i];
86597952fefSHong Zhang       aj  = a->j + ii[i];
86697952fefSHong Zhang       aa  = a->a + ii[i];
86797952fefSHong Zhang       sum = 0.0;
86897952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
86997952fefSHong Zhang       y[*ridx++] = sum;
87097952fefSHong Zhang     }
87197952fefSHong Zhang   } else { /* do not use compressed row format */
87217ab2063SBarry Smith     for (i=0; i<m; i++) {
8739ea0dfa2SSatish Balay       jrow = ii[i];
8749ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
87517ab2063SBarry Smith       sum  = 0.0;
8769ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
87797952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
8789ea0dfa2SSatish Balay       }
87917ab2063SBarry Smith       y[i] = sum;
88017ab2063SBarry Smith     }
88197952fefSHong Zhang   }
8828d195f9aSBarry Smith #endif
883efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz - m);CHKERRQ(ierr);
8841ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8851ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8863a40ed3dSBarry Smith   PetscFunctionReturn(0);
88717ab2063SBarry Smith }
88817ab2063SBarry Smith 
8894a2ae208SSatish Balay #undef __FUNCT__
8904a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
891dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
89217ab2063SBarry Smith {
893416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
89497952fefSHong Zhang   PetscScalar    *x,*y,*z,*aa;
895dfbe8321SBarry Smith   PetscErrorCode ierr;
89697952fefSHong Zhang   PetscInt       m = A->m,*aj,*ii;
897aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
89897952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
899362ced78SSatish Balay   PetscScalar    sum;
90097952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
901e36a17ebSSatish Balay #endif
9029ea0dfa2SSatish Balay 
9033a40ed3dSBarry Smith   PetscFunctionBegin;
9041ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9051ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9062e8a6d31SBarry Smith   if (zz != yy) {
9071ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9082e8a6d31SBarry Smith   } else {
9092e8a6d31SBarry Smith     z = y;
9102e8a6d31SBarry Smith   }
911bfeeae90SHong Zhang 
91297952fefSHong Zhang   aj  = a->j;
91397952fefSHong Zhang   aa  = a->a;
914cddf8d76SBarry Smith   ii  = a->i;
915aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
91697952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
91702ab625aSSatish Balay #else
9184eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
9194eb6d288SHong Zhang     if (zz != yy){
9204eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
9214eb6d288SHong Zhang     }
92297952fefSHong Zhang     m    = a->compressedrow.nrows;
92397952fefSHong Zhang     ii   = a->compressedrow.i;
92497952fefSHong Zhang     ridx = a->compressedrow.rindex;
92597952fefSHong Zhang     for (i=0; i<m; i++){
92697952fefSHong Zhang       n  = ii[i+1] - ii[i];
92797952fefSHong Zhang       aj  = a->j + ii[i];
92897952fefSHong Zhang       aa  = a->a + ii[i];
92997952fefSHong Zhang       sum = y[*ridx];
93097952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
93197952fefSHong Zhang       z[*ridx++] = sum;
93297952fefSHong Zhang     }
93397952fefSHong Zhang   } else { /* do not use compressed row format */
93417ab2063SBarry Smith     for (i=0; i<m; i++) {
9359ea0dfa2SSatish Balay       jrow = ii[i];
9369ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
93717ab2063SBarry Smith       sum  = y[i];
9389ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
93997952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9409ea0dfa2SSatish Balay       }
94117ab2063SBarry Smith       z[i] = sum;
94217ab2063SBarry Smith     }
94397952fefSHong Zhang   }
94402ab625aSSatish Balay #endif
945efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
9461ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9471ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9482e8a6d31SBarry Smith   if (zz != yy) {
9491ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9502e8a6d31SBarry Smith   }
9513a40ed3dSBarry Smith   PetscFunctionReturn(0);
95217ab2063SBarry Smith }
95317ab2063SBarry Smith 
95417ab2063SBarry Smith /*
95517ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
95617ab2063SBarry Smith */
9574a2ae208SSatish Balay #undef __FUNCT__
9584a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
959dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
96017ab2063SBarry Smith {
961416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9626849ba73SBarry Smith   PetscErrorCode ierr;
96397f1f81fSBarry Smith   PetscInt       i,j,*diag,m = A->m;
96417ab2063SBarry Smith 
9653a40ed3dSBarry Smith   PetscFunctionBegin;
966f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
967f1e2ffcdSBarry Smith 
96897f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&diag);CHKERRQ(ierr);
96952e6d16bSBarry Smith   ierr = PetscLogObjectMemory(A,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
970273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
97135b0346bSBarry Smith     diag[i] = a->i[i+1];
972bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
973bfeeae90SHong Zhang       if (a->j[j] == i) {
974bfeeae90SHong Zhang         diag[i] = j;
97517ab2063SBarry Smith         break;
97617ab2063SBarry Smith       }
97717ab2063SBarry Smith     }
97817ab2063SBarry Smith   }
979416022c9SBarry Smith   a->diag = diag;
9803a40ed3dSBarry Smith   PetscFunctionReturn(0);
98117ab2063SBarry Smith }
98217ab2063SBarry Smith 
983be5855fcSBarry Smith /*
984be5855fcSBarry Smith      Checks for missing diagonals
985be5855fcSBarry Smith */
9864a2ae208SSatish Balay #undef __FUNCT__
9874a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
988dfbe8321SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A)
989be5855fcSBarry Smith {
990be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9916849ba73SBarry Smith   PetscErrorCode ierr;
99297f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
993be5855fcSBarry Smith 
994be5855fcSBarry Smith   PetscFunctionBegin;
995f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
996f1e2ffcdSBarry Smith   diag = a->diag;
997273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
998bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
99977431f27SBarry Smith       SETERRQ1(PETSC_ERR_PLIB,"Matrix is missing diagonal number %D",i);
1000be5855fcSBarry Smith     }
1001be5855fcSBarry Smith   }
1002be5855fcSBarry Smith   PetscFunctionReturn(0);
1003be5855fcSBarry Smith }
1004be5855fcSBarry Smith 
10054a2ae208SSatish Balay #undef __FUNCT__
10064a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
100797f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
100817ab2063SBarry Smith {
1009416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1010beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1011beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1012dfbe8321SBarry Smith   PetscErrorCode     ierr;
101397f1f81fSBarry Smith   PetscInt           n = A->n,m = A->m,i;
101497f1f81fSBarry Smith   const PetscInt     *idx,*diag;
101517ab2063SBarry Smith 
10163a40ed3dSBarry Smith   PetscFunctionBegin;
1017b965ef7fSBarry Smith   its = its*lits;
101877431f27SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
101991723122SBarry Smith 
1020ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1021ed480e8bSBarry Smith   diag = a->diag;
1022ed480e8bSBarry Smith   if (!a->idiag) {
1023ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1024ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1025ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1026ed480e8bSBarry Smith 
1027ed480e8bSBarry Smith     v        = a->a;
1028ed480e8bSBarry Smith 
1029ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1030958c9bccSBarry Smith     if (omega == 1.0 && !fshift) {
1031ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1032ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1033ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1034ed480e8bSBarry Smith       }
1035efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
1036ed480e8bSBarry Smith     } else {
1037ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1038ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1039beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1040ed480e8bSBarry Smith       }
1041efee365bSSatish Balay       ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
1042beeb8507SBarry Smith     }
1043ed480e8bSBarry Smith   }
1044ed480e8bSBarry Smith   t     = a->ssor;
1045ed480e8bSBarry Smith   idiag = a->idiag;
1046ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1047ed480e8bSBarry Smith 
10481ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1049fb2e594dSBarry Smith   if (xx != bb) {
10501ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1051fb2e594dSBarry Smith   } else {
1052fb2e594dSBarry Smith     b = x;
1053fb2e594dSBarry Smith   }
1054fb2e594dSBarry Smith 
1055ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1056ed480e8bSBarry Smith   xs   = x;
105717ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
105817ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1059ed480e8bSBarry Smith     bs = b;
106017ab2063SBarry Smith     for (i=0; i<m; i++) {
1061ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1062416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1063ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1064ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
106517ab2063SBarry Smith         sum  = b[i]*d/omega;
106617ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
106717ab2063SBarry Smith         x[i] = sum;
106817ab2063SBarry Smith     }
10691ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10701ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1071efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
10723a40ed3dSBarry Smith     PetscFunctionReturn(0);
107317ab2063SBarry Smith   }
1074c783ea89SBarry Smith 
1075ed480e8bSBarry Smith 
1076fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1077fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1078fc3d8934SBarry Smith 
1079fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1080fc3d8934SBarry Smith 
1081fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1082fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
108348af12d7SBarry Smith     is the relaxation factor.
1084fc3d8934SBarry Smith     */
1085fc3d8934SBarry Smith 
108648af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
108729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
10883a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
108917ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
109017ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
109117ab2063SBarry Smith 
109217ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
109317ab2063SBarry Smith 
109417ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
109517ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
109617ab2063SBarry Smith     is the relaxation factor.
109717ab2063SBarry Smith     */
109817ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
109917ab2063SBarry Smith 
110017ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
110117ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1102416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1103ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1104ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
110517ab2063SBarry Smith       sum  = b[i];
110617ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1107ed480e8bSBarry Smith       x[i] = sum*idiag[i];
110817ab2063SBarry Smith     }
110917ab2063SBarry Smith 
111017ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1111416022c9SBarry Smith     v = a->a;
1112ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
111317ab2063SBarry Smith 
111417ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1115ed480e8bSBarry Smith     ts = t;
1116416022c9SBarry Smith     diag = a->diag;
111717ab2063SBarry Smith     for (i=0; i<m; i++) {
1118416022c9SBarry Smith       n    = diag[i] - a->i[i];
1119ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1120ed480e8bSBarry Smith       v    = a->a + a->i[i];
112117ab2063SBarry Smith       sum  = t[i];
112217ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1123ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1124733d66baSBarry Smith       /*  x = x + t */
1125733d66baSBarry Smith       x[i] += t[i];
112617ab2063SBarry Smith     }
112717ab2063SBarry Smith 
1128efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
11291ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11301ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11313a40ed3dSBarry Smith     PetscFunctionReturn(0);
113217ab2063SBarry Smith   }
113317ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
113417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
113577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
113697f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
113777d8c4bbSBarry Smith #else
113817ab2063SBarry Smith       for (i=0; i<m; i++) {
1139416022c9SBarry Smith         n    = diag[i] - a->i[i];
1140ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1141ed480e8bSBarry Smith         v    = a->a + a->i[i];
114217ab2063SBarry Smith         sum  = b[i];
114317ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1144ed480e8bSBarry Smith         x[i] = sum*idiag[i];
114517ab2063SBarry Smith       }
114677d8c4bbSBarry Smith #endif
114717ab2063SBarry Smith       xb = x;
1148efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11493a40ed3dSBarry Smith     } else xb = b;
115017ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
115117ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
115217ab2063SBarry Smith       for (i=0; i<m; i++) {
1153ed480e8bSBarry Smith         x[i] *= mdiag[i];
115417ab2063SBarry Smith       }
1155efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
115617ab2063SBarry Smith     }
115717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
115877d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
115997f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
116077d8c4bbSBarry Smith #else
116117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1162416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1163ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1164ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
116517ab2063SBarry Smith         sum  = xb[i];
116617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1167ed480e8bSBarry Smith         x[i] = sum*idiag[i];
116817ab2063SBarry Smith       }
116977d8c4bbSBarry Smith #endif
1170efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
117117ab2063SBarry Smith     }
117217ab2063SBarry Smith     its--;
117317ab2063SBarry Smith   }
117417ab2063SBarry Smith   while (its--) {
117517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
117677d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
117797f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
117877d8c4bbSBarry Smith #else
117917ab2063SBarry Smith       for (i=0; i<m; i++) {
1180ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1181416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1182ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1183ed480e8bSBarry Smith         v    = a->a + a->i[i];
118417ab2063SBarry Smith         sum  = b[i];
118517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1186ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
118717ab2063SBarry Smith       }
118877d8c4bbSBarry Smith #endif
1189efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
119017ab2063SBarry Smith     }
119117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
119277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
119397f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
119477d8c4bbSBarry Smith #else
119517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1196ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1197416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1198ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1199ed480e8bSBarry Smith         v    = a->a + a->i[i];
120017ab2063SBarry Smith         sum  = b[i];
120117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1202ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
120317ab2063SBarry Smith       }
120477d8c4bbSBarry Smith #endif
1205efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
120617ab2063SBarry Smith     }
120717ab2063SBarry Smith   }
12081ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12091ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12103a40ed3dSBarry Smith   PetscFunctionReturn(0);
121117ab2063SBarry Smith }
121217ab2063SBarry Smith 
12134a2ae208SSatish Balay #undef __FUNCT__
12144a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1215dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
121617ab2063SBarry Smith {
1217416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12184e220ebcSLois Curfman McInnes 
12193a40ed3dSBarry Smith   PetscFunctionBegin;
1220273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1221273d9f13SBarry Smith   info->columns_global = (double)A->n;
1222273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1223273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12244e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12254e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12264e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12274e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12284e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12294e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12304e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12314e220ebcSLois Curfman McInnes   if (A->factor) {
12324e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12334e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12344e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12354e220ebcSLois Curfman McInnes   } else {
12364e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12374e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12384e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12394e220ebcSLois Curfman McInnes   }
12403a40ed3dSBarry Smith   PetscFunctionReturn(0);
124117ab2063SBarry Smith }
124217ab2063SBarry Smith 
12434a2ae208SSatish Balay #undef __FUNCT__
12444a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1245f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
124617ab2063SBarry Smith {
1247416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1248f4df32b1SMatthew Knepley   PetscInt       i,m = A->m - 1;
12496849ba73SBarry Smith   PetscErrorCode ierr;
125017ab2063SBarry Smith 
12513a40ed3dSBarry Smith   PetscFunctionBegin;
1252f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1253f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
125477431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1255bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1256f1e2ffcdSBarry Smith     }
1257f4df32b1SMatthew Knepley     if (diag != 0.0) {
1258f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1259f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1260f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1261f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1262f1e2ffcdSBarry Smith       }
1263f1e2ffcdSBarry Smith     }
126488e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1265f1e2ffcdSBarry Smith   } else {
1266f4df32b1SMatthew Knepley     if (diag != 0.0) {
126717ab2063SBarry Smith       for (i=0; i<N; i++) {
126877431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
12697ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1270416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1271f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1272bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
12737ae801bdSBarry Smith         } else { /* in case row was completely empty */
1274f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
127517ab2063SBarry Smith         }
127617ab2063SBarry Smith       }
12773a40ed3dSBarry Smith     } else {
127817ab2063SBarry Smith       for (i=0; i<N; i++) {
127977431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1280416022c9SBarry Smith         a->ilen[rows[i]] = 0;
128117ab2063SBarry Smith       }
128217ab2063SBarry Smith     }
128388e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1284f1e2ffcdSBarry Smith   }
128543a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12863a40ed3dSBarry Smith   PetscFunctionReturn(0);
128717ab2063SBarry Smith }
128817ab2063SBarry Smith 
12894a2ae208SSatish Balay #undef __FUNCT__
12904a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
129197f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
129217ab2063SBarry Smith {
1293416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
129497f1f81fSBarry Smith   PetscInt   *itmp;
129517ab2063SBarry Smith 
12963a40ed3dSBarry Smith   PetscFunctionBegin;
129777431f27SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
129817ab2063SBarry Smith 
1299416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1300bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
130117ab2063SBarry Smith   if (idx) {
1302bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1303bfeeae90SHong Zhang     if (*nz) {
13044e093b46SBarry Smith       *idx = itmp;
130517ab2063SBarry Smith     }
130617ab2063SBarry Smith     else *idx = 0;
130717ab2063SBarry Smith   }
13083a40ed3dSBarry Smith   PetscFunctionReturn(0);
130917ab2063SBarry Smith }
131017ab2063SBarry Smith 
1311bfeeae90SHong Zhang /* remove this function? */
13124a2ae208SSatish Balay #undef __FUNCT__
13134a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
131497f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
131517ab2063SBarry Smith {
13163a40ed3dSBarry Smith   PetscFunctionBegin;
13173a40ed3dSBarry Smith   PetscFunctionReturn(0);
131817ab2063SBarry Smith }
131917ab2063SBarry Smith 
13204a2ae208SSatish Balay #undef __FUNCT__
13214a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1322dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
132317ab2063SBarry Smith {
1324416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
132587828ca2SBarry Smith   PetscScalar    *v = a->a;
132636db0b34SBarry Smith   PetscReal      sum = 0.0;
13276849ba73SBarry Smith   PetscErrorCode ierr;
132897f1f81fSBarry Smith   PetscInt       i,j;
132917ab2063SBarry Smith 
13303a40ed3dSBarry Smith   PetscFunctionBegin;
133117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1332416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1333aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
133436db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
133517ab2063SBarry Smith #else
133617ab2063SBarry Smith       sum += (*v)*(*v); v++;
133717ab2063SBarry Smith #endif
133817ab2063SBarry Smith     }
1339064f8208SBarry Smith     *nrm = sqrt(sum);
13403a40ed3dSBarry Smith   } else if (type == NORM_1) {
134136db0b34SBarry Smith     PetscReal *tmp;
134297f1f81fSBarry Smith     PetscInt    *jj = a->j;
1343b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1344273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1345064f8208SBarry Smith     *nrm = 0.0;
1346416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1347bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
134817ab2063SBarry Smith     }
1349273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1350064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
135117ab2063SBarry Smith     }
1352606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13533a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1354064f8208SBarry Smith     *nrm = 0.0;
1355273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1356bfeeae90SHong Zhang       v = a->a + a->i[j];
135717ab2063SBarry Smith       sum = 0.0;
1358416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1359cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
136017ab2063SBarry Smith       }
1361064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
136217ab2063SBarry Smith     }
13633a40ed3dSBarry Smith   } else {
136429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
136517ab2063SBarry Smith   }
13663a40ed3dSBarry Smith   PetscFunctionReturn(0);
136717ab2063SBarry Smith }
136817ab2063SBarry Smith 
13694a2ae208SSatish Balay #undef __FUNCT__
13704a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1371dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B)
137217ab2063SBarry Smith {
1373416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1374416022c9SBarry Smith   Mat            C;
13756849ba73SBarry Smith   PetscErrorCode ierr;
137697f1f81fSBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->m,len,*col;
137787828ca2SBarry Smith   PetscScalar    *array = a->a;
137817ab2063SBarry Smith 
13793a40ed3dSBarry Smith   PetscFunctionBegin;
1380273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
138197f1f81fSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
138297f1f81fSBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(PetscInt));CHKERRQ(ierr);
1383bfeeae90SHong Zhang 
1384bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1385f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1386f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->n,m,A->n,m);CHKERRQ(ierr);
1387f204ca49SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1388ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1389606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
139017ab2063SBarry Smith   for (i=0; i<m; i++) {
139117ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1392416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1393b9b97703SBarry Smith     array += len;
1394b9b97703SBarry Smith     aj    += len;
139517ab2063SBarry Smith   }
139617ab2063SBarry Smith 
13976d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13986d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
139917ab2063SBarry Smith 
1400f1e2ffcdSBarry Smith   if (B) {
1401416022c9SBarry Smith     *B = C;
140217ab2063SBarry Smith   } else {
1403273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
140417ab2063SBarry Smith   }
14053a40ed3dSBarry Smith   PetscFunctionReturn(0);
140617ab2063SBarry Smith }
140717ab2063SBarry Smith 
1408cd0d46ebSvictorle EXTERN_C_BEGIN
1409cd0d46ebSvictorle #undef __FUNCT__
14105fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1411be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1412cd0d46ebSvictorle {
1413cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
141497f1f81fSBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
14156849ba73SBarry Smith   PetscErrorCode ierr;
141697f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1417cd0d46ebSvictorle 
1418cd0d46ebSvictorle   PetscFunctionBegin;
1419cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1420cd0d46ebSvictorle 
1421cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1422cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
14235485867bSBarry Smith   if (ma!=nb || na!=mb){
14245485867bSBarry Smith     *f = PETSC_FALSE;
14255485867bSBarry Smith     PetscFunctionReturn(0);
14265485867bSBarry Smith   }
1427cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1428cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1429cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
143097f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
143197f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1432cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1433cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1434cd0d46ebSvictorle 
1435cd0d46ebSvictorle   *f = PETSC_TRUE;
1436cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1437cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
143897f1f81fSBarry Smith       PetscInt         idc,idr;
14395485867bSBarry Smith       PetscScalar vc,vr;
1440cd0d46ebSvictorle       /* column/row index/value */
14415485867bSBarry Smith       idc = adx[aptr[i]];
14425485867bSBarry Smith       idr = bdx[bptr[idc]];
14435485867bSBarry Smith       vc  = va[aptr[i]];
14445485867bSBarry Smith       vr  = vb[bptr[idc]];
14455485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
14465485867bSBarry Smith 	*f = PETSC_FALSE;
14475485867bSBarry Smith         goto done;
1448cd0d46ebSvictorle       } else {
14495485867bSBarry Smith 	aptr[i]++;
14505485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1451cd0d46ebSvictorle       }
1452cd0d46ebSvictorle     }
1453cd0d46ebSvictorle   }
1454cd0d46ebSvictorle  done:
1455cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
14563aeef889SHong Zhang   if (B) {
14573aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
14583aeef889SHong Zhang   }
1459cd0d46ebSvictorle   PetscFunctionReturn(0);
1460cd0d46ebSvictorle }
1461cd0d46ebSvictorle EXTERN_C_END
1462cd0d46ebSvictorle 
14639e29f15eSvictorle #undef __FUNCT__
14649e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1465dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
14669e29f15eSvictorle {
1467dfbe8321SBarry Smith   PetscErrorCode ierr;
14689e29f15eSvictorle   PetscFunctionBegin;
14695485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
14709e29f15eSvictorle   PetscFunctionReturn(0);
14719e29f15eSvictorle }
14729e29f15eSvictorle 
14734a2ae208SSatish Balay #undef __FUNCT__
14744a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1475dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
147617ab2063SBarry Smith {
1477416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
147887828ca2SBarry Smith   PetscScalar    *l,*r,x,*v;
1479dfbe8321SBarry Smith   PetscErrorCode ierr;
148097f1f81fSBarry Smith   PetscInt       i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
148117ab2063SBarry Smith 
14823a40ed3dSBarry Smith   PetscFunctionBegin;
148317ab2063SBarry Smith   if (ll) {
14843ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14853ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1486e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1487273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
14881ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1489416022c9SBarry Smith     v = a->a;
149017ab2063SBarry Smith     for (i=0; i<m; i++) {
149117ab2063SBarry Smith       x = l[i];
1492416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
149317ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
149417ab2063SBarry Smith     }
14951ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1496efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
149717ab2063SBarry Smith   }
149817ab2063SBarry Smith   if (rr) {
1499e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1500273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
15011ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1502416022c9SBarry Smith     v = a->a; jj = a->j;
150317ab2063SBarry Smith     for (i=0; i<nz; i++) {
1504bfeeae90SHong Zhang       (*v++) *= r[*jj++];
150517ab2063SBarry Smith     }
15061ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1507efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
150817ab2063SBarry Smith   }
15093a40ed3dSBarry Smith   PetscFunctionReturn(0);
151017ab2063SBarry Smith }
151117ab2063SBarry Smith 
15124a2ae208SSatish Balay #undef __FUNCT__
15134a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
151497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
151517ab2063SBarry Smith {
1516db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
15176849ba73SBarry Smith   PetscErrorCode ierr;
151897f1f81fSBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->n,*lens;
151997f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
152097f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
152197f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
152287828ca2SBarry Smith   PetscScalar    *a_new,*mat_a;
1523416022c9SBarry Smith   Mat            C;
1524fee21e36SBarry Smith   PetscTruth     stride;
152517ab2063SBarry Smith 
15263a40ed3dSBarry Smith   PetscFunctionBegin;
1527d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
152829bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1529d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
153029bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
153199141d43SSatish Balay 
153217ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1533b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1534b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
153517ab2063SBarry Smith 
1536fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1537fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1538fee21e36SBarry Smith   if (stride && step == 1) {
153902834360SBarry Smith     /* special case of contiguous rows */
154097f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
154131ebf83bSSatish Balay     starts = lens + nrows;
154202834360SBarry Smith     /* loop over new rows determining lens and starting points */
154302834360SBarry Smith     for (i=0; i<nrows; i++) {
1544bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1545a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
154602834360SBarry Smith       for (k=kstart; k<kend; k++) {
1547bfeeae90SHong Zhang         if (aj[k] >= first) {
154802834360SBarry Smith           starts[i] = k;
154902834360SBarry Smith           break;
155002834360SBarry Smith 	}
155102834360SBarry Smith       }
1552a2744918SBarry Smith       sum = 0;
155302834360SBarry Smith       while (k < kend) {
1554bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1555a2744918SBarry Smith         sum++;
155602834360SBarry Smith       }
1557a2744918SBarry Smith       lens[i] = sum;
155802834360SBarry Smith     }
155902834360SBarry Smith     /* create submatrix */
1560cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
156197f1f81fSBarry Smith       PetscInt n_cols,n_rows;
156208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
156329bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1564d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
156508480c60SBarry Smith       C = *B;
15663a40ed3dSBarry Smith     } else {
1567f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1568f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1569e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1570ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
157108480c60SBarry Smith     }
1572db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1573db02288aSLois Curfman McInnes 
157402834360SBarry Smith     /* loop over rows inserting into submatrix */
1575db02288aSLois Curfman McInnes     a_new    = c->a;
1576db02288aSLois Curfman McInnes     j_new    = c->j;
1577db02288aSLois Curfman McInnes     i_new    = c->i;
1578bfeeae90SHong Zhang 
157902834360SBarry Smith     for (i=0; i<nrows; i++) {
1580a2744918SBarry Smith       ii    = starts[i];
1581a2744918SBarry Smith       lensi = lens[i];
1582a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1583a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
158402834360SBarry Smith       }
158587828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1586a2744918SBarry Smith       a_new      += lensi;
1587a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1588a2744918SBarry Smith       c->ilen[i]  = lensi;
158902834360SBarry Smith     }
1590606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15913a40ed3dSBarry Smith   } else {
159202834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
159397f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1594bfeeae90SHong Zhang 
159597f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
159697f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
159717ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
159802834360SBarry Smith     /* determine lens of each row */
159902834360SBarry Smith     for (i=0; i<nrows; i++) {
1600bfeeae90SHong Zhang       kstart  = ai[irow[i]];
160102834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
160202834360SBarry Smith       lens[i] = 0;
160302834360SBarry Smith       for (k=kstart; k<kend; k++) {
1604bfeeae90SHong Zhang         if (smap[aj[k]]) {
160502834360SBarry Smith           lens[i]++;
160602834360SBarry Smith         }
160702834360SBarry Smith       }
160802834360SBarry Smith     }
160917ab2063SBarry Smith     /* Create and fill new matrix */
1610a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16110f5bd95cSBarry Smith       PetscTruth equal;
16120f5bd95cSBarry Smith 
161399141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1614273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
161597f1f81fSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(PetscInt),&equal);CHKERRQ(ierr);
16160f5bd95cSBarry Smith       if (!equal) {
161729bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
161899141d43SSatish Balay       }
161997f1f81fSBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(PetscInt));CHKERRQ(ierr);
162008480c60SBarry Smith       C = *B;
16213a40ed3dSBarry Smith     } else {
1622f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1623f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1624e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1625ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
162608480c60SBarry Smith     }
162799141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
162817ab2063SBarry Smith     for (i=0; i<nrows; i++) {
162999141d43SSatish Balay       row    = irow[i];
1630bfeeae90SHong Zhang       kstart = ai[row];
163199141d43SSatish Balay       kend   = kstart + a->ilen[row];
1632bfeeae90SHong Zhang       mat_i  = c->i[i];
163399141d43SSatish Balay       mat_j  = c->j + mat_i;
163499141d43SSatish Balay       mat_a  = c->a + mat_i;
163599141d43SSatish Balay       mat_ilen = c->ilen + i;
163617ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1637bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1638ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
163999141d43SSatish Balay           *mat_a++ = a->a[k];
164099141d43SSatish Balay           (*mat_ilen)++;
164199141d43SSatish Balay 
164217ab2063SBarry Smith         }
164317ab2063SBarry Smith       }
164417ab2063SBarry Smith     }
164502834360SBarry Smith     /* Free work space */
164602834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1647606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1648606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
164902834360SBarry Smith   }
16506d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16516d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
165217ab2063SBarry Smith 
165317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1654416022c9SBarry Smith   *B = C;
16553a40ed3dSBarry Smith   PetscFunctionReturn(0);
165617ab2063SBarry Smith }
165717ab2063SBarry Smith 
1658a871dcd8SBarry Smith /*
1659a871dcd8SBarry Smith */
16604a2ae208SSatish Balay #undef __FUNCT__
16614a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1662dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1663a871dcd8SBarry Smith {
166463b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1665dfbe8321SBarry Smith   PetscErrorCode ierr;
166663b91edcSBarry Smith   Mat            outA;
1667b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
166863b91edcSBarry Smith 
16693a40ed3dSBarry Smith   PetscFunctionBegin;
1670d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1671b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1672b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1673b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1674634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
1675b8a78c4aSBarry Smith   }
1676a871dcd8SBarry Smith 
167763b91edcSBarry Smith   outA          = inA;
167863b91edcSBarry Smith   inA->factor   = FACTOR_LU;
167963b91edcSBarry Smith   a->row        = row;
168063b91edcSBarry Smith   a->col        = col;
1681c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1682c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
168363b91edcSBarry Smith 
168436db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1685b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16864c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
168752e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1688f0ec6fceSSatish Balay 
168994a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
169087828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
169194a9d846SBarry Smith   }
169263b91edcSBarry Smith 
169308480c60SBarry Smith   if (!a->diag) {
1694f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
169563b91edcSBarry Smith   }
1696af281ebdSHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
16973a40ed3dSBarry Smith   PetscFunctionReturn(0);
1698a871dcd8SBarry Smith }
1699a871dcd8SBarry Smith 
1700d9eff348SSatish Balay #include "petscblaslapack.h"
17014a2ae208SSatish Balay #undef __FUNCT__
17024a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1703f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1704f0b747eeSBarry Smith {
1705f0b747eeSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)inA->data;
17064ce68768SBarry Smith   PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1;
1707f4df32b1SMatthew Knepley   PetscScalar oalpha = alpha;
1708efee365bSSatish Balay   PetscErrorCode ierr;
1709efee365bSSatish Balay 
17103a40ed3dSBarry Smith 
17113a40ed3dSBarry Smith   PetscFunctionBegin;
1712f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1713efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17143a40ed3dSBarry Smith   PetscFunctionReturn(0);
1715f0b747eeSBarry Smith }
1716f0b747eeSBarry Smith 
17174a2ae208SSatish Balay #undef __FUNCT__
17184a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
171997f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1720cddf8d76SBarry Smith {
1721dfbe8321SBarry Smith   PetscErrorCode ierr;
172297f1f81fSBarry Smith   PetscInt       i;
1723cddf8d76SBarry Smith 
17243a40ed3dSBarry Smith   PetscFunctionBegin;
1725cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1726b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1727cddf8d76SBarry Smith   }
1728cddf8d76SBarry Smith 
1729cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17306a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1731cddf8d76SBarry Smith   }
17323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1733cddf8d76SBarry Smith }
1734cddf8d76SBarry Smith 
17354a2ae208SSatish Balay #undef __FUNCT__
17364a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
173797f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
17384dcbc457SBarry Smith {
1739e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17406849ba73SBarry Smith   PetscErrorCode ierr;
174197f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
174297f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1743f1af5d2fSBarry Smith   PetscBT        table;
1744bbd702dbSSatish Balay 
17453a40ed3dSBarry Smith   PetscFunctionBegin;
1746273d9f13SBarry Smith   m     = A->m;
1747e4d965acSSatish Balay   ai    = a->i;
1748bfeeae90SHong Zhang   aj    = a->j;
17498a047759SSatish Balay 
1750a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
175106763907SSatish Balay 
175297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
17536831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
175406763907SSatish Balay 
1755e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1756b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1757e4d965acSSatish Balay     isz  = 0;
17586831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1759e4d965acSSatish Balay 
1760e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17614dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1762b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1763e4d965acSSatish Balay 
1764dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1765e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1766f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17674dcbc457SBarry Smith     }
176806763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
176906763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1770e4d965acSSatish Balay 
177104a348a9SBarry Smith     k = 0;
177204a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
177304a348a9SBarry Smith       n = isz;
177406763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1775e4d965acSSatish Balay         row   = nidx[k];
1776e4d965acSSatish Balay         start = ai[row];
1777e4d965acSSatish Balay         end   = ai[row+1];
177804a348a9SBarry Smith         for (l = start; l<end ; l++){
1779efb16452SHong Zhang           val = aj[l] ;
1780f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1781e4d965acSSatish Balay         }
1782e4d965acSSatish Balay       }
1783e4d965acSSatish Balay     }
1784029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1785e4d965acSSatish Balay   }
17866831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1787606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17883a40ed3dSBarry Smith   PetscFunctionReturn(0);
17894dcbc457SBarry Smith }
179017ab2063SBarry Smith 
17910513a670SBarry Smith /* -------------------------------------------------------------- */
17924a2ae208SSatish Balay #undef __FUNCT__
17934a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1794dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17950513a670SBarry Smith {
17960513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17976849ba73SBarry Smith   PetscErrorCode ierr;
179897f1f81fSBarry Smith   PetscInt       i,nz,m = A->m,n = A->n,*col;
179997f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
180056cd22aeSBarry Smith   IS             icolp,irowp;
180197f1f81fSBarry Smith   PetscInt       *cwork;
180232ec9ce4SBarry Smith   PetscScalar    *vwork;
18030513a670SBarry Smith 
18043a40ed3dSBarry Smith   PetscFunctionBegin;
18054c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
180656cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18074c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
180856cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18090513a670SBarry Smith 
18100513a670SBarry Smith   /* determine lengths of permuted rows */
181197f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
18120513a670SBarry Smith   for (i=0; i<m; i++) {
18130513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18140513a670SBarry Smith   }
1815f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,B);CHKERRQ(ierr);
1816f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
1817f204ca49SKris Buschelman   ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr);
1818ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1819606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18200513a670SBarry Smith 
182197f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
18220513a670SBarry Smith   for (i=0; i<m; i++) {
182332ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18240513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1825cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
182632ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18270513a670SBarry Smith   }
1828606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18293c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
18300513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18310513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
183256cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
183356cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
183456cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
183556cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18363a40ed3dSBarry Smith   PetscFunctionReturn(0);
18370513a670SBarry Smith }
18380513a670SBarry Smith 
18394a2ae208SSatish Balay #undef __FUNCT__
18404a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1841dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_SeqAIJ(Mat A)
1842682d7d0cSBarry Smith {
1843c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1844682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1845dfbe8321SBarry Smith   PetscErrorCode    ierr;
1846682d7d0cSBarry Smith 
18473a40ed3dSBarry Smith   PetscFunctionBegin;
18484846f1f5SKris Buschelman   ierr = MatPrintHelp_Inode(A);CHKERRQ(ierr);
1849c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1850d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1851d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1852d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
185373e7a558SHong Zhang   ierr = (*PetscHelpPrintf)(comm,"  -mat_no_compressedrow: Do not use compressedrow\n");CHKERRQ(ierr);
18543a40ed3dSBarry Smith   PetscFunctionReturn(0);
1855682d7d0cSBarry Smith }
185697304618SKris Buschelman 
18574a2ae208SSatish Balay #undef __FUNCT__
18584a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1859dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1860cb5b572fSBarry Smith {
1861dfbe8321SBarry Smith   PetscErrorCode ierr;
1862cb5b572fSBarry Smith 
1863cb5b572fSBarry Smith   PetscFunctionBegin;
186433f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
186533f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1866be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1867be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1868be6bf707SBarry Smith 
1869bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
1870634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1871cb5b572fSBarry Smith     }
1872bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1873cb5b572fSBarry Smith   } else {
1874cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1875cb5b572fSBarry Smith   }
1876cb5b572fSBarry Smith   PetscFunctionReturn(0);
1877cb5b572fSBarry Smith }
1878cb5b572fSBarry Smith 
18794a2ae208SSatish Balay #undef __FUNCT__
18804a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1881dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
1882273d9f13SBarry Smith {
1883dfbe8321SBarry Smith   PetscErrorCode ierr;
1884273d9f13SBarry Smith 
1885273d9f13SBarry Smith   PetscFunctionBegin;
1886ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1887273d9f13SBarry Smith   PetscFunctionReturn(0);
1888273d9f13SBarry Smith }
1889273d9f13SBarry Smith 
18904a2ae208SSatish Balay #undef __FUNCT__
18914a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
1892dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
18936c0721eeSBarry Smith {
18946c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18956c0721eeSBarry Smith   PetscFunctionBegin;
18966c0721eeSBarry Smith   *array = a->a;
18976c0721eeSBarry Smith   PetscFunctionReturn(0);
18986c0721eeSBarry Smith }
18996c0721eeSBarry Smith 
19004a2ae208SSatish Balay #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
1902dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19036c0721eeSBarry Smith {
19046c0721eeSBarry Smith   PetscFunctionBegin;
19056c0721eeSBarry Smith   PetscFunctionReturn(0);
19066c0721eeSBarry Smith }
1907273d9f13SBarry Smith 
1908ee4f033dSBarry Smith #undef __FUNCT__
1909ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1910dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1911ee4f033dSBarry Smith {
19126849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
19136849ba73SBarry Smith   PetscErrorCode ierr;
191497f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
1915*efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
191687828ca2SBarry Smith   PetscScalar    *vscale_array;
1917ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
1918ee4f033dSBarry Smith   Vec            w1,w2,w3;
1919ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
1920ee4f033dSBarry Smith   PetscTruth     flg;
1921ee4f033dSBarry Smith 
1922ee4f033dSBarry Smith   PetscFunctionBegin;
1923ee4f033dSBarry Smith   if (!coloring->w1) {
1924ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
192552e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
1926ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
192752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
1928ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
192952e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
1930ee4f033dSBarry Smith   }
1931ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1932ee4f033dSBarry Smith 
1933ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1934e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1935ee4f033dSBarry Smith   if (flg) {
193663ba0a88SBarry Smith     ierr = PetscLogInfo((coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"));CHKERRQ(ierr);
1937ee4f033dSBarry Smith   } else {
19380b9b6f31SBarry Smith     PetscTruth assembled;
19390b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
19400b9b6f31SBarry Smith     if (assembled) {
1941ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
1942ee4f033dSBarry Smith     }
19430b9b6f31SBarry Smith   }
1944ee4f033dSBarry Smith 
1945ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1946ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1947ee4f033dSBarry Smith 
1948ee4f033dSBarry Smith   /*
1949ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1950ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1951ee4f033dSBarry Smith   */
1952ee4f033dSBarry Smith   if (coloring->F) {
1953ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1954ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1955ee4f033dSBarry Smith     if (m1 != m2) {
1956ee4f033dSBarry Smith       coloring->F = 0;
1957ee4f033dSBarry Smith     }
1958ee4f033dSBarry Smith   }
1959ee4f033dSBarry Smith 
1960ee4f033dSBarry Smith   if (coloring->F) {
1961ee4f033dSBarry Smith     w1          = coloring->F;
1962ee4f033dSBarry Smith     coloring->F = 0;
1963ee4f033dSBarry Smith   } else {
196466f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1965ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
196666f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1967ee4f033dSBarry Smith   }
1968ee4f033dSBarry Smith 
1969ee4f033dSBarry Smith   /*
1970ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1971ee4f033dSBarry Smith   */
19721ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
19731ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1974ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1975ee4f033dSBarry Smith     /*
1976ee4f033dSBarry Smith        Loop over each column associated with color adding the
1977ee4f033dSBarry Smith        perturbation to the vector w3.
1978ee4f033dSBarry Smith     */
1979ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1980ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1981ee4f033dSBarry Smith       dx  = xx[col];
1982ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1983ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1984ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1985ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1986ee4f033dSBarry Smith #else
1987ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1988ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1989ee4f033dSBarry Smith #endif
1990ee4f033dSBarry Smith       dx                *= epsilon;
1991ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1992ee4f033dSBarry Smith     }
1993ee4f033dSBarry Smith   }
19941ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1995ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1996ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1997ee4f033dSBarry Smith 
1998ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
1999ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2000ee4f033dSBarry Smith 
2001ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2002ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2003ee4f033dSBarry Smith 
20041ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2005ee4f033dSBarry Smith   /*
2006ee4f033dSBarry Smith       Loop over each color
2007ee4f033dSBarry Smith   */
2008ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
200949b058dcSBarry Smith     coloring->currentcolor = k;
2010ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
20111ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2012ee4f033dSBarry Smith     /*
2013ee4f033dSBarry Smith        Loop over each column associated with color adding the
2014ee4f033dSBarry Smith        perturbation to the vector w3.
2015ee4f033dSBarry Smith     */
2016ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2017ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2018ee4f033dSBarry Smith       dx  = xx[col];
20195b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2020ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2021ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2022ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2023ee4f033dSBarry Smith #else
2024ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2025ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2026ee4f033dSBarry Smith #endif
2027ee4f033dSBarry Smith       dx            *= epsilon;
2028634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2029ee4f033dSBarry Smith       w3_array[col] += dx;
2030ee4f033dSBarry Smith     }
20311ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2032ee4f033dSBarry Smith 
2033ee4f033dSBarry Smith     /*
2034ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2035ee4f033dSBarry Smith     */
2036ee4f033dSBarry Smith 
203766f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2038ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
203966f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2040*efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2041ee4f033dSBarry Smith 
2042ee4f033dSBarry Smith     /*
2043ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2044ee4f033dSBarry Smith     */
20451ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2046ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2047ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2048ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2049ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2050ee4f033dSBarry Smith       srow   = row + start;
2051ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2052ee4f033dSBarry Smith     }
20531ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2054ee4f033dSBarry Smith   }
205549b058dcSBarry Smith   coloring->currentcolor = k;
20561ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
20571ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2058ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2059ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2060ee4f033dSBarry Smith   PetscFunctionReturn(0);
2061ee4f033dSBarry Smith }
2062ee4f033dSBarry Smith 
2063ac90fabeSBarry Smith #include "petscblaslapack.h"
2064ac90fabeSBarry Smith #undef __FUNCT__
2065ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2066f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2067ac90fabeSBarry Smith {
2068dfbe8321SBarry Smith   PetscErrorCode ierr;
206997f1f81fSBarry Smith   PetscInt       i;
2070ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
20714ce68768SBarry Smith   PetscBLASInt   one=1,bnz = (PetscBLASInt)x->nz;
2072ac90fabeSBarry Smith 
2073ac90fabeSBarry Smith   PetscFunctionBegin;
2074ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2075f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2076f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2077c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2078a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2079a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2080a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2081a30b2313SHong Zhang     }
2082a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
208324f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2084a30b2313SHong Zhang       y->XtoY = X;
2085c537a176SHong Zhang     }
2086f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
208763ba0a88SBarry Smith     ierr = PetscLogInfo((0,"MatAXPY_SeqAIJ: ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz));CHKERRQ(ierr);
2088ac90fabeSBarry Smith   } else {
2089f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2090ac90fabeSBarry Smith   }
2091ac90fabeSBarry Smith   PetscFunctionReturn(0);
2092ac90fabeSBarry Smith }
2093ac90fabeSBarry Smith 
2094521d7252SBarry Smith #undef __FUNCT__
2095521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2096521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2097521d7252SBarry Smith {
2098521d7252SBarry Smith   PetscFunctionBegin;
2099521d7252SBarry Smith   PetscFunctionReturn(0);
2100521d7252SBarry Smith }
2101521d7252SBarry Smith 
2102354c94deSBarry Smith #undef __FUNCT__
2103354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2104354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2105354c94deSBarry Smith {
2106354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2107354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2108354c94deSBarry Smith   PetscInt    i,nz;
2109354c94deSBarry Smith   PetscScalar *a;
2110354c94deSBarry Smith 
2111354c94deSBarry Smith   PetscFunctionBegin;
2112354c94deSBarry Smith   nz = aij->nz;
2113354c94deSBarry Smith   a  = aij->a;
2114354c94deSBarry Smith   for (i=0; i<nz; i++) {
2115354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2116354c94deSBarry Smith   }
2117354c94deSBarry Smith #else
2118354c94deSBarry Smith   PetscFunctionBegin;
2119354c94deSBarry Smith #endif
2120354c94deSBarry Smith   PetscFunctionReturn(0);
2121354c94deSBarry Smith }
2122354c94deSBarry Smith 
2123e34fafa9SBarry Smith #undef __FUNCT__
2124e34fafa9SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2125e34fafa9SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v)
2126e34fafa9SBarry Smith {
2127e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2128e34fafa9SBarry Smith   PetscErrorCode ierr;
2129e34fafa9SBarry Smith   PetscInt       i,j,m = A->m,*ai,*aj,ncols,n;
2130e34fafa9SBarry Smith   PetscReal      atmp;
2131e34fafa9SBarry Smith   PetscScalar    *x,zero = 0.0;
2132e34fafa9SBarry Smith   MatScalar      *aa;
2133e34fafa9SBarry Smith 
2134e34fafa9SBarry Smith   PetscFunctionBegin;
2135e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2136e34fafa9SBarry Smith   aa   = a->a;
2137e34fafa9SBarry Smith   ai   = a->i;
2138e34fafa9SBarry Smith   aj   = a->j;
2139e34fafa9SBarry Smith 
214043e18e04SHong Zhang   ierr = VecSet(v,zero);CHKERRQ(ierr);
2141e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2142e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2143e34fafa9SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2144e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2145e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2146e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2147e34fafa9SBarry Smith       atmp = PetscAbsScalar(*aa); aa++;
2148e34fafa9SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) x[i] = atmp;
2149e34fafa9SBarry Smith       aj++;
2150e34fafa9SBarry Smith     }
2151e34fafa9SBarry Smith   }
2152e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2153e34fafa9SBarry Smith   PetscFunctionReturn(0);
2154e34fafa9SBarry Smith }
2155e34fafa9SBarry Smith 
2156682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
21570a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2158cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2159cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2160cb5b572fSBarry Smith        MatMult_SeqAIJ,
216197304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
21627c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
21637c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2164cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2165cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
21667c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
216797304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2168cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2169cb5b572fSBarry Smith        0,
217017ab2063SBarry Smith        MatRelax_SeqAIJ,
217117ab2063SBarry Smith        MatTranspose_SeqAIJ,
217297304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2173cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2174cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2175cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2176cb5b572fSBarry Smith        MatNorm_SeqAIJ,
217797304618SKris Buschelman /*20*/ 0,
2178cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
217917ab2063SBarry Smith        MatCompress_SeqAIJ,
2180cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2181cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
218297304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2183cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2184cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2185f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2186a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
218797304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2188cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2189861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
21906c0721eeSBarry Smith        MatGetArray_SeqAIJ,
21916c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
219297304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2193cb5b572fSBarry Smith        0,
2194cb5b572fSBarry Smith        0,
2195cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2196cb5b572fSBarry Smith        0,
219797304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2198cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2199cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2200cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2201cb5b572fSBarry Smith        MatCopy_SeqAIJ,
220297304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ,
2203cb5b572fSBarry Smith        MatScale_SeqAIJ,
2204cb5b572fSBarry Smith        0,
2205cb5b572fSBarry Smith        0,
22066945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2207521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
22083b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
22093b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
22103b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2211a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
221297304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2213b9617806SBarry Smith        0,
22140513a670SBarry Smith        0,
2215cda55fadSBarry Smith        MatPermute_SeqAIJ,
2216cda55fadSBarry Smith        0,
221797304618SKris Buschelman /*60*/ 0,
2218b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2219b9b97703SBarry Smith        MatView_SeqAIJ,
22208a124369SBarry Smith        MatGetPetscMaps_Petsc,
2221ee4f033dSBarry Smith        0,
222297304618SKris Buschelman /*65*/ 0,
2223ee4f033dSBarry Smith        0,
2224ee4f033dSBarry Smith        0,
2225ee4f033dSBarry Smith        0,
2226ee4f033dSBarry Smith        0,
2227e34fafa9SBarry Smith /*70*/ MatGetRowMax_SeqAIJ,
2228ee4f033dSBarry Smith        0,
2229ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2230dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2231ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2232dcf5cc72SBarry Smith #else
2233dcf5cc72SBarry Smith        0,
2234dcf5cc72SBarry Smith #endif
2235ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
223697304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ,
223797304618SKris Buschelman        0,
223897304618SKris Buschelman        0,
223997304618SKris Buschelman        0,
224097304618SKris Buschelman        0,
224197304618SKris Buschelman /*80*/ 0,
224297304618SKris Buschelman        0,
224397304618SKris Buschelman        0,
224497304618SKris Buschelman        0,
2245bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2246bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
22476284ec50SHong Zhang        0,
22486284ec50SHong Zhang        0,
22496284ec50SHong Zhang        0,
2250bc011b1eSHong Zhang        0,
2251bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
225226be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
225326be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2254d439da42SKris Buschelman        MatPtAP_Basic,
22557ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
22567ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2257bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2258bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2259bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
22607ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
22617ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2262609c6c4dSKris Buschelman        0,
2263609c6c4dSKris Buschelman        0,
2264354c94deSBarry Smith        MatConjugate_SeqAIJ
22659e29f15eSvictorle };
226617ab2063SBarry Smith 
2267fb2e594dSBarry Smith EXTERN_C_BEGIN
22684a2ae208SSatish Balay #undef __FUNCT__
22694a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2270be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2271bef8e0ddSBarry Smith {
2272bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
227397f1f81fSBarry Smith   PetscInt   i,nz,n;
2274bef8e0ddSBarry Smith 
2275bef8e0ddSBarry Smith   PetscFunctionBegin;
2276bef8e0ddSBarry Smith 
2277bef8e0ddSBarry Smith   nz = aij->maxnz;
2278273d9f13SBarry Smith   n  = mat->n;
2279bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2280bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2281bef8e0ddSBarry Smith   }
2282bef8e0ddSBarry Smith   aij->nz = nz;
2283bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2284bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2285bef8e0ddSBarry Smith   }
2286bef8e0ddSBarry Smith 
2287bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2288bef8e0ddSBarry Smith }
2289fb2e594dSBarry Smith EXTERN_C_END
2290bef8e0ddSBarry Smith 
22914a2ae208SSatish Balay #undef __FUNCT__
22924a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2293bef8e0ddSBarry Smith /*@
2294bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2295bef8e0ddSBarry Smith        in the matrix.
2296bef8e0ddSBarry Smith 
2297bef8e0ddSBarry Smith   Input Parameters:
2298bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2299bef8e0ddSBarry Smith -  indices - the column indices
2300bef8e0ddSBarry Smith 
230115091d37SBarry Smith   Level: advanced
230215091d37SBarry Smith 
2303bef8e0ddSBarry Smith   Notes:
2304bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2305bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2306bef8e0ddSBarry Smith   of the MatSetValues() operation.
2307bef8e0ddSBarry Smith 
2308bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2309d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2310bef8e0ddSBarry Smith 
2311bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2312bef8e0ddSBarry Smith 
2313b9617806SBarry Smith     The indices should start with zero, not one.
2314b9617806SBarry Smith 
2315bef8e0ddSBarry Smith @*/
2316be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2317bef8e0ddSBarry Smith {
231897f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2319bef8e0ddSBarry Smith 
2320bef8e0ddSBarry Smith   PetscFunctionBegin;
23214482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
23224482741eSBarry Smith   PetscValidPointer(indices,2);
2323c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2324bef8e0ddSBarry Smith   if (f) {
2325bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2326bef8e0ddSBarry Smith   } else {
2327634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2328bef8e0ddSBarry Smith   }
2329bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2330bef8e0ddSBarry Smith }
2331bef8e0ddSBarry Smith 
2332be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2333be6bf707SBarry Smith 
2334fb2e594dSBarry Smith EXTERN_C_BEGIN
23354a2ae208SSatish Balay #undef __FUNCT__
23364a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2337be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2338be6bf707SBarry Smith {
2339be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23406849ba73SBarry Smith   PetscErrorCode ierr;
23416849ba73SBarry Smith   size_t         nz = aij->i[mat->m];
2342be6bf707SBarry Smith 
2343be6bf707SBarry Smith   PetscFunctionBegin;
2344be6bf707SBarry Smith   if (aij->nonew != 1) {
2345634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2346be6bf707SBarry Smith   }
2347be6bf707SBarry Smith 
2348be6bf707SBarry Smith   /* allocate space for values if not already there */
2349be6bf707SBarry Smith   if (!aij->saved_values) {
235087828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2351be6bf707SBarry Smith   }
2352be6bf707SBarry Smith 
2353be6bf707SBarry Smith   /* copy values over */
235487828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2355be6bf707SBarry Smith   PetscFunctionReturn(0);
2356be6bf707SBarry Smith }
2357fb2e594dSBarry Smith EXTERN_C_END
2358be6bf707SBarry Smith 
23594a2ae208SSatish Balay #undef __FUNCT__
2360b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2361be6bf707SBarry Smith /*@
2362be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2363be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2364be6bf707SBarry Smith        nonlinear portion.
2365be6bf707SBarry Smith 
2366be6bf707SBarry Smith    Collect on Mat
2367be6bf707SBarry Smith 
2368be6bf707SBarry Smith   Input Parameters:
23690e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2370be6bf707SBarry Smith 
237115091d37SBarry Smith   Level: advanced
237215091d37SBarry Smith 
2373be6bf707SBarry Smith   Common Usage, with SNESSolve():
2374be6bf707SBarry Smith $    Create Jacobian matrix
2375be6bf707SBarry Smith $    Set linear terms into matrix
2376be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2377be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2378be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2379be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2380be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2381be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2382be6bf707SBarry Smith $    In your Jacobian routine
2383be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2384be6bf707SBarry Smith $      Set nonlinear terms in matrix
2385be6bf707SBarry Smith 
2386be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2387be6bf707SBarry Smith $    // build linear portion of Jacobian
2388be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2389be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2390be6bf707SBarry Smith $    loop over nonlinear iterations
2391be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2392be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2393be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2394be6bf707SBarry Smith $       Solve linear system with Jacobian
2395be6bf707SBarry Smith $    endloop
2396be6bf707SBarry Smith 
2397be6bf707SBarry Smith   Notes:
2398be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2399be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2400be6bf707SBarry Smith     calling this routine.
2401be6bf707SBarry Smith 
24020c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
24030c468ba9SBarry Smith     and does not allocated additional space.
24040c468ba9SBarry Smith 
2405be6bf707SBarry Smith .seealso: MatRetrieveValues()
2406be6bf707SBarry Smith 
2407be6bf707SBarry Smith @*/
2408be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2409be6bf707SBarry Smith {
2410dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2411be6bf707SBarry Smith 
2412be6bf707SBarry Smith   PetscFunctionBegin;
24134482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
241429bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
241529bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2416be6bf707SBarry Smith 
2417c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2418be6bf707SBarry Smith   if (f) {
2419be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2420be6bf707SBarry Smith   } else {
2421634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2422be6bf707SBarry Smith   }
2423be6bf707SBarry Smith   PetscFunctionReturn(0);
2424be6bf707SBarry Smith }
2425be6bf707SBarry Smith 
2426fb2e594dSBarry Smith EXTERN_C_BEGIN
24274a2ae208SSatish Balay #undef __FUNCT__
24284a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2429be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2430be6bf707SBarry Smith {
2431be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
24326849ba73SBarry Smith   PetscErrorCode ierr;
243397f1f81fSBarry Smith   PetscInt       nz = aij->i[mat->m];
2434be6bf707SBarry Smith 
2435be6bf707SBarry Smith   PetscFunctionBegin;
2436be6bf707SBarry Smith   if (aij->nonew != 1) {
2437634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2438be6bf707SBarry Smith   }
2439be6bf707SBarry Smith   if (!aij->saved_values) {
2440634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2441be6bf707SBarry Smith   }
2442be6bf707SBarry Smith   /* copy values over */
244387828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2444be6bf707SBarry Smith   PetscFunctionReturn(0);
2445be6bf707SBarry Smith }
2446fb2e594dSBarry Smith EXTERN_C_END
2447be6bf707SBarry Smith 
24484a2ae208SSatish Balay #undef __FUNCT__
24494a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2450be6bf707SBarry Smith /*@
2451be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2452be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2453be6bf707SBarry Smith        nonlinear portion.
2454be6bf707SBarry Smith 
2455be6bf707SBarry Smith    Collect on Mat
2456be6bf707SBarry Smith 
2457be6bf707SBarry Smith   Input Parameters:
2458be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2459be6bf707SBarry Smith 
246015091d37SBarry Smith   Level: advanced
246115091d37SBarry Smith 
2462be6bf707SBarry Smith .seealso: MatStoreValues()
2463be6bf707SBarry Smith 
2464be6bf707SBarry Smith @*/
2465be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2466be6bf707SBarry Smith {
2467dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2468be6bf707SBarry Smith 
2469be6bf707SBarry Smith   PetscFunctionBegin;
24704482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
247129bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
247229bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2473be6bf707SBarry Smith 
2474c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2475be6bf707SBarry Smith   if (f) {
2476be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2477be6bf707SBarry Smith   } else {
2478634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2479be6bf707SBarry Smith   }
2480be6bf707SBarry Smith   PetscFunctionReturn(0);
2481be6bf707SBarry Smith }
2482be6bf707SBarry Smith 
2483f83d6046SBarry Smith 
2484be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24854a2ae208SSatish Balay #undef __FUNCT__
24864a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
248717ab2063SBarry Smith /*@C
2488682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24890d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24906e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
249151c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24922bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
249317ab2063SBarry Smith 
2494db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2495db81eaa0SLois Curfman McInnes 
249617ab2063SBarry Smith    Input Parameters:
2497db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
249817ab2063SBarry Smith .  m - number of rows
249917ab2063SBarry Smith .  n - number of columns
250017ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
250151c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
25022bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
250317ab2063SBarry Smith 
250417ab2063SBarry Smith    Output Parameter:
2505416022c9SBarry Smith .  A - the matrix
250617ab2063SBarry Smith 
2507b259b22eSLois Curfman McInnes    Notes:
250849a6f317SBarry Smith    If nnz is given then nz is ignored
250949a6f317SBarry Smith 
251017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
251117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
25120002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
251344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
251417ab2063SBarry Smith 
251517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2516a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
25173d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
25186da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
251917ab2063SBarry Smith 
2520682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
25214fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2522682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
25236c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
25246c7ebb05SLois Curfman McInnes 
25256c7ebb05SLois Curfman McInnes    Options Database Keys:
2526698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2527698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2528db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2529db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2530db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
253117ab2063SBarry Smith 
2532027ccd11SLois Curfman McInnes    Level: intermediate
2533027ccd11SLois Curfman McInnes 
253436db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
253536db0b34SBarry Smith 
253617ab2063SBarry Smith @*/
2537be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
253817ab2063SBarry Smith {
2539dfbe8321SBarry Smith   PetscErrorCode ierr;
25406945ee14SBarry Smith 
25413a40ed3dSBarry Smith   PetscFunctionBegin;
2542f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2543f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2544273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2545ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2546273d9f13SBarry Smith   PetscFunctionReturn(0);
2547273d9f13SBarry Smith }
2548273d9f13SBarry Smith 
25494a2ae208SSatish Balay #undef __FUNCT__
25504a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2551273d9f13SBarry Smith /*@C
2552273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2553273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2554273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2555273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2556273d9f13SBarry Smith 
2557273d9f13SBarry Smith    Collective on MPI_Comm
2558273d9f13SBarry Smith 
2559273d9f13SBarry Smith    Input Parameters:
256045bfc511SMatthew Knepley +  B - The matrix
2561273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2562273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2563273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2564273d9f13SBarry Smith 
2565273d9f13SBarry Smith    Notes:
256649a6f317SBarry Smith      If nnz is given then nz is ignored
256749a6f317SBarry Smith 
2568273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2569273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2570273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2571273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2572273d9f13SBarry Smith 
2573273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2574273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2575273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2576273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2577273d9f13SBarry Smith 
2578a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2579a96a251dSBarry Smith    entries or columns indices
2580a96a251dSBarry Smith 
2581273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2582273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2583273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2584273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2585273d9f13SBarry Smith 
2586273d9f13SBarry Smith    Options Database Keys:
2587698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2588698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2589273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2590273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2591273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2592273d9f13SBarry Smith 
2593273d9f13SBarry Smith    Level: intermediate
2594273d9f13SBarry Smith 
2595273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2596273d9f13SBarry Smith 
2597273d9f13SBarry Smith @*/
2598be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2599273d9f13SBarry Smith {
260097f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2601a23d5eceSKris Buschelman 
2602a23d5eceSKris Buschelman   PetscFunctionBegin;
2603a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2604a23d5eceSKris Buschelman   if (f) {
2605a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2606a23d5eceSKris Buschelman   }
2607a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2608a23d5eceSKris Buschelman }
2609a23d5eceSKris Buschelman 
2610a23d5eceSKris Buschelman EXTERN_C_BEGIN
2611a23d5eceSKris Buschelman #undef __FUNCT__
2612a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2613be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2614a23d5eceSKris Buschelman {
2615273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2616a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
26176849ba73SBarry Smith   PetscErrorCode ierr;
261897f1f81fSBarry Smith   PetscInt       i;
2619273d9f13SBarry Smith 
2620273d9f13SBarry Smith   PetscFunctionBegin;
2621d5d45c9bSBarry Smith 
2622a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2623c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2624c461c341SBarry Smith     nz             = 0;
2625c461c341SBarry Smith   }
2626c461c341SBarry Smith 
2627435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2628435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2629b73539f3SBarry Smith   if (nnz) {
2630273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
263129bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
26323a7fca6bSBarry 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);
2633b73539f3SBarry Smith     }
2634b73539f3SBarry Smith   }
2635b73539f3SBarry Smith 
2636273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2637273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2638273d9f13SBarry Smith 
2639ab93d7beSBarry Smith   if (!skipallocation) {
2640ab93d7beSBarry Smith     ierr = PetscMalloc2(B->m,PetscInt,&b->imax,B->m,PetscInt,&b->ilen);CHKERRQ(ierr);
2641273d9f13SBarry Smith     if (!nnz) {
2642435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2643273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2644273d9f13SBarry Smith       for (i=0; i<B->m; i++) b->imax[i] = nz;
2645273d9f13SBarry Smith       nz = nz*B->m;
2646273d9f13SBarry Smith     } else {
2647273d9f13SBarry Smith       nz = 0;
2648273d9f13SBarry Smith       for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2649273d9f13SBarry Smith     }
2650273d9f13SBarry Smith 
2651ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2652ab93d7beSBarry Smith     for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2653ab93d7beSBarry Smith 
2654273d9f13SBarry Smith     /* allocate the matrix space */
2655a96a251dSBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->m+1,PetscInt,&b->i);CHKERRQ(ierr);
2656bfeeae90SHong Zhang     b->i[0] = 0;
26575da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
26585da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
26595da197adSKris Buschelman     }
2660273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2661273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2662c461c341SBarry Smith   } else {
2663c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2664c461c341SBarry Smith   }
2665273d9f13SBarry Smith 
2666273d9f13SBarry Smith   b->nz                = 0;
2667273d9f13SBarry Smith   b->maxnz             = nz;
2668273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2669273d9f13SBarry Smith   PetscFunctionReturn(0);
2670273d9f13SBarry Smith }
2671a23d5eceSKris Buschelman EXTERN_C_END
2672273d9f13SBarry Smith 
2673a1661176SMatthew Knepley #undef  __FUNCT__
2674a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
2675a1661176SMatthew Knepley /*@C
2676a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2677a1661176SMatthew Knepley 
2678a1661176SMatthew Knepley    Input Parameters:
2679a1661176SMatthew Knepley +  B - the matrix
2680a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2681a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2682a1661176SMatthew Knepley -  v - optional values in the matrix
2683a1661176SMatthew Knepley 
2684d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
2685d58b2322SMatthew Knepley 
2686a1661176SMatthew Knepley    Level: developer
2687a1661176SMatthew Knepley 
2688a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2689a1661176SMatthew Knepley 
2690a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
2691a1661176SMatthew Knepley @*/
2692a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
2693a1661176SMatthew Knepley {
2694a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2695a1661176SMatthew Knepley   PetscErrorCode ierr;
2696a1661176SMatthew Knepley 
2697a1661176SMatthew Knepley   PetscFunctionBegin;
2698a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
2699a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2700a1661176SMatthew Knepley   if (f) {
2701a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2702a1661176SMatthew Knepley   }
2703a1661176SMatthew Knepley   PetscFunctionReturn(0);
2704a1661176SMatthew Knepley }
2705a1661176SMatthew Knepley 
2706a1661176SMatthew Knepley EXTERN_C_BEGIN
2707a1661176SMatthew Knepley #undef  __FUNCT__
2708a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
2709a1661176SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt I[],const PetscInt J[],const PetscScalar v[])
2710a1661176SMatthew Knepley {
2711a1661176SMatthew Knepley   PetscInt       i;
2712a1661176SMatthew Knepley   PetscInt       m,n;
2713a1661176SMatthew Knepley   PetscInt       nz;
2714a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
2715a1661176SMatthew Knepley   PetscScalar    *values;
2716a1661176SMatthew Knepley   PetscErrorCode ierr;
2717a1661176SMatthew Knepley 
2718a1661176SMatthew Knepley   PetscFunctionBegin;
2719a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
2720a1661176SMatthew Knepley 
2721a1661176SMatthew Knepley   if (I[0]) {
2722a1661176SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "I[0] must be 0 it is %D", I[0]);
2723a1661176SMatthew Knepley   }
2724a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
2725a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2726a1661176SMatthew Knepley     nz     = I[i+1]- I[i];
2727a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
2728a1661176SMatthew Knepley     if (nz < 0) {
2729a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
2730a1661176SMatthew Knepley     }
2731a1661176SMatthew Knepley     nnz[i] = nz;
2732a1661176SMatthew Knepley   }
2733a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
2734a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
2735a1661176SMatthew Knepley 
2736a1661176SMatthew Knepley   if (v) {
2737a1661176SMatthew Knepley     values = (PetscScalar*) v;
2738a1661176SMatthew Knepley   } else {
2739a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
2740a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2741a1661176SMatthew Knepley   }
2742a1661176SMatthew Knepley 
2743a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2744a1661176SMatthew Knepley 
2745a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2746a1661176SMatthew Knepley     nz  = I[i+1] - I[i];
2747a1661176SMatthew Knepley     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+I[i], values + (v ? I[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
2748a1661176SMatthew Knepley   }
2749a1661176SMatthew Knepley 
2750a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2751a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2752a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_UNSORTED);CHKERRQ(ierr);
2753a1661176SMatthew Knepley 
2754a1661176SMatthew Knepley   if (!v) {
2755a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
2756a1661176SMatthew Knepley   }
2757a1661176SMatthew Knepley   PetscFunctionReturn(0);
2758a1661176SMatthew Knepley }
2759a1661176SMatthew Knepley EXTERN_C_END
2760a1661176SMatthew Knepley 
27610bad9183SKris Buschelman /*MC
2762fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
27630bad9183SKris Buschelman    based on compressed sparse row format.
27640bad9183SKris Buschelman 
27650bad9183SKris Buschelman    Options Database Keys:
27660bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
27670bad9183SKris Buschelman 
27680bad9183SKris Buschelman   Level: beginner
27690bad9183SKris Buschelman 
2770f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
27710bad9183SKris Buschelman M*/
27720bad9183SKris Buschelman 
2773a6175056SHong Zhang EXTERN_C_BEGIN
27744a2ae208SSatish Balay #undef __FUNCT__
27754a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2776be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
2777273d9f13SBarry Smith {
2778273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2779dfbe8321SBarry Smith   PetscErrorCode ierr;
278038baddfdSBarry Smith   PetscMPIInt    size;
2781273d9f13SBarry Smith 
2782273d9f13SBarry Smith   PetscFunctionBegin;
2783273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2784273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2785273d9f13SBarry Smith 
2786273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2787273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2788273d9f13SBarry Smith 
2789b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2790b0a32e0cSBarry Smith   B->data             = (void*)b;
2791549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2792416022c9SBarry Smith   B->factor           = 0;
279390f02eecSBarry Smith   B->mapping          = 0;
2794416022c9SBarry Smith   b->row              = 0;
2795416022c9SBarry Smith   b->col              = 0;
279682bf6240SBarry Smith   b->icol             = 0;
2797b810aeb4SBarry Smith   b->reallocs         = 0;
279817ab2063SBarry Smith 
27998a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
28008a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2801a5ae1ecdSBarry Smith 
2802f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
280336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2804f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2805416022c9SBarry Smith   b->nonew             = 0;
2806416022c9SBarry Smith   b->diag              = 0;
2807416022c9SBarry Smith   b->solve_work        = 0;
28082a1b7f2aSHong Zhang   B->spptr             = 0;
2809be6bf707SBarry Smith   b->saved_values      = 0;
2810d7f994e1SBarry Smith   b->idiag             = 0;
2811d7f994e1SBarry Smith   b->ssor              = 0;
2812f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2813a30b2313SHong Zhang   b->xtoy              = 0;
2814a30b2313SHong Zhang   b->XtoY              = 0;
281573e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
2816d487561eSHong Zhang   b->compressedrow.nrows   = B->m;
2817d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
2818d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
2819d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
282088e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
282117ab2063SBarry Smith 
282235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
282335d8aa7fSBarry Smith 
2824f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2825bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2826bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2827f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2828be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2829bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2830f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2831be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2832bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2833a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2834a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2835a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
283685fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
283785fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
283885fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
28395fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
28405fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
28415fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2842a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2843a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2844a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
2845a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
2846a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
2847a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
284805b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
284905b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
285005b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
28514846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
28523a40ed3dSBarry Smith   PetscFunctionReturn(0);
285317ab2063SBarry Smith }
2854273d9f13SBarry Smith EXTERN_C_END
285517ab2063SBarry Smith 
28564a2ae208SSatish Balay #undef __FUNCT__
28574a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2858dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
285917ab2063SBarry Smith {
2860416022c9SBarry Smith   Mat            C;
2861416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
28626849ba73SBarry Smith   PetscErrorCode ierr;
286397f1f81fSBarry Smith   PetscInt       i,m = A->m;
286417ab2063SBarry Smith 
28653a40ed3dSBarry Smith   PetscFunctionBegin;
28664043dd9cSLois Curfman McInnes   *B = 0;
2867f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
2868f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->m,A->n,A->m,A->n);CHKERRQ(ierr);
2869be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
28701d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
28711d5dac46SHong Zhang 
2872273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
2873273d9f13SBarry Smith 
2874416022c9SBarry Smith   C->factor           = A->factor;
28756ad4291fSHong Zhang 
2876416022c9SBarry Smith   c->row            = 0;
2877416022c9SBarry Smith   c->col            = 0;
287882bf6240SBarry Smith   c->icol           = 0;
28796ad4291fSHong Zhang   c->reallocs       = 0;
288017ab2063SBarry Smith 
28816ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
288217ab2063SBarry Smith 
288333b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
288417ab2063SBarry Smith   for (i=0; i<m; i++) {
2885416022c9SBarry Smith     c->imax[i] = a->imax[i];
2886416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
288717ab2063SBarry Smith   }
288817ab2063SBarry Smith 
288917ab2063SBarry Smith   /* allocate the matrix space */
2890a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
2891f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
289297f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
289317ab2063SBarry Smith   if (m > 0) {
289497f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
2895be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2896bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2897be6bf707SBarry Smith     } else {
2898bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
289917ab2063SBarry Smith     }
290008480c60SBarry Smith   }
290117ab2063SBarry Smith 
2902416022c9SBarry Smith   c->sorted            = a->sorted;
29036ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
2904416022c9SBarry Smith   c->roworiented       = a->roworiented;
2905416022c9SBarry Smith   c->nonew             = a->nonew;
2906416022c9SBarry Smith   if (a->diag) {
290797f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
290852e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
290917ab2063SBarry Smith     for (i=0; i<m; i++) {
2910416022c9SBarry Smith       c->diag[i] = a->diag[i];
291117ab2063SBarry Smith     }
29123a40ed3dSBarry Smith   } else c->diag        = 0;
29136ad4291fSHong Zhang   c->solve_work         = 0;
29146ad4291fSHong Zhang   c->saved_values          = 0;
29156ad4291fSHong Zhang   c->idiag                 = 0;
29166ad4291fSHong Zhang   c->ssor                  = 0;
29176ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
29186ad4291fSHong Zhang   c->freedata              = PETSC_TRUE;
29196ad4291fSHong Zhang   c->xtoy                  = 0;
29206ad4291fSHong Zhang   c->XtoY                  = 0;
29216ad4291fSHong Zhang 
2922416022c9SBarry Smith   c->nz                 = a->nz;
2923416022c9SBarry Smith   c->maxnz              = a->maxnz;
2924273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2925754ec7b1SSatish Balay 
29266ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
29276ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
29286ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
29296ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
29306ad4291fSHong Zhang     i = a->compressedrow.nrows;
29316ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
29326ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
29336ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
29346ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
293527ea64f8SHong Zhang   } else {
293627ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
293727ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
293827ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
29396ad4291fSHong Zhang   }
294088e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
29416ad4291fSHong Zhang 
29424846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
29434846f1f5SKris Buschelman 
2944416022c9SBarry Smith   *B = C;
2945b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
29463a40ed3dSBarry Smith   PetscFunctionReturn(0);
294717ab2063SBarry Smith }
294817ab2063SBarry Smith 
29494a2ae208SSatish Balay #undef __FUNCT__
29504a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2951f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A)
295217ab2063SBarry Smith {
2953416022c9SBarry Smith   Mat_SeqAIJ     *a;
2954416022c9SBarry Smith   Mat            B;
29556849ba73SBarry Smith   PetscErrorCode ierr;
29563c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
295738baddfdSBarry Smith   int            fd;
295838baddfdSBarry Smith   PetscMPIInt    size;
2959bcd2baecSBarry Smith   MPI_Comm       comm;
296017ab2063SBarry Smith 
29613a40ed3dSBarry Smith   PetscFunctionBegin;
2962e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2963d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
296429bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2965b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
29660752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2967552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
296817ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
296917ab2063SBarry Smith 
2970d64ed03dSBarry Smith   if (nz < 0) {
297129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2972d64ed03dSBarry Smith   }
2973d64ed03dSBarry Smith 
297417ab2063SBarry Smith   /* read in row lengths */
297597f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
29760752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
297717ab2063SBarry Smith 
29783c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
29793c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
29803c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
29813c601197SSatish Balay 
298217ab2063SBarry Smith   /* create our matrix */
2983f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
2984f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
2985b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2986ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
2987416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
298817ab2063SBarry Smith 
298917ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
29900752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
299117ab2063SBarry Smith 
299217ab2063SBarry Smith   /* read in nonzero values */
29930752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
299417ab2063SBarry Smith 
299517ab2063SBarry Smith   /* set matrix "i" values */
2996efb16452SHong Zhang   a->i[0] = 0;
299717ab2063SBarry Smith   for (i=1; i<= M; i++) {
2998416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2999416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
300017ab2063SBarry Smith   }
3001606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
300217ab2063SBarry Smith 
30036d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
30046d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3005b3a1e11cSKris Buschelman   *A = B;
30063a40ed3dSBarry Smith   PetscFunctionReturn(0);
300717ab2063SBarry Smith }
300817ab2063SBarry Smith 
30094a2ae208SSatish Balay #undef __FUNCT__
3010b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3011dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
30127264ac53SSatish Balay {
30137264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3014dfbe8321SBarry Smith   PetscErrorCode ierr;
30157264ac53SSatish Balay 
30163a40ed3dSBarry Smith   PetscFunctionBegin;
3017bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3018bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
3019ca44d042SBarry Smith     *flg = PETSC_FALSE;
3020ca44d042SBarry Smith     PetscFunctionReturn(0);
3021bcd2baecSBarry Smith   }
30227264ac53SSatish Balay 
30237264ac53SSatish Balay   /* if the a->i are the same */
302497f1f81fSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3025abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
30267264ac53SSatish Balay 
30277264ac53SSatish Balay   /* if a->j are the same */
302897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3029abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3030bcd2baecSBarry Smith 
3031bcd2baecSBarry Smith   /* if a->a are the same */
303287828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
30330f5bd95cSBarry Smith 
30343a40ed3dSBarry Smith   PetscFunctionReturn(0);
30357264ac53SSatish Balay 
30367264ac53SSatish Balay }
303736db0b34SBarry Smith 
30384a2ae208SSatish Balay #undef __FUNCT__
30394a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
304005869f15SSatish Balay /*@
304136db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
304236db0b34SBarry Smith               provided by the user.
304336db0b34SBarry Smith 
304436db0b34SBarry Smith       Coolective on MPI_Comm
304536db0b34SBarry Smith 
304636db0b34SBarry Smith    Input Parameters:
304736db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
304836db0b34SBarry Smith .   m - number of rows
304936db0b34SBarry Smith .   n - number of columns
305036db0b34SBarry Smith .   i - row indices
305136db0b34SBarry Smith .   j - column indices
305236db0b34SBarry Smith -   a - matrix values
305336db0b34SBarry Smith 
305436db0b34SBarry Smith    Output Parameter:
305536db0b34SBarry Smith .   mat - the matrix
305636db0b34SBarry Smith 
305736db0b34SBarry Smith    Level: intermediate
305836db0b34SBarry Smith 
305936db0b34SBarry Smith    Notes:
30600551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
306136db0b34SBarry Smith     once the matrix is destroyed
306236db0b34SBarry Smith 
306336db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
306436db0b34SBarry Smith 
3065bfeeae90SHong Zhang        The i and j indices are 0 based
306636db0b34SBarry Smith 
306736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
306836db0b34SBarry Smith 
306936db0b34SBarry Smith @*/
3070be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
307136db0b34SBarry Smith {
3072dfbe8321SBarry Smith   PetscErrorCode ierr;
307397f1f81fSBarry Smith   PetscInt       ii;
307436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
307536db0b34SBarry Smith 
307636db0b34SBarry Smith   PetscFunctionBegin;
3077a96a251dSBarry Smith   if (i[0]) {
3078634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
307936db0b34SBarry Smith   }
3080f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3081f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3082ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3083ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3084ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3085ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3086ab93d7beSBarry Smith 
308736db0b34SBarry Smith   aij->i = i;
308836db0b34SBarry Smith   aij->j = j;
308936db0b34SBarry Smith   aij->a = a;
309036db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
309136db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
309236db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
309336db0b34SBarry Smith 
309436db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
309536db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
30962515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
309779a5c55eSBarry 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]);
309836db0b34SBarry Smith #endif
309936db0b34SBarry Smith   }
31002515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
310136db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
310279a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
310379a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
310436db0b34SBarry Smith   }
310536db0b34SBarry Smith #endif
310636db0b34SBarry Smith 
3107b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3108b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
310936db0b34SBarry Smith   PetscFunctionReturn(0);
311036db0b34SBarry Smith }
311136db0b34SBarry Smith 
3112cc8ba8e1SBarry Smith #undef __FUNCT__
3113ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3114dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3115cc8ba8e1SBarry Smith {
3116dfbe8321SBarry Smith   PetscErrorCode ierr;
3117cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
311836db0b34SBarry Smith 
3119cc8ba8e1SBarry Smith   PetscFunctionBegin;
312012c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
3121cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3122cc8ba8e1SBarry Smith     a->coloring = coloring;
312312c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
312497f1f81fSBarry Smith     PetscInt             i,*larray;
312512c595b3SBarry Smith     ISColoring      ocoloring;
312608b6dcc0SBarry Smith     ISColoringValue *colors;
312712c595b3SBarry Smith 
312812c595b3SBarry Smith     /* set coloring for diagonal portion */
312997f1f81fSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
313012c595b3SBarry Smith     for (i=0; i<A->n; i++) {
313112c595b3SBarry Smith       larray[i] = i;
313212c595b3SBarry Smith     }
313312c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
313408b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
313512c595b3SBarry Smith     for (i=0; i<A->n; i++) {
313612c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
313712c595b3SBarry Smith     }
313812c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
313912c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
314012c595b3SBarry Smith     a->coloring = ocoloring;
314112c595b3SBarry Smith   }
3142cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3143cc8ba8e1SBarry Smith }
3144cc8ba8e1SBarry Smith 
3145dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3146ee4f033dSBarry Smith EXTERN_C_BEGIN
314729c1e371SBarry Smith #include "adic/ad_utils.h"
3148ee4f033dSBarry Smith EXTERN_C_END
3149cc8ba8e1SBarry Smith 
3150cc8ba8e1SBarry Smith #undef __FUNCT__
3151ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3152dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3153cc8ba8e1SBarry Smith {
3154cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
315597f1f81fSBarry Smith   PetscInt        m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
31564440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
315708b6dcc0SBarry Smith   ISColoringValue *color;
3158cc8ba8e1SBarry Smith 
3159cc8ba8e1SBarry Smith   PetscFunctionBegin;
3160e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
31614440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3162cc8ba8e1SBarry Smith   color = a->coloring->colors;
3163cc8ba8e1SBarry Smith   /* loop over rows */
3164cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3165cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3166cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3167cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3168cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3169cc8ba8e1SBarry Smith     }
31704440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3171ee4f033dSBarry Smith   }
3172ee4f033dSBarry Smith   PetscFunctionReturn(0);
3173ee4f033dSBarry Smith }
3174ee4f033dSBarry Smith #endif
3175ee4f033dSBarry Smith 
3176ee4f033dSBarry Smith #undef __FUNCT__
3177ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
317897f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3179ee4f033dSBarry Smith {
3180ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
318197f1f81fSBarry Smith   PetscInt             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
318287828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
318308b6dcc0SBarry Smith   ISColoringValue *color;
3184ee4f033dSBarry Smith 
3185ee4f033dSBarry Smith   PetscFunctionBegin;
3186e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3187ee4f033dSBarry Smith   color = a->coloring->colors;
3188ee4f033dSBarry Smith   /* loop over rows */
3189ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3190ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3191ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3192ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3193ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3194ee4f033dSBarry Smith     }
3195ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3196cc8ba8e1SBarry Smith   }
3197cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3198cc8ba8e1SBarry Smith }
319936db0b34SBarry Smith 
3200