xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 16b2e9dcd0dd739b344ea4df27bb36f99e30329f)
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;
23ace3abfcSBarry Smith   PetscBool      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"
50ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *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"
80ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *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"
95ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *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"
137ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *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 
1634a2ae208SSatish Balay #undef __FUNCT__
1644a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16597f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16617ab2063SBarry Smith {
167416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
168e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
16997f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1706849ba73SBarry Smith   PetscErrorCode ierr;
171e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17254f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
173ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
174ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
17517ab2063SBarry Smith 
1763a40ed3dSBarry Smith   PetscFunctionBegin;
17771fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
17817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
179416022c9SBarry Smith     row  = im[k];
1805ef9f2a5SBarry Smith     if (row < 0) continue;
1812515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
182e32f2f54SBarry 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);
1833b2fbd54SBarry Smith #endif
184bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
186416022c9SBarry Smith     low  = 0;
187c71e6ed7SBarry Smith     high = nrow;
18817ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1895ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1902515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
191e32f2f54SBarry 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);
1923b2fbd54SBarry Smith #endif
193bfeeae90SHong Zhang       col = in[l];
19416371a99SBarry Smith       if (v) {
1954b0e389bSBarry Smith 	if (roworiented) {
1965ef9f2a5SBarry Smith 	  value = v[l + k*n];
197bef8e0ddSBarry Smith 	} else {
1984b0e389bSBarry Smith 	  value = v[k + l*m];
1994b0e389bSBarry Smith 	}
20016371a99SBarry Smith       } else {
20175567043SBarry Smith         value = 0.;
20216371a99SBarry Smith       }
203abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
20436db0b34SBarry Smith 
2057cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
206e2ee6c50SBarry Smith       lastcol = col;
207416022c9SBarry Smith       while (high-low > 5) {
208416022c9SBarry Smith         t = (low+high)/2;
209416022c9SBarry Smith         if (rp[t] > col) high = t;
210416022c9SBarry Smith         else             low  = t;
21117ab2063SBarry Smith       }
212416022c9SBarry Smith       for (i=low; i<high; i++) {
21317ab2063SBarry Smith         if (rp[i] > col) break;
21417ab2063SBarry Smith         if (rp[i] == col) {
215416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
21617ab2063SBarry Smith           else                  ap[i] = value;
217e44c0bd4SBarry Smith           low = i + 1;
21817ab2063SBarry Smith           goto noinsert;
21917ab2063SBarry Smith         }
22017ab2063SBarry Smith       }
221abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
222c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
223e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
224fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
225c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
226416022c9SBarry Smith       /* shift up all the later entries in this row */
227416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
22817ab2063SBarry Smith         rp[ii+1] = rp[ii];
22917ab2063SBarry Smith         ap[ii+1] = ap[ii];
23017ab2063SBarry Smith       }
23117ab2063SBarry Smith       rp[i] = col;
23217ab2063SBarry Smith       ap[i] = value;
233416022c9SBarry Smith       low   = i + 1;
234e44c0bd4SBarry Smith       noinsert:;
23517ab2063SBarry Smith     }
23617ab2063SBarry Smith     ailen[row] = nrow;
23717ab2063SBarry Smith   }
23888e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2393a40ed3dSBarry Smith   PetscFunctionReturn(0);
24017ab2063SBarry Smith }
24117ab2063SBarry Smith 
24281824310SBarry Smith 
2434a2ae208SSatish Balay #undef __FUNCT__
2444a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
245a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2467eb43aa7SLois Curfman McInnes {
2477eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
24897f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
24997f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
25054f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2517eb43aa7SLois Curfman McInnes 
2523a40ed3dSBarry Smith   PetscFunctionBegin;
2537eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2547eb43aa7SLois Curfman McInnes     row  = im[k];
255e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
256e32f2f54SBarry 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);
257bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2587eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2597eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
260e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
261e32f2f54SBarry 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);
262bfeeae90SHong Zhang       col = in[l] ;
2637eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2647eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2657eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2667eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2677eb43aa7SLois Curfman McInnes         else             low  = t;
2687eb43aa7SLois Curfman McInnes       }
2697eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2707eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2717eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
272b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2737eb43aa7SLois Curfman McInnes           goto finished;
2747eb43aa7SLois Curfman McInnes         }
2757eb43aa7SLois Curfman McInnes       }
27697e567efSBarry Smith       *v++ = 0.0;
2777eb43aa7SLois Curfman McInnes       finished:;
2787eb43aa7SLois Curfman McInnes     }
2797eb43aa7SLois Curfman McInnes   }
2803a40ed3dSBarry Smith   PetscFunctionReturn(0);
2817eb43aa7SLois Curfman McInnes }
2827eb43aa7SLois Curfman McInnes 
28317ab2063SBarry Smith 
2844a2ae208SSatish Balay #undef __FUNCT__
2854a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
286dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28717ab2063SBarry Smith {
288416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2896849ba73SBarry Smith   PetscErrorCode ierr;
2906f69ff64SBarry Smith   PetscInt       i,*col_lens;
2916f69ff64SBarry Smith   int            fd;
29217ab2063SBarry Smith 
2933a40ed3dSBarry Smith   PetscFunctionBegin;
294b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
295d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
2960700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
297d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
298d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
299416022c9SBarry Smith   col_lens[3] = a->nz;
300416022c9SBarry Smith 
301416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
302d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
303416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30417ab2063SBarry Smith   }
305d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
306606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
307416022c9SBarry Smith 
308416022c9SBarry Smith   /* store column indices (zero start index) */
3096f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
310416022c9SBarry Smith 
311416022c9SBarry Smith   /* store nonzero values */
3126f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3133a40ed3dSBarry Smith   PetscFunctionReturn(0);
31417ab2063SBarry Smith }
315416022c9SBarry Smith 
316dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
317cd155464SBarry Smith 
3184a2ae208SSatish Balay #undef __FUNCT__
3194a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
320dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
321416022c9SBarry Smith {
322416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
323dfbe8321SBarry Smith   PetscErrorCode    ierr;
324d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
325e060cb09SBarry Smith   const char        *name;
326f3ef73ceSBarry Smith   PetscViewerFormat format;
32717ab2063SBarry Smith 
3283a40ed3dSBarry Smith   PetscFunctionBegin;
329435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
330b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33171c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33297f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
333d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
334d00d2cf4SBarry Smith       nofinalvalue = 1;
335d00d2cf4SBarry Smith     }
336b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
337d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
33877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
33977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
340b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34117ab2063SBarry Smith 
34217ab2063SBarry Smith     for (i=0; i<m; i++) {
343416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
344aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34577431f27SBarry 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);
34617ab2063SBarry Smith #else
34777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34817ab2063SBarry Smith #endif
34917ab2063SBarry Smith       }
35017ab2063SBarry Smith     }
351d00d2cf4SBarry Smith     if (nofinalvalue) {
352d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
353d00d2cf4SBarry Smith     }
354fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
355b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35668369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
357cd155464SBarry Smith      PetscFunctionReturn(0);
358fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
359b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36044cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36177431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36244cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
363aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36436db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
365a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36636db0b34SBarry Smith         } else 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 (PetscRealPart(a->a[j]) != 0.0) {
369a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3706831982aSBarry Smith         }
37144cd7ae7SLois Curfman McInnes #else
372a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37344cd7ae7SLois Curfman McInnes #endif
37444cd7ae7SLois Curfman McInnes       }
375b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37644cd7ae7SLois Curfman McInnes     }
377b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
378fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
37997f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
380b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
38197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
382496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
383496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
384496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
385496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
386aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
388496be53dSLois Curfman McInnes #else
389496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
390496be53dSLois Curfman McInnes #endif
391496be53dSLois Curfman McInnes         }
392496be53dSLois Curfman McInnes       }
393496be53dSLois Curfman McInnes     }
3942e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3962e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39777431f27SBarry 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);}
39877431f27SBarry 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);}
39977431f27SBarry 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);}
40077431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40177431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40277431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
403496be53dSLois Curfman McInnes     }
404b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
405606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
406496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
407496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
40877431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
409496be53dSLois Curfman McInnes       }
410b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
411496be53dSLois Curfman McInnes     }
412b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
413496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
414496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
415496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
416aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
418b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4196831982aSBarry Smith           }
420496be53dSLois Curfman McInnes #else
421b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
422496be53dSLois Curfman McInnes #endif
423496be53dSLois Curfman McInnes         }
424496be53dSLois Curfman McInnes       }
425b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
426496be53dSLois Curfman McInnes     }
427b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
428fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
42997f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
43087828ca2SBarry Smith     PetscScalar value;
43102594712SBarry Smith 
432b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43302594712SBarry Smith     for (i=0; i<m; i++) {
43402594712SBarry Smith       jcnt = 0;
435d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
436e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43702594712SBarry Smith           value = a->a[cnt++];
438e24b481bSBarry Smith           jcnt++;
43902594712SBarry Smith         } else {
44002594712SBarry Smith           value = 0.0;
44102594712SBarry Smith         }
442aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
443b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44402594712SBarry Smith #else
445b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44602594712SBarry Smith #endif
44702594712SBarry Smith       }
448b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44902594712SBarry Smith     }
450b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4513c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4523c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4533c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4543c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4553c215bfdSMatthew Knepley #else
4563c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4573c215bfdSMatthew Knepley #endif
458d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4593c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4603c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4613c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4623c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4633c215bfdSMatthew 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);
4643c215bfdSMatthew Knepley         } else 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 {
4673c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4683c215bfdSMatthew Knepley         }
4693c215bfdSMatthew Knepley #else
4703c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4713c215bfdSMatthew Knepley #endif
4723c215bfdSMatthew Knepley       }
4733c215bfdSMatthew Knepley     }
4743c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4753a40ed3dSBarry Smith   } else {
476d6e78c31SSatish Balay     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
477d5f3da31SBarry Smith     if (A->factortype){
47816cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
47916cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
48016cd7e1dSShri Abhyankar         /* L part */
48116cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
48216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
48316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
48416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48516cd7e1dSShri Abhyankar           } else 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 {
48816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
48916cd7e1dSShri Abhyankar           }
49016cd7e1dSShri Abhyankar #else
49116cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49216cd7e1dSShri Abhyankar #endif
49316cd7e1dSShri Abhyankar         }
49416cd7e1dSShri Abhyankar 	/* diagonal */
49516cd7e1dSShri Abhyankar 	j = a->diag[i];
49616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
49716cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
49816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
49916cd7e1dSShri Abhyankar           } else 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 {
50216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
50316cd7e1dSShri Abhyankar           }
50416cd7e1dSShri Abhyankar #else
50516cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
50616cd7e1dSShri Abhyankar #endif
50716cd7e1dSShri Abhyankar 
50816cd7e1dSShri Abhyankar 	/* U part */
50916cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
51016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
51116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
51216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51316cd7e1dSShri Abhyankar           } else 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 {
51616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
51716cd7e1dSShri Abhyankar           }
51816cd7e1dSShri Abhyankar #else
51916cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
52016cd7e1dSShri Abhyankar #endif
52116cd7e1dSShri Abhyankar }
52216cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
52316cd7e1dSShri Abhyankar         }
52416cd7e1dSShri Abhyankar     } else {
52517ab2063SBarry Smith       for (i=0; i<m; i++) {
52677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
527416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
528aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
530a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
53136db0b34SBarry Smith           } else 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);
5333a40ed3dSBarry Smith           } else {
534a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
53517ab2063SBarry Smith           }
53617ab2063SBarry Smith #else
537a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
53817ab2063SBarry Smith #endif
53917ab2063SBarry Smith         }
540b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
54117ab2063SBarry Smith       }
54216cd7e1dSShri Abhyankar     }
543b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
54417ab2063SBarry Smith   }
545b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5463a40ed3dSBarry Smith   PetscFunctionReturn(0);
547416022c9SBarry Smith }
548416022c9SBarry Smith 
5494a2ae208SSatish Balay #undef __FUNCT__
5504a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
551dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
552416022c9SBarry Smith {
553480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
554416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
555dfbe8321SBarry Smith   PetscErrorCode    ierr;
556d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
55736db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
558b0a32e0cSBarry Smith   PetscViewer       viewer;
559f3ef73ceSBarry Smith   PetscViewerFormat format;
560cddf8d76SBarry Smith 
5613a40ed3dSBarry Smith   PetscFunctionBegin;
562480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
563b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
56419bcc07fSBarry Smith 
565b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
566416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5670513a670SBarry Smith 
568fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5690513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
570b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
571416022c9SBarry Smith     for (i=0; i<m; i++) {
572cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
573bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
574bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
575aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
57636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
577cddf8d76SBarry Smith #else
578cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
579cddf8d76SBarry Smith #endif
580b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
581cddf8d76SBarry Smith       }
582cddf8d76SBarry Smith     }
583b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
584cddf8d76SBarry Smith     for (i=0; i<m; i++) {
585cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
586bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
587bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
588cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
589b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
590cddf8d76SBarry Smith       }
591cddf8d76SBarry Smith     }
592b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
593cddf8d76SBarry Smith     for (i=0; i<m; i++) {
594cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
595bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
596bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
597aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
59836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
599cddf8d76SBarry Smith #else
600cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
601cddf8d76SBarry Smith #endif
602b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
603416022c9SBarry Smith       }
604416022c9SBarry Smith     }
6050513a670SBarry Smith   } else {
6060513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
6070513a670SBarry Smith     /* first determine max of all nonzero values */
60897f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
609b0a32e0cSBarry Smith     PetscDraw   popup;
61036db0b34SBarry Smith     PetscReal scale;
6110513a670SBarry Smith 
6120513a670SBarry Smith     for (i=0; i<nz; i++) {
6130513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
6140513a670SBarry Smith     }
615b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
616b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
617b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
6180513a670SBarry Smith     count = 0;
6190513a670SBarry Smith     for (i=0; i<m; i++) {
6200513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
621bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
622bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
62397f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
624b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
6250513a670SBarry Smith         count++;
6260513a670SBarry Smith       }
6270513a670SBarry Smith     }
6280513a670SBarry Smith   }
629480ef9eaSBarry Smith   PetscFunctionReturn(0);
630480ef9eaSBarry Smith }
631cddf8d76SBarry Smith 
6324a2ae208SSatish Balay #undef __FUNCT__
6334a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
634dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
635480ef9eaSBarry Smith {
636dfbe8321SBarry Smith   PetscErrorCode ierr;
637b0a32e0cSBarry Smith   PetscDraw      draw;
63836db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
639ace3abfcSBarry Smith   PetscBool      isnull;
640480ef9eaSBarry Smith 
641480ef9eaSBarry Smith   PetscFunctionBegin;
642b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
643b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
644480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
645480ef9eaSBarry Smith 
646480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
647d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
648480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
649b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
650b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
651480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6523a40ed3dSBarry Smith   PetscFunctionReturn(0);
653416022c9SBarry Smith }
654416022c9SBarry Smith 
6554a2ae208SSatish Balay #undef __FUNCT__
6564a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
657dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
658416022c9SBarry Smith {
659dfbe8321SBarry Smith   PetscErrorCode ierr;
660ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
661416022c9SBarry Smith 
6623a40ed3dSBarry Smith   PetscFunctionBegin;
6632692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6642692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
6652692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
666c45a1595SBarry Smith   if (iascii) {
6673a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6680f5bd95cSBarry Smith   } else if (isbinary) {
6693a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6700f5bd95cSBarry Smith   } else if (isdraw) {
6713a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6725cd90555SBarry Smith   } else {
673e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
67417ab2063SBarry Smith   }
6754108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
6763a40ed3dSBarry Smith   PetscFunctionReturn(0);
67717ab2063SBarry Smith }
67819bcc07fSBarry Smith 
6794a2ae208SSatish Balay #undef __FUNCT__
6804a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
681dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
68217ab2063SBarry Smith {
683416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6846849ba73SBarry Smith   PetscErrorCode ierr;
68597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
686d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
68754f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6883447b6efSHong Zhang   PetscReal      ratio=0.6;
68917ab2063SBarry Smith 
6903a40ed3dSBarry Smith   PetscFunctionBegin;
6913a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
69217ab2063SBarry Smith 
69343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
69417ab2063SBarry Smith   for (i=1; i<m; i++) {
695416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
69617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
69794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
69817ab2063SBarry Smith     if (fshift) {
699bfeeae90SHong Zhang       ip = aj + ai[i] ;
700bfeeae90SHong Zhang       ap = aa + ai[i] ;
70117ab2063SBarry Smith       N  = ailen[i];
70217ab2063SBarry Smith       for (j=0; j<N; j++) {
70317ab2063SBarry Smith         ip[j-fshift] = ip[j];
70417ab2063SBarry Smith         ap[j-fshift] = ap[j];
70517ab2063SBarry Smith       }
70617ab2063SBarry Smith     }
70717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
70817ab2063SBarry Smith   }
70917ab2063SBarry Smith   if (m) {
71017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
71117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
71217ab2063SBarry Smith   }
71317ab2063SBarry Smith   /* reset ilen and imax for each row */
71417ab2063SBarry Smith   for (i=0; i<m; i++) {
71517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
71617ab2063SBarry Smith   }
717bfeeae90SHong Zhang   a->nz = ai[m];
71865e19b50SBarry 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);
71917ab2063SBarry Smith 
72009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
721d0f46423SBarry 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);
722ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
723ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
7248e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
725dd5f02e7SSatish Balay   a->reallocs          = 0;
7264e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
72736db0b34SBarry Smith   a->rmax              = rmax;
7284e220ebcSLois Curfman McInnes 
729cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
730317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
73188e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
73271c2f376SKris Buschelman 
7334108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
73471f1c65dSBarry Smith 
73571f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
7363a40ed3dSBarry Smith   PetscFunctionReturn(0);
73717ab2063SBarry Smith }
73817ab2063SBarry Smith 
7394a2ae208SSatish Balay #undef __FUNCT__
74099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
74199cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
74299cafbc1SBarry Smith {
74399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
74499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
74554f21887SBarry Smith   MatScalar      *aa = a->a;
74699cafbc1SBarry Smith 
74799cafbc1SBarry Smith   PetscFunctionBegin;
74899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
74999cafbc1SBarry Smith   PetscFunctionReturn(0);
75099cafbc1SBarry Smith }
75199cafbc1SBarry Smith 
75299cafbc1SBarry Smith #undef __FUNCT__
75399cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
75499cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
75599cafbc1SBarry Smith {
75699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
75799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
75854f21887SBarry Smith   MatScalar      *aa = a->a;
75999cafbc1SBarry Smith 
76099cafbc1SBarry Smith   PetscFunctionBegin;
76199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
76299cafbc1SBarry Smith   PetscFunctionReturn(0);
76399cafbc1SBarry Smith }
76499cafbc1SBarry Smith 
76599cafbc1SBarry Smith #undef __FUNCT__
7664a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
767dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
76817ab2063SBarry Smith {
769416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
770dfbe8321SBarry Smith   PetscErrorCode ierr;
7713a40ed3dSBarry Smith 
7723a40ed3dSBarry Smith   PetscFunctionBegin;
773d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7743a40ed3dSBarry Smith   PetscFunctionReturn(0);
77517ab2063SBarry Smith }
776416022c9SBarry Smith 
7774a2ae208SSatish Balay #undef __FUNCT__
7784a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
779dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
78017ab2063SBarry Smith {
781416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
782dfbe8321SBarry Smith   PetscErrorCode ierr;
783d5d45c9bSBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785aa482453SBarry Smith #if defined(PETSC_USE_LOG)
786d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
78717ab2063SBarry Smith #endif
788e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
789c38d4ed2SBarry Smith   if (a->row) {
790c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
791c38d4ed2SBarry Smith   }
792c38d4ed2SBarry Smith   if (a->col) {
793c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
794c38d4ed2SBarry Smith   }
79505b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
79605b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
79771f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
79805b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
79982bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
80005b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
801cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
80205b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
803407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
8040e83c824SBarry Smith   if (a->compressedrow.checked && a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);}
805a30b2313SHong Zhang 
8064108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
8074846f1f5SKris Buschelman 
808606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
809901853e0SKris Buschelman 
810dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
811901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
812901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
813901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
814901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
815901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
8165a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
817901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
818901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
819a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
820901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
8213a40ed3dSBarry Smith   PetscFunctionReturn(0);
82217ab2063SBarry Smith }
82317ab2063SBarry Smith 
8244a2ae208SSatish Balay #undef __FUNCT__
8254a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
826ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
82717ab2063SBarry Smith {
828416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8294846f1f5SKris Buschelman   PetscErrorCode ierr;
8303a40ed3dSBarry Smith 
8313a40ed3dSBarry Smith   PetscFunctionBegin;
832a65d3064SKris Buschelman   switch (op) {
833a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
8344e0d8c25SBarry Smith       a->roworiented       = flg;
835a65d3064SKris Buschelman       break;
836a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
837a9817697SBarry Smith       a->keepnonzeropattern    = flg;
838a65d3064SKris Buschelman       break;
839512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
840512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
841a65d3064SKris Buschelman       break;
842a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
8434e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
844a65d3064SKris Buschelman       break;
845a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8464e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
847a65d3064SKris Buschelman       break;
84828b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
84928b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
85028b2fa4aSMatthew Knepley       break;
851a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8524e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8530df259c2SBarry Smith       break;
854d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8554e0d8c25SBarry Smith       a->compressedrow.use = flg;
856d487561eSHong Zhang       break;
8573d472b54SHong Zhang     case MAT_SPD:
8583d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
8593d472b54SHong Zhang       A->spd                             = flg;
8603d472b54SHong Zhang       if (flg) {
8613d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
8623d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
8633d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
8643d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
8653d472b54SHong Zhang       }
8663d472b54SHong Zhang       break;
867b1646e73SJed Brown     case MAT_SYMMETRIC:
868b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
869b1646e73SJed Brown     case MAT_HERMITIAN:
870b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
8714e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
872a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
873a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
874290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
875a65d3064SKris Buschelman       break;
876b87ac2d8SJed Brown     case MAT_USE_INODES:
877b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
878b87ac2d8SJed Brown       break;
879a65d3064SKris Buschelman     default:
880e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
881a65d3064SKris Buschelman   }
8824108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
8833a40ed3dSBarry Smith   PetscFunctionReturn(0);
88417ab2063SBarry Smith }
88517ab2063SBarry Smith 
8864a2ae208SSatish Balay #undef __FUNCT__
8874a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
888dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
88917ab2063SBarry Smith {
890416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8916849ba73SBarry Smith   PetscErrorCode ierr;
892d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
89335e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
89417ab2063SBarry Smith 
8953a40ed3dSBarry Smith   PetscFunctionBegin;
896d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
897e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
89835e7444dSHong Zhang 
899d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
900d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
90135e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
90235e7444dSHong Zhang     for (i=0; i<n; i++) x[i] = aa[diag[i]];
90335e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
90435e7444dSHong Zhang     PetscFunctionReturn(0);
90535e7444dSHong Zhang   }
90635e7444dSHong Zhang 
9072dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
9081ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
90935e7444dSHong Zhang   for (i=0; i<n; i++) {
91035e7444dSHong Zhang     nz = ai[i+1] - ai[i];
9112f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
91235e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
91335e7444dSHong Zhang       if (aj[j] == i) {
91435e7444dSHong Zhang         x[i] = aa[j];
91517ab2063SBarry Smith         break;
91617ab2063SBarry Smith       }
91717ab2063SBarry Smith     }
91817ab2063SBarry Smith   }
9191ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
9203a40ed3dSBarry Smith   PetscFunctionReturn(0);
92117ab2063SBarry Smith }
92217ab2063SBarry Smith 
923b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9244a2ae208SSatish Balay #undef __FUNCT__
9254a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
926dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
92717ab2063SBarry Smith {
928416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
9295c897100SBarry Smith   PetscScalar       *x,*y;
930dfbe8321SBarry Smith   PetscErrorCode    ierr;
931d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
9325c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
933a77337e4SBarry Smith   MatScalar         *v;
934a77337e4SBarry Smith   PetscScalar       alpha;
93504fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
9363447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
937ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
9385c897100SBarry Smith #endif
93917ab2063SBarry Smith 
9403a40ed3dSBarry Smith   PetscFunctionBegin;
9412e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
9421ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9431ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9445c897100SBarry Smith 
9455c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
946bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
9475c897100SBarry Smith #else
9483447b6efSHong Zhang   if (usecprow){
9493447b6efSHong Zhang     m    = cprow.nrows;
9503447b6efSHong Zhang     ii   = cprow.i;
9517b2bb3b9SHong Zhang     ridx = cprow.rindex;
9523447b6efSHong Zhang   } else {
9533447b6efSHong Zhang     ii = a->i;
9543447b6efSHong Zhang   }
95517ab2063SBarry Smith   for (i=0; i<m; i++) {
9563447b6efSHong Zhang     idx   = a->j + ii[i] ;
9573447b6efSHong Zhang     v     = a->a + ii[i] ;
9583447b6efSHong Zhang     n     = ii[i+1] - ii[i];
9593447b6efSHong Zhang     if (usecprow){
9607b2bb3b9SHong Zhang       alpha = x[ridx[i]];
9613447b6efSHong Zhang     } else {
96217ab2063SBarry Smith       alpha = x[i];
9633447b6efSHong Zhang     }
96404fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
96517ab2063SBarry Smith   }
9665c897100SBarry Smith #endif
967dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
9681ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9691ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9703a40ed3dSBarry Smith   PetscFunctionReturn(0);
97117ab2063SBarry Smith }
97217ab2063SBarry Smith 
9734a2ae208SSatish Balay #undef __FUNCT__
9745c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
975dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9765c897100SBarry Smith {
977dfbe8321SBarry Smith   PetscErrorCode ierr;
9785c897100SBarry Smith 
9795c897100SBarry Smith   PetscFunctionBegin;
980170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9815c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9825c897100SBarry Smith   PetscFunctionReturn(0);
9835c897100SBarry Smith }
9845c897100SBarry Smith 
985a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9865c897100SBarry Smith #undef __FUNCT__
9874a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
988dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
98917ab2063SBarry Smith {
990416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
991d9fead3dSBarry Smith   PetscScalar       *y;
99254f21887SBarry Smith   const PetscScalar *x;
99354f21887SBarry Smith   const MatScalar   *aa;
994dfbe8321SBarry Smith   PetscErrorCode    ierr;
995003131ecSBarry Smith   PetscInt          m=A->rmap->n;
996003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9978aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
998362ced78SSatish Balay   PetscScalar       sum;
999ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
100017ab2063SBarry Smith 
1001b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
100297952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1003fee21e36SBarry Smith #endif
1004fee21e36SBarry Smith 
10053a40ed3dSBarry Smith   PetscFunctionBegin;
10063649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
10071ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
100897952fefSHong Zhang   aj  = a->j;
100997952fefSHong Zhang   aa  = a->a;
1010416022c9SBarry Smith   ii  = a->i;
10114eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
101297952fefSHong Zhang     m    = a->compressedrow.nrows;
101397952fefSHong Zhang     ii   = a->compressedrow.i;
101497952fefSHong Zhang     ridx = a->compressedrow.rindex;
101597952fefSHong Zhang     for (i=0; i<m; i++){
101697952fefSHong Zhang       n   = ii[i+1] - ii[i];
101797952fefSHong Zhang       aj  = a->j + ii[i];
101897952fefSHong Zhang       aa  = a->a + ii[i];
101997952fefSHong Zhang       sum = 0.0;
1020a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1021003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1022003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
102397952fefSHong Zhang       y[*ridx++] = sum;
102497952fefSHong Zhang     }
102597952fefSHong Zhang   } else { /* do not use compressed row format */
1026b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1027b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1028b05257ddSBarry Smith #else
102917ab2063SBarry Smith     for (i=0; i<m; i++) {
1030003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1031003131ecSBarry Smith       aj  = a->j + ii[i];
1032003131ecSBarry Smith       aa  = a->a + ii[i];
103317ab2063SBarry Smith       sum  = 0.0;
1034a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1035003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
103617ab2063SBarry Smith       y[i] = sum;
103717ab2063SBarry Smith     }
10388d195f9aSBarry Smith #endif
1039b05257ddSBarry Smith   }
1040dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
10413649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
10421ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10433a40ed3dSBarry Smith   PetscFunctionReturn(0);
104417ab2063SBarry Smith }
104517ab2063SBarry Smith 
1046a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
10474a2ae208SSatish Balay #undef __FUNCT__
10484a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1049dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
105017ab2063SBarry Smith {
1051416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
105254f21887SBarry Smith   PetscScalar     *x,*y,*z;
105354f21887SBarry Smith   const MatScalar *aa;
1054dfbe8321SBarry Smith   PetscErrorCode  ierr;
1055d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1056aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
105797952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1058362ced78SSatish Balay   PetscScalar     sum;
1059ace3abfcSBarry Smith   PetscBool       usecprow=a->compressedrow.use;
1060e36a17ebSSatish Balay #endif
10619ea0dfa2SSatish Balay 
10623a40ed3dSBarry Smith   PetscFunctionBegin;
10631ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10641ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10652e8a6d31SBarry Smith   if (zz != yy) {
10661ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
10672e8a6d31SBarry Smith   } else {
10682e8a6d31SBarry Smith     z = y;
10692e8a6d31SBarry Smith   }
1070bfeeae90SHong Zhang 
107197952fefSHong Zhang   aj  = a->j;
107297952fefSHong Zhang   aa  = a->a;
1073cddf8d76SBarry Smith   ii  = a->i;
1074aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
107597952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
107602ab625aSSatish Balay #else
10774eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10784eb6d288SHong Zhang     if (zz != yy){
10794eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10804eb6d288SHong Zhang     }
108197952fefSHong Zhang     m    = a->compressedrow.nrows;
108297952fefSHong Zhang     ii   = a->compressedrow.i;
108397952fefSHong Zhang     ridx = a->compressedrow.rindex;
108497952fefSHong Zhang     for (i=0; i<m; i++){
108597952fefSHong Zhang       n  = ii[i+1] - ii[i];
108697952fefSHong Zhang       aj  = a->j + ii[i];
108797952fefSHong Zhang       aa  = a->a + ii[i];
108897952fefSHong Zhang       sum = y[*ridx];
108997952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
109097952fefSHong Zhang       z[*ridx++] = sum;
109197952fefSHong Zhang     }
109297952fefSHong Zhang   } else { /* do not use compressed row format */
109317ab2063SBarry Smith     for (i=0; i<m; i++) {
10949ea0dfa2SSatish Balay       jrow = ii[i];
10959ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
109617ab2063SBarry Smith       sum  = y[i];
10979ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
109897952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10999ea0dfa2SSatish Balay       }
110017ab2063SBarry Smith       z[i] = sum;
110117ab2063SBarry Smith     }
110297952fefSHong Zhang   }
110302ab625aSSatish Balay #endif
1104dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11051ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11061ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11072e8a6d31SBarry Smith   if (zz != yy) {
11081ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
11092e8a6d31SBarry Smith   }
1110918e98c3SVictor Minden #if defined(PETSC_HAVE_CUDA)
11116b375ea7SVictor Minden   /*
1112918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1113918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1114918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
11156b375ea7SVictor Minden   */
1116918e98c3SVictor Minden #endif
11173a40ed3dSBarry Smith   PetscFunctionReturn(0);
111817ab2063SBarry Smith }
111917ab2063SBarry Smith 
112017ab2063SBarry Smith /*
112117ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
112217ab2063SBarry Smith */
11234a2ae208SSatish Balay #undef __FUNCT__
11244a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1125dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
112617ab2063SBarry Smith {
1127416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11286849ba73SBarry Smith   PetscErrorCode ierr;
1129d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
113017ab2063SBarry Smith 
11313a40ed3dSBarry Smith   PetscFunctionBegin;
113209f38230SBarry Smith   if (!a->diag) {
113309f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
11349518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
113509f38230SBarry Smith   }
1136d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
113709f38230SBarry Smith     a->diag[i] = a->i[i+1];
1138bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1139bfeeae90SHong Zhang       if (a->j[j] == i) {
114009f38230SBarry Smith         a->diag[i] = j;
114117ab2063SBarry Smith         break;
114217ab2063SBarry Smith       }
114317ab2063SBarry Smith     }
114417ab2063SBarry Smith   }
11453a40ed3dSBarry Smith   PetscFunctionReturn(0);
114617ab2063SBarry Smith }
114717ab2063SBarry Smith 
1148be5855fcSBarry Smith /*
1149be5855fcSBarry Smith      Checks for missing diagonals
1150be5855fcSBarry Smith */
11514a2ae208SSatish Balay #undef __FUNCT__
11524a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1153ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1154be5855fcSBarry Smith {
1155be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
115697f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1157be5855fcSBarry Smith 
1158be5855fcSBarry Smith   PetscFunctionBegin;
115909f38230SBarry Smith   *missing = PETSC_FALSE;
1160d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
116109f38230SBarry Smith     *missing  = PETSC_TRUE;
116209f38230SBarry Smith     if (d) *d = 0;
116309f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
116409f38230SBarry Smith   } else {
1165f1e2ffcdSBarry Smith     diag = a->diag;
1166d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1167bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
116809f38230SBarry Smith 	*missing = PETSC_TRUE;
116909f38230SBarry Smith 	if (d) *d = i;
117009f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
117109f38230SBarry Smith       }
1172be5855fcSBarry Smith     }
1173be5855fcSBarry Smith   }
1174be5855fcSBarry Smith   PetscFunctionReturn(0);
1175be5855fcSBarry Smith }
1176be5855fcSBarry Smith 
117771f1c65dSBarry Smith EXTERN_C_BEGIN
117871f1c65dSBarry Smith #undef __FUNCT__
117971f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
118071f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
118171f1c65dSBarry Smith {
118271f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
118371f1c65dSBarry Smith   PetscErrorCode ierr;
1184d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
118554f21887SBarry Smith   MatScalar      *v = a->a;
118654f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
118771f1c65dSBarry Smith 
118871f1c65dSBarry Smith   PetscFunctionBegin;
118971f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
119071f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
119171f1c65dSBarry Smith   diag = a->diag;
119271f1c65dSBarry Smith   if (!a->idiag) {
119371f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
119471f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
119571f1c65dSBarry Smith     v        = a->a;
119671f1c65dSBarry Smith   }
119771f1c65dSBarry Smith   mdiag = a->mdiag;
119871f1c65dSBarry Smith   idiag = a->idiag;
119971f1c65dSBarry Smith 
1200028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
120171f1c65dSBarry Smith     for (i=0; i<m; i++) {
120271f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1203e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
120471f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
120571f1c65dSBarry Smith     }
120671f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
120771f1c65dSBarry Smith   } else {
120871f1c65dSBarry Smith     for (i=0; i<m; i++) {
120971f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
121071f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
121171f1c65dSBarry Smith     }
1212dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
121371f1c65dSBarry Smith   }
121471f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
121571f1c65dSBarry Smith   PetscFunctionReturn(0);
121671f1c65dSBarry Smith }
12175a9745a3SMatthew Knepley EXTERN_C_END
121871f1c65dSBarry Smith 
1219a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
12204a2ae208SSatish Balay #undef __FUNCT__
122141f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
122241f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
122317ab2063SBarry Smith {
1224416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1225e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1226e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
122754f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1228dfbe8321SBarry Smith   PetscErrorCode     ierr;
1229d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
123097f1f81fSBarry Smith   const PetscInt     *idx,*diag;
123117ab2063SBarry Smith 
12323a40ed3dSBarry Smith   PetscFunctionBegin;
1233b965ef7fSBarry Smith   its = its*lits;
123491723122SBarry Smith 
123571f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
123671f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
123771f1c65dSBarry Smith   a->fshift = fshift;
123871f1c65dSBarry Smith   a->omega  = omega;
1239ed480e8bSBarry Smith 
124071f1c65dSBarry Smith   diag = a->diag;
124171f1c65dSBarry Smith   t     = a->ssor_work;
1242ed480e8bSBarry Smith   idiag = a->idiag;
124371f1c65dSBarry Smith   mdiag = a->mdiag;
1244ed480e8bSBarry Smith 
12451ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
12463649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
124771f1c65dSBarry Smith   CHKMEMQ;
1248ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
124917ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
125017ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1251ed480e8bSBarry Smith     bs = b;
125217ab2063SBarry Smith     for (i=0; i<m; i++) {
125371f1c65dSBarry Smith         d    = fshift + mdiag[i];
1254416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1255ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1256ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
125717ab2063SBarry Smith         sum  = b[i]*d/omega;
1258003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
125917ab2063SBarry Smith         x[i] = sum;
126017ab2063SBarry Smith     }
12611ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12623649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1263efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12643a40ed3dSBarry Smith     PetscFunctionReturn(0);
126517ab2063SBarry Smith   }
1266c783ea89SBarry Smith 
126748af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1268e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
12693a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
127017ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1271887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
127217ab2063SBarry Smith 
127317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
127417ab2063SBarry Smith 
1275887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
127617ab2063SBarry Smith     */
127717ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
127817ab2063SBarry Smith 
127917ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
128017ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1281416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1282ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1283ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
128417ab2063SBarry Smith       sum  = b[i];
1285e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1286ed480e8bSBarry Smith       x[i] = sum*idiag[i];
128717ab2063SBarry Smith     }
128817ab2063SBarry Smith 
128917ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1290416022c9SBarry Smith     v = a->a;
1291ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
129217ab2063SBarry Smith 
129317ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1294ed480e8bSBarry Smith     ts = t;
1295416022c9SBarry Smith     diag = a->diag;
129617ab2063SBarry Smith     for (i=0; i<m; i++) {
1297416022c9SBarry Smith       n    = diag[i] - a->i[i];
1298ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1299ed480e8bSBarry Smith       v    = a->a + a->i[i];
130017ab2063SBarry Smith       sum  = t[i];
1301003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1302ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1303733d66baSBarry Smith       /*  x = x + t */
1304733d66baSBarry Smith       x[i] += t[i];
130517ab2063SBarry Smith     }
130617ab2063SBarry Smith 
1307dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
13081ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13093649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
13103a40ed3dSBarry Smith     PetscFunctionReturn(0);
131117ab2063SBarry Smith   }
131217ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
131317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
131417ab2063SBarry Smith       for (i=0; i<m; i++) {
1315416022c9SBarry Smith         n    = diag[i] - a->i[i];
1316ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1317ed480e8bSBarry Smith         v    = a->a + a->i[i];
131817ab2063SBarry Smith         sum  = b[i];
1319e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13205c99c7daSBarry Smith         t[i] = sum;
1321ed480e8bSBarry Smith         x[i] = sum*idiag[i];
132217ab2063SBarry Smith       }
13235c99c7daSBarry Smith       xb = t;
1324efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13253a40ed3dSBarry Smith     } else xb = b;
132617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
132717ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1328416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1329ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1330ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
133117ab2063SBarry Smith         sum  = xb[i];
1332e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13335c99c7daSBarry Smith         if (xb == b) {
1334ed480e8bSBarry Smith           x[i] = sum*idiag[i];
13355c99c7daSBarry Smith         } else {
13365c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
133717ab2063SBarry Smith         }
13385c99c7daSBarry Smith       }
1339efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
134017ab2063SBarry Smith     }
134117ab2063SBarry Smith     its--;
134217ab2063SBarry Smith   }
134317ab2063SBarry Smith   while (its--) {
134417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
134517ab2063SBarry Smith       for (i=0; i<m; 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     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
135617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1357416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1358ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1359ed480e8bSBarry Smith         v    = a->a + a->i[i];
136017ab2063SBarry Smith         sum  = b[i];
1361e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1362ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
136317ab2063SBarry Smith       }
13649f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
136517ab2063SBarry Smith     }
136617ab2063SBarry Smith   }
13671ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13683649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
136971f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
137017ab2063SBarry Smith }
137117ab2063SBarry Smith 
13722af78befSBarry Smith 
13734a2ae208SSatish Balay #undef __FUNCT__
13744a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1375dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
137617ab2063SBarry Smith {
1377416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13784e220ebcSLois Curfman McInnes 
13793a40ed3dSBarry Smith   PetscFunctionBegin;
13804e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13814e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13824e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13834e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13844e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13858e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
13867adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1387d5f3da31SBarry Smith   if (A->factortype) {
13884e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13894e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13904e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13914e220ebcSLois Curfman McInnes   } else {
13924e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13934e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13944e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13954e220ebcSLois Curfman McInnes   }
13963a40ed3dSBarry Smith   PetscFunctionReturn(0);
139717ab2063SBarry Smith }
139817ab2063SBarry Smith 
13994a2ae208SSatish Balay #undef __FUNCT__
14004a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1401f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
140217ab2063SBarry Smith {
1403416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
14043b98c0a2SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d = 0;
14056849ba73SBarry Smith   PetscErrorCode ierr;
1406ace3abfcSBarry Smith   PetscBool      missing;
140717ab2063SBarry Smith 
14083a40ed3dSBarry Smith   PetscFunctionBegin;
1409a9817697SBarry Smith   if (a->keepnonzeropattern) {
1410f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1411e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1412bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1413f1e2ffcdSBarry Smith     }
1414f4df32b1SMatthew Knepley     if (diag != 0.0) {
141509f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1416e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1417f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1418f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1419f1e2ffcdSBarry Smith       }
1420f1e2ffcdSBarry Smith     }
142188e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1422f1e2ffcdSBarry Smith   } else {
1423f4df32b1SMatthew Knepley     if (diag != 0.0) {
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]);
14267ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1427416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1428f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1429bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
14307ae801bdSBarry Smith         } else { /* in case row was completely empty */
1431f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
143217ab2063SBarry Smith         }
143317ab2063SBarry Smith       }
14343a40ed3dSBarry Smith     } else {
143517ab2063SBarry Smith       for (i=0; i<N; i++) {
1436e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1437416022c9SBarry Smith         a->ilen[rows[i]] = 0;
143817ab2063SBarry Smith       }
143917ab2063SBarry Smith     }
144088e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1441f1e2ffcdSBarry Smith   }
144243a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14433a40ed3dSBarry Smith   PetscFunctionReturn(0);
144417ab2063SBarry Smith }
144517ab2063SBarry Smith 
14464a2ae208SSatish Balay #undef __FUNCT__
14474a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1448a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
144917ab2063SBarry Smith {
1450416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
145197f1f81fSBarry Smith   PetscInt   *itmp;
145217ab2063SBarry Smith 
14533a40ed3dSBarry Smith   PetscFunctionBegin;
1454e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
145517ab2063SBarry Smith 
1456416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1457bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
145817ab2063SBarry Smith   if (idx) {
1459bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1460bfeeae90SHong Zhang     if (*nz) {
14614e093b46SBarry Smith       *idx = itmp;
146217ab2063SBarry Smith     }
146317ab2063SBarry Smith     else *idx = 0;
146417ab2063SBarry Smith   }
14653a40ed3dSBarry Smith   PetscFunctionReturn(0);
146617ab2063SBarry Smith }
146717ab2063SBarry Smith 
1468bfeeae90SHong Zhang /* remove this function? */
14694a2ae208SSatish Balay #undef __FUNCT__
14704a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1471a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
147217ab2063SBarry Smith {
14733a40ed3dSBarry Smith   PetscFunctionBegin;
14743a40ed3dSBarry Smith   PetscFunctionReturn(0);
147517ab2063SBarry Smith }
147617ab2063SBarry Smith 
14774a2ae208SSatish Balay #undef __FUNCT__
14784a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1479dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
148017ab2063SBarry Smith {
1481416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
148254f21887SBarry Smith   MatScalar      *v = a->a;
148336db0b34SBarry Smith   PetscReal      sum = 0.0;
14846849ba73SBarry Smith   PetscErrorCode ierr;
148597f1f81fSBarry Smith   PetscInt       i,j;
148617ab2063SBarry Smith 
14873a40ed3dSBarry Smith   PetscFunctionBegin;
148817ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1489416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1490aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
149136db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
149217ab2063SBarry Smith #else
149317ab2063SBarry Smith       sum += (*v)*(*v); v++;
149417ab2063SBarry Smith #endif
149517ab2063SBarry Smith     }
1496064f8208SBarry Smith     *nrm = sqrt(sum);
14973a40ed3dSBarry Smith   } else if (type == NORM_1) {
149836db0b34SBarry Smith     PetscReal *tmp;
149997f1f81fSBarry Smith     PetscInt    *jj = a->j;
1500d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1501d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1502064f8208SBarry Smith     *nrm = 0.0;
1503416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1504bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
150517ab2063SBarry Smith     }
1506d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1507064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
150817ab2063SBarry Smith     }
1509606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
15103a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1511064f8208SBarry Smith     *nrm = 0.0;
1512d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1513bfeeae90SHong Zhang       v = a->a + a->i[j];
151417ab2063SBarry Smith       sum = 0.0;
1515416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1516cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
151717ab2063SBarry Smith       }
1518064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
151917ab2063SBarry Smith     }
15203a40ed3dSBarry Smith   } else {
1521e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
152217ab2063SBarry Smith   }
15233a40ed3dSBarry Smith   PetscFunctionReturn(0);
152417ab2063SBarry Smith }
152517ab2063SBarry Smith 
15264a2ae208SSatish Balay #undef __FUNCT__
15274a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1528fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
152917ab2063SBarry Smith {
1530416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1531416022c9SBarry Smith   Mat            C;
15326849ba73SBarry Smith   PetscErrorCode ierr;
1533d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
153454f21887SBarry Smith   MatScalar      *array = a->a;
153517ab2063SBarry Smith 
15363a40ed3dSBarry Smith   PetscFunctionBegin;
1537e32f2f54SBarry 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");
1538fc4dec0aSBarry Smith 
1539fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1540d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1541d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1542bfeeae90SHong Zhang 
1543bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
15447adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1545d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
15467adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1547ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1548606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1549a541d17aSBarry Smith   } else {
1550a541d17aSBarry Smith     C = *B;
1551a541d17aSBarry Smith   }
1552a541d17aSBarry Smith 
155317ab2063SBarry Smith   for (i=0; i<m; i++) {
155417ab2063SBarry Smith     len    = ai[i+1]-ai[i];
155587d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1556b9b97703SBarry Smith     array += len;
1557b9b97703SBarry Smith     aj    += len;
155817ab2063SBarry Smith   }
15596d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15606d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
156117ab2063SBarry Smith 
1562815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1563416022c9SBarry Smith     *B = C;
156417ab2063SBarry Smith   } else {
1565eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
156617ab2063SBarry Smith   }
15673a40ed3dSBarry Smith   PetscFunctionReturn(0);
156817ab2063SBarry Smith }
156917ab2063SBarry Smith 
1570cd0d46ebSvictorle EXTERN_C_BEGIN
1571cd0d46ebSvictorle #undef __FUNCT__
15725fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1573ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1574cd0d46ebSvictorle {
1575cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
157654f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
157754f21887SBarry Smith   MatScalar      *va,*vb;
15786849ba73SBarry Smith   PetscErrorCode ierr;
157997f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1580cd0d46ebSvictorle 
1581cd0d46ebSvictorle   PetscFunctionBegin;
1582cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1583cd0d46ebSvictorle 
1584cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1585cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15865485867bSBarry Smith   if (ma!=nb || na!=mb){
15875485867bSBarry Smith     *f = PETSC_FALSE;
15885485867bSBarry Smith     PetscFunctionReturn(0);
15895485867bSBarry Smith   }
1590cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1591cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1592cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
159397f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
159497f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1595cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1596cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1597cd0d46ebSvictorle 
1598cd0d46ebSvictorle   *f = PETSC_TRUE;
1599cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1600cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
160197f1f81fSBarry Smith       PetscInt         idc,idr;
16025485867bSBarry Smith       PetscScalar vc,vr;
1603cd0d46ebSvictorle       /* column/row index/value */
16045485867bSBarry Smith       idc = adx[aptr[i]];
16055485867bSBarry Smith       idr = bdx[bptr[idc]];
16065485867bSBarry Smith       vc  = va[aptr[i]];
16075485867bSBarry Smith       vr  = vb[bptr[idc]];
16085485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
16095485867bSBarry Smith 	*f = PETSC_FALSE;
16105485867bSBarry Smith         goto done;
1611cd0d46ebSvictorle       } else {
16125485867bSBarry Smith 	aptr[i]++;
16135485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1614cd0d46ebSvictorle       }
1615cd0d46ebSvictorle     }
1616cd0d46ebSvictorle   }
1617cd0d46ebSvictorle  done:
1618cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
16193aeef889SHong Zhang   if (B) {
16203aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
16213aeef889SHong Zhang   }
1622cd0d46ebSvictorle   PetscFunctionReturn(0);
1623cd0d46ebSvictorle }
1624cd0d46ebSvictorle EXTERN_C_END
1625cd0d46ebSvictorle 
16261cbb95d3SBarry Smith EXTERN_C_BEGIN
16271cbb95d3SBarry Smith #undef __FUNCT__
16281cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
1629ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
16301cbb95d3SBarry Smith {
16311cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
163254f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
163354f21887SBarry Smith   MatScalar      *va,*vb;
16341cbb95d3SBarry Smith   PetscErrorCode ierr;
16351cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
16361cbb95d3SBarry Smith 
16371cbb95d3SBarry Smith   PetscFunctionBegin;
16381cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
16391cbb95d3SBarry Smith 
16401cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
16411cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
16421cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
16431cbb95d3SBarry Smith     *f = PETSC_FALSE;
16441cbb95d3SBarry Smith     PetscFunctionReturn(0);
16451cbb95d3SBarry Smith   }
16461cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
16471cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
16481cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
16491cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
16501cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
16511cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
16521cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
16531cbb95d3SBarry Smith 
16541cbb95d3SBarry Smith   *f = PETSC_TRUE;
16551cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
16561cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
16571cbb95d3SBarry Smith       PetscInt         idc,idr;
16581cbb95d3SBarry Smith       PetscScalar vc,vr;
16591cbb95d3SBarry Smith       /* column/row index/value */
16601cbb95d3SBarry Smith       idc = adx[aptr[i]];
16611cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16621cbb95d3SBarry Smith       vc  = va[aptr[i]];
16631cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16641cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16651cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16661cbb95d3SBarry Smith         goto done;
16671cbb95d3SBarry Smith       } else {
16681cbb95d3SBarry Smith 	aptr[i]++;
16691cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16701cbb95d3SBarry Smith       }
16711cbb95d3SBarry Smith     }
16721cbb95d3SBarry Smith   }
16731cbb95d3SBarry Smith  done:
16741cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16751cbb95d3SBarry Smith   if (B) {
16761cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16771cbb95d3SBarry Smith   }
16781cbb95d3SBarry Smith   PetscFunctionReturn(0);
16791cbb95d3SBarry Smith }
16801cbb95d3SBarry Smith EXTERN_C_END
16811cbb95d3SBarry Smith 
16829e29f15eSvictorle #undef __FUNCT__
16839e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1684ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
16859e29f15eSvictorle {
1686dfbe8321SBarry Smith   PetscErrorCode ierr;
16879e29f15eSvictorle   PetscFunctionBegin;
16885485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16899e29f15eSvictorle   PetscFunctionReturn(0);
16909e29f15eSvictorle }
16919e29f15eSvictorle 
16924a2ae208SSatish Balay #undef __FUNCT__
16931cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
1694ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
16951cbb95d3SBarry Smith {
16961cbb95d3SBarry Smith   PetscErrorCode ierr;
16971cbb95d3SBarry Smith   PetscFunctionBegin;
16981cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16991cbb95d3SBarry Smith   PetscFunctionReturn(0);
17001cbb95d3SBarry Smith }
17011cbb95d3SBarry Smith 
17021cbb95d3SBarry Smith #undef __FUNCT__
17034a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1704dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
170517ab2063SBarry Smith {
1706416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
170754f21887SBarry Smith   PetscScalar    *l,*r,x;
170854f21887SBarry Smith   MatScalar      *v;
1709dfbe8321SBarry Smith   PetscErrorCode ierr;
1710d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
171117ab2063SBarry Smith 
17123a40ed3dSBarry Smith   PetscFunctionBegin;
171317ab2063SBarry Smith   if (ll) {
17143ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
17153ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1716e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1717e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
17181ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1719416022c9SBarry Smith     v = a->a;
172017ab2063SBarry Smith     for (i=0; i<m; i++) {
172117ab2063SBarry Smith       x = l[i];
1722416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
172317ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
172417ab2063SBarry Smith     }
17251ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1726efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
172717ab2063SBarry Smith   }
172817ab2063SBarry Smith   if (rr) {
1729e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1730e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
17311ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1732416022c9SBarry Smith     v = a->a; jj = a->j;
173317ab2063SBarry Smith     for (i=0; i<nz; i++) {
1734bfeeae90SHong Zhang       (*v++) *= r[*jj++];
173517ab2063SBarry Smith     }
17361ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1737efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
173817ab2063SBarry Smith   }
17393a40ed3dSBarry Smith   PetscFunctionReturn(0);
174017ab2063SBarry Smith }
174117ab2063SBarry Smith 
17424a2ae208SSatish Balay #undef __FUNCT__
17434a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
174497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
174517ab2063SBarry Smith {
1746db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
17476849ba73SBarry Smith   PetscErrorCode ierr;
1748d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
174997f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
17505d0c19d7SBarry Smith   const PetscInt *irow,*icol;
17515d0c19d7SBarry Smith   PetscInt       nrows,ncols;
175297f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
175354f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1754416022c9SBarry Smith   Mat            C;
1755ace3abfcSBarry Smith   PetscBool      stride,sorted;
175617ab2063SBarry Smith 
17573a40ed3dSBarry Smith   PetscFunctionBegin;
175814ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1759e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
176014ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1761e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
176299141d43SSatish Balay 
176317ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1764b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1765b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
176617ab2063SBarry Smith 
1767fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1768fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1769fee21e36SBarry Smith   if (stride && step == 1) {
177002834360SBarry Smith     /* special case of contiguous rows */
17710e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
177202834360SBarry Smith     /* loop over new rows determining lens and starting points */
177302834360SBarry Smith     for (i=0; i<nrows; i++) {
1774bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1775a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
177602834360SBarry Smith       for (k=kstart; k<kend; k++) {
1777bfeeae90SHong Zhang         if (aj[k] >= first) {
177802834360SBarry Smith           starts[i] = k;
177902834360SBarry Smith           break;
178002834360SBarry Smith 	}
178102834360SBarry Smith       }
1782a2744918SBarry Smith       sum = 0;
178302834360SBarry Smith       while (k < kend) {
1784bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1785a2744918SBarry Smith         sum++;
178602834360SBarry Smith       }
1787a2744918SBarry Smith       lens[i] = sum;
178802834360SBarry Smith     }
178902834360SBarry Smith     /* create submatrix */
1790cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
179197f1f81fSBarry Smith       PetscInt n_cols,n_rows;
179208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1793e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1794d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
179508480c60SBarry Smith       C = *B;
17963a40ed3dSBarry Smith     } else {
17977adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1798f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17997adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1800ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
180108480c60SBarry Smith     }
1802db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1803db02288aSLois Curfman McInnes 
180402834360SBarry Smith     /* loop over rows inserting into submatrix */
1805db02288aSLois Curfman McInnes     a_new    = c->a;
1806db02288aSLois Curfman McInnes     j_new    = c->j;
1807db02288aSLois Curfman McInnes     i_new    = c->i;
1808bfeeae90SHong Zhang 
180902834360SBarry Smith     for (i=0; i<nrows; i++) {
1810a2744918SBarry Smith       ii    = starts[i];
1811a2744918SBarry Smith       lensi = lens[i];
1812a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1813a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
181402834360SBarry Smith       }
181587828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1816a2744918SBarry Smith       a_new      += lensi;
1817a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1818a2744918SBarry Smith       c->ilen[i]  = lensi;
181902834360SBarry Smith     }
18200e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
18213a40ed3dSBarry Smith   } else {
182202834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
18230e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
182497f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
18250e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
182617ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
182702834360SBarry Smith     /* determine lens of each row */
182802834360SBarry Smith     for (i=0; i<nrows; i++) {
1829bfeeae90SHong Zhang       kstart  = ai[irow[i]];
183002834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
183102834360SBarry Smith       lens[i] = 0;
183202834360SBarry Smith       for (k=kstart; k<kend; k++) {
1833bfeeae90SHong Zhang         if (smap[aj[k]]) {
183402834360SBarry Smith           lens[i]++;
183502834360SBarry Smith         }
183602834360SBarry Smith       }
183702834360SBarry Smith     }
183817ab2063SBarry Smith     /* Create and fill new matrix */
1839a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
1840ace3abfcSBarry Smith       PetscBool  equal;
18410f5bd95cSBarry Smith 
184299141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1843e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1844d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
18450f5bd95cSBarry Smith       if (!equal) {
1846e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
184799141d43SSatish Balay       }
1848d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
184908480c60SBarry Smith       C = *B;
18503a40ed3dSBarry Smith     } else {
18517adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1852f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
18537adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1854ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
185508480c60SBarry Smith     }
185699141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
185717ab2063SBarry Smith     for (i=0; i<nrows; i++) {
185899141d43SSatish Balay       row    = irow[i];
1859bfeeae90SHong Zhang       kstart = ai[row];
186099141d43SSatish Balay       kend   = kstart + a->ilen[row];
1861bfeeae90SHong Zhang       mat_i  = c->i[i];
186299141d43SSatish Balay       mat_j  = c->j + mat_i;
186399141d43SSatish Balay       mat_a  = c->a + mat_i;
186499141d43SSatish Balay       mat_ilen = c->ilen + i;
186517ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1866bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1867ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
186899141d43SSatish Balay           *mat_a++ = a->a[k];
186999141d43SSatish Balay           (*mat_ilen)++;
187099141d43SSatish Balay 
187117ab2063SBarry Smith         }
187217ab2063SBarry Smith       }
187317ab2063SBarry Smith     }
187402834360SBarry Smith     /* Free work space */
187502834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1876606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1877606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
187802834360SBarry Smith   }
18796d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18806d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
188117ab2063SBarry Smith 
188217ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1883416022c9SBarry Smith   *B = C;
18843a40ed3dSBarry Smith   PetscFunctionReturn(0);
188517ab2063SBarry Smith }
188617ab2063SBarry Smith 
18871df811f5SHong Zhang #undef __FUNCT__
188882d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
188982d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
189082d44351SHong Zhang {
189182d44351SHong Zhang   PetscErrorCode ierr;
189282d44351SHong Zhang   Mat            B;
189382d44351SHong Zhang 
189482d44351SHong Zhang   PetscFunctionBegin;
189582d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
189682d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
189782d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
189882d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
189982d44351SHong Zhang   *subMat = B;
190082d44351SHong Zhang   PetscFunctionReturn(0);
190182d44351SHong Zhang }
190282d44351SHong Zhang 
190382d44351SHong Zhang #undef __FUNCT__
19044a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
19050481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1906a871dcd8SBarry Smith {
190763b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1908dfbe8321SBarry Smith   PetscErrorCode ierr;
190963b91edcSBarry Smith   Mat            outA;
1910ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
191163b91edcSBarry Smith 
19123a40ed3dSBarry Smith   PetscFunctionBegin;
1913e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
19141df811f5SHong Zhang 
1915b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1916b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1917a871dcd8SBarry Smith 
191863b91edcSBarry Smith   outA              = inA;
1919d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
1920c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1921c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1922c3122656SLisandro Dalcin   a->row = row;
1923c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1924c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1925c3122656SLisandro Dalcin   a->col = col;
192663b91edcSBarry Smith 
192736db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1928b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
19294c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
193052e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1931f0ec6fceSSatish Balay 
193294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1933d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1934d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
193594a9d846SBarry Smith   }
193663b91edcSBarry Smith 
1937f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1938137fb511SHong Zhang   if (row_identity && col_identity) {
1939ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
1940137fb511SHong Zhang   } else {
1941719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1942137fb511SHong Zhang   }
19433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1944a871dcd8SBarry Smith }
1945a871dcd8SBarry Smith 
19464a2ae208SSatish Balay #undef __FUNCT__
19474a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1948f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1949f0b747eeSBarry Smith {
1950f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1951f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1952efee365bSSatish Balay   PetscErrorCode ierr;
19530805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
19543a40ed3dSBarry Smith 
19553a40ed3dSBarry Smith   PetscFunctionBegin;
1956f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1957efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
19583a40ed3dSBarry Smith   PetscFunctionReturn(0);
1959f0b747eeSBarry Smith }
1960f0b747eeSBarry Smith 
19614a2ae208SSatish Balay #undef __FUNCT__
19624a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
196397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1964cddf8d76SBarry Smith {
1965dfbe8321SBarry Smith   PetscErrorCode ierr;
196697f1f81fSBarry Smith   PetscInt       i;
1967cddf8d76SBarry Smith 
19683a40ed3dSBarry Smith   PetscFunctionBegin;
1969cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1970b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1971cddf8d76SBarry Smith   }
1972cddf8d76SBarry Smith 
1973cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19746a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1975cddf8d76SBarry Smith   }
19763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1977cddf8d76SBarry Smith }
1978cddf8d76SBarry Smith 
19794a2ae208SSatish Balay #undef __FUNCT__
19804a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
198197f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19824dcbc457SBarry Smith {
1983e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19846849ba73SBarry Smith   PetscErrorCode ierr;
19855d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19865d0c19d7SBarry Smith   const PetscInt *idx;
198797f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1988f1af5d2fSBarry Smith   PetscBT        table;
1989bbd702dbSSatish Balay 
19903a40ed3dSBarry Smith   PetscFunctionBegin;
1991d0f46423SBarry Smith   m     = A->rmap->n;
1992e4d965acSSatish Balay   ai    = a->i;
1993bfeeae90SHong Zhang   aj    = a->j;
19948a047759SSatish Balay 
1995e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
199606763907SSatish Balay 
199797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19986831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
199906763907SSatish Balay 
2000e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2001b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2002e4d965acSSatish Balay     isz  = 0;
20036831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2004e4d965acSSatish Balay 
2005e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
20064dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2007b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2008e4d965acSSatish Balay 
2009dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2010e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2011f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
20124dcbc457SBarry Smith     }
201306763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
201406763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
2015e4d965acSSatish Balay 
201604a348a9SBarry Smith     k = 0;
201704a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
201804a348a9SBarry Smith       n = isz;
201906763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2020e4d965acSSatish Balay         row   = nidx[k];
2021e4d965acSSatish Balay         start = ai[row];
2022e4d965acSSatish Balay         end   = ai[row+1];
202304a348a9SBarry Smith         for (l = start; l<end ; l++){
2024efb16452SHong Zhang           val = aj[l] ;
2025f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2026e4d965acSSatish Balay         }
2027e4d965acSSatish Balay       }
2028e4d965acSSatish Balay     }
2029029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
2030e4d965acSSatish Balay   }
20316831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2032606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
20333a40ed3dSBarry Smith   PetscFunctionReturn(0);
20344dcbc457SBarry Smith }
203517ab2063SBarry Smith 
20360513a670SBarry Smith /* -------------------------------------------------------------- */
20374a2ae208SSatish Balay #undef __FUNCT__
20384a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2039dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
20400513a670SBarry Smith {
20410513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
20426849ba73SBarry Smith   PetscErrorCode ierr;
20433b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
20445d0c19d7SBarry Smith   const PetscInt *row,*col;
20455d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
204656cd22aeSBarry Smith   IS             icolp,irowp;
20473b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
20483b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
20490513a670SBarry Smith 
20503a40ed3dSBarry Smith   PetscFunctionBegin;
20514c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
205256cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
20534c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
205456cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
20550513a670SBarry Smith 
20560513a670SBarry Smith   /* determine lengths of permuted rows */
205797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
20580513a670SBarry Smith   for (i=0; i<m; i++) {
20590513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
20600513a670SBarry Smith   }
20617adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2062f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
20637adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2064ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2065606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
20660513a670SBarry Smith 
206797f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
20680513a670SBarry Smith   for (i=0; i<m; i++) {
206932ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20700513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2071cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
207232ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20730513a670SBarry Smith   }
2074606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20753c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20760513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20770513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
207856cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
207956cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
208056cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
208156cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20823a40ed3dSBarry Smith   PetscFunctionReturn(0);
20830513a670SBarry Smith }
20840513a670SBarry Smith 
20854a2ae208SSatish Balay #undef __FUNCT__
20864a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2087dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2088cb5b572fSBarry Smith {
2089dfbe8321SBarry Smith   PetscErrorCode ierr;
2090cb5b572fSBarry Smith 
2091cb5b572fSBarry Smith   PetscFunctionBegin;
209233f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
209333f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2094be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2095be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2096be6bf707SBarry Smith 
2097d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2098e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2099cb5b572fSBarry Smith     }
2100d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2101cb5b572fSBarry Smith   } else {
2102cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2103cb5b572fSBarry Smith   }
2104cb5b572fSBarry Smith   PetscFunctionReturn(0);
2105cb5b572fSBarry Smith }
2106cb5b572fSBarry Smith 
21074a2ae208SSatish Balay #undef __FUNCT__
21084a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2109dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2110273d9f13SBarry Smith {
2111dfbe8321SBarry Smith   PetscErrorCode ierr;
2112273d9f13SBarry Smith 
2113273d9f13SBarry Smith   PetscFunctionBegin;
2114ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2115273d9f13SBarry Smith   PetscFunctionReturn(0);
2116273d9f13SBarry Smith }
2117273d9f13SBarry Smith 
21184a2ae208SSatish Balay #undef __FUNCT__
21194a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2120a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
21216c0721eeSBarry Smith {
21226c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
21236c0721eeSBarry Smith   PetscFunctionBegin;
21246c0721eeSBarry Smith   *array = a->a;
21256c0721eeSBarry Smith   PetscFunctionReturn(0);
21266c0721eeSBarry Smith }
21276c0721eeSBarry Smith 
21284a2ae208SSatish Balay #undef __FUNCT__
21294a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2130dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
21316c0721eeSBarry Smith {
21326c0721eeSBarry Smith   PetscFunctionBegin;
21336c0721eeSBarry Smith   PetscFunctionReturn(0);
21346c0721eeSBarry Smith }
2135273d9f13SBarry Smith 
2136ee4f033dSBarry Smith #undef __FUNCT__
2137ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2138dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2139ee4f033dSBarry Smith {
21406849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
21416849ba73SBarry Smith   PetscErrorCode ierr;
214297f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2143efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
214487828ca2SBarry Smith   PetscScalar    *vscale_array;
2145ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2146ee4f033dSBarry Smith   Vec            w1,w2,w3;
2147ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2148ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2149ee4f033dSBarry Smith 
2150ee4f033dSBarry Smith   PetscFunctionBegin;
2151ee4f033dSBarry Smith   if (!coloring->w1) {
2152ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
215352e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2154ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
215552e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2156ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
215752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2158ee4f033dSBarry Smith   }
2159ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2160ee4f033dSBarry Smith 
2161ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
216290d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2163ee4f033dSBarry Smith   if (flg) {
2164ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2165ee4f033dSBarry Smith   } else {
2166ace3abfcSBarry Smith     PetscBool  assembled;
21670b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
21680b9b6f31SBarry Smith     if (assembled) {
2169ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2170ee4f033dSBarry Smith     }
21710b9b6f31SBarry Smith   }
2172ee4f033dSBarry Smith 
2173ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2174ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2175ee4f033dSBarry Smith 
2176ee4f033dSBarry Smith   /*
2177ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2178ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2179ee4f033dSBarry Smith   */
2180ee4f033dSBarry Smith   if (coloring->F) {
2181ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2182ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2183ee4f033dSBarry Smith     if (m1 != m2) {
2184ee4f033dSBarry Smith       coloring->F = 0;
2185ee4f033dSBarry Smith     }
2186ee4f033dSBarry Smith   }
2187ee4f033dSBarry Smith 
2188ee4f033dSBarry Smith   if (coloring->F) {
2189ee4f033dSBarry Smith     w1          = coloring->F;
2190ee4f033dSBarry Smith     coloring->F = 0;
2191ee4f033dSBarry Smith   } else {
219266f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2193ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
219466f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2195ee4f033dSBarry Smith   }
2196ee4f033dSBarry Smith 
2197ee4f033dSBarry Smith   /*
2198ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2199ee4f033dSBarry Smith   */
22001ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
22011ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2202ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2203ee4f033dSBarry Smith     /*
2204ee4f033dSBarry Smith        Loop over each column associated with color adding the
2205ee4f033dSBarry Smith        perturbation to the vector w3.
2206ee4f033dSBarry Smith     */
2207ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2208ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2209ee4f033dSBarry Smith       dx  = xx[col];
2210ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2211ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2212ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2213ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2214ee4f033dSBarry Smith #else
2215ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2216ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2217ee4f033dSBarry Smith #endif
2218ee4f033dSBarry Smith       dx                *= epsilon;
2219ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2220ee4f033dSBarry Smith     }
2221ee4f033dSBarry Smith   }
22221ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2223ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2224ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2225ee4f033dSBarry Smith 
2226ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2227ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2228ee4f033dSBarry Smith 
2229ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2230ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2231ee4f033dSBarry Smith 
22321ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2233ee4f033dSBarry Smith   /*
2234ee4f033dSBarry Smith       Loop over each color
2235ee4f033dSBarry Smith   */
2236ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
223749b058dcSBarry Smith     coloring->currentcolor = k;
2238ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
22391ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2240ee4f033dSBarry Smith     /*
2241ee4f033dSBarry Smith        Loop over each column associated with color adding the
2242ee4f033dSBarry Smith        perturbation to the vector w3.
2243ee4f033dSBarry Smith     */
2244ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2245ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2246ee4f033dSBarry Smith       dx  = xx[col];
22475b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2248ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2249ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2250ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2251ee4f033dSBarry Smith #else
2252ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2253ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2254ee4f033dSBarry Smith #endif
2255ee4f033dSBarry Smith       dx            *= epsilon;
2256e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2257ee4f033dSBarry Smith       w3_array[col] += dx;
2258ee4f033dSBarry Smith     }
22591ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2260ee4f033dSBarry Smith 
2261ee4f033dSBarry Smith     /*
2262ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2263ee4f033dSBarry Smith     */
2264ee4f033dSBarry Smith 
226566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2266ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
226766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2268efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2269ee4f033dSBarry Smith 
2270ee4f033dSBarry Smith     /*
2271ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2272ee4f033dSBarry Smith     */
22731ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2274ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2275ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2276ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2277ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2278ee4f033dSBarry Smith       srow   = row + start;
2279ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2280ee4f033dSBarry Smith     }
22811ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2282ee4f033dSBarry Smith   }
228349b058dcSBarry Smith   coloring->currentcolor = k;
22841ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22851ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2286ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2287ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2288ee4f033dSBarry Smith   PetscFunctionReturn(0);
2289ee4f033dSBarry Smith }
2290ee4f033dSBarry Smith 
22918229c054SShri Abhyankar /*
22928229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
22938229c054SShri Abhyankar    have different nonzero structure.
22948229c054SShri Abhyankar */
2295ac90fabeSBarry Smith #undef __FUNCT__
22968229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
22978229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2298ec7775f6SShri Abhyankar {
22998229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2300ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2301ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2302ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2303ec7775f6SShri Abhyankar 
2304ec7775f6SShri Abhyankar   PetscFunctionBegin;
2305ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2306ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
23078af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
23088af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
23098af7cee1SJed Brown     nnz[i] = 0;
23108af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
23118af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
23128af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
23138af7cee1SJed Brown       nnz[i]++;
23148af7cee1SJed Brown     }
23158af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2316ec7775f6SShri Abhyankar   }
2317ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2318ec7775f6SShri Abhyankar }
2319ec7775f6SShri Abhyankar 
2320ec7775f6SShri Abhyankar #undef __FUNCT__
2321ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2322f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2323ac90fabeSBarry Smith {
2324dfbe8321SBarry Smith   PetscErrorCode ierr;
232597f1f81fSBarry Smith   PetscInt       i;
2326ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
23270805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2328ac90fabeSBarry Smith 
2329ac90fabeSBarry Smith   PetscFunctionBegin;
2330ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2331f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2332f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2333c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2334a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2335a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2336a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2337a30b2313SHong Zhang     }
2338a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2339d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2340a30b2313SHong Zhang       y->XtoY = X;
2341407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2342c537a176SHong Zhang     }
2343f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
23441e2582c4SBarry 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);
2345ac90fabeSBarry Smith   } else {
23468229c054SShri Abhyankar     Mat B;
23478229c054SShri Abhyankar     PetscInt *nnz;
2348*16b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2349ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
23504aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2351ec7775f6SShri Abhyankar     ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
23528229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
23538229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2354ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2355ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
23568229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2357ac90fabeSBarry Smith   }
2358ac90fabeSBarry Smith   PetscFunctionReturn(0);
2359ac90fabeSBarry Smith }
2360ac90fabeSBarry Smith 
2361521d7252SBarry Smith #undef __FUNCT__
2362521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2363521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2364521d7252SBarry Smith {
236541c166b1SJed Brown   PetscErrorCode ierr;
236641c166b1SJed Brown 
2367521d7252SBarry Smith   PetscFunctionBegin;
236841c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
236941c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2370521d7252SBarry Smith   PetscFunctionReturn(0);
2371521d7252SBarry Smith }
2372521d7252SBarry Smith 
2373354c94deSBarry Smith #undef __FUNCT__
2374354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2375354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2376354c94deSBarry Smith {
2377354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2378354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2379354c94deSBarry Smith   PetscInt    i,nz;
2380354c94deSBarry Smith   PetscScalar *a;
2381354c94deSBarry Smith 
2382354c94deSBarry Smith   PetscFunctionBegin;
2383354c94deSBarry Smith   nz = aij->nz;
2384354c94deSBarry Smith   a  = aij->a;
2385354c94deSBarry Smith   for (i=0; i<nz; i++) {
2386354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2387354c94deSBarry Smith   }
2388354c94deSBarry Smith #else
2389354c94deSBarry Smith   PetscFunctionBegin;
2390354c94deSBarry Smith #endif
2391354c94deSBarry Smith   PetscFunctionReturn(0);
2392354c94deSBarry Smith }
2393354c94deSBarry Smith 
2394e34fafa9SBarry Smith #undef __FUNCT__
2395985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2396985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2397e34fafa9SBarry Smith {
2398e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2399e34fafa9SBarry Smith   PetscErrorCode ierr;
2400d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2401e34fafa9SBarry Smith   PetscReal      atmp;
2402985db425SBarry Smith   PetscScalar    *x;
2403e34fafa9SBarry Smith   MatScalar      *aa;
2404e34fafa9SBarry Smith 
2405e34fafa9SBarry Smith   PetscFunctionBegin;
2406e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2407e34fafa9SBarry Smith   aa   = a->a;
2408e34fafa9SBarry Smith   ai   = a->i;
2409e34fafa9SBarry Smith   aj   = a->j;
2410e34fafa9SBarry Smith 
2411985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2412e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2413e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2414e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2415e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2416e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
24179189402eSHong Zhang     x[i] = 0.0;
2418e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2419985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2420985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2421985db425SBarry Smith       aa++; aj++;
2422985db425SBarry Smith     }
2423985db425SBarry Smith   }
2424985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2425985db425SBarry Smith   PetscFunctionReturn(0);
2426985db425SBarry Smith }
2427985db425SBarry Smith 
2428985db425SBarry Smith #undef __FUNCT__
2429985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2430985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2431985db425SBarry Smith {
2432985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2433985db425SBarry Smith   PetscErrorCode ierr;
2434d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2435985db425SBarry Smith   PetscScalar    *x;
2436985db425SBarry Smith   MatScalar      *aa;
2437985db425SBarry Smith 
2438985db425SBarry Smith   PetscFunctionBegin;
2439e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2440985db425SBarry Smith   aa   = a->a;
2441985db425SBarry Smith   ai   = a->i;
2442985db425SBarry Smith   aj   = a->j;
2443985db425SBarry Smith 
2444985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2445985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2446985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2447e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2448985db425SBarry Smith   for (i=0; i<m; i++) {
2449985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2450d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2451985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2452985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2453985db425SBarry Smith       x[i] = 0.0;
2454985db425SBarry Smith       if (idx) {
2455985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2456985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2457985db425SBarry Smith           if (aj[j] > j) {
2458985db425SBarry Smith             idx[i] = j;
2459985db425SBarry Smith             break;
2460985db425SBarry Smith           }
2461985db425SBarry Smith         }
2462985db425SBarry Smith       }
2463985db425SBarry Smith     }
2464985db425SBarry Smith     for (j=0; j<ncols; j++){
2465985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2466985db425SBarry Smith       aa++; aj++;
2467985db425SBarry Smith     }
2468985db425SBarry Smith   }
2469985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2470985db425SBarry Smith   PetscFunctionReturn(0);
2471985db425SBarry Smith }
2472985db425SBarry Smith 
2473985db425SBarry Smith #undef __FUNCT__
2474c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2475c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2476c87e5d42SMatthew Knepley {
2477c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2478c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2479c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2480c87e5d42SMatthew Knepley   PetscReal      atmp;
2481c87e5d42SMatthew Knepley   PetscScalar    *x;
2482c87e5d42SMatthew Knepley   MatScalar      *aa;
2483c87e5d42SMatthew Knepley 
2484c87e5d42SMatthew Knepley   PetscFunctionBegin;
2485e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2486c87e5d42SMatthew Knepley   aa   = a->a;
2487c87e5d42SMatthew Knepley   ai   = a->i;
2488c87e5d42SMatthew Knepley   aj   = a->j;
2489c87e5d42SMatthew Knepley 
2490c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2491c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2492c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2493e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2494c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2495c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2496289a08f5SMatthew Knepley     if (ncols) {
2497289a08f5SMatthew Knepley       /* Get first nonzero */
2498289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2499289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2500289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2501289a08f5SMatthew Knepley       }
2502289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2503289a08f5SMatthew Knepley     } else {
2504289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2505289a08f5SMatthew Knepley     }
2506c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2507c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2508289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2509c87e5d42SMatthew Knepley       aa++; aj++;
2510c87e5d42SMatthew Knepley     }
2511c87e5d42SMatthew Knepley   }
2512c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2513c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2514c87e5d42SMatthew Knepley }
2515c87e5d42SMatthew Knepley 
2516c87e5d42SMatthew Knepley #undef __FUNCT__
2517985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2518985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2519985db425SBarry Smith {
2520985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2521985db425SBarry Smith   PetscErrorCode ierr;
2522d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2523985db425SBarry Smith   PetscScalar    *x;
2524985db425SBarry Smith   MatScalar      *aa;
2525985db425SBarry Smith 
2526985db425SBarry Smith   PetscFunctionBegin;
2527e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2528985db425SBarry Smith   aa   = a->a;
2529985db425SBarry Smith   ai   = a->i;
2530985db425SBarry Smith   aj   = a->j;
2531985db425SBarry Smith 
2532985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2533985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2534985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2535e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2536985db425SBarry Smith   for (i=0; i<m; i++) {
2537985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2538d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2539985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2540985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2541985db425SBarry Smith       x[i] = 0.0;
2542985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2543985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2544985db425SBarry Smith         for (j=0;j<ncols;j++) {
2545985db425SBarry Smith           if (aj[j] > j) {
2546985db425SBarry Smith             idx[i] = j;
2547985db425SBarry Smith             break;
2548985db425SBarry Smith           }
2549985db425SBarry Smith         }
2550985db425SBarry Smith       }
2551985db425SBarry Smith     }
2552985db425SBarry Smith     for (j=0; j<ncols; j++){
2553985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2554985db425SBarry Smith       aa++; aj++;
2555e34fafa9SBarry Smith     }
2556e34fafa9SBarry Smith   }
2557e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2558e34fafa9SBarry Smith   PetscFunctionReturn(0);
2559e34fafa9SBarry Smith }
25603acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2561682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
25620a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2563cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2564cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2565cb5b572fSBarry Smith        MatMult_SeqAIJ,
256697304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
25677c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
25687c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2569db4efbfdSBarry Smith        0,
2570db4efbfdSBarry Smith        0,
2571db4efbfdSBarry Smith        0,
2572db4efbfdSBarry Smith /*10*/ 0,
2573cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2574cb5b572fSBarry Smith        0,
257541f059aeSBarry Smith        MatSOR_SeqAIJ,
257617ab2063SBarry Smith        MatTranspose_SeqAIJ,
257797304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2578cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2579cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2580cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2581cb5b572fSBarry Smith        MatNorm_SeqAIJ,
258297304618SKris Buschelman /*20*/ 0,
2583cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2584cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2585cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2586d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2587db4efbfdSBarry Smith        0,
2588db4efbfdSBarry Smith        0,
2589db4efbfdSBarry Smith        0,
2590db4efbfdSBarry Smith        0,
2591d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2592db4efbfdSBarry Smith        0,
2593db4efbfdSBarry Smith        0,
25946c0721eeSBarry Smith        MatGetArray_SeqAIJ,
25956c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2596d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2597cb5b572fSBarry Smith        0,
2598cb5b572fSBarry Smith        0,
2599cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2600cb5b572fSBarry Smith        0,
2601d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2602cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2603cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2604cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2605cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2606d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2607cb5b572fSBarry Smith        MatScale_SeqAIJ,
2608cb5b572fSBarry Smith        0,
260979299369SBarry Smith        MatDiagonalSet_SeqAIJ,
2610fe97e370SBarry Smith        0,
2611d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
26123b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
26133b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
26143b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2615a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2616d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2617b9617806SBarry Smith        0,
26180513a670SBarry Smith        0,
2619cda55fadSBarry Smith        MatPermute_SeqAIJ,
2620cda55fadSBarry Smith        0,
2621d519adbfSMatthew Knepley /*59*/ 0,
2622b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2623b9b97703SBarry Smith        MatView_SeqAIJ,
2624357abbc8SBarry Smith        0,
2625ee4f033dSBarry Smith        0,
2626d519adbfSMatthew Knepley /*64*/ 0,
2627ee4f033dSBarry Smith        0,
2628ee4f033dSBarry Smith        0,
2629ee4f033dSBarry Smith        0,
2630ee4f033dSBarry Smith        0,
2631d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2632c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2633ee4f033dSBarry Smith        0,
2634ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2635dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2636ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2637dcf5cc72SBarry Smith #else
2638dcf5cc72SBarry Smith        0,
2639dcf5cc72SBarry Smith #endif
2640d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
26413acb8795SBarry Smith        MatFDColoringApply_AIJ,
264297304618SKris Buschelman        0,
264397304618SKris Buschelman        0,
264497304618SKris Buschelman        0,
2645d519adbfSMatthew Knepley /*79*/ 0,
264697304618SKris Buschelman        0,
264797304618SKris Buschelman        0,
264897304618SKris Buschelman        0,
2649bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2650d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
26511cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
26526284ec50SHong Zhang        0,
26536284ec50SHong Zhang        0,
2654bc011b1eSHong Zhang        0,
2655d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
265626be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
265726be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2658d439da42SKris Buschelman        MatPtAP_Basic,
26597ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2660d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2661bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2662bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2663bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
26647ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2665d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2666609c6c4dSKris Buschelman        0,
2667609c6c4dSKris Buschelman        0,
266887d4246cSBarry Smith        MatConjugate_SeqAIJ,
266987d4246cSBarry Smith        0,
2670d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
267199cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2672f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2673f5edf698SHong Zhang        0,
26742bebee5dSHong Zhang        0,
2675d519adbfSMatthew Knepley /*109*/0,
2676985db425SBarry Smith        0,
26772af78befSBarry Smith        MatGetRowMin_SeqAIJ,
26782af78befSBarry Smith        0,
2679599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2680d519adbfSMatthew Knepley /*114*/0,
2681599ef60dSHong Zhang        0,
26823c2a7987SHong Zhang        0,
2683fe97e370SBarry Smith        0,
2684fbdbba38SShri Abhyankar        0,
2685fbdbba38SShri Abhyankar /*119*/0,
2686fbdbba38SShri Abhyankar        0,
2687fbdbba38SShri Abhyankar        0,
268882d44351SHong Zhang        0,
268982d44351SHong Zhang        MatGetMultiProcBlock_SeqAIJ
26909e29f15eSvictorle };
269117ab2063SBarry Smith 
2692fb2e594dSBarry Smith EXTERN_C_BEGIN
26934a2ae208SSatish Balay #undef __FUNCT__
26944a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2695be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2696bef8e0ddSBarry Smith {
2697bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
269897f1f81fSBarry Smith   PetscInt   i,nz,n;
2699bef8e0ddSBarry Smith 
2700bef8e0ddSBarry Smith   PetscFunctionBegin;
2701bef8e0ddSBarry Smith 
2702bef8e0ddSBarry Smith   nz = aij->maxnz;
2703d0f46423SBarry Smith   n  = mat->rmap->n;
2704bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2705bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2706bef8e0ddSBarry Smith   }
2707bef8e0ddSBarry Smith   aij->nz = nz;
2708bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2709bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2710bef8e0ddSBarry Smith   }
2711bef8e0ddSBarry Smith 
2712bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2713bef8e0ddSBarry Smith }
2714fb2e594dSBarry Smith EXTERN_C_END
2715bef8e0ddSBarry Smith 
27164a2ae208SSatish Balay #undef __FUNCT__
27174a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2718bef8e0ddSBarry Smith /*@
2719bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2720bef8e0ddSBarry Smith        in the matrix.
2721bef8e0ddSBarry Smith 
2722bef8e0ddSBarry Smith   Input Parameters:
2723bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2724bef8e0ddSBarry Smith -  indices - the column indices
2725bef8e0ddSBarry Smith 
272615091d37SBarry Smith   Level: advanced
272715091d37SBarry Smith 
2728bef8e0ddSBarry Smith   Notes:
2729bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2730bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2731bef8e0ddSBarry Smith   of the MatSetValues() operation.
2732bef8e0ddSBarry Smith 
2733bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2734d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2735bef8e0ddSBarry Smith 
2736bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2737bef8e0ddSBarry Smith 
2738b9617806SBarry Smith     The indices should start with zero, not one.
2739b9617806SBarry Smith 
2740bef8e0ddSBarry Smith @*/
2741be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2742bef8e0ddSBarry Smith {
274397f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2744bef8e0ddSBarry Smith 
2745bef8e0ddSBarry Smith   PetscFunctionBegin;
27460700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
27474482741eSBarry Smith   PetscValidPointer(indices,2);
2748c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2749bef8e0ddSBarry Smith   if (f) {
2750bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2751bef8e0ddSBarry Smith   } else {
2752e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2753bef8e0ddSBarry Smith   }
2754bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2755bef8e0ddSBarry Smith }
2756bef8e0ddSBarry Smith 
2757be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2758be6bf707SBarry Smith 
2759fb2e594dSBarry Smith EXTERN_C_BEGIN
27604a2ae208SSatish Balay #undef __FUNCT__
27614a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2762be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2763be6bf707SBarry Smith {
2764be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27656849ba73SBarry Smith   PetscErrorCode ierr;
2766d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2767be6bf707SBarry Smith 
2768be6bf707SBarry Smith   PetscFunctionBegin;
2769be6bf707SBarry Smith   if (aij->nonew != 1) {
2770e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2771be6bf707SBarry Smith   }
2772be6bf707SBarry Smith 
2773be6bf707SBarry Smith   /* allocate space for values if not already there */
2774be6bf707SBarry Smith   if (!aij->saved_values) {
277587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
27769518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2777be6bf707SBarry Smith   }
2778be6bf707SBarry Smith 
2779be6bf707SBarry Smith   /* copy values over */
278087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2781be6bf707SBarry Smith   PetscFunctionReturn(0);
2782be6bf707SBarry Smith }
2783fb2e594dSBarry Smith EXTERN_C_END
2784be6bf707SBarry Smith 
27854a2ae208SSatish Balay #undef __FUNCT__
2786b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2787be6bf707SBarry Smith /*@
2788be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2789be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2790be6bf707SBarry Smith        nonlinear portion.
2791be6bf707SBarry Smith 
2792be6bf707SBarry Smith    Collect on Mat
2793be6bf707SBarry Smith 
2794be6bf707SBarry Smith   Input Parameters:
27950e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2796be6bf707SBarry Smith 
279715091d37SBarry Smith   Level: advanced
279815091d37SBarry Smith 
2799be6bf707SBarry Smith   Common Usage, with SNESSolve():
2800be6bf707SBarry Smith $    Create Jacobian matrix
2801be6bf707SBarry Smith $    Set linear terms into matrix
2802be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2803be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2804be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2805512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2806be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2807be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2808be6bf707SBarry Smith $    In your Jacobian routine
2809be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2810be6bf707SBarry Smith $      Set nonlinear terms in matrix
2811be6bf707SBarry Smith 
2812be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2813be6bf707SBarry Smith $    // build linear portion of Jacobian
2814512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2815be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2816be6bf707SBarry Smith $    loop over nonlinear iterations
2817be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2818be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2819be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2820be6bf707SBarry Smith $       Solve linear system with Jacobian
2821be6bf707SBarry Smith $    endloop
2822be6bf707SBarry Smith 
2823be6bf707SBarry Smith   Notes:
2824be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2825512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2826be6bf707SBarry Smith     calling this routine.
2827be6bf707SBarry Smith 
28280c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
28290c468ba9SBarry Smith     and does not allocated additional space.
28300c468ba9SBarry Smith 
2831be6bf707SBarry Smith .seealso: MatRetrieveValues()
2832be6bf707SBarry Smith 
2833be6bf707SBarry Smith @*/
2834be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2835be6bf707SBarry Smith {
2836dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2837be6bf707SBarry Smith 
2838be6bf707SBarry Smith   PetscFunctionBegin;
28390700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2840e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2841e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2842be6bf707SBarry Smith 
2843c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2844be6bf707SBarry Smith   if (f) {
2845be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2846be6bf707SBarry Smith   } else {
2847e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to store values");
2848be6bf707SBarry Smith   }
2849be6bf707SBarry Smith   PetscFunctionReturn(0);
2850be6bf707SBarry Smith }
2851be6bf707SBarry Smith 
2852fb2e594dSBarry Smith EXTERN_C_BEGIN
28534a2ae208SSatish Balay #undef __FUNCT__
28544a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2855be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2856be6bf707SBarry Smith {
2857be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
28586849ba73SBarry Smith   PetscErrorCode ierr;
2859d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2860be6bf707SBarry Smith 
2861be6bf707SBarry Smith   PetscFunctionBegin;
2862be6bf707SBarry Smith   if (aij->nonew != 1) {
2863e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2864be6bf707SBarry Smith   }
2865be6bf707SBarry Smith   if (!aij->saved_values) {
2866e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2867be6bf707SBarry Smith   }
2868be6bf707SBarry Smith   /* copy values over */
286987828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2870be6bf707SBarry Smith   PetscFunctionReturn(0);
2871be6bf707SBarry Smith }
2872fb2e594dSBarry Smith EXTERN_C_END
2873be6bf707SBarry Smith 
28744a2ae208SSatish Balay #undef __FUNCT__
28754a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2876be6bf707SBarry Smith /*@
2877be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2878be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2879be6bf707SBarry Smith        nonlinear portion.
2880be6bf707SBarry Smith 
2881be6bf707SBarry Smith    Collect on Mat
2882be6bf707SBarry Smith 
2883be6bf707SBarry Smith   Input Parameters:
2884be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2885be6bf707SBarry Smith 
288615091d37SBarry Smith   Level: advanced
288715091d37SBarry Smith 
2888be6bf707SBarry Smith .seealso: MatStoreValues()
2889be6bf707SBarry Smith 
2890be6bf707SBarry Smith @*/
2891be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2892be6bf707SBarry Smith {
2893dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2894be6bf707SBarry Smith 
2895be6bf707SBarry Smith   PetscFunctionBegin;
28960700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2897e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2898e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2899be6bf707SBarry Smith 
2900c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2901be6bf707SBarry Smith   if (f) {
2902be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2903be6bf707SBarry Smith   } else {
2904e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2905be6bf707SBarry Smith   }
2906be6bf707SBarry Smith   PetscFunctionReturn(0);
2907be6bf707SBarry Smith }
2908be6bf707SBarry Smith 
2909f83d6046SBarry Smith 
2910be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
29114a2ae208SSatish Balay #undef __FUNCT__
29124a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
291317ab2063SBarry Smith /*@C
2914682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
29150d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
29166e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
291751c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
29182bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
291917ab2063SBarry Smith 
2920db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2921db81eaa0SLois Curfman McInnes 
292217ab2063SBarry Smith    Input Parameters:
2923db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
292417ab2063SBarry Smith .  m - number of rows
292517ab2063SBarry Smith .  n - number of columns
292617ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
292751c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
29282bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
292917ab2063SBarry Smith 
293017ab2063SBarry Smith    Output Parameter:
2931416022c9SBarry Smith .  A - the matrix
293217ab2063SBarry Smith 
2933175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2934ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2935175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2936175b88e8SBarry Smith 
2937b259b22eSLois Curfman McInnes    Notes:
293849a6f317SBarry Smith    If nnz is given then nz is ignored
293949a6f317SBarry Smith 
294017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
294117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
29420002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
294344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
294417ab2063SBarry Smith 
294517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2946a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
29473d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
29486da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
294917ab2063SBarry Smith 
2950682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
29514fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2952682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
29536c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
29546c7ebb05SLois Curfman McInnes 
29556c7ebb05SLois Curfman McInnes    Options Database Keys:
2956698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
29579db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
295817ab2063SBarry Smith 
2959027ccd11SLois Curfman McInnes    Level: intermediate
2960027ccd11SLois Curfman McInnes 
296136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
296236db0b34SBarry Smith 
296317ab2063SBarry Smith @*/
2964be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
296517ab2063SBarry Smith {
2966dfbe8321SBarry Smith   PetscErrorCode ierr;
29676945ee14SBarry Smith 
29683a40ed3dSBarry Smith   PetscFunctionBegin;
2969f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2970117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2971c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2972d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
2973273d9f13SBarry Smith   PetscFunctionReturn(0);
2974273d9f13SBarry Smith }
2975273d9f13SBarry Smith 
29764a2ae208SSatish Balay #undef __FUNCT__
29774a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2978273d9f13SBarry Smith /*@C
2979273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2980273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2981273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2982273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2983273d9f13SBarry Smith 
2984273d9f13SBarry Smith    Collective on MPI_Comm
2985273d9f13SBarry Smith 
2986273d9f13SBarry Smith    Input Parameters:
2987117016b1SBarry Smith +  B - The matrix-free
2988273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2989273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2990273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2991273d9f13SBarry Smith 
2992273d9f13SBarry Smith    Notes:
299349a6f317SBarry Smith      If nnz is given then nz is ignored
299449a6f317SBarry Smith 
2995273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2996273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2997273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2998273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2999273d9f13SBarry Smith 
3000273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3001273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3002273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3003273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3004273d9f13SBarry Smith 
3005aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3006aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3007aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3008aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3009aa95bbe8SBarry Smith 
3010a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3011a96a251dSBarry Smith    entries or columns indices
3012a96a251dSBarry Smith 
3013273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3014273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3015273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3016273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3017273d9f13SBarry Smith 
3018273d9f13SBarry Smith    Options Database Keys:
3019698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3020698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3021273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3022273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3023273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3024273d9f13SBarry Smith 
3025273d9f13SBarry Smith    Level: intermediate
3026273d9f13SBarry Smith 
3027aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3028273d9f13SBarry Smith 
3029273d9f13SBarry Smith @*/
3030be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3031273d9f13SBarry Smith {
303297f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
3033a23d5eceSKris Buschelman 
3034a23d5eceSKris Buschelman   PetscFunctionBegin;
3035a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
3036a23d5eceSKris Buschelman   if (f) {
3037a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
3038a23d5eceSKris Buschelman   }
3039a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3040a23d5eceSKris Buschelman }
3041a23d5eceSKris Buschelman 
3042a23d5eceSKris Buschelman EXTERN_C_BEGIN
3043a23d5eceSKris Buschelman #undef __FUNCT__
3044a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
3045d28bb7d2SJed Brown PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3046a23d5eceSKris Buschelman {
3047273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3048ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
30496849ba73SBarry Smith   PetscErrorCode ierr;
305097f1f81fSBarry Smith   PetscInt       i;
3051273d9f13SBarry Smith 
3052273d9f13SBarry Smith   PetscFunctionBegin;
3053d5d45c9bSBarry Smith 
3054a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3055c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3056c461c341SBarry Smith     nz             = 0;
3057c461c341SBarry Smith   }
3058c461c341SBarry Smith 
305926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
306026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
306126283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
306226283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3063899cda47SBarry Smith 
3064435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3065e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3066b73539f3SBarry Smith   if (nnz) {
3067d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3068e32f2f54SBarry 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]);
3069e32f2f54SBarry 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);
3070b73539f3SBarry Smith     }
3071b73539f3SBarry Smith   }
3072b73539f3SBarry Smith 
3073273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3074273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3075273d9f13SBarry Smith 
3076ab93d7beSBarry Smith   if (!skipallocation) {
30772ee49352SLisandro Dalcin     if (!b->imax) {
3078d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3079d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
30802ee49352SLisandro Dalcin     }
3081273d9f13SBarry Smith     if (!nnz) {
3082435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3083c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3084d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3085d0f46423SBarry Smith       nz = nz*B->rmap->n;
3086273d9f13SBarry Smith     } else {
3087273d9f13SBarry Smith       nz = 0;
3088d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3089273d9f13SBarry Smith     }
3090ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3091d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3092ab93d7beSBarry Smith 
3093273d9f13SBarry Smith     /* allocate the matrix space */
30942ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3095d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3096d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3097bfeeae90SHong Zhang     b->i[0] = 0;
3098d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
30995da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
31005da197adSKris Buschelman     }
3101273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3102e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3103e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3104c461c341SBarry Smith   } else {
3105e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3106e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3107c461c341SBarry Smith   }
3108273d9f13SBarry Smith 
3109273d9f13SBarry Smith   b->nz                = 0;
3110273d9f13SBarry Smith   b->maxnz             = nz;
3111273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3112273d9f13SBarry Smith   PetscFunctionReturn(0);
3113273d9f13SBarry Smith }
3114a23d5eceSKris Buschelman EXTERN_C_END
3115273d9f13SBarry Smith 
3116a1661176SMatthew Knepley #undef  __FUNCT__
3117a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
311858d36128SBarry Smith /*@
3119a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3120a1661176SMatthew Knepley 
3121a1661176SMatthew Knepley    Input Parameters:
3122a1661176SMatthew Knepley +  B - the matrix
3123a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3124a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3125a1661176SMatthew Knepley -  v - optional values in the matrix
3126a1661176SMatthew Knepley 
3127a1661176SMatthew Knepley    Level: developer
3128a1661176SMatthew Knepley 
312958d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
313058d36128SBarry Smith 
3131a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3132a1661176SMatthew Knepley 
3133a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3134a1661176SMatthew Knepley @*/
3135a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3136a1661176SMatthew Knepley {
3137a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3138a1661176SMatthew Knepley   PetscErrorCode ierr;
3139a1661176SMatthew Knepley 
3140a1661176SMatthew Knepley   PetscFunctionBegin;
31410700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
3142a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3143a1661176SMatthew Knepley   if (f) {
3144a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3145a1661176SMatthew Knepley   }
3146a1661176SMatthew Knepley   PetscFunctionReturn(0);
3147a1661176SMatthew Knepley }
3148a1661176SMatthew Knepley 
3149a1661176SMatthew Knepley EXTERN_C_BEGIN
3150a1661176SMatthew Knepley #undef  __FUNCT__
3151a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3152b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3153a1661176SMatthew Knepley {
3154a1661176SMatthew Knepley   PetscInt       i;
3155a1661176SMatthew Knepley   PetscInt       m,n;
3156a1661176SMatthew Knepley   PetscInt       nz;
3157a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3158a1661176SMatthew Knepley   PetscScalar    *values;
3159a1661176SMatthew Knepley   PetscErrorCode ierr;
3160a1661176SMatthew Knepley 
3161a1661176SMatthew Knepley   PetscFunctionBegin;
3162a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3163a1661176SMatthew Knepley 
316465e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3165a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3166a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3167b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3168a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
316965e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3170a1661176SMatthew Knepley     nnz[i] = nz;
3171a1661176SMatthew Knepley   }
3172a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3173a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3174a1661176SMatthew Knepley 
3175a1661176SMatthew Knepley   if (v) {
3176a1661176SMatthew Knepley     values = (PetscScalar*) v;
3177a1661176SMatthew Knepley   } else {
31780e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3179a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3180a1661176SMatthew Knepley   }
3181a1661176SMatthew Knepley 
3182a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3183b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3184b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3185a1661176SMatthew Knepley   }
3186a1661176SMatthew Knepley 
3187a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3188a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3189a1661176SMatthew Knepley 
3190a1661176SMatthew Knepley   if (!v) {
3191a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3192a1661176SMatthew Knepley   }
3193a1661176SMatthew Knepley   PetscFunctionReturn(0);
3194a1661176SMatthew Knepley }
3195a1661176SMatthew Knepley EXTERN_C_END
3196a1661176SMatthew Knepley 
31977c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3198be7314b0SBarry Smith #include "private/petscaxpy.h"
3199170fe5c8SBarry Smith 
3200170fe5c8SBarry Smith #undef __FUNCT__
3201170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3202170fe5c8SBarry Smith /*
3203170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3204170fe5c8SBarry Smith 
3205170fe5c8SBarry Smith                n                       p                          p
3206170fe5c8SBarry Smith         (              )       (              )         (                  )
3207170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3208170fe5c8SBarry Smith         (              )       (              )         (                  )
3209170fe5c8SBarry Smith 
3210170fe5c8SBarry Smith */
3211170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3212170fe5c8SBarry Smith {
3213170fe5c8SBarry Smith   PetscErrorCode     ierr;
3214170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3215170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3216170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
32171de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3218170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3219170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3220170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3221170fe5c8SBarry Smith 
3222170fe5c8SBarry Smith   PetscFunctionBegin;
3223d0f46423SBarry Smith   m = A->rmap->n;
3224d0f46423SBarry Smith   n = A->cmap->n;
3225d0f46423SBarry Smith   p = B->cmap->n;
3226170fe5c8SBarry Smith   a = sub_a->v;
3227170fe5c8SBarry Smith   b = sub_b->a;
3228170fe5c8SBarry Smith   c = sub_c->v;
3229170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3230170fe5c8SBarry Smith 
3231170fe5c8SBarry Smith   ii  = sub_b->i;
3232170fe5c8SBarry Smith   idx = sub_b->j;
3233170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3234170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3235170fe5c8SBarry Smith     while (q-->0) {
3236170fe5c8SBarry Smith       c_q = c + m*(*idx);
3237170fe5c8SBarry Smith       a_q = a + m*i;
3238be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3239170fe5c8SBarry Smith       idx++;
3240170fe5c8SBarry Smith       b++;
3241170fe5c8SBarry Smith     }
3242170fe5c8SBarry Smith   }
3243170fe5c8SBarry Smith   PetscFunctionReturn(0);
3244170fe5c8SBarry Smith }
3245170fe5c8SBarry Smith 
3246170fe5c8SBarry Smith #undef __FUNCT__
3247170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3248170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3249170fe5c8SBarry Smith {
3250170fe5c8SBarry Smith   PetscErrorCode ierr;
3251d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3252170fe5c8SBarry Smith   Mat            Cmat;
3253170fe5c8SBarry Smith 
3254170fe5c8SBarry Smith   PetscFunctionBegin;
3255e32f2f54SBarry 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);
325639804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3257170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3258170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3259170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3260170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3261170fe5c8SBarry Smith   *C = Cmat;
3262170fe5c8SBarry Smith   PetscFunctionReturn(0);
3263170fe5c8SBarry Smith }
3264170fe5c8SBarry Smith 
3265170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3266170fe5c8SBarry Smith #undef __FUNCT__
3267170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3268170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3269170fe5c8SBarry Smith {
3270170fe5c8SBarry Smith   PetscErrorCode ierr;
3271170fe5c8SBarry Smith 
3272170fe5c8SBarry Smith   PetscFunctionBegin;
3273170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3274170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3275170fe5c8SBarry Smith   }
3276170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3277170fe5c8SBarry Smith   PetscFunctionReturn(0);
3278170fe5c8SBarry Smith }
3279170fe5c8SBarry Smith 
3280170fe5c8SBarry Smith 
32810bad9183SKris Buschelman /*MC
3282fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
32830bad9183SKris Buschelman    based on compressed sparse row format.
32840bad9183SKris Buschelman 
32850bad9183SKris Buschelman    Options Database Keys:
32860bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
32870bad9183SKris Buschelman 
32880bad9183SKris Buschelman   Level: beginner
32890bad9183SKris Buschelman 
3290f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
32910bad9183SKris Buschelman M*/
32920bad9183SKris Buschelman 
3293a6175056SHong Zhang EXTERN_C_BEGIN
3294b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3295b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3296b5e56a35SBarry Smith #endif
329765460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3298af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3299af1023dbSSatish Balay #endif
33005a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3301b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3302a83b8d76SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
3303ace3abfcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3304611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3305bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3306611f576cSBarry Smith #endif
3307611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
33085c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3309611f576cSBarry Smith #endif
3310f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3311f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3312f3c0ef26SHong Zhang #endif
3313611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
33145c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3315611f576cSBarry Smith #endif
3316eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3317eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3318eb3b5408SSatish Balay #endif
3319586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3320586621ddSJed Brown extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3321586621ddSJed Brown #endif
3322719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3323719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3324719d5645SBarry Smith #endif
3325b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3326b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
3327b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEnginePut_SeqAIJ(PetscObject,void*);
3328b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEngineGet_SeqAIJ(PetscObject,void*);
3329b3866ffcSBarry Smith #endif
333017667f90SBarry Smith EXTERN_C_END
333117667f90SBarry Smith 
3332b24902e0SBarry Smith 
333317667f90SBarry Smith EXTERN_C_BEGIN
33344a2ae208SSatish Balay #undef __FUNCT__
33354a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3336be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3337273d9f13SBarry Smith {
3338273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3339dfbe8321SBarry Smith   PetscErrorCode ierr;
334038baddfdSBarry Smith   PetscMPIInt    size;
3341273d9f13SBarry Smith 
3342273d9f13SBarry Smith   PetscFunctionBegin;
33437adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3344e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3345273d9f13SBarry Smith 
334638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3347b0a32e0cSBarry Smith   B->data             = (void*)b;
3348549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
334990f02eecSBarry Smith   B->mapping          = 0;
3350416022c9SBarry Smith   b->row              = 0;
3351416022c9SBarry Smith   b->col              = 0;
335282bf6240SBarry Smith   b->icol             = 0;
3353b810aeb4SBarry Smith   b->reallocs         = 0;
335436db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3355f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3356416022c9SBarry Smith   b->nonew             = 0;
3357416022c9SBarry Smith   b->diag              = 0;
3358416022c9SBarry Smith   b->solve_work        = 0;
33592a1b7f2aSHong Zhang   B->spptr             = 0;
3360be6bf707SBarry Smith   b->saved_values      = 0;
3361d7f994e1SBarry Smith   b->idiag             = 0;
336271f1c65dSBarry Smith   b->mdiag             = 0;
336371f1c65dSBarry Smith   b->ssor_work         = 0;
336471f1c65dSBarry Smith   b->omega             = 1.0;
336571f1c65dSBarry Smith   b->fshift            = 0.0;
336671f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3367a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3368a30b2313SHong Zhang   b->xtoy              = 0;
3369a30b2313SHong Zhang   b->XtoY              = 0;
337073e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3371d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3372d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3373d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3374d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
337588e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
337617ab2063SBarry Smith 
337735d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3378b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3379b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C",
3380b3866ffcSBarry Smith 					   "MatGetFactor_seqaij_matlab",
3381b3866ffcSBarry Smith 					   MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3382b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3383b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3384b3866ffcSBarry Smith #endif
3385b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3386ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
3387b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3388b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3389b5e56a35SBarry Smith #endif
339065460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3391ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C",
3392719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3393719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3394719d5645SBarry Smith #endif
3395611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3396ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C",
33975c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
33985c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3399611f576cSBarry Smith #endif
3400f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3401ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
3402f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3403f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3404f3c0ef26SHong Zhang #endif
3405611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3406ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
34075c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
34085c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3409611f576cSBarry Smith #endif
3410611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3411ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
3412bccb9932SShri Abhyankar                                      "MatGetFactor_aij_mumps",
3413bccb9932SShri Abhyankar                                      MatGetFactor_aij_mumps);CHKERRQ(ierr);
3414611f576cSBarry Smith #endif
3415eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3416ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C",
3417eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3418eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3419eb3b5408SSatish Balay #endif
3420586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3421586621ddSJed Brown     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C",
3422586621ddSJed Brown                                      "MatGetFactor_seqaij_cholmod",
3423586621ddSJed Brown                                      MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3424586621ddSJed Brown #endif
3425719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3426ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C",
3427719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3428719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3429719d5645SBarry Smith #endif
3430ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3431b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3432b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3433ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3434db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3435db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3436174acd27SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C",
3437a83b8d76SBarry Smith                                      "MatGetFactor_seqaij_bas",
3438174acd27SBarry Smith                                      MatGetFactor_seqaij_bas);
3439f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3440bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3441bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3442f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3443be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3444bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3445f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3446be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3447bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3448a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3449a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3450a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
345185fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
345285fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
345385fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
34545a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",
34555a11e1b2SBarry Smith                                      "MatConvert_SeqAIJ_SeqAIJPERM",
34565a11e1b2SBarry Smith                                       MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
34575a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",
34585a11e1b2SBarry Smith                                      "MatConvert_SeqAIJ_SeqAIJCRL",
34595a11e1b2SBarry Smith                                       MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
34605fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
34615fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
34625fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
34631cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
34641cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
34651cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3466a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3467a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3468a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3469a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3470a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3471a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
347205b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
347305b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
347405b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3475170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3476170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3477170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3478170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3479170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3480170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3481170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3482170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3483170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
34844108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
348517667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
34863a40ed3dSBarry Smith   PetscFunctionReturn(0);
348717ab2063SBarry Smith }
3488273d9f13SBarry Smith EXTERN_C_END
348917ab2063SBarry Smith 
34904a2ae208SSatish Balay #undef __FUNCT__
3491b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3492b24902e0SBarry Smith /*
3493b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3494b24902e0SBarry Smith */
3495ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
349617ab2063SBarry Smith {
3497416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
34986849ba73SBarry Smith   PetscErrorCode ierr;
3499d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
350017ab2063SBarry Smith 
35013a40ed3dSBarry Smith   PetscFunctionBegin;
3502273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3503273d9f13SBarry Smith 
3504d5f3da31SBarry Smith   C->factortype     = A->factortype;
3505416022c9SBarry Smith   c->row            = 0;
3506416022c9SBarry Smith   c->col            = 0;
350782bf6240SBarry Smith   c->icol           = 0;
35086ad4291fSHong Zhang   c->reallocs       = 0;
350917ab2063SBarry Smith 
35106ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
351117ab2063SBarry Smith 
351226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
351326283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
351426283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
351526283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3516eec197d1SBarry Smith 
351733b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
35189518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
351917ab2063SBarry Smith   for (i=0; i<m; i++) {
3520416022c9SBarry Smith     c->imax[i] = a->imax[i];
3521416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
352217ab2063SBarry Smith   }
352317ab2063SBarry Smith 
352417ab2063SBarry Smith   /* allocate the matrix space */
3525f77e22a1SHong Zhang   if (mallocmatspace){
3526a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
35279518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3528f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
352997f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
353017ab2063SBarry Smith     if (m > 0) {
353197f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3532be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3533bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3534be6bf707SBarry Smith       } else {
3535bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
353617ab2063SBarry Smith       }
353708480c60SBarry Smith     }
3538f77e22a1SHong Zhang   }
353917ab2063SBarry Smith 
35406ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3541416022c9SBarry Smith   c->roworiented       = a->roworiented;
3542416022c9SBarry Smith   c->nonew             = a->nonew;
3543416022c9SBarry Smith   if (a->diag) {
354497f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
354552e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
354617ab2063SBarry Smith     for (i=0; i<m; i++) {
3547416022c9SBarry Smith       c->diag[i] = a->diag[i];
354817ab2063SBarry Smith     }
35493a40ed3dSBarry Smith   } else c->diag           = 0;
35506ad4291fSHong Zhang   c->solve_work            = 0;
35516ad4291fSHong Zhang   c->saved_values          = 0;
35526ad4291fSHong Zhang   c->idiag                 = 0;
355371f1c65dSBarry Smith   c->ssor_work             = 0;
3554a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3555e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3556e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
35576ad4291fSHong Zhang   c->xtoy                  = 0;
35586ad4291fSHong Zhang   c->XtoY                  = 0;
35596ad4291fSHong Zhang 
3560416022c9SBarry Smith   c->nz                 = a->nz;
35618ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3562273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3563754ec7b1SSatish Balay 
35646ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
35656ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
35666ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
35676ad4291fSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
35686ad4291fSHong Zhang     i = a->compressedrow.nrows;
35690e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
35706ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
35716ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
357227ea64f8SHong Zhang   } else {
357327ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
357427ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
357527ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
35766ad4291fSHong Zhang   }
357788e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
35784108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
35794846f1f5SKris Buschelman 
35807adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
35813a40ed3dSBarry Smith   PetscFunctionReturn(0);
358217ab2063SBarry Smith }
358317ab2063SBarry Smith 
35844a2ae208SSatish Balay #undef __FUNCT__
3585b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3586b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3587b24902e0SBarry Smith {
3588b24902e0SBarry Smith   PetscErrorCode ierr;
3589b24902e0SBarry Smith 
3590b24902e0SBarry Smith   PetscFunctionBegin;
3591b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
35924b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3593b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3594f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3595b24902e0SBarry Smith   PetscFunctionReturn(0);
3596b24902e0SBarry Smith }
3597b24902e0SBarry Smith 
3598b24902e0SBarry Smith #undef __FUNCT__
35994a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3600112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3601fbdbba38SShri Abhyankar {
3602fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3603fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3604fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3605fbdbba38SShri Abhyankar   int            fd;
3606fbdbba38SShri Abhyankar   PetscMPIInt    size;
3607fbdbba38SShri Abhyankar   MPI_Comm       comm;
3608fbdbba38SShri Abhyankar 
3609fbdbba38SShri Abhyankar   PetscFunctionBegin;
3610fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3611fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3612fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3613fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3614fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3615fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
3616fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3617fbdbba38SShri Abhyankar 
3618fbdbba38SShri Abhyankar   if (nz < 0) {
3619fbdbba38SShri Abhyankar     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3620fbdbba38SShri Abhyankar   }
3621fbdbba38SShri Abhyankar 
3622fbdbba38SShri Abhyankar   /* read in row lengths */
3623fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3624fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3625fbdbba38SShri Abhyankar 
3626fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
3627fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3628fbdbba38SShri Abhyankar   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);
3629fbdbba38SShri Abhyankar 
3630fbdbba38SShri Abhyankar   /* set global size if not set already*/
3631f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
3632fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3633aabbc4fbSShri Abhyankar   } else {
3634fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
3635fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
3636f501eaabSShri Abhyankar     if (M != rows ||  N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols);
3637aabbc4fbSShri Abhyankar   }
3638fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
3639fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
3640fbdbba38SShri Abhyankar 
3641fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
3642fbdbba38SShri Abhyankar 
3643fbdbba38SShri Abhyankar   /* read in nonzero values */
3644fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
3645fbdbba38SShri Abhyankar 
3646fbdbba38SShri Abhyankar   /* set matrix "i" values */
3647fbdbba38SShri Abhyankar   a->i[0] = 0;
3648fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
3649fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3650fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
3651fbdbba38SShri Abhyankar   }
3652fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3653fbdbba38SShri Abhyankar 
3654fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3655fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3656fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
3657fbdbba38SShri Abhyankar }
3658fbdbba38SShri Abhyankar 
3659fbdbba38SShri Abhyankar #undef __FUNCT__
3660b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3661ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
36627264ac53SSatish Balay {
36637264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3664dfbe8321SBarry Smith   PetscErrorCode ierr;
3665eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3666eeffb40dSHong Zhang   PetscInt k;
3667eeffb40dSHong Zhang #endif
36687264ac53SSatish Balay 
36693a40ed3dSBarry Smith   PetscFunctionBegin;
3670bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3671d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3672ca44d042SBarry Smith     *flg = PETSC_FALSE;
3673ca44d042SBarry Smith     PetscFunctionReturn(0);
3674bcd2baecSBarry Smith   }
36757264ac53SSatish Balay 
36767264ac53SSatish Balay   /* if the a->i are the same */
3677d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3678abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
36797264ac53SSatish Balay 
36807264ac53SSatish Balay   /* if a->j are the same */
368197f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3682abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3683bcd2baecSBarry Smith 
3684bcd2baecSBarry Smith   /* if a->a are the same */
3685eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3686eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3687eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3688eeffb40dSHong Zhang       *flg = PETSC_FALSE;
36893a40ed3dSBarry Smith       PetscFunctionReturn(0);
3690eeffb40dSHong Zhang     }
3691eeffb40dSHong Zhang   }
3692eeffb40dSHong Zhang #else
3693eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3694eeffb40dSHong Zhang #endif
3695eeffb40dSHong Zhang   PetscFunctionReturn(0);
36967264ac53SSatish Balay }
369736db0b34SBarry Smith 
36984a2ae208SSatish Balay #undef __FUNCT__
36994a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
370005869f15SSatish Balay /*@
370136db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
370236db0b34SBarry Smith               provided by the user.
370336db0b34SBarry Smith 
3704c75a6043SHong Zhang       Collective on MPI_Comm
370536db0b34SBarry Smith 
370636db0b34SBarry Smith    Input Parameters:
370736db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
370836db0b34SBarry Smith .   m - number of rows
370936db0b34SBarry Smith .   n - number of columns
371036db0b34SBarry Smith .   i - row indices
371136db0b34SBarry Smith .   j - column indices
371236db0b34SBarry Smith -   a - matrix values
371336db0b34SBarry Smith 
371436db0b34SBarry Smith    Output Parameter:
371536db0b34SBarry Smith .   mat - the matrix
371636db0b34SBarry Smith 
371736db0b34SBarry Smith    Level: intermediate
371836db0b34SBarry Smith 
371936db0b34SBarry Smith    Notes:
37200551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
372136db0b34SBarry Smith     once the matrix is destroyed
372236db0b34SBarry Smith 
372336db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
372436db0b34SBarry Smith 
3725bfeeae90SHong Zhang        The i and j indices are 0 based
372636db0b34SBarry Smith 
3727a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3728a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3729a4552177SSatish Balay     as shown:
3730a4552177SSatish Balay 
3731a4552177SSatish Balay         1 0 0
3732a4552177SSatish Balay         2 0 3
3733a4552177SSatish Balay         4 5 6
3734a4552177SSatish Balay 
3735a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
37369985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3737a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3738a4552177SSatish Balay 
37399985e31cSBarry Smith 
37402fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
374136db0b34SBarry Smith 
374236db0b34SBarry Smith @*/
3743a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
374436db0b34SBarry Smith {
3745dfbe8321SBarry Smith   PetscErrorCode ierr;
3746cbcfb4deSHong Zhang   PetscInt       ii;
374736db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3748cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3749cbcfb4deSHong Zhang   PetscInt       jj;
3750cbcfb4deSHong Zhang #endif
375136db0b34SBarry Smith 
375236db0b34SBarry Smith   PetscFunctionBegin;
3753a96a251dSBarry Smith   if (i[0]) {
3754e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
375536db0b34SBarry Smith   }
3756f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3757f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3758ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3759ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3760ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3761ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3762ab93d7beSBarry Smith 
376336db0b34SBarry Smith   aij->i = i;
376436db0b34SBarry Smith   aij->j = j;
376536db0b34SBarry Smith   aij->a = a;
376636db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
376736db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3768e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3769e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
377036db0b34SBarry Smith 
377136db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
377236db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
37732515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
3774e32f2f54SBarry 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]);
37759985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
3776e32f2f54SBarry 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);
3777e32f2f54SBarry 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);
37789985e31cSBarry Smith     }
377936db0b34SBarry Smith #endif
378036db0b34SBarry Smith   }
37812515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
378236db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3783e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3784e32f2f54SBarry 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]);
378536db0b34SBarry Smith   }
378636db0b34SBarry Smith #endif
378736db0b34SBarry Smith 
3788b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3789b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
379036db0b34SBarry Smith   PetscFunctionReturn(0);
379136db0b34SBarry Smith }
379236db0b34SBarry Smith 
3793cc8ba8e1SBarry Smith #undef __FUNCT__
3794ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3795dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3796cc8ba8e1SBarry Smith {
3797dfbe8321SBarry Smith   PetscErrorCode ierr;
3798cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
379936db0b34SBarry Smith 
3800cc8ba8e1SBarry Smith   PetscFunctionBegin;
38018ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3802cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3803cc8ba8e1SBarry Smith     a->coloring = coloring;
380412c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
380597f1f81fSBarry Smith     PetscInt             i,*larray;
380612c595b3SBarry Smith     ISColoring      ocoloring;
380708b6dcc0SBarry Smith     ISColoringValue *colors;
380812c595b3SBarry Smith 
380912c595b3SBarry Smith     /* set coloring for diagonal portion */
38100e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3811d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
381212c595b3SBarry Smith       larray[i] = i;
381312c595b3SBarry Smith     }
3814d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
38150e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3816d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
381712c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
381812c595b3SBarry Smith     }
381912c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3820d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
382112c595b3SBarry Smith     a->coloring = ocoloring;
382212c595b3SBarry Smith   }
3823cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3824cc8ba8e1SBarry Smith }
3825cc8ba8e1SBarry Smith 
3826dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3827ee4f033dSBarry Smith EXTERN_C_BEGIN
382829c1e371SBarry Smith #include "adic/ad_utils.h"
3829ee4f033dSBarry Smith EXTERN_C_END
3830cc8ba8e1SBarry Smith 
3831cc8ba8e1SBarry Smith #undef __FUNCT__
3832ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3833dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3834cc8ba8e1SBarry Smith {
3835cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3836d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
38374440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
383808b6dcc0SBarry Smith   ISColoringValue *color;
3839cc8ba8e1SBarry Smith 
3840cc8ba8e1SBarry Smith   PetscFunctionBegin;
3841e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
38424440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3843cc8ba8e1SBarry Smith   color = a->coloring->colors;
3844cc8ba8e1SBarry Smith   /* loop over rows */
3845cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3846cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3847cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3848cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3849cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3850cc8ba8e1SBarry Smith     }
38514440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3852ee4f033dSBarry Smith   }
3853ee4f033dSBarry Smith   PetscFunctionReturn(0);
3854ee4f033dSBarry Smith }
3855ee4f033dSBarry Smith #endif
3856ee4f033dSBarry Smith 
3857ee4f033dSBarry Smith #undef __FUNCT__
3858ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
385997f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3860ee4f033dSBarry Smith {
3861ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3862d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
386354f21887SBarry Smith   MatScalar       *v = a->a;
386454f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
386508b6dcc0SBarry Smith   ISColoringValue *color;
3866ee4f033dSBarry Smith 
3867ee4f033dSBarry Smith   PetscFunctionBegin;
3868e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3869ee4f033dSBarry Smith   color = a->coloring->colors;
3870ee4f033dSBarry Smith   /* loop over rows */
3871ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3872ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3873ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3874ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3875ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3876ee4f033dSBarry Smith     }
3877ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3878cc8ba8e1SBarry Smith   }
3879cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3880cc8ba8e1SBarry Smith }
388136db0b34SBarry Smith 
388281824310SBarry Smith /*
388381824310SBarry Smith     Special version for direct calls from Fortran
388481824310SBarry Smith */
388581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
388681824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
388781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
388881824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
388981824310SBarry Smith #endif
389081824310SBarry Smith 
389181824310SBarry Smith /* Change these macros so can be used in void function */
389281824310SBarry Smith #undef CHKERRQ
38937adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
389481824310SBarry Smith #undef SETERRQ2
3895e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
389681824310SBarry Smith 
389781824310SBarry Smith EXTERN_C_BEGIN
389881824310SBarry Smith #undef __FUNCT__
389981824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
39001f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
390181824310SBarry Smith {
390281824310SBarry Smith   Mat            A = *AA;
390381824310SBarry Smith   PetscInt       m = *mm, n = *nn;
390481824310SBarry Smith   InsertMode     is = *isis;
390581824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
390681824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
390781824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
390881824310SBarry Smith   PetscErrorCode ierr;
390981824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
391054f21887SBarry Smith   MatScalar      *ap,value,*aa;
3911ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
3912ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
391381824310SBarry Smith 
391481824310SBarry Smith   PetscFunctionBegin;
3915d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
391681824310SBarry Smith   imax = a->imax;
391781824310SBarry Smith   ai = a->i;
391881824310SBarry Smith   ailen = a->ilen;
391981824310SBarry Smith   aj = a->j;
392081824310SBarry Smith   aa = a->a;
392181824310SBarry Smith 
392281824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
392381824310SBarry Smith     row  = im[k];
392481824310SBarry Smith     if (row < 0) continue;
392581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3926d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
392781824310SBarry Smith #endif
392881824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
392981824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
393081824310SBarry Smith     low  = 0;
393181824310SBarry Smith     high = nrow;
393281824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
393381824310SBarry Smith       if (in[l] < 0) continue;
393481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3935d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
393681824310SBarry Smith #endif
393781824310SBarry Smith       col = in[l];
393881824310SBarry Smith       if (roworiented) {
393981824310SBarry Smith         value = v[l + k*n];
394081824310SBarry Smith       } else {
394181824310SBarry Smith         value = v[k + l*m];
394281824310SBarry Smith       }
394381824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
394481824310SBarry Smith 
394581824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
394681824310SBarry Smith       lastcol = col;
394781824310SBarry Smith       while (high-low > 5) {
394881824310SBarry Smith         t = (low+high)/2;
394981824310SBarry Smith         if (rp[t] > col) high = t;
395081824310SBarry Smith         else             low  = t;
395181824310SBarry Smith       }
395281824310SBarry Smith       for (i=low; i<high; i++) {
395381824310SBarry Smith         if (rp[i] > col) break;
395481824310SBarry Smith         if (rp[i] == col) {
395581824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
395681824310SBarry Smith           else                  ap[i] = value;
395781824310SBarry Smith           goto noinsert;
395881824310SBarry Smith         }
395981824310SBarry Smith       }
396081824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
396181824310SBarry Smith       if (nonew == 1) goto noinsert;
39627adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3963fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
396481824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
396581824310SBarry Smith       /* shift up all the later entries in this row */
396681824310SBarry Smith       for (ii=N; ii>=i; ii--) {
396781824310SBarry Smith         rp[ii+1] = rp[ii];
396881824310SBarry Smith         ap[ii+1] = ap[ii];
396981824310SBarry Smith       }
397081824310SBarry Smith       rp[i] = col;
397181824310SBarry Smith       ap[i] = value;
397281824310SBarry Smith       noinsert:;
397381824310SBarry Smith       low = i + 1;
397481824310SBarry Smith     }
397581824310SBarry Smith     ailen[row] = nrow;
397681824310SBarry Smith   }
397781824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
397881824310SBarry Smith   PetscFunctionReturnVoid();
397981824310SBarry Smith }
398081824310SBarry Smith EXTERN_C_END
3981