xref: /petsc/src/mat/impls/aij/seq/aij.c (revision c69eeb134b0a89be91478fd6fb62b6aa4f391c9c)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
89e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
9f5eb4b81SSatish Balay #include "src/inline/spops.h"
108d195f9aSBarry Smith #include "src/inline/dot.h"
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
134a2ae208SSatish Balay #undef __FUNCT__
1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1579299369SBarry Smith /*   only works if matrix has a full set of diagonal entries */
1679299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1779299369SBarry Smith {
1879299369SBarry Smith   PetscErrorCode ierr;
1979299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
2079299369SBarry Smith   PetscInt       i,*diag, m = Y->m;
2179299369SBarry Smith   PetscScalar    *v,*aa = aij->a;
2279299369SBarry Smith 
2379299369SBarry Smith   PetscFunctionBegin;
2479299369SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(Y);CHKERRQ(ierr);
2579299369SBarry Smith   diag = aij->diag;
2679299369SBarry Smith   ierr = VecGetArray(D,&v);CHKERRQ(ierr);
2779299369SBarry Smith   if (is == INSERT_VALUES) {
2879299369SBarry Smith     for (i=0; i<m; i++) {
2979299369SBarry Smith       aa[diag[i]] = v[i];
3079299369SBarry Smith     }
3179299369SBarry Smith   } else {
3279299369SBarry Smith     for (i=0; i<m; i++) {
3379299369SBarry Smith       aa[diag[i]] += v[i];
3479299369SBarry Smith     }
3579299369SBarry Smith   }
3679299369SBarry Smith   ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
3779299369SBarry Smith   PetscFunctionReturn(0);
3879299369SBarry Smith }
3979299369SBarry Smith 
4079299369SBarry Smith #undef __FUNCT__
414a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
4297f1f81fSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
4317ab2063SBarry Smith {
44416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
45dfbe8321SBarry Smith   PetscErrorCode ierr;
4697f1f81fSBarry Smith   PetscInt       i,ishift;
4717ab2063SBarry Smith 
483a40ed3dSBarry Smith   PetscFunctionBegin;
4931625ec5SSatish Balay   *m     = A->m;
503a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
51bfeeae90SHong Zhang   ishift = 0;
5253e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
53273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
54bfeeae90SHong Zhang   } else if (oshift == 1) {
5597f1f81fSBarry Smith     PetscInt nz = a->i[A->m];
563b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
5797f1f81fSBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
5897f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
593b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
60273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
616945ee14SBarry Smith   } else {
626945ee14SBarry Smith     *ia = a->i; *ja = a->j;
63a2ce50c7SBarry Smith   }
643a40ed3dSBarry Smith   PetscFunctionReturn(0);
65a2744918SBarry Smith }
66a2744918SBarry Smith 
674a2ae208SSatish Balay #undef __FUNCT__
684a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
6997f1f81fSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
706945ee14SBarry Smith {
71dfbe8321SBarry Smith   PetscErrorCode ierr;
726945ee14SBarry Smith 
733a40ed3dSBarry Smith   PetscFunctionBegin;
743a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
75bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
76606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
77606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
78bcd2baecSBarry Smith   }
793a40ed3dSBarry Smith   PetscFunctionReturn(0);
8017ab2063SBarry Smith }
8117ab2063SBarry Smith 
824a2ae208SSatish Balay #undef __FUNCT__
834a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
8497f1f81fSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
853b2fbd54SBarry Smith {
863b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
87dfbe8321SBarry Smith   PetscErrorCode ierr;
8897f1f81fSBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->n,m = A->m;
8997f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
903b2fbd54SBarry Smith 
913a40ed3dSBarry Smith   PetscFunctionBegin;
923b2fbd54SBarry Smith   *nn     = A->n;
933a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
943b2fbd54SBarry Smith   if (symmetric) {
95bfeeae90SHong Zhang     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
963b2fbd54SBarry Smith   } else {
9797f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
9897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
9997f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
10097f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1013b2fbd54SBarry Smith     jj = a->j;
1023b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
103bfeeae90SHong Zhang       collengths[jj[i]]++;
1043b2fbd54SBarry Smith     }
1053b2fbd54SBarry Smith     cia[0] = oshift;
1063b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1073b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1083b2fbd54SBarry Smith     }
10997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1103b2fbd54SBarry Smith     jj   = a->j;
111a93ec695SBarry Smith     for (row=0; row<m; row++) {
112a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
113a93ec695SBarry Smith       for (i=0; i<mr; i++) {
114bfeeae90SHong Zhang         col = *jj++;
1153b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1163b2fbd54SBarry Smith       }
1173b2fbd54SBarry Smith     }
118606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1193b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1203b2fbd54SBarry Smith   }
1213a40ed3dSBarry Smith   PetscFunctionReturn(0);
1223b2fbd54SBarry Smith }
1233b2fbd54SBarry Smith 
1244a2ae208SSatish Balay #undef __FUNCT__
1254a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
12697f1f81fSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1273b2fbd54SBarry Smith {
128dfbe8321SBarry Smith   PetscErrorCode ierr;
129606d414cSSatish Balay 
1303a40ed3dSBarry Smith   PetscFunctionBegin;
1313a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1323b2fbd54SBarry Smith 
133606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
134606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1353b2fbd54SBarry Smith 
1363a40ed3dSBarry Smith   PetscFunctionReturn(0);
1373b2fbd54SBarry Smith }
1383b2fbd54SBarry Smith 
13987d4246cSBarry Smith #undef __FUNCT__
14087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
14187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
14287d4246cSBarry Smith {
14387d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
14487d4246cSBarry Smith   PetscInt       *ai = a->i;
14587d4246cSBarry Smith   PetscErrorCode ierr;
14687d4246cSBarry Smith 
14787d4246cSBarry Smith   PetscFunctionBegin;
14887d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
14987d4246cSBarry Smith   PetscFunctionReturn(0);
15087d4246cSBarry Smith }
15187d4246cSBarry Smith 
152227d817aSBarry Smith #define CHUNKSIZE   15
15317ab2063SBarry Smith 
1544a2ae208SSatish Balay #undef __FUNCT__
1554a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
15697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
15717ab2063SBarry Smith {
158416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
159e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
16097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1616849ba73SBarry Smith   PetscErrorCode ierr;
162e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
16387828ca2SBarry Smith   PetscScalar    *ap,value,*aa = a->a;
16436db0b34SBarry Smith   PetscTruth     ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
165273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
16617ab2063SBarry Smith 
1673a40ed3dSBarry Smith   PetscFunctionBegin;
16817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
169416022c9SBarry Smith     row  = im[k];
1705ef9f2a5SBarry Smith     if (row < 0) continue;
1712515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
17277431f27SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1);
1733b2fbd54SBarry Smith #endif
174bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
17517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
176416022c9SBarry Smith     low  = 0;
177c71e6ed7SBarry Smith     high = nrow;
17817ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1795ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1802515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
18177431f27SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1);
1823b2fbd54SBarry Smith #endif
183bfeeae90SHong Zhang       col = in[l];
1844b0e389bSBarry Smith       if (roworiented) {
1855ef9f2a5SBarry Smith         value = v[l + k*n];
186bef8e0ddSBarry Smith       } else {
1874b0e389bSBarry Smith         value = v[k + l*m];
1884b0e389bSBarry Smith       }
189abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
19036db0b34SBarry Smith 
1917cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
192e2ee6c50SBarry Smith       lastcol = col;
193416022c9SBarry Smith       while (high-low > 5) {
194416022c9SBarry Smith         t = (low+high)/2;
195416022c9SBarry Smith         if (rp[t] > col) high = t;
196416022c9SBarry Smith         else             low  = t;
19717ab2063SBarry Smith       }
198416022c9SBarry Smith       for (i=low; i<high; i++) {
19917ab2063SBarry Smith         if (rp[i] > col) break;
20017ab2063SBarry Smith         if (rp[i] == col) {
201416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
20217ab2063SBarry Smith           else                  ap[i] = value;
20317ab2063SBarry Smith           goto noinsert;
20417ab2063SBarry Smith         }
20517ab2063SBarry Smith       }
206abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
207c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
208cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
209ed1caa07SMatthew Knepley       MatSeqXAIJReallocateAIJ(a,1,nrow,row,col,rmax,aa,ai,aj,A->m,rp,ap,imax,nonew);
210416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
211416022c9SBarry Smith       /* shift up all the later entries in this row */
212416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
21317ab2063SBarry Smith         rp[ii+1] = rp[ii];
21417ab2063SBarry Smith         ap[ii+1] = ap[ii];
21517ab2063SBarry Smith       }
21617ab2063SBarry Smith       rp[i] = col;
21717ab2063SBarry Smith       ap[i] = value;
21817ab2063SBarry Smith       noinsert:;
219416022c9SBarry Smith       low = i + 1;
22017ab2063SBarry Smith     }
22117ab2063SBarry Smith     ailen[row] = nrow;
22217ab2063SBarry Smith   }
22388e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2243a40ed3dSBarry Smith   PetscFunctionReturn(0);
22517ab2063SBarry Smith }
22617ab2063SBarry Smith 
2274a2ae208SSatish Balay #undef __FUNCT__
2284a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
22997f1f81fSBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2307eb43aa7SLois Curfman McInnes {
2317eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
23297f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
23397f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
23487828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
2357eb43aa7SLois Curfman McInnes 
2363a40ed3dSBarry Smith   PetscFunctionBegin;
2377eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2387eb43aa7SLois Curfman McInnes     row  = im[k];
23977431f27SBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row);
24077431f27SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1);
241bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2427eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2437eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
24477431f27SBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]);
24577431f27SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1);
246bfeeae90SHong Zhang       col = in[l] ;
2477eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2487eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2497eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2507eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2517eb43aa7SLois Curfman McInnes         else             low  = t;
2527eb43aa7SLois Curfman McInnes       }
2537eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2547eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2557eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
256b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2577eb43aa7SLois Curfman McInnes           goto finished;
2587eb43aa7SLois Curfman McInnes         }
2597eb43aa7SLois Curfman McInnes       }
260b49de8d1SLois Curfman McInnes       *v++ = zero;
2617eb43aa7SLois Curfman McInnes       finished:;
2627eb43aa7SLois Curfman McInnes     }
2637eb43aa7SLois Curfman McInnes   }
2643a40ed3dSBarry Smith   PetscFunctionReturn(0);
2657eb43aa7SLois Curfman McInnes }
2667eb43aa7SLois Curfman McInnes 
26717ab2063SBarry Smith 
2684a2ae208SSatish Balay #undef __FUNCT__
2694a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
270dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
27117ab2063SBarry Smith {
272416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2736849ba73SBarry Smith   PetscErrorCode ierr;
2746f69ff64SBarry Smith   PetscInt       i,*col_lens;
2756f69ff64SBarry Smith   int            fd;
27617ab2063SBarry Smith 
2773a40ed3dSBarry Smith   PetscFunctionBegin;
278b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
27997f1f81fSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
280552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
281273d9f13SBarry Smith   col_lens[1] = A->m;
282273d9f13SBarry Smith   col_lens[2] = A->n;
283416022c9SBarry Smith   col_lens[3] = a->nz;
284416022c9SBarry Smith 
285416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
286273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
287416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
28817ab2063SBarry Smith   }
2896f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
290606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
291416022c9SBarry Smith 
292416022c9SBarry Smith   /* store column indices (zero start index) */
2936f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
294416022c9SBarry Smith 
295416022c9SBarry Smith   /* store nonzero values */
2966f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
2973a40ed3dSBarry Smith   PetscFunctionReturn(0);
29817ab2063SBarry Smith }
299416022c9SBarry Smith 
300dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
301cd155464SBarry Smith 
3024a2ae208SSatish Balay #undef __FUNCT__
3034a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
304dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
305416022c9SBarry Smith {
306416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
307dfbe8321SBarry Smith   PetscErrorCode    ierr;
30897f1f81fSBarry Smith   PetscInt          i,j,m = A->m,shift=0;
309e060cb09SBarry Smith   const char        *name;
310f3ef73ceSBarry Smith   PetscViewerFormat format;
31117ab2063SBarry Smith 
3123a40ed3dSBarry Smith   PetscFunctionBegin;
313435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
314b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
31571c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
31697f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
317273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
318d00d2cf4SBarry Smith       nofinalvalue = 1;
319d00d2cf4SBarry Smith     }
320b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
32177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->n);CHKERRQ(ierr);
32277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
32377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
324b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
32517ab2063SBarry Smith 
32617ab2063SBarry Smith     for (i=0; i<m; i++) {
327416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
328aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
32977431f27SBarry 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);
33017ab2063SBarry Smith #else
33177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
33217ab2063SBarry Smith #endif
33317ab2063SBarry Smith       }
33417ab2063SBarry Smith     }
335d00d2cf4SBarry Smith     if (nofinalvalue) {
33677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
337d00d2cf4SBarry Smith     }
338fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
339b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
34068369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
341cd155464SBarry Smith      PetscFunctionReturn(0);
342fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
343b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
34444cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
34577431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
34644cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
347aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34836db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
34977431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35036db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
35177431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35236db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
35377431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3546831982aSBarry Smith         }
35544cd7ae7SLois Curfman McInnes #else
35677431f27SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
35744cd7ae7SLois Curfman McInnes #endif
35844cd7ae7SLois Curfman McInnes       }
359b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
36044cd7ae7SLois Curfman McInnes     }
361b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
362fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
36397f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
364b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36597f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
366496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
367496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
368496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
369496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
370aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
37136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
372496be53dSLois Curfman McInnes #else
373496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
374496be53dSLois Curfman McInnes #endif
375496be53dSLois Curfman McInnes         }
376496be53dSLois Curfman McInnes       }
377496be53dSLois Curfman McInnes     }
3782e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
37977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3802e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
38177431f27SBarry 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);}
38277431f27SBarry 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);}
38377431f27SBarry 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);}
38477431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
38577431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
38677431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
387496be53dSLois Curfman McInnes     }
388b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
389606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
390496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
391496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
39277431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
393496be53dSLois Curfman McInnes       }
394b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
395496be53dSLois Curfman McInnes     }
396b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
397496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
398496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
399496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
400aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
40136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
402b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4036831982aSBarry Smith           }
404496be53dSLois Curfman McInnes #else
405b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
406496be53dSLois Curfman McInnes #endif
407496be53dSLois Curfman McInnes         }
408496be53dSLois Curfman McInnes       }
409b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
410496be53dSLois Curfman McInnes     }
411b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
412fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
41397f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
41487828ca2SBarry Smith     PetscScalar value;
41502594712SBarry Smith 
416b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
41702594712SBarry Smith     for (i=0; i<m; i++) {
41802594712SBarry Smith       jcnt = 0;
419273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
420e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
42102594712SBarry Smith           value = a->a[cnt++];
422e24b481bSBarry Smith           jcnt++;
42302594712SBarry Smith         } else {
42402594712SBarry Smith           value = 0.0;
42502594712SBarry Smith         }
426aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
427b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
42802594712SBarry Smith #else
429b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
43002594712SBarry Smith #endif
43102594712SBarry Smith       }
432b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
43302594712SBarry Smith     }
434b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4353a40ed3dSBarry Smith   } else {
436b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43717ab2063SBarry Smith     for (i=0; i<m; i++) {
43877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
439416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
440aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
44277431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
44477431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4453a40ed3dSBarry Smith         } else {
44677431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
44717ab2063SBarry Smith         }
44817ab2063SBarry Smith #else
44977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
45017ab2063SBarry Smith #endif
45117ab2063SBarry Smith       }
452b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45317ab2063SBarry Smith     }
454b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
45517ab2063SBarry Smith   }
456b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4573a40ed3dSBarry Smith   PetscFunctionReturn(0);
458416022c9SBarry Smith }
459416022c9SBarry Smith 
4604a2ae208SSatish Balay #undef __FUNCT__
4614a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
462dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
463416022c9SBarry Smith {
464480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
465416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
466dfbe8321SBarry Smith   PetscErrorCode    ierr;
46797f1f81fSBarry Smith   PetscInt          i,j,m = A->m,color;
46836db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
469b0a32e0cSBarry Smith   PetscViewer       viewer;
470f3ef73ceSBarry Smith   PetscViewerFormat format;
471cddf8d76SBarry Smith 
4723a40ed3dSBarry Smith   PetscFunctionBegin;
473480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
474b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
47519bcc07fSBarry Smith 
476b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
477416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4780513a670SBarry Smith 
479fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4800513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
481b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
482416022c9SBarry Smith     for (i=0; i<m; i++) {
483cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
484bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
485bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
486aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
488cddf8d76SBarry Smith #else
489cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
490cddf8d76SBarry Smith #endif
491b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
492cddf8d76SBarry Smith       }
493cddf8d76SBarry Smith     }
494b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
495cddf8d76SBarry Smith     for (i=0; i<m; i++) {
496cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
497bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
498bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
499cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
500b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
501cddf8d76SBarry Smith       }
502cddf8d76SBarry Smith     }
503b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
504cddf8d76SBarry Smith     for (i=0; i<m; i++) {
505cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
506bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
507bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
508aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
510cddf8d76SBarry Smith #else
511cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
512cddf8d76SBarry Smith #endif
513b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
514416022c9SBarry Smith       }
515416022c9SBarry Smith     }
5160513a670SBarry Smith   } else {
5170513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5180513a670SBarry Smith     /* first determine max of all nonzero values */
51997f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
520b0a32e0cSBarry Smith     PetscDraw   popup;
52136db0b34SBarry Smith     PetscReal scale;
5220513a670SBarry Smith 
5230513a670SBarry Smith     for (i=0; i<nz; i++) {
5240513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5250513a670SBarry Smith     }
526b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
527b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
528b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5290513a670SBarry Smith     count = 0;
5300513a670SBarry Smith     for (i=0; i<m; i++) {
5310513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
532bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
533bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
53497f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
535b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5360513a670SBarry Smith         count++;
5370513a670SBarry Smith       }
5380513a670SBarry Smith     }
5390513a670SBarry Smith   }
540480ef9eaSBarry Smith   PetscFunctionReturn(0);
541480ef9eaSBarry Smith }
542cddf8d76SBarry Smith 
5434a2ae208SSatish Balay #undef __FUNCT__
5444a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
545dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
546480ef9eaSBarry Smith {
547dfbe8321SBarry Smith   PetscErrorCode ierr;
548b0a32e0cSBarry Smith   PetscDraw      draw;
54936db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
550480ef9eaSBarry Smith   PetscTruth     isnull;
551480ef9eaSBarry Smith 
552480ef9eaSBarry Smith   PetscFunctionBegin;
553b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
554b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
555480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
556480ef9eaSBarry Smith 
557480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
558273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
559480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
560b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
561b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
562480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5633a40ed3dSBarry Smith   PetscFunctionReturn(0);
564416022c9SBarry Smith }
565416022c9SBarry Smith 
5664a2ae208SSatish Balay #undef __FUNCT__
5674a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
568dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
569416022c9SBarry Smith {
570dfbe8321SBarry Smith   PetscErrorCode ierr;
57132077d6dSBarry Smith   PetscTruth     issocket,iascii,isbinary,isdraw;
572416022c9SBarry Smith 
5733a40ed3dSBarry Smith   PetscFunctionBegin;
574b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
57532077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
576fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
577fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
578c45a1595SBarry Smith   if (iascii) {
5793a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5804cf18111SSatish Balay #if defined(PETSC_USE_SOCKET_VIEWER)
581c45a1595SBarry Smith   } else if (issocket) {
5824cf18111SSatish Balay     Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
583c45a1595SBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
584c45a1595SBarry Smith #endif
5850f5bd95cSBarry Smith   } else if (isbinary) {
5863a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5870f5bd95cSBarry Smith   } else if (isdraw) {
5883a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5895cd90555SBarry Smith   } else {
590958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
59117ab2063SBarry Smith   }
5924846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
5933a40ed3dSBarry Smith   PetscFunctionReturn(0);
59417ab2063SBarry Smith }
59519bcc07fSBarry Smith 
5964a2ae208SSatish Balay #undef __FUNCT__
5974a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
598dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
59917ab2063SBarry Smith {
600416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6016849ba73SBarry Smith   PetscErrorCode ierr;
60297f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
60397f1f81fSBarry Smith   PetscInt       m = A->m,*ip,N,*ailen = a->ilen,rmax = 0;
60487828ca2SBarry Smith   PetscScalar    *aa = a->a,*ap;
6053447b6efSHong Zhang   PetscReal      ratio=0.6;
60617ab2063SBarry Smith 
6073a40ed3dSBarry Smith   PetscFunctionBegin;
6083a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
60917ab2063SBarry Smith 
61043ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
61117ab2063SBarry Smith   for (i=1; i<m; i++) {
612416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
61317ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
61494a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
61517ab2063SBarry Smith     if (fshift) {
616bfeeae90SHong Zhang       ip = aj + ai[i] ;
617bfeeae90SHong Zhang       ap = aa + ai[i] ;
61817ab2063SBarry Smith       N  = ailen[i];
61917ab2063SBarry Smith       for (j=0; j<N; j++) {
62017ab2063SBarry Smith         ip[j-fshift] = ip[j];
62117ab2063SBarry Smith         ap[j-fshift] = ap[j];
62217ab2063SBarry Smith       }
62317ab2063SBarry Smith     }
62417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
62517ab2063SBarry Smith   }
62617ab2063SBarry Smith   if (m) {
62717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
62817ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
62917ab2063SBarry Smith   }
63017ab2063SBarry Smith   /* reset ilen and imax for each row */
63117ab2063SBarry Smith   for (i=0; i<m; i++) {
63217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
63317ab2063SBarry Smith   }
634bfeeae90SHong Zhang   a->nz = ai[m];
63517ab2063SBarry Smith 
63617ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
637416022c9SBarry Smith   if (fshift && a->diag) {
638606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
63952e6d16bSBarry Smith     ierr = PetscLogObjectMemory(A,-(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
640416022c9SBarry Smith     a->diag = 0;
64117ab2063SBarry Smith   }
64263ba0a88SBarry 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);
64363ba0a88SBarry Smith   ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %D\n",a->reallocs));CHKERRQ(ierr);
64463ba0a88SBarry Smith   ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Maximum nonzeros in any row is %D\n",rmax));CHKERRQ(ierr);
645dd5f02e7SSatish Balay   a->reallocs          = 0;
6464e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
64736db0b34SBarry Smith   a->rmax              = rmax;
6484e220ebcSLois Curfman McInnes 
649cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
650317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
65188e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
65271c2f376SKris Buschelman 
6534846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
6543a40ed3dSBarry Smith   PetscFunctionReturn(0);
65517ab2063SBarry Smith }
65617ab2063SBarry Smith 
6574a2ae208SSatish Balay #undef __FUNCT__
6584a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
659dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
66017ab2063SBarry Smith {
661416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
662dfbe8321SBarry Smith   PetscErrorCode ierr;
6633a40ed3dSBarry Smith 
6643a40ed3dSBarry Smith   PetscFunctionBegin;
665bfeeae90SHong Zhang   ierr = PetscMemzero(a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
6663a40ed3dSBarry Smith   PetscFunctionReturn(0);
66717ab2063SBarry Smith }
668416022c9SBarry Smith 
6694a2ae208SSatish Balay #undef __FUNCT__
6704a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
671dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
67217ab2063SBarry Smith {
673416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
674dfbe8321SBarry Smith   PetscErrorCode ierr;
675d5d45c9bSBarry Smith 
6763a40ed3dSBarry Smith   PetscFunctionBegin;
677aa482453SBarry Smith #if defined(PETSC_USE_LOG)
67877431f27SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->m,A->n,a->nz);
67917ab2063SBarry Smith #endif
6803cbb7e54SHong Zhang   if (a->freedata){
681085a36d4SBarry Smith     ierr = MatSeqXAIJFreeAIJ(a->singlemalloc,&a->a,&a->j,&a->i);CHKERRQ(ierr);
6823cbb7e54SHong Zhang   }
683c38d4ed2SBarry Smith   if (a->row) {
684c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
685c38d4ed2SBarry Smith   }
686c38d4ed2SBarry Smith   if (a->col) {
687c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
688c38d4ed2SBarry Smith   }
689606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
690ab93d7beSBarry Smith   if (a->ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
691273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
692606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
69382bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
694606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
695cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
696a30b2313SHong Zhang   if (a->xtoy) {ierr = PetscFree(a->xtoy);CHKERRQ(ierr);}
697d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
698a30b2313SHong Zhang 
6994846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7004846f1f5SKris Buschelman 
701606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
702901853e0SKris Buschelman 
703901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
704901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
705901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
706901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
707901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
708901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
709901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
710a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
711901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7123a40ed3dSBarry Smith   PetscFunctionReturn(0);
71317ab2063SBarry Smith }
71417ab2063SBarry Smith 
7154a2ae208SSatish Balay #undef __FUNCT__
7164a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
717dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
71817ab2063SBarry Smith {
7193a40ed3dSBarry Smith   PetscFunctionBegin;
7203a40ed3dSBarry Smith   PetscFunctionReturn(0);
72117ab2063SBarry Smith }
72217ab2063SBarry Smith 
7234a2ae208SSatish Balay #undef __FUNCT__
7244a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
725dfbe8321SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op)
72617ab2063SBarry Smith {
727416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7284846f1f5SKris Buschelman   PetscErrorCode ierr;
7293a40ed3dSBarry Smith 
7303a40ed3dSBarry Smith   PetscFunctionBegin;
731a65d3064SKris Buschelman   switch (op) {
732a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
733a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
734a65d3064SKris Buschelman       break;
735a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
736a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
737a65d3064SKris Buschelman       break;
738a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
739a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
740a65d3064SKris Buschelman       break;
741a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
742a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
743a65d3064SKris Buschelman       break;
744a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
745a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
746a65d3064SKris Buschelman       break;
747a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
748a65d3064SKris Buschelman       a->nonew             = 1;
749a65d3064SKris Buschelman       break;
750a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
751a65d3064SKris Buschelman       a->nonew             = -1;
752a65d3064SKris Buschelman       break;
753a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
754a65d3064SKris Buschelman       a->nonew             = -2;
755a65d3064SKris Buschelman       break;
756a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
757a65d3064SKris Buschelman       a->nonew             = 0;
758a65d3064SKris Buschelman       break;
759a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
760a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
761a65d3064SKris Buschelman       break;
762d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
763d487561eSHong Zhang       a->compressedrow.use = PETSC_TRUE;
764d487561eSHong Zhang       break;
765d487561eSHong Zhang     case MAT_DO_NOT_USE_COMPRESSEDROW:
766d487561eSHong Zhang       a->compressedrow.use = PETSC_FALSE;
767d487561eSHong Zhang       break;
768a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
769a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
770a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
771a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
772a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
77363ba0a88SBarry Smith       ierr = PetscLogInfo((A,"MatSetOption_SeqAIJ:Option ignored\n"));CHKERRQ(ierr);
774a65d3064SKris Buschelman       break;
775a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
77629bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
777a65d3064SKris Buschelman     default:
77871c2f376SKris Buschelman       break;
779a65d3064SKris Buschelman   }
7804846f1f5SKris Buschelman   ierr = MatSetOption_Inode(A,op);CHKERRQ(ierr);
7813a40ed3dSBarry Smith   PetscFunctionReturn(0);
78217ab2063SBarry Smith }
78317ab2063SBarry Smith 
7844a2ae208SSatish Balay #undef __FUNCT__
7854a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
786dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
78717ab2063SBarry Smith {
788416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7896849ba73SBarry Smith   PetscErrorCode ierr;
79097f1f81fSBarry Smith   PetscInt       i,j,n;
79187828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
79217ab2063SBarry Smith 
7933a40ed3dSBarry Smith   PetscFunctionBegin;
7942dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
7951ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
79636db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
797273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
798273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
799bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
800bfeeae90SHong Zhang       if (a->j[j] == i) {
801416022c9SBarry Smith         x[i] = a->a[j];
80217ab2063SBarry Smith         break;
80317ab2063SBarry Smith       }
80417ab2063SBarry Smith     }
80517ab2063SBarry Smith   }
8061ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8073a40ed3dSBarry Smith   PetscFunctionReturn(0);
80817ab2063SBarry Smith }
80917ab2063SBarry Smith 
8104a2ae208SSatish Balay #undef __FUNCT__
8114a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
812dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
81317ab2063SBarry Smith {
814416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8155c897100SBarry Smith   PetscScalar       *x,*y;
816dfbe8321SBarry Smith   PetscErrorCode    ierr;
81797f1f81fSBarry Smith   PetscInt          m = A->m;
8185c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8195c897100SBarry Smith   PetscScalar       *v,alpha;
8207b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8213447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8224eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8235c897100SBarry Smith #endif
82417ab2063SBarry Smith 
8253a40ed3dSBarry Smith   PetscFunctionBegin;
8262e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8271ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8281ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8295c897100SBarry Smith 
8305c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
831bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8325c897100SBarry Smith #else
8333447b6efSHong Zhang   if (usecprow){
8343447b6efSHong Zhang     m    = cprow.nrows;
8353447b6efSHong Zhang     ii   = cprow.i;
8367b2bb3b9SHong Zhang     ridx = cprow.rindex;
8373447b6efSHong Zhang   } else {
8383447b6efSHong Zhang     ii = a->i;
8393447b6efSHong Zhang   }
84017ab2063SBarry Smith   for (i=0; i<m; i++) {
8413447b6efSHong Zhang     idx   = a->j + ii[i] ;
8423447b6efSHong Zhang     v     = a->a + ii[i] ;
8433447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8443447b6efSHong Zhang     if (usecprow){
8457b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8463447b6efSHong Zhang     } else {
84717ab2063SBarry Smith       alpha = x[i];
8483447b6efSHong Zhang     }
84917ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
85017ab2063SBarry Smith   }
8515c897100SBarry Smith #endif
852efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
8531ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8541ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8553a40ed3dSBarry Smith   PetscFunctionReturn(0);
85617ab2063SBarry Smith }
85717ab2063SBarry Smith 
8584a2ae208SSatish Balay #undef __FUNCT__
8595c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
860dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8615c897100SBarry Smith {
8628d5b0100SBarry Smith   PetscScalar    zero = 0.0;
863dfbe8321SBarry Smith   PetscErrorCode ierr;
8645c897100SBarry Smith 
8655c897100SBarry Smith   PetscFunctionBegin;
8662dcb1b2aSMatthew Knepley   ierr = VecSet(yy,zero);CHKERRQ(ierr);
8675c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8685c897100SBarry Smith   PetscFunctionReturn(0);
8695c897100SBarry Smith }
8705c897100SBarry Smith 
8715c897100SBarry Smith 
8725c897100SBarry Smith #undef __FUNCT__
8734a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
874dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
87517ab2063SBarry Smith {
876416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
87797952fefSHong Zhang   PetscScalar    *x,*y,*aa;
878dfbe8321SBarry Smith   PetscErrorCode ierr;
87997952fefSHong Zhang   PetscInt       m=A->m,*aj,*ii;
880f2e71c43SSatish Balay   PetscInt       n,i,j,*ridx=PETSC_NULL;
881362ced78SSatish Balay   PetscScalar    sum;
88297952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
883f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
884f2e71c43SSatish Balay   PetscInt       jrow;
885e36a17ebSSatish Balay #endif
88617ab2063SBarry Smith 
887b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
88897952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
889fee21e36SBarry Smith #endif
890fee21e36SBarry Smith 
8913a40ed3dSBarry Smith   PetscFunctionBegin;
8921ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8931ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
89497952fefSHong Zhang   aj  = a->j;
89597952fefSHong Zhang   aa    = a->a;
896416022c9SBarry Smith   ii   = a->i;
8974eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
89897952fefSHong Zhang     m    = a->compressedrow.nrows;
89997952fefSHong Zhang     ii   = a->compressedrow.i;
90097952fefSHong Zhang     ridx = a->compressedrow.rindex;
90197952fefSHong Zhang     for (i=0; i<m; i++){
90297952fefSHong Zhang       n   = ii[i+1] - ii[i];
90397952fefSHong Zhang       aj  = a->j + ii[i];
90497952fefSHong Zhang       aa  = a->a + ii[i];
90597952fefSHong Zhang       sum = 0.0;
90697952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
90797952fefSHong Zhang       y[*ridx++] = sum;
90897952fefSHong Zhang     }
90997952fefSHong Zhang   } else { /* do not use compressed row format */
910b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
911b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
912b05257ddSBarry Smith #else
91317ab2063SBarry Smith     for (i=0; i<m; i++) {
9149ea0dfa2SSatish Balay       jrow = ii[i];
9159ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
91617ab2063SBarry Smith       sum  = 0.0;
9179ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
91897952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9199ea0dfa2SSatish Balay       }
92017ab2063SBarry Smith       y[i] = sum;
92117ab2063SBarry Smith     }
9228d195f9aSBarry Smith #endif
923b05257ddSBarry Smith   }
924efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz - m);CHKERRQ(ierr);
9251ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9261ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9273a40ed3dSBarry Smith   PetscFunctionReturn(0);
92817ab2063SBarry Smith }
92917ab2063SBarry Smith 
9304a2ae208SSatish Balay #undef __FUNCT__
9314a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
932dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
93317ab2063SBarry Smith {
934416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
93597952fefSHong Zhang   PetscScalar    *x,*y,*z,*aa;
936dfbe8321SBarry Smith   PetscErrorCode ierr;
93797952fefSHong Zhang   PetscInt       m = A->m,*aj,*ii;
938aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
93997952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
940362ced78SSatish Balay   PetscScalar    sum;
94197952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
942e36a17ebSSatish Balay #endif
9439ea0dfa2SSatish Balay 
9443a40ed3dSBarry Smith   PetscFunctionBegin;
9451ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9461ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9472e8a6d31SBarry Smith   if (zz != yy) {
9481ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9492e8a6d31SBarry Smith   } else {
9502e8a6d31SBarry Smith     z = y;
9512e8a6d31SBarry Smith   }
952bfeeae90SHong Zhang 
95397952fefSHong Zhang   aj  = a->j;
95497952fefSHong Zhang   aa  = a->a;
955cddf8d76SBarry Smith   ii  = a->i;
956aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
95797952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
95802ab625aSSatish Balay #else
9594eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
9604eb6d288SHong Zhang     if (zz != yy){
9614eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
9624eb6d288SHong Zhang     }
96397952fefSHong Zhang     m    = a->compressedrow.nrows;
96497952fefSHong Zhang     ii   = a->compressedrow.i;
96597952fefSHong Zhang     ridx = a->compressedrow.rindex;
96697952fefSHong Zhang     for (i=0; i<m; i++){
96797952fefSHong Zhang       n  = ii[i+1] - ii[i];
96897952fefSHong Zhang       aj  = a->j + ii[i];
96997952fefSHong Zhang       aa  = a->a + ii[i];
97097952fefSHong Zhang       sum = y[*ridx];
97197952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
97297952fefSHong Zhang       z[*ridx++] = sum;
97397952fefSHong Zhang     }
97497952fefSHong Zhang   } else { /* do not use compressed row format */
97517ab2063SBarry Smith     for (i=0; i<m; i++) {
9769ea0dfa2SSatish Balay       jrow = ii[i];
9779ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
97817ab2063SBarry Smith       sum  = y[i];
9799ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
98097952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9819ea0dfa2SSatish Balay       }
98217ab2063SBarry Smith       z[i] = sum;
98317ab2063SBarry Smith     }
98497952fefSHong Zhang   }
98502ab625aSSatish Balay #endif
986efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
9871ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9881ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9892e8a6d31SBarry Smith   if (zz != yy) {
9901ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9912e8a6d31SBarry Smith   }
9923a40ed3dSBarry Smith   PetscFunctionReturn(0);
99317ab2063SBarry Smith }
99417ab2063SBarry Smith 
99517ab2063SBarry Smith /*
99617ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
99717ab2063SBarry Smith */
9984a2ae208SSatish Balay #undef __FUNCT__
9994a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1000dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
100117ab2063SBarry Smith {
1002416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10036849ba73SBarry Smith   PetscErrorCode ierr;
100497f1f81fSBarry Smith   PetscInt       i,j,*diag,m = A->m;
100517ab2063SBarry Smith 
10063a40ed3dSBarry Smith   PetscFunctionBegin;
1007f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
1008f1e2ffcdSBarry Smith 
100997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&diag);CHKERRQ(ierr);
101052e6d16bSBarry Smith   ierr = PetscLogObjectMemory(A,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
1011273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
101235b0346bSBarry Smith     diag[i] = a->i[i+1];
1013bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1014bfeeae90SHong Zhang       if (a->j[j] == i) {
1015bfeeae90SHong Zhang         diag[i] = j;
101617ab2063SBarry Smith         break;
101717ab2063SBarry Smith       }
101817ab2063SBarry Smith     }
101917ab2063SBarry Smith   }
1020416022c9SBarry Smith   a->diag = diag;
10213a40ed3dSBarry Smith   PetscFunctionReturn(0);
102217ab2063SBarry Smith }
102317ab2063SBarry Smith 
1024be5855fcSBarry Smith /*
1025be5855fcSBarry Smith      Checks for missing diagonals
1026be5855fcSBarry Smith */
10274a2ae208SSatish Balay #undef __FUNCT__
10284a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1029dfbe8321SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A)
1030be5855fcSBarry Smith {
1031be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10326849ba73SBarry Smith   PetscErrorCode ierr;
103397f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1034be5855fcSBarry Smith 
1035be5855fcSBarry Smith   PetscFunctionBegin;
1036*c69eeb13SBarry Smith   if (A->m > 0 && !jj) SETERRQ(PETSC_ERR_PLIB,"Matrix has no entries therefor is missing diagonal");
1037f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1038f1e2ffcdSBarry Smith   diag = a->diag;
1039273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
1040bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
104177431f27SBarry Smith       SETERRQ1(PETSC_ERR_PLIB,"Matrix is missing diagonal number %D",i);
1042be5855fcSBarry Smith     }
1043be5855fcSBarry Smith   }
1044be5855fcSBarry Smith   PetscFunctionReturn(0);
1045be5855fcSBarry Smith }
1046be5855fcSBarry Smith 
10474a2ae208SSatish Balay #undef __FUNCT__
10484a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
104997f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
105017ab2063SBarry Smith {
1051416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1052beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1053beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1054dfbe8321SBarry Smith   PetscErrorCode     ierr;
105597f1f81fSBarry Smith   PetscInt           n = A->n,m = A->m,i;
105697f1f81fSBarry Smith   const PetscInt     *idx,*diag;
105717ab2063SBarry Smith 
10583a40ed3dSBarry Smith   PetscFunctionBegin;
1059b965ef7fSBarry Smith   its = its*lits;
106077431f27SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
106191723122SBarry Smith 
1062ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1063ed480e8bSBarry Smith   diag = a->diag;
1064ed480e8bSBarry Smith   if (!a->idiag) {
1065ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1066ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1067ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1068ed480e8bSBarry Smith 
1069ed480e8bSBarry Smith     v        = a->a;
1070ed480e8bSBarry Smith 
1071ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1072958c9bccSBarry Smith     if (omega == 1.0 && !fshift) {
1073ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1074ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1075ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1076ed480e8bSBarry Smith       }
1077efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
1078ed480e8bSBarry Smith     } else {
1079ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1080ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1081beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1082ed480e8bSBarry Smith       }
1083efee365bSSatish Balay       ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
1084beeb8507SBarry Smith     }
1085ed480e8bSBarry Smith   }
1086ed480e8bSBarry Smith   t     = a->ssor;
1087ed480e8bSBarry Smith   idiag = a->idiag;
1088ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1089ed480e8bSBarry Smith 
10901ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1091fb2e594dSBarry Smith   if (xx != bb) {
10921ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1093fb2e594dSBarry Smith   } else {
1094fb2e594dSBarry Smith     b = x;
1095fb2e594dSBarry Smith   }
1096fb2e594dSBarry Smith 
1097ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1098ed480e8bSBarry Smith   xs   = x;
109917ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
110017ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1101ed480e8bSBarry Smith     bs = b;
110217ab2063SBarry Smith     for (i=0; i<m; i++) {
1103ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1104416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1105ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1106ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
110717ab2063SBarry Smith         sum  = b[i]*d/omega;
110817ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
110917ab2063SBarry Smith         x[i] = sum;
111017ab2063SBarry Smith     }
11111ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11121ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1113efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11143a40ed3dSBarry Smith     PetscFunctionReturn(0);
111517ab2063SBarry Smith   }
1116c783ea89SBarry Smith 
1117ed480e8bSBarry Smith 
1118fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1119fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1120fc3d8934SBarry Smith 
1121fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1122fc3d8934SBarry Smith 
1123fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1124fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
112548af12d7SBarry Smith     is the relaxation factor.
1126fc3d8934SBarry Smith     */
1127fc3d8934SBarry Smith 
112848af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
112929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11303a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
113117ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
113217ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
113317ab2063SBarry Smith 
113417ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
113517ab2063SBarry Smith 
113617ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
113717ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
113817ab2063SBarry Smith     is the relaxation factor.
113917ab2063SBarry Smith     */
114017ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
114117ab2063SBarry Smith 
114217ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
114317ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1144416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1145ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1146ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
114717ab2063SBarry Smith       sum  = b[i];
114817ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1149ed480e8bSBarry Smith       x[i] = sum*idiag[i];
115017ab2063SBarry Smith     }
115117ab2063SBarry Smith 
115217ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1153416022c9SBarry Smith     v = a->a;
1154ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
115517ab2063SBarry Smith 
115617ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1157ed480e8bSBarry Smith     ts = t;
1158416022c9SBarry Smith     diag = a->diag;
115917ab2063SBarry Smith     for (i=0; i<m; i++) {
1160416022c9SBarry Smith       n    = diag[i] - a->i[i];
1161ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1162ed480e8bSBarry Smith       v    = a->a + a->i[i];
116317ab2063SBarry Smith       sum  = t[i];
116417ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1165ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1166733d66baSBarry Smith       /*  x = x + t */
1167733d66baSBarry Smith       x[i] += t[i];
116817ab2063SBarry Smith     }
116917ab2063SBarry Smith 
1170efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
11711ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11721ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11733a40ed3dSBarry Smith     PetscFunctionReturn(0);
117417ab2063SBarry Smith   }
117517ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
117617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
117777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
117897f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
117977d8c4bbSBarry Smith #else
118017ab2063SBarry Smith       for (i=0; i<m; i++) {
1181416022c9SBarry Smith         n    = diag[i] - 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] = sum*idiag[i];
118717ab2063SBarry Smith       }
118877d8c4bbSBarry Smith #endif
118917ab2063SBarry Smith       xb = x;
1190efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11913a40ed3dSBarry Smith     } else xb = b;
119217ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
119317ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
119417ab2063SBarry Smith       for (i=0; i<m; i++) {
1195ed480e8bSBarry Smith         x[i] *= mdiag[i];
119617ab2063SBarry Smith       }
1197efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
119817ab2063SBarry Smith     }
119917ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
120077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
120197f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
120277d8c4bbSBarry Smith #else
120317ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1204416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1205ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1206ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
120717ab2063SBarry Smith         sum  = xb[i];
120817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1209ed480e8bSBarry Smith         x[i] = sum*idiag[i];
121017ab2063SBarry Smith       }
121177d8c4bbSBarry Smith #endif
1212efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
121317ab2063SBarry Smith     }
121417ab2063SBarry Smith     its--;
121517ab2063SBarry Smith   }
121617ab2063SBarry Smith   while (its--) {
121717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
121877d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
121997f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
122077d8c4bbSBarry Smith #else
122117ab2063SBarry Smith       for (i=0; i<m; i++) {
1222ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1223416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1224ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1225ed480e8bSBarry Smith         v    = a->a + a->i[i];
122617ab2063SBarry Smith         sum  = b[i];
122717ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1228ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
122917ab2063SBarry Smith       }
123077d8c4bbSBarry Smith #endif
1231efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
123217ab2063SBarry Smith     }
123317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
123477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
123597f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
123677d8c4bbSBarry Smith #else
123717ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1238ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1239416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1240ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1241ed480e8bSBarry Smith         v    = a->a + a->i[i];
124217ab2063SBarry Smith         sum  = b[i];
124317ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1244ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
124517ab2063SBarry Smith       }
124677d8c4bbSBarry Smith #endif
1247efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
124817ab2063SBarry Smith     }
124917ab2063SBarry Smith   }
12501ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12511ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12523a40ed3dSBarry Smith   PetscFunctionReturn(0);
125317ab2063SBarry Smith }
125417ab2063SBarry Smith 
12554a2ae208SSatish Balay #undef __FUNCT__
12564a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1257dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
125817ab2063SBarry Smith {
1259416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12604e220ebcSLois Curfman McInnes 
12613a40ed3dSBarry Smith   PetscFunctionBegin;
1262273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1263273d9f13SBarry Smith   info->columns_global = (double)A->n;
1264273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1265273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12664e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12674e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12684e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12694e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12704e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12714e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12724e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12734e220ebcSLois Curfman McInnes   if (A->factor) {
12744e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12754e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12764e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12774e220ebcSLois Curfman McInnes   } else {
12784e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12794e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12804e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12814e220ebcSLois Curfman McInnes   }
12823a40ed3dSBarry Smith   PetscFunctionReturn(0);
128317ab2063SBarry Smith }
128417ab2063SBarry Smith 
12854a2ae208SSatish Balay #undef __FUNCT__
12864a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1287f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
128817ab2063SBarry Smith {
1289416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1290f4df32b1SMatthew Knepley   PetscInt       i,m = A->m - 1;
12916849ba73SBarry Smith   PetscErrorCode ierr;
129217ab2063SBarry Smith 
12933a40ed3dSBarry Smith   PetscFunctionBegin;
1294f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1295f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
129677431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1297bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1298f1e2ffcdSBarry Smith     }
1299f4df32b1SMatthew Knepley     if (diag != 0.0) {
1300f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1301f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1302f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1303f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1304f1e2ffcdSBarry Smith       }
1305f1e2ffcdSBarry Smith     }
130688e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1307f1e2ffcdSBarry Smith   } else {
1308f4df32b1SMatthew Knepley     if (diag != 0.0) {
130917ab2063SBarry Smith       for (i=0; i<N; i++) {
131077431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13117ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1312416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1313f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1314bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13157ae801bdSBarry Smith         } else { /* in case row was completely empty */
1316f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
131717ab2063SBarry Smith         }
131817ab2063SBarry Smith       }
13193a40ed3dSBarry Smith     } else {
132017ab2063SBarry Smith       for (i=0; i<N; i++) {
132177431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1322416022c9SBarry Smith         a->ilen[rows[i]] = 0;
132317ab2063SBarry Smith       }
132417ab2063SBarry Smith     }
132588e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1326f1e2ffcdSBarry Smith   }
132743a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13283a40ed3dSBarry Smith   PetscFunctionReturn(0);
132917ab2063SBarry Smith }
133017ab2063SBarry Smith 
13314a2ae208SSatish Balay #undef __FUNCT__
13324a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
133397f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
133417ab2063SBarry Smith {
1335416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
133697f1f81fSBarry Smith   PetscInt   *itmp;
133717ab2063SBarry Smith 
13383a40ed3dSBarry Smith   PetscFunctionBegin;
133977431f27SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
134017ab2063SBarry Smith 
1341416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1342bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
134317ab2063SBarry Smith   if (idx) {
1344bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1345bfeeae90SHong Zhang     if (*nz) {
13464e093b46SBarry Smith       *idx = itmp;
134717ab2063SBarry Smith     }
134817ab2063SBarry Smith     else *idx = 0;
134917ab2063SBarry Smith   }
13503a40ed3dSBarry Smith   PetscFunctionReturn(0);
135117ab2063SBarry Smith }
135217ab2063SBarry Smith 
1353bfeeae90SHong Zhang /* remove this function? */
13544a2ae208SSatish Balay #undef __FUNCT__
13554a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
135697f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
135717ab2063SBarry Smith {
13583a40ed3dSBarry Smith   PetscFunctionBegin;
13593a40ed3dSBarry Smith   PetscFunctionReturn(0);
136017ab2063SBarry Smith }
136117ab2063SBarry Smith 
13624a2ae208SSatish Balay #undef __FUNCT__
13634a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1364dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
136517ab2063SBarry Smith {
1366416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
136787828ca2SBarry Smith   PetscScalar    *v = a->a;
136836db0b34SBarry Smith   PetscReal      sum = 0.0;
13696849ba73SBarry Smith   PetscErrorCode ierr;
137097f1f81fSBarry Smith   PetscInt       i,j;
137117ab2063SBarry Smith 
13723a40ed3dSBarry Smith   PetscFunctionBegin;
137317ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1374416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1375aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
137636db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
137717ab2063SBarry Smith #else
137817ab2063SBarry Smith       sum += (*v)*(*v); v++;
137917ab2063SBarry Smith #endif
138017ab2063SBarry Smith     }
1381064f8208SBarry Smith     *nrm = sqrt(sum);
13823a40ed3dSBarry Smith   } else if (type == NORM_1) {
138336db0b34SBarry Smith     PetscReal *tmp;
138497f1f81fSBarry Smith     PetscInt    *jj = a->j;
1385b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1386273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1387064f8208SBarry Smith     *nrm = 0.0;
1388416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1389bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
139017ab2063SBarry Smith     }
1391273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1392064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
139317ab2063SBarry Smith     }
1394606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13953a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1396064f8208SBarry Smith     *nrm = 0.0;
1397273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1398bfeeae90SHong Zhang       v = a->a + a->i[j];
139917ab2063SBarry Smith       sum = 0.0;
1400416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1401cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
140217ab2063SBarry Smith       }
1403064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
140417ab2063SBarry Smith     }
14053a40ed3dSBarry Smith   } else {
140629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
140717ab2063SBarry Smith   }
14083a40ed3dSBarry Smith   PetscFunctionReturn(0);
140917ab2063SBarry Smith }
141017ab2063SBarry Smith 
14114a2ae208SSatish Balay #undef __FUNCT__
14124a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1413dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B)
141417ab2063SBarry Smith {
1415416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1416416022c9SBarry Smith   Mat            C;
14176849ba73SBarry Smith   PetscErrorCode ierr;
141897f1f81fSBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->m,len,*col;
141987828ca2SBarry Smith   PetscScalar    *array = a->a;
142017ab2063SBarry Smith 
14213a40ed3dSBarry Smith   PetscFunctionBegin;
1422273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
142397f1f81fSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
142497f1f81fSBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(PetscInt));CHKERRQ(ierr);
1425bfeeae90SHong Zhang 
1426bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1427f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1428f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->n,m,A->n,m);CHKERRQ(ierr);
1429f204ca49SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1430ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1431606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
143217ab2063SBarry Smith   for (i=0; i<m; i++) {
143317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
143487d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1435b9b97703SBarry Smith     array += len;
1436b9b97703SBarry Smith     aj    += len;
143717ab2063SBarry Smith   }
143817ab2063SBarry Smith 
14396d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14406d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
144117ab2063SBarry Smith 
1442f1e2ffcdSBarry Smith   if (B) {
1443416022c9SBarry Smith     *B = C;
144417ab2063SBarry Smith   } else {
1445273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
144617ab2063SBarry Smith   }
14473a40ed3dSBarry Smith   PetscFunctionReturn(0);
144817ab2063SBarry Smith }
144917ab2063SBarry Smith 
1450cd0d46ebSvictorle EXTERN_C_BEGIN
1451cd0d46ebSvictorle #undef __FUNCT__
14525fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1453be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1454cd0d46ebSvictorle {
1455cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
145697f1f81fSBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
14576849ba73SBarry Smith   PetscErrorCode ierr;
145897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1459cd0d46ebSvictorle 
1460cd0d46ebSvictorle   PetscFunctionBegin;
1461cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1462cd0d46ebSvictorle 
1463cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1464cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
14655485867bSBarry Smith   if (ma!=nb || na!=mb){
14665485867bSBarry Smith     *f = PETSC_FALSE;
14675485867bSBarry Smith     PetscFunctionReturn(0);
14685485867bSBarry Smith   }
1469cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1470cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1471cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
147297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
147397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1474cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1475cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1476cd0d46ebSvictorle 
1477cd0d46ebSvictorle   *f = PETSC_TRUE;
1478cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1479cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
148097f1f81fSBarry Smith       PetscInt         idc,idr;
14815485867bSBarry Smith       PetscScalar vc,vr;
1482cd0d46ebSvictorle       /* column/row index/value */
14835485867bSBarry Smith       idc = adx[aptr[i]];
14845485867bSBarry Smith       idr = bdx[bptr[idc]];
14855485867bSBarry Smith       vc  = va[aptr[i]];
14865485867bSBarry Smith       vr  = vb[bptr[idc]];
14875485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
14885485867bSBarry Smith 	*f = PETSC_FALSE;
14895485867bSBarry Smith         goto done;
1490cd0d46ebSvictorle       } else {
14915485867bSBarry Smith 	aptr[i]++;
14925485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1493cd0d46ebSvictorle       }
1494cd0d46ebSvictorle     }
1495cd0d46ebSvictorle   }
1496cd0d46ebSvictorle  done:
1497cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
14983aeef889SHong Zhang   if (B) {
14993aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15003aeef889SHong Zhang   }
1501cd0d46ebSvictorle   PetscFunctionReturn(0);
1502cd0d46ebSvictorle }
1503cd0d46ebSvictorle EXTERN_C_END
1504cd0d46ebSvictorle 
15059e29f15eSvictorle #undef __FUNCT__
15069e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1507dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
15089e29f15eSvictorle {
1509dfbe8321SBarry Smith   PetscErrorCode ierr;
15109e29f15eSvictorle   PetscFunctionBegin;
15115485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
15129e29f15eSvictorle   PetscFunctionReturn(0);
15139e29f15eSvictorle }
15149e29f15eSvictorle 
15154a2ae208SSatish Balay #undef __FUNCT__
15164a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1517dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
151817ab2063SBarry Smith {
1519416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
152087828ca2SBarry Smith   PetscScalar    *l,*r,x,*v;
1521dfbe8321SBarry Smith   PetscErrorCode ierr;
152297f1f81fSBarry Smith   PetscInt       i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
152317ab2063SBarry Smith 
15243a40ed3dSBarry Smith   PetscFunctionBegin;
152517ab2063SBarry Smith   if (ll) {
15263ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
15273ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1528e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1529273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
15301ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1531416022c9SBarry Smith     v = a->a;
153217ab2063SBarry Smith     for (i=0; i<m; i++) {
153317ab2063SBarry Smith       x = l[i];
1534416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
153517ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
153617ab2063SBarry Smith     }
15371ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1538efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
153917ab2063SBarry Smith   }
154017ab2063SBarry Smith   if (rr) {
1541e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1542273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
15431ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1544416022c9SBarry Smith     v = a->a; jj = a->j;
154517ab2063SBarry Smith     for (i=0; i<nz; i++) {
1546bfeeae90SHong Zhang       (*v++) *= r[*jj++];
154717ab2063SBarry Smith     }
15481ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1549efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
155017ab2063SBarry Smith   }
15513a40ed3dSBarry Smith   PetscFunctionReturn(0);
155217ab2063SBarry Smith }
155317ab2063SBarry Smith 
15544a2ae208SSatish Balay #undef __FUNCT__
15554a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
155697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
155717ab2063SBarry Smith {
1558db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
15596849ba73SBarry Smith   PetscErrorCode ierr;
156097f1f81fSBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->n,*lens;
156197f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
156297f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
156397f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
156487828ca2SBarry Smith   PetscScalar    *a_new,*mat_a;
1565416022c9SBarry Smith   Mat            C;
1566fee21e36SBarry Smith   PetscTruth     stride;
156717ab2063SBarry Smith 
15683a40ed3dSBarry Smith   PetscFunctionBegin;
1569d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
157029bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1571d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
157229bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
157399141d43SSatish Balay 
157417ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1575b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1576b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
157717ab2063SBarry Smith 
1578fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1579fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1580fee21e36SBarry Smith   if (stride && step == 1) {
158102834360SBarry Smith     /* special case of contiguous rows */
158297f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
158331ebf83bSSatish Balay     starts = lens + nrows;
158402834360SBarry Smith     /* loop over new rows determining lens and starting points */
158502834360SBarry Smith     for (i=0; i<nrows; i++) {
1586bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1587a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
158802834360SBarry Smith       for (k=kstart; k<kend; k++) {
1589bfeeae90SHong Zhang         if (aj[k] >= first) {
159002834360SBarry Smith           starts[i] = k;
159102834360SBarry Smith           break;
159202834360SBarry Smith 	}
159302834360SBarry Smith       }
1594a2744918SBarry Smith       sum = 0;
159502834360SBarry Smith       while (k < kend) {
1596bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1597a2744918SBarry Smith         sum++;
159802834360SBarry Smith       }
1599a2744918SBarry Smith       lens[i] = sum;
160002834360SBarry Smith     }
160102834360SBarry Smith     /* create submatrix */
1602cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
160397f1f81fSBarry Smith       PetscInt n_cols,n_rows;
160408480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
160529bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1606d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
160708480c60SBarry Smith       C = *B;
16083a40ed3dSBarry Smith     } else {
1609f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1610f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1611e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1612ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
161308480c60SBarry Smith     }
1614db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1615db02288aSLois Curfman McInnes 
161602834360SBarry Smith     /* loop over rows inserting into submatrix */
1617db02288aSLois Curfman McInnes     a_new    = c->a;
1618db02288aSLois Curfman McInnes     j_new    = c->j;
1619db02288aSLois Curfman McInnes     i_new    = c->i;
1620bfeeae90SHong Zhang 
162102834360SBarry Smith     for (i=0; i<nrows; i++) {
1622a2744918SBarry Smith       ii    = starts[i];
1623a2744918SBarry Smith       lensi = lens[i];
1624a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1625a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
162602834360SBarry Smith       }
162787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1628a2744918SBarry Smith       a_new      += lensi;
1629a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1630a2744918SBarry Smith       c->ilen[i]  = lensi;
163102834360SBarry Smith     }
1632606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
16333a40ed3dSBarry Smith   } else {
163402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
163597f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1636bfeeae90SHong Zhang 
163797f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
163897f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
163917ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
164002834360SBarry Smith     /* determine lens of each row */
164102834360SBarry Smith     for (i=0; i<nrows; i++) {
1642bfeeae90SHong Zhang       kstart  = ai[irow[i]];
164302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
164402834360SBarry Smith       lens[i] = 0;
164502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1646bfeeae90SHong Zhang         if (smap[aj[k]]) {
164702834360SBarry Smith           lens[i]++;
164802834360SBarry Smith         }
164902834360SBarry Smith       }
165002834360SBarry Smith     }
165117ab2063SBarry Smith     /* Create and fill new matrix */
1652a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16530f5bd95cSBarry Smith       PetscTruth equal;
16540f5bd95cSBarry Smith 
165599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1656273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
165797f1f81fSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(PetscInt),&equal);CHKERRQ(ierr);
16580f5bd95cSBarry Smith       if (!equal) {
165929bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
166099141d43SSatish Balay       }
166197f1f81fSBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(PetscInt));CHKERRQ(ierr);
166208480c60SBarry Smith       C = *B;
16633a40ed3dSBarry Smith     } else {
1664f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1665f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1666e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1667ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
166808480c60SBarry Smith     }
166999141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
167017ab2063SBarry Smith     for (i=0; i<nrows; i++) {
167199141d43SSatish Balay       row    = irow[i];
1672bfeeae90SHong Zhang       kstart = ai[row];
167399141d43SSatish Balay       kend   = kstart + a->ilen[row];
1674bfeeae90SHong Zhang       mat_i  = c->i[i];
167599141d43SSatish Balay       mat_j  = c->j + mat_i;
167699141d43SSatish Balay       mat_a  = c->a + mat_i;
167799141d43SSatish Balay       mat_ilen = c->ilen + i;
167817ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1679bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1680ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
168199141d43SSatish Balay           *mat_a++ = a->a[k];
168299141d43SSatish Balay           (*mat_ilen)++;
168399141d43SSatish Balay 
168417ab2063SBarry Smith         }
168517ab2063SBarry Smith       }
168617ab2063SBarry Smith     }
168702834360SBarry Smith     /* Free work space */
168802834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1689606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1690606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
169102834360SBarry Smith   }
16926d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16936d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
169417ab2063SBarry Smith 
169517ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1696416022c9SBarry Smith   *B = C;
16973a40ed3dSBarry Smith   PetscFunctionReturn(0);
169817ab2063SBarry Smith }
169917ab2063SBarry Smith 
1700a871dcd8SBarry Smith /*
1701a871dcd8SBarry Smith */
17024a2ae208SSatish Balay #undef __FUNCT__
17034a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1704dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1705a871dcd8SBarry Smith {
170663b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1707dfbe8321SBarry Smith   PetscErrorCode ierr;
170863b91edcSBarry Smith   Mat            outA;
1709b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
171063b91edcSBarry Smith 
17113a40ed3dSBarry Smith   PetscFunctionBegin;
1712d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1713b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1714b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1715b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1716634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
1717b8a78c4aSBarry Smith   }
1718a871dcd8SBarry Smith 
171963b91edcSBarry Smith   outA          = inA;
172063b91edcSBarry Smith   inA->factor   = FACTOR_LU;
172163b91edcSBarry Smith   a->row        = row;
172263b91edcSBarry Smith   a->col        = col;
1723c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1724c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
172563b91edcSBarry Smith 
172636db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1727b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
17284c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
172952e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1730f0ec6fceSSatish Balay 
173194a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
173287828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
173394a9d846SBarry Smith   }
173463b91edcSBarry Smith 
173508480c60SBarry Smith   if (!a->diag) {
1736f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
173763b91edcSBarry Smith   }
1738af281ebdSHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
17393a40ed3dSBarry Smith   PetscFunctionReturn(0);
1740a871dcd8SBarry Smith }
1741a871dcd8SBarry Smith 
1742d9eff348SSatish Balay #include "petscblaslapack.h"
17434a2ae208SSatish Balay #undef __FUNCT__
17444a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1745f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1746f0b747eeSBarry Smith {
1747f0b747eeSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)inA->data;
17484ce68768SBarry Smith   PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1;
1749f4df32b1SMatthew Knepley   PetscScalar oalpha = alpha;
1750efee365bSSatish Balay   PetscErrorCode ierr;
1751efee365bSSatish Balay 
17523a40ed3dSBarry Smith 
17533a40ed3dSBarry Smith   PetscFunctionBegin;
1754f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1755efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17563a40ed3dSBarry Smith   PetscFunctionReturn(0);
1757f0b747eeSBarry Smith }
1758f0b747eeSBarry Smith 
17594a2ae208SSatish Balay #undef __FUNCT__
17604a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
176197f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1762cddf8d76SBarry Smith {
1763dfbe8321SBarry Smith   PetscErrorCode ierr;
176497f1f81fSBarry Smith   PetscInt       i;
1765cddf8d76SBarry Smith 
17663a40ed3dSBarry Smith   PetscFunctionBegin;
1767cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1768b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1769cddf8d76SBarry Smith   }
1770cddf8d76SBarry Smith 
1771cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17726a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1773cddf8d76SBarry Smith   }
17743a40ed3dSBarry Smith   PetscFunctionReturn(0);
1775cddf8d76SBarry Smith }
1776cddf8d76SBarry Smith 
17774a2ae208SSatish Balay #undef __FUNCT__
17784a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
177997f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
17804dcbc457SBarry Smith {
1781e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17826849ba73SBarry Smith   PetscErrorCode ierr;
178397f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
178497f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1785f1af5d2fSBarry Smith   PetscBT        table;
1786bbd702dbSSatish Balay 
17873a40ed3dSBarry Smith   PetscFunctionBegin;
1788273d9f13SBarry Smith   m     = A->m;
1789e4d965acSSatish Balay   ai    = a->i;
1790bfeeae90SHong Zhang   aj    = a->j;
17918a047759SSatish Balay 
1792a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
179306763907SSatish Balay 
179497f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
17956831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
179606763907SSatish Balay 
1797e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1798b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1799e4d965acSSatish Balay     isz  = 0;
18006831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1801e4d965acSSatish Balay 
1802e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
18034dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1804b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1805e4d965acSSatish Balay 
1806dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1807e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1808f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
18094dcbc457SBarry Smith     }
181006763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
181106763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1812e4d965acSSatish Balay 
181304a348a9SBarry Smith     k = 0;
181404a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
181504a348a9SBarry Smith       n = isz;
181606763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1817e4d965acSSatish Balay         row   = nidx[k];
1818e4d965acSSatish Balay         start = ai[row];
1819e4d965acSSatish Balay         end   = ai[row+1];
182004a348a9SBarry Smith         for (l = start; l<end ; l++){
1821efb16452SHong Zhang           val = aj[l] ;
1822f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1823e4d965acSSatish Balay         }
1824e4d965acSSatish Balay       }
1825e4d965acSSatish Balay     }
1826029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1827e4d965acSSatish Balay   }
18286831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1829606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
18303a40ed3dSBarry Smith   PetscFunctionReturn(0);
18314dcbc457SBarry Smith }
183217ab2063SBarry Smith 
18330513a670SBarry Smith /* -------------------------------------------------------------- */
18344a2ae208SSatish Balay #undef __FUNCT__
18354a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1836dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
18370513a670SBarry Smith {
18380513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18396849ba73SBarry Smith   PetscErrorCode ierr;
184097f1f81fSBarry Smith   PetscInt       i,nz,m = A->m,n = A->n,*col;
184197f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
184256cd22aeSBarry Smith   IS             icolp,irowp;
184397f1f81fSBarry Smith   PetscInt       *cwork;
184432ec9ce4SBarry Smith   PetscScalar    *vwork;
18450513a670SBarry Smith 
18463a40ed3dSBarry Smith   PetscFunctionBegin;
18474c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
184856cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18494c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
185056cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18510513a670SBarry Smith 
18520513a670SBarry Smith   /* determine lengths of permuted rows */
185397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
18540513a670SBarry Smith   for (i=0; i<m; i++) {
18550513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18560513a670SBarry Smith   }
1857f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,B);CHKERRQ(ierr);
1858f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
1859f204ca49SKris Buschelman   ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr);
1860ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1861606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18620513a670SBarry Smith 
186397f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
18640513a670SBarry Smith   for (i=0; i<m; i++) {
186532ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18660513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1867cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
186832ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18690513a670SBarry Smith   }
1870606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18713c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
18720513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18730513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
187456cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
187556cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
187656cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
187756cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18783a40ed3dSBarry Smith   PetscFunctionReturn(0);
18790513a670SBarry Smith }
18800513a670SBarry Smith 
18814a2ae208SSatish Balay #undef __FUNCT__
18824a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1883dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_SeqAIJ(Mat A)
1884682d7d0cSBarry Smith {
1885c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1886682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1887dfbe8321SBarry Smith   PetscErrorCode    ierr;
1888682d7d0cSBarry Smith 
18893a40ed3dSBarry Smith   PetscFunctionBegin;
18904846f1f5SKris Buschelman   ierr = MatPrintHelp_Inode(A);CHKERRQ(ierr);
1891c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1892d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1893d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1894d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
189573e7a558SHong Zhang   ierr = (*PetscHelpPrintf)(comm,"  -mat_no_compressedrow: Do not use compressedrow\n");CHKERRQ(ierr);
18963a40ed3dSBarry Smith   PetscFunctionReturn(0);
1897682d7d0cSBarry Smith }
189897304618SKris Buschelman 
18994a2ae208SSatish Balay #undef __FUNCT__
19004a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1901dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1902cb5b572fSBarry Smith {
1903dfbe8321SBarry Smith   PetscErrorCode ierr;
1904cb5b572fSBarry Smith 
1905cb5b572fSBarry Smith   PetscFunctionBegin;
190633f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
190733f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1908be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1909be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1910be6bf707SBarry Smith 
1911bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
1912634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1913cb5b572fSBarry Smith     }
1914bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1915cb5b572fSBarry Smith   } else {
1916cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1917cb5b572fSBarry Smith   }
1918cb5b572fSBarry Smith   PetscFunctionReturn(0);
1919cb5b572fSBarry Smith }
1920cb5b572fSBarry Smith 
19214a2ae208SSatish Balay #undef __FUNCT__
19224a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1923dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
1924273d9f13SBarry Smith {
1925dfbe8321SBarry Smith   PetscErrorCode ierr;
1926273d9f13SBarry Smith 
1927273d9f13SBarry Smith   PetscFunctionBegin;
1928ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1929273d9f13SBarry Smith   PetscFunctionReturn(0);
1930273d9f13SBarry Smith }
1931273d9f13SBarry Smith 
19324a2ae208SSatish Balay #undef __FUNCT__
19334a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
1934dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
19356c0721eeSBarry Smith {
19366c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19376c0721eeSBarry Smith   PetscFunctionBegin;
19386c0721eeSBarry Smith   *array = a->a;
19396c0721eeSBarry Smith   PetscFunctionReturn(0);
19406c0721eeSBarry Smith }
19416c0721eeSBarry Smith 
19424a2ae208SSatish Balay #undef __FUNCT__
19434a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
1944dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19456c0721eeSBarry Smith {
19466c0721eeSBarry Smith   PetscFunctionBegin;
19476c0721eeSBarry Smith   PetscFunctionReturn(0);
19486c0721eeSBarry Smith }
1949273d9f13SBarry Smith 
1950ee4f033dSBarry Smith #undef __FUNCT__
1951ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1952dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1953ee4f033dSBarry Smith {
19546849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
19556849ba73SBarry Smith   PetscErrorCode ierr;
195697f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
1957efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
195887828ca2SBarry Smith   PetscScalar    *vscale_array;
1959ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
1960ee4f033dSBarry Smith   Vec            w1,w2,w3;
1961ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
1962ee4f033dSBarry Smith   PetscTruth     flg;
1963ee4f033dSBarry Smith 
1964ee4f033dSBarry Smith   PetscFunctionBegin;
1965ee4f033dSBarry Smith   if (!coloring->w1) {
1966ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
196752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
1968ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
196952e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
1970ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
197152e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
1972ee4f033dSBarry Smith   }
1973ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1974ee4f033dSBarry Smith 
1975ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1976e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1977ee4f033dSBarry Smith   if (flg) {
197863ba0a88SBarry Smith     ierr = PetscLogInfo((coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"));CHKERRQ(ierr);
1979ee4f033dSBarry Smith   } else {
19800b9b6f31SBarry Smith     PetscTruth assembled;
19810b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
19820b9b6f31SBarry Smith     if (assembled) {
1983ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
1984ee4f033dSBarry Smith     }
19850b9b6f31SBarry Smith   }
1986ee4f033dSBarry Smith 
1987ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1988ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1989ee4f033dSBarry Smith 
1990ee4f033dSBarry Smith   /*
1991ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1992ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1993ee4f033dSBarry Smith   */
1994ee4f033dSBarry Smith   if (coloring->F) {
1995ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1996ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1997ee4f033dSBarry Smith     if (m1 != m2) {
1998ee4f033dSBarry Smith       coloring->F = 0;
1999ee4f033dSBarry Smith     }
2000ee4f033dSBarry Smith   }
2001ee4f033dSBarry Smith 
2002ee4f033dSBarry Smith   if (coloring->F) {
2003ee4f033dSBarry Smith     w1          = coloring->F;
2004ee4f033dSBarry Smith     coloring->F = 0;
2005ee4f033dSBarry Smith   } else {
200666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2007ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
200866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2009ee4f033dSBarry Smith   }
2010ee4f033dSBarry Smith 
2011ee4f033dSBarry Smith   /*
2012ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2013ee4f033dSBarry Smith   */
20141ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
20151ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2016ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2017ee4f033dSBarry Smith     /*
2018ee4f033dSBarry Smith        Loop over each column associated with color adding the
2019ee4f033dSBarry Smith        perturbation to the vector w3.
2020ee4f033dSBarry Smith     */
2021ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2022ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2023ee4f033dSBarry Smith       dx  = xx[col];
2024ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2025ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2026ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2027ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2028ee4f033dSBarry Smith #else
2029ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2030ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2031ee4f033dSBarry Smith #endif
2032ee4f033dSBarry Smith       dx                *= epsilon;
2033ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2034ee4f033dSBarry Smith     }
2035ee4f033dSBarry Smith   }
20361ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2037ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2038ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2039ee4f033dSBarry Smith 
2040ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2041ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2042ee4f033dSBarry Smith 
2043ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2044ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2045ee4f033dSBarry Smith 
20461ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2047ee4f033dSBarry Smith   /*
2048ee4f033dSBarry Smith       Loop over each color
2049ee4f033dSBarry Smith   */
2050ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
205149b058dcSBarry Smith     coloring->currentcolor = k;
2052ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
20531ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2054ee4f033dSBarry Smith     /*
2055ee4f033dSBarry Smith        Loop over each column associated with color adding the
2056ee4f033dSBarry Smith        perturbation to the vector w3.
2057ee4f033dSBarry Smith     */
2058ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2059ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2060ee4f033dSBarry Smith       dx  = xx[col];
20615b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2062ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2063ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2064ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2065ee4f033dSBarry Smith #else
2066ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2067ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2068ee4f033dSBarry Smith #endif
2069ee4f033dSBarry Smith       dx            *= epsilon;
2070634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2071ee4f033dSBarry Smith       w3_array[col] += dx;
2072ee4f033dSBarry Smith     }
20731ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2074ee4f033dSBarry Smith 
2075ee4f033dSBarry Smith     /*
2076ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2077ee4f033dSBarry Smith     */
2078ee4f033dSBarry Smith 
207966f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2080ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
208166f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2082efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2083ee4f033dSBarry Smith 
2084ee4f033dSBarry Smith     /*
2085ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2086ee4f033dSBarry Smith     */
20871ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2088ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2089ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2090ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2091ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2092ee4f033dSBarry Smith       srow   = row + start;
2093ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2094ee4f033dSBarry Smith     }
20951ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2096ee4f033dSBarry Smith   }
209749b058dcSBarry Smith   coloring->currentcolor = k;
20981ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
20991ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2100ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2101ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2102ee4f033dSBarry Smith   PetscFunctionReturn(0);
2103ee4f033dSBarry Smith }
2104ee4f033dSBarry Smith 
2105ac90fabeSBarry Smith #include "petscblaslapack.h"
2106ac90fabeSBarry Smith #undef __FUNCT__
2107ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2108f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2109ac90fabeSBarry Smith {
2110dfbe8321SBarry Smith   PetscErrorCode ierr;
211197f1f81fSBarry Smith   PetscInt       i;
2112ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
21134ce68768SBarry Smith   PetscBLASInt   one=1,bnz = (PetscBLASInt)x->nz;
2114ac90fabeSBarry Smith 
2115ac90fabeSBarry Smith   PetscFunctionBegin;
2116ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2117f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2118f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2119c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2120a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2121a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2122a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2123a30b2313SHong Zhang     }
2124a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
212524f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2126a30b2313SHong Zhang       y->XtoY = X;
2127c537a176SHong Zhang     }
2128f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
212963ba0a88SBarry 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);
2130ac90fabeSBarry Smith   } else {
2131f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2132ac90fabeSBarry Smith   }
2133ac90fabeSBarry Smith   PetscFunctionReturn(0);
2134ac90fabeSBarry Smith }
2135ac90fabeSBarry Smith 
2136521d7252SBarry Smith #undef __FUNCT__
2137521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2138521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2139521d7252SBarry Smith {
2140521d7252SBarry Smith   PetscFunctionBegin;
2141521d7252SBarry Smith   PetscFunctionReturn(0);
2142521d7252SBarry Smith }
2143521d7252SBarry Smith 
2144354c94deSBarry Smith #undef __FUNCT__
2145354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2146354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2147354c94deSBarry Smith {
2148354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2149354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2150354c94deSBarry Smith   PetscInt    i,nz;
2151354c94deSBarry Smith   PetscScalar *a;
2152354c94deSBarry Smith 
2153354c94deSBarry Smith   PetscFunctionBegin;
2154354c94deSBarry Smith   nz = aij->nz;
2155354c94deSBarry Smith   a  = aij->a;
2156354c94deSBarry Smith   for (i=0; i<nz; i++) {
2157354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2158354c94deSBarry Smith   }
2159354c94deSBarry Smith #else
2160354c94deSBarry Smith   PetscFunctionBegin;
2161354c94deSBarry Smith #endif
2162354c94deSBarry Smith   PetscFunctionReturn(0);
2163354c94deSBarry Smith }
2164354c94deSBarry Smith 
2165e34fafa9SBarry Smith #undef __FUNCT__
2166e34fafa9SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2167e34fafa9SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v)
2168e34fafa9SBarry Smith {
2169e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2170e34fafa9SBarry Smith   PetscErrorCode ierr;
2171e34fafa9SBarry Smith   PetscInt       i,j,m = A->m,*ai,*aj,ncols,n;
2172e34fafa9SBarry Smith   PetscReal      atmp;
2173e34fafa9SBarry Smith   PetscScalar    *x,zero = 0.0;
2174e34fafa9SBarry Smith   MatScalar      *aa;
2175e34fafa9SBarry Smith 
2176e34fafa9SBarry Smith   PetscFunctionBegin;
2177e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2178e34fafa9SBarry Smith   aa   = a->a;
2179e34fafa9SBarry Smith   ai   = a->i;
2180e34fafa9SBarry Smith   aj   = a->j;
2181e34fafa9SBarry Smith 
218243e18e04SHong Zhang   ierr = VecSet(v,zero);CHKERRQ(ierr);
2183e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2184e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2185e34fafa9SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2186e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2187e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2188e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2189e34fafa9SBarry Smith       atmp = PetscAbsScalar(*aa); aa++;
2190e34fafa9SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) x[i] = atmp;
2191e34fafa9SBarry Smith       aj++;
2192e34fafa9SBarry Smith     }
2193e34fafa9SBarry Smith   }
2194e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2195e34fafa9SBarry Smith   PetscFunctionReturn(0);
2196e34fafa9SBarry Smith }
2197e34fafa9SBarry Smith 
2198682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
21990a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2200cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2201cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2202cb5b572fSBarry Smith        MatMult_SeqAIJ,
220397304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
22047c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
22057c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2206cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2207cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
22087c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
220997304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2210cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2211cb5b572fSBarry Smith        0,
221217ab2063SBarry Smith        MatRelax_SeqAIJ,
221317ab2063SBarry Smith        MatTranspose_SeqAIJ,
221497304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2215cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2216cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2217cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2218cb5b572fSBarry Smith        MatNorm_SeqAIJ,
221997304618SKris Buschelman /*20*/ 0,
2220cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
222117ab2063SBarry Smith        MatCompress_SeqAIJ,
2222cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2223cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
222497304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2225cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2226cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2227f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2228a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
222997304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2230cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2231861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
22326c0721eeSBarry Smith        MatGetArray_SeqAIJ,
22336c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
223497304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2235cb5b572fSBarry Smith        0,
2236cb5b572fSBarry Smith        0,
2237cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2238cb5b572fSBarry Smith        0,
223997304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2240cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2241cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2242cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2243cb5b572fSBarry Smith        MatCopy_SeqAIJ,
224497304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ,
2245cb5b572fSBarry Smith        MatScale_SeqAIJ,
2246cb5b572fSBarry Smith        0,
224779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
22486945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2249521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
22503b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
22513b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
22523b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2253a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
225497304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2255b9617806SBarry Smith        0,
22560513a670SBarry Smith        0,
2257cda55fadSBarry Smith        MatPermute_SeqAIJ,
2258cda55fadSBarry Smith        0,
225997304618SKris Buschelman /*60*/ 0,
2260b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2261b9b97703SBarry Smith        MatView_SeqAIJ,
22628a124369SBarry Smith        MatGetPetscMaps_Petsc,
2263ee4f033dSBarry Smith        0,
226497304618SKris Buschelman /*65*/ 0,
2265ee4f033dSBarry Smith        0,
2266ee4f033dSBarry Smith        0,
2267ee4f033dSBarry Smith        0,
2268ee4f033dSBarry Smith        0,
2269e34fafa9SBarry Smith /*70*/ MatGetRowMax_SeqAIJ,
2270ee4f033dSBarry Smith        0,
2271ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2272dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2273ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2274dcf5cc72SBarry Smith #else
2275dcf5cc72SBarry Smith        0,
2276dcf5cc72SBarry Smith #endif
2277ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
227897304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ,
227997304618SKris Buschelman        0,
228097304618SKris Buschelman        0,
228197304618SKris Buschelman        0,
228297304618SKris Buschelman        0,
228397304618SKris Buschelman /*80*/ 0,
228497304618SKris Buschelman        0,
228597304618SKris Buschelman        0,
228697304618SKris Buschelman        0,
2287bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2288bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
22896284ec50SHong Zhang        0,
22906284ec50SHong Zhang        0,
22916284ec50SHong Zhang        0,
2292bc011b1eSHong Zhang        0,
2293bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
229426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
229526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2296d439da42SKris Buschelman        MatPtAP_Basic,
22977ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
22987ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2299bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2300bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2301bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
23027ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
23037ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2304609c6c4dSKris Buschelman        0,
2305609c6c4dSKris Buschelman        0,
230687d4246cSBarry Smith        MatConjugate_SeqAIJ,
230787d4246cSBarry Smith        0,
230887d4246cSBarry Smith /*105*/MatSetValuesRow_SeqAIJ
23099e29f15eSvictorle };
231017ab2063SBarry Smith 
2311fb2e594dSBarry Smith EXTERN_C_BEGIN
23124a2ae208SSatish Balay #undef __FUNCT__
23134a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2314be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2315bef8e0ddSBarry Smith {
2316bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
231797f1f81fSBarry Smith   PetscInt   i,nz,n;
2318bef8e0ddSBarry Smith 
2319bef8e0ddSBarry Smith   PetscFunctionBegin;
2320bef8e0ddSBarry Smith 
2321bef8e0ddSBarry Smith   nz = aij->maxnz;
2322273d9f13SBarry Smith   n  = mat->n;
2323bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2324bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2325bef8e0ddSBarry Smith   }
2326bef8e0ddSBarry Smith   aij->nz = nz;
2327bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2328bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2329bef8e0ddSBarry Smith   }
2330bef8e0ddSBarry Smith 
2331bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2332bef8e0ddSBarry Smith }
2333fb2e594dSBarry Smith EXTERN_C_END
2334bef8e0ddSBarry Smith 
23354a2ae208SSatish Balay #undef __FUNCT__
23364a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2337bef8e0ddSBarry Smith /*@
2338bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2339bef8e0ddSBarry Smith        in the matrix.
2340bef8e0ddSBarry Smith 
2341bef8e0ddSBarry Smith   Input Parameters:
2342bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2343bef8e0ddSBarry Smith -  indices - the column indices
2344bef8e0ddSBarry Smith 
234515091d37SBarry Smith   Level: advanced
234615091d37SBarry Smith 
2347bef8e0ddSBarry Smith   Notes:
2348bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2349bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2350bef8e0ddSBarry Smith   of the MatSetValues() operation.
2351bef8e0ddSBarry Smith 
2352bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2353d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2354bef8e0ddSBarry Smith 
2355bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2356bef8e0ddSBarry Smith 
2357b9617806SBarry Smith     The indices should start with zero, not one.
2358b9617806SBarry Smith 
2359bef8e0ddSBarry Smith @*/
2360be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2361bef8e0ddSBarry Smith {
236297f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2363bef8e0ddSBarry Smith 
2364bef8e0ddSBarry Smith   PetscFunctionBegin;
23654482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
23664482741eSBarry Smith   PetscValidPointer(indices,2);
2367c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2368bef8e0ddSBarry Smith   if (f) {
2369bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2370bef8e0ddSBarry Smith   } else {
2371634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2372bef8e0ddSBarry Smith   }
2373bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2374bef8e0ddSBarry Smith }
2375bef8e0ddSBarry Smith 
2376be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2377be6bf707SBarry Smith 
2378fb2e594dSBarry Smith EXTERN_C_BEGIN
23794a2ae208SSatish Balay #undef __FUNCT__
23804a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2381be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2382be6bf707SBarry Smith {
2383be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23846849ba73SBarry Smith   PetscErrorCode ierr;
23856849ba73SBarry Smith   size_t         nz = aij->i[mat->m];
2386be6bf707SBarry Smith 
2387be6bf707SBarry Smith   PetscFunctionBegin;
2388be6bf707SBarry Smith   if (aij->nonew != 1) {
2389634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2390be6bf707SBarry Smith   }
2391be6bf707SBarry Smith 
2392be6bf707SBarry Smith   /* allocate space for values if not already there */
2393be6bf707SBarry Smith   if (!aij->saved_values) {
239487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2395be6bf707SBarry Smith   }
2396be6bf707SBarry Smith 
2397be6bf707SBarry Smith   /* copy values over */
239887828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2399be6bf707SBarry Smith   PetscFunctionReturn(0);
2400be6bf707SBarry Smith }
2401fb2e594dSBarry Smith EXTERN_C_END
2402be6bf707SBarry Smith 
24034a2ae208SSatish Balay #undef __FUNCT__
2404b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2405be6bf707SBarry Smith /*@
2406be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2407be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2408be6bf707SBarry Smith        nonlinear portion.
2409be6bf707SBarry Smith 
2410be6bf707SBarry Smith    Collect on Mat
2411be6bf707SBarry Smith 
2412be6bf707SBarry Smith   Input Parameters:
24130e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2414be6bf707SBarry Smith 
241515091d37SBarry Smith   Level: advanced
241615091d37SBarry Smith 
2417be6bf707SBarry Smith   Common Usage, with SNESSolve():
2418be6bf707SBarry Smith $    Create Jacobian matrix
2419be6bf707SBarry Smith $    Set linear terms into matrix
2420be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2421be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2422be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2423be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2424be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2425be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2426be6bf707SBarry Smith $    In your Jacobian routine
2427be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2428be6bf707SBarry Smith $      Set nonlinear terms in matrix
2429be6bf707SBarry Smith 
2430be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2431be6bf707SBarry Smith $    // build linear portion of Jacobian
2432be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2433be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2434be6bf707SBarry Smith $    loop over nonlinear iterations
2435be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2436be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2437be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2438be6bf707SBarry Smith $       Solve linear system with Jacobian
2439be6bf707SBarry Smith $    endloop
2440be6bf707SBarry Smith 
2441be6bf707SBarry Smith   Notes:
2442be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2443be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2444be6bf707SBarry Smith     calling this routine.
2445be6bf707SBarry Smith 
24460c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
24470c468ba9SBarry Smith     and does not allocated additional space.
24480c468ba9SBarry Smith 
2449be6bf707SBarry Smith .seealso: MatRetrieveValues()
2450be6bf707SBarry Smith 
2451be6bf707SBarry Smith @*/
2452be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2453be6bf707SBarry Smith {
2454dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2455be6bf707SBarry Smith 
2456be6bf707SBarry Smith   PetscFunctionBegin;
24574482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
245829bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
245929bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2460be6bf707SBarry Smith 
2461c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2462be6bf707SBarry Smith   if (f) {
2463be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2464be6bf707SBarry Smith   } else {
2465634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2466be6bf707SBarry Smith   }
2467be6bf707SBarry Smith   PetscFunctionReturn(0);
2468be6bf707SBarry Smith }
2469be6bf707SBarry Smith 
2470fb2e594dSBarry Smith EXTERN_C_BEGIN
24714a2ae208SSatish Balay #undef __FUNCT__
24724a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2473be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2474be6bf707SBarry Smith {
2475be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
24766849ba73SBarry Smith   PetscErrorCode ierr;
247797f1f81fSBarry Smith   PetscInt       nz = aij->i[mat->m];
2478be6bf707SBarry Smith 
2479be6bf707SBarry Smith   PetscFunctionBegin;
2480be6bf707SBarry Smith   if (aij->nonew != 1) {
2481634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2482be6bf707SBarry Smith   }
2483be6bf707SBarry Smith   if (!aij->saved_values) {
2484634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2485be6bf707SBarry Smith   }
2486be6bf707SBarry Smith   /* copy values over */
248787828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2488be6bf707SBarry Smith   PetscFunctionReturn(0);
2489be6bf707SBarry Smith }
2490fb2e594dSBarry Smith EXTERN_C_END
2491be6bf707SBarry Smith 
24924a2ae208SSatish Balay #undef __FUNCT__
24934a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2494be6bf707SBarry Smith /*@
2495be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2496be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2497be6bf707SBarry Smith        nonlinear portion.
2498be6bf707SBarry Smith 
2499be6bf707SBarry Smith    Collect on Mat
2500be6bf707SBarry Smith 
2501be6bf707SBarry Smith   Input Parameters:
2502be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2503be6bf707SBarry Smith 
250415091d37SBarry Smith   Level: advanced
250515091d37SBarry Smith 
2506be6bf707SBarry Smith .seealso: MatStoreValues()
2507be6bf707SBarry Smith 
2508be6bf707SBarry Smith @*/
2509be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2510be6bf707SBarry Smith {
2511dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2512be6bf707SBarry Smith 
2513be6bf707SBarry Smith   PetscFunctionBegin;
25144482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
251529bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
251629bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2517be6bf707SBarry Smith 
2518c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2519be6bf707SBarry Smith   if (f) {
2520be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2521be6bf707SBarry Smith   } else {
2522634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2523be6bf707SBarry Smith   }
2524be6bf707SBarry Smith   PetscFunctionReturn(0);
2525be6bf707SBarry Smith }
2526be6bf707SBarry Smith 
2527f83d6046SBarry Smith 
2528be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
25294a2ae208SSatish Balay #undef __FUNCT__
25304a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
253117ab2063SBarry Smith /*@C
2532682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
25330d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
25346e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
253551c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
25362bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
253717ab2063SBarry Smith 
2538db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2539db81eaa0SLois Curfman McInnes 
254017ab2063SBarry Smith    Input Parameters:
2541db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
254217ab2063SBarry Smith .  m - number of rows
254317ab2063SBarry Smith .  n - number of columns
254417ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
254551c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
25462bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
254717ab2063SBarry Smith 
254817ab2063SBarry Smith    Output Parameter:
2549416022c9SBarry Smith .  A - the matrix
255017ab2063SBarry Smith 
2551b259b22eSLois Curfman McInnes    Notes:
255249a6f317SBarry Smith    If nnz is given then nz is ignored
255349a6f317SBarry Smith 
255417ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
255517ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
25560002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
255744cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
255817ab2063SBarry Smith 
255917ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2560a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
25613d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
25626da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
256317ab2063SBarry Smith 
2564682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
25654fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2566682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
25676c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
25686c7ebb05SLois Curfman McInnes 
25696c7ebb05SLois Curfman McInnes    Options Database Keys:
2570698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2571698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2572db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2573db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2574db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
257517ab2063SBarry Smith 
2576027ccd11SLois Curfman McInnes    Level: intermediate
2577027ccd11SLois Curfman McInnes 
257836db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
257936db0b34SBarry Smith 
258017ab2063SBarry Smith @*/
2581be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
258217ab2063SBarry Smith {
2583dfbe8321SBarry Smith   PetscErrorCode ierr;
25846945ee14SBarry Smith 
25853a40ed3dSBarry Smith   PetscFunctionBegin;
2586f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2587f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2588273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2589ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2590273d9f13SBarry Smith   PetscFunctionReturn(0);
2591273d9f13SBarry Smith }
2592273d9f13SBarry Smith 
25934a2ae208SSatish Balay #undef __FUNCT__
25944a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2595273d9f13SBarry Smith /*@C
2596273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2597273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2598273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2599273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2600273d9f13SBarry Smith 
2601273d9f13SBarry Smith    Collective on MPI_Comm
2602273d9f13SBarry Smith 
2603273d9f13SBarry Smith    Input Parameters:
260445bfc511SMatthew Knepley +  B - The matrix
2605273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2606273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2607273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2608273d9f13SBarry Smith 
2609273d9f13SBarry Smith    Notes:
261049a6f317SBarry Smith      If nnz is given then nz is ignored
261149a6f317SBarry Smith 
2612273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2613273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2614273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2615273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2616273d9f13SBarry Smith 
2617273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2618273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2619273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2620273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2621273d9f13SBarry Smith 
2622a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2623a96a251dSBarry Smith    entries or columns indices
2624a96a251dSBarry Smith 
2625273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2626273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2627273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2628273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2629273d9f13SBarry Smith 
2630273d9f13SBarry Smith    Options Database Keys:
2631698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2632698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2633273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2634273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2635273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2636273d9f13SBarry Smith 
2637273d9f13SBarry Smith    Level: intermediate
2638273d9f13SBarry Smith 
2639273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2640273d9f13SBarry Smith 
2641273d9f13SBarry Smith @*/
2642be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2643273d9f13SBarry Smith {
264497f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2645a23d5eceSKris Buschelman 
2646a23d5eceSKris Buschelman   PetscFunctionBegin;
2647a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2648a23d5eceSKris Buschelman   if (f) {
2649a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2650a23d5eceSKris Buschelman   }
2651a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2652a23d5eceSKris Buschelman }
2653a23d5eceSKris Buschelman 
2654a23d5eceSKris Buschelman EXTERN_C_BEGIN
2655a23d5eceSKris Buschelman #undef __FUNCT__
2656a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2657be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2658a23d5eceSKris Buschelman {
2659273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2660a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
26616849ba73SBarry Smith   PetscErrorCode ierr;
266297f1f81fSBarry Smith   PetscInt       i;
2663273d9f13SBarry Smith 
2664273d9f13SBarry Smith   PetscFunctionBegin;
2665d5d45c9bSBarry Smith 
2666a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2667c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2668c461c341SBarry Smith     nz             = 0;
2669c461c341SBarry Smith   }
2670c461c341SBarry Smith 
2671435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2672435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2673b73539f3SBarry Smith   if (nnz) {
2674273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
267529bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
26763a7fca6bSBarry 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);
2677b73539f3SBarry Smith     }
2678b73539f3SBarry Smith   }
2679b73539f3SBarry Smith 
2680273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2681273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2682273d9f13SBarry Smith 
2683ab93d7beSBarry Smith   if (!skipallocation) {
2684ab93d7beSBarry Smith     ierr = PetscMalloc2(B->m,PetscInt,&b->imax,B->m,PetscInt,&b->ilen);CHKERRQ(ierr);
2685273d9f13SBarry Smith     if (!nnz) {
2686435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2687273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2688273d9f13SBarry Smith       for (i=0; i<B->m; i++) b->imax[i] = nz;
2689273d9f13SBarry Smith       nz = nz*B->m;
2690273d9f13SBarry Smith     } else {
2691273d9f13SBarry Smith       nz = 0;
2692273d9f13SBarry Smith       for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2693273d9f13SBarry Smith     }
2694273d9f13SBarry Smith 
2695ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2696ab93d7beSBarry Smith     for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2697ab93d7beSBarry Smith 
2698273d9f13SBarry Smith     /* allocate the matrix space */
2699a96a251dSBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->m+1,PetscInt,&b->i);CHKERRQ(ierr);
2700bfeeae90SHong Zhang     b->i[0] = 0;
27015da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
27025da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
27035da197adSKris Buschelman     }
2704273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2705273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2706c461c341SBarry Smith   } else {
2707c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2708c461c341SBarry Smith   }
2709273d9f13SBarry Smith 
2710273d9f13SBarry Smith   b->nz                = 0;
2711273d9f13SBarry Smith   b->maxnz             = nz;
2712273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2713273d9f13SBarry Smith   PetscFunctionReturn(0);
2714273d9f13SBarry Smith }
2715a23d5eceSKris Buschelman EXTERN_C_END
2716273d9f13SBarry Smith 
2717a1661176SMatthew Knepley #undef  __FUNCT__
2718a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
2719a1661176SMatthew Knepley /*@C
2720a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2721a1661176SMatthew Knepley 
2722a1661176SMatthew Knepley    Input Parameters:
2723a1661176SMatthew Knepley +  B - the matrix
2724a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2725a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2726a1661176SMatthew Knepley -  v - optional values in the matrix
2727a1661176SMatthew Knepley 
2728d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
2729d58b2322SMatthew Knepley 
2730a1661176SMatthew Knepley    Level: developer
2731a1661176SMatthew Knepley 
2732a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2733a1661176SMatthew Knepley 
2734a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
2735a1661176SMatthew Knepley @*/
2736a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
2737a1661176SMatthew Knepley {
2738a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2739a1661176SMatthew Knepley   PetscErrorCode ierr;
2740a1661176SMatthew Knepley 
2741a1661176SMatthew Knepley   PetscFunctionBegin;
2742a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
2743a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2744a1661176SMatthew Knepley   if (f) {
2745a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2746a1661176SMatthew Knepley   }
2747a1661176SMatthew Knepley   PetscFunctionReturn(0);
2748a1661176SMatthew Knepley }
2749a1661176SMatthew Knepley 
2750a1661176SMatthew Knepley EXTERN_C_BEGIN
2751a1661176SMatthew Knepley #undef  __FUNCT__
2752a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
2753a1661176SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt I[],const PetscInt J[],const PetscScalar v[])
2754a1661176SMatthew Knepley {
2755a1661176SMatthew Knepley   PetscInt       i;
2756a1661176SMatthew Knepley   PetscInt       m,n;
2757a1661176SMatthew Knepley   PetscInt       nz;
2758a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
2759a1661176SMatthew Knepley   PetscScalar    *values;
2760a1661176SMatthew Knepley   PetscErrorCode ierr;
2761a1661176SMatthew Knepley 
2762a1661176SMatthew Knepley   PetscFunctionBegin;
2763a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
2764a1661176SMatthew Knepley 
2765a1661176SMatthew Knepley   if (I[0]) {
2766a1661176SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "I[0] must be 0 it is %D", I[0]);
2767a1661176SMatthew Knepley   }
2768a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
2769a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2770a1661176SMatthew Knepley     nz     = I[i+1]- I[i];
2771a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
2772a1661176SMatthew Knepley     if (nz < 0) {
2773a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
2774a1661176SMatthew Knepley     }
2775a1661176SMatthew Knepley     nnz[i] = nz;
2776a1661176SMatthew Knepley   }
2777a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
2778a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
2779a1661176SMatthew Knepley 
2780a1661176SMatthew Knepley   if (v) {
2781a1661176SMatthew Knepley     values = (PetscScalar*) v;
2782a1661176SMatthew Knepley   } else {
2783a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
2784a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2785a1661176SMatthew Knepley   }
2786a1661176SMatthew Knepley 
2787a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2788a1661176SMatthew Knepley 
2789a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2790a1661176SMatthew Knepley     nz  = I[i+1] - I[i];
2791a1661176SMatthew Knepley     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+I[i], values + (v ? I[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
2792a1661176SMatthew Knepley   }
2793a1661176SMatthew Knepley 
2794a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2795a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2796a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_UNSORTED);CHKERRQ(ierr);
2797a1661176SMatthew Knepley 
2798a1661176SMatthew Knepley   if (!v) {
2799a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
2800a1661176SMatthew Knepley   }
2801a1661176SMatthew Knepley   PetscFunctionReturn(0);
2802a1661176SMatthew Knepley }
2803a1661176SMatthew Knepley EXTERN_C_END
2804a1661176SMatthew Knepley 
28050bad9183SKris Buschelman /*MC
2806fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
28070bad9183SKris Buschelman    based on compressed sparse row format.
28080bad9183SKris Buschelman 
28090bad9183SKris Buschelman    Options Database Keys:
28100bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
28110bad9183SKris Buschelman 
28120bad9183SKris Buschelman   Level: beginner
28130bad9183SKris Buschelman 
2814f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
28150bad9183SKris Buschelman M*/
28160bad9183SKris Buschelman 
2817a6175056SHong Zhang EXTERN_C_BEGIN
28184a2ae208SSatish Balay #undef __FUNCT__
28194a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2820be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
2821273d9f13SBarry Smith {
2822273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2823dfbe8321SBarry Smith   PetscErrorCode ierr;
282438baddfdSBarry Smith   PetscMPIInt    size;
2825273d9f13SBarry Smith 
2826273d9f13SBarry Smith   PetscFunctionBegin;
2827273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2828273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2829273d9f13SBarry Smith 
2830273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2831273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2832273d9f13SBarry Smith 
2833b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2834b0a32e0cSBarry Smith   B->data             = (void*)b;
2835549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2836416022c9SBarry Smith   B->factor           = 0;
283790f02eecSBarry Smith   B->mapping          = 0;
2838416022c9SBarry Smith   b->row              = 0;
2839416022c9SBarry Smith   b->col              = 0;
284082bf6240SBarry Smith   b->icol             = 0;
2841b810aeb4SBarry Smith   b->reallocs         = 0;
284217ab2063SBarry Smith 
28438a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
28448a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2845a5ae1ecdSBarry Smith 
2846f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
284736db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2848f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2849416022c9SBarry Smith   b->nonew             = 0;
2850416022c9SBarry Smith   b->diag              = 0;
2851416022c9SBarry Smith   b->solve_work        = 0;
28522a1b7f2aSHong Zhang   B->spptr             = 0;
2853be6bf707SBarry Smith   b->saved_values      = 0;
2854d7f994e1SBarry Smith   b->idiag             = 0;
2855d7f994e1SBarry Smith   b->ssor              = 0;
2856f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2857a30b2313SHong Zhang   b->xtoy              = 0;
2858a30b2313SHong Zhang   b->XtoY              = 0;
285973e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
2860d487561eSHong Zhang   b->compressedrow.nrows   = B->m;
2861d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
2862d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
2863d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
286488e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
286517ab2063SBarry Smith 
286635d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
286735d8aa7fSBarry Smith 
2868f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2869bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2870bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2871f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2872be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2873bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2874f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2875be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2876bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2877a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2878a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2879a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
288085fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
288185fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
288285fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
28835fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
28845fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
28855fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2886a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2887a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2888a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
2889a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
2890a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
2891a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
289205b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
289305b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
289405b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
28954846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
28963a40ed3dSBarry Smith   PetscFunctionReturn(0);
289717ab2063SBarry Smith }
2898273d9f13SBarry Smith EXTERN_C_END
289917ab2063SBarry Smith 
29004a2ae208SSatish Balay #undef __FUNCT__
29014a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2902dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
290317ab2063SBarry Smith {
2904416022c9SBarry Smith   Mat            C;
2905416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
29066849ba73SBarry Smith   PetscErrorCode ierr;
290797f1f81fSBarry Smith   PetscInt       i,m = A->m;
290817ab2063SBarry Smith 
29093a40ed3dSBarry Smith   PetscFunctionBegin;
29104043dd9cSLois Curfman McInnes   *B = 0;
2911f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
2912f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->m,A->n,A->m,A->n);CHKERRQ(ierr);
2913be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
29141d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
29151d5dac46SHong Zhang 
2916273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
2917273d9f13SBarry Smith 
2918416022c9SBarry Smith   C->factor           = A->factor;
29196ad4291fSHong Zhang 
2920416022c9SBarry Smith   c->row            = 0;
2921416022c9SBarry Smith   c->col            = 0;
292282bf6240SBarry Smith   c->icol           = 0;
29236ad4291fSHong Zhang   c->reallocs       = 0;
292417ab2063SBarry Smith 
29256ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
292617ab2063SBarry Smith 
292733b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
292817ab2063SBarry Smith   for (i=0; i<m; i++) {
2929416022c9SBarry Smith     c->imax[i] = a->imax[i];
2930416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
293117ab2063SBarry Smith   }
293217ab2063SBarry Smith 
293317ab2063SBarry Smith   /* allocate the matrix space */
2934a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
2935f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
293697f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
293717ab2063SBarry Smith   if (m > 0) {
293897f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
2939be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2940bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2941be6bf707SBarry Smith     } else {
2942bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
294317ab2063SBarry Smith     }
294408480c60SBarry Smith   }
294517ab2063SBarry Smith 
2946416022c9SBarry Smith   c->sorted            = a->sorted;
29476ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
2948416022c9SBarry Smith   c->roworiented       = a->roworiented;
2949416022c9SBarry Smith   c->nonew             = a->nonew;
2950416022c9SBarry Smith   if (a->diag) {
295197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
295252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
295317ab2063SBarry Smith     for (i=0; i<m; i++) {
2954416022c9SBarry Smith       c->diag[i] = a->diag[i];
295517ab2063SBarry Smith     }
29563a40ed3dSBarry Smith   } else c->diag        = 0;
29576ad4291fSHong Zhang   c->solve_work         = 0;
29586ad4291fSHong Zhang   c->saved_values          = 0;
29596ad4291fSHong Zhang   c->idiag                 = 0;
29606ad4291fSHong Zhang   c->ssor                  = 0;
29616ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
29626ad4291fSHong Zhang   c->freedata              = PETSC_TRUE;
29636ad4291fSHong Zhang   c->xtoy                  = 0;
29646ad4291fSHong Zhang   c->XtoY                  = 0;
29656ad4291fSHong Zhang 
2966416022c9SBarry Smith   c->nz                 = a->nz;
2967416022c9SBarry Smith   c->maxnz              = a->maxnz;
2968273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2969754ec7b1SSatish Balay 
29706ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
29716ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
29726ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
29736ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
29746ad4291fSHong Zhang     i = a->compressedrow.nrows;
29756ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
29766ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
29776ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
29786ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
297927ea64f8SHong Zhang   } else {
298027ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
298127ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
298227ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
29836ad4291fSHong Zhang   }
298488e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
29856ad4291fSHong Zhang 
29864846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
29874846f1f5SKris Buschelman 
2988416022c9SBarry Smith   *B = C;
2989b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
29903a40ed3dSBarry Smith   PetscFunctionReturn(0);
299117ab2063SBarry Smith }
299217ab2063SBarry Smith 
29934a2ae208SSatish Balay #undef __FUNCT__
29944a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2995f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A)
299617ab2063SBarry Smith {
2997416022c9SBarry Smith   Mat_SeqAIJ     *a;
2998416022c9SBarry Smith   Mat            B;
29996849ba73SBarry Smith   PetscErrorCode ierr;
30003c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
300138baddfdSBarry Smith   int            fd;
300238baddfdSBarry Smith   PetscMPIInt    size;
3003bcd2baecSBarry Smith   MPI_Comm       comm;
300417ab2063SBarry Smith 
30053a40ed3dSBarry Smith   PetscFunctionBegin;
3006e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3007d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
300829bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3009b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
30100752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3011552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
301217ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
301317ab2063SBarry Smith 
3014d64ed03dSBarry Smith   if (nz < 0) {
301529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3016d64ed03dSBarry Smith   }
3017d64ed03dSBarry Smith 
301817ab2063SBarry Smith   /* read in row lengths */
301997f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
30200752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
302117ab2063SBarry Smith 
30223c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
30233c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
30243c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
30253c601197SSatish Balay 
302617ab2063SBarry Smith   /* create our matrix */
3027f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3028f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3029b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3030ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3031416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
303217ab2063SBarry Smith 
303317ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
30340752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
303517ab2063SBarry Smith 
303617ab2063SBarry Smith   /* read in nonzero values */
30370752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
303817ab2063SBarry Smith 
303917ab2063SBarry Smith   /* set matrix "i" values */
3040efb16452SHong Zhang   a->i[0] = 0;
304117ab2063SBarry Smith   for (i=1; i<= M; i++) {
3042416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3043416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
304417ab2063SBarry Smith   }
3045606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
304617ab2063SBarry Smith 
30476d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
30486d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3049b3a1e11cSKris Buschelman   *A = B;
30503a40ed3dSBarry Smith   PetscFunctionReturn(0);
305117ab2063SBarry Smith }
305217ab2063SBarry Smith 
30534a2ae208SSatish Balay #undef __FUNCT__
3054b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3055dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
30567264ac53SSatish Balay {
30577264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3058dfbe8321SBarry Smith   PetscErrorCode ierr;
30597264ac53SSatish Balay 
30603a40ed3dSBarry Smith   PetscFunctionBegin;
3061bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3062bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
3063ca44d042SBarry Smith     *flg = PETSC_FALSE;
3064ca44d042SBarry Smith     PetscFunctionReturn(0);
3065bcd2baecSBarry Smith   }
30667264ac53SSatish Balay 
30677264ac53SSatish Balay   /* if the a->i are the same */
306897f1f81fSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3069abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
30707264ac53SSatish Balay 
30717264ac53SSatish Balay   /* if a->j are the same */
307297f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3073abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3074bcd2baecSBarry Smith 
3075bcd2baecSBarry Smith   /* if a->a are the same */
307687828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
30770f5bd95cSBarry Smith 
30783a40ed3dSBarry Smith   PetscFunctionReturn(0);
30797264ac53SSatish Balay 
30807264ac53SSatish Balay }
308136db0b34SBarry Smith 
30824a2ae208SSatish Balay #undef __FUNCT__
30834a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
308405869f15SSatish Balay /*@
308536db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
308636db0b34SBarry Smith               provided by the user.
308736db0b34SBarry Smith 
308836db0b34SBarry Smith       Coolective on MPI_Comm
308936db0b34SBarry Smith 
309036db0b34SBarry Smith    Input Parameters:
309136db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
309236db0b34SBarry Smith .   m - number of rows
309336db0b34SBarry Smith .   n - number of columns
309436db0b34SBarry Smith .   i - row indices
309536db0b34SBarry Smith .   j - column indices
309636db0b34SBarry Smith -   a - matrix values
309736db0b34SBarry Smith 
309836db0b34SBarry Smith    Output Parameter:
309936db0b34SBarry Smith .   mat - the matrix
310036db0b34SBarry Smith 
310136db0b34SBarry Smith    Level: intermediate
310236db0b34SBarry Smith 
310336db0b34SBarry Smith    Notes:
31040551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
310536db0b34SBarry Smith     once the matrix is destroyed
310636db0b34SBarry Smith 
310736db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
310836db0b34SBarry Smith 
3109bfeeae90SHong Zhang        The i and j indices are 0 based
311036db0b34SBarry Smith 
311136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
311236db0b34SBarry Smith 
311336db0b34SBarry Smith @*/
3114be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
311536db0b34SBarry Smith {
3116dfbe8321SBarry Smith   PetscErrorCode ierr;
311797f1f81fSBarry Smith   PetscInt       ii;
311836db0b34SBarry Smith   Mat_SeqAIJ     *aij;
311936db0b34SBarry Smith 
312036db0b34SBarry Smith   PetscFunctionBegin;
3121a96a251dSBarry Smith   if (i[0]) {
3122634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
312336db0b34SBarry Smith   }
3124f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3125f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3126ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3127ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3128ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3129ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3130ab93d7beSBarry Smith 
313136db0b34SBarry Smith   aij->i = i;
313236db0b34SBarry Smith   aij->j = j;
313336db0b34SBarry Smith   aij->a = a;
313436db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
313536db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
313636db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
313736db0b34SBarry Smith 
313836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
313936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
31402515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
314179a5c55eSBarry 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]);
314236db0b34SBarry Smith #endif
314336db0b34SBarry Smith   }
31442515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
314536db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
314679a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
314779a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
314836db0b34SBarry Smith   }
314936db0b34SBarry Smith #endif
315036db0b34SBarry Smith 
3151b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3152b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
315336db0b34SBarry Smith   PetscFunctionReturn(0);
315436db0b34SBarry Smith }
315536db0b34SBarry Smith 
3156cc8ba8e1SBarry Smith #undef __FUNCT__
3157ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3158dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3159cc8ba8e1SBarry Smith {
3160dfbe8321SBarry Smith   PetscErrorCode ierr;
3161cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
316236db0b34SBarry Smith 
3163cc8ba8e1SBarry Smith   PetscFunctionBegin;
316412c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
3165cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3166cc8ba8e1SBarry Smith     a->coloring = coloring;
316712c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
316897f1f81fSBarry Smith     PetscInt             i,*larray;
316912c595b3SBarry Smith     ISColoring      ocoloring;
317008b6dcc0SBarry Smith     ISColoringValue *colors;
317112c595b3SBarry Smith 
317212c595b3SBarry Smith     /* set coloring for diagonal portion */
317397f1f81fSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
317412c595b3SBarry Smith     for (i=0; i<A->n; i++) {
317512c595b3SBarry Smith       larray[i] = i;
317612c595b3SBarry Smith     }
317712c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
317808b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
317912c595b3SBarry Smith     for (i=0; i<A->n; i++) {
318012c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
318112c595b3SBarry Smith     }
318212c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
318312c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
318412c595b3SBarry Smith     a->coloring = ocoloring;
318512c595b3SBarry Smith   }
3186cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3187cc8ba8e1SBarry Smith }
3188cc8ba8e1SBarry Smith 
3189dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3190ee4f033dSBarry Smith EXTERN_C_BEGIN
319129c1e371SBarry Smith #include "adic/ad_utils.h"
3192ee4f033dSBarry Smith EXTERN_C_END
3193cc8ba8e1SBarry Smith 
3194cc8ba8e1SBarry Smith #undef __FUNCT__
3195ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3196dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3197cc8ba8e1SBarry Smith {
3198cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
319997f1f81fSBarry Smith   PetscInt        m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
32004440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
320108b6dcc0SBarry Smith   ISColoringValue *color;
3202cc8ba8e1SBarry Smith 
3203cc8ba8e1SBarry Smith   PetscFunctionBegin;
3204e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
32054440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3206cc8ba8e1SBarry Smith   color = a->coloring->colors;
3207cc8ba8e1SBarry Smith   /* loop over rows */
3208cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3209cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3210cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3211cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3212cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3213cc8ba8e1SBarry Smith     }
32144440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3215ee4f033dSBarry Smith   }
3216ee4f033dSBarry Smith   PetscFunctionReturn(0);
3217ee4f033dSBarry Smith }
3218ee4f033dSBarry Smith #endif
3219ee4f033dSBarry Smith 
3220ee4f033dSBarry Smith #undef __FUNCT__
3221ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
322297f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3223ee4f033dSBarry Smith {
3224ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
322597f1f81fSBarry Smith   PetscInt             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
322687828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
322708b6dcc0SBarry Smith   ISColoringValue *color;
3228ee4f033dSBarry Smith 
3229ee4f033dSBarry Smith   PetscFunctionBegin;
3230e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3231ee4f033dSBarry Smith   color = a->coloring->colors;
3232ee4f033dSBarry Smith   /* loop over rows */
3233ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3234ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3235ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3236ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3237ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3238ee4f033dSBarry Smith     }
3239ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3240cc8ba8e1SBarry Smith   }
3241cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3242cc8ba8e1SBarry Smith }
324336db0b34SBarry Smith 
3244