xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 8e58a17063def9c11d307e97b7d4ceafd1fd34ac)
1992c00aaSSatish Balay #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3b377110cSBarry Smith 
4d5d45c9bSBarry Smith /*
53369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
6d5d45c9bSBarry Smith   matrix storage format.
7d5d45c9bSBarry Smith */
83369ce9aSBarry Smith 
97c4f633dSBarry Smith 
107c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
11f3da1532SBarry Smith #include "petscblaslapack.h"
120a835dfdSSatish Balay #include "petscbt.h"
1317ab2063SBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
1579299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1679299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1779299369SBarry Smith {
1879299369SBarry Smith   PetscErrorCode ierr;
1979299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
20d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
2154f21887SBarry Smith   MatScalar      *aa = aij->a;
2254f21887SBarry Smith   PetscScalar    *v;
2309f38230SBarry Smith   PetscTruth     missing;
2479299369SBarry Smith 
2579299369SBarry Smith   PetscFunctionBegin;
2609f38230SBarry Smith   if (Y->assembled) {
2709f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2809f38230SBarry Smith     if (!missing) {
2979299369SBarry Smith       diag = aij->diag;
3079299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3179299369SBarry Smith       if (is == INSERT_VALUES) {
3279299369SBarry Smith 	for (i=0; i<m; i++) {
3379299369SBarry Smith 	  aa[diag[i]] = v[i];
3479299369SBarry Smith 	}
3579299369SBarry Smith       } else {
3679299369SBarry Smith 	for (i=0; i<m; i++) {
3779299369SBarry Smith 	  aa[diag[i]] += v[i];
3879299369SBarry Smith 	}
3979299369SBarry Smith       }
4079299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4179299369SBarry Smith       PetscFunctionReturn(0);
4279299369SBarry Smith     }
4309f38230SBarry Smith   }
4409f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4509f38230SBarry Smith   PetscFunctionReturn(0);
4609f38230SBarry Smith }
4779299369SBarry Smith 
4879299369SBarry Smith #undef __FUNCT__
494a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
503acb8795SBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5117ab2063SBarry Smith {
52416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
53dfbe8321SBarry Smith   PetscErrorCode ierr;
5497f1f81fSBarry Smith   PetscInt       i,ishift;
5517ab2063SBarry Smith 
563a40ed3dSBarry Smith   PetscFunctionBegin;
57d0f46423SBarry Smith   *m     = A->rmap->n;
583a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
59bfeeae90SHong Zhang   ishift = 0;
6053e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
61d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
62bfeeae90SHong Zhang   } else if (oshift == 1) {
63d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
643b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
65d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
66d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
67ecc77c7aSBarry Smith     if (ja) {
6897f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
693b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
70ecc77c7aSBarry Smith     }
716945ee14SBarry Smith   } else {
72ecc77c7aSBarry Smith     *ia = a->i;
73ecc77c7aSBarry Smith     if (ja) *ja = a->j;
74a2ce50c7SBarry Smith   }
753a40ed3dSBarry Smith   PetscFunctionReturn(0);
76a2744918SBarry Smith }
77a2744918SBarry Smith 
784a2ae208SSatish Balay #undef __FUNCT__
794a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
803acb8795SBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
816945ee14SBarry Smith {
82dfbe8321SBarry Smith   PetscErrorCode ierr;
836945ee14SBarry Smith 
843a40ed3dSBarry Smith   PetscFunctionBegin;
853a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
86bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
87606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
88ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
89bcd2baecSBarry Smith   }
903a40ed3dSBarry Smith   PetscFunctionReturn(0);
9117ab2063SBarry Smith }
9217ab2063SBarry Smith 
934a2ae208SSatish Balay #undef __FUNCT__
944a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
953acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
963b2fbd54SBarry Smith {
973b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
98dfbe8321SBarry Smith   PetscErrorCode ierr;
99d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
10097f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1013b2fbd54SBarry Smith 
1023a40ed3dSBarry Smith   PetscFunctionBegin;
103899cda47SBarry Smith   *nn = n;
1043a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1053b2fbd54SBarry Smith   if (symmetric) {
106d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1073b2fbd54SBarry Smith   } else {
10897f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11197f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1123b2fbd54SBarry Smith     jj = a->j;
1133b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
114bfeeae90SHong Zhang       collengths[jj[i]]++;
1153b2fbd54SBarry Smith     }
1163b2fbd54SBarry Smith     cia[0] = oshift;
1173b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1183b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1193b2fbd54SBarry Smith     }
12097f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1213b2fbd54SBarry Smith     jj   = a->j;
122a93ec695SBarry Smith     for (row=0; row<m; row++) {
123a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
124a93ec695SBarry Smith       for (i=0; i<mr; i++) {
125bfeeae90SHong Zhang         col = *jj++;
1263b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1273b2fbd54SBarry Smith       }
1283b2fbd54SBarry Smith     }
129606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1303b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1313b2fbd54SBarry Smith   }
1323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1333b2fbd54SBarry Smith }
1343b2fbd54SBarry Smith 
1354a2ae208SSatish Balay #undef __FUNCT__
1364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1373acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1383b2fbd54SBarry Smith {
139dfbe8321SBarry Smith   PetscErrorCode ierr;
140606d414cSSatish Balay 
1413a40ed3dSBarry Smith   PetscFunctionBegin;
1423a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1433b2fbd54SBarry Smith 
144606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
145606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1463b2fbd54SBarry Smith 
1473a40ed3dSBarry Smith   PetscFunctionReturn(0);
1483b2fbd54SBarry Smith }
1493b2fbd54SBarry Smith 
15087d4246cSBarry Smith #undef __FUNCT__
15187d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15287d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15387d4246cSBarry Smith {
15487d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15587d4246cSBarry Smith   PetscInt       *ai = a->i;
15687d4246cSBarry Smith   PetscErrorCode ierr;
15787d4246cSBarry Smith 
15887d4246cSBarry Smith   PetscFunctionBegin;
15987d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
16087d4246cSBarry Smith   PetscFunctionReturn(0);
16187d4246cSBarry Smith }
16287d4246cSBarry Smith 
163227d817aSBarry Smith #define CHUNKSIZE   15
16417ab2063SBarry Smith 
1654a2ae208SSatish Balay #undef __FUNCT__
1664a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16797f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16817ab2063SBarry Smith {
169416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
170e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
17197f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1726849ba73SBarry Smith   PetscErrorCode ierr;
173e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17454f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
175edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
176273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17717ab2063SBarry Smith 
1783a40ed3dSBarry Smith   PetscFunctionBegin;
17971fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
18017ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
181416022c9SBarry Smith     row  = im[k];
1825ef9f2a5SBarry Smith     if (row < 0) continue;
1832515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
184e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
1853b2fbd54SBarry Smith #endif
186bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18717ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
188416022c9SBarry Smith     low  = 0;
189c71e6ed7SBarry Smith     high = nrow;
19017ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1915ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1922515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
193e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
1943b2fbd54SBarry Smith #endif
195bfeeae90SHong Zhang       col = in[l];
19616371a99SBarry Smith       if (v) {
1974b0e389bSBarry Smith 	if (roworiented) {
1985ef9f2a5SBarry Smith 	  value = v[l + k*n];
199bef8e0ddSBarry Smith 	} else {
2004b0e389bSBarry Smith 	  value = v[k + l*m];
2014b0e389bSBarry Smith 	}
20216371a99SBarry Smith       } else {
20375567043SBarry Smith         value = 0.;
20416371a99SBarry Smith       }
205abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
20636db0b34SBarry Smith 
2077cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
208e2ee6c50SBarry Smith       lastcol = col;
209416022c9SBarry Smith       while (high-low > 5) {
210416022c9SBarry Smith         t = (low+high)/2;
211416022c9SBarry Smith         if (rp[t] > col) high = t;
212416022c9SBarry Smith         else             low  = t;
21317ab2063SBarry Smith       }
214416022c9SBarry Smith       for (i=low; i<high; i++) {
21517ab2063SBarry Smith         if (rp[i] > col) break;
21617ab2063SBarry Smith         if (rp[i] == col) {
217416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
21817ab2063SBarry Smith           else                  ap[i] = value;
219e44c0bd4SBarry Smith           low = i + 1;
22017ab2063SBarry Smith           goto noinsert;
22117ab2063SBarry Smith         }
22217ab2063SBarry Smith       }
223abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
224c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
225e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
226d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
227c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
228416022c9SBarry Smith       /* shift up all the later entries in this row */
229416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
23017ab2063SBarry Smith         rp[ii+1] = rp[ii];
23117ab2063SBarry Smith         ap[ii+1] = ap[ii];
23217ab2063SBarry Smith       }
23317ab2063SBarry Smith       rp[i] = col;
23417ab2063SBarry Smith       ap[i] = value;
235416022c9SBarry Smith       low   = i + 1;
236e44c0bd4SBarry Smith       noinsert:;
23717ab2063SBarry Smith     }
23817ab2063SBarry Smith     ailen[row] = nrow;
23917ab2063SBarry Smith   }
24088e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2413a40ed3dSBarry Smith   PetscFunctionReturn(0);
24217ab2063SBarry Smith }
24317ab2063SBarry Smith 
24481824310SBarry Smith 
2454a2ae208SSatish Balay #undef __FUNCT__
2464a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
247a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2487eb43aa7SLois Curfman McInnes {
2497eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
25097f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
25197f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
25254f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2537eb43aa7SLois Curfman McInnes 
2543a40ed3dSBarry Smith   PetscFunctionBegin;
2557eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2567eb43aa7SLois Curfman McInnes     row  = im[k];
257e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
258e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
259bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2607eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2617eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
262e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
263e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
264bfeeae90SHong Zhang       col = in[l] ;
2657eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2667eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2677eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2687eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2697eb43aa7SLois Curfman McInnes         else             low  = t;
2707eb43aa7SLois Curfman McInnes       }
2717eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2727eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2737eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
274b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2757eb43aa7SLois Curfman McInnes           goto finished;
2767eb43aa7SLois Curfman McInnes         }
2777eb43aa7SLois Curfman McInnes       }
27897e567efSBarry Smith       *v++ = 0.0;
2797eb43aa7SLois Curfman McInnes       finished:;
2807eb43aa7SLois Curfman McInnes     }
2817eb43aa7SLois Curfman McInnes   }
2823a40ed3dSBarry Smith   PetscFunctionReturn(0);
2837eb43aa7SLois Curfman McInnes }
2847eb43aa7SLois Curfman McInnes 
28517ab2063SBarry Smith 
2864a2ae208SSatish Balay #undef __FUNCT__
2874a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
288dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28917ab2063SBarry Smith {
290416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2916849ba73SBarry Smith   PetscErrorCode ierr;
2926f69ff64SBarry Smith   PetscInt       i,*col_lens;
2936f69ff64SBarry Smith   int            fd;
29417ab2063SBarry Smith 
2953a40ed3dSBarry Smith   PetscFunctionBegin;
296b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
297d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
2980700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
299d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
300d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
301416022c9SBarry Smith   col_lens[3] = a->nz;
302416022c9SBarry Smith 
303416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
304d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
305416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30617ab2063SBarry Smith   }
307d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
308606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
309416022c9SBarry Smith 
310416022c9SBarry Smith   /* store column indices (zero start index) */
3116f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
312416022c9SBarry Smith 
313416022c9SBarry Smith   /* store nonzero values */
3146f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3153a40ed3dSBarry Smith   PetscFunctionReturn(0);
31617ab2063SBarry Smith }
317416022c9SBarry Smith 
318dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
319cd155464SBarry Smith 
3204a2ae208SSatish Balay #undef __FUNCT__
3214a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
322dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
323416022c9SBarry Smith {
324416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
325dfbe8321SBarry Smith   PetscErrorCode    ierr;
326d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
327e060cb09SBarry Smith   const char        *name;
328f3ef73ceSBarry Smith   PetscViewerFormat format;
32917ab2063SBarry Smith 
3303a40ed3dSBarry Smith   PetscFunctionBegin;
331435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
332b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33371c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33497f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
335d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
336d00d2cf4SBarry Smith       nofinalvalue = 1;
337d00d2cf4SBarry Smith     }
338b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
339d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
34077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
34177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
342b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34317ab2063SBarry Smith 
34417ab2063SBarry Smith     for (i=0; i<m; i++) {
345416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
346aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34777431f27SBarry 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);
34817ab2063SBarry Smith #else
34977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
35017ab2063SBarry Smith #endif
35117ab2063SBarry Smith       }
35217ab2063SBarry Smith     }
353d00d2cf4SBarry Smith     if (nofinalvalue) {
354d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
355d00d2cf4SBarry Smith     }
356fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
357b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35868369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
359cd155464SBarry Smith      PetscFunctionReturn(0);
360fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
361b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36244cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36377431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36444cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
365aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36636db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
367a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36836db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
369a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
37036db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
371a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3726831982aSBarry Smith         }
37344cd7ae7SLois Curfman McInnes #else
374a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37544cd7ae7SLois Curfman McInnes #endif
37644cd7ae7SLois Curfman McInnes       }
377b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37844cd7ae7SLois Curfman McInnes     }
379b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
380fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
38197f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
382b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
38397f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
384496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
385496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
386496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
387496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
388aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
390496be53dSLois Curfman McInnes #else
391496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
392496be53dSLois Curfman McInnes #endif
393496be53dSLois Curfman McInnes         }
394496be53dSLois Curfman McInnes       }
395496be53dSLois Curfman McInnes     }
3962e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3982e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39977431f27SBarry 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);}
40077431f27SBarry 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);}
40177431f27SBarry 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);}
40277431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40377431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40477431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
405496be53dSLois Curfman McInnes     }
406b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
407606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
408496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
409496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
41077431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
411496be53dSLois Curfman McInnes       }
412b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
413496be53dSLois Curfman McInnes     }
414b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
415496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
416496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
417496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
418aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
420b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4216831982aSBarry Smith           }
422496be53dSLois Curfman McInnes #else
423b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
424496be53dSLois Curfman McInnes #endif
425496be53dSLois Curfman McInnes         }
426496be53dSLois Curfman McInnes       }
427b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
428496be53dSLois Curfman McInnes     }
429b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
430fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
43197f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
43287828ca2SBarry Smith     PetscScalar value;
43302594712SBarry Smith 
434b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43502594712SBarry Smith     for (i=0; i<m; i++) {
43602594712SBarry Smith       jcnt = 0;
437d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
438e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43902594712SBarry Smith           value = a->a[cnt++];
440e24b481bSBarry Smith           jcnt++;
44102594712SBarry Smith         } else {
44202594712SBarry Smith           value = 0.0;
44302594712SBarry Smith         }
444aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
445b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44602594712SBarry Smith #else
447b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44802594712SBarry Smith #endif
44902594712SBarry Smith       }
450b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45102594712SBarry Smith     }
452b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4533c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4543c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4553c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4563c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4573c215bfdSMatthew Knepley #else
4583c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4593c215bfdSMatthew Knepley #endif
460d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4613c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4623c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4633c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4643c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4653c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4663c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
4673c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G -%G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4683c215bfdSMatthew Knepley         } else {
4693c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4703c215bfdSMatthew Knepley         }
4713c215bfdSMatthew Knepley #else
4723c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4733c215bfdSMatthew Knepley #endif
4743c215bfdSMatthew Knepley       }
4753c215bfdSMatthew Knepley     }
4763c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4773a40ed3dSBarry Smith   } else {
478d6e78c31SSatish Balay     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
479d5f3da31SBarry Smith     if (A->factortype){
48016cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
48116cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
48216cd7e1dSShri Abhyankar         /* L part */
48316cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
48416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
48516cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
48616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48716cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
48816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48916cd7e1dSShri Abhyankar           } else {
49016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
49116cd7e1dSShri Abhyankar           }
49216cd7e1dSShri Abhyankar #else
49316cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49416cd7e1dSShri Abhyankar #endif
49516cd7e1dSShri Abhyankar         }
49616cd7e1dSShri Abhyankar 	/* diagonal */
49716cd7e1dSShri Abhyankar 	j = a->diag[i];
49816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
49916cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
50016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50116cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
50216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50316cd7e1dSShri Abhyankar           } else {
50416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
50516cd7e1dSShri Abhyankar           }
50616cd7e1dSShri Abhyankar #else
50716cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
50816cd7e1dSShri Abhyankar #endif
50916cd7e1dSShri Abhyankar 
51016cd7e1dSShri Abhyankar 	/* U part */
51116cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
51216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
51316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
51416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51516cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
51616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51716cd7e1dSShri Abhyankar           } else {
51816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
51916cd7e1dSShri Abhyankar           }
52016cd7e1dSShri Abhyankar #else
52116cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
52216cd7e1dSShri Abhyankar #endif
52316cd7e1dSShri Abhyankar }
52416cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
52516cd7e1dSShri Abhyankar         }
52616cd7e1dSShri Abhyankar     } else {
52717ab2063SBarry Smith       for (i=0; i<m; i++) {
52877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
529416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
530aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
53136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
532a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
53336db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
534a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5353a40ed3dSBarry Smith           } else {
536a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
53717ab2063SBarry Smith           }
53817ab2063SBarry Smith #else
539a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
54017ab2063SBarry Smith #endif
54117ab2063SBarry Smith         }
542b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
54317ab2063SBarry Smith       }
54416cd7e1dSShri Abhyankar     }
545b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
54617ab2063SBarry Smith   }
547b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5483a40ed3dSBarry Smith   PetscFunctionReturn(0);
549416022c9SBarry Smith }
550416022c9SBarry Smith 
5514a2ae208SSatish Balay #undef __FUNCT__
5524a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
553dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
554416022c9SBarry Smith {
555480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
556416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
557dfbe8321SBarry Smith   PetscErrorCode    ierr;
558d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
55936db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
560b0a32e0cSBarry Smith   PetscViewer       viewer;
561f3ef73ceSBarry Smith   PetscViewerFormat format;
562cddf8d76SBarry Smith 
5633a40ed3dSBarry Smith   PetscFunctionBegin;
564480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
565b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
56619bcc07fSBarry Smith 
567b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
568416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5690513a670SBarry Smith 
570fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5710513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
572b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
573416022c9SBarry Smith     for (i=0; i<m; i++) {
574cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
575bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
576bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
577aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
57836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
579cddf8d76SBarry Smith #else
580cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
581cddf8d76SBarry Smith #endif
582b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
583cddf8d76SBarry Smith       }
584cddf8d76SBarry Smith     }
585b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
586cddf8d76SBarry Smith     for (i=0; i<m; i++) {
587cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
588bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
589bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
590cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
591b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
592cddf8d76SBarry Smith       }
593cddf8d76SBarry Smith     }
594b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
595cddf8d76SBarry Smith     for (i=0; i<m; i++) {
596cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
597bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
598bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
599aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
60036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
601cddf8d76SBarry Smith #else
602cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
603cddf8d76SBarry Smith #endif
604b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
605416022c9SBarry Smith       }
606416022c9SBarry Smith     }
6070513a670SBarry Smith   } else {
6080513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
6090513a670SBarry Smith     /* first determine max of all nonzero values */
61097f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
611b0a32e0cSBarry Smith     PetscDraw   popup;
61236db0b34SBarry Smith     PetscReal scale;
6130513a670SBarry Smith 
6140513a670SBarry Smith     for (i=0; i<nz; i++) {
6150513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
6160513a670SBarry Smith     }
617b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
618b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
619b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
6200513a670SBarry Smith     count = 0;
6210513a670SBarry Smith     for (i=0; i<m; i++) {
6220513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
623bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
624bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
62597f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
626b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
6270513a670SBarry Smith         count++;
6280513a670SBarry Smith       }
6290513a670SBarry Smith     }
6300513a670SBarry Smith   }
631480ef9eaSBarry Smith   PetscFunctionReturn(0);
632480ef9eaSBarry Smith }
633cddf8d76SBarry Smith 
6344a2ae208SSatish Balay #undef __FUNCT__
6354a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
636dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
637480ef9eaSBarry Smith {
638dfbe8321SBarry Smith   PetscErrorCode ierr;
639b0a32e0cSBarry Smith   PetscDraw      draw;
64036db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
641480ef9eaSBarry Smith   PetscTruth     isnull;
642480ef9eaSBarry Smith 
643480ef9eaSBarry Smith   PetscFunctionBegin;
644b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
645b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
646480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
647480ef9eaSBarry Smith 
648480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
649d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
650480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
651b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
652b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
653480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6543a40ed3dSBarry Smith   PetscFunctionReturn(0);
655416022c9SBarry Smith }
656416022c9SBarry Smith 
6574a2ae208SSatish Balay #undef __FUNCT__
6584a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
659dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
660416022c9SBarry Smith {
661dfbe8321SBarry Smith   PetscErrorCode ierr;
6626805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
663416022c9SBarry Smith 
6643a40ed3dSBarry Smith   PetscFunctionBegin;
6652692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6662692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
6672692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
668c45a1595SBarry Smith   if (iascii) {
6693a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6700f5bd95cSBarry Smith   } else if (isbinary) {
6713a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6720f5bd95cSBarry Smith   } else if (isdraw) {
6733a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6745cd90555SBarry Smith   } else {
675e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
67617ab2063SBarry Smith   }
6774108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
6783a40ed3dSBarry Smith   PetscFunctionReturn(0);
67917ab2063SBarry Smith }
68019bcc07fSBarry Smith 
6814a2ae208SSatish Balay #undef __FUNCT__
6824a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
683dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
68417ab2063SBarry Smith {
685416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6866849ba73SBarry Smith   PetscErrorCode ierr;
68797f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
688d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
68954f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6903447b6efSHong Zhang   PetscReal      ratio=0.6;
69117ab2063SBarry Smith 
6923a40ed3dSBarry Smith   PetscFunctionBegin;
6933a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
69417ab2063SBarry Smith 
69543ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
69617ab2063SBarry Smith   for (i=1; i<m; i++) {
697416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
69817ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
69994a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
70017ab2063SBarry Smith     if (fshift) {
701bfeeae90SHong Zhang       ip = aj + ai[i] ;
702bfeeae90SHong Zhang       ap = aa + ai[i] ;
70317ab2063SBarry Smith       N  = ailen[i];
70417ab2063SBarry Smith       for (j=0; j<N; j++) {
70517ab2063SBarry Smith         ip[j-fshift] = ip[j];
70617ab2063SBarry Smith         ap[j-fshift] = ap[j];
70717ab2063SBarry Smith       }
70817ab2063SBarry Smith     }
70917ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
71017ab2063SBarry Smith   }
71117ab2063SBarry Smith   if (m) {
71217ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
71317ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
71417ab2063SBarry Smith   }
71517ab2063SBarry Smith   /* reset ilen and imax for each row */
71617ab2063SBarry Smith   for (i=0; i<m; i++) {
71717ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
71817ab2063SBarry Smith   }
719bfeeae90SHong Zhang   a->nz = ai[m];
72065e19b50SBarry Smith   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
72117ab2063SBarry Smith 
72209f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
723d0f46423SBarry 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);
724ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
725ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
726*8e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
727dd5f02e7SSatish Balay   a->reallocs          = 0;
7284e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
72936db0b34SBarry Smith   a->rmax              = rmax;
7304e220ebcSLois Curfman McInnes 
731cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
732317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
73388e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
73471c2f376SKris Buschelman 
7354108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
73671f1c65dSBarry Smith 
73771f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
7383a40ed3dSBarry Smith   PetscFunctionReturn(0);
73917ab2063SBarry Smith }
74017ab2063SBarry Smith 
7414a2ae208SSatish Balay #undef __FUNCT__
74299cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
74399cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
74499cafbc1SBarry Smith {
74599cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
74699cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
74754f21887SBarry Smith   MatScalar      *aa = a->a;
74899cafbc1SBarry Smith 
74999cafbc1SBarry Smith   PetscFunctionBegin;
75099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
75199cafbc1SBarry Smith   PetscFunctionReturn(0);
75299cafbc1SBarry Smith }
75399cafbc1SBarry Smith 
75499cafbc1SBarry Smith #undef __FUNCT__
75599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
75699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
75799cafbc1SBarry Smith {
75899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
75999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
76054f21887SBarry Smith   MatScalar      *aa = a->a;
76199cafbc1SBarry Smith 
76299cafbc1SBarry Smith   PetscFunctionBegin;
76399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
76499cafbc1SBarry Smith   PetscFunctionReturn(0);
76599cafbc1SBarry Smith }
76699cafbc1SBarry Smith 
76799cafbc1SBarry Smith #undef __FUNCT__
7684a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
769dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
77017ab2063SBarry Smith {
771416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
772dfbe8321SBarry Smith   PetscErrorCode ierr;
7733a40ed3dSBarry Smith 
7743a40ed3dSBarry Smith   PetscFunctionBegin;
775d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7763a40ed3dSBarry Smith   PetscFunctionReturn(0);
77717ab2063SBarry Smith }
778416022c9SBarry Smith 
7794a2ae208SSatish Balay #undef __FUNCT__
7804a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
781dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
78217ab2063SBarry Smith {
783416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
784dfbe8321SBarry Smith   PetscErrorCode ierr;
785d5d45c9bSBarry Smith 
7863a40ed3dSBarry Smith   PetscFunctionBegin;
787aa482453SBarry Smith #if defined(PETSC_USE_LOG)
788d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
78917ab2063SBarry Smith #endif
790e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
791c38d4ed2SBarry Smith   if (a->row) {
792c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
793c38d4ed2SBarry Smith   }
794c38d4ed2SBarry Smith   if (a->col) {
795c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
796c38d4ed2SBarry Smith   }
79705b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
79805b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
79971f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
80005b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
80182bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
80205b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
803cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
80405b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
805407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
8060e83c824SBarry Smith   if (a->compressedrow.checked && a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);}
807a30b2313SHong Zhang 
8084108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
8094846f1f5SKris Buschelman 
810606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
811901853e0SKris Buschelman 
812dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
813901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
814901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
815901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
816901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
817901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
818ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
819901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
820901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
821a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
822901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
8233a40ed3dSBarry Smith   PetscFunctionReturn(0);
82417ab2063SBarry Smith }
82517ab2063SBarry Smith 
8264a2ae208SSatish Balay #undef __FUNCT__
8274a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
8284e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
82917ab2063SBarry Smith {
830416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8314846f1f5SKris Buschelman   PetscErrorCode ierr;
8323a40ed3dSBarry Smith 
8333a40ed3dSBarry Smith   PetscFunctionBegin;
834a65d3064SKris Buschelman   switch (op) {
835a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
8364e0d8c25SBarry Smith       a->roworiented       = flg;
837a65d3064SKris Buschelman       break;
838a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
839a9817697SBarry Smith       a->keepnonzeropattern    = flg;
840a65d3064SKris Buschelman       break;
841512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
842512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
843a65d3064SKris Buschelman       break;
844a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
8454e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
846a65d3064SKris Buschelman       break;
847a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8484e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
849a65d3064SKris Buschelman       break;
85028b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
85128b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
85228b2fa4aSMatthew Knepley       break;
853a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8544e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8550df259c2SBarry Smith       break;
856d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8574e0d8c25SBarry Smith       a->compressedrow.use = flg;
858d487561eSHong Zhang       break;
859b1646e73SJed Brown     case MAT_SYMMETRIC:
860b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
861b1646e73SJed Brown     case MAT_HERMITIAN:
862b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
8634e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
864a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
865a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
866290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
867a65d3064SKris Buschelman       break;
868b87ac2d8SJed Brown     case MAT_USE_INODES:
869b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
870b87ac2d8SJed Brown       break;
871a65d3064SKris Buschelman     default:
872e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
873a65d3064SKris Buschelman   }
8744108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
8753a40ed3dSBarry Smith   PetscFunctionReturn(0);
87617ab2063SBarry Smith }
87717ab2063SBarry Smith 
8784a2ae208SSatish Balay #undef __FUNCT__
8794a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
880dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
88117ab2063SBarry Smith {
882416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8836849ba73SBarry Smith   PetscErrorCode ierr;
884d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
88535e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
88617ab2063SBarry Smith 
8873a40ed3dSBarry Smith   PetscFunctionBegin;
888d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
889e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
89035e7444dSHong Zhang 
891d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
892d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
89335e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
89435e7444dSHong Zhang     for (i=0; i<n; i++) x[i] = aa[diag[i]];
89535e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
89635e7444dSHong Zhang     PetscFunctionReturn(0);
89735e7444dSHong Zhang   }
89835e7444dSHong Zhang 
8992dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
9001ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
90135e7444dSHong Zhang   for (i=0; i<n; i++) {
90235e7444dSHong Zhang     nz = ai[i+1] - ai[i];
9032f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
90435e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
90535e7444dSHong Zhang       if (aj[j] == i) {
90635e7444dSHong Zhang         x[i] = aa[j];
90717ab2063SBarry Smith         break;
90817ab2063SBarry Smith       }
90917ab2063SBarry Smith     }
91017ab2063SBarry Smith   }
9111ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
9123a40ed3dSBarry Smith   PetscFunctionReturn(0);
91317ab2063SBarry Smith }
91417ab2063SBarry Smith 
915b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9164a2ae208SSatish Balay #undef __FUNCT__
9174a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
918dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
91917ab2063SBarry Smith {
920416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
9215c897100SBarry Smith   PetscScalar       *x,*y;
922dfbe8321SBarry Smith   PetscErrorCode    ierr;
923d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
9245c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
925a77337e4SBarry Smith   MatScalar         *v;
926a77337e4SBarry Smith   PetscScalar       alpha;
92704fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
9283447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
9294eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
9305c897100SBarry Smith #endif
93117ab2063SBarry Smith 
9323a40ed3dSBarry Smith   PetscFunctionBegin;
9332e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
9341ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9351ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9365c897100SBarry Smith 
9375c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
938bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
9395c897100SBarry Smith #else
9403447b6efSHong Zhang   if (usecprow){
9413447b6efSHong Zhang     m    = cprow.nrows;
9423447b6efSHong Zhang     ii   = cprow.i;
9437b2bb3b9SHong Zhang     ridx = cprow.rindex;
9443447b6efSHong Zhang   } else {
9453447b6efSHong Zhang     ii = a->i;
9463447b6efSHong Zhang   }
94717ab2063SBarry Smith   for (i=0; i<m; i++) {
9483447b6efSHong Zhang     idx   = a->j + ii[i] ;
9493447b6efSHong Zhang     v     = a->a + ii[i] ;
9503447b6efSHong Zhang     n     = ii[i+1] - ii[i];
9513447b6efSHong Zhang     if (usecprow){
9527b2bb3b9SHong Zhang       alpha = x[ridx[i]];
9533447b6efSHong Zhang     } else {
95417ab2063SBarry Smith       alpha = x[i];
9553447b6efSHong Zhang     }
95604fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
95717ab2063SBarry Smith   }
9585c897100SBarry Smith #endif
959dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
9601ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9611ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9623a40ed3dSBarry Smith   PetscFunctionReturn(0);
96317ab2063SBarry Smith }
96417ab2063SBarry Smith 
9654a2ae208SSatish Balay #undef __FUNCT__
9665c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
967dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9685c897100SBarry Smith {
969dfbe8321SBarry Smith   PetscErrorCode ierr;
9705c897100SBarry Smith 
9715c897100SBarry Smith   PetscFunctionBegin;
972170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9735c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9745c897100SBarry Smith   PetscFunctionReturn(0);
9755c897100SBarry Smith }
9765c897100SBarry Smith 
977a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9785c897100SBarry Smith #undef __FUNCT__
9794a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
980dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
98117ab2063SBarry Smith {
982416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
983d9fead3dSBarry Smith   PetscScalar       *y;
98454f21887SBarry Smith   const PetscScalar *x;
98554f21887SBarry Smith   const MatScalar   *aa;
986dfbe8321SBarry Smith   PetscErrorCode    ierr;
987003131ecSBarry Smith   PetscInt          m=A->rmap->n;
988003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9898aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
990362ced78SSatish Balay   PetscScalar       sum;
99197952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
99217ab2063SBarry Smith 
993b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
99497952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
995fee21e36SBarry Smith #endif
996fee21e36SBarry Smith 
9973a40ed3dSBarry Smith   PetscFunctionBegin;
998d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9991ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
100097952fefSHong Zhang   aj  = a->j;
100197952fefSHong Zhang   aa  = a->a;
1002416022c9SBarry Smith   ii  = a->i;
10034eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
100497952fefSHong Zhang     m    = a->compressedrow.nrows;
100597952fefSHong Zhang     ii   = a->compressedrow.i;
100697952fefSHong Zhang     ridx = a->compressedrow.rindex;
100797952fefSHong Zhang     for (i=0; i<m; i++){
100897952fefSHong Zhang       n   = ii[i+1] - ii[i];
100997952fefSHong Zhang       aj  = a->j + ii[i];
101097952fefSHong Zhang       aa  = a->a + ii[i];
101197952fefSHong Zhang       sum = 0.0;
1012a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1013003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1014003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
101597952fefSHong Zhang       y[*ridx++] = sum;
101697952fefSHong Zhang     }
101797952fefSHong Zhang   } else { /* do not use compressed row format */
1018b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1019b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1020b05257ddSBarry Smith #else
102117ab2063SBarry Smith     for (i=0; i<m; i++) {
1022003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1023003131ecSBarry Smith       aj  = a->j + ii[i];
1024003131ecSBarry Smith       aa  = a->a + ii[i];
102517ab2063SBarry Smith       sum  = 0.0;
1026a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1027003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
102817ab2063SBarry Smith       y[i] = sum;
102917ab2063SBarry Smith     }
10308d195f9aSBarry Smith #endif
1031b05257ddSBarry Smith   }
1032dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1033d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
10341ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10353a40ed3dSBarry Smith   PetscFunctionReturn(0);
103617ab2063SBarry Smith }
103717ab2063SBarry Smith 
1038a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
10394a2ae208SSatish Balay #undef __FUNCT__
10404a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1041dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
104217ab2063SBarry Smith {
1043416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
104454f21887SBarry Smith   PetscScalar     *x,*y,*z;
104554f21887SBarry Smith   const MatScalar *aa;
1046dfbe8321SBarry Smith   PetscErrorCode  ierr;
1047d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1048aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
104997952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1050362ced78SSatish Balay   PetscScalar     sum;
105197952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
1052e36a17ebSSatish Balay #endif
10539ea0dfa2SSatish Balay 
10543a40ed3dSBarry Smith   PetscFunctionBegin;
10551ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10561ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10572e8a6d31SBarry Smith   if (zz != yy) {
10581ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
10592e8a6d31SBarry Smith   } else {
10602e8a6d31SBarry Smith     z = y;
10612e8a6d31SBarry Smith   }
1062bfeeae90SHong Zhang 
106397952fefSHong Zhang   aj  = a->j;
106497952fefSHong Zhang   aa  = a->a;
1065cddf8d76SBarry Smith   ii  = a->i;
1066aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
106797952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
106802ab625aSSatish Balay #else
10694eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10704eb6d288SHong Zhang     if (zz != yy){
10714eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10724eb6d288SHong Zhang     }
107397952fefSHong Zhang     m    = a->compressedrow.nrows;
107497952fefSHong Zhang     ii   = a->compressedrow.i;
107597952fefSHong Zhang     ridx = a->compressedrow.rindex;
107697952fefSHong Zhang     for (i=0; i<m; i++){
107797952fefSHong Zhang       n  = ii[i+1] - ii[i];
107897952fefSHong Zhang       aj  = a->j + ii[i];
107997952fefSHong Zhang       aa  = a->a + ii[i];
108097952fefSHong Zhang       sum = y[*ridx];
108197952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
108297952fefSHong Zhang       z[*ridx++] = sum;
108397952fefSHong Zhang     }
108497952fefSHong Zhang   } else { /* do not use compressed row format */
108517ab2063SBarry Smith     for (i=0; i<m; i++) {
10869ea0dfa2SSatish Balay       jrow = ii[i];
10879ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
108817ab2063SBarry Smith       sum  = y[i];
10899ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
109097952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10919ea0dfa2SSatish Balay       }
109217ab2063SBarry Smith       z[i] = sum;
109317ab2063SBarry Smith     }
109497952fefSHong Zhang   }
109502ab625aSSatish Balay #endif
1096dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10971ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10981ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10992e8a6d31SBarry Smith   if (zz != yy) {
11001ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
11012e8a6d31SBarry Smith   }
11023a40ed3dSBarry Smith   PetscFunctionReturn(0);
110317ab2063SBarry Smith }
110417ab2063SBarry Smith 
110517ab2063SBarry Smith /*
110617ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
110717ab2063SBarry Smith */
11084a2ae208SSatish Balay #undef __FUNCT__
11094a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1110dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
111117ab2063SBarry Smith {
1112416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11136849ba73SBarry Smith   PetscErrorCode ierr;
1114d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
111517ab2063SBarry Smith 
11163a40ed3dSBarry Smith   PetscFunctionBegin;
111709f38230SBarry Smith   if (!a->diag) {
111809f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
11199518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
112009f38230SBarry Smith   }
1121d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
112209f38230SBarry Smith     a->diag[i] = a->i[i+1];
1123bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1124bfeeae90SHong Zhang       if (a->j[j] == i) {
112509f38230SBarry Smith         a->diag[i] = j;
112617ab2063SBarry Smith         break;
112717ab2063SBarry Smith       }
112817ab2063SBarry Smith     }
112917ab2063SBarry Smith   }
11303a40ed3dSBarry Smith   PetscFunctionReturn(0);
113117ab2063SBarry Smith }
113217ab2063SBarry Smith 
1133be5855fcSBarry Smith /*
1134be5855fcSBarry Smith      Checks for missing diagonals
1135be5855fcSBarry Smith */
11364a2ae208SSatish Balay #undef __FUNCT__
11374a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
113809f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1139be5855fcSBarry Smith {
1140be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
114197f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1142be5855fcSBarry Smith 
1143be5855fcSBarry Smith   PetscFunctionBegin;
114409f38230SBarry Smith   *missing = PETSC_FALSE;
1145d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
114609f38230SBarry Smith     *missing  = PETSC_TRUE;
114709f38230SBarry Smith     if (d) *d = 0;
114809f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
114909f38230SBarry Smith   } else {
1150f1e2ffcdSBarry Smith     diag = a->diag;
1151d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1152bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
115309f38230SBarry Smith 	*missing = PETSC_TRUE;
115409f38230SBarry Smith 	if (d) *d = i;
115509f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
115609f38230SBarry Smith       }
1157be5855fcSBarry Smith     }
1158be5855fcSBarry Smith   }
1159be5855fcSBarry Smith   PetscFunctionReturn(0);
1160be5855fcSBarry Smith }
1161be5855fcSBarry Smith 
116271f1c65dSBarry Smith EXTERN_C_BEGIN
116371f1c65dSBarry Smith #undef __FUNCT__
116471f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
116571f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
116671f1c65dSBarry Smith {
116771f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
116871f1c65dSBarry Smith   PetscErrorCode ierr;
1169d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
117054f21887SBarry Smith   MatScalar      *v = a->a;
117154f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
117271f1c65dSBarry Smith 
117371f1c65dSBarry Smith   PetscFunctionBegin;
117471f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
117571f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
117671f1c65dSBarry Smith   diag = a->diag;
117771f1c65dSBarry Smith   if (!a->idiag) {
117871f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
117971f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
118071f1c65dSBarry Smith     v        = a->a;
118171f1c65dSBarry Smith   }
118271f1c65dSBarry Smith   mdiag = a->mdiag;
118371f1c65dSBarry Smith   idiag = a->idiag;
118471f1c65dSBarry Smith 
1185028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
118671f1c65dSBarry Smith     for (i=0; i<m; i++) {
118771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1188e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
118971f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
119071f1c65dSBarry Smith     }
119171f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
119271f1c65dSBarry Smith   } else {
119371f1c65dSBarry Smith     for (i=0; i<m; i++) {
119471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
119571f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
119671f1c65dSBarry Smith     }
1197dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
119871f1c65dSBarry Smith   }
119971f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
120071f1c65dSBarry Smith   PetscFunctionReturn(0);
120171f1c65dSBarry Smith }
12025a9745a3SMatthew Knepley EXTERN_C_END
120371f1c65dSBarry Smith 
1204a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
12054a2ae208SSatish Balay #undef __FUNCT__
120641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
120741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
120817ab2063SBarry Smith {
1209416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1210e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1211e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
121254f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1213dfbe8321SBarry Smith   PetscErrorCode     ierr;
1214d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
121597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
121617ab2063SBarry Smith 
12173a40ed3dSBarry Smith   PetscFunctionBegin;
1218b965ef7fSBarry Smith   its = its*lits;
121991723122SBarry Smith 
122071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
122171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
122271f1c65dSBarry Smith   a->fshift = fshift;
122371f1c65dSBarry Smith   a->omega  = omega;
1224ed480e8bSBarry Smith 
122571f1c65dSBarry Smith   diag = a->diag;
122671f1c65dSBarry Smith   t     = a->ssor_work;
1227ed480e8bSBarry Smith   idiag = a->idiag;
122871f1c65dSBarry Smith   mdiag = a->mdiag;
1229ed480e8bSBarry Smith 
12301ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1231fb2e594dSBarry Smith   if (xx != bb) {
12321ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1233fb2e594dSBarry Smith   } else {
1234fb2e594dSBarry Smith     b = x;
1235fb2e594dSBarry Smith   }
123671f1c65dSBarry Smith   CHKMEMQ;
1237ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
123817ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
123917ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1240ed480e8bSBarry Smith     bs = b;
124117ab2063SBarry Smith     for (i=0; i<m; i++) {
124271f1c65dSBarry Smith         d    = fshift + mdiag[i];
1243416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1244ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1245ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
124617ab2063SBarry Smith         sum  = b[i]*d/omega;
1247003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
124817ab2063SBarry Smith         x[i] = sum;
124917ab2063SBarry Smith     }
12501ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12511ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1252efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12533a40ed3dSBarry Smith     PetscFunctionReturn(0);
125417ab2063SBarry Smith   }
1255c783ea89SBarry Smith 
125648af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1257e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
12583a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
125917ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1260887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
126117ab2063SBarry Smith 
126217ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
126317ab2063SBarry Smith 
1264887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
126517ab2063SBarry Smith     */
126617ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
126717ab2063SBarry Smith 
126817ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
126917ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1270416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1271ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1272ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
127317ab2063SBarry Smith       sum  = b[i];
1274e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1275ed480e8bSBarry Smith       x[i] = sum*idiag[i];
127617ab2063SBarry Smith     }
127717ab2063SBarry Smith 
127817ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1279416022c9SBarry Smith     v = a->a;
1280ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
128117ab2063SBarry Smith 
128217ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1283ed480e8bSBarry Smith     ts = t;
1284416022c9SBarry Smith     diag = a->diag;
128517ab2063SBarry Smith     for (i=0; i<m; i++) {
1286416022c9SBarry Smith       n    = diag[i] - a->i[i];
1287ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1288ed480e8bSBarry Smith       v    = a->a + a->i[i];
128917ab2063SBarry Smith       sum  = t[i];
1290003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1291ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1292733d66baSBarry Smith       /*  x = x + t */
1293733d66baSBarry Smith       x[i] += t[i];
129417ab2063SBarry Smith     }
129517ab2063SBarry Smith 
1296dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
12971ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12981ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12993a40ed3dSBarry Smith     PetscFunctionReturn(0);
130017ab2063SBarry Smith   }
130117ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
130217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
130317ab2063SBarry Smith       for (i=0; i<m; i++) {
1304416022c9SBarry Smith         n    = diag[i] - a->i[i];
1305ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1306ed480e8bSBarry Smith         v    = a->a + a->i[i];
130717ab2063SBarry Smith         sum  = b[i];
1308e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13095c99c7daSBarry Smith         t[i] = sum;
1310ed480e8bSBarry Smith         x[i] = sum*idiag[i];
131117ab2063SBarry Smith       }
13125c99c7daSBarry Smith       xb = t;
1313efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13143a40ed3dSBarry Smith     } else xb = b;
131517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
131617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1317416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1318ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1319ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
132017ab2063SBarry Smith         sum  = xb[i];
1321e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13225c99c7daSBarry Smith         if (xb == b) {
1323ed480e8bSBarry Smith           x[i] = sum*idiag[i];
13245c99c7daSBarry Smith         } else {
13255c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
132617ab2063SBarry Smith         }
13275c99c7daSBarry Smith       }
1328efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
132917ab2063SBarry Smith     }
133017ab2063SBarry Smith     its--;
133117ab2063SBarry Smith   }
133217ab2063SBarry Smith   while (its--) {
133317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
133417ab2063SBarry Smith       for (i=0; i<m; i++) {
1335416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1336ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1337ed480e8bSBarry Smith         v    = a->a + a->i[i];
133817ab2063SBarry Smith         sum  = b[i];
1339e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1340ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
134117ab2063SBarry Smith       }
13429f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
134317ab2063SBarry Smith     }
134417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
134517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1346416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1347ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1348ed480e8bSBarry Smith         v    = a->a + a->i[i];
134917ab2063SBarry Smith         sum  = b[i];
1350e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1351ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
135217ab2063SBarry Smith       }
13539f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
135417ab2063SBarry Smith     }
135517ab2063SBarry Smith   }
13561ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13571ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
135871f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
135917ab2063SBarry Smith }
136017ab2063SBarry Smith 
13612af78befSBarry Smith 
13624a2ae208SSatish Balay #undef __FUNCT__
13634a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1364dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
136517ab2063SBarry Smith {
1366416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13674e220ebcSLois Curfman McInnes 
13683a40ed3dSBarry Smith   PetscFunctionBegin;
13694e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13704e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13714e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13724e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13734e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
1374*8e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
13757adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1376d5f3da31SBarry Smith   if (A->factortype) {
13774e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13784e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13794e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13804e220ebcSLois Curfman McInnes   } else {
13814e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13824e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13834e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13844e220ebcSLois Curfman McInnes   }
13853a40ed3dSBarry Smith   PetscFunctionReturn(0);
138617ab2063SBarry Smith }
138717ab2063SBarry Smith 
13884a2ae208SSatish Balay #undef __FUNCT__
13894a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1390f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
139117ab2063SBarry Smith {
1392416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
13933b98c0a2SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d = 0;
13946849ba73SBarry Smith   PetscErrorCode ierr;
139509f38230SBarry Smith   PetscTruth     missing;
139617ab2063SBarry Smith 
13973a40ed3dSBarry Smith   PetscFunctionBegin;
1398a9817697SBarry Smith   if (a->keepnonzeropattern) {
1399f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1400e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1401bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1402f1e2ffcdSBarry Smith     }
1403f4df32b1SMatthew Knepley     if (diag != 0.0) {
140409f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1405e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1406f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1407f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1408f1e2ffcdSBarry Smith       }
1409f1e2ffcdSBarry Smith     }
141088e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1411f1e2ffcdSBarry Smith   } else {
1412f4df32b1SMatthew Knepley     if (diag != 0.0) {
141317ab2063SBarry Smith       for (i=0; i<N; i++) {
1414e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
14157ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1416416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1417f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1418bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
14197ae801bdSBarry Smith         } else { /* in case row was completely empty */
1420f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
142117ab2063SBarry Smith         }
142217ab2063SBarry Smith       }
14233a40ed3dSBarry Smith     } else {
142417ab2063SBarry Smith       for (i=0; i<N; i++) {
1425e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1426416022c9SBarry Smith         a->ilen[rows[i]] = 0;
142717ab2063SBarry Smith       }
142817ab2063SBarry Smith     }
142988e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1430f1e2ffcdSBarry Smith   }
143143a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14323a40ed3dSBarry Smith   PetscFunctionReturn(0);
143317ab2063SBarry Smith }
143417ab2063SBarry Smith 
14354a2ae208SSatish Balay #undef __FUNCT__
14364a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1437a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
143817ab2063SBarry Smith {
1439416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
144097f1f81fSBarry Smith   PetscInt   *itmp;
144117ab2063SBarry Smith 
14423a40ed3dSBarry Smith   PetscFunctionBegin;
1443e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
144417ab2063SBarry Smith 
1445416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1446bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
144717ab2063SBarry Smith   if (idx) {
1448bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1449bfeeae90SHong Zhang     if (*nz) {
14504e093b46SBarry Smith       *idx = itmp;
145117ab2063SBarry Smith     }
145217ab2063SBarry Smith     else *idx = 0;
145317ab2063SBarry Smith   }
14543a40ed3dSBarry Smith   PetscFunctionReturn(0);
145517ab2063SBarry Smith }
145617ab2063SBarry Smith 
1457bfeeae90SHong Zhang /* remove this function? */
14584a2ae208SSatish Balay #undef __FUNCT__
14594a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1460a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
146117ab2063SBarry Smith {
14623a40ed3dSBarry Smith   PetscFunctionBegin;
14633a40ed3dSBarry Smith   PetscFunctionReturn(0);
146417ab2063SBarry Smith }
146517ab2063SBarry Smith 
14664a2ae208SSatish Balay #undef __FUNCT__
14674a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1468dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
146917ab2063SBarry Smith {
1470416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
147154f21887SBarry Smith   MatScalar      *v = a->a;
147236db0b34SBarry Smith   PetscReal      sum = 0.0;
14736849ba73SBarry Smith   PetscErrorCode ierr;
147497f1f81fSBarry Smith   PetscInt       i,j;
147517ab2063SBarry Smith 
14763a40ed3dSBarry Smith   PetscFunctionBegin;
147717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1478416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1479aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
148036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
148117ab2063SBarry Smith #else
148217ab2063SBarry Smith       sum += (*v)*(*v); v++;
148317ab2063SBarry Smith #endif
148417ab2063SBarry Smith     }
1485064f8208SBarry Smith     *nrm = sqrt(sum);
14863a40ed3dSBarry Smith   } else if (type == NORM_1) {
148736db0b34SBarry Smith     PetscReal *tmp;
148897f1f81fSBarry Smith     PetscInt    *jj = a->j;
1489d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1490d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1491064f8208SBarry Smith     *nrm = 0.0;
1492416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1493bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
149417ab2063SBarry Smith     }
1495d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1496064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
149717ab2063SBarry Smith     }
1498606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14993a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1500064f8208SBarry Smith     *nrm = 0.0;
1501d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1502bfeeae90SHong Zhang       v = a->a + a->i[j];
150317ab2063SBarry Smith       sum = 0.0;
1504416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1505cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
150617ab2063SBarry Smith       }
1507064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
150817ab2063SBarry Smith     }
15093a40ed3dSBarry Smith   } else {
1510e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
151117ab2063SBarry Smith   }
15123a40ed3dSBarry Smith   PetscFunctionReturn(0);
151317ab2063SBarry Smith }
151417ab2063SBarry Smith 
15154a2ae208SSatish Balay #undef __FUNCT__
15164a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1517fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
151817ab2063SBarry Smith {
1519416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1520416022c9SBarry Smith   Mat            C;
15216849ba73SBarry Smith   PetscErrorCode ierr;
1522d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
152354f21887SBarry Smith   MatScalar      *array = a->a;
152417ab2063SBarry Smith 
15253a40ed3dSBarry Smith   PetscFunctionBegin;
1526e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1527fc4dec0aSBarry Smith 
1528fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1529d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1530d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1531bfeeae90SHong Zhang 
1532bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
15337adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1534d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
15357adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1536ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1537606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1538a541d17aSBarry Smith   } else {
1539a541d17aSBarry Smith     C = *B;
1540a541d17aSBarry Smith   }
1541a541d17aSBarry Smith 
154217ab2063SBarry Smith   for (i=0; i<m; i++) {
154317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
154487d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1545b9b97703SBarry Smith     array += len;
1546b9b97703SBarry Smith     aj    += len;
154717ab2063SBarry Smith   }
15486d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15496d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
155017ab2063SBarry Smith 
1551815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1552416022c9SBarry Smith     *B = C;
155317ab2063SBarry Smith   } else {
1554273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
155517ab2063SBarry Smith   }
15563a40ed3dSBarry Smith   PetscFunctionReturn(0);
155717ab2063SBarry Smith }
155817ab2063SBarry Smith 
1559cd0d46ebSvictorle EXTERN_C_BEGIN
1560cd0d46ebSvictorle #undef __FUNCT__
15615fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1562be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1563cd0d46ebSvictorle {
1564cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
156554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
156654f21887SBarry Smith   MatScalar      *va,*vb;
15676849ba73SBarry Smith   PetscErrorCode ierr;
156897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1569cd0d46ebSvictorle 
1570cd0d46ebSvictorle   PetscFunctionBegin;
1571cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1572cd0d46ebSvictorle 
1573cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1574cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15755485867bSBarry Smith   if (ma!=nb || na!=mb){
15765485867bSBarry Smith     *f = PETSC_FALSE;
15775485867bSBarry Smith     PetscFunctionReturn(0);
15785485867bSBarry Smith   }
1579cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1580cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1581cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
158297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
158397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1584cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1585cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1586cd0d46ebSvictorle 
1587cd0d46ebSvictorle   *f = PETSC_TRUE;
1588cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1589cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
159097f1f81fSBarry Smith       PetscInt         idc,idr;
15915485867bSBarry Smith       PetscScalar vc,vr;
1592cd0d46ebSvictorle       /* column/row index/value */
15935485867bSBarry Smith       idc = adx[aptr[i]];
15945485867bSBarry Smith       idr = bdx[bptr[idc]];
15955485867bSBarry Smith       vc  = va[aptr[i]];
15965485867bSBarry Smith       vr  = vb[bptr[idc]];
15975485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15985485867bSBarry Smith 	*f = PETSC_FALSE;
15995485867bSBarry Smith         goto done;
1600cd0d46ebSvictorle       } else {
16015485867bSBarry Smith 	aptr[i]++;
16025485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1603cd0d46ebSvictorle       }
1604cd0d46ebSvictorle     }
1605cd0d46ebSvictorle   }
1606cd0d46ebSvictorle  done:
1607cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
16083aeef889SHong Zhang   if (B) {
16093aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
16103aeef889SHong Zhang   }
1611cd0d46ebSvictorle   PetscFunctionReturn(0);
1612cd0d46ebSvictorle }
1613cd0d46ebSvictorle EXTERN_C_END
1614cd0d46ebSvictorle 
16151cbb95d3SBarry Smith EXTERN_C_BEGIN
16161cbb95d3SBarry Smith #undef __FUNCT__
16171cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
16181cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
16191cbb95d3SBarry Smith {
16201cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
162154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
162254f21887SBarry Smith   MatScalar      *va,*vb;
16231cbb95d3SBarry Smith   PetscErrorCode ierr;
16241cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
16251cbb95d3SBarry Smith 
16261cbb95d3SBarry Smith   PetscFunctionBegin;
16271cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
16281cbb95d3SBarry Smith 
16291cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
16301cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
16311cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
16321cbb95d3SBarry Smith     *f = PETSC_FALSE;
16331cbb95d3SBarry Smith     PetscFunctionReturn(0);
16341cbb95d3SBarry Smith   }
16351cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
16361cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
16371cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
16381cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
16391cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
16401cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
16411cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
16421cbb95d3SBarry Smith 
16431cbb95d3SBarry Smith   *f = PETSC_TRUE;
16441cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
16451cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
16461cbb95d3SBarry Smith       PetscInt         idc,idr;
16471cbb95d3SBarry Smith       PetscScalar vc,vr;
16481cbb95d3SBarry Smith       /* column/row index/value */
16491cbb95d3SBarry Smith       idc = adx[aptr[i]];
16501cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16511cbb95d3SBarry Smith       vc  = va[aptr[i]];
16521cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16531cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16541cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16551cbb95d3SBarry Smith         goto done;
16561cbb95d3SBarry Smith       } else {
16571cbb95d3SBarry Smith 	aptr[i]++;
16581cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16591cbb95d3SBarry Smith       }
16601cbb95d3SBarry Smith     }
16611cbb95d3SBarry Smith   }
16621cbb95d3SBarry Smith  done:
16631cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16641cbb95d3SBarry Smith   if (B) {
16651cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16661cbb95d3SBarry Smith   }
16671cbb95d3SBarry Smith   PetscFunctionReturn(0);
16681cbb95d3SBarry Smith }
16691cbb95d3SBarry Smith EXTERN_C_END
16701cbb95d3SBarry Smith 
16719e29f15eSvictorle #undef __FUNCT__
16729e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1673dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16749e29f15eSvictorle {
1675dfbe8321SBarry Smith   PetscErrorCode ierr;
16769e29f15eSvictorle   PetscFunctionBegin;
16775485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16789e29f15eSvictorle   PetscFunctionReturn(0);
16799e29f15eSvictorle }
16809e29f15eSvictorle 
16814a2ae208SSatish Balay #undef __FUNCT__
16821cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16831cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16841cbb95d3SBarry Smith {
16851cbb95d3SBarry Smith   PetscErrorCode ierr;
16861cbb95d3SBarry Smith   PetscFunctionBegin;
16871cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16881cbb95d3SBarry Smith   PetscFunctionReturn(0);
16891cbb95d3SBarry Smith }
16901cbb95d3SBarry Smith 
16911cbb95d3SBarry Smith #undef __FUNCT__
16924a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1693dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
169417ab2063SBarry Smith {
1695416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
169654f21887SBarry Smith   PetscScalar    *l,*r,x;
169754f21887SBarry Smith   MatScalar      *v;
1698dfbe8321SBarry Smith   PetscErrorCode ierr;
1699d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
170017ab2063SBarry Smith 
17013a40ed3dSBarry Smith   PetscFunctionBegin;
170217ab2063SBarry Smith   if (ll) {
17033ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
17043ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1705e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1706e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
17071ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1708416022c9SBarry Smith     v = a->a;
170917ab2063SBarry Smith     for (i=0; i<m; i++) {
171017ab2063SBarry Smith       x = l[i];
1711416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
171217ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
171317ab2063SBarry Smith     }
17141ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1715efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
171617ab2063SBarry Smith   }
171717ab2063SBarry Smith   if (rr) {
1718e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1719e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
17201ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1721416022c9SBarry Smith     v = a->a; jj = a->j;
172217ab2063SBarry Smith     for (i=0; i<nz; i++) {
1723bfeeae90SHong Zhang       (*v++) *= r[*jj++];
172417ab2063SBarry Smith     }
17251ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1726efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
172717ab2063SBarry Smith   }
17283a40ed3dSBarry Smith   PetscFunctionReturn(0);
172917ab2063SBarry Smith }
173017ab2063SBarry Smith 
17314a2ae208SSatish Balay #undef __FUNCT__
17324a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
173397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
173417ab2063SBarry Smith {
1735db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
17366849ba73SBarry Smith   PetscErrorCode ierr;
1737d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
173897f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
17395d0c19d7SBarry Smith   const PetscInt *irow,*icol;
17405d0c19d7SBarry Smith   PetscInt       nrows,ncols;
174197f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
174254f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1743416022c9SBarry Smith   Mat            C;
174414ca34e6SBarry Smith   PetscTruth     stride,sorted;
174517ab2063SBarry Smith 
17463a40ed3dSBarry Smith   PetscFunctionBegin;
174714ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1748e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
174914ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1750e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
175199141d43SSatish Balay 
175217ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1753b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1754b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
175517ab2063SBarry Smith 
1756fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1757fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1758fee21e36SBarry Smith   if (stride && step == 1) {
175902834360SBarry Smith     /* special case of contiguous rows */
17600e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
176102834360SBarry Smith     /* loop over new rows determining lens and starting points */
176202834360SBarry Smith     for (i=0; i<nrows; i++) {
1763bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1764a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
176502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1766bfeeae90SHong Zhang         if (aj[k] >= first) {
176702834360SBarry Smith           starts[i] = k;
176802834360SBarry Smith           break;
176902834360SBarry Smith 	}
177002834360SBarry Smith       }
1771a2744918SBarry Smith       sum = 0;
177202834360SBarry Smith       while (k < kend) {
1773bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1774a2744918SBarry Smith         sum++;
177502834360SBarry Smith       }
1776a2744918SBarry Smith       lens[i] = sum;
177702834360SBarry Smith     }
177802834360SBarry Smith     /* create submatrix */
1779cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
178097f1f81fSBarry Smith       PetscInt n_cols,n_rows;
178108480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1782e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1783d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
178408480c60SBarry Smith       C = *B;
17853a40ed3dSBarry Smith     } else {
17867adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1787f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17887adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1789ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
179008480c60SBarry Smith     }
1791db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1792db02288aSLois Curfman McInnes 
179302834360SBarry Smith     /* loop over rows inserting into submatrix */
1794db02288aSLois Curfman McInnes     a_new    = c->a;
1795db02288aSLois Curfman McInnes     j_new    = c->j;
1796db02288aSLois Curfman McInnes     i_new    = c->i;
1797bfeeae90SHong Zhang 
179802834360SBarry Smith     for (i=0; i<nrows; i++) {
1799a2744918SBarry Smith       ii    = starts[i];
1800a2744918SBarry Smith       lensi = lens[i];
1801a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1802a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
180302834360SBarry Smith       }
180487828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1805a2744918SBarry Smith       a_new      += lensi;
1806a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1807a2744918SBarry Smith       c->ilen[i]  = lensi;
180802834360SBarry Smith     }
18090e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
18103a40ed3dSBarry Smith   } else {
181102834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
18120e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
181397f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
18140e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
181517ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
181602834360SBarry Smith     /* determine lens of each row */
181702834360SBarry Smith     for (i=0; i<nrows; i++) {
1818bfeeae90SHong Zhang       kstart  = ai[irow[i]];
181902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
182002834360SBarry Smith       lens[i] = 0;
182102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1822bfeeae90SHong Zhang         if (smap[aj[k]]) {
182302834360SBarry Smith           lens[i]++;
182402834360SBarry Smith         }
182502834360SBarry Smith       }
182602834360SBarry Smith     }
182717ab2063SBarry Smith     /* Create and fill new matrix */
1828a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
18290f5bd95cSBarry Smith       PetscTruth equal;
18300f5bd95cSBarry Smith 
183199141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1832e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1833d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
18340f5bd95cSBarry Smith       if (!equal) {
1835e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
183699141d43SSatish Balay       }
1837d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
183808480c60SBarry Smith       C = *B;
18393a40ed3dSBarry Smith     } else {
18407adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1841f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
18427adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1843ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
184408480c60SBarry Smith     }
184599141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
184617ab2063SBarry Smith     for (i=0; i<nrows; i++) {
184799141d43SSatish Balay       row    = irow[i];
1848bfeeae90SHong Zhang       kstart = ai[row];
184999141d43SSatish Balay       kend   = kstart + a->ilen[row];
1850bfeeae90SHong Zhang       mat_i  = c->i[i];
185199141d43SSatish Balay       mat_j  = c->j + mat_i;
185299141d43SSatish Balay       mat_a  = c->a + mat_i;
185399141d43SSatish Balay       mat_ilen = c->ilen + i;
185417ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1855bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1856ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
185799141d43SSatish Balay           *mat_a++ = a->a[k];
185899141d43SSatish Balay           (*mat_ilen)++;
185999141d43SSatish Balay 
186017ab2063SBarry Smith         }
186117ab2063SBarry Smith       }
186217ab2063SBarry Smith     }
186302834360SBarry Smith     /* Free work space */
186402834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1865606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1866606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
186702834360SBarry Smith   }
18686d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18696d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
187017ab2063SBarry Smith 
187117ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1872416022c9SBarry Smith   *B = C;
18733a40ed3dSBarry Smith   PetscFunctionReturn(0);
187417ab2063SBarry Smith }
187517ab2063SBarry Smith 
18761df811f5SHong Zhang #undef __FUNCT__
18774a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18780481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1879a871dcd8SBarry Smith {
188063b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1881dfbe8321SBarry Smith   PetscErrorCode ierr;
188263b91edcSBarry Smith   Mat            outA;
1883b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
188463b91edcSBarry Smith 
18853a40ed3dSBarry Smith   PetscFunctionBegin;
1886e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
18871df811f5SHong Zhang 
1888b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1889b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1890a871dcd8SBarry Smith 
189163b91edcSBarry Smith   outA              = inA;
1892d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
1893c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1894c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1895c3122656SLisandro Dalcin   a->row = row;
1896c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1897c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1898c3122656SLisandro Dalcin   a->col = col;
189963b91edcSBarry Smith 
190036db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1901b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
19024c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
190352e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1904f0ec6fceSSatish Balay 
190594a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1906d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1907d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
190894a9d846SBarry Smith   }
190963b91edcSBarry Smith 
1910f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1911137fb511SHong Zhang   if (row_identity && col_identity) {
1912ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
1913137fb511SHong Zhang   } else {
1914719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1915137fb511SHong Zhang   }
19163a40ed3dSBarry Smith   PetscFunctionReturn(0);
1917a871dcd8SBarry Smith }
1918a871dcd8SBarry Smith 
19194a2ae208SSatish Balay #undef __FUNCT__
19204a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1921f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1922f0b747eeSBarry Smith {
1923f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1924f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1925efee365bSSatish Balay   PetscErrorCode ierr;
19260805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
19273a40ed3dSBarry Smith 
19283a40ed3dSBarry Smith   PetscFunctionBegin;
1929f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1930efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
19313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1932f0b747eeSBarry Smith }
1933f0b747eeSBarry Smith 
19344a2ae208SSatish Balay #undef __FUNCT__
19354a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
193697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1937cddf8d76SBarry Smith {
1938dfbe8321SBarry Smith   PetscErrorCode ierr;
193997f1f81fSBarry Smith   PetscInt       i;
1940cddf8d76SBarry Smith 
19413a40ed3dSBarry Smith   PetscFunctionBegin;
1942cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1943b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1944cddf8d76SBarry Smith   }
1945cddf8d76SBarry Smith 
1946cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19476a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1948cddf8d76SBarry Smith   }
19493a40ed3dSBarry Smith   PetscFunctionReturn(0);
1950cddf8d76SBarry Smith }
1951cddf8d76SBarry Smith 
19524a2ae208SSatish Balay #undef __FUNCT__
19534a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
195497f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19554dcbc457SBarry Smith {
1956e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19576849ba73SBarry Smith   PetscErrorCode ierr;
19585d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19595d0c19d7SBarry Smith   const PetscInt *idx;
196097f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1961f1af5d2fSBarry Smith   PetscBT        table;
1962bbd702dbSSatish Balay 
19633a40ed3dSBarry Smith   PetscFunctionBegin;
1964d0f46423SBarry Smith   m     = A->rmap->n;
1965e4d965acSSatish Balay   ai    = a->i;
1966bfeeae90SHong Zhang   aj    = a->j;
19678a047759SSatish Balay 
1968e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
196906763907SSatish Balay 
197097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19716831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
197206763907SSatish Balay 
1973e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1974b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1975e4d965acSSatish Balay     isz  = 0;
19766831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1977e4d965acSSatish Balay 
1978e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19794dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1980b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1981e4d965acSSatish Balay 
1982dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1983e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1984f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19854dcbc457SBarry Smith     }
198606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
198706763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1988e4d965acSSatish Balay 
198904a348a9SBarry Smith     k = 0;
199004a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
199104a348a9SBarry Smith       n = isz;
199206763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1993e4d965acSSatish Balay         row   = nidx[k];
1994e4d965acSSatish Balay         start = ai[row];
1995e4d965acSSatish Balay         end   = ai[row+1];
199604a348a9SBarry Smith         for (l = start; l<end ; l++){
1997efb16452SHong Zhang           val = aj[l] ;
1998f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1999e4d965acSSatish Balay         }
2000e4d965acSSatish Balay       }
2001e4d965acSSatish Balay     }
2002029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
2003e4d965acSSatish Balay   }
20046831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2005606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
20063a40ed3dSBarry Smith   PetscFunctionReturn(0);
20074dcbc457SBarry Smith }
200817ab2063SBarry Smith 
20090513a670SBarry Smith /* -------------------------------------------------------------- */
20104a2ae208SSatish Balay #undef __FUNCT__
20114a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2012dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
20130513a670SBarry Smith {
20140513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
20156849ba73SBarry Smith   PetscErrorCode ierr;
20163b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
20175d0c19d7SBarry Smith   const PetscInt *row,*col;
20185d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
201956cd22aeSBarry Smith   IS             icolp,irowp;
20203b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
20213b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
20220513a670SBarry Smith 
20233a40ed3dSBarry Smith   PetscFunctionBegin;
20244c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
202556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
20264c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
202756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
20280513a670SBarry Smith 
20290513a670SBarry Smith   /* determine lengths of permuted rows */
203097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
20310513a670SBarry Smith   for (i=0; i<m; i++) {
20320513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
20330513a670SBarry Smith   }
20347adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2035f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
20367adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2037ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2038606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
20390513a670SBarry Smith 
204097f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
20410513a670SBarry Smith   for (i=0; i<m; i++) {
204232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20430513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2044cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
204532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20460513a670SBarry Smith   }
2047606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20483c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20490513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20500513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
205156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
205256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
205356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
205456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20553a40ed3dSBarry Smith   PetscFunctionReturn(0);
20560513a670SBarry Smith }
20570513a670SBarry Smith 
20584a2ae208SSatish Balay #undef __FUNCT__
20594a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2060dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2061cb5b572fSBarry Smith {
2062dfbe8321SBarry Smith   PetscErrorCode ierr;
2063cb5b572fSBarry Smith 
2064cb5b572fSBarry Smith   PetscFunctionBegin;
206533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
206633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2067be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2068be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2069be6bf707SBarry Smith 
2070d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2071e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2072cb5b572fSBarry Smith     }
2073d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2074cb5b572fSBarry Smith   } else {
2075cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2076cb5b572fSBarry Smith   }
2077cb5b572fSBarry Smith   PetscFunctionReturn(0);
2078cb5b572fSBarry Smith }
2079cb5b572fSBarry Smith 
20804a2ae208SSatish Balay #undef __FUNCT__
20814a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2082dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2083273d9f13SBarry Smith {
2084dfbe8321SBarry Smith   PetscErrorCode ierr;
2085273d9f13SBarry Smith 
2086273d9f13SBarry Smith   PetscFunctionBegin;
2087ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2088273d9f13SBarry Smith   PetscFunctionReturn(0);
2089273d9f13SBarry Smith }
2090273d9f13SBarry Smith 
20914a2ae208SSatish Balay #undef __FUNCT__
20924a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2093a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20946c0721eeSBarry Smith {
20956c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20966c0721eeSBarry Smith   PetscFunctionBegin;
20976c0721eeSBarry Smith   *array = a->a;
20986c0721eeSBarry Smith   PetscFunctionReturn(0);
20996c0721eeSBarry Smith }
21006c0721eeSBarry Smith 
21014a2ae208SSatish Balay #undef __FUNCT__
21024a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2103dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
21046c0721eeSBarry Smith {
21056c0721eeSBarry Smith   PetscFunctionBegin;
21066c0721eeSBarry Smith   PetscFunctionReturn(0);
21076c0721eeSBarry Smith }
2108273d9f13SBarry Smith 
2109ee4f033dSBarry Smith #undef __FUNCT__
2110ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2111dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2112ee4f033dSBarry Smith {
21136849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
21146849ba73SBarry Smith   PetscErrorCode ierr;
211597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2116efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
211787828ca2SBarry Smith   PetscScalar    *vscale_array;
2118ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2119ee4f033dSBarry Smith   Vec            w1,w2,w3;
2120ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
212190d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2122ee4f033dSBarry Smith 
2123ee4f033dSBarry Smith   PetscFunctionBegin;
2124ee4f033dSBarry Smith   if (!coloring->w1) {
2125ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
212652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2127ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
212852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2129ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
213052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2131ee4f033dSBarry Smith   }
2132ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2133ee4f033dSBarry Smith 
2134ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
213590d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2136ee4f033dSBarry Smith   if (flg) {
2137ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2138ee4f033dSBarry Smith   } else {
21390b9b6f31SBarry Smith     PetscTruth assembled;
21400b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
21410b9b6f31SBarry Smith     if (assembled) {
2142ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2143ee4f033dSBarry Smith     }
21440b9b6f31SBarry Smith   }
2145ee4f033dSBarry Smith 
2146ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2147ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2148ee4f033dSBarry Smith 
2149ee4f033dSBarry Smith   /*
2150ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2151ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2152ee4f033dSBarry Smith   */
2153ee4f033dSBarry Smith   if (coloring->F) {
2154ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2155ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2156ee4f033dSBarry Smith     if (m1 != m2) {
2157ee4f033dSBarry Smith       coloring->F = 0;
2158ee4f033dSBarry Smith     }
2159ee4f033dSBarry Smith   }
2160ee4f033dSBarry Smith 
2161ee4f033dSBarry Smith   if (coloring->F) {
2162ee4f033dSBarry Smith     w1          = coloring->F;
2163ee4f033dSBarry Smith     coloring->F = 0;
2164ee4f033dSBarry Smith   } else {
216566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2166ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
216766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2168ee4f033dSBarry Smith   }
2169ee4f033dSBarry Smith 
2170ee4f033dSBarry Smith   /*
2171ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2172ee4f033dSBarry Smith   */
21731ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21741ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2175ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2176ee4f033dSBarry Smith     /*
2177ee4f033dSBarry Smith        Loop over each column associated with color adding the
2178ee4f033dSBarry Smith        perturbation to the vector w3.
2179ee4f033dSBarry Smith     */
2180ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2181ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2182ee4f033dSBarry Smith       dx  = xx[col];
2183ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2184ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2185ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2186ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2187ee4f033dSBarry Smith #else
2188ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2189ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2190ee4f033dSBarry Smith #endif
2191ee4f033dSBarry Smith       dx                *= epsilon;
2192ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2193ee4f033dSBarry Smith     }
2194ee4f033dSBarry Smith   }
21951ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2196ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2197ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2198ee4f033dSBarry Smith 
2199ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2200ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2201ee4f033dSBarry Smith 
2202ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2203ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2204ee4f033dSBarry Smith 
22051ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2206ee4f033dSBarry Smith   /*
2207ee4f033dSBarry Smith       Loop over each color
2208ee4f033dSBarry Smith   */
2209ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
221049b058dcSBarry Smith     coloring->currentcolor = k;
2211ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
22121ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2213ee4f033dSBarry Smith     /*
2214ee4f033dSBarry Smith        Loop over each column associated with color adding the
2215ee4f033dSBarry Smith        perturbation to the vector w3.
2216ee4f033dSBarry Smith     */
2217ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2218ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2219ee4f033dSBarry Smith       dx  = xx[col];
22205b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2221ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2222ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2223ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2224ee4f033dSBarry Smith #else
2225ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2226ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2227ee4f033dSBarry Smith #endif
2228ee4f033dSBarry Smith       dx            *= epsilon;
2229e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2230ee4f033dSBarry Smith       w3_array[col] += dx;
2231ee4f033dSBarry Smith     }
22321ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2233ee4f033dSBarry Smith 
2234ee4f033dSBarry Smith     /*
2235ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2236ee4f033dSBarry Smith     */
2237ee4f033dSBarry Smith 
223866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2239ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
224066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2241efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2242ee4f033dSBarry Smith 
2243ee4f033dSBarry Smith     /*
2244ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2245ee4f033dSBarry Smith     */
22461ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2247ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2248ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2249ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2250ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2251ee4f033dSBarry Smith       srow   = row + start;
2252ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2253ee4f033dSBarry Smith     }
22541ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2255ee4f033dSBarry Smith   }
225649b058dcSBarry Smith   coloring->currentcolor = k;
22571ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22581ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2259ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2260ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2261ee4f033dSBarry Smith   PetscFunctionReturn(0);
2262ee4f033dSBarry Smith }
2263ee4f033dSBarry Smith 
2264ac90fabeSBarry Smith #undef __FUNCT__
2265ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2266f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2267ac90fabeSBarry Smith {
2268dfbe8321SBarry Smith   PetscErrorCode ierr;
226997f1f81fSBarry Smith   PetscInt       i;
2270ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22710805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2272ac90fabeSBarry Smith 
2273ac90fabeSBarry Smith   PetscFunctionBegin;
2274ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2275f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2276f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2277c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2278a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2279a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2280a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2281a30b2313SHong Zhang     }
2282a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2283d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2284a30b2313SHong Zhang       y->XtoY = X;
2285407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2286c537a176SHong Zhang     }
2287f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22881e2582c4SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);CHKERRQ(ierr);
2289ac90fabeSBarry Smith   } else {
2290f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2291ac90fabeSBarry Smith   }
2292ac90fabeSBarry Smith   PetscFunctionReturn(0);
2293ac90fabeSBarry Smith }
2294ac90fabeSBarry Smith 
2295521d7252SBarry Smith #undef __FUNCT__
2296521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2297521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2298521d7252SBarry Smith {
229941c166b1SJed Brown   PetscErrorCode ierr;
230041c166b1SJed Brown 
2301521d7252SBarry Smith   PetscFunctionBegin;
230241c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
230341c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2304521d7252SBarry Smith   PetscFunctionReturn(0);
2305521d7252SBarry Smith }
2306521d7252SBarry Smith 
2307354c94deSBarry Smith #undef __FUNCT__
2308354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2309354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2310354c94deSBarry Smith {
2311354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2312354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2313354c94deSBarry Smith   PetscInt    i,nz;
2314354c94deSBarry Smith   PetscScalar *a;
2315354c94deSBarry Smith 
2316354c94deSBarry Smith   PetscFunctionBegin;
2317354c94deSBarry Smith   nz = aij->nz;
2318354c94deSBarry Smith   a  = aij->a;
2319354c94deSBarry Smith   for (i=0; i<nz; i++) {
2320354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2321354c94deSBarry Smith   }
2322354c94deSBarry Smith #else
2323354c94deSBarry Smith   PetscFunctionBegin;
2324354c94deSBarry Smith #endif
2325354c94deSBarry Smith   PetscFunctionReturn(0);
2326354c94deSBarry Smith }
2327354c94deSBarry Smith 
2328e34fafa9SBarry Smith #undef __FUNCT__
2329985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2330985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2331e34fafa9SBarry Smith {
2332e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2333e34fafa9SBarry Smith   PetscErrorCode ierr;
2334d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2335e34fafa9SBarry Smith   PetscReal      atmp;
2336985db425SBarry Smith   PetscScalar    *x;
2337e34fafa9SBarry Smith   MatScalar      *aa;
2338e34fafa9SBarry Smith 
2339e34fafa9SBarry Smith   PetscFunctionBegin;
2340e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2341e34fafa9SBarry Smith   aa   = a->a;
2342e34fafa9SBarry Smith   ai   = a->i;
2343e34fafa9SBarry Smith   aj   = a->j;
2344e34fafa9SBarry Smith 
2345985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2346e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2347e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2348e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2349e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2350e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23519189402eSHong Zhang     x[i] = 0.0;
2352e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2353985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2354985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2355985db425SBarry Smith       aa++; aj++;
2356985db425SBarry Smith     }
2357985db425SBarry Smith   }
2358985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2359985db425SBarry Smith   PetscFunctionReturn(0);
2360985db425SBarry Smith }
2361985db425SBarry Smith 
2362985db425SBarry Smith #undef __FUNCT__
2363985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2364985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2365985db425SBarry Smith {
2366985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2367985db425SBarry Smith   PetscErrorCode ierr;
2368d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2369985db425SBarry Smith   PetscScalar    *x;
2370985db425SBarry Smith   MatScalar      *aa;
2371985db425SBarry Smith 
2372985db425SBarry Smith   PetscFunctionBegin;
2373e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2374985db425SBarry Smith   aa   = a->a;
2375985db425SBarry Smith   ai   = a->i;
2376985db425SBarry Smith   aj   = a->j;
2377985db425SBarry Smith 
2378985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2379985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2380985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2381e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2382985db425SBarry Smith   for (i=0; i<m; i++) {
2383985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2384d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2385985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2386985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2387985db425SBarry Smith       x[i] = 0.0;
2388985db425SBarry Smith       if (idx) {
2389985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2390985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2391985db425SBarry Smith           if (aj[j] > j) {
2392985db425SBarry Smith             idx[i] = j;
2393985db425SBarry Smith             break;
2394985db425SBarry Smith           }
2395985db425SBarry Smith         }
2396985db425SBarry Smith       }
2397985db425SBarry Smith     }
2398985db425SBarry Smith     for (j=0; j<ncols; j++){
2399985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2400985db425SBarry Smith       aa++; aj++;
2401985db425SBarry Smith     }
2402985db425SBarry Smith   }
2403985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2404985db425SBarry Smith   PetscFunctionReturn(0);
2405985db425SBarry Smith }
2406985db425SBarry Smith 
2407985db425SBarry Smith #undef __FUNCT__
2408c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2409c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2410c87e5d42SMatthew Knepley {
2411c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2412c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2413c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2414c87e5d42SMatthew Knepley   PetscReal      atmp;
2415c87e5d42SMatthew Knepley   PetscScalar    *x;
2416c87e5d42SMatthew Knepley   MatScalar      *aa;
2417c87e5d42SMatthew Knepley 
2418c87e5d42SMatthew Knepley   PetscFunctionBegin;
2419e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2420c87e5d42SMatthew Knepley   aa   = a->a;
2421c87e5d42SMatthew Knepley   ai   = a->i;
2422c87e5d42SMatthew Knepley   aj   = a->j;
2423c87e5d42SMatthew Knepley 
2424c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2425c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2426c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2427e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2428c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2429c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2430289a08f5SMatthew Knepley     if (ncols) {
2431289a08f5SMatthew Knepley       /* Get first nonzero */
2432289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2433289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2434289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2435289a08f5SMatthew Knepley       }
2436289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2437289a08f5SMatthew Knepley     } else {
2438289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2439289a08f5SMatthew Knepley     }
2440c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2441c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2442289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2443c87e5d42SMatthew Knepley       aa++; aj++;
2444c87e5d42SMatthew Knepley     }
2445c87e5d42SMatthew Knepley   }
2446c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2447c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2448c87e5d42SMatthew Knepley }
2449c87e5d42SMatthew Knepley 
2450c87e5d42SMatthew Knepley #undef __FUNCT__
2451985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2452985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2453985db425SBarry Smith {
2454985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2455985db425SBarry Smith   PetscErrorCode ierr;
2456d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2457985db425SBarry Smith   PetscScalar    *x;
2458985db425SBarry Smith   MatScalar      *aa;
2459985db425SBarry Smith 
2460985db425SBarry Smith   PetscFunctionBegin;
2461e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2462985db425SBarry Smith   aa   = a->a;
2463985db425SBarry Smith   ai   = a->i;
2464985db425SBarry Smith   aj   = a->j;
2465985db425SBarry Smith 
2466985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2467985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2468985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2469e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2470985db425SBarry Smith   for (i=0; i<m; i++) {
2471985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2472d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2473985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2474985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2475985db425SBarry Smith       x[i] = 0.0;
2476985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2477985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2478985db425SBarry Smith         for (j=0;j<ncols;j++) {
2479985db425SBarry Smith           if (aj[j] > j) {
2480985db425SBarry Smith             idx[i] = j;
2481985db425SBarry Smith             break;
2482985db425SBarry Smith           }
2483985db425SBarry Smith         }
2484985db425SBarry Smith       }
2485985db425SBarry Smith     }
2486985db425SBarry Smith     for (j=0; j<ncols; j++){
2487985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2488985db425SBarry Smith       aa++; aj++;
2489e34fafa9SBarry Smith     }
2490e34fafa9SBarry Smith   }
2491e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2492e34fafa9SBarry Smith   PetscFunctionReturn(0);
2493e34fafa9SBarry Smith }
24943acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2495682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24960a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2497cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2498cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2499cb5b572fSBarry Smith        MatMult_SeqAIJ,
250097304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
25017c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
25027c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2503db4efbfdSBarry Smith        0,
2504db4efbfdSBarry Smith        0,
2505db4efbfdSBarry Smith        0,
2506db4efbfdSBarry Smith /*10*/ 0,
2507cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2508cb5b572fSBarry Smith        0,
250941f059aeSBarry Smith        MatSOR_SeqAIJ,
251017ab2063SBarry Smith        MatTranspose_SeqAIJ,
251197304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2512cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2513cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2514cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2515cb5b572fSBarry Smith        MatNorm_SeqAIJ,
251697304618SKris Buschelman /*20*/ 0,
2517cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2518cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2519cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2520d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2521db4efbfdSBarry Smith        0,
2522db4efbfdSBarry Smith        0,
2523db4efbfdSBarry Smith        0,
2524db4efbfdSBarry Smith        0,
2525d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2526db4efbfdSBarry Smith        0,
2527db4efbfdSBarry Smith        0,
25286c0721eeSBarry Smith        MatGetArray_SeqAIJ,
25296c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2530d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2531cb5b572fSBarry Smith        0,
2532cb5b572fSBarry Smith        0,
2533cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2534cb5b572fSBarry Smith        0,
2535d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2536cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2537cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2538cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2539cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2540d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2541cb5b572fSBarry Smith        MatScale_SeqAIJ,
2542cb5b572fSBarry Smith        0,
254379299369SBarry Smith        MatDiagonalSet_SeqAIJ,
2544fe97e370SBarry Smith        0,
2545d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
25463b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
25473b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
25483b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2549a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2550d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2551b9617806SBarry Smith        0,
25520513a670SBarry Smith        0,
2553cda55fadSBarry Smith        MatPermute_SeqAIJ,
2554cda55fadSBarry Smith        0,
2555d519adbfSMatthew Knepley /*59*/ 0,
2556b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2557b9b97703SBarry Smith        MatView_SeqAIJ,
2558357abbc8SBarry Smith        0,
2559ee4f033dSBarry Smith        0,
2560d519adbfSMatthew Knepley /*64*/ 0,
2561ee4f033dSBarry Smith        0,
2562ee4f033dSBarry Smith        0,
2563ee4f033dSBarry Smith        0,
2564ee4f033dSBarry Smith        0,
2565d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2566c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2567ee4f033dSBarry Smith        0,
2568ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2569dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2570ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2571dcf5cc72SBarry Smith #else
2572dcf5cc72SBarry Smith        0,
2573dcf5cc72SBarry Smith #endif
2574d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
25753acb8795SBarry Smith        MatFDColoringApply_AIJ,
257697304618SKris Buschelman        0,
257797304618SKris Buschelman        0,
257897304618SKris Buschelman        0,
2579d519adbfSMatthew Knepley /*79*/ 0,
258097304618SKris Buschelman        0,
258197304618SKris Buschelman        0,
258297304618SKris Buschelman        0,
2583bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2584d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
25851cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25866284ec50SHong Zhang        0,
25876284ec50SHong Zhang        0,
2588bc011b1eSHong Zhang        0,
2589d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
259026be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
259126be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2592d439da42SKris Buschelman        MatPtAP_Basic,
25937ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2594d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2595bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2596bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2597bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25987ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2599d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2600609c6c4dSKris Buschelman        0,
2601609c6c4dSKris Buschelman        0,
260287d4246cSBarry Smith        MatConjugate_SeqAIJ,
260387d4246cSBarry Smith        0,
2604d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
260599cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2606f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2607f5edf698SHong Zhang        0,
26082bebee5dSHong Zhang        0,
2609d519adbfSMatthew Knepley /*109*/0,
2610985db425SBarry Smith        0,
26112af78befSBarry Smith        MatGetRowMin_SeqAIJ,
26122af78befSBarry Smith        0,
2613599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2614d519adbfSMatthew Knepley /*114*/0,
2615599ef60dSHong Zhang        0,
26163c2a7987SHong Zhang        0,
2617fe97e370SBarry Smith        0,
2618fe97e370SBarry Smith        0
26199e29f15eSvictorle };
262017ab2063SBarry Smith 
2621fb2e594dSBarry Smith EXTERN_C_BEGIN
26224a2ae208SSatish Balay #undef __FUNCT__
26234a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2624be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2625bef8e0ddSBarry Smith {
2626bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
262797f1f81fSBarry Smith   PetscInt   i,nz,n;
2628bef8e0ddSBarry Smith 
2629bef8e0ddSBarry Smith   PetscFunctionBegin;
2630bef8e0ddSBarry Smith 
2631bef8e0ddSBarry Smith   nz = aij->maxnz;
2632d0f46423SBarry Smith   n  = mat->rmap->n;
2633bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2634bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2635bef8e0ddSBarry Smith   }
2636bef8e0ddSBarry Smith   aij->nz = nz;
2637bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2638bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2639bef8e0ddSBarry Smith   }
2640bef8e0ddSBarry Smith 
2641bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2642bef8e0ddSBarry Smith }
2643fb2e594dSBarry Smith EXTERN_C_END
2644bef8e0ddSBarry Smith 
26454a2ae208SSatish Balay #undef __FUNCT__
26464a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2647bef8e0ddSBarry Smith /*@
2648bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2649bef8e0ddSBarry Smith        in the matrix.
2650bef8e0ddSBarry Smith 
2651bef8e0ddSBarry Smith   Input Parameters:
2652bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2653bef8e0ddSBarry Smith -  indices - the column indices
2654bef8e0ddSBarry Smith 
265515091d37SBarry Smith   Level: advanced
265615091d37SBarry Smith 
2657bef8e0ddSBarry Smith   Notes:
2658bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2659bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2660bef8e0ddSBarry Smith   of the MatSetValues() operation.
2661bef8e0ddSBarry Smith 
2662bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2663d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2664bef8e0ddSBarry Smith 
2665bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2666bef8e0ddSBarry Smith 
2667b9617806SBarry Smith     The indices should start with zero, not one.
2668b9617806SBarry Smith 
2669bef8e0ddSBarry Smith @*/
2670be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2671bef8e0ddSBarry Smith {
267297f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2673bef8e0ddSBarry Smith 
2674bef8e0ddSBarry Smith   PetscFunctionBegin;
26750700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
26764482741eSBarry Smith   PetscValidPointer(indices,2);
2677c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2678bef8e0ddSBarry Smith   if (f) {
2679bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2680bef8e0ddSBarry Smith   } else {
2681e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2682bef8e0ddSBarry Smith   }
2683bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2684bef8e0ddSBarry Smith }
2685bef8e0ddSBarry Smith 
2686be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2687be6bf707SBarry Smith 
2688fb2e594dSBarry Smith EXTERN_C_BEGIN
26894a2ae208SSatish Balay #undef __FUNCT__
26904a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2691be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2692be6bf707SBarry Smith {
2693be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26946849ba73SBarry Smith   PetscErrorCode ierr;
2695d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2696be6bf707SBarry Smith 
2697be6bf707SBarry Smith   PetscFunctionBegin;
2698be6bf707SBarry Smith   if (aij->nonew != 1) {
2699e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2700be6bf707SBarry Smith   }
2701be6bf707SBarry Smith 
2702be6bf707SBarry Smith   /* allocate space for values if not already there */
2703be6bf707SBarry Smith   if (!aij->saved_values) {
270487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
27059518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2706be6bf707SBarry Smith   }
2707be6bf707SBarry Smith 
2708be6bf707SBarry Smith   /* copy values over */
270987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2710be6bf707SBarry Smith   PetscFunctionReturn(0);
2711be6bf707SBarry Smith }
2712fb2e594dSBarry Smith EXTERN_C_END
2713be6bf707SBarry Smith 
27144a2ae208SSatish Balay #undef __FUNCT__
2715b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2716be6bf707SBarry Smith /*@
2717be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2718be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2719be6bf707SBarry Smith        nonlinear portion.
2720be6bf707SBarry Smith 
2721be6bf707SBarry Smith    Collect on Mat
2722be6bf707SBarry Smith 
2723be6bf707SBarry Smith   Input Parameters:
27240e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2725be6bf707SBarry Smith 
272615091d37SBarry Smith   Level: advanced
272715091d37SBarry Smith 
2728be6bf707SBarry Smith   Common Usage, with SNESSolve():
2729be6bf707SBarry Smith $    Create Jacobian matrix
2730be6bf707SBarry Smith $    Set linear terms into matrix
2731be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2732be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2733be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2734512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2735be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2736be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2737be6bf707SBarry Smith $    In your Jacobian routine
2738be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2739be6bf707SBarry Smith $      Set nonlinear terms in matrix
2740be6bf707SBarry Smith 
2741be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2742be6bf707SBarry Smith $    // build linear portion of Jacobian
2743512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2744be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2745be6bf707SBarry Smith $    loop over nonlinear iterations
2746be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2747be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2748be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2749be6bf707SBarry Smith $       Solve linear system with Jacobian
2750be6bf707SBarry Smith $    endloop
2751be6bf707SBarry Smith 
2752be6bf707SBarry Smith   Notes:
2753be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2754512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2755be6bf707SBarry Smith     calling this routine.
2756be6bf707SBarry Smith 
27570c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
27580c468ba9SBarry Smith     and does not allocated additional space.
27590c468ba9SBarry Smith 
2760be6bf707SBarry Smith .seealso: MatRetrieveValues()
2761be6bf707SBarry Smith 
2762be6bf707SBarry Smith @*/
2763be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2764be6bf707SBarry Smith {
2765dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2766be6bf707SBarry Smith 
2767be6bf707SBarry Smith   PetscFunctionBegin;
27680700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2769e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2770e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2771be6bf707SBarry Smith 
2772c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2773be6bf707SBarry Smith   if (f) {
2774be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2775be6bf707SBarry Smith   } else {
2776e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to store values");
2777be6bf707SBarry Smith   }
2778be6bf707SBarry Smith   PetscFunctionReturn(0);
2779be6bf707SBarry Smith }
2780be6bf707SBarry Smith 
2781fb2e594dSBarry Smith EXTERN_C_BEGIN
27824a2ae208SSatish Balay #undef __FUNCT__
27834a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2784be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2785be6bf707SBarry Smith {
2786be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27876849ba73SBarry Smith   PetscErrorCode ierr;
2788d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2789be6bf707SBarry Smith 
2790be6bf707SBarry Smith   PetscFunctionBegin;
2791be6bf707SBarry Smith   if (aij->nonew != 1) {
2792e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2793be6bf707SBarry Smith   }
2794be6bf707SBarry Smith   if (!aij->saved_values) {
2795e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2796be6bf707SBarry Smith   }
2797be6bf707SBarry Smith   /* copy values over */
279887828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2799be6bf707SBarry Smith   PetscFunctionReturn(0);
2800be6bf707SBarry Smith }
2801fb2e594dSBarry Smith EXTERN_C_END
2802be6bf707SBarry Smith 
28034a2ae208SSatish Balay #undef __FUNCT__
28044a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2805be6bf707SBarry Smith /*@
2806be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2807be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2808be6bf707SBarry Smith        nonlinear portion.
2809be6bf707SBarry Smith 
2810be6bf707SBarry Smith    Collect on Mat
2811be6bf707SBarry Smith 
2812be6bf707SBarry Smith   Input Parameters:
2813be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2814be6bf707SBarry Smith 
281515091d37SBarry Smith   Level: advanced
281615091d37SBarry Smith 
2817be6bf707SBarry Smith .seealso: MatStoreValues()
2818be6bf707SBarry Smith 
2819be6bf707SBarry Smith @*/
2820be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2821be6bf707SBarry Smith {
2822dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2823be6bf707SBarry Smith 
2824be6bf707SBarry Smith   PetscFunctionBegin;
28250700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2826e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2827e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2828be6bf707SBarry Smith 
2829c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2830be6bf707SBarry Smith   if (f) {
2831be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2832be6bf707SBarry Smith   } else {
2833e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2834be6bf707SBarry Smith   }
2835be6bf707SBarry Smith   PetscFunctionReturn(0);
2836be6bf707SBarry Smith }
2837be6bf707SBarry Smith 
2838f83d6046SBarry Smith 
2839be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
28404a2ae208SSatish Balay #undef __FUNCT__
28414a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
284217ab2063SBarry Smith /*@C
2843682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
28440d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
28456e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
284651c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
28472bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
284817ab2063SBarry Smith 
2849db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2850db81eaa0SLois Curfman McInnes 
285117ab2063SBarry Smith    Input Parameters:
2852db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
285317ab2063SBarry Smith .  m - number of rows
285417ab2063SBarry Smith .  n - number of columns
285517ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
285651c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
28572bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
285817ab2063SBarry Smith 
285917ab2063SBarry Smith    Output Parameter:
2860416022c9SBarry Smith .  A - the matrix
286117ab2063SBarry Smith 
2862175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2863ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2864175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2865175b88e8SBarry Smith 
2866b259b22eSLois Curfman McInnes    Notes:
286749a6f317SBarry Smith    If nnz is given then nz is ignored
286849a6f317SBarry Smith 
286917ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
287017ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28710002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
287244cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
287317ab2063SBarry Smith 
287417ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2875a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28763d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28776da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
287817ab2063SBarry Smith 
2879682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
28804fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2881682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
28826c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
28836c7ebb05SLois Curfman McInnes 
28846c7ebb05SLois Curfman McInnes    Options Database Keys:
2885698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2886698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2887db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2888db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2889db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
289017ab2063SBarry Smith 
2891027ccd11SLois Curfman McInnes    Level: intermediate
2892027ccd11SLois Curfman McInnes 
289336db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
289436db0b34SBarry Smith 
289517ab2063SBarry Smith @*/
2896be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
289717ab2063SBarry Smith {
2898dfbe8321SBarry Smith   PetscErrorCode ierr;
28996945ee14SBarry Smith 
29003a40ed3dSBarry Smith   PetscFunctionBegin;
2901f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2902117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2903c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2904ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2905273d9f13SBarry Smith   PetscFunctionReturn(0);
2906273d9f13SBarry Smith }
2907273d9f13SBarry Smith 
29084a2ae208SSatish Balay #undef __FUNCT__
29094a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2910273d9f13SBarry Smith /*@C
2911273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2912273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2913273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2914273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2915273d9f13SBarry Smith 
2916273d9f13SBarry Smith    Collective on MPI_Comm
2917273d9f13SBarry Smith 
2918273d9f13SBarry Smith    Input Parameters:
2919117016b1SBarry Smith +  B - The matrix-free
2920273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2921273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2922273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2923273d9f13SBarry Smith 
2924273d9f13SBarry Smith    Notes:
292549a6f317SBarry Smith      If nnz is given then nz is ignored
292649a6f317SBarry Smith 
2927273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2928273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2929273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2930273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2931273d9f13SBarry Smith 
2932273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2933273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2934273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2935273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2936273d9f13SBarry Smith 
2937aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2938aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2939aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2940aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2941aa95bbe8SBarry Smith 
2942a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2943a96a251dSBarry Smith    entries or columns indices
2944a96a251dSBarry Smith 
2945273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2946273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2947273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2948273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2949273d9f13SBarry Smith 
2950273d9f13SBarry Smith    Options Database Keys:
2951698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2952698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2953273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2954273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2955273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2956273d9f13SBarry Smith 
2957273d9f13SBarry Smith    Level: intermediate
2958273d9f13SBarry Smith 
2959aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2960273d9f13SBarry Smith 
2961273d9f13SBarry Smith @*/
2962be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2963273d9f13SBarry Smith {
296497f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2965a23d5eceSKris Buschelman 
2966a23d5eceSKris Buschelman   PetscFunctionBegin;
2967a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2968a23d5eceSKris Buschelman   if (f) {
2969a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2970a23d5eceSKris Buschelman   }
2971a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2972a23d5eceSKris Buschelman }
2973a23d5eceSKris Buschelman 
2974a23d5eceSKris Buschelman EXTERN_C_BEGIN
2975a23d5eceSKris Buschelman #undef __FUNCT__
2976a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2977be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2978a23d5eceSKris Buschelman {
2979273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2980a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
29816849ba73SBarry Smith   PetscErrorCode ierr;
298297f1f81fSBarry Smith   PetscInt       i;
2983273d9f13SBarry Smith 
2984273d9f13SBarry Smith   PetscFunctionBegin;
2985d5d45c9bSBarry Smith 
2986a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2987c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2988c461c341SBarry Smith     nz             = 0;
2989c461c341SBarry Smith   }
2990c461c341SBarry Smith 
299126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
299226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
299326283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
299426283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2995899cda47SBarry Smith 
2996435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2997e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2998b73539f3SBarry Smith   if (nnz) {
2999d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3000e32f2f54SBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
3001e32f2f54SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
3002b73539f3SBarry Smith     }
3003b73539f3SBarry Smith   }
3004b73539f3SBarry Smith 
3005273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3006273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3007273d9f13SBarry Smith 
3008ab93d7beSBarry Smith   if (!skipallocation) {
30092ee49352SLisandro Dalcin     if (!b->imax) {
3010d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3011d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
30122ee49352SLisandro Dalcin     }
3013273d9f13SBarry Smith     if (!nnz) {
3014435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3015273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
3016d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3017d0f46423SBarry Smith       nz = nz*B->rmap->n;
3018273d9f13SBarry Smith     } else {
3019273d9f13SBarry Smith       nz = 0;
3020d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3021273d9f13SBarry Smith     }
3022ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3023d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3024ab93d7beSBarry Smith 
3025273d9f13SBarry Smith     /* allocate the matrix space */
30262ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3027d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3028d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3029bfeeae90SHong Zhang     b->i[0] = 0;
3030d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
30315da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
30325da197adSKris Buschelman     }
3033273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3034e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3035e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3036c461c341SBarry Smith   } else {
3037e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3038e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3039c461c341SBarry Smith   }
3040273d9f13SBarry Smith 
3041273d9f13SBarry Smith   b->nz                = 0;
3042273d9f13SBarry Smith   b->maxnz             = nz;
3043273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3044273d9f13SBarry Smith   PetscFunctionReturn(0);
3045273d9f13SBarry Smith }
3046a23d5eceSKris Buschelman EXTERN_C_END
3047273d9f13SBarry Smith 
3048a1661176SMatthew Knepley #undef  __FUNCT__
3049a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
305058d36128SBarry Smith /*@
3051a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3052a1661176SMatthew Knepley 
3053a1661176SMatthew Knepley    Input Parameters:
3054a1661176SMatthew Knepley +  B - the matrix
3055a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3056a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3057a1661176SMatthew Knepley -  v - optional values in the matrix
3058a1661176SMatthew Knepley 
3059a1661176SMatthew Knepley    Level: developer
3060a1661176SMatthew Knepley 
306158d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
306258d36128SBarry Smith 
3063a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3064a1661176SMatthew Knepley 
3065a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3066a1661176SMatthew Knepley @*/
3067a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3068a1661176SMatthew Knepley {
3069a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3070a1661176SMatthew Knepley   PetscErrorCode ierr;
3071a1661176SMatthew Knepley 
3072a1661176SMatthew Knepley   PetscFunctionBegin;
30730700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
3074a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3075a1661176SMatthew Knepley   if (f) {
3076a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3077a1661176SMatthew Knepley   }
3078a1661176SMatthew Knepley   PetscFunctionReturn(0);
3079a1661176SMatthew Knepley }
3080a1661176SMatthew Knepley 
3081a1661176SMatthew Knepley EXTERN_C_BEGIN
3082a1661176SMatthew Knepley #undef  __FUNCT__
3083a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3084b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3085a1661176SMatthew Knepley {
3086a1661176SMatthew Knepley   PetscInt       i;
3087a1661176SMatthew Knepley   PetscInt       m,n;
3088a1661176SMatthew Knepley   PetscInt       nz;
3089a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3090a1661176SMatthew Knepley   PetscScalar    *values;
3091a1661176SMatthew Knepley   PetscErrorCode ierr;
3092a1661176SMatthew Knepley 
3093a1661176SMatthew Knepley   PetscFunctionBegin;
3094a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3095a1661176SMatthew Knepley 
309665e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3097a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3098a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3099b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3100a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
310165e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3102a1661176SMatthew Knepley     nnz[i] = nz;
3103a1661176SMatthew Knepley   }
3104a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3105a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3106a1661176SMatthew Knepley 
3107a1661176SMatthew Knepley   if (v) {
3108a1661176SMatthew Knepley     values = (PetscScalar*) v;
3109a1661176SMatthew Knepley   } else {
31100e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3111a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3112a1661176SMatthew Knepley   }
3113a1661176SMatthew Knepley 
3114a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3115b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3116b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3117a1661176SMatthew Knepley   }
3118a1661176SMatthew Knepley 
3119a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3120a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3121a1661176SMatthew Knepley 
3122a1661176SMatthew Knepley   if (!v) {
3123a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3124a1661176SMatthew Knepley   }
3125a1661176SMatthew Knepley   PetscFunctionReturn(0);
3126a1661176SMatthew Knepley }
3127a1661176SMatthew Knepley EXTERN_C_END
3128a1661176SMatthew Knepley 
31297c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3130be7314b0SBarry Smith #include "private/petscaxpy.h"
3131170fe5c8SBarry Smith 
3132170fe5c8SBarry Smith #undef __FUNCT__
3133170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3134170fe5c8SBarry Smith /*
3135170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3136170fe5c8SBarry Smith 
3137170fe5c8SBarry Smith                n                       p                          p
3138170fe5c8SBarry Smith         (              )       (              )         (                  )
3139170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3140170fe5c8SBarry Smith         (              )       (              )         (                  )
3141170fe5c8SBarry Smith 
3142170fe5c8SBarry Smith */
3143170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3144170fe5c8SBarry Smith {
3145170fe5c8SBarry Smith   PetscErrorCode     ierr;
3146170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3147170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3148170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
31491de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3150170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3151170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3152170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3153170fe5c8SBarry Smith 
3154170fe5c8SBarry Smith   PetscFunctionBegin;
3155d0f46423SBarry Smith   m = A->rmap->n;
3156d0f46423SBarry Smith   n = A->cmap->n;
3157d0f46423SBarry Smith   p = B->cmap->n;
3158170fe5c8SBarry Smith   a = sub_a->v;
3159170fe5c8SBarry Smith   b = sub_b->a;
3160170fe5c8SBarry Smith   c = sub_c->v;
3161170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3162170fe5c8SBarry Smith 
3163170fe5c8SBarry Smith   ii  = sub_b->i;
3164170fe5c8SBarry Smith   idx = sub_b->j;
3165170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3166170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3167170fe5c8SBarry Smith     while (q-->0) {
3168170fe5c8SBarry Smith       c_q = c + m*(*idx);
3169170fe5c8SBarry Smith       a_q = a + m*i;
3170be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3171170fe5c8SBarry Smith       idx++;
3172170fe5c8SBarry Smith       b++;
3173170fe5c8SBarry Smith     }
3174170fe5c8SBarry Smith   }
3175170fe5c8SBarry Smith   PetscFunctionReturn(0);
3176170fe5c8SBarry Smith }
3177170fe5c8SBarry Smith 
3178170fe5c8SBarry Smith #undef __FUNCT__
3179170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3180170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3181170fe5c8SBarry Smith {
3182170fe5c8SBarry Smith   PetscErrorCode ierr;
3183d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3184170fe5c8SBarry Smith   Mat            Cmat;
3185170fe5c8SBarry Smith 
3186170fe5c8SBarry Smith   PetscFunctionBegin;
3187e32f2f54SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
318839804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3189170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3190170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3191170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3192170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3193170fe5c8SBarry Smith   *C = Cmat;
3194170fe5c8SBarry Smith   PetscFunctionReturn(0);
3195170fe5c8SBarry Smith }
3196170fe5c8SBarry Smith 
3197170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3198170fe5c8SBarry Smith #undef __FUNCT__
3199170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3200170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3201170fe5c8SBarry Smith {
3202170fe5c8SBarry Smith   PetscErrorCode ierr;
3203170fe5c8SBarry Smith 
3204170fe5c8SBarry Smith   PetscFunctionBegin;
3205170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3206170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3207170fe5c8SBarry Smith   }
3208170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3209170fe5c8SBarry Smith   PetscFunctionReturn(0);
3210170fe5c8SBarry Smith }
3211170fe5c8SBarry Smith 
3212170fe5c8SBarry Smith 
32130bad9183SKris Buschelman /*MC
3214fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
32150bad9183SKris Buschelman    based on compressed sparse row format.
32160bad9183SKris Buschelman 
32170bad9183SKris Buschelman    Options Database Keys:
32180bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
32190bad9183SKris Buschelman 
32200bad9183SKris Buschelman   Level: beginner
32210bad9183SKris Buschelman 
3222f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
32230bad9183SKris Buschelman M*/
32240bad9183SKris Buschelman 
3225a6175056SHong Zhang EXTERN_C_BEGIN
3226b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3227b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3228b5e56a35SBarry Smith #endif
322965460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3230af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3231af1023dbSSatish Balay #endif
323217667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3233b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3234a83b8d76SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
3235db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3236611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3237bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3238611f576cSBarry Smith #endif
3239611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32405c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3241611f576cSBarry Smith #endif
3242f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3243f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3244f3c0ef26SHong Zhang #endif
3245611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32465c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3247611f576cSBarry Smith #endif
3248eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3249eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3250eb3b5408SSatish Balay #endif
3251719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3252719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3253719d5645SBarry Smith #endif
3254b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3255b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
3256b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEnginePut_SeqAIJ(PetscObject,void*);
3257b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEngineGet_SeqAIJ(PetscObject,void*);
3258b3866ffcSBarry Smith #endif
325917667f90SBarry Smith EXTERN_C_END
326017667f90SBarry Smith 
3261b24902e0SBarry Smith 
326217667f90SBarry Smith EXTERN_C_BEGIN
32634a2ae208SSatish Balay #undef __FUNCT__
32644a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3265be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3266273d9f13SBarry Smith {
3267273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3268dfbe8321SBarry Smith   PetscErrorCode ierr;
326938baddfdSBarry Smith   PetscMPIInt    size;
3270273d9f13SBarry Smith 
3271273d9f13SBarry Smith   PetscFunctionBegin;
32727adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3273e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3274273d9f13SBarry Smith 
327538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3276b0a32e0cSBarry Smith   B->data             = (void*)b;
3277549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
327890f02eecSBarry Smith   B->mapping          = 0;
3279416022c9SBarry Smith   b->row              = 0;
3280416022c9SBarry Smith   b->col              = 0;
328182bf6240SBarry Smith   b->icol             = 0;
3282b810aeb4SBarry Smith   b->reallocs         = 0;
328336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3284f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3285416022c9SBarry Smith   b->nonew             = 0;
3286416022c9SBarry Smith   b->diag              = 0;
3287416022c9SBarry Smith   b->solve_work        = 0;
32882a1b7f2aSHong Zhang   B->spptr             = 0;
3289be6bf707SBarry Smith   b->saved_values      = 0;
3290d7f994e1SBarry Smith   b->idiag             = 0;
329171f1c65dSBarry Smith   b->mdiag             = 0;
329271f1c65dSBarry Smith   b->ssor_work         = 0;
329371f1c65dSBarry Smith   b->omega             = 1.0;
329471f1c65dSBarry Smith   b->fshift            = 0.0;
329571f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3296a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3297a30b2313SHong Zhang   b->xtoy              = 0;
3298a30b2313SHong Zhang   b->XtoY              = 0;
329973e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3300d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3301d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3302d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3303d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
330488e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
330517ab2063SBarry Smith 
330635d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3307b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3308b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C",
3309b3866ffcSBarry Smith 					   "MatGetFactor_seqaij_matlab",
3310b3866ffcSBarry Smith 					   MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3311b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3312b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3313b3866ffcSBarry Smith #endif
3314b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3315ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
3316b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3317b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3318b5e56a35SBarry Smith #endif
331965460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3320ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C",
3321719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3322719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3323719d5645SBarry Smith #endif
3324611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3325ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C",
33265c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
33275c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3328611f576cSBarry Smith #endif
3329f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3330ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
3331f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3332f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3333f3c0ef26SHong Zhang #endif
3334611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3335ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
33365c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
33375c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3338611f576cSBarry Smith #endif
3339611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3340ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
3341bccb9932SShri Abhyankar                                      "MatGetFactor_aij_mumps",
3342bccb9932SShri Abhyankar                                      MatGetFactor_aij_mumps);CHKERRQ(ierr);
3343611f576cSBarry Smith #endif
3344eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3345ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C",
3346eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3347eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3348eb3b5408SSatish Balay #endif
3349719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3350ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C",
3351719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3352719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3353719d5645SBarry Smith #endif
3354ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3355b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3356b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3357ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3358db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3359db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3360174acd27SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C",
3361a83b8d76SBarry Smith                                      "MatGetFactor_seqaij_bas",
3362174acd27SBarry Smith                                      MatGetFactor_seqaij_bas);
3363f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3364bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3365bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3366f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3367be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3368bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3369f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3370be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3371bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3372a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3373a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3374a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
337585fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
337685fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
337785fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3378ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3379ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3380ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
338117667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
338217667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
338317667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33845fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33855fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33865fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33871cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33881cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33891cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3390a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3391a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3392a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3393a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3394a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3395a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
339605b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
339705b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
339805b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3399170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3400170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3401170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3402170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3403170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3404170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3405170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3406170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3407170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
34084108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
340917667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
34103a40ed3dSBarry Smith   PetscFunctionReturn(0);
341117ab2063SBarry Smith }
3412273d9f13SBarry Smith EXTERN_C_END
341317ab2063SBarry Smith 
34144a2ae208SSatish Balay #undef __FUNCT__
3415b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3416b24902e0SBarry Smith /*
3417b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3418b24902e0SBarry Smith */
3419f77e22a1SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
342017ab2063SBarry Smith {
3421416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
34226849ba73SBarry Smith   PetscErrorCode ierr;
3423d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
342417ab2063SBarry Smith 
34253a40ed3dSBarry Smith   PetscFunctionBegin;
3426273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3427273d9f13SBarry Smith 
3428d5f3da31SBarry Smith   C->factortype     = A->factortype;
3429416022c9SBarry Smith   c->row            = 0;
3430416022c9SBarry Smith   c->col            = 0;
343182bf6240SBarry Smith   c->icol           = 0;
34326ad4291fSHong Zhang   c->reallocs       = 0;
343317ab2063SBarry Smith 
34346ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
343517ab2063SBarry Smith 
343626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
343726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
343826283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
343926283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3440eec197d1SBarry Smith 
344133b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
34429518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
344317ab2063SBarry Smith   for (i=0; i<m; i++) {
3444416022c9SBarry Smith     c->imax[i] = a->imax[i];
3445416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
344617ab2063SBarry Smith   }
344717ab2063SBarry Smith 
344817ab2063SBarry Smith   /* allocate the matrix space */
3449f77e22a1SHong Zhang   if (mallocmatspace){
3450a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
34519518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3452f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
345397f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
345417ab2063SBarry Smith     if (m > 0) {
345597f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3456be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3457bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3458be6bf707SBarry Smith       } else {
3459bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
346017ab2063SBarry Smith       }
346108480c60SBarry Smith     }
3462f77e22a1SHong Zhang   }
346317ab2063SBarry Smith 
34646ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3465416022c9SBarry Smith   c->roworiented       = a->roworiented;
3466416022c9SBarry Smith   c->nonew             = a->nonew;
3467416022c9SBarry Smith   if (a->diag) {
346897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
346952e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
347017ab2063SBarry Smith     for (i=0; i<m; i++) {
3471416022c9SBarry Smith       c->diag[i] = a->diag[i];
347217ab2063SBarry Smith     }
34733a40ed3dSBarry Smith   } else c->diag           = 0;
34746ad4291fSHong Zhang   c->solve_work            = 0;
34756ad4291fSHong Zhang   c->saved_values          = 0;
34766ad4291fSHong Zhang   c->idiag                 = 0;
347771f1c65dSBarry Smith   c->ssor_work             = 0;
3478a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3479e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3480e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34816ad4291fSHong Zhang   c->xtoy                  = 0;
34826ad4291fSHong Zhang   c->XtoY                  = 0;
34836ad4291fSHong Zhang 
3484416022c9SBarry Smith   c->nz                 = a->nz;
34858ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3486273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3487754ec7b1SSatish Balay 
34886ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34896ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
34906ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
34916ad4291fSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
34926ad4291fSHong Zhang     i = a->compressedrow.nrows;
34930e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
34946ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
34956ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
349627ea64f8SHong Zhang   } else {
349727ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
349827ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
349927ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
35006ad4291fSHong Zhang   }
350188e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
35024108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
35034846f1f5SKris Buschelman 
35047adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
35053a40ed3dSBarry Smith   PetscFunctionReturn(0);
350617ab2063SBarry Smith }
350717ab2063SBarry Smith 
35084a2ae208SSatish Balay #undef __FUNCT__
3509b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3510b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3511b24902e0SBarry Smith {
3512b24902e0SBarry Smith   PetscErrorCode ierr;
3513b24902e0SBarry Smith 
3514b24902e0SBarry Smith   PetscFunctionBegin;
3515b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
35164b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3517b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3518f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3519b24902e0SBarry Smith   PetscFunctionReturn(0);
3520b24902e0SBarry Smith }
3521b24902e0SBarry Smith 
3522b24902e0SBarry Smith #undef __FUNCT__
35234a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3524a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
352517ab2063SBarry Smith {
3526416022c9SBarry Smith   Mat_SeqAIJ     *a;
3527416022c9SBarry Smith   Mat            B;
35286849ba73SBarry Smith   PetscErrorCode ierr;
35293c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
353038baddfdSBarry Smith   int            fd;
353138baddfdSBarry Smith   PetscMPIInt    size;
3532bcd2baecSBarry Smith   MPI_Comm       comm;
353317ab2063SBarry Smith 
35343a40ed3dSBarry Smith   PetscFunctionBegin;
3535e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3536d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3537e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3538b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
35390752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3540e32f2f54SBarry Smith   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
354117ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
354217ab2063SBarry Smith 
3543d64ed03dSBarry Smith   if (nz < 0) {
3544e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3545d64ed03dSBarry Smith   }
3546d64ed03dSBarry Smith 
354717ab2063SBarry Smith   /* read in row lengths */
354897f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
35490752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
355017ab2063SBarry Smith 
35513c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
35523c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3553e32f2f54SBarry Smith   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
35543c601197SSatish Balay 
355517ab2063SBarry Smith   /* create our matrix */
3556f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3557f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3558b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3559ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3560416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
356117ab2063SBarry Smith 
35620752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
356317ab2063SBarry Smith 
356417ab2063SBarry Smith   /* read in nonzero values */
35650752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
356617ab2063SBarry Smith 
356717ab2063SBarry Smith   /* set matrix "i" values */
3568efb16452SHong Zhang   a->i[0] = 0;
356917ab2063SBarry Smith   for (i=1; i<= M; i++) {
3570416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3571416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
357217ab2063SBarry Smith   }
3573606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
357417ab2063SBarry Smith 
35756d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35766d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3577b3a1e11cSKris Buschelman   *A = B;
35783a40ed3dSBarry Smith   PetscFunctionReturn(0);
357917ab2063SBarry Smith }
358017ab2063SBarry Smith 
35814a2ae208SSatish Balay #undef __FUNCT__
3582b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3583dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35847264ac53SSatish Balay {
35857264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3586dfbe8321SBarry Smith   PetscErrorCode ierr;
3587eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3588eeffb40dSHong Zhang   PetscInt k;
3589eeffb40dSHong Zhang #endif
35907264ac53SSatish Balay 
35913a40ed3dSBarry Smith   PetscFunctionBegin;
3592bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3593d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3594ca44d042SBarry Smith     *flg = PETSC_FALSE;
3595ca44d042SBarry Smith     PetscFunctionReturn(0);
3596bcd2baecSBarry Smith   }
35977264ac53SSatish Balay 
35987264ac53SSatish Balay   /* if the a->i are the same */
3599d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3600abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
36017264ac53SSatish Balay 
36027264ac53SSatish Balay   /* if a->j are the same */
360397f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3604abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3605bcd2baecSBarry Smith 
3606bcd2baecSBarry Smith   /* if a->a are the same */
3607eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3608eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3609eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3610eeffb40dSHong Zhang       *flg = PETSC_FALSE;
36113a40ed3dSBarry Smith       PetscFunctionReturn(0);
3612eeffb40dSHong Zhang     }
3613eeffb40dSHong Zhang   }
3614eeffb40dSHong Zhang #else
3615eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3616eeffb40dSHong Zhang #endif
3617eeffb40dSHong Zhang   PetscFunctionReturn(0);
36187264ac53SSatish Balay }
361936db0b34SBarry Smith 
36204a2ae208SSatish Balay #undef __FUNCT__
36214a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
362205869f15SSatish Balay /*@
362336db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
362436db0b34SBarry Smith               provided by the user.
362536db0b34SBarry Smith 
3626c75a6043SHong Zhang       Collective on MPI_Comm
362736db0b34SBarry Smith 
362836db0b34SBarry Smith    Input Parameters:
362936db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
363036db0b34SBarry Smith .   m - number of rows
363136db0b34SBarry Smith .   n - number of columns
363236db0b34SBarry Smith .   i - row indices
363336db0b34SBarry Smith .   j - column indices
363436db0b34SBarry Smith -   a - matrix values
363536db0b34SBarry Smith 
363636db0b34SBarry Smith    Output Parameter:
363736db0b34SBarry Smith .   mat - the matrix
363836db0b34SBarry Smith 
363936db0b34SBarry Smith    Level: intermediate
364036db0b34SBarry Smith 
364136db0b34SBarry Smith    Notes:
36420551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
364336db0b34SBarry Smith     once the matrix is destroyed
364436db0b34SBarry Smith 
364536db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
364636db0b34SBarry Smith 
3647bfeeae90SHong Zhang        The i and j indices are 0 based
364836db0b34SBarry Smith 
3649a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3650a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3651a4552177SSatish Balay     as shown:
3652a4552177SSatish Balay 
3653a4552177SSatish Balay         1 0 0
3654a4552177SSatish Balay         2 0 3
3655a4552177SSatish Balay         4 5 6
3656a4552177SSatish Balay 
3657a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
36589985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3659a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3660a4552177SSatish Balay 
36619985e31cSBarry Smith 
36622fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
366336db0b34SBarry Smith 
366436db0b34SBarry Smith @*/
3665a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
366636db0b34SBarry Smith {
3667dfbe8321SBarry Smith   PetscErrorCode ierr;
3668cbcfb4deSHong Zhang   PetscInt       ii;
366936db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3670cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3671cbcfb4deSHong Zhang   PetscInt       jj;
3672cbcfb4deSHong Zhang #endif
367336db0b34SBarry Smith 
367436db0b34SBarry Smith   PetscFunctionBegin;
3675a96a251dSBarry Smith   if (i[0]) {
3676e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
367736db0b34SBarry Smith   }
3678f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3679f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3680ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3681ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3682ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3683ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3684ab93d7beSBarry Smith 
368536db0b34SBarry Smith   aij->i = i;
368636db0b34SBarry Smith   aij->j = j;
368736db0b34SBarry Smith   aij->a = a;
368836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
368936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3690e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3691e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
369236db0b34SBarry Smith 
369336db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
369436db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
36952515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
3696e32f2f54SBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
36979985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
3698e32f2f54SBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
3699e32f2f54SBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
37009985e31cSBarry Smith     }
370136db0b34SBarry Smith #endif
370236db0b34SBarry Smith   }
37032515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
370436db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3705e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3706e32f2f54SBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
370736db0b34SBarry Smith   }
370836db0b34SBarry Smith #endif
370936db0b34SBarry Smith 
3710b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3711b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
371236db0b34SBarry Smith   PetscFunctionReturn(0);
371336db0b34SBarry Smith }
371436db0b34SBarry Smith 
3715cc8ba8e1SBarry Smith #undef __FUNCT__
3716ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3717dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3718cc8ba8e1SBarry Smith {
3719dfbe8321SBarry Smith   PetscErrorCode ierr;
3720cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
372136db0b34SBarry Smith 
3722cc8ba8e1SBarry Smith   PetscFunctionBegin;
37238ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3724cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3725cc8ba8e1SBarry Smith     a->coloring = coloring;
372612c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
372797f1f81fSBarry Smith     PetscInt             i,*larray;
372812c595b3SBarry Smith     ISColoring      ocoloring;
372908b6dcc0SBarry Smith     ISColoringValue *colors;
373012c595b3SBarry Smith 
373112c595b3SBarry Smith     /* set coloring for diagonal portion */
37320e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3733d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
373412c595b3SBarry Smith       larray[i] = i;
373512c595b3SBarry Smith     }
3736d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
37370e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3738d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
373912c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
374012c595b3SBarry Smith     }
374112c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3742d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
374312c595b3SBarry Smith     a->coloring = ocoloring;
374412c595b3SBarry Smith   }
3745cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3746cc8ba8e1SBarry Smith }
3747cc8ba8e1SBarry Smith 
3748dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3749ee4f033dSBarry Smith EXTERN_C_BEGIN
375029c1e371SBarry Smith #include "adic/ad_utils.h"
3751ee4f033dSBarry Smith EXTERN_C_END
3752cc8ba8e1SBarry Smith 
3753cc8ba8e1SBarry Smith #undef __FUNCT__
3754ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3755dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3756cc8ba8e1SBarry Smith {
3757cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3758d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
37594440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
376008b6dcc0SBarry Smith   ISColoringValue *color;
3761cc8ba8e1SBarry Smith 
3762cc8ba8e1SBarry Smith   PetscFunctionBegin;
3763e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
37644440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3765cc8ba8e1SBarry Smith   color = a->coloring->colors;
3766cc8ba8e1SBarry Smith   /* loop over rows */
3767cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3768cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3769cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3770cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3771cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3772cc8ba8e1SBarry Smith     }
37734440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3774ee4f033dSBarry Smith   }
3775ee4f033dSBarry Smith   PetscFunctionReturn(0);
3776ee4f033dSBarry Smith }
3777ee4f033dSBarry Smith #endif
3778ee4f033dSBarry Smith 
3779ee4f033dSBarry Smith #undef __FUNCT__
3780ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
378197f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3782ee4f033dSBarry Smith {
3783ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3784d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
378554f21887SBarry Smith   MatScalar       *v = a->a;
378654f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
378708b6dcc0SBarry Smith   ISColoringValue *color;
3788ee4f033dSBarry Smith 
3789ee4f033dSBarry Smith   PetscFunctionBegin;
3790e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3791ee4f033dSBarry Smith   color = a->coloring->colors;
3792ee4f033dSBarry Smith   /* loop over rows */
3793ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3794ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3795ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3796ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3797ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3798ee4f033dSBarry Smith     }
3799ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3800cc8ba8e1SBarry Smith   }
3801cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3802cc8ba8e1SBarry Smith }
380336db0b34SBarry Smith 
380481824310SBarry Smith /*
380581824310SBarry Smith     Special version for direct calls from Fortran
380681824310SBarry Smith */
380781824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
380881824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
380981824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
381081824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
381181824310SBarry Smith #endif
381281824310SBarry Smith 
381381824310SBarry Smith /* Change these macros so can be used in void function */
381481824310SBarry Smith #undef CHKERRQ
38157adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
381681824310SBarry Smith #undef SETERRQ2
3817e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
381881824310SBarry Smith 
381981824310SBarry Smith EXTERN_C_BEGIN
382081824310SBarry Smith #undef __FUNCT__
382181824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
38221f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
382381824310SBarry Smith {
382481824310SBarry Smith   Mat            A = *AA;
382581824310SBarry Smith   PetscInt       m = *mm, n = *nn;
382681824310SBarry Smith   InsertMode     is = *isis;
382781824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
382881824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
382981824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
383081824310SBarry Smith   PetscErrorCode ierr;
383181824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
383254f21887SBarry Smith   MatScalar      *ap,value,*aa;
3833edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
383481824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
383581824310SBarry Smith 
383681824310SBarry Smith   PetscFunctionBegin;
3837d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
383881824310SBarry Smith   imax = a->imax;
383981824310SBarry Smith   ai = a->i;
384081824310SBarry Smith   ailen = a->ilen;
384181824310SBarry Smith   aj = a->j;
384281824310SBarry Smith   aa = a->a;
384381824310SBarry Smith 
384481824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
384581824310SBarry Smith     row  = im[k];
384681824310SBarry Smith     if (row < 0) continue;
384781824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3848d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
384981824310SBarry Smith #endif
385081824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
385181824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
385281824310SBarry Smith     low  = 0;
385381824310SBarry Smith     high = nrow;
385481824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
385581824310SBarry Smith       if (in[l] < 0) continue;
385681824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3857d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
385881824310SBarry Smith #endif
385981824310SBarry Smith       col = in[l];
386081824310SBarry Smith       if (roworiented) {
386181824310SBarry Smith         value = v[l + k*n];
386281824310SBarry Smith       } else {
386381824310SBarry Smith         value = v[k + l*m];
386481824310SBarry Smith       }
386581824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
386681824310SBarry Smith 
386781824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
386881824310SBarry Smith       lastcol = col;
386981824310SBarry Smith       while (high-low > 5) {
387081824310SBarry Smith         t = (low+high)/2;
387181824310SBarry Smith         if (rp[t] > col) high = t;
387281824310SBarry Smith         else             low  = t;
387381824310SBarry Smith       }
387481824310SBarry Smith       for (i=low; i<high; i++) {
387581824310SBarry Smith         if (rp[i] > col) break;
387681824310SBarry Smith         if (rp[i] == col) {
387781824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
387881824310SBarry Smith           else                  ap[i] = value;
387981824310SBarry Smith           goto noinsert;
388081824310SBarry Smith         }
388181824310SBarry Smith       }
388281824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
388381824310SBarry Smith       if (nonew == 1) goto noinsert;
38847adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3885d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
388681824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
388781824310SBarry Smith       /* shift up all the later entries in this row */
388881824310SBarry Smith       for (ii=N; ii>=i; ii--) {
388981824310SBarry Smith         rp[ii+1] = rp[ii];
389081824310SBarry Smith         ap[ii+1] = ap[ii];
389181824310SBarry Smith       }
389281824310SBarry Smith       rp[i] = col;
389381824310SBarry Smith       ap[i] = value;
389481824310SBarry Smith       noinsert:;
389581824310SBarry Smith       low = i + 1;
389681824310SBarry Smith     }
389781824310SBarry Smith     ailen[row] = nrow;
389881824310SBarry Smith   }
389981824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
390081824310SBarry Smith   PetscFunctionReturnVoid();
390181824310SBarry Smith }
390281824310SBarry Smith EXTERN_C_END
3903