xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 407f6b053baf20633f092eab651bee7ba44b562b)
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 PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1679299369SBarry Smith {
1779299369SBarry Smith   PetscErrorCode ierr;
1879299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
19899cda47SBarry Smith   PetscInt       i,*diag, m = Y->rmap.n;
2079299369SBarry Smith   PetscScalar    *v,*aa = aij->a;
2109f38230SBarry Smith   PetscTruth     missing;
2279299369SBarry Smith 
2379299369SBarry Smith   PetscFunctionBegin;
2409f38230SBarry Smith   if (Y->assembled) {
2509f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2609f38230SBarry Smith     if (!missing) {
2779299369SBarry Smith       diag = aij->diag;
2879299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
2979299369SBarry Smith       if (is == INSERT_VALUES) {
3079299369SBarry Smith 	for (i=0; i<m; i++) {
3179299369SBarry Smith 	  aa[diag[i]] = v[i];
3279299369SBarry Smith 	}
3379299369SBarry Smith       } else {
3479299369SBarry Smith 	for (i=0; i<m; i++) {
3579299369SBarry Smith 	  aa[diag[i]] += v[i];
3679299369SBarry Smith 	}
3779299369SBarry Smith       }
3879299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
3979299369SBarry Smith       PetscFunctionReturn(0);
4079299369SBarry Smith     }
4109f38230SBarry Smith   }
4209f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4309f38230SBarry Smith   PetscFunctionReturn(0);
4409f38230SBarry Smith }
4579299369SBarry Smith 
4679299369SBarry Smith #undef __FUNCT__
474a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
4897f1f81fSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
4917ab2063SBarry Smith {
50416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
51dfbe8321SBarry Smith   PetscErrorCode ierr;
5297f1f81fSBarry Smith   PetscInt       i,ishift;
5317ab2063SBarry Smith 
543a40ed3dSBarry Smith   PetscFunctionBegin;
55899cda47SBarry Smith   *m     = A->rmap.n;
563a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
57bfeeae90SHong Zhang   ishift = 0;
5853e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
59899cda47SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
60bfeeae90SHong Zhang   } else if (oshift == 1) {
61899cda47SBarry Smith     PetscInt nz = a->i[A->rmap.n];
623b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
63899cda47SBarry Smith     ierr = PetscMalloc((A->rmap.n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
6497f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
653b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
66899cda47SBarry Smith     for (i=0; i<A->rmap.n+1; i++) (*ia)[i] = a->i[i] + 1;
676945ee14SBarry Smith   } else {
686945ee14SBarry Smith     *ia = a->i; *ja = a->j;
69a2ce50c7SBarry Smith   }
703a40ed3dSBarry Smith   PetscFunctionReturn(0);
71a2744918SBarry Smith }
72a2744918SBarry Smith 
734a2ae208SSatish Balay #undef __FUNCT__
744a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
7597f1f81fSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
766945ee14SBarry Smith {
77dfbe8321SBarry Smith   PetscErrorCode ierr;
786945ee14SBarry Smith 
793a40ed3dSBarry Smith   PetscFunctionBegin;
803a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
81bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
82606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
83606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
84bcd2baecSBarry Smith   }
853a40ed3dSBarry Smith   PetscFunctionReturn(0);
8617ab2063SBarry Smith }
8717ab2063SBarry Smith 
884a2ae208SSatish Balay #undef __FUNCT__
894a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
9097f1f81fSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
913b2fbd54SBarry Smith {
923b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
93dfbe8321SBarry Smith   PetscErrorCode ierr;
94899cda47SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap.n,m = A->rmap.n;
9597f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
963b2fbd54SBarry Smith 
973a40ed3dSBarry Smith   PetscFunctionBegin;
98899cda47SBarry Smith   *nn = n;
993a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1003b2fbd54SBarry Smith   if (symmetric) {
101899cda47SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1023b2fbd54SBarry Smith   } else {
10397f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10497f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
10597f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
10697f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1073b2fbd54SBarry Smith     jj = a->j;
1083b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
109bfeeae90SHong Zhang       collengths[jj[i]]++;
1103b2fbd54SBarry Smith     }
1113b2fbd54SBarry Smith     cia[0] = oshift;
1123b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1133b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1143b2fbd54SBarry Smith     }
11597f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1163b2fbd54SBarry Smith     jj   = a->j;
117a93ec695SBarry Smith     for (row=0; row<m; row++) {
118a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
119a93ec695SBarry Smith       for (i=0; i<mr; i++) {
120bfeeae90SHong Zhang         col = *jj++;
1213b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1223b2fbd54SBarry Smith       }
1233b2fbd54SBarry Smith     }
124606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1253b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1263b2fbd54SBarry Smith   }
1273a40ed3dSBarry Smith   PetscFunctionReturn(0);
1283b2fbd54SBarry Smith }
1293b2fbd54SBarry Smith 
1304a2ae208SSatish Balay #undef __FUNCT__
1314a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
13297f1f81fSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1333b2fbd54SBarry Smith {
134dfbe8321SBarry Smith   PetscErrorCode ierr;
135606d414cSSatish Balay 
1363a40ed3dSBarry Smith   PetscFunctionBegin;
1373a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1383b2fbd54SBarry Smith 
139606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
140606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1413b2fbd54SBarry Smith 
1423a40ed3dSBarry Smith   PetscFunctionReturn(0);
1433b2fbd54SBarry Smith }
1443b2fbd54SBarry Smith 
14587d4246cSBarry Smith #undef __FUNCT__
14687d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
14787d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
14887d4246cSBarry Smith {
14987d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15087d4246cSBarry Smith   PetscInt       *ai = a->i;
15187d4246cSBarry Smith   PetscErrorCode ierr;
15287d4246cSBarry Smith 
15387d4246cSBarry Smith   PetscFunctionBegin;
15487d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
15587d4246cSBarry Smith   PetscFunctionReturn(0);
15687d4246cSBarry Smith }
15787d4246cSBarry Smith 
158227d817aSBarry Smith #define CHUNKSIZE   15
15917ab2063SBarry Smith 
1604a2ae208SSatish Balay #undef __FUNCT__
1614a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16297f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16317ab2063SBarry Smith {
164416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
165e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
16697f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1676849ba73SBarry Smith   PetscErrorCode ierr;
168e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
16987828ca2SBarry Smith   PetscScalar    *ap,value,*aa = a->a;
17036db0b34SBarry Smith   PetscTruth     ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
171273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17217ab2063SBarry Smith 
1733a40ed3dSBarry Smith   PetscFunctionBegin;
17417ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
175416022c9SBarry Smith     row  = im[k];
1765ef9f2a5SBarry Smith     if (row < 0) continue;
1772515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
178899cda47SBarry Smith     if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1);
1793b2fbd54SBarry Smith #endif
180bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18117ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
182416022c9SBarry Smith     low  = 0;
183c71e6ed7SBarry Smith     high = nrow;
18417ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1855ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1862515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
187899cda47SBarry Smith       if (in[l] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap.n-1);
1883b2fbd54SBarry Smith #endif
189bfeeae90SHong Zhang       col = in[l];
1904b0e389bSBarry Smith       if (roworiented) {
1915ef9f2a5SBarry Smith         value = v[l + k*n];
192bef8e0ddSBarry Smith       } else {
1934b0e389bSBarry Smith         value = v[k + l*m];
1944b0e389bSBarry Smith       }
195abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
19636db0b34SBarry Smith 
1977cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
198e2ee6c50SBarry Smith       lastcol = col;
199416022c9SBarry Smith       while (high-low > 5) {
200416022c9SBarry Smith         t = (low+high)/2;
201416022c9SBarry Smith         if (rp[t] > col) high = t;
202416022c9SBarry Smith         else             low  = t;
20317ab2063SBarry Smith       }
204416022c9SBarry Smith       for (i=low; i<high; i++) {
20517ab2063SBarry Smith         if (rp[i] > col) break;
20617ab2063SBarry Smith         if (rp[i] == col) {
207416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
20817ab2063SBarry Smith           else                  ap[i] = value;
20917ab2063SBarry Smith           goto noinsert;
21017ab2063SBarry Smith         }
21117ab2063SBarry Smith       }
212abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
213c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
214cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
215421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
216c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
217416022c9SBarry Smith       /* shift up all the later entries in this row */
218416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
21917ab2063SBarry Smith         rp[ii+1] = rp[ii];
22017ab2063SBarry Smith         ap[ii+1] = ap[ii];
22117ab2063SBarry Smith       }
22217ab2063SBarry Smith       rp[i] = col;
22317ab2063SBarry Smith       ap[i] = value;
22417ab2063SBarry Smith       noinsert:;
225416022c9SBarry Smith       low = i + 1;
22617ab2063SBarry Smith     }
22717ab2063SBarry Smith     ailen[row] = nrow;
22817ab2063SBarry Smith   }
22988e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2303a40ed3dSBarry Smith   PetscFunctionReturn(0);
23117ab2063SBarry Smith }
23217ab2063SBarry Smith 
23381824310SBarry Smith 
2344a2ae208SSatish Balay #undef __FUNCT__
2354a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
23697f1f81fSBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2377eb43aa7SLois Curfman McInnes {
2387eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
23997f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
24097f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
24187828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
2427eb43aa7SLois Curfman McInnes 
2433a40ed3dSBarry Smith   PetscFunctionBegin;
2447eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2457eb43aa7SLois Curfman McInnes     row  = im[k];
24677431f27SBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row);
247899cda47SBarry Smith     if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1);
248bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2497eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2507eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
25177431f27SBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]);
252899cda47SBarry Smith       if (in[l] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap.n-1);
253bfeeae90SHong Zhang       col = in[l] ;
2547eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2557eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2567eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2577eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2587eb43aa7SLois Curfman McInnes         else             low  = t;
2597eb43aa7SLois Curfman McInnes       }
2607eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2617eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2627eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
263b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2647eb43aa7SLois Curfman McInnes           goto finished;
2657eb43aa7SLois Curfman McInnes         }
2667eb43aa7SLois Curfman McInnes       }
267b49de8d1SLois Curfman McInnes       *v++ = zero;
2687eb43aa7SLois Curfman McInnes       finished:;
2697eb43aa7SLois Curfman McInnes     }
2707eb43aa7SLois Curfman McInnes   }
2713a40ed3dSBarry Smith   PetscFunctionReturn(0);
2727eb43aa7SLois Curfman McInnes }
2737eb43aa7SLois Curfman McInnes 
27417ab2063SBarry Smith 
2754a2ae208SSatish Balay #undef __FUNCT__
2764a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
277dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
27817ab2063SBarry Smith {
279416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2806849ba73SBarry Smith   PetscErrorCode ierr;
2816f69ff64SBarry Smith   PetscInt       i,*col_lens;
2826f69ff64SBarry Smith   int            fd;
28317ab2063SBarry Smith 
2843a40ed3dSBarry Smith   PetscFunctionBegin;
285b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
286899cda47SBarry Smith   ierr = PetscMalloc((4+A->rmap.n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
287552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
288899cda47SBarry Smith   col_lens[1] = A->rmap.n;
289899cda47SBarry Smith   col_lens[2] = A->cmap.n;
290416022c9SBarry Smith   col_lens[3] = a->nz;
291416022c9SBarry Smith 
292416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
293899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
294416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
29517ab2063SBarry Smith   }
296899cda47SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap.n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
297606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
298416022c9SBarry Smith 
299416022c9SBarry Smith   /* store column indices (zero start index) */
3006f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
301416022c9SBarry Smith 
302416022c9SBarry Smith   /* store nonzero values */
3036f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3043a40ed3dSBarry Smith   PetscFunctionReturn(0);
30517ab2063SBarry Smith }
306416022c9SBarry Smith 
307dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
308cd155464SBarry Smith 
3094a2ae208SSatish Balay #undef __FUNCT__
3104a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
311dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
312416022c9SBarry Smith {
313416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
314dfbe8321SBarry Smith   PetscErrorCode    ierr;
315899cda47SBarry Smith   PetscInt          i,j,m = A->rmap.n,shift=0;
316e060cb09SBarry Smith   const char        *name;
317f3ef73ceSBarry Smith   PetscViewerFormat format;
31817ab2063SBarry Smith 
3193a40ed3dSBarry Smith   PetscFunctionBegin;
320435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
321b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
32271c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
32397f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
324899cda47SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap.n-!shift)) {
325d00d2cf4SBarry Smith       nofinalvalue = 1;
326d00d2cf4SBarry Smith     }
327b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
328899cda47SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap.n);CHKERRQ(ierr);
32977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
33077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
331b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
33217ab2063SBarry Smith 
33317ab2063SBarry Smith     for (i=0; i<m; i++) {
334416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
335aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
33677431f27SBarry 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);
33717ab2063SBarry Smith #else
33877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
33917ab2063SBarry Smith #endif
34017ab2063SBarry Smith       }
34117ab2063SBarry Smith     }
342d00d2cf4SBarry Smith     if (nofinalvalue) {
343899cda47SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap.n,0.0);CHKERRQ(ierr);
344d00d2cf4SBarry Smith     }
345fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
346b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
34768369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
348cd155464SBarry Smith      PetscFunctionReturn(0);
349fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
350b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
35144cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
35277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
35344cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
354aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
35536db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
356a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35736db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
358a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35936db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
360a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3616831982aSBarry Smith         }
36244cd7ae7SLois Curfman McInnes #else
363a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
36444cd7ae7SLois Curfman McInnes #endif
36544cd7ae7SLois Curfman McInnes       }
366b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
36744cd7ae7SLois Curfman McInnes     }
368b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
369fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
37097f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
371b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
37297f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
373496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
374496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
375496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
376496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
377aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
37836db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
379496be53dSLois Curfman McInnes #else
380496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
381496be53dSLois Curfman McInnes #endif
382496be53dSLois Curfman McInnes         }
383496be53dSLois Curfman McInnes       }
384496be53dSLois Curfman McInnes     }
3852e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
38677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3872e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
38877431f27SBarry 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);}
38977431f27SBarry 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);}
39077431f27SBarry 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);}
39177431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
39277431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
39377431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
394496be53dSLois Curfman McInnes     }
395b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
396606d414cSSatish Balay     ierr = PetscFree(sptr);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++) {
39977431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
400496be53dSLois Curfman McInnes       }
401b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
402496be53dSLois Curfman McInnes     }
403b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
404496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
405496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
406496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
407aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
40836db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
409b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4106831982aSBarry Smith           }
411496be53dSLois Curfman McInnes #else
412b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
413496be53dSLois Curfman McInnes #endif
414496be53dSLois Curfman McInnes         }
415496be53dSLois Curfman McInnes       }
416b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
417496be53dSLois Curfman McInnes     }
418b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
419fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
42097f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
42187828ca2SBarry Smith     PetscScalar value;
42202594712SBarry Smith 
423b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
42402594712SBarry Smith     for (i=0; i<m; i++) {
42502594712SBarry Smith       jcnt = 0;
426899cda47SBarry Smith       for (j=0; j<A->cmap.n; j++) {
427e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
42802594712SBarry Smith           value = a->a[cnt++];
429e24b481bSBarry Smith           jcnt++;
43002594712SBarry Smith         } else {
43102594712SBarry Smith           value = 0.0;
43202594712SBarry Smith         }
433aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
434b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
43502594712SBarry Smith #else
436b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
43702594712SBarry Smith #endif
43802594712SBarry Smith       }
439b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44002594712SBarry Smith     }
441b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4423a40ed3dSBarry Smith   } else {
443b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44417ab2063SBarry Smith     for (i=0; i<m; i++) {
44577431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
446416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
447aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44836db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
449a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
45036db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
451a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4523a40ed3dSBarry Smith         } else {
453a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
45417ab2063SBarry Smith         }
45517ab2063SBarry Smith #else
456a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
45717ab2063SBarry Smith #endif
45817ab2063SBarry Smith       }
459b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
46017ab2063SBarry Smith     }
461b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
46217ab2063SBarry Smith   }
463b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4643a40ed3dSBarry Smith   PetscFunctionReturn(0);
465416022c9SBarry Smith }
466416022c9SBarry Smith 
4674a2ae208SSatish Balay #undef __FUNCT__
4684a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
469dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
470416022c9SBarry Smith {
471480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
472416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
473dfbe8321SBarry Smith   PetscErrorCode    ierr;
474899cda47SBarry Smith   PetscInt          i,j,m = A->rmap.n,color;
47536db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
476b0a32e0cSBarry Smith   PetscViewer       viewer;
477f3ef73ceSBarry Smith   PetscViewerFormat format;
478cddf8d76SBarry Smith 
4793a40ed3dSBarry Smith   PetscFunctionBegin;
480480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
481b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
48219bcc07fSBarry Smith 
483b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
484416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4850513a670SBarry Smith 
486fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4870513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
488b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
489416022c9SBarry Smith     for (i=0; i<m; i++) {
490cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
491bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
492bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
493aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49436db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
495cddf8d76SBarry Smith #else
496cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
497cddf8d76SBarry Smith #endif
498b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
499cddf8d76SBarry Smith       }
500cddf8d76SBarry Smith     }
501b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
502cddf8d76SBarry Smith     for (i=0; i<m; i++) {
503cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
504bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
505bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
506cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
507b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
508cddf8d76SBarry Smith       }
509cddf8d76SBarry Smith     }
510b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
511cddf8d76SBarry Smith     for (i=0; i<m; i++) {
512cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
513bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
514bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
515aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
51636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
517cddf8d76SBarry Smith #else
518cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
519cddf8d76SBarry Smith #endif
520b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
521416022c9SBarry Smith       }
522416022c9SBarry Smith     }
5230513a670SBarry Smith   } else {
5240513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5250513a670SBarry Smith     /* first determine max of all nonzero values */
52697f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
527b0a32e0cSBarry Smith     PetscDraw   popup;
52836db0b34SBarry Smith     PetscReal scale;
5290513a670SBarry Smith 
5300513a670SBarry Smith     for (i=0; i<nz; i++) {
5310513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5320513a670SBarry Smith     }
533b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
534b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
535b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5360513a670SBarry Smith     count = 0;
5370513a670SBarry Smith     for (i=0; i<m; i++) {
5380513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
539bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
540bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
54197f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
542b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5430513a670SBarry Smith         count++;
5440513a670SBarry Smith       }
5450513a670SBarry Smith     }
5460513a670SBarry Smith   }
547480ef9eaSBarry Smith   PetscFunctionReturn(0);
548480ef9eaSBarry Smith }
549cddf8d76SBarry Smith 
5504a2ae208SSatish Balay #undef __FUNCT__
5514a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
552dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
553480ef9eaSBarry Smith {
554dfbe8321SBarry Smith   PetscErrorCode ierr;
555b0a32e0cSBarry Smith   PetscDraw      draw;
55636db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
557480ef9eaSBarry Smith   PetscTruth     isnull;
558480ef9eaSBarry Smith 
559480ef9eaSBarry Smith   PetscFunctionBegin;
560b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
561b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
562480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
563480ef9eaSBarry Smith 
564480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
565899cda47SBarry Smith   xr  = A->cmap.n; yr = A->rmap.n; h = yr/10.0; w = xr/10.0;
566480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
567b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
568b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
569480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5703a40ed3dSBarry Smith   PetscFunctionReturn(0);
571416022c9SBarry Smith }
572416022c9SBarry Smith 
5734a2ae208SSatish Balay #undef __FUNCT__
5744a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
575dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
576416022c9SBarry Smith {
577dfbe8321SBarry Smith   PetscErrorCode ierr;
5786805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
579416022c9SBarry Smith 
5803a40ed3dSBarry Smith   PetscFunctionBegin;
58132077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
582fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
583fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
584c45a1595SBarry Smith   if (iascii) {
5853a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5860f5bd95cSBarry Smith   } else if (isbinary) {
5873a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5880f5bd95cSBarry Smith   } else if (isdraw) {
5893a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5905cd90555SBarry Smith   } else {
591958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
59217ab2063SBarry Smith   }
5934846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
5943a40ed3dSBarry Smith   PetscFunctionReturn(0);
59517ab2063SBarry Smith }
59619bcc07fSBarry Smith 
5974a2ae208SSatish Balay #undef __FUNCT__
5984a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
599dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
60017ab2063SBarry Smith {
601416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6026849ba73SBarry Smith   PetscErrorCode ierr;
60397f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
604899cda47SBarry Smith   PetscInt       m = A->rmap.n,*ip,N,*ailen = a->ilen,rmax = 0;
60587828ca2SBarry Smith   PetscScalar    *aa = a->a,*ap;
6063447b6efSHong Zhang   PetscReal      ratio=0.6;
60717ab2063SBarry Smith 
6083a40ed3dSBarry Smith   PetscFunctionBegin;
6093a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
61017ab2063SBarry Smith 
61143ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
61217ab2063SBarry Smith   for (i=1; i<m; i++) {
613416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
61417ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
61594a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
61617ab2063SBarry Smith     if (fshift) {
617bfeeae90SHong Zhang       ip = aj + ai[i] ;
618bfeeae90SHong Zhang       ap = aa + ai[i] ;
61917ab2063SBarry Smith       N  = ailen[i];
62017ab2063SBarry Smith       for (j=0; j<N; j++) {
62117ab2063SBarry Smith         ip[j-fshift] = ip[j];
62217ab2063SBarry Smith         ap[j-fshift] = ap[j];
62317ab2063SBarry Smith       }
62417ab2063SBarry Smith     }
62517ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
62617ab2063SBarry Smith   }
62717ab2063SBarry Smith   if (m) {
62817ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
62917ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
63017ab2063SBarry Smith   }
63117ab2063SBarry Smith   /* reset ilen and imax for each row */
63217ab2063SBarry Smith   for (i=0; i<m; i++) {
63317ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
63417ab2063SBarry Smith   }
635bfeeae90SHong Zhang   a->nz = ai[m];
63617ab2063SBarry Smith 
63709f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
638899cda47SBarry Smith   ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap.n,fshift,a->nz);CHKERRQ(ierr);
639ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
640ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
641a0e1a203SBarry Smith 
642dd5f02e7SSatish Balay   a->reallocs          = 0;
6434e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
64436db0b34SBarry Smith   a->rmax              = rmax;
6454e220ebcSLois Curfman McInnes 
646cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
647317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
64888e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
64971c2f376SKris Buschelman 
6504846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
6513a40ed3dSBarry Smith   PetscFunctionReturn(0);
65217ab2063SBarry Smith }
65317ab2063SBarry Smith 
6544a2ae208SSatish Balay #undef __FUNCT__
65599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
65699cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
65799cafbc1SBarry Smith {
65899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
65999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
66099cafbc1SBarry Smith   PetscScalar    *aa = a->a;
66199cafbc1SBarry Smith 
66299cafbc1SBarry Smith   PetscFunctionBegin;
66399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
66499cafbc1SBarry Smith   PetscFunctionReturn(0);
66599cafbc1SBarry Smith }
66699cafbc1SBarry Smith 
66799cafbc1SBarry Smith #undef __FUNCT__
66899cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
66999cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
67099cafbc1SBarry Smith {
67199cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
67299cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
67399cafbc1SBarry Smith   PetscScalar    *aa = a->a;
67499cafbc1SBarry Smith 
67599cafbc1SBarry Smith   PetscFunctionBegin;
67699cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
67799cafbc1SBarry Smith   PetscFunctionReturn(0);
67899cafbc1SBarry Smith }
67999cafbc1SBarry Smith 
68099cafbc1SBarry Smith #undef __FUNCT__
6814a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
682dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
68317ab2063SBarry Smith {
684416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
685dfbe8321SBarry Smith   PetscErrorCode ierr;
6863a40ed3dSBarry Smith 
6873a40ed3dSBarry Smith   PetscFunctionBegin;
688899cda47SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr);
6893a40ed3dSBarry Smith   PetscFunctionReturn(0);
69017ab2063SBarry Smith }
691416022c9SBarry Smith 
6924a2ae208SSatish Balay #undef __FUNCT__
6934a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
694dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
69517ab2063SBarry Smith {
696416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
697dfbe8321SBarry Smith   PetscErrorCode ierr;
698d5d45c9bSBarry Smith 
6993a40ed3dSBarry Smith   PetscFunctionBegin;
700aa482453SBarry Smith #if defined(PETSC_USE_LOG)
701899cda47SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap.n,A->cmap.n,a->nz);
70217ab2063SBarry Smith #endif
703e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
704c38d4ed2SBarry Smith   if (a->row) {
705c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
706c38d4ed2SBarry Smith   }
707c38d4ed2SBarry Smith   if (a->col) {
708c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
709c38d4ed2SBarry Smith   }
71005b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
71105b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
71205b42c5fSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
71305b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
71482bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
71505b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
716cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
71705b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
718*407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
719d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
720a30b2313SHong Zhang 
7214846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7224846f1f5SKris Buschelman 
723606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
724901853e0SKris Buschelman 
725dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
726901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
727901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
728901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
729901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
730901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
731ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
732901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
733901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
734a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
735901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7363a40ed3dSBarry Smith   PetscFunctionReturn(0);
73717ab2063SBarry Smith }
73817ab2063SBarry Smith 
7394a2ae208SSatish Balay #undef __FUNCT__
7404a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
741dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
74217ab2063SBarry Smith {
7433a40ed3dSBarry Smith   PetscFunctionBegin;
7443a40ed3dSBarry Smith   PetscFunctionReturn(0);
74517ab2063SBarry Smith }
74617ab2063SBarry Smith 
7474a2ae208SSatish Balay #undef __FUNCT__
7484a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
749dfbe8321SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op)
75017ab2063SBarry Smith {
751416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7524846f1f5SKris Buschelman   PetscErrorCode ierr;
7533a40ed3dSBarry Smith 
7543a40ed3dSBarry Smith   PetscFunctionBegin;
755a65d3064SKris Buschelman   switch (op) {
756a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
757a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
758a65d3064SKris Buschelman       break;
759a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
760a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
761a65d3064SKris Buschelman       break;
762a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
763a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
764a65d3064SKris Buschelman       break;
765a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
766a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
767a65d3064SKris Buschelman       break;
768a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
769a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
770a65d3064SKris Buschelman       break;
771a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
772a65d3064SKris Buschelman       a->nonew             = 1;
773a65d3064SKris Buschelman       break;
774a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
775a65d3064SKris Buschelman       a->nonew             = -1;
776a65d3064SKris Buschelman       break;
777a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
778a65d3064SKris Buschelman       a->nonew             = -2;
779a65d3064SKris Buschelman       break;
780a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
781a65d3064SKris Buschelman       a->nonew             = 0;
782a65d3064SKris Buschelman       break;
783a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
784a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
785a65d3064SKris Buschelman       break;
786d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
787d487561eSHong Zhang       a->compressedrow.use = PETSC_TRUE;
788d487561eSHong Zhang       break;
789d487561eSHong Zhang     case MAT_DO_NOT_USE_COMPRESSEDROW:
790d487561eSHong Zhang       a->compressedrow.use = PETSC_FALSE;
791d487561eSHong Zhang       break;
792a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
793a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
794a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
795a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
796a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
797290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
798a65d3064SKris Buschelman       break;
799a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
80029bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
801a65d3064SKris Buschelman     default:
80271c2f376SKris Buschelman       break;
803a65d3064SKris Buschelman   }
8044846f1f5SKris Buschelman   ierr = MatSetOption_Inode(A,op);CHKERRQ(ierr);
8053a40ed3dSBarry Smith   PetscFunctionReturn(0);
80617ab2063SBarry Smith }
80717ab2063SBarry Smith 
8084a2ae208SSatish Balay #undef __FUNCT__
8094a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
810dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
81117ab2063SBarry Smith {
812416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8136849ba73SBarry Smith   PetscErrorCode ierr;
81497f1f81fSBarry Smith   PetscInt       i,j,n;
81587828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
81617ab2063SBarry Smith 
8173a40ed3dSBarry Smith   PetscFunctionBegin;
8182dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8191ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
82036db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
821899cda47SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
822899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
823bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
824bfeeae90SHong Zhang       if (a->j[j] == i) {
825416022c9SBarry Smith         x[i] = a->a[j];
82617ab2063SBarry Smith         break;
82717ab2063SBarry Smith       }
82817ab2063SBarry Smith     }
82917ab2063SBarry Smith   }
8301ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8313a40ed3dSBarry Smith   PetscFunctionReturn(0);
83217ab2063SBarry Smith }
83317ab2063SBarry Smith 
8344a2ae208SSatish Balay #undef __FUNCT__
8354a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
836dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
83717ab2063SBarry Smith {
838416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8395c897100SBarry Smith   PetscScalar       *x,*y;
840dfbe8321SBarry Smith   PetscErrorCode    ierr;
841899cda47SBarry Smith   PetscInt          m = A->rmap.n;
8425c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8435c897100SBarry Smith   PetscScalar       *v,alpha;
8447b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8453447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8464eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8475c897100SBarry Smith #endif
84817ab2063SBarry Smith 
8493a40ed3dSBarry Smith   PetscFunctionBegin;
8502e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8511ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8521ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8535c897100SBarry Smith 
8545c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
855bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8565c897100SBarry Smith #else
8573447b6efSHong Zhang   if (usecprow){
8583447b6efSHong Zhang     m    = cprow.nrows;
8593447b6efSHong Zhang     ii   = cprow.i;
8607b2bb3b9SHong Zhang     ridx = cprow.rindex;
8613447b6efSHong Zhang   } else {
8623447b6efSHong Zhang     ii = a->i;
8633447b6efSHong Zhang   }
86417ab2063SBarry Smith   for (i=0; i<m; i++) {
8653447b6efSHong Zhang     idx   = a->j + ii[i] ;
8663447b6efSHong Zhang     v     = a->a + ii[i] ;
8673447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8683447b6efSHong Zhang     if (usecprow){
8697b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8703447b6efSHong Zhang     } else {
87117ab2063SBarry Smith       alpha = x[i];
8723447b6efSHong Zhang     }
87317ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
87417ab2063SBarry Smith   }
8755c897100SBarry Smith #endif
876efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
8771ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8781ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8793a40ed3dSBarry Smith   PetscFunctionReturn(0);
88017ab2063SBarry Smith }
88117ab2063SBarry Smith 
8824a2ae208SSatish Balay #undef __FUNCT__
8835c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
884dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8855c897100SBarry Smith {
8868d5b0100SBarry Smith   PetscScalar    zero = 0.0;
887dfbe8321SBarry Smith   PetscErrorCode ierr;
8885c897100SBarry Smith 
8895c897100SBarry Smith   PetscFunctionBegin;
8902dcb1b2aSMatthew Knepley   ierr = VecSet(yy,zero);CHKERRQ(ierr);
8915c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8925c897100SBarry Smith   PetscFunctionReturn(0);
8935c897100SBarry Smith }
8945c897100SBarry Smith 
8955c897100SBarry Smith 
8965c897100SBarry Smith #undef __FUNCT__
8974a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
898dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
89917ab2063SBarry Smith {
900416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
90197952fefSHong Zhang   PetscScalar    *x,*y,*aa;
902dfbe8321SBarry Smith   PetscErrorCode ierr;
903899cda47SBarry Smith   PetscInt       m=A->rmap.n,*aj,*ii;
904f2e71c43SSatish Balay   PetscInt       n,i,j,*ridx=PETSC_NULL;
905362ced78SSatish Balay   PetscScalar    sum;
90697952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
907f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
908f2e71c43SSatish Balay   PetscInt       jrow;
909e36a17ebSSatish Balay #endif
91017ab2063SBarry Smith 
911b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
91297952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
913fee21e36SBarry Smith #endif
914fee21e36SBarry Smith 
9153a40ed3dSBarry Smith   PetscFunctionBegin;
9161ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9171ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
91897952fefSHong Zhang   aj  = a->j;
91997952fefSHong Zhang   aa  = a->a;
920416022c9SBarry Smith   ii  = a->i;
9214eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
92297952fefSHong Zhang     m    = a->compressedrow.nrows;
92397952fefSHong Zhang     ii   = a->compressedrow.i;
92497952fefSHong Zhang     ridx = a->compressedrow.rindex;
92597952fefSHong Zhang     for (i=0; i<m; i++){
92697952fefSHong Zhang       n   = ii[i+1] - ii[i];
92797952fefSHong Zhang       aj  = a->j + ii[i];
92897952fefSHong Zhang       aa  = a->a + ii[i];
92997952fefSHong Zhang       sum = 0.0;
93097952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
93197952fefSHong Zhang       y[*ridx++] = sum;
93297952fefSHong Zhang     }
93397952fefSHong Zhang   } else { /* do not use compressed row format */
934b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
935b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
936b05257ddSBarry Smith #else
93717ab2063SBarry Smith     for (i=0; i<m; i++) {
9389ea0dfa2SSatish Balay       jrow = ii[i];
9399ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
94017ab2063SBarry Smith       sum  = 0.0;
9419ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
94297952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9439ea0dfa2SSatish Balay       }
94417ab2063SBarry Smith       y[i] = sum;
94517ab2063SBarry Smith     }
9468d195f9aSBarry Smith #endif
947b05257ddSBarry Smith   }
948efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz - m);CHKERRQ(ierr);
9491ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9501ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9513a40ed3dSBarry Smith   PetscFunctionReturn(0);
95217ab2063SBarry Smith }
95317ab2063SBarry Smith 
9544a2ae208SSatish Balay #undef __FUNCT__
9554a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
956dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
95717ab2063SBarry Smith {
958416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
95997952fefSHong Zhang   PetscScalar    *x,*y,*z,*aa;
960dfbe8321SBarry Smith   PetscErrorCode ierr;
961899cda47SBarry Smith   PetscInt       m = A->rmap.n,*aj,*ii;
962aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
96397952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
964362ced78SSatish Balay   PetscScalar    sum;
96597952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
966e36a17ebSSatish Balay #endif
9679ea0dfa2SSatish Balay 
9683a40ed3dSBarry Smith   PetscFunctionBegin;
9691ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9701ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9712e8a6d31SBarry Smith   if (zz != yy) {
9721ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9732e8a6d31SBarry Smith   } else {
9742e8a6d31SBarry Smith     z = y;
9752e8a6d31SBarry Smith   }
976bfeeae90SHong Zhang 
97797952fefSHong Zhang   aj  = a->j;
97897952fefSHong Zhang   aa  = a->a;
979cddf8d76SBarry Smith   ii  = a->i;
980aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
98197952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
98202ab625aSSatish Balay #else
9834eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
9844eb6d288SHong Zhang     if (zz != yy){
9854eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
9864eb6d288SHong Zhang     }
98797952fefSHong Zhang     m    = a->compressedrow.nrows;
98897952fefSHong Zhang     ii   = a->compressedrow.i;
98997952fefSHong Zhang     ridx = a->compressedrow.rindex;
99097952fefSHong Zhang     for (i=0; i<m; i++){
99197952fefSHong Zhang       n  = ii[i+1] - ii[i];
99297952fefSHong Zhang       aj  = a->j + ii[i];
99397952fefSHong Zhang       aa  = a->a + ii[i];
99497952fefSHong Zhang       sum = y[*ridx];
99597952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
99697952fefSHong Zhang       z[*ridx++] = sum;
99797952fefSHong Zhang     }
99897952fefSHong Zhang   } else { /* do not use compressed row format */
99917ab2063SBarry Smith     for (i=0; i<m; i++) {
10009ea0dfa2SSatish Balay       jrow = ii[i];
10019ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
100217ab2063SBarry Smith       sum  = y[i];
10039ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
100497952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10059ea0dfa2SSatish Balay       }
100617ab2063SBarry Smith       z[i] = sum;
100717ab2063SBarry Smith     }
100897952fefSHong Zhang   }
100902ab625aSSatish Balay #endif
1010efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
10111ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10121ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10132e8a6d31SBarry Smith   if (zz != yy) {
10141ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10152e8a6d31SBarry Smith   }
10163a40ed3dSBarry Smith   PetscFunctionReturn(0);
101717ab2063SBarry Smith }
101817ab2063SBarry Smith 
101917ab2063SBarry Smith /*
102017ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
102117ab2063SBarry Smith */
10224a2ae208SSatish Balay #undef __FUNCT__
10234a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1024dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
102517ab2063SBarry Smith {
1026416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10276849ba73SBarry Smith   PetscErrorCode ierr;
102809f38230SBarry Smith   PetscInt       i,j,m = A->rmap.n;
102917ab2063SBarry Smith 
10303a40ed3dSBarry Smith   PetscFunctionBegin;
103109f38230SBarry Smith   if (!a->diag) {
103209f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
103309f38230SBarry Smith   }
1034899cda47SBarry Smith   for (i=0; i<A->rmap.n; i++) {
103509f38230SBarry Smith     a->diag[i] = a->i[i+1];
1036bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1037bfeeae90SHong Zhang       if (a->j[j] == i) {
103809f38230SBarry Smith         a->diag[i] = j;
103917ab2063SBarry Smith         break;
104017ab2063SBarry Smith       }
104117ab2063SBarry Smith     }
104217ab2063SBarry Smith   }
10433a40ed3dSBarry Smith   PetscFunctionReturn(0);
104417ab2063SBarry Smith }
104517ab2063SBarry Smith 
1046be5855fcSBarry Smith /*
1047be5855fcSBarry Smith      Checks for missing diagonals
1048be5855fcSBarry Smith */
10494a2ae208SSatish Balay #undef __FUNCT__
10504a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
105109f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1052be5855fcSBarry Smith {
1053be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
105497f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1055be5855fcSBarry Smith 
1056be5855fcSBarry Smith   PetscFunctionBegin;
105709f38230SBarry Smith   *missing = PETSC_FALSE;
105809f38230SBarry Smith   if (A->rmap.n > 0 && !jj) {
105909f38230SBarry Smith     *missing  = PETSC_TRUE;
106009f38230SBarry Smith     if (d) *d = 0;
106109f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
106209f38230SBarry Smith   } else {
1063f1e2ffcdSBarry Smith     diag = a->diag;
1064899cda47SBarry Smith     for (i=0; i<A->rmap.n; i++) {
1065bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
106609f38230SBarry Smith 	*missing = PETSC_TRUE;
106709f38230SBarry Smith 	if (d) *d = i;
106809f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
106909f38230SBarry Smith       }
1070be5855fcSBarry Smith     }
1071be5855fcSBarry Smith   }
1072be5855fcSBarry Smith   PetscFunctionReturn(0);
1073be5855fcSBarry Smith }
1074be5855fcSBarry Smith 
10754a2ae208SSatish Balay #undef __FUNCT__
10764a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
107797f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
107817ab2063SBarry Smith {
1079416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1080beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1081beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1082dfbe8321SBarry Smith   PetscErrorCode     ierr;
1083899cda47SBarry Smith   PetscInt           n = A->cmap.n,m = A->rmap.n,i;
108497f1f81fSBarry Smith   const PetscInt     *idx,*diag;
108517ab2063SBarry Smith 
10863a40ed3dSBarry Smith   PetscFunctionBegin;
1087b965ef7fSBarry Smith   its = its*lits;
108877431f27SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
108991723122SBarry Smith 
1090ed480e8bSBarry Smith   diag = a->diag;
1091ed480e8bSBarry Smith   if (!a->idiag) {
1092ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1093ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1094ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1095ed480e8bSBarry Smith     v        = a->a;
1096ed480e8bSBarry Smith 
1097ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1098958c9bccSBarry Smith     if (omega == 1.0 && !fshift) {
1099ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1100ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
110129c1d7e0SHong Zhang         if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1102ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1103ed480e8bSBarry Smith       }
1104efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
1105ed480e8bSBarry Smith     } else {
1106ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1107ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1108beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1109ed480e8bSBarry Smith       }
1110efee365bSSatish Balay       ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
1111beeb8507SBarry Smith     }
1112ed480e8bSBarry Smith   }
1113ed480e8bSBarry Smith   t     = a->ssor;
1114ed480e8bSBarry Smith   idiag = a->idiag;
1115ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1116ed480e8bSBarry Smith 
11171ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1118fb2e594dSBarry Smith   if (xx != bb) {
11191ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1120fb2e594dSBarry Smith   } else {
1121fb2e594dSBarry Smith     b = x;
1122fb2e594dSBarry Smith   }
1123fb2e594dSBarry Smith 
1124ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1125ed480e8bSBarry Smith   xs   = x;
112617ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
112717ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1128ed480e8bSBarry Smith     bs = b;
112917ab2063SBarry Smith     for (i=0; i<m; i++) {
1130ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1131416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1132ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1133ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
113417ab2063SBarry Smith         sum  = b[i]*d/omega;
113517ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
113617ab2063SBarry Smith         x[i] = sum;
113717ab2063SBarry Smith     }
11381ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11391ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1140efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11413a40ed3dSBarry Smith     PetscFunctionReturn(0);
114217ab2063SBarry Smith   }
1143c783ea89SBarry Smith 
1144ed480e8bSBarry Smith 
1145fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1146fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1147fc3d8934SBarry Smith 
1148fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1149fc3d8934SBarry Smith 
1150fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1151fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
115248af12d7SBarry Smith     is the relaxation factor.
1153fc3d8934SBarry Smith     */
1154fc3d8934SBarry Smith 
115548af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
115629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11573a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
115817ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
115917ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
116017ab2063SBarry Smith 
116117ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
116217ab2063SBarry Smith 
116317ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
116417ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
116517ab2063SBarry Smith     is the relaxation factor.
116617ab2063SBarry Smith     */
116717ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
116817ab2063SBarry Smith 
116917ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
117017ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1171416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1172ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1173ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
117417ab2063SBarry Smith       sum  = b[i];
117517ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1176ed480e8bSBarry Smith       x[i] = sum*idiag[i];
117717ab2063SBarry Smith     }
117817ab2063SBarry Smith 
117917ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1180416022c9SBarry Smith     v = a->a;
1181ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
118217ab2063SBarry Smith 
118317ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1184ed480e8bSBarry Smith     ts = t;
1185416022c9SBarry Smith     diag = a->diag;
118617ab2063SBarry Smith     for (i=0; i<m; i++) {
1187416022c9SBarry Smith       n    = diag[i] - a->i[i];
1188ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1189ed480e8bSBarry Smith       v    = a->a + a->i[i];
119017ab2063SBarry Smith       sum  = t[i];
119117ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1192ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1193733d66baSBarry Smith       /*  x = x + t */
1194733d66baSBarry Smith       x[i] += t[i];
119517ab2063SBarry Smith     }
119617ab2063SBarry Smith 
1197efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
11981ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11991ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12003a40ed3dSBarry Smith     PetscFunctionReturn(0);
120117ab2063SBarry Smith   }
120217ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
120317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
120477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
120597f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
120677d8c4bbSBarry Smith #else
120717ab2063SBarry Smith       for (i=0; i<m; i++) {
1208416022c9SBarry Smith         n    = diag[i] - a->i[i];
1209ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1210ed480e8bSBarry Smith         v    = a->a + a->i[i];
121117ab2063SBarry Smith         sum  = b[i];
121217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1213ed480e8bSBarry Smith         x[i] = sum*idiag[i];
121417ab2063SBarry Smith       }
121577d8c4bbSBarry Smith #endif
121617ab2063SBarry Smith       xb = x;
1217efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12183a40ed3dSBarry Smith     } else xb = b;
121917ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
122017ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
122117ab2063SBarry Smith       for (i=0; i<m; i++) {
1222ed480e8bSBarry Smith         x[i] *= mdiag[i];
122317ab2063SBarry Smith       }
1224efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
122517ab2063SBarry Smith     }
122617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
122777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
122897f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
122977d8c4bbSBarry Smith #else
123017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1231416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1232ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1233ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
123417ab2063SBarry Smith         sum  = xb[i];
123517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1236ed480e8bSBarry Smith         x[i] = sum*idiag[i];
123717ab2063SBarry Smith       }
123877d8c4bbSBarry Smith #endif
1239efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
124017ab2063SBarry Smith     }
124117ab2063SBarry Smith     its--;
124217ab2063SBarry Smith   }
124317ab2063SBarry Smith   while (its--) {
124417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
124577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
124697f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
124777d8c4bbSBarry Smith #else
124817ab2063SBarry Smith       for (i=0; i<m; i++) {
1249416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1250ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1251ed480e8bSBarry Smith         v    = a->a + a->i[i];
125217ab2063SBarry Smith         sum  = b[i];
125317ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1254ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
125517ab2063SBarry Smith       }
125677d8c4bbSBarry Smith #endif
1257efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
125817ab2063SBarry Smith     }
125917ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
126077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
126197f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
126277d8c4bbSBarry Smith #else
126317ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1264416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1265ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1266ed480e8bSBarry Smith         v    = a->a + a->i[i];
126717ab2063SBarry Smith         sum  = b[i];
126817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1269ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
127017ab2063SBarry Smith       }
127177d8c4bbSBarry Smith #endif
1272efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
127317ab2063SBarry Smith     }
127417ab2063SBarry Smith   }
12751ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12761ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12773a40ed3dSBarry Smith   PetscFunctionReturn(0);
127817ab2063SBarry Smith }
127917ab2063SBarry Smith 
12804a2ae208SSatish Balay #undef __FUNCT__
12814a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1282dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
128317ab2063SBarry Smith {
1284416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12854e220ebcSLois Curfman McInnes 
12863a40ed3dSBarry Smith   PetscFunctionBegin;
1287899cda47SBarry Smith   info->rows_global    = (double)A->rmap.n;
1288899cda47SBarry Smith   info->columns_global = (double)A->cmap.n;
1289899cda47SBarry Smith   info->rows_local     = (double)A->rmap.n;
1290899cda47SBarry Smith   info->columns_local  = (double)A->cmap.n;
12914e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12924e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12934e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12944e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12954e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12964e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12974e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12984e220ebcSLois Curfman McInnes   if (A->factor) {
12994e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13004e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13014e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13024e220ebcSLois Curfman McInnes   } else {
13034e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13044e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13054e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13064e220ebcSLois Curfman McInnes   }
13073a40ed3dSBarry Smith   PetscFunctionReturn(0);
130817ab2063SBarry Smith }
130917ab2063SBarry Smith 
13104a2ae208SSatish Balay #undef __FUNCT__
13114a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1312f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
131317ab2063SBarry Smith {
1314416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
131509f38230SBarry Smith   PetscInt       i,m = A->rmap.n - 1,d;
13166849ba73SBarry Smith   PetscErrorCode ierr;
131709f38230SBarry Smith   PetscTruth     missing;
131817ab2063SBarry Smith 
13193a40ed3dSBarry Smith   PetscFunctionBegin;
1320f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1321f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
132277431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1323bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1324f1e2ffcdSBarry Smith     }
1325f4df32b1SMatthew Knepley     if (diag != 0.0) {
132609f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
132709f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1328f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1329f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1330f1e2ffcdSBarry Smith       }
1331f1e2ffcdSBarry Smith     }
133288e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1333f1e2ffcdSBarry Smith   } else {
1334f4df32b1SMatthew Knepley     if (diag != 0.0) {
133517ab2063SBarry Smith       for (i=0; i<N; i++) {
133677431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13377ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1338416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1339f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1340bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13417ae801bdSBarry Smith         } else { /* in case row was completely empty */
1342f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
134317ab2063SBarry Smith         }
134417ab2063SBarry Smith       }
13453a40ed3dSBarry Smith     } else {
134617ab2063SBarry Smith       for (i=0; i<N; i++) {
134777431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1348416022c9SBarry Smith         a->ilen[rows[i]] = 0;
134917ab2063SBarry Smith       }
135017ab2063SBarry Smith     }
135188e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1352f1e2ffcdSBarry Smith   }
135343a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13543a40ed3dSBarry Smith   PetscFunctionReturn(0);
135517ab2063SBarry Smith }
135617ab2063SBarry Smith 
13574a2ae208SSatish Balay #undef __FUNCT__
13584a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
135997f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
136017ab2063SBarry Smith {
1361416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
136297f1f81fSBarry Smith   PetscInt   *itmp;
136317ab2063SBarry Smith 
13643a40ed3dSBarry Smith   PetscFunctionBegin;
1365899cda47SBarry Smith   if (row < 0 || row >= A->rmap.n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
136617ab2063SBarry Smith 
1367416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1368bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
136917ab2063SBarry Smith   if (idx) {
1370bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1371bfeeae90SHong Zhang     if (*nz) {
13724e093b46SBarry Smith       *idx = itmp;
137317ab2063SBarry Smith     }
137417ab2063SBarry Smith     else *idx = 0;
137517ab2063SBarry Smith   }
13763a40ed3dSBarry Smith   PetscFunctionReturn(0);
137717ab2063SBarry Smith }
137817ab2063SBarry Smith 
1379bfeeae90SHong Zhang /* remove this function? */
13804a2ae208SSatish Balay #undef __FUNCT__
13814a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
138297f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
138317ab2063SBarry Smith {
13843a40ed3dSBarry Smith   PetscFunctionBegin;
13853a40ed3dSBarry Smith   PetscFunctionReturn(0);
138617ab2063SBarry Smith }
138717ab2063SBarry Smith 
13884a2ae208SSatish Balay #undef __FUNCT__
13894a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1390dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
139117ab2063SBarry Smith {
1392416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
139387828ca2SBarry Smith   PetscScalar    *v = a->a;
139436db0b34SBarry Smith   PetscReal      sum = 0.0;
13956849ba73SBarry Smith   PetscErrorCode ierr;
139697f1f81fSBarry Smith   PetscInt       i,j;
139717ab2063SBarry Smith 
13983a40ed3dSBarry Smith   PetscFunctionBegin;
139917ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1400416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1401aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
140236db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
140317ab2063SBarry Smith #else
140417ab2063SBarry Smith       sum += (*v)*(*v); v++;
140517ab2063SBarry Smith #endif
140617ab2063SBarry Smith     }
1407064f8208SBarry Smith     *nrm = sqrt(sum);
14083a40ed3dSBarry Smith   } else if (type == NORM_1) {
140936db0b34SBarry Smith     PetscReal *tmp;
141097f1f81fSBarry Smith     PetscInt    *jj = a->j;
1411899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1412899cda47SBarry Smith     ierr = PetscMemzero(tmp,A->cmap.n*sizeof(PetscReal));CHKERRQ(ierr);
1413064f8208SBarry Smith     *nrm = 0.0;
1414416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1415bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
141617ab2063SBarry Smith     }
1417899cda47SBarry Smith     for (j=0; j<A->cmap.n; j++) {
1418064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
141917ab2063SBarry Smith     }
1420606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14213a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1422064f8208SBarry Smith     *nrm = 0.0;
1423899cda47SBarry Smith     for (j=0; j<A->rmap.n; j++) {
1424bfeeae90SHong Zhang       v = a->a + a->i[j];
142517ab2063SBarry Smith       sum = 0.0;
1426416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1427cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
142817ab2063SBarry Smith       }
1429064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
143017ab2063SBarry Smith     }
14313a40ed3dSBarry Smith   } else {
143229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
143317ab2063SBarry Smith   }
14343a40ed3dSBarry Smith   PetscFunctionReturn(0);
143517ab2063SBarry Smith }
143617ab2063SBarry Smith 
14374a2ae208SSatish Balay #undef __FUNCT__
14384a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1439dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B)
144017ab2063SBarry Smith {
1441416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1442416022c9SBarry Smith   Mat            C;
14436849ba73SBarry Smith   PetscErrorCode ierr;
1444899cda47SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap.n,len,*col;
144587828ca2SBarry Smith   PetscScalar    *array = a->a;
144617ab2063SBarry Smith 
14473a40ed3dSBarry Smith   PetscFunctionBegin;
1448899cda47SBarry Smith   if (!B && m != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1449899cda47SBarry Smith   ierr = PetscMalloc((1+A->cmap.n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1450899cda47SBarry Smith   ierr = PetscMemzero(col,(1+A->cmap.n)*sizeof(PetscInt));CHKERRQ(ierr);
1451bfeeae90SHong Zhang 
1452bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1453f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1454899cda47SBarry Smith   ierr = MatSetSizes(C,A->cmap.n,m,A->cmap.n,m);CHKERRQ(ierr);
1455f204ca49SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1456ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1457606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
145817ab2063SBarry Smith   for (i=0; i<m; i++) {
145917ab2063SBarry Smith     len    = ai[i+1]-ai[i];
146087d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1461b9b97703SBarry Smith     array += len;
1462b9b97703SBarry Smith     aj    += len;
146317ab2063SBarry Smith   }
146417ab2063SBarry Smith 
14656d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14666d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
146717ab2063SBarry Smith 
1468f1e2ffcdSBarry Smith   if (B) {
1469416022c9SBarry Smith     *B = C;
147017ab2063SBarry Smith   } else {
1471273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
147217ab2063SBarry Smith   }
14733a40ed3dSBarry Smith   PetscFunctionReturn(0);
147417ab2063SBarry Smith }
147517ab2063SBarry Smith 
1476cd0d46ebSvictorle EXTERN_C_BEGIN
1477cd0d46ebSvictorle #undef __FUNCT__
14785fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1479be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1480cd0d46ebSvictorle {
1481cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
148297f1f81fSBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
14836849ba73SBarry Smith   PetscErrorCode ierr;
148497f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1485cd0d46ebSvictorle 
1486cd0d46ebSvictorle   PetscFunctionBegin;
1487cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1488cd0d46ebSvictorle 
1489cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1490cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
14915485867bSBarry Smith   if (ma!=nb || na!=mb){
14925485867bSBarry Smith     *f = PETSC_FALSE;
14935485867bSBarry Smith     PetscFunctionReturn(0);
14945485867bSBarry Smith   }
1495cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1496cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1497cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
149897f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
149997f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1500cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1501cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1502cd0d46ebSvictorle 
1503cd0d46ebSvictorle   *f = PETSC_TRUE;
1504cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1505cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
150697f1f81fSBarry Smith       PetscInt         idc,idr;
15075485867bSBarry Smith       PetscScalar vc,vr;
1508cd0d46ebSvictorle       /* column/row index/value */
15095485867bSBarry Smith       idc = adx[aptr[i]];
15105485867bSBarry Smith       idr = bdx[bptr[idc]];
15115485867bSBarry Smith       vc  = va[aptr[i]];
15125485867bSBarry Smith       vr  = vb[bptr[idc]];
15135485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15145485867bSBarry Smith 	*f = PETSC_FALSE;
15155485867bSBarry Smith         goto done;
1516cd0d46ebSvictorle       } else {
15175485867bSBarry Smith 	aptr[i]++;
15185485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1519cd0d46ebSvictorle       }
1520cd0d46ebSvictorle     }
1521cd0d46ebSvictorle   }
1522cd0d46ebSvictorle  done:
1523cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15243aeef889SHong Zhang   if (B) {
15253aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15263aeef889SHong Zhang   }
1527cd0d46ebSvictorle   PetscFunctionReturn(0);
1528cd0d46ebSvictorle }
1529cd0d46ebSvictorle EXTERN_C_END
1530cd0d46ebSvictorle 
15319e29f15eSvictorle #undef __FUNCT__
15329e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1533dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
15349e29f15eSvictorle {
1535dfbe8321SBarry Smith   PetscErrorCode ierr;
15369e29f15eSvictorle   PetscFunctionBegin;
15375485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
15389e29f15eSvictorle   PetscFunctionReturn(0);
15399e29f15eSvictorle }
15409e29f15eSvictorle 
15414a2ae208SSatish Balay #undef __FUNCT__
15424a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1543dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
154417ab2063SBarry Smith {
1545416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
154687828ca2SBarry Smith   PetscScalar    *l,*r,x,*v;
1547dfbe8321SBarry Smith   PetscErrorCode ierr;
1548899cda47SBarry Smith   PetscInt       i,j,m = A->rmap.n,n = A->cmap.n,M,nz = a->nz,*jj;
154917ab2063SBarry Smith 
15503a40ed3dSBarry Smith   PetscFunctionBegin;
155117ab2063SBarry Smith   if (ll) {
15523ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
15533ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1554e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1555899cda47SBarry Smith     if (m != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
15561ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1557416022c9SBarry Smith     v = a->a;
155817ab2063SBarry Smith     for (i=0; i<m; i++) {
155917ab2063SBarry Smith       x = l[i];
1560416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
156117ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
156217ab2063SBarry Smith     }
15631ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1564efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
156517ab2063SBarry Smith   }
156617ab2063SBarry Smith   if (rr) {
1567e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1568899cda47SBarry Smith     if (n != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
15691ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1570416022c9SBarry Smith     v = a->a; jj = a->j;
157117ab2063SBarry Smith     for (i=0; i<nz; i++) {
1572bfeeae90SHong Zhang       (*v++) *= r[*jj++];
157317ab2063SBarry Smith     }
15741ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1575efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
157617ab2063SBarry Smith   }
15773a40ed3dSBarry Smith   PetscFunctionReturn(0);
157817ab2063SBarry Smith }
157917ab2063SBarry Smith 
15804a2ae208SSatish Balay #undef __FUNCT__
15814a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
158297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
158317ab2063SBarry Smith {
1584db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
15856849ba73SBarry Smith   PetscErrorCode ierr;
1586899cda47SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap.n,*lens;
158797f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
158897f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
158997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
159087828ca2SBarry Smith   PetscScalar    *a_new,*mat_a;
1591416022c9SBarry Smith   Mat            C;
1592fee21e36SBarry Smith   PetscTruth     stride;
159317ab2063SBarry Smith 
15943a40ed3dSBarry Smith   PetscFunctionBegin;
1595d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
159629bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1597d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
159829bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
159999141d43SSatish Balay 
160017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1601b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1602b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
160317ab2063SBarry Smith 
1604fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1605fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1606fee21e36SBarry Smith   if (stride && step == 1) {
160702834360SBarry Smith     /* special case of contiguous rows */
160897f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
160931ebf83bSSatish Balay     starts = lens + nrows;
161002834360SBarry Smith     /* loop over new rows determining lens and starting points */
161102834360SBarry Smith     for (i=0; i<nrows; i++) {
1612bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1613a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
161402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1615bfeeae90SHong Zhang         if (aj[k] >= first) {
161602834360SBarry Smith           starts[i] = k;
161702834360SBarry Smith           break;
161802834360SBarry Smith 	}
161902834360SBarry Smith       }
1620a2744918SBarry Smith       sum = 0;
162102834360SBarry Smith       while (k < kend) {
1622bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1623a2744918SBarry Smith         sum++;
162402834360SBarry Smith       }
1625a2744918SBarry Smith       lens[i] = sum;
162602834360SBarry Smith     }
162702834360SBarry Smith     /* create submatrix */
1628cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
162997f1f81fSBarry Smith       PetscInt n_cols,n_rows;
163008480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
163129bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1632d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
163308480c60SBarry Smith       C = *B;
16343a40ed3dSBarry Smith     } else {
1635f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1636f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1637e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1638ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
163908480c60SBarry Smith     }
1640db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1641db02288aSLois Curfman McInnes 
164202834360SBarry Smith     /* loop over rows inserting into submatrix */
1643db02288aSLois Curfman McInnes     a_new    = c->a;
1644db02288aSLois Curfman McInnes     j_new    = c->j;
1645db02288aSLois Curfman McInnes     i_new    = c->i;
1646bfeeae90SHong Zhang 
164702834360SBarry Smith     for (i=0; i<nrows; i++) {
1648a2744918SBarry Smith       ii    = starts[i];
1649a2744918SBarry Smith       lensi = lens[i];
1650a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1651a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
165202834360SBarry Smith       }
165387828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1654a2744918SBarry Smith       a_new      += lensi;
1655a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1656a2744918SBarry Smith       c->ilen[i]  = lensi;
165702834360SBarry Smith     }
1658606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
16593a40ed3dSBarry Smith   } else {
166002834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
166197f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1662bfeeae90SHong Zhang 
166397f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
166497f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
166517ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
166602834360SBarry Smith     /* determine lens of each row */
166702834360SBarry Smith     for (i=0; i<nrows; i++) {
1668bfeeae90SHong Zhang       kstart  = ai[irow[i]];
166902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
167002834360SBarry Smith       lens[i] = 0;
167102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1672bfeeae90SHong Zhang         if (smap[aj[k]]) {
167302834360SBarry Smith           lens[i]++;
167402834360SBarry Smith         }
167502834360SBarry Smith       }
167602834360SBarry Smith     }
167717ab2063SBarry Smith     /* Create and fill new matrix */
1678a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16790f5bd95cSBarry Smith       PetscTruth equal;
16800f5bd95cSBarry Smith 
168199141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1682899cda47SBarry Smith       if ((*B)->rmap.n  != nrows || (*B)->cmap.n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1683899cda47SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap.n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
16840f5bd95cSBarry Smith       if (!equal) {
168529bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
168699141d43SSatish Balay       }
1687899cda47SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap.n*sizeof(PetscInt));CHKERRQ(ierr);
168808480c60SBarry Smith       C = *B;
16893a40ed3dSBarry Smith     } else {
1690f69a0ea3SMatthew Knepley       ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
1691f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1692e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1693ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
169408480c60SBarry Smith     }
169599141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
169617ab2063SBarry Smith     for (i=0; i<nrows; i++) {
169799141d43SSatish Balay       row    = irow[i];
1698bfeeae90SHong Zhang       kstart = ai[row];
169999141d43SSatish Balay       kend   = kstart + a->ilen[row];
1700bfeeae90SHong Zhang       mat_i  = c->i[i];
170199141d43SSatish Balay       mat_j  = c->j + mat_i;
170299141d43SSatish Balay       mat_a  = c->a + mat_i;
170399141d43SSatish Balay       mat_ilen = c->ilen + i;
170417ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1705bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1706ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
170799141d43SSatish Balay           *mat_a++ = a->a[k];
170899141d43SSatish Balay           (*mat_ilen)++;
170999141d43SSatish Balay 
171017ab2063SBarry Smith         }
171117ab2063SBarry Smith       }
171217ab2063SBarry Smith     }
171302834360SBarry Smith     /* Free work space */
171402834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1715606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1716606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
171702834360SBarry Smith   }
17186d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17196d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
172017ab2063SBarry Smith 
172117ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1722416022c9SBarry Smith   *B = C;
17233a40ed3dSBarry Smith   PetscFunctionReturn(0);
172417ab2063SBarry Smith }
172517ab2063SBarry Smith 
1726a871dcd8SBarry Smith /*
1727a871dcd8SBarry Smith */
17284a2ae208SSatish Balay #undef __FUNCT__
17294a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1730dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1731a871dcd8SBarry Smith {
173263b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1733dfbe8321SBarry Smith   PetscErrorCode ierr;
173463b91edcSBarry Smith   Mat            outA;
1735b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
173663b91edcSBarry Smith 
17373a40ed3dSBarry Smith   PetscFunctionBegin;
1738d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1739b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1740b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1741a871dcd8SBarry Smith 
174263b91edcSBarry Smith   outA          = inA;
174363b91edcSBarry Smith   inA->factor   = FACTOR_LU;
1744c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1745c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row); CHKERRQ(ierr);}
1746c3122656SLisandro Dalcin   a->row = row;
1747c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1748c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col); CHKERRQ(ierr);}
1749c3122656SLisandro Dalcin   a->col = col;
175063b91edcSBarry Smith 
175136db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1752b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
17534c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
175452e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1755f0ec6fceSSatish Balay 
175694a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1757899cda47SBarry Smith      ierr = PetscMalloc((inA->rmap.n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
175894a9d846SBarry Smith   }
175963b91edcSBarry Smith 
1760f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1761137fb511SHong Zhang   if (row_identity && col_identity) {
1762af281ebdSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
1763137fb511SHong Zhang   } else {
1764137fb511SHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(inA,info,&outA);CHKERRQ(ierr);
1765137fb511SHong Zhang   }
17663a40ed3dSBarry Smith   PetscFunctionReturn(0);
1767a871dcd8SBarry Smith }
1768a871dcd8SBarry Smith 
1769d9eff348SSatish Balay #include "petscblaslapack.h"
17704a2ae208SSatish Balay #undef __FUNCT__
17714a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1772f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1773f0b747eeSBarry Smith {
1774f0b747eeSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)inA->data;
17754ce68768SBarry Smith   PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1;
1776f4df32b1SMatthew Knepley   PetscScalar oalpha = alpha;
1777efee365bSSatish Balay   PetscErrorCode ierr;
1778efee365bSSatish Balay 
17793a40ed3dSBarry Smith 
17803a40ed3dSBarry Smith   PetscFunctionBegin;
1781f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1782efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17833a40ed3dSBarry Smith   PetscFunctionReturn(0);
1784f0b747eeSBarry Smith }
1785f0b747eeSBarry Smith 
17864a2ae208SSatish Balay #undef __FUNCT__
17874a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
178897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1789cddf8d76SBarry Smith {
1790dfbe8321SBarry Smith   PetscErrorCode ierr;
179197f1f81fSBarry Smith   PetscInt       i;
1792cddf8d76SBarry Smith 
17933a40ed3dSBarry Smith   PetscFunctionBegin;
1794cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1795b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1796cddf8d76SBarry Smith   }
1797cddf8d76SBarry Smith 
1798cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17996a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1800cddf8d76SBarry Smith   }
18013a40ed3dSBarry Smith   PetscFunctionReturn(0);
1802cddf8d76SBarry Smith }
1803cddf8d76SBarry Smith 
18044a2ae208SSatish Balay #undef __FUNCT__
18054a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
180697f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
18074dcbc457SBarry Smith {
1808e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18096849ba73SBarry Smith   PetscErrorCode ierr;
181097f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
181197f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1812f1af5d2fSBarry Smith   PetscBT        table;
1813bbd702dbSSatish Balay 
18143a40ed3dSBarry Smith   PetscFunctionBegin;
1815899cda47SBarry Smith   m     = A->rmap.n;
1816e4d965acSSatish Balay   ai    = a->i;
1817bfeeae90SHong Zhang   aj    = a->j;
18188a047759SSatish Balay 
1819a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
182006763907SSatish Balay 
182197f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
18226831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
182306763907SSatish Balay 
1824e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1825b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1826e4d965acSSatish Balay     isz  = 0;
18276831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1828e4d965acSSatish Balay 
1829e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
18304dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1831b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1832e4d965acSSatish Balay 
1833dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1834e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1835f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
18364dcbc457SBarry Smith     }
183706763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
183806763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1839e4d965acSSatish Balay 
184004a348a9SBarry Smith     k = 0;
184104a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
184204a348a9SBarry Smith       n = isz;
184306763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1844e4d965acSSatish Balay         row   = nidx[k];
1845e4d965acSSatish Balay         start = ai[row];
1846e4d965acSSatish Balay         end   = ai[row+1];
184704a348a9SBarry Smith         for (l = start; l<end ; l++){
1848efb16452SHong Zhang           val = aj[l] ;
1849f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1850e4d965acSSatish Balay         }
1851e4d965acSSatish Balay       }
1852e4d965acSSatish Balay     }
1853029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1854e4d965acSSatish Balay   }
18556831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1856606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
18573a40ed3dSBarry Smith   PetscFunctionReturn(0);
18584dcbc457SBarry Smith }
185917ab2063SBarry Smith 
18600513a670SBarry Smith /* -------------------------------------------------------------- */
18614a2ae208SSatish Balay #undef __FUNCT__
18624a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1863dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
18640513a670SBarry Smith {
18650513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18666849ba73SBarry Smith   PetscErrorCode ierr;
1867899cda47SBarry Smith   PetscInt       i,nz,m = A->rmap.n,n = A->cmap.n,*col;
186897f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
186956cd22aeSBarry Smith   IS             icolp,irowp;
187097f1f81fSBarry Smith   PetscInt       *cwork;
187132ec9ce4SBarry Smith   PetscScalar    *vwork;
18720513a670SBarry Smith 
18733a40ed3dSBarry Smith   PetscFunctionBegin;
18744c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
187556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18764c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
187756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18780513a670SBarry Smith 
18790513a670SBarry Smith   /* determine lengths of permuted rows */
188097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
18810513a670SBarry Smith   for (i=0; i<m; i++) {
18820513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18830513a670SBarry Smith   }
1884f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,B);CHKERRQ(ierr);
1885f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
1886f204ca49SKris Buschelman   ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr);
1887ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1888606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18890513a670SBarry Smith 
189097f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
18910513a670SBarry Smith   for (i=0; i<m; i++) {
189232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18930513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1894cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
189532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18960513a670SBarry Smith   }
1897606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18983c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
18990513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19000513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
190156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
190256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
190356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
190456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
19053a40ed3dSBarry Smith   PetscFunctionReturn(0);
19060513a670SBarry Smith }
19070513a670SBarry Smith 
19084a2ae208SSatish Balay #undef __FUNCT__
19094a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1910dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1911cb5b572fSBarry Smith {
1912dfbe8321SBarry Smith   PetscErrorCode ierr;
1913cb5b572fSBarry Smith 
1914cb5b572fSBarry Smith   PetscFunctionBegin;
191533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
191633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1917be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1918be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1919be6bf707SBarry Smith 
1920899cda47SBarry Smith     if (a->i[A->rmap.n] != b->i[B->rmap.n]) {
1921634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1922cb5b572fSBarry Smith     }
1923899cda47SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr);
1924cb5b572fSBarry Smith   } else {
1925cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1926cb5b572fSBarry Smith   }
1927cb5b572fSBarry Smith   PetscFunctionReturn(0);
1928cb5b572fSBarry Smith }
1929cb5b572fSBarry Smith 
19304a2ae208SSatish Balay #undef __FUNCT__
19314a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1932dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
1933273d9f13SBarry Smith {
1934dfbe8321SBarry Smith   PetscErrorCode ierr;
1935273d9f13SBarry Smith 
1936273d9f13SBarry Smith   PetscFunctionBegin;
1937ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1938273d9f13SBarry Smith   PetscFunctionReturn(0);
1939273d9f13SBarry Smith }
1940273d9f13SBarry Smith 
19414a2ae208SSatish Balay #undef __FUNCT__
19424a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
1943dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
19446c0721eeSBarry Smith {
19456c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19466c0721eeSBarry Smith   PetscFunctionBegin;
19476c0721eeSBarry Smith   *array = a->a;
19486c0721eeSBarry Smith   PetscFunctionReturn(0);
19496c0721eeSBarry Smith }
19506c0721eeSBarry Smith 
19514a2ae208SSatish Balay #undef __FUNCT__
19524a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
1953dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19546c0721eeSBarry Smith {
19556c0721eeSBarry Smith   PetscFunctionBegin;
19566c0721eeSBarry Smith   PetscFunctionReturn(0);
19576c0721eeSBarry Smith }
1958273d9f13SBarry Smith 
1959ee4f033dSBarry Smith #undef __FUNCT__
1960ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1961dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1962ee4f033dSBarry Smith {
19636849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
19646849ba73SBarry Smith   PetscErrorCode ierr;
196597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
1966efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
196787828ca2SBarry Smith   PetscScalar    *vscale_array;
1968ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
1969ee4f033dSBarry Smith   Vec            w1,w2,w3;
1970ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
1971ee4f033dSBarry Smith   PetscTruth     flg;
1972ee4f033dSBarry Smith 
1973ee4f033dSBarry Smith   PetscFunctionBegin;
1974ee4f033dSBarry Smith   if (!coloring->w1) {
1975ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
197652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
1977ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
197852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
1979ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
198052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
1981ee4f033dSBarry Smith   }
1982ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1983ee4f033dSBarry Smith 
1984ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1985e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1986ee4f033dSBarry Smith   if (flg) {
1987ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
1988ee4f033dSBarry Smith   } else {
19890b9b6f31SBarry Smith     PetscTruth assembled;
19900b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
19910b9b6f31SBarry Smith     if (assembled) {
1992ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
1993ee4f033dSBarry Smith     }
19940b9b6f31SBarry Smith   }
1995ee4f033dSBarry Smith 
1996ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1997ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1998ee4f033dSBarry Smith 
1999ee4f033dSBarry Smith   /*
2000ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2001ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2002ee4f033dSBarry Smith   */
2003ee4f033dSBarry Smith   if (coloring->F) {
2004ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2005ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2006ee4f033dSBarry Smith     if (m1 != m2) {
2007ee4f033dSBarry Smith       coloring->F = 0;
2008ee4f033dSBarry Smith     }
2009ee4f033dSBarry Smith   }
2010ee4f033dSBarry Smith 
2011ee4f033dSBarry Smith   if (coloring->F) {
2012ee4f033dSBarry Smith     w1          = coloring->F;
2013ee4f033dSBarry Smith     coloring->F = 0;
2014ee4f033dSBarry Smith   } else {
201566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2016ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
201766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2018ee4f033dSBarry Smith   }
2019ee4f033dSBarry Smith 
2020ee4f033dSBarry Smith   /*
2021ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2022ee4f033dSBarry Smith   */
20231ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
20241ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2025ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2026ee4f033dSBarry Smith     /*
2027ee4f033dSBarry Smith        Loop over each column associated with color adding the
2028ee4f033dSBarry Smith        perturbation to the vector w3.
2029ee4f033dSBarry Smith     */
2030ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2031ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2032ee4f033dSBarry Smith       dx  = xx[col];
2033ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2034ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2035ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2036ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2037ee4f033dSBarry Smith #else
2038ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2039ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2040ee4f033dSBarry Smith #endif
2041ee4f033dSBarry Smith       dx                *= epsilon;
2042ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2043ee4f033dSBarry Smith     }
2044ee4f033dSBarry Smith   }
20451ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2046ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2047ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2048ee4f033dSBarry Smith 
2049ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2050ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2051ee4f033dSBarry Smith 
2052ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2053ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2054ee4f033dSBarry Smith 
20551ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2056ee4f033dSBarry Smith   /*
2057ee4f033dSBarry Smith       Loop over each color
2058ee4f033dSBarry Smith   */
2059ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
206049b058dcSBarry Smith     coloring->currentcolor = k;
2061ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
20621ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2063ee4f033dSBarry Smith     /*
2064ee4f033dSBarry Smith        Loop over each column associated with color adding the
2065ee4f033dSBarry Smith        perturbation to the vector w3.
2066ee4f033dSBarry Smith     */
2067ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2068ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2069ee4f033dSBarry Smith       dx  = xx[col];
20705b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2071ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2072ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2073ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2074ee4f033dSBarry Smith #else
2075ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2076ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2077ee4f033dSBarry Smith #endif
2078ee4f033dSBarry Smith       dx            *= epsilon;
2079634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2080ee4f033dSBarry Smith       w3_array[col] += dx;
2081ee4f033dSBarry Smith     }
20821ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2083ee4f033dSBarry Smith 
2084ee4f033dSBarry Smith     /*
2085ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2086ee4f033dSBarry Smith     */
2087ee4f033dSBarry Smith 
208866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2089ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
209066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2091efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2092ee4f033dSBarry Smith 
2093ee4f033dSBarry Smith     /*
2094ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2095ee4f033dSBarry Smith     */
20961ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2097ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2098ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2099ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2100ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2101ee4f033dSBarry Smith       srow   = row + start;
2102ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2103ee4f033dSBarry Smith     }
21041ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2105ee4f033dSBarry Smith   }
210649b058dcSBarry Smith   coloring->currentcolor = k;
21071ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
21081ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2109ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2110ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2111ee4f033dSBarry Smith   PetscFunctionReturn(0);
2112ee4f033dSBarry Smith }
2113ee4f033dSBarry Smith 
2114ac90fabeSBarry Smith #include "petscblaslapack.h"
2115ac90fabeSBarry Smith #undef __FUNCT__
2116ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2117f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2118ac90fabeSBarry Smith {
2119dfbe8321SBarry Smith   PetscErrorCode ierr;
212097f1f81fSBarry Smith   PetscInt       i;
2121ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
21224ce68768SBarry Smith   PetscBLASInt   one=1,bnz = (PetscBLASInt)x->nz;
2123ac90fabeSBarry Smith 
2124ac90fabeSBarry Smith   PetscFunctionBegin;
2125ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2126f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2127f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2128c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2129a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2130a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2131a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2132a30b2313SHong Zhang     }
2133a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2134899cda47SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap.n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2135a30b2313SHong Zhang       y->XtoY = X;
2136*407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2137c537a176SHong Zhang     }
2138f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2139ae15b995SBarry Smith     ierr = PetscInfo3(0,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);CHKERRQ(ierr);
2140ac90fabeSBarry Smith   } else {
2141f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2142ac90fabeSBarry Smith   }
2143ac90fabeSBarry Smith   PetscFunctionReturn(0);
2144ac90fabeSBarry Smith }
2145ac90fabeSBarry Smith 
2146521d7252SBarry Smith #undef __FUNCT__
2147521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2148521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2149521d7252SBarry Smith {
2150521d7252SBarry Smith   PetscFunctionBegin;
2151521d7252SBarry Smith   PetscFunctionReturn(0);
2152521d7252SBarry Smith }
2153521d7252SBarry Smith 
2154354c94deSBarry Smith #undef __FUNCT__
2155354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2156354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2157354c94deSBarry Smith {
2158354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2159354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2160354c94deSBarry Smith   PetscInt    i,nz;
2161354c94deSBarry Smith   PetscScalar *a;
2162354c94deSBarry Smith 
2163354c94deSBarry Smith   PetscFunctionBegin;
2164354c94deSBarry Smith   nz = aij->nz;
2165354c94deSBarry Smith   a  = aij->a;
2166354c94deSBarry Smith   for (i=0; i<nz; i++) {
2167354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2168354c94deSBarry Smith   }
2169354c94deSBarry Smith #else
2170354c94deSBarry Smith   PetscFunctionBegin;
2171354c94deSBarry Smith #endif
2172354c94deSBarry Smith   PetscFunctionReturn(0);
2173354c94deSBarry Smith }
2174354c94deSBarry Smith 
2175e34fafa9SBarry Smith #undef __FUNCT__
2176e34fafa9SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2177e34fafa9SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v)
2178e34fafa9SBarry Smith {
2179e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2180e34fafa9SBarry Smith   PetscErrorCode ierr;
2181899cda47SBarry Smith   PetscInt       i,j,m = A->rmap.n,*ai,*aj,ncols,n;
2182e34fafa9SBarry Smith   PetscReal      atmp;
2183e34fafa9SBarry Smith   PetscScalar    *x,zero = 0.0;
2184e34fafa9SBarry Smith   MatScalar      *aa;
2185e34fafa9SBarry Smith 
2186e34fafa9SBarry Smith   PetscFunctionBegin;
2187e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2188e34fafa9SBarry Smith   aa   = a->a;
2189e34fafa9SBarry Smith   ai   = a->i;
2190e34fafa9SBarry Smith   aj   = a->j;
2191e34fafa9SBarry Smith 
219243e18e04SHong Zhang   ierr = VecSet(v,zero);CHKERRQ(ierr);
2193e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2194e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2195899cda47SBarry Smith   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2196e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2197e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2198e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2199e34fafa9SBarry Smith       atmp = PetscAbsScalar(*aa); aa++;
2200e34fafa9SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) x[i] = atmp;
2201e34fafa9SBarry Smith       aj++;
2202e34fafa9SBarry Smith     }
2203e34fafa9SBarry Smith   }
2204e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2205e34fafa9SBarry Smith   PetscFunctionReturn(0);
2206e34fafa9SBarry Smith }
2207e34fafa9SBarry Smith 
2208682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
22090a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2210cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2211cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2212cb5b572fSBarry Smith        MatMult_SeqAIJ,
221397304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
22147c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
22157c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2216cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2217cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
22187c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
221997304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2220cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2221cb5b572fSBarry Smith        0,
222217ab2063SBarry Smith        MatRelax_SeqAIJ,
222317ab2063SBarry Smith        MatTranspose_SeqAIJ,
222497304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2225cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2226cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2227cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2228cb5b572fSBarry Smith        MatNorm_SeqAIJ,
222997304618SKris Buschelman /*20*/ 0,
2230cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
223117ab2063SBarry Smith        MatCompress_SeqAIJ,
2232cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2233cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
223497304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2235cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2236cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2237f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2238a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
223997304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2240cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2241861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
22426c0721eeSBarry Smith        MatGetArray_SeqAIJ,
22436c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
224497304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2245cb5b572fSBarry Smith        0,
2246cb5b572fSBarry Smith        0,
2247cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2248cb5b572fSBarry Smith        0,
224997304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2250cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2251cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2252cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2253cb5b572fSBarry Smith        MatCopy_SeqAIJ,
22548c07d4e3SBarry Smith /*45*/ 0,
2255cb5b572fSBarry Smith        MatScale_SeqAIJ,
2256cb5b572fSBarry Smith        0,
225779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
22586945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2259521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
22603b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
22613b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
22623b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2263a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
226497304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2265b9617806SBarry Smith        0,
22660513a670SBarry Smith        0,
2267cda55fadSBarry Smith        MatPermute_SeqAIJ,
2268cda55fadSBarry Smith        0,
226997304618SKris Buschelman /*60*/ 0,
2270b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2271b9b97703SBarry Smith        MatView_SeqAIJ,
2272357abbc8SBarry Smith        0,
2273ee4f033dSBarry Smith        0,
227497304618SKris Buschelman /*65*/ 0,
2275ee4f033dSBarry Smith        0,
2276ee4f033dSBarry Smith        0,
2277ee4f033dSBarry Smith        0,
2278ee4f033dSBarry Smith        0,
2279e34fafa9SBarry Smith /*70*/ MatGetRowMax_SeqAIJ,
2280ee4f033dSBarry Smith        0,
2281ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2282dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2283ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2284dcf5cc72SBarry Smith #else
2285dcf5cc72SBarry Smith        0,
2286dcf5cc72SBarry Smith #endif
2287ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2288b8f8c88eSHong Zhang /*75*/ 0,
228997304618SKris Buschelman        0,
229097304618SKris Buschelman        0,
229197304618SKris Buschelman        0,
229297304618SKris Buschelman        0,
229397304618SKris Buschelman /*80*/ 0,
229497304618SKris Buschelman        0,
229597304618SKris Buschelman        0,
229697304618SKris Buschelman        0,
2297bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2298bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
22996284ec50SHong Zhang        0,
23006284ec50SHong Zhang        0,
23016284ec50SHong Zhang        0,
2302bc011b1eSHong Zhang        0,
2303bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
230426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
230526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2306d439da42SKris Buschelman        MatPtAP_Basic,
23077ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
23087ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2309bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2310bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2311bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
23127ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
23137ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2314609c6c4dSKris Buschelman        0,
2315609c6c4dSKris Buschelman        0,
231687d4246cSBarry Smith        MatConjugate_SeqAIJ,
231787d4246cSBarry Smith        0,
231899cafbc1SBarry Smith /*105*/MatSetValuesRow_SeqAIJ,
231999cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2320f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2321f5edf698SHong Zhang        0,
23222bebee5dSHong Zhang        0,
23232bebee5dSHong Zhang /*110*/MatMatSolve_SeqAIJ
23249e29f15eSvictorle };
232517ab2063SBarry Smith 
2326fb2e594dSBarry Smith EXTERN_C_BEGIN
23274a2ae208SSatish Balay #undef __FUNCT__
23284a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2329be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2330bef8e0ddSBarry Smith {
2331bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
233297f1f81fSBarry Smith   PetscInt   i,nz,n;
2333bef8e0ddSBarry Smith 
2334bef8e0ddSBarry Smith   PetscFunctionBegin;
2335bef8e0ddSBarry Smith 
2336bef8e0ddSBarry Smith   nz = aij->maxnz;
2337899cda47SBarry Smith   n  = mat->cmap.n;
2338bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2339bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2340bef8e0ddSBarry Smith   }
2341bef8e0ddSBarry Smith   aij->nz = nz;
2342bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2343bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2344bef8e0ddSBarry Smith   }
2345bef8e0ddSBarry Smith 
2346bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2347bef8e0ddSBarry Smith }
2348fb2e594dSBarry Smith EXTERN_C_END
2349bef8e0ddSBarry Smith 
23504a2ae208SSatish Balay #undef __FUNCT__
23514a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2352bef8e0ddSBarry Smith /*@
2353bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2354bef8e0ddSBarry Smith        in the matrix.
2355bef8e0ddSBarry Smith 
2356bef8e0ddSBarry Smith   Input Parameters:
2357bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2358bef8e0ddSBarry Smith -  indices - the column indices
2359bef8e0ddSBarry Smith 
236015091d37SBarry Smith   Level: advanced
236115091d37SBarry Smith 
2362bef8e0ddSBarry Smith   Notes:
2363bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2364bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2365bef8e0ddSBarry Smith   of the MatSetValues() operation.
2366bef8e0ddSBarry Smith 
2367bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2368d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2369bef8e0ddSBarry Smith 
2370bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2371bef8e0ddSBarry Smith 
2372b9617806SBarry Smith     The indices should start with zero, not one.
2373b9617806SBarry Smith 
2374bef8e0ddSBarry Smith @*/
2375be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2376bef8e0ddSBarry Smith {
237797f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2378bef8e0ddSBarry Smith 
2379bef8e0ddSBarry Smith   PetscFunctionBegin;
23804482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
23814482741eSBarry Smith   PetscValidPointer(indices,2);
2382c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2383bef8e0ddSBarry Smith   if (f) {
2384bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2385bef8e0ddSBarry Smith   } else {
2386634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2387bef8e0ddSBarry Smith   }
2388bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2389bef8e0ddSBarry Smith }
2390bef8e0ddSBarry Smith 
2391be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2392be6bf707SBarry Smith 
2393fb2e594dSBarry Smith EXTERN_C_BEGIN
23944a2ae208SSatish Balay #undef __FUNCT__
23954a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2396be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2397be6bf707SBarry Smith {
2398be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23996849ba73SBarry Smith   PetscErrorCode ierr;
2400899cda47SBarry Smith   size_t         nz = aij->i[mat->rmap.n];
2401be6bf707SBarry Smith 
2402be6bf707SBarry Smith   PetscFunctionBegin;
2403be6bf707SBarry Smith   if (aij->nonew != 1) {
2404634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2405be6bf707SBarry Smith   }
2406be6bf707SBarry Smith 
2407be6bf707SBarry Smith   /* allocate space for values if not already there */
2408be6bf707SBarry Smith   if (!aij->saved_values) {
240987828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2410be6bf707SBarry Smith   }
2411be6bf707SBarry Smith 
2412be6bf707SBarry Smith   /* copy values over */
241387828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2414be6bf707SBarry Smith   PetscFunctionReturn(0);
2415be6bf707SBarry Smith }
2416fb2e594dSBarry Smith EXTERN_C_END
2417be6bf707SBarry Smith 
24184a2ae208SSatish Balay #undef __FUNCT__
2419b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2420be6bf707SBarry Smith /*@
2421be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2422be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2423be6bf707SBarry Smith        nonlinear portion.
2424be6bf707SBarry Smith 
2425be6bf707SBarry Smith    Collect on Mat
2426be6bf707SBarry Smith 
2427be6bf707SBarry Smith   Input Parameters:
24280e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2429be6bf707SBarry Smith 
243015091d37SBarry Smith   Level: advanced
243115091d37SBarry Smith 
2432be6bf707SBarry Smith   Common Usage, with SNESSolve():
2433be6bf707SBarry Smith $    Create Jacobian matrix
2434be6bf707SBarry Smith $    Set linear terms into matrix
2435be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2436be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2437be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2438be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2439be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2440be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2441be6bf707SBarry Smith $    In your Jacobian routine
2442be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2443be6bf707SBarry Smith $      Set nonlinear terms in matrix
2444be6bf707SBarry Smith 
2445be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2446be6bf707SBarry Smith $    // build linear portion of Jacobian
2447be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2448be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2449be6bf707SBarry Smith $    loop over nonlinear iterations
2450be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2451be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2452be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2453be6bf707SBarry Smith $       Solve linear system with Jacobian
2454be6bf707SBarry Smith $    endloop
2455be6bf707SBarry Smith 
2456be6bf707SBarry Smith   Notes:
2457be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2458be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2459be6bf707SBarry Smith     calling this routine.
2460be6bf707SBarry Smith 
24610c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
24620c468ba9SBarry Smith     and does not allocated additional space.
24630c468ba9SBarry Smith 
2464be6bf707SBarry Smith .seealso: MatRetrieveValues()
2465be6bf707SBarry Smith 
2466be6bf707SBarry Smith @*/
2467be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2468be6bf707SBarry Smith {
2469dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2470be6bf707SBarry Smith 
2471be6bf707SBarry Smith   PetscFunctionBegin;
24724482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
247329bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
247429bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2475be6bf707SBarry Smith 
2476c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2477be6bf707SBarry Smith   if (f) {
2478be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2479be6bf707SBarry Smith   } else {
2480634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2481be6bf707SBarry Smith   }
2482be6bf707SBarry Smith   PetscFunctionReturn(0);
2483be6bf707SBarry Smith }
2484be6bf707SBarry Smith 
2485fb2e594dSBarry Smith EXTERN_C_BEGIN
24864a2ae208SSatish Balay #undef __FUNCT__
24874a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2488be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2489be6bf707SBarry Smith {
2490be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
24916849ba73SBarry Smith   PetscErrorCode ierr;
2492899cda47SBarry Smith   PetscInt       nz = aij->i[mat->rmap.n];
2493be6bf707SBarry Smith 
2494be6bf707SBarry Smith   PetscFunctionBegin;
2495be6bf707SBarry Smith   if (aij->nonew != 1) {
2496634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2497be6bf707SBarry Smith   }
2498be6bf707SBarry Smith   if (!aij->saved_values) {
2499634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2500be6bf707SBarry Smith   }
2501be6bf707SBarry Smith   /* copy values over */
250287828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2503be6bf707SBarry Smith   PetscFunctionReturn(0);
2504be6bf707SBarry Smith }
2505fb2e594dSBarry Smith EXTERN_C_END
2506be6bf707SBarry Smith 
25074a2ae208SSatish Balay #undef __FUNCT__
25084a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2509be6bf707SBarry Smith /*@
2510be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2511be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2512be6bf707SBarry Smith        nonlinear portion.
2513be6bf707SBarry Smith 
2514be6bf707SBarry Smith    Collect on Mat
2515be6bf707SBarry Smith 
2516be6bf707SBarry Smith   Input Parameters:
2517be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2518be6bf707SBarry Smith 
251915091d37SBarry Smith   Level: advanced
252015091d37SBarry Smith 
2521be6bf707SBarry Smith .seealso: MatStoreValues()
2522be6bf707SBarry Smith 
2523be6bf707SBarry Smith @*/
2524be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2525be6bf707SBarry Smith {
2526dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2527be6bf707SBarry Smith 
2528be6bf707SBarry Smith   PetscFunctionBegin;
25294482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
253029bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
253129bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2532be6bf707SBarry Smith 
2533c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2534be6bf707SBarry Smith   if (f) {
2535be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2536be6bf707SBarry Smith   } else {
2537634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2538be6bf707SBarry Smith   }
2539be6bf707SBarry Smith   PetscFunctionReturn(0);
2540be6bf707SBarry Smith }
2541be6bf707SBarry Smith 
2542f83d6046SBarry Smith 
2543be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
25444a2ae208SSatish Balay #undef __FUNCT__
25454a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
254617ab2063SBarry Smith /*@C
2547682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
25480d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
25496e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
255051c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
25512bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
255217ab2063SBarry Smith 
2553db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2554db81eaa0SLois Curfman McInnes 
255517ab2063SBarry Smith    Input Parameters:
2556db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
255717ab2063SBarry Smith .  m - number of rows
255817ab2063SBarry Smith .  n - number of columns
255917ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
256051c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
25612bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
256217ab2063SBarry Smith 
256317ab2063SBarry Smith    Output Parameter:
2564416022c9SBarry Smith .  A - the matrix
256517ab2063SBarry Smith 
2566b259b22eSLois Curfman McInnes    Notes:
256749a6f317SBarry Smith    If nnz is given then nz is ignored
256849a6f317SBarry Smith 
256917ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
257017ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
25710002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
257244cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
257317ab2063SBarry Smith 
257417ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2575a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
25763d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
25776da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
257817ab2063SBarry Smith 
2579682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
25804fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2581682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
25826c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
25836c7ebb05SLois Curfman McInnes 
25846c7ebb05SLois Curfman McInnes    Options Database Keys:
2585698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2586698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2587db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2588db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2589db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
259017ab2063SBarry Smith 
2591027ccd11SLois Curfman McInnes    Level: intermediate
2592027ccd11SLois Curfman McInnes 
259336db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
259436db0b34SBarry Smith 
259517ab2063SBarry Smith @*/
2596be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
259717ab2063SBarry Smith {
2598dfbe8321SBarry Smith   PetscErrorCode ierr;
25996945ee14SBarry Smith 
26003a40ed3dSBarry Smith   PetscFunctionBegin;
2601f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2602f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2603273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2604ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2605273d9f13SBarry Smith   PetscFunctionReturn(0);
2606273d9f13SBarry Smith }
2607273d9f13SBarry Smith 
26084a2ae208SSatish Balay #undef __FUNCT__
26094a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2610273d9f13SBarry Smith /*@C
2611273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2612273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2613273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2614273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2615273d9f13SBarry Smith 
2616273d9f13SBarry Smith    Collective on MPI_Comm
2617273d9f13SBarry Smith 
2618273d9f13SBarry Smith    Input Parameters:
261945bfc511SMatthew Knepley +  B - The matrix
2620273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2621273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2622273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2623273d9f13SBarry Smith 
2624273d9f13SBarry Smith    Notes:
262549a6f317SBarry Smith      If nnz is given then nz is ignored
262649a6f317SBarry Smith 
2627273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2628273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2629273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2630273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2631273d9f13SBarry Smith 
2632273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2633273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2634273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2635273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2636273d9f13SBarry Smith 
2637a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2638a96a251dSBarry Smith    entries or columns indices
2639a96a251dSBarry Smith 
2640273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2641273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2642273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2643273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2644273d9f13SBarry Smith 
2645273d9f13SBarry Smith    Options Database Keys:
2646698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2647698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2648273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2649273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2650273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2651273d9f13SBarry Smith 
2652273d9f13SBarry Smith    Level: intermediate
2653273d9f13SBarry Smith 
2654273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2655273d9f13SBarry Smith 
2656273d9f13SBarry Smith @*/
2657be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2658273d9f13SBarry Smith {
265997f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2660a23d5eceSKris Buschelman 
2661a23d5eceSKris Buschelman   PetscFunctionBegin;
2662a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2663a23d5eceSKris Buschelman   if (f) {
2664a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2665a23d5eceSKris Buschelman   }
2666a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2667a23d5eceSKris Buschelman }
2668a23d5eceSKris Buschelman 
2669a23d5eceSKris Buschelman EXTERN_C_BEGIN
2670a23d5eceSKris Buschelman #undef __FUNCT__
2671a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2672be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2673a23d5eceSKris Buschelman {
2674273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2675a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
26766849ba73SBarry Smith   PetscErrorCode ierr;
267797f1f81fSBarry Smith   PetscInt       i;
2678273d9f13SBarry Smith 
2679273d9f13SBarry Smith   PetscFunctionBegin;
2680d5d45c9bSBarry Smith 
2681a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2682c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2683c461c341SBarry Smith     nz             = 0;
2684c461c341SBarry Smith   }
2685c461c341SBarry Smith 
2686899cda47SBarry Smith   B->rmap.bs = B->cmap.bs = 1;
2687899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->rmap);CHKERRQ(ierr);
2688899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->cmap);CHKERRQ(ierr);
2689899cda47SBarry Smith 
2690435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2691435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2692b73539f3SBarry Smith   if (nnz) {
2693899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) {
269429bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2695899cda47SBarry Smith       if (nnz[i] > B->cmap.n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap.n);
2696b73539f3SBarry Smith     }
2697b73539f3SBarry Smith   }
2698b73539f3SBarry Smith 
2699273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2700273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2701273d9f13SBarry Smith 
2702ab93d7beSBarry Smith   if (!skipallocation) {
2703899cda47SBarry Smith     ierr = PetscMalloc2(B->rmap.n,PetscInt,&b->imax,B->rmap.n,PetscInt,&b->ilen);CHKERRQ(ierr);
2704273d9f13SBarry Smith     if (!nnz) {
2705435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2706273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2707899cda47SBarry Smith       for (i=0; i<B->rmap.n; i++) b->imax[i] = nz;
2708899cda47SBarry Smith       nz = nz*B->rmap.n;
2709273d9f13SBarry Smith     } else {
2710273d9f13SBarry Smith       nz = 0;
2711899cda47SBarry Smith       for (i=0; i<B->rmap.n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2712273d9f13SBarry Smith     }
2713273d9f13SBarry Smith 
2714ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2715899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) { b->ilen[i] = 0;}
2716ab93d7beSBarry Smith 
2717273d9f13SBarry Smith     /* allocate the matrix space */
2718899cda47SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap.n+1,PetscInt,&b->i);CHKERRQ(ierr);
2719bfeeae90SHong Zhang     b->i[0] = 0;
2720899cda47SBarry Smith     for (i=1; i<B->rmap.n+1; i++) {
27215da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
27225da197adSKris Buschelman     }
2723273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2724e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2725e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2726c461c341SBarry Smith   } else {
2727e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2728e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2729c461c341SBarry Smith   }
2730273d9f13SBarry Smith 
2731273d9f13SBarry Smith   b->nz                = 0;
2732273d9f13SBarry Smith   b->maxnz             = nz;
2733273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2734273d9f13SBarry Smith   PetscFunctionReturn(0);
2735273d9f13SBarry Smith }
2736a23d5eceSKris Buschelman EXTERN_C_END
2737273d9f13SBarry Smith 
2738a1661176SMatthew Knepley #undef  __FUNCT__
2739a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
2740a1661176SMatthew Knepley /*@C
2741a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2742a1661176SMatthew Knepley 
2743a1661176SMatthew Knepley    Input Parameters:
2744a1661176SMatthew Knepley +  B - the matrix
2745a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2746a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2747a1661176SMatthew Knepley -  v - optional values in the matrix
2748a1661176SMatthew Knepley 
2749d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
2750d58b2322SMatthew Knepley 
2751a1661176SMatthew Knepley    Level: developer
2752a1661176SMatthew Knepley 
2753a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2754a1661176SMatthew Knepley 
2755a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
2756a1661176SMatthew Knepley @*/
2757a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
2758a1661176SMatthew Knepley {
2759a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2760a1661176SMatthew Knepley   PetscErrorCode ierr;
2761a1661176SMatthew Knepley 
2762a1661176SMatthew Knepley   PetscFunctionBegin;
2763a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
2764a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2765a1661176SMatthew Knepley   if (f) {
2766a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2767a1661176SMatthew Knepley   }
2768a1661176SMatthew Knepley   PetscFunctionReturn(0);
2769a1661176SMatthew Knepley }
2770a1661176SMatthew Knepley 
2771a1661176SMatthew Knepley EXTERN_C_BEGIN
2772a1661176SMatthew Knepley #undef  __FUNCT__
2773a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
2774b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
2775a1661176SMatthew Knepley {
2776a1661176SMatthew Knepley   PetscInt       i;
2777a1661176SMatthew Knepley   PetscInt       m,n;
2778a1661176SMatthew Knepley   PetscInt       nz;
2779a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
2780a1661176SMatthew Knepley   PetscScalar    *values;
2781a1661176SMatthew Knepley   PetscErrorCode ierr;
2782a1661176SMatthew Knepley 
2783a1661176SMatthew Knepley   PetscFunctionBegin;
2784a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
2785a1661176SMatthew Knepley 
2786b7940d39SSatish Balay   if (Ii[0]) {
2787b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
2788a1661176SMatthew Knepley   }
2789a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
2790a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2791b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
2792a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
2793a1661176SMatthew Knepley     if (nz < 0) {
2794a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
2795a1661176SMatthew Knepley     }
2796a1661176SMatthew Knepley     nnz[i] = nz;
2797a1661176SMatthew Knepley   }
2798a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
2799a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
2800a1661176SMatthew Knepley 
2801a1661176SMatthew Knepley   if (v) {
2802a1661176SMatthew Knepley     values = (PetscScalar*) v;
2803a1661176SMatthew Knepley   } else {
2804a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
2805a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2806a1661176SMatthew Knepley   }
2807a1661176SMatthew Knepley 
2808a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2809a1661176SMatthew Knepley 
2810a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
2811b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
2812b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
2813a1661176SMatthew Knepley   }
2814a1661176SMatthew Knepley 
2815a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2816a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2817a1661176SMatthew Knepley   ierr = MatSetOption(B,MAT_COLUMNS_UNSORTED);CHKERRQ(ierr);
2818a1661176SMatthew Knepley 
2819a1661176SMatthew Knepley   if (!v) {
2820a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
2821a1661176SMatthew Knepley   }
2822a1661176SMatthew Knepley   PetscFunctionReturn(0);
2823a1661176SMatthew Knepley }
2824a1661176SMatthew Knepley EXTERN_C_END
2825a1661176SMatthew Knepley 
28260bad9183SKris Buschelman /*MC
2827fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
28280bad9183SKris Buschelman    based on compressed sparse row format.
28290bad9183SKris Buschelman 
28300bad9183SKris Buschelman    Options Database Keys:
28310bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
28320bad9183SKris Buschelman 
28330bad9183SKris Buschelman   Level: beginner
28340bad9183SKris Buschelman 
2835f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
28360bad9183SKris Buschelman M*/
28370bad9183SKris Buschelman 
2838a6175056SHong Zhang EXTERN_C_BEGIN
283917667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
284017667f90SBarry Smith EXTERN_C_END
284117667f90SBarry Smith 
284217667f90SBarry Smith EXTERN_C_BEGIN
28434a2ae208SSatish Balay #undef __FUNCT__
28444a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2845be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
2846273d9f13SBarry Smith {
2847273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2848dfbe8321SBarry Smith   PetscErrorCode ierr;
284938baddfdSBarry Smith   PetscMPIInt    size;
2850273d9f13SBarry Smith 
2851273d9f13SBarry Smith   PetscFunctionBegin;
2852273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2853273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2854273d9f13SBarry Smith 
2855b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2856b0a32e0cSBarry Smith   B->data             = (void*)b;
2857549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2858416022c9SBarry Smith   B->factor           = 0;
285990f02eecSBarry Smith   B->mapping          = 0;
2860416022c9SBarry Smith   b->row              = 0;
2861416022c9SBarry Smith   b->col              = 0;
286282bf6240SBarry Smith   b->icol             = 0;
2863b810aeb4SBarry Smith   b->reallocs         = 0;
2864f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
286536db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2866f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2867416022c9SBarry Smith   b->nonew             = 0;
2868416022c9SBarry Smith   b->diag              = 0;
2869416022c9SBarry Smith   b->solve_work        = 0;
28702a1b7f2aSHong Zhang   B->spptr             = 0;
2871be6bf707SBarry Smith   b->saved_values      = 0;
2872d7f994e1SBarry Smith   b->idiag             = 0;
2873d7f994e1SBarry Smith   b->ssor              = 0;
2874f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2875a30b2313SHong Zhang   b->xtoy              = 0;
2876a30b2313SHong Zhang   b->XtoY              = 0;
287773e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
2878899cda47SBarry Smith   b->compressedrow.nrows   = B->rmap.n;
2879d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
2880d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
2881d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
288288e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
288317ab2063SBarry Smith 
288435d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
2885f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2886bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2887bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2888f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2889be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2890bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2891f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2892be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2893bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2894a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2895a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2896a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
289785fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
289885fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
289985fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
2900ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
2901ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
2902ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
290317667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
290417667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
290517667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
29065fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
29075fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
29085fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2909a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2910a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2911a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
2912a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
2913a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
2914a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
291505b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
291605b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
291705b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
29184846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
291917667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
29203a40ed3dSBarry Smith   PetscFunctionReturn(0);
292117ab2063SBarry Smith }
2922273d9f13SBarry Smith EXTERN_C_END
292317ab2063SBarry Smith 
29244a2ae208SSatish Balay #undef __FUNCT__
29254a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2926dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
292717ab2063SBarry Smith {
2928416022c9SBarry Smith   Mat            C;
2929416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
29306849ba73SBarry Smith   PetscErrorCode ierr;
2931899cda47SBarry Smith   PetscInt       i,m = A->rmap.n;
293217ab2063SBarry Smith 
29333a40ed3dSBarry Smith   PetscFunctionBegin;
29344043dd9cSLois Curfman McInnes   *B = 0;
2935f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&C);CHKERRQ(ierr);
2936899cda47SBarry Smith   ierr = MatSetSizes(C,A->rmap.n,A->cmap.n,A->rmap.n,A->cmap.n);CHKERRQ(ierr);
2937be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
29381d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
29391d5dac46SHong Zhang 
2940899cda47SBarry Smith   ierr = PetscMapCopy(A->comm,&A->rmap,&C->rmap);CHKERRQ(ierr);
2941899cda47SBarry Smith   ierr = PetscMapCopy(A->comm,&A->cmap,&C->cmap);CHKERRQ(ierr);
2942899cda47SBarry Smith 
2943273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
2944273d9f13SBarry Smith 
2945416022c9SBarry Smith   C->factor           = A->factor;
29466ad4291fSHong Zhang 
2947416022c9SBarry Smith   c->row            = 0;
2948416022c9SBarry Smith   c->col            = 0;
294982bf6240SBarry Smith   c->icol           = 0;
29506ad4291fSHong Zhang   c->reallocs       = 0;
295117ab2063SBarry Smith 
29526ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
295317ab2063SBarry Smith 
295433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
295517ab2063SBarry Smith   for (i=0; i<m; i++) {
2956416022c9SBarry Smith     c->imax[i] = a->imax[i];
2957416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
295817ab2063SBarry Smith   }
295917ab2063SBarry Smith 
296017ab2063SBarry Smith   /* allocate the matrix space */
2961a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
2962f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
296397f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
296417ab2063SBarry Smith   if (m > 0) {
296597f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
2966be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2967bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2968be6bf707SBarry Smith     } else {
2969bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
297017ab2063SBarry Smith     }
297108480c60SBarry Smith   }
297217ab2063SBarry Smith 
2973416022c9SBarry Smith   c->sorted            = a->sorted;
29746ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
2975416022c9SBarry Smith   c->roworiented       = a->roworiented;
2976416022c9SBarry Smith   c->nonew             = a->nonew;
2977416022c9SBarry Smith   if (a->diag) {
297897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
297952e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
298017ab2063SBarry Smith     for (i=0; i<m; i++) {
2981416022c9SBarry Smith       c->diag[i] = a->diag[i];
298217ab2063SBarry Smith     }
29833a40ed3dSBarry Smith   } else c->diag        = 0;
29846ad4291fSHong Zhang   c->solve_work         = 0;
29856ad4291fSHong Zhang   c->saved_values          = 0;
29866ad4291fSHong Zhang   c->idiag                 = 0;
29876ad4291fSHong Zhang   c->ssor                  = 0;
29886ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
2989e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
2990e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
29916ad4291fSHong Zhang   c->xtoy                  = 0;
29926ad4291fSHong Zhang   c->XtoY                  = 0;
29936ad4291fSHong Zhang 
2994416022c9SBarry Smith   c->nz                 = a->nz;
2995416022c9SBarry Smith   c->maxnz              = a->maxnz;
2996273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2997754ec7b1SSatish Balay 
29986ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
29996ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
30006ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
30016ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
30026ad4291fSHong Zhang     i = a->compressedrow.nrows;
30036ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
30046ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
30056ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
30066ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
300727ea64f8SHong Zhang   } else {
300827ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
300927ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
301027ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
30116ad4291fSHong Zhang   }
301288e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
30134846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
30144846f1f5SKris Buschelman 
3015416022c9SBarry Smith   *B = C;
3016b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
30173a40ed3dSBarry Smith   PetscFunctionReturn(0);
301817ab2063SBarry Smith }
301917ab2063SBarry Smith 
30204a2ae208SSatish Balay #undef __FUNCT__
30214a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3022f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A)
302317ab2063SBarry Smith {
3024416022c9SBarry Smith   Mat_SeqAIJ     *a;
3025416022c9SBarry Smith   Mat            B;
30266849ba73SBarry Smith   PetscErrorCode ierr;
30273c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
302838baddfdSBarry Smith   int            fd;
302938baddfdSBarry Smith   PetscMPIInt    size;
3030bcd2baecSBarry Smith   MPI_Comm       comm;
303117ab2063SBarry Smith 
30323a40ed3dSBarry Smith   PetscFunctionBegin;
3033e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3034d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
303529bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3036b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
30370752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3038552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
303917ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
304017ab2063SBarry Smith 
3041d64ed03dSBarry Smith   if (nz < 0) {
304229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3043d64ed03dSBarry Smith   }
3044d64ed03dSBarry Smith 
304517ab2063SBarry Smith   /* read in row lengths */
304697f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
30470752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
304817ab2063SBarry Smith 
30493c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
30503c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
30513c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
30523c601197SSatish Balay 
305317ab2063SBarry Smith   /* create our matrix */
3054f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3055f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3056b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3057ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3058416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
305917ab2063SBarry Smith 
30600752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
306117ab2063SBarry Smith 
306217ab2063SBarry Smith   /* read in nonzero values */
30630752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
306417ab2063SBarry Smith 
306517ab2063SBarry Smith   /* set matrix "i" values */
3066efb16452SHong Zhang   a->i[0] = 0;
306717ab2063SBarry Smith   for (i=1; i<= M; i++) {
3068416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3069416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
307017ab2063SBarry Smith   }
3071606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
307217ab2063SBarry Smith 
30736d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
30746d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3075b3a1e11cSKris Buschelman   *A = B;
30763a40ed3dSBarry Smith   PetscFunctionReturn(0);
307717ab2063SBarry Smith }
307817ab2063SBarry Smith 
30794a2ae208SSatish Balay #undef __FUNCT__
3080b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3081dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
30827264ac53SSatish Balay {
30837264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3084dfbe8321SBarry Smith   PetscErrorCode ierr;
30857264ac53SSatish Balay 
30863a40ed3dSBarry Smith   PetscFunctionBegin;
3087bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3088899cda47SBarry Smith   if ((A->rmap.n != B->rmap.n) || (A->cmap.n != B->cmap.n) ||(a->nz != b->nz)) {
3089ca44d042SBarry Smith     *flg = PETSC_FALSE;
3090ca44d042SBarry Smith     PetscFunctionReturn(0);
3091bcd2baecSBarry Smith   }
30927264ac53SSatish Balay 
30937264ac53SSatish Balay   /* if the a->i are the same */
3094899cda47SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap.n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3095abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
30967264ac53SSatish Balay 
30977264ac53SSatish Balay   /* if a->j are the same */
309897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3099abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3100bcd2baecSBarry Smith 
3101bcd2baecSBarry Smith   /* if a->a are the same */
310287828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
31030f5bd95cSBarry Smith 
31043a40ed3dSBarry Smith   PetscFunctionReturn(0);
31057264ac53SSatish Balay 
31067264ac53SSatish Balay }
310736db0b34SBarry Smith 
31084a2ae208SSatish Balay #undef __FUNCT__
31094a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
311005869f15SSatish Balay /*@
311136db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
311236db0b34SBarry Smith               provided by the user.
311336db0b34SBarry Smith 
3114c75a6043SHong Zhang       Collective on MPI_Comm
311536db0b34SBarry Smith 
311636db0b34SBarry Smith    Input Parameters:
311736db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
311836db0b34SBarry Smith .   m - number of rows
311936db0b34SBarry Smith .   n - number of columns
312036db0b34SBarry Smith .   i - row indices
312136db0b34SBarry Smith .   j - column indices
312236db0b34SBarry Smith -   a - matrix values
312336db0b34SBarry Smith 
312436db0b34SBarry Smith    Output Parameter:
312536db0b34SBarry Smith .   mat - the matrix
312636db0b34SBarry Smith 
312736db0b34SBarry Smith    Level: intermediate
312836db0b34SBarry Smith 
312936db0b34SBarry Smith    Notes:
31300551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
313136db0b34SBarry Smith     once the matrix is destroyed
313236db0b34SBarry Smith 
313336db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
313436db0b34SBarry Smith 
3135bfeeae90SHong Zhang        The i and j indices are 0 based
313636db0b34SBarry Smith 
31372fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
313836db0b34SBarry Smith 
313936db0b34SBarry Smith @*/
3140be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
314136db0b34SBarry Smith {
3142dfbe8321SBarry Smith   PetscErrorCode ierr;
314397f1f81fSBarry Smith   PetscInt       ii;
314436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
314536db0b34SBarry Smith 
314636db0b34SBarry Smith   PetscFunctionBegin;
3147a96a251dSBarry Smith   if (i[0]) {
3148634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
314936db0b34SBarry Smith   }
3150f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3151f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3152ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3153ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3154ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3155ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3156ab93d7beSBarry Smith 
315736db0b34SBarry Smith   aij->i = i;
315836db0b34SBarry Smith   aij->j = j;
315936db0b34SBarry Smith   aij->a = a;
316036db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
316136db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3162e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3163e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
316436db0b34SBarry Smith 
316536db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
316636db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
31672515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
316879a5c55eSBarry 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]);
316936db0b34SBarry Smith #endif
317036db0b34SBarry Smith   }
31712515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
317236db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
317379a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
317479a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
317536db0b34SBarry Smith   }
317636db0b34SBarry Smith #endif
317736db0b34SBarry Smith 
3178b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3179b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
318036db0b34SBarry Smith   PetscFunctionReturn(0);
318136db0b34SBarry Smith }
318236db0b34SBarry Smith 
3183cc8ba8e1SBarry Smith #undef __FUNCT__
3184ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3185dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3186cc8ba8e1SBarry Smith {
3187dfbe8321SBarry Smith   PetscErrorCode ierr;
3188cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
318936db0b34SBarry Smith 
3190cc8ba8e1SBarry Smith   PetscFunctionBegin;
31918ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3192cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3193cc8ba8e1SBarry Smith     a->coloring = coloring;
319412c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
319597f1f81fSBarry Smith     PetscInt             i,*larray;
319612c595b3SBarry Smith     ISColoring      ocoloring;
319708b6dcc0SBarry Smith     ISColoringValue *colors;
319812c595b3SBarry Smith 
319912c595b3SBarry Smith     /* set coloring for diagonal portion */
3200899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3201899cda47SBarry Smith     for (i=0; i<A->cmap.n; i++) {
320212c595b3SBarry Smith       larray[i] = i;
320312c595b3SBarry Smith     }
3204899cda47SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap.n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3205899cda47SBarry Smith     ierr = PetscMalloc((A->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3206899cda47SBarry Smith     for (i=0; i<A->cmap.n; i++) {
320712c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
320812c595b3SBarry Smith     }
320912c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
321095a80f87SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap.n,colors,&ocoloring);CHKERRQ(ierr);
321112c595b3SBarry Smith     a->coloring = ocoloring;
321212c595b3SBarry Smith   }
3213cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3214cc8ba8e1SBarry Smith }
3215cc8ba8e1SBarry Smith 
3216dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3217ee4f033dSBarry Smith EXTERN_C_BEGIN
321829c1e371SBarry Smith #include "adic/ad_utils.h"
3219ee4f033dSBarry Smith EXTERN_C_END
3220cc8ba8e1SBarry Smith 
3221cc8ba8e1SBarry Smith #undef __FUNCT__
3222ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3223dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3224cc8ba8e1SBarry Smith {
3225cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3226899cda47SBarry Smith   PetscInt        m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
32274440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
322808b6dcc0SBarry Smith   ISColoringValue *color;
3229cc8ba8e1SBarry Smith 
3230cc8ba8e1SBarry Smith   PetscFunctionBegin;
3231e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
32324440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3233cc8ba8e1SBarry Smith   color = a->coloring->colors;
3234cc8ba8e1SBarry Smith   /* loop over rows */
3235cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3236cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3237cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3238cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3239cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3240cc8ba8e1SBarry Smith     }
32414440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3242ee4f033dSBarry Smith   }
3243ee4f033dSBarry Smith   PetscFunctionReturn(0);
3244ee4f033dSBarry Smith }
3245ee4f033dSBarry Smith #endif
3246ee4f033dSBarry Smith 
3247ee4f033dSBarry Smith #undef __FUNCT__
3248ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
324997f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3250ee4f033dSBarry Smith {
3251ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3252899cda47SBarry Smith   PetscInt             m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j;
325387828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
325408b6dcc0SBarry Smith   ISColoringValue *color;
3255ee4f033dSBarry Smith 
3256ee4f033dSBarry Smith   PetscFunctionBegin;
3257e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3258ee4f033dSBarry Smith   color = a->coloring->colors;
3259ee4f033dSBarry Smith   /* loop over rows */
3260ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3261ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3262ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3263ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3264ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3265ee4f033dSBarry Smith     }
3266ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3267cc8ba8e1SBarry Smith   }
3268cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3269cc8ba8e1SBarry Smith }
327036db0b34SBarry Smith 
327181824310SBarry Smith /*
327281824310SBarry Smith     Special version for direct calls from Fortran
327381824310SBarry Smith */
327481824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
327581824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
327681824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
327781824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
327881824310SBarry Smith #endif
327981824310SBarry Smith 
328081824310SBarry Smith /* Change these macros so can be used in void function */
328181824310SBarry Smith #undef CHKERRQ
328281824310SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(A->comm,ierr)
328381824310SBarry Smith #undef SETERRQ2
328481824310SBarry Smith #define SETERRQ2(ierr,b,c,d) CHKERRABORT(A->comm,ierr)
328581824310SBarry Smith 
328681824310SBarry Smith EXTERN_C_BEGIN
328781824310SBarry Smith #undef __FUNCT__
328881824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
32891f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
329081824310SBarry Smith {
329181824310SBarry Smith   Mat            A = *AA;
329281824310SBarry Smith   PetscInt       m = *mm, n = *nn;
329381824310SBarry Smith   InsertMode     is = *isis;
329481824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
329581824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
329681824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
329781824310SBarry Smith   PetscErrorCode ierr;
329881824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
329981824310SBarry Smith   PetscScalar    *ap,value,*aa;
330081824310SBarry Smith   PetscTruth     ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
330181824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
330281824310SBarry Smith 
330381824310SBarry Smith   PetscFunctionBegin;
330481824310SBarry Smith   MatPreallocated(A);
330581824310SBarry Smith   imax = a->imax;
330681824310SBarry Smith   ai = a->i;
330781824310SBarry Smith   ailen = a->ilen;
330881824310SBarry Smith   aj = a->j;
330981824310SBarry Smith   aa = a->a;
331081824310SBarry Smith 
331181824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
331281824310SBarry Smith     row  = im[k];
331381824310SBarry Smith     if (row < 0) continue;
331481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3315899cda47SBarry Smith     if (row >= A->rmap.n) SETERRABORT(A->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
331681824310SBarry Smith #endif
331781824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
331881824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
331981824310SBarry Smith     low  = 0;
332081824310SBarry Smith     high = nrow;
332181824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
332281824310SBarry Smith       if (in[l] < 0) continue;
332381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3324899cda47SBarry Smith       if (in[l] >= A->cmap.n) SETERRABORT(A->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
332581824310SBarry Smith #endif
332681824310SBarry Smith       col = in[l];
332781824310SBarry Smith       if (roworiented) {
332881824310SBarry Smith         value = v[l + k*n];
332981824310SBarry Smith       } else {
333081824310SBarry Smith         value = v[k + l*m];
333181824310SBarry Smith       }
333281824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
333381824310SBarry Smith 
333481824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
333581824310SBarry Smith       lastcol = col;
333681824310SBarry Smith       while (high-low > 5) {
333781824310SBarry Smith         t = (low+high)/2;
333881824310SBarry Smith         if (rp[t] > col) high = t;
333981824310SBarry Smith         else             low  = t;
334081824310SBarry Smith       }
334181824310SBarry Smith       for (i=low; i<high; i++) {
334281824310SBarry Smith         if (rp[i] > col) break;
334381824310SBarry Smith         if (rp[i] == col) {
334481824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
334581824310SBarry Smith           else                  ap[i] = value;
334681824310SBarry Smith           goto noinsert;
334781824310SBarry Smith         }
334881824310SBarry Smith       }
334981824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
335081824310SBarry Smith       if (nonew == 1) goto noinsert;
335181824310SBarry Smith       if (nonew == -1) SETERRABORT(A->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3352421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
335381824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
335481824310SBarry Smith       /* shift up all the later entries in this row */
335581824310SBarry Smith       for (ii=N; ii>=i; ii--) {
335681824310SBarry Smith         rp[ii+1] = rp[ii];
335781824310SBarry Smith         ap[ii+1] = ap[ii];
335881824310SBarry Smith       }
335981824310SBarry Smith       rp[i] = col;
336081824310SBarry Smith       ap[i] = value;
336181824310SBarry Smith       noinsert:;
336281824310SBarry Smith       low = i + 1;
336381824310SBarry Smith     }
336481824310SBarry Smith     ailen[row] = nrow;
336581824310SBarry Smith   }
336681824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
336781824310SBarry Smith   PetscFunctionReturnVoid();
336881824310SBarry Smith }
336981824310SBarry Smith EXTERN_C_END
3370