xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 87d4246c4e6f032faf9d328cc0e6ec758a7a5646)
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 
139*87d4246cSBarry Smith #undef __FUNCT__
140*87d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
141*87d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
142*87d4246cSBarry Smith {
143*87d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
144*87d4246cSBarry Smith   PetscInt       *ai = a->i;
145*87d4246cSBarry Smith   PetscErrorCode ierr;
146*87d4246cSBarry Smith 
147*87d4246cSBarry Smith   PetscFunctionBegin;
148*87d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
149*87d4246cSBarry Smith   PetscFunctionReturn(0);
150*87d4246cSBarry Smith }
151*87d4246cSBarry 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;
1036f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1037f1e2ffcdSBarry Smith   diag = a->diag;
1038273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
1039bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
104077431f27SBarry Smith       SETERRQ1(PETSC_ERR_PLIB,"Matrix is missing diagonal number %D",i);
1041be5855fcSBarry Smith     }
1042be5855fcSBarry Smith   }
1043be5855fcSBarry Smith   PetscFunctionReturn(0);
1044be5855fcSBarry Smith }
1045be5855fcSBarry Smith 
10464a2ae208SSatish Balay #undef __FUNCT__
10474a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
104897f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
104917ab2063SBarry Smith {
1050416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1051beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1052beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1053dfbe8321SBarry Smith   PetscErrorCode     ierr;
105497f1f81fSBarry Smith   PetscInt           n = A->n,m = A->m,i;
105597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
105617ab2063SBarry Smith 
10573a40ed3dSBarry Smith   PetscFunctionBegin;
1058b965ef7fSBarry Smith   its = its*lits;
105977431f27SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
106091723122SBarry Smith 
1061ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1062ed480e8bSBarry Smith   diag = a->diag;
1063ed480e8bSBarry Smith   if (!a->idiag) {
1064ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1065ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1066ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1067ed480e8bSBarry Smith 
1068ed480e8bSBarry Smith     v        = a->a;
1069ed480e8bSBarry Smith 
1070ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1071958c9bccSBarry Smith     if (omega == 1.0 && !fshift) {
1072ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1073ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1074ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1075ed480e8bSBarry Smith       }
1076efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
1077ed480e8bSBarry Smith     } else {
1078ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1079ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1080beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1081ed480e8bSBarry Smith       }
1082efee365bSSatish Balay       ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
1083beeb8507SBarry Smith     }
1084ed480e8bSBarry Smith   }
1085ed480e8bSBarry Smith   t     = a->ssor;
1086ed480e8bSBarry Smith   idiag = a->idiag;
1087ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1088ed480e8bSBarry Smith 
10891ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1090fb2e594dSBarry Smith   if (xx != bb) {
10911ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1092fb2e594dSBarry Smith   } else {
1093fb2e594dSBarry Smith     b = x;
1094fb2e594dSBarry Smith   }
1095fb2e594dSBarry Smith 
1096ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1097ed480e8bSBarry Smith   xs   = x;
109817ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
109917ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1100ed480e8bSBarry Smith     bs = b;
110117ab2063SBarry Smith     for (i=0; i<m; i++) {
1102ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1103416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1104ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1105ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
110617ab2063SBarry Smith         sum  = b[i]*d/omega;
110717ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
110817ab2063SBarry Smith         x[i] = sum;
110917ab2063SBarry Smith     }
11101ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11111ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1112efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11133a40ed3dSBarry Smith     PetscFunctionReturn(0);
111417ab2063SBarry Smith   }
1115c783ea89SBarry Smith 
1116ed480e8bSBarry Smith 
1117fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1118fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1119fc3d8934SBarry Smith 
1120fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1121fc3d8934SBarry Smith 
1122fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1123fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
112448af12d7SBarry Smith     is the relaxation factor.
1125fc3d8934SBarry Smith     */
1126fc3d8934SBarry Smith 
112748af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
112829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11293a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
113017ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
113117ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
113217ab2063SBarry Smith 
113317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
113417ab2063SBarry Smith 
113517ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
113617ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
113717ab2063SBarry Smith     is the relaxation factor.
113817ab2063SBarry Smith     */
113917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
114017ab2063SBarry Smith 
114117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
114217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1143416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1144ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1145ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
114617ab2063SBarry Smith       sum  = b[i];
114717ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1148ed480e8bSBarry Smith       x[i] = sum*idiag[i];
114917ab2063SBarry Smith     }
115017ab2063SBarry Smith 
115117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1152416022c9SBarry Smith     v = a->a;
1153ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
115417ab2063SBarry Smith 
115517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1156ed480e8bSBarry Smith     ts = t;
1157416022c9SBarry Smith     diag = a->diag;
115817ab2063SBarry Smith     for (i=0; i<m; i++) {
1159416022c9SBarry Smith       n    = diag[i] - a->i[i];
1160ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1161ed480e8bSBarry Smith       v    = a->a + a->i[i];
116217ab2063SBarry Smith       sum  = t[i];
116317ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1164ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1165733d66baSBarry Smith       /*  x = x + t */
1166733d66baSBarry Smith       x[i] += t[i];
116717ab2063SBarry Smith     }
116817ab2063SBarry Smith 
1169efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
11701ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11711ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11723a40ed3dSBarry Smith     PetscFunctionReturn(0);
117317ab2063SBarry Smith   }
117417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
117517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
117677d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
117797f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
117877d8c4bbSBarry Smith #else
117917ab2063SBarry Smith       for (i=0; i<m; i++) {
1180416022c9SBarry Smith         n    = diag[i] - a->i[i];
1181ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1182ed480e8bSBarry Smith         v    = a->a + a->i[i];
118317ab2063SBarry Smith         sum  = b[i];
118417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1185ed480e8bSBarry Smith         x[i] = sum*idiag[i];
118617ab2063SBarry Smith       }
118777d8c4bbSBarry Smith #endif
118817ab2063SBarry Smith       xb = x;
1189efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11903a40ed3dSBarry Smith     } else xb = b;
119117ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
119217ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
119317ab2063SBarry Smith       for (i=0; i<m; i++) {
1194ed480e8bSBarry Smith         x[i] *= mdiag[i];
119517ab2063SBarry Smith       }
1196efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
119717ab2063SBarry Smith     }
119817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
119977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
120097f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
120177d8c4bbSBarry Smith #else
120217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1203416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1204ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1205ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
120617ab2063SBarry Smith         sum  = xb[i];
120717ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1208ed480e8bSBarry Smith         x[i] = sum*idiag[i];
120917ab2063SBarry Smith       }
121077d8c4bbSBarry Smith #endif
1211efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
121217ab2063SBarry Smith     }
121317ab2063SBarry Smith     its--;
121417ab2063SBarry Smith   }
121517ab2063SBarry Smith   while (its--) {
121617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
121777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
121897f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
121977d8c4bbSBarry Smith #else
122017ab2063SBarry Smith       for (i=0; i<m; i++) {
1221ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1222416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1223ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1224ed480e8bSBarry Smith         v    = a->a + a->i[i];
122517ab2063SBarry Smith         sum  = b[i];
122617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1227ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
122817ab2063SBarry Smith       }
122977d8c4bbSBarry Smith #endif
1230efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
123117ab2063SBarry Smith     }
123217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
123377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
123497f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
123577d8c4bbSBarry Smith #else
123617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1237ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1238416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1239ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1240ed480e8bSBarry Smith         v    = a->a + a->i[i];
124117ab2063SBarry Smith         sum  = b[i];
124217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1243ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
124417ab2063SBarry Smith       }
124577d8c4bbSBarry Smith #endif
1246efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
124717ab2063SBarry Smith     }
124817ab2063SBarry Smith   }
12491ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12501ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12513a40ed3dSBarry Smith   PetscFunctionReturn(0);
125217ab2063SBarry Smith }
125317ab2063SBarry Smith 
12544a2ae208SSatish Balay #undef __FUNCT__
12554a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1256dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
125717ab2063SBarry Smith {
1258416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12594e220ebcSLois Curfman McInnes 
12603a40ed3dSBarry Smith   PetscFunctionBegin;
1261273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1262273d9f13SBarry Smith   info->columns_global = (double)A->n;
1263273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1264273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12654e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12664e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12674e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12684e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12694e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12704e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12714e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12724e220ebcSLois Curfman McInnes   if (A->factor) {
12734e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12744e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12754e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12764e220ebcSLois Curfman McInnes   } else {
12774e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12784e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12794e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12804e220ebcSLois Curfman McInnes   }
12813a40ed3dSBarry Smith   PetscFunctionReturn(0);
128217ab2063SBarry Smith }
128317ab2063SBarry Smith 
12844a2ae208SSatish Balay #undef __FUNCT__
12854a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1286f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
128717ab2063SBarry Smith {
1288416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1289f4df32b1SMatthew Knepley   PetscInt       i,m = A->m - 1;
12906849ba73SBarry Smith   PetscErrorCode ierr;
129117ab2063SBarry Smith 
12923a40ed3dSBarry Smith   PetscFunctionBegin;
1293f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1294f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
129577431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1296bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1297f1e2ffcdSBarry Smith     }
1298f4df32b1SMatthew Knepley     if (diag != 0.0) {
1299f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1300f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1301f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1302f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1303f1e2ffcdSBarry Smith       }
1304f1e2ffcdSBarry Smith     }
130588e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1306f1e2ffcdSBarry Smith   } else {
1307f4df32b1SMatthew Knepley     if (diag != 0.0) {
130817ab2063SBarry Smith       for (i=0; i<N; i++) {
130977431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13107ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1311416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1312f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1313bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13147ae801bdSBarry Smith         } else { /* in case row was completely empty */
1315f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
131617ab2063SBarry Smith         }
131717ab2063SBarry Smith       }
13183a40ed3dSBarry Smith     } else {
131917ab2063SBarry Smith       for (i=0; i<N; i++) {
132077431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1321416022c9SBarry Smith         a->ilen[rows[i]] = 0;
132217ab2063SBarry Smith       }
132317ab2063SBarry Smith     }
132488e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1325f1e2ffcdSBarry Smith   }
132643a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13273a40ed3dSBarry Smith   PetscFunctionReturn(0);
132817ab2063SBarry Smith }
132917ab2063SBarry Smith 
13304a2ae208SSatish Balay #undef __FUNCT__
13314a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
133297f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
133317ab2063SBarry Smith {
1334416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
133597f1f81fSBarry Smith   PetscInt   *itmp;
133617ab2063SBarry Smith 
13373a40ed3dSBarry Smith   PetscFunctionBegin;
133877431f27SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
133917ab2063SBarry Smith 
1340416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1341bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
134217ab2063SBarry Smith   if (idx) {
1343bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1344bfeeae90SHong Zhang     if (*nz) {
13454e093b46SBarry Smith       *idx = itmp;
134617ab2063SBarry Smith     }
134717ab2063SBarry Smith     else *idx = 0;
134817ab2063SBarry Smith   }
13493a40ed3dSBarry Smith   PetscFunctionReturn(0);
135017ab2063SBarry Smith }
135117ab2063SBarry Smith 
1352bfeeae90SHong Zhang /* remove this function? */
13534a2ae208SSatish Balay #undef __FUNCT__
13544a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
135597f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
135617ab2063SBarry Smith {
13573a40ed3dSBarry Smith   PetscFunctionBegin;
13583a40ed3dSBarry Smith   PetscFunctionReturn(0);
135917ab2063SBarry Smith }
136017ab2063SBarry Smith 
13614a2ae208SSatish Balay #undef __FUNCT__
13624a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1363dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
136417ab2063SBarry Smith {
1365416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
136687828ca2SBarry Smith   PetscScalar    *v = a->a;
136736db0b34SBarry Smith   PetscReal      sum = 0.0;
13686849ba73SBarry Smith   PetscErrorCode ierr;
136997f1f81fSBarry Smith   PetscInt       i,j;
137017ab2063SBarry Smith 
13713a40ed3dSBarry Smith   PetscFunctionBegin;
137217ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1373416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1374aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
137536db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
137617ab2063SBarry Smith #else
137717ab2063SBarry Smith       sum += (*v)*(*v); v++;
137817ab2063SBarry Smith #endif
137917ab2063SBarry Smith     }
1380064f8208SBarry Smith     *nrm = sqrt(sum);
13813a40ed3dSBarry Smith   } else if (type == NORM_1) {
138236db0b34SBarry Smith     PetscReal *tmp;
138397f1f81fSBarry Smith     PetscInt    *jj = a->j;
1384b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1385273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1386064f8208SBarry Smith     *nrm = 0.0;
1387416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1388bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
138917ab2063SBarry Smith     }
1390273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1391064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
139217ab2063SBarry Smith     }
1393606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13943a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1395064f8208SBarry Smith     *nrm = 0.0;
1396273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1397bfeeae90SHong Zhang       v = a->a + a->i[j];
139817ab2063SBarry Smith       sum = 0.0;
1399416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1400cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
140117ab2063SBarry Smith       }
1402064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
140317ab2063SBarry Smith     }
14043a40ed3dSBarry Smith   } else {
140529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
140617ab2063SBarry Smith   }
14073a40ed3dSBarry Smith   PetscFunctionReturn(0);
140817ab2063SBarry Smith }
140917ab2063SBarry Smith 
14104a2ae208SSatish Balay #undef __FUNCT__
14114a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1412dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B)
141317ab2063SBarry Smith {
1414416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1415416022c9SBarry Smith   Mat            C;
14166849ba73SBarry Smith   PetscErrorCode ierr;
141797f1f81fSBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->m,len,*col;
141887828ca2SBarry Smith   PetscScalar    *array = a->a;
141917ab2063SBarry Smith 
14203a40ed3dSBarry Smith   PetscFunctionBegin;
1421273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
142297f1f81fSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
142397f1f81fSBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(PetscInt));CHKERRQ(ierr);
1424bfeeae90SHong Zhang 
1425bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1426f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1427f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->n,m,A->n,m);CHKERRQ(ierr);
1428f204ca49SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1429ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1430606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
143117ab2063SBarry Smith   for (i=0; i<m; i++) {
143217ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1433*87d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1434b9b97703SBarry Smith     array += len;
1435b9b97703SBarry Smith     aj    += len;
143617ab2063SBarry Smith   }
143717ab2063SBarry Smith 
14386d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14396d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
144017ab2063SBarry Smith 
1441f1e2ffcdSBarry Smith   if (B) {
1442416022c9SBarry Smith     *B = C;
144317ab2063SBarry Smith   } else {
1444273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
144517ab2063SBarry Smith   }
14463a40ed3dSBarry Smith   PetscFunctionReturn(0);
144717ab2063SBarry Smith }
144817ab2063SBarry Smith 
1449cd0d46ebSvictorle EXTERN_C_BEGIN
1450cd0d46ebSvictorle #undef __FUNCT__
14515fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1452be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1453cd0d46ebSvictorle {
1454cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
145597f1f81fSBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
14566849ba73SBarry Smith   PetscErrorCode ierr;
145797f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1458cd0d46ebSvictorle 
1459cd0d46ebSvictorle   PetscFunctionBegin;
1460cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1461cd0d46ebSvictorle 
1462cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1463cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
14645485867bSBarry Smith   if (ma!=nb || na!=mb){
14655485867bSBarry Smith     *f = PETSC_FALSE;
14665485867bSBarry Smith     PetscFunctionReturn(0);
14675485867bSBarry Smith   }
1468cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1469cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1470cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
147197f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
147297f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1473cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1474cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1475cd0d46ebSvictorle 
1476cd0d46ebSvictorle   *f = PETSC_TRUE;
1477cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1478cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
147997f1f81fSBarry Smith       PetscInt         idc,idr;
14805485867bSBarry Smith       PetscScalar vc,vr;
1481cd0d46ebSvictorle       /* column/row index/value */
14825485867bSBarry Smith       idc = adx[aptr[i]];
14835485867bSBarry Smith       idr = bdx[bptr[idc]];
14845485867bSBarry Smith       vc  = va[aptr[i]];
14855485867bSBarry Smith       vr  = vb[bptr[idc]];
14865485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
14875485867bSBarry Smith 	*f = PETSC_FALSE;
14885485867bSBarry Smith         goto done;
1489cd0d46ebSvictorle       } else {
14905485867bSBarry Smith 	aptr[i]++;
14915485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1492cd0d46ebSvictorle       }
1493cd0d46ebSvictorle     }
1494cd0d46ebSvictorle   }
1495cd0d46ebSvictorle  done:
1496cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
14973aeef889SHong Zhang   if (B) {
14983aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
14993aeef889SHong Zhang   }
1500cd0d46ebSvictorle   PetscFunctionReturn(0);
1501cd0d46ebSvictorle }
1502cd0d46ebSvictorle EXTERN_C_END
1503cd0d46ebSvictorle 
15049e29f15eSvictorle #undef __FUNCT__
15059e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1506dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
15079e29f15eSvictorle {
1508dfbe8321SBarry Smith   PetscErrorCode ierr;
15099e29f15eSvictorle   PetscFunctionBegin;
15105485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
15119e29f15eSvictorle   PetscFunctionReturn(0);
15129e29f15eSvictorle }
15139e29f15eSvictorle 
15144a2ae208SSatish Balay #undef __FUNCT__
15154a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1516dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
151717ab2063SBarry Smith {
1518416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
151987828ca2SBarry Smith   PetscScalar    *l,*r,x,*v;
1520dfbe8321SBarry Smith   PetscErrorCode ierr;
152197f1f81fSBarry Smith   PetscInt       i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
152217ab2063SBarry Smith 
15233a40ed3dSBarry Smith   PetscFunctionBegin;
152417ab2063SBarry Smith   if (ll) {
15253ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
15263ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1527e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1528273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
15291ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1530416022c9SBarry Smith     v = a->a;
153117ab2063SBarry Smith     for (i=0; i<m; i++) {
153217ab2063SBarry Smith       x = l[i];
1533416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
153417ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
153517ab2063SBarry Smith     }
15361ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1537efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
153817ab2063SBarry Smith   }
153917ab2063SBarry Smith   if (rr) {
1540e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1541273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
15421ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1543416022c9SBarry Smith     v = a->a; jj = a->j;
154417ab2063SBarry Smith     for (i=0; i<nz; i++) {
1545bfeeae90SHong Zhang       (*v++) *= r[*jj++];
154617ab2063SBarry Smith     }
15471ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1548efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
154917ab2063SBarry Smith   }
15503a40ed3dSBarry Smith   PetscFunctionReturn(0);
155117ab2063SBarry Smith }
155217ab2063SBarry Smith 
15534a2ae208SSatish Balay #undef __FUNCT__
15544a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
155597f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
155617ab2063SBarry Smith {
1557db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
15586849ba73SBarry Smith   PetscErrorCode ierr;
155997f1f81fSBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->n,*lens;
156097f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
156197f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
156297f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
156387828ca2SBarry Smith   PetscScalar    *a_new,*mat_a;
1564416022c9SBarry Smith   Mat            C;
1565fee21e36SBarry Smith   PetscTruth     stride;
156617ab2063SBarry Smith 
15673a40ed3dSBarry Smith   PetscFunctionBegin;
1568d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
156929bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1570d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
157129bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
157299141d43SSatish Balay 
157317ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1574b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1575b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
157617ab2063SBarry Smith 
1577fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1578fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1579fee21e36SBarry Smith   if (stride && step == 1) {
158002834360SBarry Smith     /* special case of contiguous rows */
158197f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
158231ebf83bSSatish Balay     starts = lens + nrows;
158302834360SBarry Smith     /* loop over new rows determining lens and starting points */
158402834360SBarry Smith     for (i=0; i<nrows; i++) {
1585bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1586a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
158702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1588bfeeae90SHong Zhang         if (aj[k] >= first) {
158902834360SBarry Smith           starts[i] = k;
159002834360SBarry Smith           break;
159102834360SBarry Smith 	}
159202834360SBarry Smith       }
1593a2744918SBarry Smith       sum = 0;
159402834360SBarry Smith       while (k < kend) {
1595bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1596a2744918SBarry Smith         sum++;
159702834360SBarry Smith       }
1598a2744918SBarry Smith       lens[i] = sum;
159902834360SBarry Smith     }
160002834360SBarry Smith     /* create submatrix */
1601cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
160297f1f81fSBarry Smith       PetscInt n_cols,n_rows;
160308480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
160429bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1605d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
160608480c60SBarry Smith       C = *B;
16073a40ed3dSBarry Smith     } else {
1608f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1609f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1610e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1611ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
161208480c60SBarry Smith     }
1613db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1614db02288aSLois Curfman McInnes 
161502834360SBarry Smith     /* loop over rows inserting into submatrix */
1616db02288aSLois Curfman McInnes     a_new    = c->a;
1617db02288aSLois Curfman McInnes     j_new    = c->j;
1618db02288aSLois Curfman McInnes     i_new    = c->i;
1619bfeeae90SHong Zhang 
162002834360SBarry Smith     for (i=0; i<nrows; i++) {
1621a2744918SBarry Smith       ii    = starts[i];
1622a2744918SBarry Smith       lensi = lens[i];
1623a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1624a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
162502834360SBarry Smith       }
162687828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1627a2744918SBarry Smith       a_new      += lensi;
1628a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1629a2744918SBarry Smith       c->ilen[i]  = lensi;
163002834360SBarry Smith     }
1631606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
16323a40ed3dSBarry Smith   } else {
163302834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
163497f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1635bfeeae90SHong Zhang 
163697f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
163797f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
163817ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
163902834360SBarry Smith     /* determine lens of each row */
164002834360SBarry Smith     for (i=0; i<nrows; i++) {
1641bfeeae90SHong Zhang       kstart  = ai[irow[i]];
164202834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
164302834360SBarry Smith       lens[i] = 0;
164402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1645bfeeae90SHong Zhang         if (smap[aj[k]]) {
164602834360SBarry Smith           lens[i]++;
164702834360SBarry Smith         }
164802834360SBarry Smith       }
164902834360SBarry Smith     }
165017ab2063SBarry Smith     /* Create and fill new matrix */
1651a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16520f5bd95cSBarry Smith       PetscTruth equal;
16530f5bd95cSBarry Smith 
165499141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1655273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
165697f1f81fSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(PetscInt),&equal);CHKERRQ(ierr);
16570f5bd95cSBarry Smith       if (!equal) {
165829bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
165999141d43SSatish Balay       }
166097f1f81fSBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(PetscInt));CHKERRQ(ierr);
166108480c60SBarry Smith       C = *B;
16623a40ed3dSBarry Smith     } else {
1663f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1664f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1665e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1666ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
166708480c60SBarry Smith     }
166899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
166917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
167099141d43SSatish Balay       row    = irow[i];
1671bfeeae90SHong Zhang       kstart = ai[row];
167299141d43SSatish Balay       kend   = kstart + a->ilen[row];
1673bfeeae90SHong Zhang       mat_i  = c->i[i];
167499141d43SSatish Balay       mat_j  = c->j + mat_i;
167599141d43SSatish Balay       mat_a  = c->a + mat_i;
167699141d43SSatish Balay       mat_ilen = c->ilen + i;
167717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1678bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1679ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
168099141d43SSatish Balay           *mat_a++ = a->a[k];
168199141d43SSatish Balay           (*mat_ilen)++;
168299141d43SSatish Balay 
168317ab2063SBarry Smith         }
168417ab2063SBarry Smith       }
168517ab2063SBarry Smith     }
168602834360SBarry Smith     /* Free work space */
168702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1688606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1689606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
169002834360SBarry Smith   }
16916d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16926d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
169317ab2063SBarry Smith 
169417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1695416022c9SBarry Smith   *B = C;
16963a40ed3dSBarry Smith   PetscFunctionReturn(0);
169717ab2063SBarry Smith }
169817ab2063SBarry Smith 
1699a871dcd8SBarry Smith /*
1700a871dcd8SBarry Smith */
17014a2ae208SSatish Balay #undef __FUNCT__
17024a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1703dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1704a871dcd8SBarry Smith {
170563b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1706dfbe8321SBarry Smith   PetscErrorCode ierr;
170763b91edcSBarry Smith   Mat            outA;
1708b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
170963b91edcSBarry Smith 
17103a40ed3dSBarry Smith   PetscFunctionBegin;
1711d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1712b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1713b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1714b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1715634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
1716b8a78c4aSBarry Smith   }
1717a871dcd8SBarry Smith 
171863b91edcSBarry Smith   outA          = inA;
171963b91edcSBarry Smith   inA->factor   = FACTOR_LU;
172063b91edcSBarry Smith   a->row        = row;
172163b91edcSBarry Smith   a->col        = col;
1722c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1723c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
172463b91edcSBarry Smith 
172536db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1726b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
17274c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
172852e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1729f0ec6fceSSatish Balay 
173094a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
173187828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
173294a9d846SBarry Smith   }
173363b91edcSBarry Smith 
173408480c60SBarry Smith   if (!a->diag) {
1735f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
173663b91edcSBarry Smith   }
1737af281ebdSHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
17383a40ed3dSBarry Smith   PetscFunctionReturn(0);
1739a871dcd8SBarry Smith }
1740a871dcd8SBarry Smith 
1741d9eff348SSatish Balay #include "petscblaslapack.h"
17424a2ae208SSatish Balay #undef __FUNCT__
17434a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1744f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1745f0b747eeSBarry Smith {
1746f0b747eeSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)inA->data;
17474ce68768SBarry Smith   PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1;
1748f4df32b1SMatthew Knepley   PetscScalar oalpha = alpha;
1749efee365bSSatish Balay   PetscErrorCode ierr;
1750efee365bSSatish Balay 
17513a40ed3dSBarry Smith 
17523a40ed3dSBarry Smith   PetscFunctionBegin;
1753f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1754efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17553a40ed3dSBarry Smith   PetscFunctionReturn(0);
1756f0b747eeSBarry Smith }
1757f0b747eeSBarry Smith 
17584a2ae208SSatish Balay #undef __FUNCT__
17594a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
176097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1761cddf8d76SBarry Smith {
1762dfbe8321SBarry Smith   PetscErrorCode ierr;
176397f1f81fSBarry Smith   PetscInt       i;
1764cddf8d76SBarry Smith 
17653a40ed3dSBarry Smith   PetscFunctionBegin;
1766cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1767b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1768cddf8d76SBarry Smith   }
1769cddf8d76SBarry Smith 
1770cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17716a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1772cddf8d76SBarry Smith   }
17733a40ed3dSBarry Smith   PetscFunctionReturn(0);
1774cddf8d76SBarry Smith }
1775cddf8d76SBarry Smith 
17764a2ae208SSatish Balay #undef __FUNCT__
17774a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
177897f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
17794dcbc457SBarry Smith {
1780e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17816849ba73SBarry Smith   PetscErrorCode ierr;
178297f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
178397f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1784f1af5d2fSBarry Smith   PetscBT        table;
1785bbd702dbSSatish Balay 
17863a40ed3dSBarry Smith   PetscFunctionBegin;
1787273d9f13SBarry Smith   m     = A->m;
1788e4d965acSSatish Balay   ai    = a->i;
1789bfeeae90SHong Zhang   aj    = a->j;
17908a047759SSatish Balay 
1791a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
179206763907SSatish Balay 
179397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
17946831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
179506763907SSatish Balay 
1796e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1797b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1798e4d965acSSatish Balay     isz  = 0;
17996831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1800e4d965acSSatish Balay 
1801e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
18024dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1803b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1804e4d965acSSatish Balay 
1805dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1806e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1807f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
18084dcbc457SBarry Smith     }
180906763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
181006763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1811e4d965acSSatish Balay 
181204a348a9SBarry Smith     k = 0;
181304a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
181404a348a9SBarry Smith       n = isz;
181506763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1816e4d965acSSatish Balay         row   = nidx[k];
1817e4d965acSSatish Balay         start = ai[row];
1818e4d965acSSatish Balay         end   = ai[row+1];
181904a348a9SBarry Smith         for (l = start; l<end ; l++){
1820efb16452SHong Zhang           val = aj[l] ;
1821f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1822e4d965acSSatish Balay         }
1823e4d965acSSatish Balay       }
1824e4d965acSSatish Balay     }
1825029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1826e4d965acSSatish Balay   }
18276831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1828606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
18293a40ed3dSBarry Smith   PetscFunctionReturn(0);
18304dcbc457SBarry Smith }
183117ab2063SBarry Smith 
18320513a670SBarry Smith /* -------------------------------------------------------------- */
18334a2ae208SSatish Balay #undef __FUNCT__
18344a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1835dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
18360513a670SBarry Smith {
18370513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18386849ba73SBarry Smith   PetscErrorCode ierr;
183997f1f81fSBarry Smith   PetscInt       i,nz,m = A->m,n = A->n,*col;
184097f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
184156cd22aeSBarry Smith   IS             icolp,irowp;
184297f1f81fSBarry Smith   PetscInt       *cwork;
184332ec9ce4SBarry Smith   PetscScalar    *vwork;
18440513a670SBarry Smith 
18453a40ed3dSBarry Smith   PetscFunctionBegin;
18464c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
184756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18484c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
184956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18500513a670SBarry Smith 
18510513a670SBarry Smith   /* determine lengths of permuted rows */
185297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
18530513a670SBarry Smith   for (i=0; i<m; i++) {
18540513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18550513a670SBarry Smith   }
1856f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,B);CHKERRQ(ierr);
1857f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
1858f204ca49SKris Buschelman   ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr);
1859ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1860606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18610513a670SBarry Smith 
186297f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
18630513a670SBarry Smith   for (i=0; i<m; i++) {
186432ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18650513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1866cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
186732ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18680513a670SBarry Smith   }
1869606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18703c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
18710513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18720513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
187356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
187456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
187556cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
187656cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18773a40ed3dSBarry Smith   PetscFunctionReturn(0);
18780513a670SBarry Smith }
18790513a670SBarry Smith 
18804a2ae208SSatish Balay #undef __FUNCT__
18814a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1882dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_SeqAIJ(Mat A)
1883682d7d0cSBarry Smith {
1884c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1885682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1886dfbe8321SBarry Smith   PetscErrorCode    ierr;
1887682d7d0cSBarry Smith 
18883a40ed3dSBarry Smith   PetscFunctionBegin;
18894846f1f5SKris Buschelman   ierr = MatPrintHelp_Inode(A);CHKERRQ(ierr);
1890c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1891d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1892d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1893d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
189473e7a558SHong Zhang   ierr = (*PetscHelpPrintf)(comm,"  -mat_no_compressedrow: Do not use compressedrow\n");CHKERRQ(ierr);
18953a40ed3dSBarry Smith   PetscFunctionReturn(0);
1896682d7d0cSBarry Smith }
189797304618SKris Buschelman 
18984a2ae208SSatish Balay #undef __FUNCT__
18994a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1900dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1901cb5b572fSBarry Smith {
1902dfbe8321SBarry Smith   PetscErrorCode ierr;
1903cb5b572fSBarry Smith 
1904cb5b572fSBarry Smith   PetscFunctionBegin;
190533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
190633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1907be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1908be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1909be6bf707SBarry Smith 
1910bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
1911634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1912cb5b572fSBarry Smith     }
1913bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1914cb5b572fSBarry Smith   } else {
1915cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1916cb5b572fSBarry Smith   }
1917cb5b572fSBarry Smith   PetscFunctionReturn(0);
1918cb5b572fSBarry Smith }
1919cb5b572fSBarry Smith 
19204a2ae208SSatish Balay #undef __FUNCT__
19214a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1922dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
1923273d9f13SBarry Smith {
1924dfbe8321SBarry Smith   PetscErrorCode ierr;
1925273d9f13SBarry Smith 
1926273d9f13SBarry Smith   PetscFunctionBegin;
1927ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1928273d9f13SBarry Smith   PetscFunctionReturn(0);
1929273d9f13SBarry Smith }
1930273d9f13SBarry Smith 
19314a2ae208SSatish Balay #undef __FUNCT__
19324a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
1933dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
19346c0721eeSBarry Smith {
19356c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19366c0721eeSBarry Smith   PetscFunctionBegin;
19376c0721eeSBarry Smith   *array = a->a;
19386c0721eeSBarry Smith   PetscFunctionReturn(0);
19396c0721eeSBarry Smith }
19406c0721eeSBarry Smith 
19414a2ae208SSatish Balay #undef __FUNCT__
19424a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
1943dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19446c0721eeSBarry Smith {
19456c0721eeSBarry Smith   PetscFunctionBegin;
19466c0721eeSBarry Smith   PetscFunctionReturn(0);
19476c0721eeSBarry Smith }
1948273d9f13SBarry Smith 
1949ee4f033dSBarry Smith #undef __FUNCT__
1950ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1951dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1952ee4f033dSBarry Smith {
19536849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
19546849ba73SBarry Smith   PetscErrorCode ierr;
195597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
1956efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
195787828ca2SBarry Smith   PetscScalar    *vscale_array;
1958ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
1959ee4f033dSBarry Smith   Vec            w1,w2,w3;
1960ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
1961ee4f033dSBarry Smith   PetscTruth     flg;
1962ee4f033dSBarry Smith 
1963ee4f033dSBarry Smith   PetscFunctionBegin;
1964ee4f033dSBarry Smith   if (!coloring->w1) {
1965ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
196652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
1967ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
196852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
1969ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
197052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
1971ee4f033dSBarry Smith   }
1972ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1973ee4f033dSBarry Smith 
1974ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1975e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1976ee4f033dSBarry Smith   if (flg) {
197763ba0a88SBarry Smith     ierr = PetscLogInfo((coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"));CHKERRQ(ierr);
1978ee4f033dSBarry Smith   } else {
19790b9b6f31SBarry Smith     PetscTruth assembled;
19800b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
19810b9b6f31SBarry Smith     if (assembled) {
1982ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
1983ee4f033dSBarry Smith     }
19840b9b6f31SBarry Smith   }
1985ee4f033dSBarry Smith 
1986ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1987ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1988ee4f033dSBarry Smith 
1989ee4f033dSBarry Smith   /*
1990ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1991ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1992ee4f033dSBarry Smith   */
1993ee4f033dSBarry Smith   if (coloring->F) {
1994ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1995ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1996ee4f033dSBarry Smith     if (m1 != m2) {
1997ee4f033dSBarry Smith       coloring->F = 0;
1998ee4f033dSBarry Smith     }
1999ee4f033dSBarry Smith   }
2000ee4f033dSBarry Smith 
2001ee4f033dSBarry Smith   if (coloring->F) {
2002ee4f033dSBarry Smith     w1          = coloring->F;
2003ee4f033dSBarry Smith     coloring->F = 0;
2004ee4f033dSBarry Smith   } else {
200566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2006ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
200766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2008ee4f033dSBarry Smith   }
2009ee4f033dSBarry Smith 
2010ee4f033dSBarry Smith   /*
2011ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2012ee4f033dSBarry Smith   */
20131ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
20141ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2015ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2016ee4f033dSBarry Smith     /*
2017ee4f033dSBarry Smith        Loop over each column associated with color adding the
2018ee4f033dSBarry Smith        perturbation to the vector w3.
2019ee4f033dSBarry Smith     */
2020ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2021ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2022ee4f033dSBarry Smith       dx  = xx[col];
2023ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2024ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2025ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2026ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2027ee4f033dSBarry Smith #else
2028ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2029ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2030ee4f033dSBarry Smith #endif
2031ee4f033dSBarry Smith       dx                *= epsilon;
2032ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2033ee4f033dSBarry Smith     }
2034ee4f033dSBarry Smith   }
20351ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2036ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2037ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2038ee4f033dSBarry Smith 
2039ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2040ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2041ee4f033dSBarry Smith 
2042ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2043ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2044ee4f033dSBarry Smith 
20451ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2046ee4f033dSBarry Smith   /*
2047ee4f033dSBarry Smith       Loop over each color
2048ee4f033dSBarry Smith   */
2049ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
205049b058dcSBarry Smith     coloring->currentcolor = k;
2051ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
20521ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2053ee4f033dSBarry Smith     /*
2054ee4f033dSBarry Smith        Loop over each column associated with color adding the
2055ee4f033dSBarry Smith        perturbation to the vector w3.
2056ee4f033dSBarry Smith     */
2057ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2058ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2059ee4f033dSBarry Smith       dx  = xx[col];
20605b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2061ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2062ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2063ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2064ee4f033dSBarry Smith #else
2065ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2066ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2067ee4f033dSBarry Smith #endif
2068ee4f033dSBarry Smith       dx            *= epsilon;
2069634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2070ee4f033dSBarry Smith       w3_array[col] += dx;
2071ee4f033dSBarry Smith     }
20721ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2073ee4f033dSBarry Smith 
2074ee4f033dSBarry Smith     /*
2075ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2076ee4f033dSBarry Smith     */
2077ee4f033dSBarry Smith 
207866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2079ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
208066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2081efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2082ee4f033dSBarry Smith 
2083ee4f033dSBarry Smith     /*
2084ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2085ee4f033dSBarry Smith     */
20861ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2087ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2088ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2089ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2090ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2091ee4f033dSBarry Smith       srow   = row + start;
2092ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2093ee4f033dSBarry Smith     }
20941ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2095ee4f033dSBarry Smith   }
209649b058dcSBarry Smith   coloring->currentcolor = k;
20971ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
20981ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2099ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2100ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2101ee4f033dSBarry Smith   PetscFunctionReturn(0);
2102ee4f033dSBarry Smith }
2103ee4f033dSBarry Smith 
2104ac90fabeSBarry Smith #include "petscblaslapack.h"
2105ac90fabeSBarry Smith #undef __FUNCT__
2106ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2107f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2108ac90fabeSBarry Smith {
2109dfbe8321SBarry Smith   PetscErrorCode ierr;
211097f1f81fSBarry Smith   PetscInt       i;
2111ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
21124ce68768SBarry Smith   PetscBLASInt   one=1,bnz = (PetscBLASInt)x->nz;
2113ac90fabeSBarry Smith 
2114ac90fabeSBarry Smith   PetscFunctionBegin;
2115ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2116f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2117f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2118c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2119a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2120a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2121a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2122a30b2313SHong Zhang     }
2123a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
212424f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2125a30b2313SHong Zhang       y->XtoY = X;
2126c537a176SHong Zhang     }
2127f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
212863ba0a88SBarry 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);
2129ac90fabeSBarry Smith   } else {
2130f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2131ac90fabeSBarry Smith   }
2132ac90fabeSBarry Smith   PetscFunctionReturn(0);
2133ac90fabeSBarry Smith }
2134ac90fabeSBarry Smith 
2135521d7252SBarry Smith #undef __FUNCT__
2136521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2137521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2138521d7252SBarry Smith {
2139521d7252SBarry Smith   PetscFunctionBegin;
2140521d7252SBarry Smith   PetscFunctionReturn(0);
2141521d7252SBarry Smith }
2142521d7252SBarry Smith 
2143354c94deSBarry Smith #undef __FUNCT__
2144354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2145354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2146354c94deSBarry Smith {
2147354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2148354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2149354c94deSBarry Smith   PetscInt    i,nz;
2150354c94deSBarry Smith   PetscScalar *a;
2151354c94deSBarry Smith 
2152354c94deSBarry Smith   PetscFunctionBegin;
2153354c94deSBarry Smith   nz = aij->nz;
2154354c94deSBarry Smith   a  = aij->a;
2155354c94deSBarry Smith   for (i=0; i<nz; i++) {
2156354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2157354c94deSBarry Smith   }
2158354c94deSBarry Smith #else
2159354c94deSBarry Smith   PetscFunctionBegin;
2160354c94deSBarry Smith #endif
2161354c94deSBarry Smith   PetscFunctionReturn(0);
2162354c94deSBarry Smith }
2163354c94deSBarry Smith 
2164e34fafa9SBarry Smith #undef __FUNCT__
2165e34fafa9SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2166e34fafa9SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v)
2167e34fafa9SBarry Smith {
2168e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2169e34fafa9SBarry Smith   PetscErrorCode ierr;
2170e34fafa9SBarry Smith   PetscInt       i,j,m = A->m,*ai,*aj,ncols,n;
2171e34fafa9SBarry Smith   PetscReal      atmp;
2172e34fafa9SBarry Smith   PetscScalar    *x,zero = 0.0;
2173e34fafa9SBarry Smith   MatScalar      *aa;
2174e34fafa9SBarry Smith 
2175e34fafa9SBarry Smith   PetscFunctionBegin;
2176e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2177e34fafa9SBarry Smith   aa   = a->a;
2178e34fafa9SBarry Smith   ai   = a->i;
2179e34fafa9SBarry Smith   aj   = a->j;
2180e34fafa9SBarry Smith 
218143e18e04SHong Zhang   ierr = VecSet(v,zero);CHKERRQ(ierr);
2182e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2183e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2184e34fafa9SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2185e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2186e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2187e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2188e34fafa9SBarry Smith       atmp = PetscAbsScalar(*aa); aa++;
2189e34fafa9SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) x[i] = atmp;
2190e34fafa9SBarry Smith       aj++;
2191e34fafa9SBarry Smith     }
2192e34fafa9SBarry Smith   }
2193e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2194e34fafa9SBarry Smith   PetscFunctionReturn(0);
2195e34fafa9SBarry Smith }
2196e34fafa9SBarry Smith 
2197682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
21980a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2199cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2200cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2201cb5b572fSBarry Smith        MatMult_SeqAIJ,
220297304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
22037c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
22047c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2205cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2206cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
22077c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
220897304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2209cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2210cb5b572fSBarry Smith        0,
221117ab2063SBarry Smith        MatRelax_SeqAIJ,
221217ab2063SBarry Smith        MatTranspose_SeqAIJ,
221397304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2214cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2215cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2216cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2217cb5b572fSBarry Smith        MatNorm_SeqAIJ,
221897304618SKris Buschelman /*20*/ 0,
2219cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
222017ab2063SBarry Smith        MatCompress_SeqAIJ,
2221cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2222cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
222397304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2224cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2225cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2226f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2227a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
222897304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2229cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2230861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
22316c0721eeSBarry Smith        MatGetArray_SeqAIJ,
22326c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
223397304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2234cb5b572fSBarry Smith        0,
2235cb5b572fSBarry Smith        0,
2236cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2237cb5b572fSBarry Smith        0,
223897304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2239cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2240cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2241cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2242cb5b572fSBarry Smith        MatCopy_SeqAIJ,
224397304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ,
2244cb5b572fSBarry Smith        MatScale_SeqAIJ,
2245cb5b572fSBarry Smith        0,
224679299369SBarry Smith        MatDiagonalSet_SeqAIJ,
22476945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2248521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
22493b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
22503b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
22513b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2252a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
225397304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2254b9617806SBarry Smith        0,
22550513a670SBarry Smith        0,
2256cda55fadSBarry Smith        MatPermute_SeqAIJ,
2257cda55fadSBarry Smith        0,
225897304618SKris Buschelman /*60*/ 0,
2259b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2260b9b97703SBarry Smith        MatView_SeqAIJ,
22618a124369SBarry Smith        MatGetPetscMaps_Petsc,
2262ee4f033dSBarry Smith        0,
226397304618SKris Buschelman /*65*/ 0,
2264ee4f033dSBarry Smith        0,
2265ee4f033dSBarry Smith        0,
2266ee4f033dSBarry Smith        0,
2267ee4f033dSBarry Smith        0,
2268e34fafa9SBarry Smith /*70*/ MatGetRowMax_SeqAIJ,
2269ee4f033dSBarry Smith        0,
2270ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2271dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2272ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2273dcf5cc72SBarry Smith #else
2274dcf5cc72SBarry Smith        0,
2275dcf5cc72SBarry Smith #endif
2276ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
227797304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ,
227897304618SKris Buschelman        0,
227997304618SKris Buschelman        0,
228097304618SKris Buschelman        0,
228197304618SKris Buschelman        0,
228297304618SKris Buschelman /*80*/ 0,
228397304618SKris Buschelman        0,
228497304618SKris Buschelman        0,
228597304618SKris Buschelman        0,
2286bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2287bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
22886284ec50SHong Zhang        0,
22896284ec50SHong Zhang        0,
22906284ec50SHong Zhang        0,
2291bc011b1eSHong Zhang        0,
2292bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
229326be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
229426be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2295d439da42SKris Buschelman        MatPtAP_Basic,
22967ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
22977ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2298bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2299bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2300bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
23017ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
23027ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2303609c6c4dSKris Buschelman        0,
2304609c6c4dSKris Buschelman        0,
2305*87d4246cSBarry Smith        MatConjugate_SeqAIJ,
2306*87d4246cSBarry Smith        0,
2307*87d4246cSBarry Smith /*105*/MatSetValuesRow_SeqAIJ
23089e29f15eSvictorle };
230917ab2063SBarry Smith 
2310fb2e594dSBarry Smith EXTERN_C_BEGIN
23114a2ae208SSatish Balay #undef __FUNCT__
23124a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2313be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2314bef8e0ddSBarry Smith {
2315bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
231697f1f81fSBarry Smith   PetscInt   i,nz,n;
2317bef8e0ddSBarry Smith 
2318bef8e0ddSBarry Smith   PetscFunctionBegin;
2319bef8e0ddSBarry Smith 
2320bef8e0ddSBarry Smith   nz = aij->maxnz;
2321273d9f13SBarry Smith   n  = mat->n;
2322bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2323bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2324bef8e0ddSBarry Smith   }
2325bef8e0ddSBarry Smith   aij->nz = nz;
2326bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2327bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2328bef8e0ddSBarry Smith   }
2329bef8e0ddSBarry Smith 
2330bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2331bef8e0ddSBarry Smith }
2332fb2e594dSBarry Smith EXTERN_C_END
2333bef8e0ddSBarry Smith 
23344a2ae208SSatish Balay #undef __FUNCT__
23354a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2336bef8e0ddSBarry Smith /*@
2337bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2338bef8e0ddSBarry Smith        in the matrix.
2339bef8e0ddSBarry Smith 
2340bef8e0ddSBarry Smith   Input Parameters:
2341bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2342bef8e0ddSBarry Smith -  indices - the column indices
2343bef8e0ddSBarry Smith 
234415091d37SBarry Smith   Level: advanced
234515091d37SBarry Smith 
2346bef8e0ddSBarry Smith   Notes:
2347bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2348bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2349bef8e0ddSBarry Smith   of the MatSetValues() operation.
2350bef8e0ddSBarry Smith 
2351bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2352d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2353bef8e0ddSBarry Smith 
2354bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2355bef8e0ddSBarry Smith 
2356b9617806SBarry Smith     The indices should start with zero, not one.
2357b9617806SBarry Smith 
2358bef8e0ddSBarry Smith @*/
2359be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2360bef8e0ddSBarry Smith {
236197f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2362bef8e0ddSBarry Smith 
2363bef8e0ddSBarry Smith   PetscFunctionBegin;
23644482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
23654482741eSBarry Smith   PetscValidPointer(indices,2);
2366c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2367bef8e0ddSBarry Smith   if (f) {
2368bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2369bef8e0ddSBarry Smith   } else {
2370634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2371bef8e0ddSBarry Smith   }
2372bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2373bef8e0ddSBarry Smith }
2374bef8e0ddSBarry Smith 
2375be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2376be6bf707SBarry Smith 
2377fb2e594dSBarry Smith EXTERN_C_BEGIN
23784a2ae208SSatish Balay #undef __FUNCT__
23794a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2380be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2381be6bf707SBarry Smith {
2382be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23836849ba73SBarry Smith   PetscErrorCode ierr;
23846849ba73SBarry Smith   size_t         nz = aij->i[mat->m];
2385be6bf707SBarry Smith 
2386be6bf707SBarry Smith   PetscFunctionBegin;
2387be6bf707SBarry Smith   if (aij->nonew != 1) {
2388634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2389be6bf707SBarry Smith   }
2390be6bf707SBarry Smith 
2391be6bf707SBarry Smith   /* allocate space for values if not already there */
2392be6bf707SBarry Smith   if (!aij->saved_values) {
239387828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2394be6bf707SBarry Smith   }
2395be6bf707SBarry Smith 
2396be6bf707SBarry Smith   /* copy values over */
239787828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2398be6bf707SBarry Smith   PetscFunctionReturn(0);
2399be6bf707SBarry Smith }
2400fb2e594dSBarry Smith EXTERN_C_END
2401be6bf707SBarry Smith 
24024a2ae208SSatish Balay #undef __FUNCT__
2403b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2404be6bf707SBarry Smith /*@
2405be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2406be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2407be6bf707SBarry Smith        nonlinear portion.
2408be6bf707SBarry Smith 
2409be6bf707SBarry Smith    Collect on Mat
2410be6bf707SBarry Smith 
2411be6bf707SBarry Smith   Input Parameters:
24120e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2413be6bf707SBarry Smith 
241415091d37SBarry Smith   Level: advanced
241515091d37SBarry Smith 
2416be6bf707SBarry Smith   Common Usage, with SNESSolve():
2417be6bf707SBarry Smith $    Create Jacobian matrix
2418be6bf707SBarry Smith $    Set linear terms into matrix
2419be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2420be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2421be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2422be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2423be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2424be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2425be6bf707SBarry Smith $    In your Jacobian routine
2426be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2427be6bf707SBarry Smith $      Set nonlinear terms in matrix
2428be6bf707SBarry Smith 
2429be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2430be6bf707SBarry Smith $    // build linear portion of Jacobian
2431be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2432be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2433be6bf707SBarry Smith $    loop over nonlinear iterations
2434be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2435be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2436be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2437be6bf707SBarry Smith $       Solve linear system with Jacobian
2438be6bf707SBarry Smith $    endloop
2439be6bf707SBarry Smith 
2440be6bf707SBarry Smith   Notes:
2441be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2442be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2443be6bf707SBarry Smith     calling this routine.
2444be6bf707SBarry Smith 
24450c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
24460c468ba9SBarry Smith     and does not allocated additional space.
24470c468ba9SBarry Smith 
2448be6bf707SBarry Smith .seealso: MatRetrieveValues()
2449be6bf707SBarry Smith 
2450be6bf707SBarry Smith @*/
2451be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2452be6bf707SBarry Smith {
2453dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2454be6bf707SBarry Smith 
2455be6bf707SBarry Smith   PetscFunctionBegin;
24564482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
245729bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
245829bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2459be6bf707SBarry Smith 
2460c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2461be6bf707SBarry Smith   if (f) {
2462be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2463be6bf707SBarry Smith   } else {
2464634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2465be6bf707SBarry Smith   }
2466be6bf707SBarry Smith   PetscFunctionReturn(0);
2467be6bf707SBarry Smith }
2468be6bf707SBarry Smith 
2469fb2e594dSBarry Smith EXTERN_C_BEGIN
24704a2ae208SSatish Balay #undef __FUNCT__
24714a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2472be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2473be6bf707SBarry Smith {
2474be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
24756849ba73SBarry Smith   PetscErrorCode ierr;
247697f1f81fSBarry Smith   PetscInt       nz = aij->i[mat->m];
2477be6bf707SBarry Smith 
2478be6bf707SBarry Smith   PetscFunctionBegin;
2479be6bf707SBarry Smith   if (aij->nonew != 1) {
2480634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2481be6bf707SBarry Smith   }
2482be6bf707SBarry Smith   if (!aij->saved_values) {
2483634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2484be6bf707SBarry Smith   }
2485be6bf707SBarry Smith   /* copy values over */
248687828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2487be6bf707SBarry Smith   PetscFunctionReturn(0);
2488be6bf707SBarry Smith }
2489fb2e594dSBarry Smith EXTERN_C_END
2490be6bf707SBarry Smith 
24914a2ae208SSatish Balay #undef __FUNCT__
24924a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2493be6bf707SBarry Smith /*@
2494be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2495be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2496be6bf707SBarry Smith        nonlinear portion.
2497be6bf707SBarry Smith 
2498be6bf707SBarry Smith    Collect on Mat
2499be6bf707SBarry Smith 
2500be6bf707SBarry Smith   Input Parameters:
2501be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2502be6bf707SBarry Smith 
250315091d37SBarry Smith   Level: advanced
250415091d37SBarry Smith 
2505be6bf707SBarry Smith .seealso: MatStoreValues()
2506be6bf707SBarry Smith 
2507be6bf707SBarry Smith @*/
2508be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2509be6bf707SBarry Smith {
2510dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2511be6bf707SBarry Smith 
2512be6bf707SBarry Smith   PetscFunctionBegin;
25134482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
251429bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
251529bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2516be6bf707SBarry Smith 
2517c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2518be6bf707SBarry Smith   if (f) {
2519be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2520be6bf707SBarry Smith   } else {
2521634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2522be6bf707SBarry Smith   }
2523be6bf707SBarry Smith   PetscFunctionReturn(0);
2524be6bf707SBarry Smith }
2525be6bf707SBarry Smith 
2526f83d6046SBarry Smith 
2527be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
25284a2ae208SSatish Balay #undef __FUNCT__
25294a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
253017ab2063SBarry Smith /*@C
2531682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
25320d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
25336e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
253451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
25352bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
253617ab2063SBarry Smith 
2537db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2538db81eaa0SLois Curfman McInnes 
253917ab2063SBarry Smith    Input Parameters:
2540db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
254117ab2063SBarry Smith .  m - number of rows
254217ab2063SBarry Smith .  n - number of columns
254317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
254451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
25452bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
254617ab2063SBarry Smith 
254717ab2063SBarry Smith    Output Parameter:
2548416022c9SBarry Smith .  A - the matrix
254917ab2063SBarry Smith 
2550b259b22eSLois Curfman McInnes    Notes:
255149a6f317SBarry Smith    If nnz is given then nz is ignored
255249a6f317SBarry Smith 
255317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
255417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
25550002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
255644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
255717ab2063SBarry Smith 
255817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2559a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
25603d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
25616da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
256217ab2063SBarry Smith 
2563682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
25644fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2565682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
25666c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
25676c7ebb05SLois Curfman McInnes 
25686c7ebb05SLois Curfman McInnes    Options Database Keys:
2569698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2570698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2571db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2572db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2573db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
257417ab2063SBarry Smith 
2575027ccd11SLois Curfman McInnes    Level: intermediate
2576027ccd11SLois Curfman McInnes 
257736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
257836db0b34SBarry Smith 
257917ab2063SBarry Smith @*/
2580be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
258117ab2063SBarry Smith {
2582dfbe8321SBarry Smith   PetscErrorCode ierr;
25836945ee14SBarry Smith 
25843a40ed3dSBarry Smith   PetscFunctionBegin;
2585f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2586f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2587273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2588ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2589273d9f13SBarry Smith   PetscFunctionReturn(0);
2590273d9f13SBarry Smith }
2591273d9f13SBarry Smith 
25924a2ae208SSatish Balay #undef __FUNCT__
25934a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2594273d9f13SBarry Smith /*@C
2595273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2596273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2597273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2598273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2599273d9f13SBarry Smith 
2600273d9f13SBarry Smith    Collective on MPI_Comm
2601273d9f13SBarry Smith 
2602273d9f13SBarry Smith    Input Parameters:
260345bfc511SMatthew Knepley +  B - The matrix
2604273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2605273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2606273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2607273d9f13SBarry Smith 
2608273d9f13SBarry Smith    Notes:
260949a6f317SBarry Smith      If nnz is given then nz is ignored
261049a6f317SBarry Smith 
2611273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2612273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2613273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2614273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2615273d9f13SBarry Smith 
2616273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2617273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2618273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2619273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2620273d9f13SBarry Smith 
2621a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2622a96a251dSBarry Smith    entries or columns indices
2623a96a251dSBarry Smith 
2624273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2625273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2626273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2627273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2628273d9f13SBarry Smith 
2629273d9f13SBarry Smith    Options Database Keys:
2630698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2631698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2632273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2633273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2634273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2635273d9f13SBarry Smith 
2636273d9f13SBarry Smith    Level: intermediate
2637273d9f13SBarry Smith 
2638273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2639273d9f13SBarry Smith 
2640273d9f13SBarry Smith @*/
2641be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2642273d9f13SBarry Smith {
264397f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2644a23d5eceSKris Buschelman 
2645a23d5eceSKris Buschelman   PetscFunctionBegin;
2646a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2647a23d5eceSKris Buschelman   if (f) {
2648a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2649a23d5eceSKris Buschelman   }
2650a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2651a23d5eceSKris Buschelman }
2652a23d5eceSKris Buschelman 
2653a23d5eceSKris Buschelman EXTERN_C_BEGIN
2654a23d5eceSKris Buschelman #undef __FUNCT__
2655a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2656be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2657a23d5eceSKris Buschelman {
2658273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2659a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
26606849ba73SBarry Smith   PetscErrorCode ierr;
266197f1f81fSBarry Smith   PetscInt       i;
2662273d9f13SBarry Smith 
2663273d9f13SBarry Smith   PetscFunctionBegin;
2664d5d45c9bSBarry Smith 
2665a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2666c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2667c461c341SBarry Smith     nz             = 0;
2668c461c341SBarry Smith   }
2669c461c341SBarry Smith 
2670435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2671435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2672b73539f3SBarry Smith   if (nnz) {
2673273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
267429bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
26753a7fca6bSBarry 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);
2676b73539f3SBarry Smith     }
2677b73539f3SBarry Smith   }
2678b73539f3SBarry Smith 
2679273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2680273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2681273d9f13SBarry Smith 
2682ab93d7beSBarry Smith   if (!skipallocation) {
2683ab93d7beSBarry Smith     ierr = PetscMalloc2(B->m,PetscInt,&b->imax,B->m,PetscInt,&b->ilen);CHKERRQ(ierr);
2684273d9f13SBarry Smith     if (!nnz) {
2685435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2686273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2687273d9f13SBarry Smith       for (i=0; i<B->m; i++) b->imax[i] = nz;
2688273d9f13SBarry Smith       nz = nz*B->m;
2689273d9f13SBarry Smith     } else {
2690273d9f13SBarry Smith       nz = 0;
2691273d9f13SBarry Smith       for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2692273d9f13SBarry Smith     }
2693273d9f13SBarry Smith 
2694ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2695ab93d7beSBarry Smith     for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2696ab93d7beSBarry Smith 
2697273d9f13SBarry Smith     /* allocate the matrix space */
2698a96a251dSBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->m+1,PetscInt,&b->i);CHKERRQ(ierr);
2699bfeeae90SHong Zhang     b->i[0] = 0;
27005da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
27015da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
27025da197adSKris Buschelman     }
2703273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2704273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2705c461c341SBarry Smith   } else {
2706c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2707c461c341SBarry Smith   }
2708273d9f13SBarry Smith 
2709273d9f13SBarry Smith   b->nz                = 0;
2710273d9f13SBarry Smith   b->maxnz             = nz;
2711273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2712273d9f13SBarry Smith   PetscFunctionReturn(0);
2713273d9f13SBarry Smith }
2714a23d5eceSKris Buschelman EXTERN_C_END
2715273d9f13SBarry Smith 
2716a1661176SMatthew Knepley #undef  __FUNCT__
2717a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
2718a1661176SMatthew Knepley /*@C
2719a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2720a1661176SMatthew Knepley 
2721a1661176SMatthew Knepley    Input Parameters:
2722a1661176SMatthew Knepley +  B - the matrix
2723a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2724a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2725a1661176SMatthew Knepley -  v - optional values in the matrix
2726a1661176SMatthew Knepley 
2727d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
2728d58b2322SMatthew Knepley 
2729a1661176SMatthew Knepley    Level: developer
2730a1661176SMatthew Knepley 
2731a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2732a1661176SMatthew Knepley 
2733a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
2734a1661176SMatthew Knepley @*/
2735a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
2736a1661176SMatthew Knepley {
2737a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2738a1661176SMatthew Knepley   PetscErrorCode ierr;
2739a1661176SMatthew Knepley 
2740a1661176SMatthew Knepley   PetscFunctionBegin;
2741a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
2742a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2743a1661176SMatthew Knepley   if (f) {
2744a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2745a1661176SMatthew Knepley   }
2746a1661176SMatthew Knepley   PetscFunctionReturn(0);
2747a1661176SMatthew Knepley }
2748a1661176SMatthew Knepley 
2749a1661176SMatthew Knepley EXTERN_C_BEGIN
2750a1661176SMatthew Knepley #undef  __FUNCT__
2751a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
2752a1661176SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt I[],const PetscInt J[],const PetscScalar v[])
2753a1661176SMatthew Knepley {
2754a1661176SMatthew Knepley   PetscInt       i;
2755a1661176SMatthew Knepley   PetscInt       m,n;
2756a1661176SMatthew Knepley   PetscInt       nz;
2757a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
2758a1661176SMatthew Knepley   PetscScalar    *values;
2759a1661176SMatthew Knepley   PetscErrorCode ierr;
2760a1661176SMatthew Knepley 
2761a1661176SMatthew Knepley   PetscFunctionBegin;
2762a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
2763a1661176SMatthew Knepley 
2764a1661176SMatthew Knepley   if (I[0]) {
2765a1661176SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "I[0] must be 0 it is %D", I[0]);
2766a1661176SMatthew Knepley   }
2767a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
2768a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2769a1661176SMatthew Knepley     nz     = I[i+1]- I[i];
2770a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
2771a1661176SMatthew Knepley     if (nz < 0) {
2772a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
2773a1661176SMatthew Knepley     }
2774a1661176SMatthew Knepley     nnz[i] = nz;
2775a1661176SMatthew Knepley   }
2776a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
2777a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
2778a1661176SMatthew Knepley 
2779a1661176SMatthew Knepley   if (v) {
2780a1661176SMatthew Knepley     values = (PetscScalar*) v;
2781a1661176SMatthew Knepley   } else {
2782a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
2783a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2784a1661176SMatthew Knepley   }
2785a1661176SMatthew Knepley 
2786a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2787a1661176SMatthew Knepley 
2788a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2789a1661176SMatthew Knepley     nz  = I[i+1] - I[i];
2790a1661176SMatthew Knepley     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+I[i], values + (v ? I[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
2791a1661176SMatthew Knepley   }
2792a1661176SMatthew Knepley 
2793a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2794a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2795a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_UNSORTED);CHKERRQ(ierr);
2796a1661176SMatthew Knepley 
2797a1661176SMatthew Knepley   if (!v) {
2798a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
2799a1661176SMatthew Knepley   }
2800a1661176SMatthew Knepley   PetscFunctionReturn(0);
2801a1661176SMatthew Knepley }
2802a1661176SMatthew Knepley EXTERN_C_END
2803a1661176SMatthew Knepley 
28040bad9183SKris Buschelman /*MC
2805fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
28060bad9183SKris Buschelman    based on compressed sparse row format.
28070bad9183SKris Buschelman 
28080bad9183SKris Buschelman    Options Database Keys:
28090bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
28100bad9183SKris Buschelman 
28110bad9183SKris Buschelman   Level: beginner
28120bad9183SKris Buschelman 
2813f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
28140bad9183SKris Buschelman M*/
28150bad9183SKris Buschelman 
2816a6175056SHong Zhang EXTERN_C_BEGIN
28174a2ae208SSatish Balay #undef __FUNCT__
28184a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2819be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
2820273d9f13SBarry Smith {
2821273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2822dfbe8321SBarry Smith   PetscErrorCode ierr;
282338baddfdSBarry Smith   PetscMPIInt    size;
2824273d9f13SBarry Smith 
2825273d9f13SBarry Smith   PetscFunctionBegin;
2826273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2827273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2828273d9f13SBarry Smith 
2829273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2830273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2831273d9f13SBarry Smith 
2832b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2833b0a32e0cSBarry Smith   B->data             = (void*)b;
2834549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2835416022c9SBarry Smith   B->factor           = 0;
283690f02eecSBarry Smith   B->mapping          = 0;
2837416022c9SBarry Smith   b->row              = 0;
2838416022c9SBarry Smith   b->col              = 0;
283982bf6240SBarry Smith   b->icol             = 0;
2840b810aeb4SBarry Smith   b->reallocs         = 0;
284117ab2063SBarry Smith 
28428a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
28438a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2844a5ae1ecdSBarry Smith 
2845f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
284636db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2847f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2848416022c9SBarry Smith   b->nonew             = 0;
2849416022c9SBarry Smith   b->diag              = 0;
2850416022c9SBarry Smith   b->solve_work        = 0;
28512a1b7f2aSHong Zhang   B->spptr             = 0;
2852be6bf707SBarry Smith   b->saved_values      = 0;
2853d7f994e1SBarry Smith   b->idiag             = 0;
2854d7f994e1SBarry Smith   b->ssor              = 0;
2855f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2856a30b2313SHong Zhang   b->xtoy              = 0;
2857a30b2313SHong Zhang   b->XtoY              = 0;
285873e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
2859d487561eSHong Zhang   b->compressedrow.nrows   = B->m;
2860d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
2861d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
2862d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
286388e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
286417ab2063SBarry Smith 
286535d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
286635d8aa7fSBarry Smith 
2867f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2868bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2869bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2870f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2871be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2872bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2873f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2874be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2875bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2876a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2877a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2878a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
287985fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
288085fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
288185fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
28825fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
28835fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
28845fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2885a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2886a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2887a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
2888a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
2889a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
2890a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
289105b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
289205b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
289305b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
28944846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
28953a40ed3dSBarry Smith   PetscFunctionReturn(0);
289617ab2063SBarry Smith }
2897273d9f13SBarry Smith EXTERN_C_END
289817ab2063SBarry Smith 
28994a2ae208SSatish Balay #undef __FUNCT__
29004a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2901dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
290217ab2063SBarry Smith {
2903416022c9SBarry Smith   Mat            C;
2904416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
29056849ba73SBarry Smith   PetscErrorCode ierr;
290697f1f81fSBarry Smith   PetscInt       i,m = A->m;
290717ab2063SBarry Smith 
29083a40ed3dSBarry Smith   PetscFunctionBegin;
29094043dd9cSLois Curfman McInnes   *B = 0;
2910f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
2911f69a0ea3SMatthew Knepley   ierr = MatSetSizes(C,A->m,A->n,A->m,A->n);CHKERRQ(ierr);
2912be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
29131d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
29141d5dac46SHong Zhang 
2915273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
2916273d9f13SBarry Smith 
2917416022c9SBarry Smith   C->factor           = A->factor;
29186ad4291fSHong Zhang 
2919416022c9SBarry Smith   c->row            = 0;
2920416022c9SBarry Smith   c->col            = 0;
292182bf6240SBarry Smith   c->icol           = 0;
29226ad4291fSHong Zhang   c->reallocs       = 0;
292317ab2063SBarry Smith 
29246ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
292517ab2063SBarry Smith 
292633b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
292717ab2063SBarry Smith   for (i=0; i<m; i++) {
2928416022c9SBarry Smith     c->imax[i] = a->imax[i];
2929416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
293017ab2063SBarry Smith   }
293117ab2063SBarry Smith 
293217ab2063SBarry Smith   /* allocate the matrix space */
2933a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
2934f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
293597f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
293617ab2063SBarry Smith   if (m > 0) {
293797f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
2938be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2939bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2940be6bf707SBarry Smith     } else {
2941bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
294217ab2063SBarry Smith     }
294308480c60SBarry Smith   }
294417ab2063SBarry Smith 
2945416022c9SBarry Smith   c->sorted            = a->sorted;
29466ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
2947416022c9SBarry Smith   c->roworiented       = a->roworiented;
2948416022c9SBarry Smith   c->nonew             = a->nonew;
2949416022c9SBarry Smith   if (a->diag) {
295097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
295152e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
295217ab2063SBarry Smith     for (i=0; i<m; i++) {
2953416022c9SBarry Smith       c->diag[i] = a->diag[i];
295417ab2063SBarry Smith     }
29553a40ed3dSBarry Smith   } else c->diag        = 0;
29566ad4291fSHong Zhang   c->solve_work         = 0;
29576ad4291fSHong Zhang   c->saved_values          = 0;
29586ad4291fSHong Zhang   c->idiag                 = 0;
29596ad4291fSHong Zhang   c->ssor                  = 0;
29606ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
29616ad4291fSHong Zhang   c->freedata              = PETSC_TRUE;
29626ad4291fSHong Zhang   c->xtoy                  = 0;
29636ad4291fSHong Zhang   c->XtoY                  = 0;
29646ad4291fSHong Zhang 
2965416022c9SBarry Smith   c->nz                 = a->nz;
2966416022c9SBarry Smith   c->maxnz              = a->maxnz;
2967273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2968754ec7b1SSatish Balay 
29696ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
29706ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
29716ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
29726ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
29736ad4291fSHong Zhang     i = a->compressedrow.nrows;
29746ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
29756ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
29766ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
29776ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
297827ea64f8SHong Zhang   } else {
297927ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
298027ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
298127ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
29826ad4291fSHong Zhang   }
298388e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
29846ad4291fSHong Zhang 
29854846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
29864846f1f5SKris Buschelman 
2987416022c9SBarry Smith   *B = C;
2988b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
29893a40ed3dSBarry Smith   PetscFunctionReturn(0);
299017ab2063SBarry Smith }
299117ab2063SBarry Smith 
29924a2ae208SSatish Balay #undef __FUNCT__
29934a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2994f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A)
299517ab2063SBarry Smith {
2996416022c9SBarry Smith   Mat_SeqAIJ     *a;
2997416022c9SBarry Smith   Mat            B;
29986849ba73SBarry Smith   PetscErrorCode ierr;
29993c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
300038baddfdSBarry Smith   int            fd;
300138baddfdSBarry Smith   PetscMPIInt    size;
3002bcd2baecSBarry Smith   MPI_Comm       comm;
300317ab2063SBarry Smith 
30043a40ed3dSBarry Smith   PetscFunctionBegin;
3005e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3006d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
300729bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3008b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
30090752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3010552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
301117ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
301217ab2063SBarry Smith 
3013d64ed03dSBarry Smith   if (nz < 0) {
301429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3015d64ed03dSBarry Smith   }
3016d64ed03dSBarry Smith 
301717ab2063SBarry Smith   /* read in row lengths */
301897f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
30190752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
302017ab2063SBarry Smith 
30213c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
30223c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
30233c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
30243c601197SSatish Balay 
302517ab2063SBarry Smith   /* create our matrix */
3026f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3027f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3028b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3029ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3030416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
303117ab2063SBarry Smith 
303217ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
30330752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
303417ab2063SBarry Smith 
303517ab2063SBarry Smith   /* read in nonzero values */
30360752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
303717ab2063SBarry Smith 
303817ab2063SBarry Smith   /* set matrix "i" values */
3039efb16452SHong Zhang   a->i[0] = 0;
304017ab2063SBarry Smith   for (i=1; i<= M; i++) {
3041416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3042416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
304317ab2063SBarry Smith   }
3044606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
304517ab2063SBarry Smith 
30466d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
30476d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3048b3a1e11cSKris Buschelman   *A = B;
30493a40ed3dSBarry Smith   PetscFunctionReturn(0);
305017ab2063SBarry Smith }
305117ab2063SBarry Smith 
30524a2ae208SSatish Balay #undef __FUNCT__
3053b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3054dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
30557264ac53SSatish Balay {
30567264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3057dfbe8321SBarry Smith   PetscErrorCode ierr;
30587264ac53SSatish Balay 
30593a40ed3dSBarry Smith   PetscFunctionBegin;
3060bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3061bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
3062ca44d042SBarry Smith     *flg = PETSC_FALSE;
3063ca44d042SBarry Smith     PetscFunctionReturn(0);
3064bcd2baecSBarry Smith   }
30657264ac53SSatish Balay 
30667264ac53SSatish Balay   /* if the a->i are the same */
306797f1f81fSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3068abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
30697264ac53SSatish Balay 
30707264ac53SSatish Balay   /* if a->j are the same */
307197f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3072abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3073bcd2baecSBarry Smith 
3074bcd2baecSBarry Smith   /* if a->a are the same */
307587828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
30760f5bd95cSBarry Smith 
30773a40ed3dSBarry Smith   PetscFunctionReturn(0);
30787264ac53SSatish Balay 
30797264ac53SSatish Balay }
308036db0b34SBarry Smith 
30814a2ae208SSatish Balay #undef __FUNCT__
30824a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
308305869f15SSatish Balay /*@
308436db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
308536db0b34SBarry Smith               provided by the user.
308636db0b34SBarry Smith 
308736db0b34SBarry Smith       Coolective on MPI_Comm
308836db0b34SBarry Smith 
308936db0b34SBarry Smith    Input Parameters:
309036db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
309136db0b34SBarry Smith .   m - number of rows
309236db0b34SBarry Smith .   n - number of columns
309336db0b34SBarry Smith .   i - row indices
309436db0b34SBarry Smith .   j - column indices
309536db0b34SBarry Smith -   a - matrix values
309636db0b34SBarry Smith 
309736db0b34SBarry Smith    Output Parameter:
309836db0b34SBarry Smith .   mat - the matrix
309936db0b34SBarry Smith 
310036db0b34SBarry Smith    Level: intermediate
310136db0b34SBarry Smith 
310236db0b34SBarry Smith    Notes:
31030551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
310436db0b34SBarry Smith     once the matrix is destroyed
310536db0b34SBarry Smith 
310636db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
310736db0b34SBarry Smith 
3108bfeeae90SHong Zhang        The i and j indices are 0 based
310936db0b34SBarry Smith 
311036db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
311136db0b34SBarry Smith 
311236db0b34SBarry Smith @*/
3113be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
311436db0b34SBarry Smith {
3115dfbe8321SBarry Smith   PetscErrorCode ierr;
311697f1f81fSBarry Smith   PetscInt       ii;
311736db0b34SBarry Smith   Mat_SeqAIJ     *aij;
311836db0b34SBarry Smith 
311936db0b34SBarry Smith   PetscFunctionBegin;
3120a96a251dSBarry Smith   if (i[0]) {
3121634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
312236db0b34SBarry Smith   }
3123f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3124f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3125ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3126ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3127ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3128ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3129ab93d7beSBarry Smith 
313036db0b34SBarry Smith   aij->i = i;
313136db0b34SBarry Smith   aij->j = j;
313236db0b34SBarry Smith   aij->a = a;
313336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
313436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
313536db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
313636db0b34SBarry Smith 
313736db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
313836db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
31392515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
314079a5c55eSBarry 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]);
314136db0b34SBarry Smith #endif
314236db0b34SBarry Smith   }
31432515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
314436db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
314579a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
314679a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
314736db0b34SBarry Smith   }
314836db0b34SBarry Smith #endif
314936db0b34SBarry Smith 
3150b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3151b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
315236db0b34SBarry Smith   PetscFunctionReturn(0);
315336db0b34SBarry Smith }
315436db0b34SBarry Smith 
3155cc8ba8e1SBarry Smith #undef __FUNCT__
3156ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3157dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3158cc8ba8e1SBarry Smith {
3159dfbe8321SBarry Smith   PetscErrorCode ierr;
3160cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
316136db0b34SBarry Smith 
3162cc8ba8e1SBarry Smith   PetscFunctionBegin;
316312c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
3164cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3165cc8ba8e1SBarry Smith     a->coloring = coloring;
316612c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
316797f1f81fSBarry Smith     PetscInt             i,*larray;
316812c595b3SBarry Smith     ISColoring      ocoloring;
316908b6dcc0SBarry Smith     ISColoringValue *colors;
317012c595b3SBarry Smith 
317112c595b3SBarry Smith     /* set coloring for diagonal portion */
317297f1f81fSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
317312c595b3SBarry Smith     for (i=0; i<A->n; i++) {
317412c595b3SBarry Smith       larray[i] = i;
317512c595b3SBarry Smith     }
317612c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
317708b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
317812c595b3SBarry Smith     for (i=0; i<A->n; i++) {
317912c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
318012c595b3SBarry Smith     }
318112c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
318212c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
318312c595b3SBarry Smith     a->coloring = ocoloring;
318412c595b3SBarry Smith   }
3185cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3186cc8ba8e1SBarry Smith }
3187cc8ba8e1SBarry Smith 
3188dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3189ee4f033dSBarry Smith EXTERN_C_BEGIN
319029c1e371SBarry Smith #include "adic/ad_utils.h"
3191ee4f033dSBarry Smith EXTERN_C_END
3192cc8ba8e1SBarry Smith 
3193cc8ba8e1SBarry Smith #undef __FUNCT__
3194ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3195dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3196cc8ba8e1SBarry Smith {
3197cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
319897f1f81fSBarry Smith   PetscInt        m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
31994440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
320008b6dcc0SBarry Smith   ISColoringValue *color;
3201cc8ba8e1SBarry Smith 
3202cc8ba8e1SBarry Smith   PetscFunctionBegin;
3203e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
32044440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3205cc8ba8e1SBarry Smith   color = a->coloring->colors;
3206cc8ba8e1SBarry Smith   /* loop over rows */
3207cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3208cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3209cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3210cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3211cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3212cc8ba8e1SBarry Smith     }
32134440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3214ee4f033dSBarry Smith   }
3215ee4f033dSBarry Smith   PetscFunctionReturn(0);
3216ee4f033dSBarry Smith }
3217ee4f033dSBarry Smith #endif
3218ee4f033dSBarry Smith 
3219ee4f033dSBarry Smith #undef __FUNCT__
3220ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
322197f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3222ee4f033dSBarry Smith {
3223ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
322497f1f81fSBarry Smith   PetscInt             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
322587828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
322608b6dcc0SBarry Smith   ISColoringValue *color;
3227ee4f033dSBarry Smith 
3228ee4f033dSBarry Smith   PetscFunctionBegin;
3229e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3230ee4f033dSBarry Smith   color = a->coloring->colors;
3231ee4f033dSBarry Smith   /* loop over rows */
3232ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3233ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3234ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3235ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3236ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3237ee4f033dSBarry Smith     }
3238ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3239cc8ba8e1SBarry Smith   }
3240cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3241cc8ba8e1SBarry Smith }
324236db0b34SBarry Smith 
3243