xref: /petsc/src/mat/impls/aij/seq/aij.c (revision eeffb40d691afbdd57a8091619e7ddd44ac5fdca)
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*/
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
134a2ae208SSatish Balay #undef __FUNCT__
1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1579299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1679299369SBarry Smith {
1779299369SBarry Smith   PetscErrorCode ierr;
1879299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
19d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
2054f21887SBarry Smith   MatScalar      *aa = aij->a;
2154f21887SBarry Smith   PetscScalar    *v;
2209f38230SBarry Smith   PetscTruth     missing;
2379299369SBarry Smith 
2479299369SBarry Smith   PetscFunctionBegin;
2509f38230SBarry Smith   if (Y->assembled) {
2609f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2709f38230SBarry Smith     if (!missing) {
2879299369SBarry Smith       diag = aij->diag;
2979299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3079299369SBarry Smith       if (is == INSERT_VALUES) {
3179299369SBarry Smith 	for (i=0; i<m; i++) {
3279299369SBarry Smith 	  aa[diag[i]] = v[i];
3379299369SBarry Smith 	}
3479299369SBarry Smith       } else {
3579299369SBarry Smith 	for (i=0; i<m; i++) {
3679299369SBarry Smith 	  aa[diag[i]] += v[i];
3779299369SBarry Smith 	}
3879299369SBarry Smith       }
3979299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4079299369SBarry Smith       PetscFunctionReturn(0);
4179299369SBarry Smith     }
4209f38230SBarry Smith   }
4309f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4409f38230SBarry Smith   PetscFunctionReturn(0);
4509f38230SBarry Smith }
4679299369SBarry Smith 
4779299369SBarry Smith #undef __FUNCT__
484a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
493acb8795SBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5017ab2063SBarry Smith {
51416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
52dfbe8321SBarry Smith   PetscErrorCode ierr;
5397f1f81fSBarry Smith   PetscInt       i,ishift;
5417ab2063SBarry Smith 
553a40ed3dSBarry Smith   PetscFunctionBegin;
56d0f46423SBarry Smith   *m     = A->rmap->n;
573a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
58bfeeae90SHong Zhang   ishift = 0;
5953e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
60d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
61bfeeae90SHong Zhang   } else if (oshift == 1) {
62d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
633b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
64d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
65d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
66ecc77c7aSBarry Smith     if (ja) {
6797f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
683b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
69ecc77c7aSBarry Smith     }
706945ee14SBarry Smith   } else {
71ecc77c7aSBarry Smith     *ia = a->i;
72ecc77c7aSBarry Smith     if (ja) *ja = a->j;
73a2ce50c7SBarry Smith   }
743a40ed3dSBarry Smith   PetscFunctionReturn(0);
75a2744918SBarry Smith }
76a2744918SBarry Smith 
774a2ae208SSatish Balay #undef __FUNCT__
784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
793acb8795SBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
806945ee14SBarry Smith {
81dfbe8321SBarry Smith   PetscErrorCode ierr;
826945ee14SBarry Smith 
833a40ed3dSBarry Smith   PetscFunctionBegin;
843a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
85bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
86606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
87ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
88bcd2baecSBarry Smith   }
893a40ed3dSBarry Smith   PetscFunctionReturn(0);
9017ab2063SBarry Smith }
9117ab2063SBarry Smith 
924a2ae208SSatish Balay #undef __FUNCT__
934a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
943acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
953b2fbd54SBarry Smith {
963b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
97dfbe8321SBarry Smith   PetscErrorCode ierr;
98d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
9997f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1003b2fbd54SBarry Smith 
1013a40ed3dSBarry Smith   PetscFunctionBegin;
102899cda47SBarry Smith   *nn = n;
1033a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1043b2fbd54SBarry Smith   if (symmetric) {
105d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1063b2fbd54SBarry Smith   } else {
10797f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1113b2fbd54SBarry Smith     jj = a->j;
1123b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
113bfeeae90SHong Zhang       collengths[jj[i]]++;
1143b2fbd54SBarry Smith     }
1153b2fbd54SBarry Smith     cia[0] = oshift;
1163b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1173b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1183b2fbd54SBarry Smith     }
11997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1203b2fbd54SBarry Smith     jj   = a->j;
121a93ec695SBarry Smith     for (row=0; row<m; row++) {
122a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
123a93ec695SBarry Smith       for (i=0; i<mr; i++) {
124bfeeae90SHong Zhang         col = *jj++;
1253b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1263b2fbd54SBarry Smith       }
1273b2fbd54SBarry Smith     }
128606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1293b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1303b2fbd54SBarry Smith   }
1313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1323b2fbd54SBarry Smith }
1333b2fbd54SBarry Smith 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1363acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1373b2fbd54SBarry Smith {
138dfbe8321SBarry Smith   PetscErrorCode ierr;
139606d414cSSatish Balay 
1403a40ed3dSBarry Smith   PetscFunctionBegin;
1413a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1423b2fbd54SBarry Smith 
143606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
144606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1453b2fbd54SBarry Smith 
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1473b2fbd54SBarry Smith }
1483b2fbd54SBarry Smith 
14987d4246cSBarry Smith #undef __FUNCT__
15087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15287d4246cSBarry Smith {
15387d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15487d4246cSBarry Smith   PetscInt       *ai = a->i;
15587d4246cSBarry Smith   PetscErrorCode ierr;
15687d4246cSBarry Smith 
15787d4246cSBarry Smith   PetscFunctionBegin;
15887d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
15987d4246cSBarry Smith   PetscFunctionReturn(0);
16087d4246cSBarry Smith }
16187d4246cSBarry Smith 
162227d817aSBarry Smith #define CHUNKSIZE   15
16317ab2063SBarry Smith 
1644a2ae208SSatish Balay #undef __FUNCT__
1654a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16717ab2063SBarry Smith {
168416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
169e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
17097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1716849ba73SBarry Smith   PetscErrorCode ierr;
172e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17354f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
174edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
175273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17617ab2063SBarry Smith 
1773a40ed3dSBarry Smith   PetscFunctionBegin;
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)
182d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(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)
191d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
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 {
20116371a99SBarry 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;
223cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
224d0f46423SBarry 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];
25597e567efSBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
256d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(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 */
26097e567efSBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
261d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
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);
296552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
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 {
476b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
47717ab2063SBarry Smith     for (i=0; i<m; i++) {
47877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
479416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
480aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
482a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
484a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4853a40ed3dSBarry Smith         } else {
486a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
48717ab2063SBarry Smith         }
48817ab2063SBarry Smith #else
489a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49017ab2063SBarry Smith #endif
49117ab2063SBarry Smith       }
492b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
49317ab2063SBarry Smith     }
494b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
49517ab2063SBarry Smith   }
496b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4973a40ed3dSBarry Smith   PetscFunctionReturn(0);
498416022c9SBarry Smith }
499416022c9SBarry Smith 
5004a2ae208SSatish Balay #undef __FUNCT__
5014a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
502dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
503416022c9SBarry Smith {
504480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
505416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
506dfbe8321SBarry Smith   PetscErrorCode    ierr;
507d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
50836db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
509b0a32e0cSBarry Smith   PetscViewer       viewer;
510f3ef73ceSBarry Smith   PetscViewerFormat format;
511cddf8d76SBarry Smith 
5123a40ed3dSBarry Smith   PetscFunctionBegin;
513480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
514b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
51519bcc07fSBarry Smith 
516b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
517416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5180513a670SBarry Smith 
519fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5200513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
521b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
522416022c9SBarry Smith     for (i=0; i<m; i++) {
523cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
524bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
525bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
526aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
528cddf8d76SBarry Smith #else
529cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
530cddf8d76SBarry Smith #endif
531b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
532cddf8d76SBarry Smith       }
533cddf8d76SBarry Smith     }
534b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
535cddf8d76SBarry Smith     for (i=0; i<m; i++) {
536cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
537bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
538bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
539cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
540b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
541cddf8d76SBarry Smith       }
542cddf8d76SBarry Smith     }
543b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
544cddf8d76SBarry Smith     for (i=0; i<m; i++) {
545cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
546bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
547bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
548aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
54936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
550cddf8d76SBarry Smith #else
551cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
552cddf8d76SBarry Smith #endif
553b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
554416022c9SBarry Smith       }
555416022c9SBarry Smith     }
5560513a670SBarry Smith   } else {
5570513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5580513a670SBarry Smith     /* first determine max of all nonzero values */
55997f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
560b0a32e0cSBarry Smith     PetscDraw   popup;
56136db0b34SBarry Smith     PetscReal scale;
5620513a670SBarry Smith 
5630513a670SBarry Smith     for (i=0; i<nz; i++) {
5640513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5650513a670SBarry Smith     }
566b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
567b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
568b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5690513a670SBarry Smith     count = 0;
5700513a670SBarry Smith     for (i=0; i<m; i++) {
5710513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
572bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
573bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
57497f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
575b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5760513a670SBarry Smith         count++;
5770513a670SBarry Smith       }
5780513a670SBarry Smith     }
5790513a670SBarry Smith   }
580480ef9eaSBarry Smith   PetscFunctionReturn(0);
581480ef9eaSBarry Smith }
582cddf8d76SBarry Smith 
5834a2ae208SSatish Balay #undef __FUNCT__
5844a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
585dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
586480ef9eaSBarry Smith {
587dfbe8321SBarry Smith   PetscErrorCode ierr;
588b0a32e0cSBarry Smith   PetscDraw      draw;
58936db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
590480ef9eaSBarry Smith   PetscTruth     isnull;
591480ef9eaSBarry Smith 
592480ef9eaSBarry Smith   PetscFunctionBegin;
593b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
594b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
595480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
596480ef9eaSBarry Smith 
597480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
598d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
599480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
600b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
601b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
602480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6033a40ed3dSBarry Smith   PetscFunctionReturn(0);
604416022c9SBarry Smith }
605416022c9SBarry Smith 
6064a2ae208SSatish Balay #undef __FUNCT__
6074a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
608dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
609416022c9SBarry Smith {
610dfbe8321SBarry Smith   PetscErrorCode ierr;
6116805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
612416022c9SBarry Smith 
6133a40ed3dSBarry Smith   PetscFunctionBegin;
61432077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
615fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
616fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
617c45a1595SBarry Smith   if (iascii) {
6183a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6190f5bd95cSBarry Smith   } else if (isbinary) {
6203a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6210f5bd95cSBarry Smith   } else if (isdraw) {
6223a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6235cd90555SBarry Smith   } else {
624958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
62517ab2063SBarry Smith   }
6264108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
6273a40ed3dSBarry Smith   PetscFunctionReturn(0);
62817ab2063SBarry Smith }
62919bcc07fSBarry Smith 
6304a2ae208SSatish Balay #undef __FUNCT__
6314a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
632dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
63317ab2063SBarry Smith {
634416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6356849ba73SBarry Smith   PetscErrorCode ierr;
63697f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
637d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
63854f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6393447b6efSHong Zhang   PetscReal      ratio=0.6;
64017ab2063SBarry Smith 
6413a40ed3dSBarry Smith   PetscFunctionBegin;
6423a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
64317ab2063SBarry Smith 
64443ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
64517ab2063SBarry Smith   for (i=1; i<m; i++) {
646416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
64717ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
64894a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
64917ab2063SBarry Smith     if (fshift) {
650bfeeae90SHong Zhang       ip = aj + ai[i] ;
651bfeeae90SHong Zhang       ap = aa + ai[i] ;
65217ab2063SBarry Smith       N  = ailen[i];
65317ab2063SBarry Smith       for (j=0; j<N; j++) {
65417ab2063SBarry Smith         ip[j-fshift] = ip[j];
65517ab2063SBarry Smith         ap[j-fshift] = ap[j];
65617ab2063SBarry Smith       }
65717ab2063SBarry Smith     }
65817ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
65917ab2063SBarry Smith   }
66017ab2063SBarry Smith   if (m) {
66117ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
66217ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
66317ab2063SBarry Smith   }
66417ab2063SBarry Smith   /* reset ilen and imax for each row */
66517ab2063SBarry Smith   for (i=0; i<m; i++) {
66617ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
66717ab2063SBarry Smith   }
668bfeeae90SHong Zhang   a->nz = ai[m];
66928b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
67028b2fa4aSMatthew Knepley     SETERRQ3(PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
67128b2fa4aSMatthew Knepley   }
67217ab2063SBarry Smith 
67309f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
674d0f46423SBarry 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);
675ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
676ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
677a0e1a203SBarry Smith 
678dd5f02e7SSatish Balay   a->reallocs          = 0;
6794e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
68036db0b34SBarry Smith   a->rmax              = rmax;
6814e220ebcSLois Curfman McInnes 
682cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
683317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
68488e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
68571c2f376SKris Buschelman 
6864108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
68771f1c65dSBarry Smith 
68871f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6893a40ed3dSBarry Smith   PetscFunctionReturn(0);
69017ab2063SBarry Smith }
69117ab2063SBarry Smith 
6924a2ae208SSatish Balay #undef __FUNCT__
69399cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
69499cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
69599cafbc1SBarry Smith {
69699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
69799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
69854f21887SBarry Smith   MatScalar      *aa = a->a;
69999cafbc1SBarry Smith 
70099cafbc1SBarry Smith   PetscFunctionBegin;
70199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
70299cafbc1SBarry Smith   PetscFunctionReturn(0);
70399cafbc1SBarry Smith }
70499cafbc1SBarry Smith 
70599cafbc1SBarry Smith #undef __FUNCT__
70699cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
70799cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
70899cafbc1SBarry Smith {
70999cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
71099cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
71154f21887SBarry Smith   MatScalar      *aa = a->a;
71299cafbc1SBarry Smith 
71399cafbc1SBarry Smith   PetscFunctionBegin;
71499cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
71599cafbc1SBarry Smith   PetscFunctionReturn(0);
71699cafbc1SBarry Smith }
71799cafbc1SBarry Smith 
71899cafbc1SBarry Smith #undef __FUNCT__
7194a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
720dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
72117ab2063SBarry Smith {
722416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
723dfbe8321SBarry Smith   PetscErrorCode ierr;
7243a40ed3dSBarry Smith 
7253a40ed3dSBarry Smith   PetscFunctionBegin;
726d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7273a40ed3dSBarry Smith   PetscFunctionReturn(0);
72817ab2063SBarry Smith }
729416022c9SBarry Smith 
7304a2ae208SSatish Balay #undef __FUNCT__
7314a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
732dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
73317ab2063SBarry Smith {
734416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
735dfbe8321SBarry Smith   PetscErrorCode ierr;
736d5d45c9bSBarry Smith 
7373a40ed3dSBarry Smith   PetscFunctionBegin;
738aa482453SBarry Smith #if defined(PETSC_USE_LOG)
739d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
74017ab2063SBarry Smith #endif
741e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
742c38d4ed2SBarry Smith   if (a->row) {
743c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
744c38d4ed2SBarry Smith   }
745c38d4ed2SBarry Smith   if (a->col) {
746c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
747c38d4ed2SBarry Smith   }
74805b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
74905b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
75071f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
75105b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
75282bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
75305b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
754cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
75505b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
756407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
757d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
758a30b2313SHong Zhang 
7594108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
7604846f1f5SKris Buschelman 
761606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
762901853e0SKris Buschelman 
763dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
764901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
765901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
766901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
767901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
768901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
769ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
770901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
771901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
772a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
773901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7743a40ed3dSBarry Smith   PetscFunctionReturn(0);
77517ab2063SBarry Smith }
77617ab2063SBarry Smith 
7774a2ae208SSatish Balay #undef __FUNCT__
7784a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7794e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
78017ab2063SBarry Smith {
781416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7824846f1f5SKris Buschelman   PetscErrorCode ierr;
7833a40ed3dSBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785a65d3064SKris Buschelman   switch (op) {
786a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7874e0d8c25SBarry Smith       a->roworiented       = flg;
788a65d3064SKris Buschelman       break;
789a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
790a9817697SBarry Smith       a->keepnonzeropattern    = flg;
791a65d3064SKris Buschelman       break;
792512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
793512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
794a65d3064SKris Buschelman       break;
795a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
7964e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
797a65d3064SKris Buschelman       break;
798a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
7994e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
800a65d3064SKris Buschelman       break;
80128b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
80228b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
80328b2fa4aSMatthew Knepley       break;
804a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8054e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8060df259c2SBarry Smith       break;
807d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8084e0d8c25SBarry Smith       a->compressedrow.use = flg;
809d487561eSHong Zhang       break;
8104e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
811a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
812a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
813290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
814a65d3064SKris Buschelman       break;
815a65d3064SKris Buschelman     default:
81671c2f376SKris Buschelman       break;
817a65d3064SKris Buschelman   }
8184108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
8193a40ed3dSBarry Smith   PetscFunctionReturn(0);
82017ab2063SBarry Smith }
82117ab2063SBarry Smith 
8224a2ae208SSatish Balay #undef __FUNCT__
8234a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
824dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
82517ab2063SBarry Smith {
826416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8276849ba73SBarry Smith   PetscErrorCode ierr;
82897f1f81fSBarry Smith   PetscInt       i,j,n;
82987828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
83017ab2063SBarry Smith 
8313a40ed3dSBarry Smith   PetscFunctionBegin;
8322dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8331ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
83436db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
835d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
836d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
837bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
838bfeeae90SHong Zhang       if (a->j[j] == i) {
839416022c9SBarry Smith         x[i] = a->a[j];
84017ab2063SBarry Smith         break;
84117ab2063SBarry Smith       }
84217ab2063SBarry Smith     }
84317ab2063SBarry Smith   }
8441ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8453a40ed3dSBarry Smith   PetscFunctionReturn(0);
84617ab2063SBarry Smith }
84717ab2063SBarry Smith 
848b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
8494a2ae208SSatish Balay #undef __FUNCT__
8504a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
851dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
85217ab2063SBarry Smith {
853416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8545c897100SBarry Smith   PetscScalar       *x,*y;
855dfbe8321SBarry Smith   PetscErrorCode    ierr;
856d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
8575c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
858a77337e4SBarry Smith   MatScalar         *v;
859a77337e4SBarry Smith   PetscScalar       alpha;
86004fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
8613447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8624eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8635c897100SBarry Smith #endif
86417ab2063SBarry Smith 
8653a40ed3dSBarry Smith   PetscFunctionBegin;
8662e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8671ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8681ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8695c897100SBarry Smith 
8705c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
871bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8725c897100SBarry Smith #else
8733447b6efSHong Zhang   if (usecprow){
8743447b6efSHong Zhang     m    = cprow.nrows;
8753447b6efSHong Zhang     ii   = cprow.i;
8767b2bb3b9SHong Zhang     ridx = cprow.rindex;
8773447b6efSHong Zhang   } else {
8783447b6efSHong Zhang     ii = a->i;
8793447b6efSHong Zhang   }
88017ab2063SBarry Smith   for (i=0; i<m; i++) {
8813447b6efSHong Zhang     idx   = a->j + ii[i] ;
8823447b6efSHong Zhang     v     = a->a + ii[i] ;
8833447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8843447b6efSHong Zhang     if (usecprow){
8857b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8863447b6efSHong Zhang     } else {
88717ab2063SBarry Smith       alpha = x[i];
8883447b6efSHong Zhang     }
88904fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
89017ab2063SBarry Smith   }
8915c897100SBarry Smith #endif
892dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
8931ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8941ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8953a40ed3dSBarry Smith   PetscFunctionReturn(0);
89617ab2063SBarry Smith }
89717ab2063SBarry Smith 
8984a2ae208SSatish Balay #undef __FUNCT__
8995c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
900dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9015c897100SBarry Smith {
902dfbe8321SBarry Smith   PetscErrorCode ierr;
9035c897100SBarry Smith 
9045c897100SBarry Smith   PetscFunctionBegin;
905170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9065c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9075c897100SBarry Smith   PetscFunctionReturn(0);
9085c897100SBarry Smith }
9095c897100SBarry Smith 
910a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9115c897100SBarry Smith #undef __FUNCT__
9124a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
913dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
91417ab2063SBarry Smith {
915416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
916d9fead3dSBarry Smith   PetscScalar       *y;
91754f21887SBarry Smith   const PetscScalar *x;
91854f21887SBarry Smith   const MatScalar   *aa;
919dfbe8321SBarry Smith   PetscErrorCode    ierr;
920003131ecSBarry Smith   PetscInt          m=A->rmap->n;
921003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9228aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
923362ced78SSatish Balay   PetscScalar       sum;
92497952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
92517ab2063SBarry Smith 
926b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
92797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
928fee21e36SBarry Smith #endif
929fee21e36SBarry Smith 
9303a40ed3dSBarry Smith   PetscFunctionBegin;
931d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9321ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
93397952fefSHong Zhang   aj  = a->j;
93497952fefSHong Zhang   aa  = a->a;
935416022c9SBarry Smith   ii  = a->i;
9364eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
93797952fefSHong Zhang     m    = a->compressedrow.nrows;
93897952fefSHong Zhang     ii   = a->compressedrow.i;
93997952fefSHong Zhang     ridx = a->compressedrow.rindex;
94097952fefSHong Zhang     for (i=0; i<m; i++){
94197952fefSHong Zhang       n   = ii[i+1] - ii[i];
94297952fefSHong Zhang       aj  = a->j + ii[i];
94397952fefSHong Zhang       aa  = a->a + ii[i];
94497952fefSHong Zhang       sum = 0.0;
945a46b3154SVictor Eijkhout       nonzerorow += (n>0);
946003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
947003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
94897952fefSHong Zhang       y[*ridx++] = sum;
94997952fefSHong Zhang     }
95097952fefSHong Zhang   } else { /* do not use compressed row format */
951b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
952b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
953b05257ddSBarry Smith #else
95417ab2063SBarry Smith     for (i=0; i<m; i++) {
955003131ecSBarry Smith       n   = ii[i+1] - ii[i];
956003131ecSBarry Smith       aj  = a->j + ii[i];
957003131ecSBarry Smith       aa  = a->a + ii[i];
95817ab2063SBarry Smith       sum  = 0.0;
959a46b3154SVictor Eijkhout       nonzerorow += (n>0);
960003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
96117ab2063SBarry Smith       y[i] = sum;
96217ab2063SBarry Smith     }
9638d195f9aSBarry Smith #endif
964b05257ddSBarry Smith   }
965dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
966d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9671ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9683a40ed3dSBarry Smith   PetscFunctionReturn(0);
96917ab2063SBarry Smith }
97017ab2063SBarry Smith 
971a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
9724a2ae208SSatish Balay #undef __FUNCT__
9734a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
974dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
97517ab2063SBarry Smith {
976416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
97754f21887SBarry Smith   PetscScalar     *x,*y,*z;
97854f21887SBarry Smith   const MatScalar *aa;
979dfbe8321SBarry Smith   PetscErrorCode  ierr;
980d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
981aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
98297952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
983362ced78SSatish Balay   PetscScalar     sum;
98497952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
985e36a17ebSSatish Balay #endif
9869ea0dfa2SSatish Balay 
9873a40ed3dSBarry Smith   PetscFunctionBegin;
9881ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9891ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9902e8a6d31SBarry Smith   if (zz != yy) {
9911ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9922e8a6d31SBarry Smith   } else {
9932e8a6d31SBarry Smith     z = y;
9942e8a6d31SBarry Smith   }
995bfeeae90SHong Zhang 
99697952fefSHong Zhang   aj  = a->j;
99797952fefSHong Zhang   aa  = a->a;
998cddf8d76SBarry Smith   ii  = a->i;
999aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
100097952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
100102ab625aSSatish Balay #else
10024eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10034eb6d288SHong Zhang     if (zz != yy){
10044eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10054eb6d288SHong Zhang     }
100697952fefSHong Zhang     m    = a->compressedrow.nrows;
100797952fefSHong Zhang     ii   = a->compressedrow.i;
100897952fefSHong Zhang     ridx = a->compressedrow.rindex;
100997952fefSHong Zhang     for (i=0; i<m; i++){
101097952fefSHong Zhang       n  = ii[i+1] - ii[i];
101197952fefSHong Zhang       aj  = a->j + ii[i];
101297952fefSHong Zhang       aa  = a->a + ii[i];
101397952fefSHong Zhang       sum = y[*ridx];
101497952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
101597952fefSHong Zhang       z[*ridx++] = sum;
101697952fefSHong Zhang     }
101797952fefSHong Zhang   } else { /* do not use compressed row format */
101817ab2063SBarry Smith     for (i=0; i<m; i++) {
10199ea0dfa2SSatish Balay       jrow = ii[i];
10209ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
102117ab2063SBarry Smith       sum  = y[i];
10229ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
102397952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10249ea0dfa2SSatish Balay       }
102517ab2063SBarry Smith       z[i] = sum;
102617ab2063SBarry Smith     }
102797952fefSHong Zhang   }
102802ab625aSSatish Balay #endif
1029dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10301ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10311ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10322e8a6d31SBarry Smith   if (zz != yy) {
10331ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10342e8a6d31SBarry Smith   }
10353a40ed3dSBarry Smith   PetscFunctionReturn(0);
103617ab2063SBarry Smith }
103717ab2063SBarry Smith 
103817ab2063SBarry Smith /*
103917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
104017ab2063SBarry Smith */
10414a2ae208SSatish Balay #undef __FUNCT__
10424a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1043dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
104417ab2063SBarry Smith {
1045416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10466849ba73SBarry Smith   PetscErrorCode ierr;
1047d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
104817ab2063SBarry Smith 
10493a40ed3dSBarry Smith   PetscFunctionBegin;
105009f38230SBarry Smith   if (!a->diag) {
105109f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10529518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
105309f38230SBarry Smith   }
1054d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
105509f38230SBarry Smith     a->diag[i] = a->i[i+1];
1056bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1057bfeeae90SHong Zhang       if (a->j[j] == i) {
105809f38230SBarry Smith         a->diag[i] = j;
105917ab2063SBarry Smith         break;
106017ab2063SBarry Smith       }
106117ab2063SBarry Smith     }
106217ab2063SBarry Smith   }
10633a40ed3dSBarry Smith   PetscFunctionReturn(0);
106417ab2063SBarry Smith }
106517ab2063SBarry Smith 
1066be5855fcSBarry Smith /*
1067be5855fcSBarry Smith      Checks for missing diagonals
1068be5855fcSBarry Smith */
10694a2ae208SSatish Balay #undef __FUNCT__
10704a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
107109f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1072be5855fcSBarry Smith {
1073be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
107497f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1075be5855fcSBarry Smith 
1076be5855fcSBarry Smith   PetscFunctionBegin;
107709f38230SBarry Smith   *missing = PETSC_FALSE;
1078d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
107909f38230SBarry Smith     *missing  = PETSC_TRUE;
108009f38230SBarry Smith     if (d) *d = 0;
108109f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
108209f38230SBarry Smith   } else {
1083f1e2ffcdSBarry Smith     diag = a->diag;
1084d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1085bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
108609f38230SBarry Smith 	*missing = PETSC_TRUE;
108709f38230SBarry Smith 	if (d) *d = i;
108809f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
108909f38230SBarry Smith       }
1090be5855fcSBarry Smith     }
1091be5855fcSBarry Smith   }
1092be5855fcSBarry Smith   PetscFunctionReturn(0);
1093be5855fcSBarry Smith }
1094be5855fcSBarry Smith 
109571f1c65dSBarry Smith EXTERN_C_BEGIN
109671f1c65dSBarry Smith #undef __FUNCT__
109771f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
109871f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
109971f1c65dSBarry Smith {
110071f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
110171f1c65dSBarry Smith   PetscErrorCode ierr;
1102d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
110354f21887SBarry Smith   MatScalar      *v = a->a;
110454f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
110571f1c65dSBarry Smith 
110671f1c65dSBarry Smith   PetscFunctionBegin;
110771f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
110871f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
110971f1c65dSBarry Smith   diag = a->diag;
111071f1c65dSBarry Smith   if (!a->idiag) {
111171f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
111271f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
111371f1c65dSBarry Smith     v        = a->a;
111471f1c65dSBarry Smith   }
111571f1c65dSBarry Smith   mdiag = a->mdiag;
111671f1c65dSBarry Smith   idiag = a->idiag;
111771f1c65dSBarry Smith 
1118028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
111971f1c65dSBarry Smith     for (i=0; i<m; i++) {
112071f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
112171f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
112271f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
112371f1c65dSBarry Smith     }
112471f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
112571f1c65dSBarry Smith   } else {
112671f1c65dSBarry Smith     for (i=0; i<m; i++) {
112771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
112871f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
112971f1c65dSBarry Smith     }
1130dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
113171f1c65dSBarry Smith   }
113271f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
113371f1c65dSBarry Smith   PetscFunctionReturn(0);
113471f1c65dSBarry Smith }
11355a9745a3SMatthew Knepley EXTERN_C_END
113671f1c65dSBarry Smith 
1137a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
11384a2ae208SSatish Balay #undef __FUNCT__
113941f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
114041f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
114117ab2063SBarry Smith {
1142416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1143e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1144e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
114554f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1146dfbe8321SBarry Smith   PetscErrorCode     ierr;
1147d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
114897f1f81fSBarry Smith   const PetscInt     *idx,*diag;
114917ab2063SBarry Smith 
11503a40ed3dSBarry Smith   PetscFunctionBegin;
1151b965ef7fSBarry Smith   its = its*lits;
115291723122SBarry Smith 
115371f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
115471f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
115571f1c65dSBarry Smith   a->fshift = fshift;
115671f1c65dSBarry Smith   a->omega  = omega;
1157ed480e8bSBarry Smith 
115871f1c65dSBarry Smith   diag = a->diag;
115971f1c65dSBarry Smith   t     = a->ssor_work;
1160ed480e8bSBarry Smith   idiag = a->idiag;
116171f1c65dSBarry Smith   mdiag = a->mdiag;
1162ed480e8bSBarry Smith 
11631ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1164fb2e594dSBarry Smith   if (xx != bb) {
11651ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1166fb2e594dSBarry Smith   } else {
1167fb2e594dSBarry Smith     b = x;
1168fb2e594dSBarry Smith   }
116971f1c65dSBarry Smith   CHKMEMQ;
1170ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
117117ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
117217ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1173ed480e8bSBarry Smith     bs = b;
117417ab2063SBarry Smith     for (i=0; i<m; i++) {
117571f1c65dSBarry Smith         d    = fshift + mdiag[i];
1176416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1177ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1178ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
117917ab2063SBarry Smith         sum  = b[i]*d/omega;
1180003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
118117ab2063SBarry Smith         x[i] = sum;
118217ab2063SBarry Smith     }
11831ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11841ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1185efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11863a40ed3dSBarry Smith     PetscFunctionReturn(0);
118717ab2063SBarry Smith   }
1188c783ea89SBarry Smith 
118948af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
119029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11913a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
119217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1193887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
119417ab2063SBarry Smith 
119517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
119617ab2063SBarry Smith 
1197887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
119817ab2063SBarry Smith     */
119917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
120017ab2063SBarry Smith 
120117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
120217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1203416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1204ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1205ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
120617ab2063SBarry Smith       sum  = b[i];
1207e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1208ed480e8bSBarry Smith       x[i] = sum*idiag[i];
120917ab2063SBarry Smith     }
121017ab2063SBarry Smith 
121117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1212416022c9SBarry Smith     v = a->a;
1213ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
121417ab2063SBarry Smith 
121517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1216ed480e8bSBarry Smith     ts = t;
1217416022c9SBarry Smith     diag = a->diag;
121817ab2063SBarry Smith     for (i=0; i<m; i++) {
1219416022c9SBarry Smith       n    = diag[i] - a->i[i];
1220ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1221ed480e8bSBarry Smith       v    = a->a + a->i[i];
122217ab2063SBarry Smith       sum  = t[i];
1223003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1224ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1225733d66baSBarry Smith       /*  x = x + t */
1226733d66baSBarry Smith       x[i] += t[i];
122717ab2063SBarry Smith     }
122817ab2063SBarry Smith 
1229dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
12301ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12311ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12323a40ed3dSBarry Smith     PetscFunctionReturn(0);
123317ab2063SBarry Smith   }
123417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
123517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
123617ab2063SBarry Smith       for (i=0; i<m; i++) {
1237416022c9SBarry Smith         n    = diag[i] - a->i[i];
1238ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1239ed480e8bSBarry Smith         v    = a->a + a->i[i];
124017ab2063SBarry Smith         sum  = b[i];
1241e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
12425c99c7daSBarry Smith         t[i] = sum;
1243ed480e8bSBarry Smith         x[i] = sum*idiag[i];
124417ab2063SBarry Smith       }
12455c99c7daSBarry Smith       xb = t;
1246efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12473a40ed3dSBarry Smith     } else xb = b;
124817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
124917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1250416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1251ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1252ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
125317ab2063SBarry Smith         sum  = xb[i];
1254e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
12555c99c7daSBarry Smith         if (xb == b) {
1256ed480e8bSBarry Smith           x[i] = sum*idiag[i];
12575c99c7daSBarry Smith         } else {
12585c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
125917ab2063SBarry Smith         }
12605c99c7daSBarry Smith       }
1261efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
126217ab2063SBarry Smith     }
126317ab2063SBarry Smith     its--;
126417ab2063SBarry Smith   }
126517ab2063SBarry Smith   while (its--) {
126617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
126717ab2063SBarry Smith       for (i=0; i<m; i++) {
1268416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1269ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1270ed480e8bSBarry Smith         v    = a->a + a->i[i];
127117ab2063SBarry Smith         sum  = b[i];
1272e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1273ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
127417ab2063SBarry Smith       }
12759f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
127617ab2063SBarry Smith     }
127717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
127817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1279416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1280ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1281ed480e8bSBarry Smith         v    = a->a + a->i[i];
128217ab2063SBarry Smith         sum  = b[i];
1283e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1284ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
128517ab2063SBarry Smith       }
12869f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
128717ab2063SBarry Smith     }
128817ab2063SBarry Smith   }
12891ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12901ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
129171f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
129217ab2063SBarry Smith }
129317ab2063SBarry Smith 
12942af78befSBarry Smith 
12954a2ae208SSatish Balay #undef __FUNCT__
12964a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1297dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
129817ab2063SBarry Smith {
1299416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13004e220ebcSLois Curfman McInnes 
13013a40ed3dSBarry Smith   PetscFunctionBegin;
13024e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13034e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13044e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13054e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13064e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13074e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13087adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13094e220ebcSLois Curfman McInnes   if (A->factor) {
13104e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13114e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13124e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13134e220ebcSLois Curfman McInnes   } else {
13144e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13154e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13164e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13174e220ebcSLois Curfman McInnes   }
13183a40ed3dSBarry Smith   PetscFunctionReturn(0);
131917ab2063SBarry Smith }
132017ab2063SBarry Smith 
13214a2ae208SSatish Balay #undef __FUNCT__
13224a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1323f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
132417ab2063SBarry Smith {
1325416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1326d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d;
13276849ba73SBarry Smith   PetscErrorCode ierr;
132809f38230SBarry Smith   PetscTruth     missing;
132917ab2063SBarry Smith 
13303a40ed3dSBarry Smith   PetscFunctionBegin;
1331a9817697SBarry Smith   if (a->keepnonzeropattern) {
1332f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
133377431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1334bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1335f1e2ffcdSBarry Smith     }
1336f4df32b1SMatthew Knepley     if (diag != 0.0) {
133709f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
133809f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1339f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1340f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1341f1e2ffcdSBarry Smith       }
1342f1e2ffcdSBarry Smith     }
134388e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1344f1e2ffcdSBarry Smith   } else {
1345f4df32b1SMatthew Knepley     if (diag != 0.0) {
134617ab2063SBarry Smith       for (i=0; i<N; i++) {
134777431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13487ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1349416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1350f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1351bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13527ae801bdSBarry Smith         } else { /* in case row was completely empty */
1353f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
135417ab2063SBarry Smith         }
135517ab2063SBarry Smith       }
13563a40ed3dSBarry Smith     } else {
135717ab2063SBarry Smith       for (i=0; i<N; i++) {
135877431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1359416022c9SBarry Smith         a->ilen[rows[i]] = 0;
136017ab2063SBarry Smith       }
136117ab2063SBarry Smith     }
136288e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1363f1e2ffcdSBarry Smith   }
136443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13653a40ed3dSBarry Smith   PetscFunctionReturn(0);
136617ab2063SBarry Smith }
136717ab2063SBarry Smith 
13684a2ae208SSatish Balay #undef __FUNCT__
13694a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1370a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
137117ab2063SBarry Smith {
1372416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
137397f1f81fSBarry Smith   PetscInt   *itmp;
137417ab2063SBarry Smith 
13753a40ed3dSBarry Smith   PetscFunctionBegin;
1376d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
137717ab2063SBarry Smith 
1378416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1379bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
138017ab2063SBarry Smith   if (idx) {
1381bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1382bfeeae90SHong Zhang     if (*nz) {
13834e093b46SBarry Smith       *idx = itmp;
138417ab2063SBarry Smith     }
138517ab2063SBarry Smith     else *idx = 0;
138617ab2063SBarry Smith   }
13873a40ed3dSBarry Smith   PetscFunctionReturn(0);
138817ab2063SBarry Smith }
138917ab2063SBarry Smith 
1390bfeeae90SHong Zhang /* remove this function? */
13914a2ae208SSatish Balay #undef __FUNCT__
13924a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1393a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
139417ab2063SBarry Smith {
13953a40ed3dSBarry Smith   PetscFunctionBegin;
13963a40ed3dSBarry Smith   PetscFunctionReturn(0);
139717ab2063SBarry Smith }
139817ab2063SBarry Smith 
13994a2ae208SSatish Balay #undef __FUNCT__
14004a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1401dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
140217ab2063SBarry Smith {
1403416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
140454f21887SBarry Smith   MatScalar      *v = a->a;
140536db0b34SBarry Smith   PetscReal      sum = 0.0;
14066849ba73SBarry Smith   PetscErrorCode ierr;
140797f1f81fSBarry Smith   PetscInt       i,j;
140817ab2063SBarry Smith 
14093a40ed3dSBarry Smith   PetscFunctionBegin;
141017ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1411416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1412aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
141336db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
141417ab2063SBarry Smith #else
141517ab2063SBarry Smith       sum += (*v)*(*v); v++;
141617ab2063SBarry Smith #endif
141717ab2063SBarry Smith     }
1418064f8208SBarry Smith     *nrm = sqrt(sum);
14193a40ed3dSBarry Smith   } else if (type == NORM_1) {
142036db0b34SBarry Smith     PetscReal *tmp;
142197f1f81fSBarry Smith     PetscInt    *jj = a->j;
1422d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1423d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1424064f8208SBarry Smith     *nrm = 0.0;
1425416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1426bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
142717ab2063SBarry Smith     }
1428d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1429064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
143017ab2063SBarry Smith     }
1431606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14323a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1433064f8208SBarry Smith     *nrm = 0.0;
1434d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1435bfeeae90SHong Zhang       v = a->a + a->i[j];
143617ab2063SBarry Smith       sum = 0.0;
1437416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1438cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
143917ab2063SBarry Smith       }
1440064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
144117ab2063SBarry Smith     }
14423a40ed3dSBarry Smith   } else {
144329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
144417ab2063SBarry Smith   }
14453a40ed3dSBarry Smith   PetscFunctionReturn(0);
144617ab2063SBarry Smith }
144717ab2063SBarry Smith 
14484a2ae208SSatish Balay #undef __FUNCT__
14494a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1450fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
145117ab2063SBarry Smith {
1452416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1453416022c9SBarry Smith   Mat            C;
14546849ba73SBarry Smith   PetscErrorCode ierr;
1455d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
145654f21887SBarry Smith   MatScalar      *array = a->a;
145717ab2063SBarry Smith 
14583a40ed3dSBarry Smith   PetscFunctionBegin;
1459d0f46423SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1460fc4dec0aSBarry Smith 
1461fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1462d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1463d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1464bfeeae90SHong Zhang 
1465bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14667adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1467d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
14687adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1469ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1470606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1471a541d17aSBarry Smith   } else {
1472a541d17aSBarry Smith     C = *B;
1473a541d17aSBarry Smith   }
1474a541d17aSBarry Smith 
147517ab2063SBarry Smith   for (i=0; i<m; i++) {
147617ab2063SBarry Smith     len    = ai[i+1]-ai[i];
147787d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1478b9b97703SBarry Smith     array += len;
1479b9b97703SBarry Smith     aj    += len;
148017ab2063SBarry Smith   }
14816d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14826d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148317ab2063SBarry Smith 
1484815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1485416022c9SBarry Smith     *B = C;
148617ab2063SBarry Smith   } else {
1487273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
148817ab2063SBarry Smith   }
14893a40ed3dSBarry Smith   PetscFunctionReturn(0);
149017ab2063SBarry Smith }
149117ab2063SBarry Smith 
1492cd0d46ebSvictorle EXTERN_C_BEGIN
1493cd0d46ebSvictorle #undef __FUNCT__
14945fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1495be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1496cd0d46ebSvictorle {
1497cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
149854f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
149954f21887SBarry Smith   MatScalar      *va,*vb;
15006849ba73SBarry Smith   PetscErrorCode ierr;
150197f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1502cd0d46ebSvictorle 
1503cd0d46ebSvictorle   PetscFunctionBegin;
1504cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1505cd0d46ebSvictorle 
1506cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1507cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15085485867bSBarry Smith   if (ma!=nb || na!=mb){
15095485867bSBarry Smith     *f = PETSC_FALSE;
15105485867bSBarry Smith     PetscFunctionReturn(0);
15115485867bSBarry Smith   }
1512cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1513cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1514cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
151597f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
151697f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1517cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1518cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1519cd0d46ebSvictorle 
1520cd0d46ebSvictorle   *f = PETSC_TRUE;
1521cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1522cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
152397f1f81fSBarry Smith       PetscInt         idc,idr;
15245485867bSBarry Smith       PetscScalar vc,vr;
1525cd0d46ebSvictorle       /* column/row index/value */
15265485867bSBarry Smith       idc = adx[aptr[i]];
15275485867bSBarry Smith       idr = bdx[bptr[idc]];
15285485867bSBarry Smith       vc  = va[aptr[i]];
15295485867bSBarry Smith       vr  = vb[bptr[idc]];
15305485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15315485867bSBarry Smith 	*f = PETSC_FALSE;
15325485867bSBarry Smith         goto done;
1533cd0d46ebSvictorle       } else {
15345485867bSBarry Smith 	aptr[i]++;
15355485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1536cd0d46ebSvictorle       }
1537cd0d46ebSvictorle     }
1538cd0d46ebSvictorle   }
1539cd0d46ebSvictorle  done:
1540cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15413aeef889SHong Zhang   if (B) {
15423aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15433aeef889SHong Zhang   }
1544cd0d46ebSvictorle   PetscFunctionReturn(0);
1545cd0d46ebSvictorle }
1546cd0d46ebSvictorle EXTERN_C_END
1547cd0d46ebSvictorle 
15481cbb95d3SBarry Smith EXTERN_C_BEGIN
15491cbb95d3SBarry Smith #undef __FUNCT__
15501cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15511cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15521cbb95d3SBarry Smith {
15531cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
155454f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
155554f21887SBarry Smith   MatScalar      *va,*vb;
15561cbb95d3SBarry Smith   PetscErrorCode ierr;
15571cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15581cbb95d3SBarry Smith 
15591cbb95d3SBarry Smith   PetscFunctionBegin;
15601cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15611cbb95d3SBarry Smith 
15621cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15631cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15641cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15651cbb95d3SBarry Smith     *f = PETSC_FALSE;
15661cbb95d3SBarry Smith     PetscFunctionReturn(0);
15671cbb95d3SBarry Smith   }
15681cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15691cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15701cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
15711cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
15721cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
15731cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
15741cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
15751cbb95d3SBarry Smith 
15761cbb95d3SBarry Smith   *f = PETSC_TRUE;
15771cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
15781cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
15791cbb95d3SBarry Smith       PetscInt         idc,idr;
15801cbb95d3SBarry Smith       PetscScalar vc,vr;
15811cbb95d3SBarry Smith       /* column/row index/value */
15821cbb95d3SBarry Smith       idc = adx[aptr[i]];
15831cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
15841cbb95d3SBarry Smith       vc  = va[aptr[i]];
15851cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
15861cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
15871cbb95d3SBarry Smith 	*f = PETSC_FALSE;
15881cbb95d3SBarry Smith         goto done;
15891cbb95d3SBarry Smith       } else {
15901cbb95d3SBarry Smith 	aptr[i]++;
15911cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
15921cbb95d3SBarry Smith       }
15931cbb95d3SBarry Smith     }
15941cbb95d3SBarry Smith   }
15951cbb95d3SBarry Smith  done:
15961cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
15971cbb95d3SBarry Smith   if (B) {
15981cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
15991cbb95d3SBarry Smith   }
16001cbb95d3SBarry Smith   PetscFunctionReturn(0);
16011cbb95d3SBarry Smith }
16021cbb95d3SBarry Smith EXTERN_C_END
16031cbb95d3SBarry Smith 
16049e29f15eSvictorle #undef __FUNCT__
16059e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1606dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16079e29f15eSvictorle {
1608dfbe8321SBarry Smith   PetscErrorCode ierr;
16099e29f15eSvictorle   PetscFunctionBegin;
16105485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16119e29f15eSvictorle   PetscFunctionReturn(0);
16129e29f15eSvictorle }
16139e29f15eSvictorle 
16144a2ae208SSatish Balay #undef __FUNCT__
16151cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16161cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16171cbb95d3SBarry Smith {
16181cbb95d3SBarry Smith   PetscErrorCode ierr;
16191cbb95d3SBarry Smith   PetscFunctionBegin;
16201cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16211cbb95d3SBarry Smith   PetscFunctionReturn(0);
16221cbb95d3SBarry Smith }
16231cbb95d3SBarry Smith 
16241cbb95d3SBarry Smith #undef __FUNCT__
16254a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1626dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
162717ab2063SBarry Smith {
1628416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
162954f21887SBarry Smith   PetscScalar    *l,*r,x;
163054f21887SBarry Smith   MatScalar      *v;
1631dfbe8321SBarry Smith   PetscErrorCode ierr;
1632d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
163317ab2063SBarry Smith 
16343a40ed3dSBarry Smith   PetscFunctionBegin;
163517ab2063SBarry Smith   if (ll) {
16363ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16373ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1638e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1639d0f46423SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16401ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1641416022c9SBarry Smith     v = a->a;
164217ab2063SBarry Smith     for (i=0; i<m; i++) {
164317ab2063SBarry Smith       x = l[i];
1644416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
164517ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
164617ab2063SBarry Smith     }
16471ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1648efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
164917ab2063SBarry Smith   }
165017ab2063SBarry Smith   if (rr) {
1651e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1652d0f46423SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16531ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1654416022c9SBarry Smith     v = a->a; jj = a->j;
165517ab2063SBarry Smith     for (i=0; i<nz; i++) {
1656bfeeae90SHong Zhang       (*v++) *= r[*jj++];
165717ab2063SBarry Smith     }
16581ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1659efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
166017ab2063SBarry Smith   }
16613a40ed3dSBarry Smith   PetscFunctionReturn(0);
166217ab2063SBarry Smith }
166317ab2063SBarry Smith 
16644a2ae208SSatish Balay #undef __FUNCT__
16654a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
166697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
166717ab2063SBarry Smith {
1668db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16696849ba73SBarry Smith   PetscErrorCode ierr;
1670d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
167197f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
16725d0c19d7SBarry Smith   const PetscInt *irow,*icol;
16735d0c19d7SBarry Smith   PetscInt       nrows,ncols;
167497f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
167554f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1676416022c9SBarry Smith   Mat            C;
167714ca34e6SBarry Smith   PetscTruth     stride,sorted;
167817ab2063SBarry Smith 
16793a40ed3dSBarry Smith   PetscFunctionBegin;
168014ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
168114ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
168214ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
168314ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
168499141d43SSatish Balay 
168517ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1686b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1687b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
168817ab2063SBarry Smith 
1689fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1690fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1691fee21e36SBarry Smith   if (stride && step == 1) {
169202834360SBarry Smith     /* special case of contiguous rows */
169397f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
169431ebf83bSSatish Balay     starts = lens + nrows;
169502834360SBarry Smith     /* loop over new rows determining lens and starting points */
169602834360SBarry Smith     for (i=0; i<nrows; i++) {
1697bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1698a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
169902834360SBarry Smith       for (k=kstart; k<kend; k++) {
1700bfeeae90SHong Zhang         if (aj[k] >= first) {
170102834360SBarry Smith           starts[i] = k;
170202834360SBarry Smith           break;
170302834360SBarry Smith 	}
170402834360SBarry Smith       }
1705a2744918SBarry Smith       sum = 0;
170602834360SBarry Smith       while (k < kend) {
1707bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1708a2744918SBarry Smith         sum++;
170902834360SBarry Smith       }
1710a2744918SBarry Smith       lens[i] = sum;
171102834360SBarry Smith     }
171202834360SBarry Smith     /* create submatrix */
1713cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
171497f1f81fSBarry Smith       PetscInt n_cols,n_rows;
171508480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
171629bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1717d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
171808480c60SBarry Smith       C = *B;
17193a40ed3dSBarry Smith     } else {
17207adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1721f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17227adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1723ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
172408480c60SBarry Smith     }
1725db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1726db02288aSLois Curfman McInnes 
172702834360SBarry Smith     /* loop over rows inserting into submatrix */
1728db02288aSLois Curfman McInnes     a_new    = c->a;
1729db02288aSLois Curfman McInnes     j_new    = c->j;
1730db02288aSLois Curfman McInnes     i_new    = c->i;
1731bfeeae90SHong Zhang 
173202834360SBarry Smith     for (i=0; i<nrows; i++) {
1733a2744918SBarry Smith       ii    = starts[i];
1734a2744918SBarry Smith       lensi = lens[i];
1735a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1736a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
173702834360SBarry Smith       }
173887828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1739a2744918SBarry Smith       a_new      += lensi;
1740a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1741a2744918SBarry Smith       c->ilen[i]  = lensi;
174202834360SBarry Smith     }
1743606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17443a40ed3dSBarry Smith   } else {
174502834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
174697f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1747bfeeae90SHong Zhang 
174897f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
174997f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
175017ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
175102834360SBarry Smith     /* determine lens of each row */
175202834360SBarry Smith     for (i=0; i<nrows; i++) {
1753bfeeae90SHong Zhang       kstart  = ai[irow[i]];
175402834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
175502834360SBarry Smith       lens[i] = 0;
175602834360SBarry Smith       for (k=kstart; k<kend; k++) {
1757bfeeae90SHong Zhang         if (smap[aj[k]]) {
175802834360SBarry Smith           lens[i]++;
175902834360SBarry Smith         }
176002834360SBarry Smith       }
176102834360SBarry Smith     }
176217ab2063SBarry Smith     /* Create and fill new matrix */
1763a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17640f5bd95cSBarry Smith       PetscTruth equal;
17650f5bd95cSBarry Smith 
176699141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1767d0f46423SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1768d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17690f5bd95cSBarry Smith       if (!equal) {
177029bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
177199141d43SSatish Balay       }
1772d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
177308480c60SBarry Smith       C = *B;
17743a40ed3dSBarry Smith     } else {
17757adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1776f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17777adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1778ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
177908480c60SBarry Smith     }
178099141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
178117ab2063SBarry Smith     for (i=0; i<nrows; i++) {
178299141d43SSatish Balay       row    = irow[i];
1783bfeeae90SHong Zhang       kstart = ai[row];
178499141d43SSatish Balay       kend   = kstart + a->ilen[row];
1785bfeeae90SHong Zhang       mat_i  = c->i[i];
178699141d43SSatish Balay       mat_j  = c->j + mat_i;
178799141d43SSatish Balay       mat_a  = c->a + mat_i;
178899141d43SSatish Balay       mat_ilen = c->ilen + i;
178917ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1790bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1791ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
179299141d43SSatish Balay           *mat_a++ = a->a[k];
179399141d43SSatish Balay           (*mat_ilen)++;
179499141d43SSatish Balay 
179517ab2063SBarry Smith         }
179617ab2063SBarry Smith       }
179717ab2063SBarry Smith     }
179802834360SBarry Smith     /* Free work space */
179902834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1800606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1801606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
180202834360SBarry Smith   }
18036d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18046d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
180517ab2063SBarry Smith 
180617ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1807416022c9SBarry Smith   *B = C;
18083a40ed3dSBarry Smith   PetscFunctionReturn(0);
180917ab2063SBarry Smith }
181017ab2063SBarry Smith 
1811a871dcd8SBarry Smith /*
1812a871dcd8SBarry Smith */
18134a2ae208SSatish Balay #undef __FUNCT__
18144a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18150481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1816a871dcd8SBarry Smith {
181763b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1818dfbe8321SBarry Smith   PetscErrorCode ierr;
181963b91edcSBarry Smith   Mat            outA;
1820b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
182163b91edcSBarry Smith 
18223a40ed3dSBarry Smith   PetscFunctionBegin;
1823d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1824b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1825b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1826a871dcd8SBarry Smith 
182763b91edcSBarry Smith   outA          = inA;
18285c9eb25fSBarry Smith   inA->factor   = MAT_FACTOR_LU;
1829c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1830c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1831c3122656SLisandro Dalcin   a->row = row;
1832c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1833c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1834c3122656SLisandro Dalcin   a->col = col;
183563b91edcSBarry Smith 
183636db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1837b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18384c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
183952e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1840f0ec6fceSSatish Balay 
184194a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1842d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1843d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
184494a9d846SBarry Smith   }
184563b91edcSBarry Smith 
1846f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1847137fb511SHong Zhang   if (row_identity && col_identity) {
1848719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ(outA,inA,info);CHKERRQ(ierr);
1849137fb511SHong Zhang   } else {
1850719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1851137fb511SHong Zhang   }
18523a40ed3dSBarry Smith   PetscFunctionReturn(0);
1853a871dcd8SBarry Smith }
1854a871dcd8SBarry Smith 
1855d9eff348SSatish Balay #include "petscblaslapack.h"
18564a2ae208SSatish Balay #undef __FUNCT__
18574a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1858f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1859f0b747eeSBarry Smith {
1860f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1861f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1862efee365bSSatish Balay   PetscErrorCode ierr;
18630805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18643a40ed3dSBarry Smith 
18653a40ed3dSBarry Smith   PetscFunctionBegin;
1866f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1867efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18683a40ed3dSBarry Smith   PetscFunctionReturn(0);
1869f0b747eeSBarry Smith }
1870f0b747eeSBarry Smith 
18714a2ae208SSatish Balay #undef __FUNCT__
18724a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
187397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1874cddf8d76SBarry Smith {
1875dfbe8321SBarry Smith   PetscErrorCode ierr;
187697f1f81fSBarry Smith   PetscInt       i;
1877cddf8d76SBarry Smith 
18783a40ed3dSBarry Smith   PetscFunctionBegin;
1879cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1880b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1881cddf8d76SBarry Smith   }
1882cddf8d76SBarry Smith 
1883cddf8d76SBarry Smith   for (i=0; i<n; i++) {
18846a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1885cddf8d76SBarry Smith   }
18863a40ed3dSBarry Smith   PetscFunctionReturn(0);
1887cddf8d76SBarry Smith }
1888cddf8d76SBarry Smith 
18894a2ae208SSatish Balay #undef __FUNCT__
18904a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
189197f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
18924dcbc457SBarry Smith {
1893e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18946849ba73SBarry Smith   PetscErrorCode ierr;
18955d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
18965d0c19d7SBarry Smith   const PetscInt *idx;
189797f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1898f1af5d2fSBarry Smith   PetscBT        table;
1899bbd702dbSSatish Balay 
19003a40ed3dSBarry Smith   PetscFunctionBegin;
1901d0f46423SBarry Smith   m     = A->rmap->n;
1902e4d965acSSatish Balay   ai    = a->i;
1903bfeeae90SHong Zhang   aj    = a->j;
19048a047759SSatish Balay 
1905a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
190606763907SSatish Balay 
190797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19086831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
190906763907SSatish Balay 
1910e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1911b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1912e4d965acSSatish Balay     isz  = 0;
19136831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1914e4d965acSSatish Balay 
1915e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19164dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1917b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1918e4d965acSSatish Balay 
1919dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1920e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1921f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19224dcbc457SBarry Smith     }
192306763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
192406763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1925e4d965acSSatish Balay 
192604a348a9SBarry Smith     k = 0;
192704a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
192804a348a9SBarry Smith       n = isz;
192906763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1930e4d965acSSatish Balay         row   = nidx[k];
1931e4d965acSSatish Balay         start = ai[row];
1932e4d965acSSatish Balay         end   = ai[row+1];
193304a348a9SBarry Smith         for (l = start; l<end ; l++){
1934efb16452SHong Zhang           val = aj[l] ;
1935f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1936e4d965acSSatish Balay         }
1937e4d965acSSatish Balay       }
1938e4d965acSSatish Balay     }
1939029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1940e4d965acSSatish Balay   }
19416831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1942606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19433a40ed3dSBarry Smith   PetscFunctionReturn(0);
19444dcbc457SBarry Smith }
194517ab2063SBarry Smith 
19460513a670SBarry Smith /* -------------------------------------------------------------- */
19474a2ae208SSatish Balay #undef __FUNCT__
19484a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1949dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19500513a670SBarry Smith {
19510513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19526849ba73SBarry Smith   PetscErrorCode ierr;
19535d0c19d7SBarry Smith   PetscInt       i,nz,m = A->rmap->n,n = A->cmap->n;
19545d0c19d7SBarry Smith   const PetscInt *row,*col;
19555d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
195656cd22aeSBarry Smith   IS             icolp,irowp;
195797f1f81fSBarry Smith   PetscInt       *cwork;
195832ec9ce4SBarry Smith   PetscScalar    *vwork;
19590513a670SBarry Smith 
19603a40ed3dSBarry Smith   PetscFunctionBegin;
19614c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
196256cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19634c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
196456cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19650513a670SBarry Smith 
19660513a670SBarry Smith   /* determine lengths of permuted rows */
196797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19680513a670SBarry Smith   for (i=0; i<m; i++) {
19690513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19700513a670SBarry Smith   }
19717adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
1972f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
19737adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1974ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1975606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
19760513a670SBarry Smith 
197797f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
19780513a670SBarry Smith   for (i=0; i<m; i++) {
197932ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19800513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1981cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
198232ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19830513a670SBarry Smith   }
1984606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
19853c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
19860513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19870513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
198856cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
198956cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
199056cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
199156cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
19923a40ed3dSBarry Smith   PetscFunctionReturn(0);
19930513a670SBarry Smith }
19940513a670SBarry Smith 
19954a2ae208SSatish Balay #undef __FUNCT__
19964a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1997dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1998cb5b572fSBarry Smith {
1999dfbe8321SBarry Smith   PetscErrorCode ierr;
2000cb5b572fSBarry Smith 
2001cb5b572fSBarry Smith   PetscFunctionBegin;
200233f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
200333f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2004be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2005be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2006be6bf707SBarry Smith 
2007d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2008634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2009cb5b572fSBarry Smith     }
2010d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2011cb5b572fSBarry Smith   } else {
2012cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2013cb5b572fSBarry Smith   }
2014cb5b572fSBarry Smith   PetscFunctionReturn(0);
2015cb5b572fSBarry Smith }
2016cb5b572fSBarry Smith 
20174a2ae208SSatish Balay #undef __FUNCT__
20184a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2019dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2020273d9f13SBarry Smith {
2021dfbe8321SBarry Smith   PetscErrorCode ierr;
2022273d9f13SBarry Smith 
2023273d9f13SBarry Smith   PetscFunctionBegin;
2024ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2025273d9f13SBarry Smith   PetscFunctionReturn(0);
2026273d9f13SBarry Smith }
2027273d9f13SBarry Smith 
20284a2ae208SSatish Balay #undef __FUNCT__
20294a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2030a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20316c0721eeSBarry Smith {
20326c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20336c0721eeSBarry Smith   PetscFunctionBegin;
20346c0721eeSBarry Smith   *array = a->a;
20356c0721eeSBarry Smith   PetscFunctionReturn(0);
20366c0721eeSBarry Smith }
20376c0721eeSBarry Smith 
20384a2ae208SSatish Balay #undef __FUNCT__
20394a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2040dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20416c0721eeSBarry Smith {
20426c0721eeSBarry Smith   PetscFunctionBegin;
20436c0721eeSBarry Smith   PetscFunctionReturn(0);
20446c0721eeSBarry Smith }
2045273d9f13SBarry Smith 
2046ee4f033dSBarry Smith #undef __FUNCT__
2047ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2048dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2049ee4f033dSBarry Smith {
20506849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20516849ba73SBarry Smith   PetscErrorCode ierr;
205297f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2053efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
205487828ca2SBarry Smith   PetscScalar    *vscale_array;
2055ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2056ee4f033dSBarry Smith   Vec            w1,w2,w3;
2057ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
205890d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2059ee4f033dSBarry Smith 
2060ee4f033dSBarry Smith   PetscFunctionBegin;
2061ee4f033dSBarry Smith   if (!coloring->w1) {
2062ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
206352e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2064ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
206552e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2066ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
206752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2068ee4f033dSBarry Smith   }
2069ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2070ee4f033dSBarry Smith 
2071ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
207290d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2073ee4f033dSBarry Smith   if (flg) {
2074ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2075ee4f033dSBarry Smith   } else {
20760b9b6f31SBarry Smith     PetscTruth assembled;
20770b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
20780b9b6f31SBarry Smith     if (assembled) {
2079ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2080ee4f033dSBarry Smith     }
20810b9b6f31SBarry Smith   }
2082ee4f033dSBarry Smith 
2083ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2084ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2085ee4f033dSBarry Smith 
2086ee4f033dSBarry Smith   /*
2087ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2088ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2089ee4f033dSBarry Smith   */
2090ee4f033dSBarry Smith   if (coloring->F) {
2091ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2092ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2093ee4f033dSBarry Smith     if (m1 != m2) {
2094ee4f033dSBarry Smith       coloring->F = 0;
2095ee4f033dSBarry Smith     }
2096ee4f033dSBarry Smith   }
2097ee4f033dSBarry Smith 
2098ee4f033dSBarry Smith   if (coloring->F) {
2099ee4f033dSBarry Smith     w1          = coloring->F;
2100ee4f033dSBarry Smith     coloring->F = 0;
2101ee4f033dSBarry Smith   } else {
210266f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2103ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
210466f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2105ee4f033dSBarry Smith   }
2106ee4f033dSBarry Smith 
2107ee4f033dSBarry Smith   /*
2108ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2109ee4f033dSBarry Smith   */
21101ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21111ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2112ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2113ee4f033dSBarry Smith     /*
2114ee4f033dSBarry Smith        Loop over each column associated with color adding the
2115ee4f033dSBarry Smith        perturbation to the vector w3.
2116ee4f033dSBarry Smith     */
2117ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2118ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2119ee4f033dSBarry Smith       dx  = xx[col];
2120ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2121ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2122ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2123ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2124ee4f033dSBarry Smith #else
2125ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2126ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2127ee4f033dSBarry Smith #endif
2128ee4f033dSBarry Smith       dx                *= epsilon;
2129ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2130ee4f033dSBarry Smith     }
2131ee4f033dSBarry Smith   }
21321ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2133ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2134ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2135ee4f033dSBarry Smith 
2136ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2137ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2138ee4f033dSBarry Smith 
2139ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2140ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2141ee4f033dSBarry Smith 
21421ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2143ee4f033dSBarry Smith   /*
2144ee4f033dSBarry Smith       Loop over each color
2145ee4f033dSBarry Smith   */
2146ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
214749b058dcSBarry Smith     coloring->currentcolor = k;
2148ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21491ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2150ee4f033dSBarry Smith     /*
2151ee4f033dSBarry Smith        Loop over each column associated with color adding the
2152ee4f033dSBarry Smith        perturbation to the vector w3.
2153ee4f033dSBarry Smith     */
2154ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2155ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2156ee4f033dSBarry Smith       dx  = xx[col];
21575b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2158ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2159ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2160ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2161ee4f033dSBarry Smith #else
2162ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2163ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2164ee4f033dSBarry Smith #endif
2165ee4f033dSBarry Smith       dx            *= epsilon;
2166634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2167ee4f033dSBarry Smith       w3_array[col] += dx;
2168ee4f033dSBarry Smith     }
21691ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2170ee4f033dSBarry Smith 
2171ee4f033dSBarry Smith     /*
2172ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2173ee4f033dSBarry Smith     */
2174ee4f033dSBarry Smith 
217566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2176ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
217766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2178efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2179ee4f033dSBarry Smith 
2180ee4f033dSBarry Smith     /*
2181ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2182ee4f033dSBarry Smith     */
21831ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2184ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2185ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2186ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2187ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2188ee4f033dSBarry Smith       srow   = row + start;
2189ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2190ee4f033dSBarry Smith     }
21911ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2192ee4f033dSBarry Smith   }
219349b058dcSBarry Smith   coloring->currentcolor = k;
21941ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
21951ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2196ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2197ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2198ee4f033dSBarry Smith   PetscFunctionReturn(0);
2199ee4f033dSBarry Smith }
2200ee4f033dSBarry Smith 
2201ac90fabeSBarry Smith #include "petscblaslapack.h"
2202ac90fabeSBarry Smith #undef __FUNCT__
2203ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2204f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2205ac90fabeSBarry Smith {
2206dfbe8321SBarry Smith   PetscErrorCode ierr;
220797f1f81fSBarry Smith   PetscInt       i;
2208ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22090805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2210ac90fabeSBarry Smith 
2211ac90fabeSBarry Smith   PetscFunctionBegin;
2212ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2213f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2214f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2215c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2216a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2217a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2218a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2219a30b2313SHong Zhang     }
2220a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2221d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2222a30b2313SHong Zhang       y->XtoY = X;
2223407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2224c537a176SHong Zhang     }
2225f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22261e2582c4SBarry 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);
2227ac90fabeSBarry Smith   } else {
2228f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2229ac90fabeSBarry Smith   }
2230ac90fabeSBarry Smith   PetscFunctionReturn(0);
2231ac90fabeSBarry Smith }
2232ac90fabeSBarry Smith 
2233521d7252SBarry Smith #undef __FUNCT__
2234521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2235521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2236521d7252SBarry Smith {
2237521d7252SBarry Smith   PetscFunctionBegin;
2238521d7252SBarry Smith   PetscFunctionReturn(0);
2239521d7252SBarry Smith }
2240521d7252SBarry Smith 
2241354c94deSBarry Smith #undef __FUNCT__
2242354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2243354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2244354c94deSBarry Smith {
2245354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2246354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2247354c94deSBarry Smith   PetscInt    i,nz;
2248354c94deSBarry Smith   PetscScalar *a;
2249354c94deSBarry Smith 
2250354c94deSBarry Smith   PetscFunctionBegin;
2251354c94deSBarry Smith   nz = aij->nz;
2252354c94deSBarry Smith   a  = aij->a;
2253354c94deSBarry Smith   for (i=0; i<nz; i++) {
2254354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2255354c94deSBarry Smith   }
2256354c94deSBarry Smith #else
2257354c94deSBarry Smith   PetscFunctionBegin;
2258354c94deSBarry Smith #endif
2259354c94deSBarry Smith   PetscFunctionReturn(0);
2260354c94deSBarry Smith }
2261354c94deSBarry Smith 
2262e34fafa9SBarry Smith #undef __FUNCT__
2263985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2264985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2265e34fafa9SBarry Smith {
2266e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2267e34fafa9SBarry Smith   PetscErrorCode ierr;
2268d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2269e34fafa9SBarry Smith   PetscReal      atmp;
2270985db425SBarry Smith   PetscScalar    *x;
2271e34fafa9SBarry Smith   MatScalar      *aa;
2272e34fafa9SBarry Smith 
2273e34fafa9SBarry Smith   PetscFunctionBegin;
2274e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2275e34fafa9SBarry Smith   aa   = a->a;
2276e34fafa9SBarry Smith   ai   = a->i;
2277e34fafa9SBarry Smith   aj   = a->j;
2278e34fafa9SBarry Smith 
2279985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2280e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2281e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2282d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2283e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2284e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
22859189402eSHong Zhang     x[i] = 0.0;
2286e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2287985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2288985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2289985db425SBarry Smith       aa++; aj++;
2290985db425SBarry Smith     }
2291985db425SBarry Smith   }
2292985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2293985db425SBarry Smith   PetscFunctionReturn(0);
2294985db425SBarry Smith }
2295985db425SBarry Smith 
2296985db425SBarry Smith #undef __FUNCT__
2297985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2298985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2299985db425SBarry Smith {
2300985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2301985db425SBarry Smith   PetscErrorCode ierr;
2302d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2303985db425SBarry Smith   PetscScalar    *x;
2304985db425SBarry Smith   MatScalar      *aa;
2305985db425SBarry Smith 
2306985db425SBarry Smith   PetscFunctionBegin;
2307985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2308985db425SBarry Smith   aa   = a->a;
2309985db425SBarry Smith   ai   = a->i;
2310985db425SBarry Smith   aj   = a->j;
2311985db425SBarry Smith 
2312985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2313985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2314985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2315d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2316985db425SBarry Smith   for (i=0; i<m; i++) {
2317985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2318d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2319985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2320985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2321985db425SBarry Smith       x[i] = 0.0;
2322985db425SBarry Smith       if (idx) {
2323985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2324985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2325985db425SBarry Smith           if (aj[j] > j) {
2326985db425SBarry Smith             idx[i] = j;
2327985db425SBarry Smith             break;
2328985db425SBarry Smith           }
2329985db425SBarry Smith         }
2330985db425SBarry Smith       }
2331985db425SBarry Smith     }
2332985db425SBarry Smith     for (j=0; j<ncols; j++){
2333985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2334985db425SBarry Smith       aa++; aj++;
2335985db425SBarry Smith     }
2336985db425SBarry Smith   }
2337985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2338985db425SBarry Smith   PetscFunctionReturn(0);
2339985db425SBarry Smith }
2340985db425SBarry Smith 
2341985db425SBarry Smith #undef __FUNCT__
2342c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2343c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2344c87e5d42SMatthew Knepley {
2345c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2346c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2347c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2348c87e5d42SMatthew Knepley   PetscReal      atmp;
2349c87e5d42SMatthew Knepley   PetscScalar    *x;
2350c87e5d42SMatthew Knepley   MatScalar      *aa;
2351c87e5d42SMatthew Knepley 
2352c87e5d42SMatthew Knepley   PetscFunctionBegin;
2353c87e5d42SMatthew Knepley   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2354c87e5d42SMatthew Knepley   aa   = a->a;
2355c87e5d42SMatthew Knepley   ai   = a->i;
2356c87e5d42SMatthew Knepley   aj   = a->j;
2357c87e5d42SMatthew Knepley 
2358c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2359c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2360c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2361c87e5d42SMatthew Knepley   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2362c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2363c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2364289a08f5SMatthew Knepley     if (ncols) {
2365289a08f5SMatthew Knepley       /* Get first nonzero */
2366289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2367289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2368289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2369289a08f5SMatthew Knepley       }
2370289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2371289a08f5SMatthew Knepley     } else {
2372289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2373289a08f5SMatthew Knepley     }
2374c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2375c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2376289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2377c87e5d42SMatthew Knepley       aa++; aj++;
2378c87e5d42SMatthew Knepley     }
2379c87e5d42SMatthew Knepley   }
2380c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2381c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2382c87e5d42SMatthew Knepley }
2383c87e5d42SMatthew Knepley 
2384c87e5d42SMatthew Knepley #undef __FUNCT__
2385985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2386985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2387985db425SBarry Smith {
2388985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2389985db425SBarry Smith   PetscErrorCode ierr;
2390d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2391985db425SBarry Smith   PetscScalar    *x;
2392985db425SBarry Smith   MatScalar      *aa;
2393985db425SBarry Smith 
2394985db425SBarry Smith   PetscFunctionBegin;
2395985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2396985db425SBarry Smith   aa   = a->a;
2397985db425SBarry Smith   ai   = a->i;
2398985db425SBarry Smith   aj   = a->j;
2399985db425SBarry Smith 
2400985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2401985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2402985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2403d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2404985db425SBarry Smith   for (i=0; i<m; i++) {
2405985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2406d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2407985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2408985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2409985db425SBarry Smith       x[i] = 0.0;
2410985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2411985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2412985db425SBarry Smith         for (j=0;j<ncols;j++) {
2413985db425SBarry Smith           if (aj[j] > j) {
2414985db425SBarry Smith             idx[i] = j;
2415985db425SBarry Smith             break;
2416985db425SBarry Smith           }
2417985db425SBarry Smith         }
2418985db425SBarry Smith       }
2419985db425SBarry Smith     }
2420985db425SBarry Smith     for (j=0; j<ncols; j++){
2421985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2422985db425SBarry Smith       aa++; aj++;
2423e34fafa9SBarry Smith     }
2424e34fafa9SBarry Smith   }
2425e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2426e34fafa9SBarry Smith   PetscFunctionReturn(0);
2427e34fafa9SBarry Smith }
24283acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2429682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24300a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2431cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2432cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2433cb5b572fSBarry Smith        MatMult_SeqAIJ,
243497304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
24357c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
24367c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2437db4efbfdSBarry Smith        0,
2438db4efbfdSBarry Smith        0,
2439db4efbfdSBarry Smith        0,
2440db4efbfdSBarry Smith /*10*/ 0,
2441cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2442cb5b572fSBarry Smith        0,
244341f059aeSBarry Smith        MatSOR_SeqAIJ,
244417ab2063SBarry Smith        MatTranspose_SeqAIJ,
244597304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2446cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2447cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2448cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2449cb5b572fSBarry Smith        MatNorm_SeqAIJ,
245097304618SKris Buschelman /*20*/ 0,
2451cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2452cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2453cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2454d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2455db4efbfdSBarry Smith        0,
2456db4efbfdSBarry Smith        0,
2457db4efbfdSBarry Smith        0,
2458db4efbfdSBarry Smith        0,
2459d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2460db4efbfdSBarry Smith        0,
2461db4efbfdSBarry Smith        0,
24626c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24636c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2464d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2465cb5b572fSBarry Smith        0,
2466cb5b572fSBarry Smith        0,
2467cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2468cb5b572fSBarry Smith        0,
2469d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2470cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2471cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2472cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2473cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2474d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2475cb5b572fSBarry Smith        MatScale_SeqAIJ,
2476cb5b572fSBarry Smith        0,
247779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
24786945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2479d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
24803b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
24813b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
24823b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2483a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2484d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2485b9617806SBarry Smith        0,
24860513a670SBarry Smith        0,
2487cda55fadSBarry Smith        MatPermute_SeqAIJ,
2488cda55fadSBarry Smith        0,
2489d519adbfSMatthew Knepley /*59*/ 0,
2490b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2491b9b97703SBarry Smith        MatView_SeqAIJ,
2492357abbc8SBarry Smith        0,
2493ee4f033dSBarry Smith        0,
2494d519adbfSMatthew Knepley /*64*/ 0,
2495ee4f033dSBarry Smith        0,
2496ee4f033dSBarry Smith        0,
2497ee4f033dSBarry Smith        0,
2498ee4f033dSBarry Smith        0,
2499d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2500c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2501ee4f033dSBarry Smith        0,
2502ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2503dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2504ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2505dcf5cc72SBarry Smith #else
2506dcf5cc72SBarry Smith        0,
2507dcf5cc72SBarry Smith #endif
2508d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
25093acb8795SBarry Smith        MatFDColoringApply_AIJ,
251097304618SKris Buschelman        0,
251197304618SKris Buschelman        0,
251297304618SKris Buschelman        0,
2513d519adbfSMatthew Knepley /*79*/ 0,
251497304618SKris Buschelman        0,
251597304618SKris Buschelman        0,
251697304618SKris Buschelman        0,
2517bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2518d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
25191cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25206284ec50SHong Zhang        0,
25216284ec50SHong Zhang        0,
2522bc011b1eSHong Zhang        0,
2523d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
252426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
252526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2526d439da42SKris Buschelman        MatPtAP_Basic,
25277ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2528d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2529bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2530bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2531bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25327ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2533d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2534609c6c4dSKris Buschelman        0,
2535609c6c4dSKris Buschelman        0,
253687d4246cSBarry Smith        MatConjugate_SeqAIJ,
253787d4246cSBarry Smith        0,
2538d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
253999cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2540f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2541f5edf698SHong Zhang        0,
25422bebee5dSHong Zhang        0,
2543d519adbfSMatthew Knepley /*109*/0,
2544985db425SBarry Smith        0,
25452af78befSBarry Smith        MatGetRowMin_SeqAIJ,
25462af78befSBarry Smith        0,
2547599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2548d519adbfSMatthew Knepley /*114*/0,
2549599ef60dSHong Zhang        0,
25503c2a7987SHong Zhang        0,
25513c2a7987SHong Zhang        MatILUDTFactorSymbolic_SeqAIJ,
25523c2a7987SHong Zhang        MatILUDTFactorNumeric_SeqAIJ
25539e29f15eSvictorle };
255417ab2063SBarry Smith 
2555fb2e594dSBarry Smith EXTERN_C_BEGIN
25564a2ae208SSatish Balay #undef __FUNCT__
25574a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2558be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2559bef8e0ddSBarry Smith {
2560bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
256197f1f81fSBarry Smith   PetscInt   i,nz,n;
2562bef8e0ddSBarry Smith 
2563bef8e0ddSBarry Smith   PetscFunctionBegin;
2564bef8e0ddSBarry Smith 
2565bef8e0ddSBarry Smith   nz = aij->maxnz;
2566d0f46423SBarry Smith   n  = mat->rmap->n;
2567bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2568bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2569bef8e0ddSBarry Smith   }
2570bef8e0ddSBarry Smith   aij->nz = nz;
2571bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2572bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2573bef8e0ddSBarry Smith   }
2574bef8e0ddSBarry Smith 
2575bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2576bef8e0ddSBarry Smith }
2577fb2e594dSBarry Smith EXTERN_C_END
2578bef8e0ddSBarry Smith 
25794a2ae208SSatish Balay #undef __FUNCT__
25804a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2581bef8e0ddSBarry Smith /*@
2582bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2583bef8e0ddSBarry Smith        in the matrix.
2584bef8e0ddSBarry Smith 
2585bef8e0ddSBarry Smith   Input Parameters:
2586bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2587bef8e0ddSBarry Smith -  indices - the column indices
2588bef8e0ddSBarry Smith 
258915091d37SBarry Smith   Level: advanced
259015091d37SBarry Smith 
2591bef8e0ddSBarry Smith   Notes:
2592bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2593bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2594bef8e0ddSBarry Smith   of the MatSetValues() operation.
2595bef8e0ddSBarry Smith 
2596bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2597d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2598bef8e0ddSBarry Smith 
2599bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2600bef8e0ddSBarry Smith 
2601b9617806SBarry Smith     The indices should start with zero, not one.
2602b9617806SBarry Smith 
2603bef8e0ddSBarry Smith @*/
2604be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2605bef8e0ddSBarry Smith {
260697f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2607bef8e0ddSBarry Smith 
2608bef8e0ddSBarry Smith   PetscFunctionBegin;
26094482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
26104482741eSBarry Smith   PetscValidPointer(indices,2);
2611c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2612bef8e0ddSBarry Smith   if (f) {
2613bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2614bef8e0ddSBarry Smith   } else {
2615634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2616bef8e0ddSBarry Smith   }
2617bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2618bef8e0ddSBarry Smith }
2619bef8e0ddSBarry Smith 
2620be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2621be6bf707SBarry Smith 
2622fb2e594dSBarry Smith EXTERN_C_BEGIN
26234a2ae208SSatish Balay #undef __FUNCT__
26244a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2625be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2626be6bf707SBarry Smith {
2627be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26286849ba73SBarry Smith   PetscErrorCode ierr;
2629d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2630be6bf707SBarry Smith 
2631be6bf707SBarry Smith   PetscFunctionBegin;
2632be6bf707SBarry Smith   if (aij->nonew != 1) {
2633512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2634be6bf707SBarry Smith   }
2635be6bf707SBarry Smith 
2636be6bf707SBarry Smith   /* allocate space for values if not already there */
2637be6bf707SBarry Smith   if (!aij->saved_values) {
263887828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
26399518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2640be6bf707SBarry Smith   }
2641be6bf707SBarry Smith 
2642be6bf707SBarry Smith   /* copy values over */
264387828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2644be6bf707SBarry Smith   PetscFunctionReturn(0);
2645be6bf707SBarry Smith }
2646fb2e594dSBarry Smith EXTERN_C_END
2647be6bf707SBarry Smith 
26484a2ae208SSatish Balay #undef __FUNCT__
2649b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2650be6bf707SBarry Smith /*@
2651be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2652be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2653be6bf707SBarry Smith        nonlinear portion.
2654be6bf707SBarry Smith 
2655be6bf707SBarry Smith    Collect on Mat
2656be6bf707SBarry Smith 
2657be6bf707SBarry Smith   Input Parameters:
26580e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2659be6bf707SBarry Smith 
266015091d37SBarry Smith   Level: advanced
266115091d37SBarry Smith 
2662be6bf707SBarry Smith   Common Usage, with SNESSolve():
2663be6bf707SBarry Smith $    Create Jacobian matrix
2664be6bf707SBarry Smith $    Set linear terms into matrix
2665be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2666be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2667be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2668512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2669be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2670be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2671be6bf707SBarry Smith $    In your Jacobian routine
2672be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2673be6bf707SBarry Smith $      Set nonlinear terms in matrix
2674be6bf707SBarry Smith 
2675be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2676be6bf707SBarry Smith $    // build linear portion of Jacobian
2677512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2678be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2679be6bf707SBarry Smith $    loop over nonlinear iterations
2680be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2681be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2682be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2683be6bf707SBarry Smith $       Solve linear system with Jacobian
2684be6bf707SBarry Smith $    endloop
2685be6bf707SBarry Smith 
2686be6bf707SBarry Smith   Notes:
2687be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2688512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2689be6bf707SBarry Smith     calling this routine.
2690be6bf707SBarry Smith 
26910c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
26920c468ba9SBarry Smith     and does not allocated additional space.
26930c468ba9SBarry Smith 
2694be6bf707SBarry Smith .seealso: MatRetrieveValues()
2695be6bf707SBarry Smith 
2696be6bf707SBarry Smith @*/
2697be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2698be6bf707SBarry Smith {
2699dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2700be6bf707SBarry Smith 
2701be6bf707SBarry Smith   PetscFunctionBegin;
27024482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
270329bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
270429bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2705be6bf707SBarry Smith 
2706c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2707be6bf707SBarry Smith   if (f) {
2708be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2709be6bf707SBarry Smith   } else {
2710634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2711be6bf707SBarry Smith   }
2712be6bf707SBarry Smith   PetscFunctionReturn(0);
2713be6bf707SBarry Smith }
2714be6bf707SBarry Smith 
2715fb2e594dSBarry Smith EXTERN_C_BEGIN
27164a2ae208SSatish Balay #undef __FUNCT__
27174a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2718be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2719be6bf707SBarry Smith {
2720be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27216849ba73SBarry Smith   PetscErrorCode ierr;
2722d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2723be6bf707SBarry Smith 
2724be6bf707SBarry Smith   PetscFunctionBegin;
2725be6bf707SBarry Smith   if (aij->nonew != 1) {
2726512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2727be6bf707SBarry Smith   }
2728be6bf707SBarry Smith   if (!aij->saved_values) {
2729634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2730be6bf707SBarry Smith   }
2731be6bf707SBarry Smith   /* copy values over */
273287828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2733be6bf707SBarry Smith   PetscFunctionReturn(0);
2734be6bf707SBarry Smith }
2735fb2e594dSBarry Smith EXTERN_C_END
2736be6bf707SBarry Smith 
27374a2ae208SSatish Balay #undef __FUNCT__
27384a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2739be6bf707SBarry Smith /*@
2740be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2741be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2742be6bf707SBarry Smith        nonlinear portion.
2743be6bf707SBarry Smith 
2744be6bf707SBarry Smith    Collect on Mat
2745be6bf707SBarry Smith 
2746be6bf707SBarry Smith   Input Parameters:
2747be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2748be6bf707SBarry Smith 
274915091d37SBarry Smith   Level: advanced
275015091d37SBarry Smith 
2751be6bf707SBarry Smith .seealso: MatStoreValues()
2752be6bf707SBarry Smith 
2753be6bf707SBarry Smith @*/
2754be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2755be6bf707SBarry Smith {
2756dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2757be6bf707SBarry Smith 
2758be6bf707SBarry Smith   PetscFunctionBegin;
27594482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
276029bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
276129bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2762be6bf707SBarry Smith 
2763c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2764be6bf707SBarry Smith   if (f) {
2765be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2766be6bf707SBarry Smith   } else {
2767634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2768be6bf707SBarry Smith   }
2769be6bf707SBarry Smith   PetscFunctionReturn(0);
2770be6bf707SBarry Smith }
2771be6bf707SBarry Smith 
2772f83d6046SBarry Smith 
2773be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
27744a2ae208SSatish Balay #undef __FUNCT__
27754a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
277617ab2063SBarry Smith /*@C
2777682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
27780d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
27796e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
278051c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
27812bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
278217ab2063SBarry Smith 
2783db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2784db81eaa0SLois Curfman McInnes 
278517ab2063SBarry Smith    Input Parameters:
2786db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
278717ab2063SBarry Smith .  m - number of rows
278817ab2063SBarry Smith .  n - number of columns
278917ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
279051c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
27912bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
279217ab2063SBarry Smith 
279317ab2063SBarry Smith    Output Parameter:
2794416022c9SBarry Smith .  A - the matrix
279517ab2063SBarry Smith 
2796175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2797ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2798175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2799175b88e8SBarry Smith 
2800b259b22eSLois Curfman McInnes    Notes:
280149a6f317SBarry Smith    If nnz is given then nz is ignored
280249a6f317SBarry Smith 
280317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
280417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28050002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
280644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
280717ab2063SBarry Smith 
280817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2809a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28103d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28116da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
281217ab2063SBarry Smith 
2813682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
28144fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2815682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
28166c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
28176c7ebb05SLois Curfman McInnes 
28186c7ebb05SLois Curfman McInnes    Options Database Keys:
2819698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2820698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2821db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2822db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2823db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
282417ab2063SBarry Smith 
2825027ccd11SLois Curfman McInnes    Level: intermediate
2826027ccd11SLois Curfman McInnes 
282736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
282836db0b34SBarry Smith 
282917ab2063SBarry Smith @*/
2830be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
283117ab2063SBarry Smith {
2832dfbe8321SBarry Smith   PetscErrorCode ierr;
28336945ee14SBarry Smith 
28343a40ed3dSBarry Smith   PetscFunctionBegin;
2835f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2836117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2837c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2838ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2839273d9f13SBarry Smith   PetscFunctionReturn(0);
2840273d9f13SBarry Smith }
2841273d9f13SBarry Smith 
28424a2ae208SSatish Balay #undef __FUNCT__
28434a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2844273d9f13SBarry Smith /*@C
2845273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2846273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2847273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2848273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2849273d9f13SBarry Smith 
2850273d9f13SBarry Smith    Collective on MPI_Comm
2851273d9f13SBarry Smith 
2852273d9f13SBarry Smith    Input Parameters:
2853117016b1SBarry Smith +  B - The matrix-free
2854273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2855273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2856273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2857273d9f13SBarry Smith 
2858273d9f13SBarry Smith    Notes:
285949a6f317SBarry Smith      If nnz is given then nz is ignored
286049a6f317SBarry Smith 
2861273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2862273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2863273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2864273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2865273d9f13SBarry Smith 
2866273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2867273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2868273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2869273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2870273d9f13SBarry Smith 
2871aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2872aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2873aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2874aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2875aa95bbe8SBarry Smith 
2876a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2877a96a251dSBarry Smith    entries or columns indices
2878a96a251dSBarry Smith 
2879273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2880273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2881273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2882273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2883273d9f13SBarry Smith 
2884273d9f13SBarry Smith    Options Database Keys:
2885698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2886698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2887273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2888273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2889273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2890273d9f13SBarry Smith 
2891273d9f13SBarry Smith    Level: intermediate
2892273d9f13SBarry Smith 
2893aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2894273d9f13SBarry Smith 
2895273d9f13SBarry Smith @*/
2896be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2897273d9f13SBarry Smith {
289897f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2899a23d5eceSKris Buschelman 
2900a23d5eceSKris Buschelman   PetscFunctionBegin;
2901a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2902a23d5eceSKris Buschelman   if (f) {
2903a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2904a23d5eceSKris Buschelman   }
2905a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2906a23d5eceSKris Buschelman }
2907a23d5eceSKris Buschelman 
2908a23d5eceSKris Buschelman EXTERN_C_BEGIN
2909a23d5eceSKris Buschelman #undef __FUNCT__
2910a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2911be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2912a23d5eceSKris Buschelman {
2913273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2914a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
29156849ba73SBarry Smith   PetscErrorCode ierr;
291697f1f81fSBarry Smith   PetscInt       i;
2917273d9f13SBarry Smith 
2918273d9f13SBarry Smith   PetscFunctionBegin;
2919d5d45c9bSBarry Smith 
2920a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2921c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2922c461c341SBarry Smith     nz             = 0;
2923c461c341SBarry Smith   }
2924c461c341SBarry Smith 
292526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
292626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
292726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
292826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2929899cda47SBarry Smith 
2930435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2931435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2932b73539f3SBarry Smith   if (nnz) {
2933d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
293429bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2935d0f46423SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
2936b73539f3SBarry Smith     }
2937b73539f3SBarry Smith   }
2938b73539f3SBarry Smith 
2939273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2940273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2941273d9f13SBarry Smith 
2942ab93d7beSBarry Smith   if (!skipallocation) {
29432ee49352SLisandro Dalcin     if (!b->imax) {
2944d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
2945d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
29462ee49352SLisandro Dalcin     }
2947273d9f13SBarry Smith     if (!nnz) {
2948435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2949273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2950d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
2951d0f46423SBarry Smith       nz = nz*B->rmap->n;
2952273d9f13SBarry Smith     } else {
2953273d9f13SBarry Smith       nz = 0;
2954d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2955273d9f13SBarry Smith     }
2956ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2957d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
2958ab93d7beSBarry Smith 
2959273d9f13SBarry Smith     /* allocate the matrix space */
29602ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2961d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
2962d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2963bfeeae90SHong Zhang     b->i[0] = 0;
2964d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
29655da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29665da197adSKris Buschelman     }
2967273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2968e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2969e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2970c461c341SBarry Smith   } else {
2971e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2972e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2973c461c341SBarry Smith   }
2974273d9f13SBarry Smith 
2975273d9f13SBarry Smith   b->nz                = 0;
2976273d9f13SBarry Smith   b->maxnz             = nz;
2977273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2978273d9f13SBarry Smith   PetscFunctionReturn(0);
2979273d9f13SBarry Smith }
2980a23d5eceSKris Buschelman EXTERN_C_END
2981273d9f13SBarry Smith 
2982a1661176SMatthew Knepley #undef  __FUNCT__
2983a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
298458d36128SBarry Smith /*@
2985a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
2986a1661176SMatthew Knepley 
2987a1661176SMatthew Knepley    Input Parameters:
2988a1661176SMatthew Knepley +  B - the matrix
2989a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
2990a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
2991a1661176SMatthew Knepley -  v - optional values in the matrix
2992a1661176SMatthew Knepley 
2993a1661176SMatthew Knepley    Level: developer
2994a1661176SMatthew Knepley 
299558d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
299658d36128SBarry Smith 
2997a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
2998a1661176SMatthew Knepley 
2999a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3000a1661176SMatthew Knepley @*/
3001a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3002a1661176SMatthew Knepley {
3003a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3004a1661176SMatthew Knepley   PetscErrorCode ierr;
3005a1661176SMatthew Knepley 
3006a1661176SMatthew Knepley   PetscFunctionBegin;
3007a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
3008a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3009a1661176SMatthew Knepley   if (f) {
3010a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3011a1661176SMatthew Knepley   }
3012a1661176SMatthew Knepley   PetscFunctionReturn(0);
3013a1661176SMatthew Knepley }
3014a1661176SMatthew Knepley 
3015a1661176SMatthew Knepley EXTERN_C_BEGIN
3016a1661176SMatthew Knepley #undef  __FUNCT__
3017a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3018b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3019a1661176SMatthew Knepley {
3020a1661176SMatthew Knepley   PetscInt       i;
3021a1661176SMatthew Knepley   PetscInt       m,n;
3022a1661176SMatthew Knepley   PetscInt       nz;
3023a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3024a1661176SMatthew Knepley   PetscScalar    *values;
3025a1661176SMatthew Knepley   PetscErrorCode ierr;
3026a1661176SMatthew Knepley 
3027a1661176SMatthew Knepley   PetscFunctionBegin;
3028a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3029a1661176SMatthew Knepley 
3030b7940d39SSatish Balay   if (Ii[0]) {
3031b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3032a1661176SMatthew Knepley   }
3033a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3034a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3035b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3036a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
3037a1661176SMatthew Knepley     if (nz < 0) {
3038a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3039a1661176SMatthew Knepley     }
3040a1661176SMatthew Knepley     nnz[i] = nz;
3041a1661176SMatthew Knepley   }
3042a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3043a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3044a1661176SMatthew Knepley 
3045a1661176SMatthew Knepley   if (v) {
3046a1661176SMatthew Knepley     values = (PetscScalar*) v;
3047a1661176SMatthew Knepley   } else {
3048a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3049a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3050a1661176SMatthew Knepley   }
3051a1661176SMatthew Knepley 
3052a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3053b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3054b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3055a1661176SMatthew Knepley   }
3056a1661176SMatthew Knepley 
3057a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3058a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3059a1661176SMatthew Knepley 
3060a1661176SMatthew Knepley   if (!v) {
3061a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3062a1661176SMatthew Knepley   }
3063a1661176SMatthew Knepley   PetscFunctionReturn(0);
3064a1661176SMatthew Knepley }
3065a1661176SMatthew Knepley EXTERN_C_END
3066a1661176SMatthew Knepley 
30677c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3068be7314b0SBarry Smith #include "private/petscaxpy.h"
3069170fe5c8SBarry Smith 
3070170fe5c8SBarry Smith #undef __FUNCT__
3071170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3072170fe5c8SBarry Smith /*
3073170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3074170fe5c8SBarry Smith 
3075170fe5c8SBarry Smith                n                       p                          p
3076170fe5c8SBarry Smith         (              )       (              )         (                  )
3077170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3078170fe5c8SBarry Smith         (              )       (              )         (                  )
3079170fe5c8SBarry Smith 
3080170fe5c8SBarry Smith */
3081170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3082170fe5c8SBarry Smith {
3083170fe5c8SBarry Smith   PetscErrorCode     ierr;
3084170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3085170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3086170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
30871de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3088170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3089170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3090170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3091170fe5c8SBarry Smith 
3092170fe5c8SBarry Smith   PetscFunctionBegin;
3093d0f46423SBarry Smith   m = A->rmap->n;
3094d0f46423SBarry Smith   n = A->cmap->n;
3095d0f46423SBarry Smith   p = B->cmap->n;
3096170fe5c8SBarry Smith   a = sub_a->v;
3097170fe5c8SBarry Smith   b = sub_b->a;
3098170fe5c8SBarry Smith   c = sub_c->v;
3099170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3100170fe5c8SBarry Smith 
3101170fe5c8SBarry Smith   ii  = sub_b->i;
3102170fe5c8SBarry Smith   idx = sub_b->j;
3103170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3104170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3105170fe5c8SBarry Smith     while (q-->0) {
3106170fe5c8SBarry Smith       c_q = c + m*(*idx);
3107170fe5c8SBarry Smith       a_q = a + m*i;
3108be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3109170fe5c8SBarry Smith       idx++;
3110170fe5c8SBarry Smith       b++;
3111170fe5c8SBarry Smith     }
3112170fe5c8SBarry Smith   }
3113170fe5c8SBarry Smith   PetscFunctionReturn(0);
3114170fe5c8SBarry Smith }
3115170fe5c8SBarry Smith 
3116170fe5c8SBarry Smith #undef __FUNCT__
3117170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3118170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3119170fe5c8SBarry Smith {
3120170fe5c8SBarry Smith   PetscErrorCode ierr;
3121d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3122170fe5c8SBarry Smith   Mat            Cmat;
3123170fe5c8SBarry Smith 
3124170fe5c8SBarry Smith   PetscFunctionBegin;
3125d0f46423SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
312639804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3127170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3128170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3129170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3130170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3131170fe5c8SBarry Smith   *C = Cmat;
3132170fe5c8SBarry Smith   PetscFunctionReturn(0);
3133170fe5c8SBarry Smith }
3134170fe5c8SBarry Smith 
3135170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3136170fe5c8SBarry Smith #undef __FUNCT__
3137170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3138170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3139170fe5c8SBarry Smith {
3140170fe5c8SBarry Smith   PetscErrorCode ierr;
3141170fe5c8SBarry Smith 
3142170fe5c8SBarry Smith   PetscFunctionBegin;
3143170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3144170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3145170fe5c8SBarry Smith   }
3146170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3147170fe5c8SBarry Smith   PetscFunctionReturn(0);
3148170fe5c8SBarry Smith }
3149170fe5c8SBarry Smith 
3150170fe5c8SBarry Smith 
31510bad9183SKris Buschelman /*MC
3152fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31530bad9183SKris Buschelman    based on compressed sparse row format.
31540bad9183SKris Buschelman 
31550bad9183SKris Buschelman    Options Database Keys:
31560bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31570bad9183SKris Buschelman 
31580bad9183SKris Buschelman   Level: beginner
31590bad9183SKris Buschelman 
3160f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31610bad9183SKris Buschelman M*/
31620bad9183SKris Buschelman 
3163a6175056SHong Zhang EXTERN_C_BEGIN
3164b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3165b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3166b5e56a35SBarry Smith #endif
3167611a798aSSatish Balay #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
3168af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3169af1023dbSSatish Balay #endif
317017667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3171b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3172db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3173611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
31745c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3175611f576cSBarry Smith #endif
3176611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
31775c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3178611f576cSBarry Smith #endif
3179f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3180f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3181f3c0ef26SHong Zhang #endif
3182611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
31835c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3184611f576cSBarry Smith #endif
3185eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3186eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3187eb3b5408SSatish Balay #endif
3188719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3189719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3190719d5645SBarry Smith #endif
319117667f90SBarry Smith EXTERN_C_END
319217667f90SBarry Smith 
3193b24902e0SBarry Smith 
319417667f90SBarry Smith EXTERN_C_BEGIN
31954a2ae208SSatish Balay #undef __FUNCT__
31964a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3197be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3198273d9f13SBarry Smith {
3199273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3200dfbe8321SBarry Smith   PetscErrorCode ierr;
320138baddfdSBarry Smith   PetscMPIInt    size;
3202273d9f13SBarry Smith 
3203273d9f13SBarry Smith   PetscFunctionBegin;
32047adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3205273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3206273d9f13SBarry Smith 
320738f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3208b0a32e0cSBarry Smith   B->data             = (void*)b;
3209549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
321090f02eecSBarry Smith   B->mapping          = 0;
3211416022c9SBarry Smith   b->row              = 0;
3212416022c9SBarry Smith   b->col              = 0;
321382bf6240SBarry Smith   b->icol             = 0;
3214b810aeb4SBarry Smith   b->reallocs         = 0;
321536db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3216f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3217416022c9SBarry Smith   b->nonew             = 0;
3218416022c9SBarry Smith   b->diag              = 0;
3219416022c9SBarry Smith   b->solve_work        = 0;
32202a1b7f2aSHong Zhang   B->spptr             = 0;
3221be6bf707SBarry Smith   b->saved_values      = 0;
3222d7f994e1SBarry Smith   b->idiag             = 0;
322371f1c65dSBarry Smith   b->mdiag             = 0;
322471f1c65dSBarry Smith   b->ssor_work         = 0;
322571f1c65dSBarry Smith   b->omega             = 1.0;
322671f1c65dSBarry Smith   b->fshift            = 0.0;
322771f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3228a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3229a30b2313SHong Zhang   b->xtoy              = 0;
3230a30b2313SHong Zhang   b->XtoY              = 0;
323173e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3232d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3233d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3234d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3235d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
323688e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
323717ab2063SBarry Smith 
323835d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3239b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3240ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
3241b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3242b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3243b5e56a35SBarry Smith #endif
3244611a798aSSatish Balay #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
3245ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C",
3246719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3247719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3248719d5645SBarry Smith #endif
3249611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3250ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C",
32515c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
32525c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3253611f576cSBarry Smith #endif
3254f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3255ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
3256f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3257f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3258f3c0ef26SHong Zhang #endif
3259611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3260ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
32615c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
32625c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3263611f576cSBarry Smith #endif
3264611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3265ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
32665c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
32675c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3268611f576cSBarry Smith #endif
3269eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3270ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C",
3271eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3272eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3273eb3b5408SSatish Balay #endif
3274719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3275ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C",
3276719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3277719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3278719d5645SBarry Smith #endif
3279ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3280b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3281b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3282ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3283db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3284db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3285f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3286bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3287bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3288f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3289be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3290bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3291f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3292be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3293bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3294a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3295a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3296a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
329785fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
329885fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
329985fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3300ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3301ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3302ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
330317667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
330417667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
330517667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33065fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33075fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33085fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33091cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33101cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33111cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3312a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3313a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3314a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3315a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3316a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3317a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
331805b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
331905b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
332005b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3321170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3322170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3323170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3324170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3325170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3326170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3327170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3328170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3329170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
33304108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
333117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
33323a40ed3dSBarry Smith   PetscFunctionReturn(0);
333317ab2063SBarry Smith }
3334273d9f13SBarry Smith EXTERN_C_END
333517ab2063SBarry Smith 
33364a2ae208SSatish Balay #undef __FUNCT__
3337b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3338b24902e0SBarry Smith /*
3339b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3340b24902e0SBarry Smith */
3341f77e22a1SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
334217ab2063SBarry Smith {
3343416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
33446849ba73SBarry Smith   PetscErrorCode ierr;
3345d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
334617ab2063SBarry Smith 
33473a40ed3dSBarry Smith   PetscFunctionBegin;
3348273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3349273d9f13SBarry Smith 
3350416022c9SBarry Smith   C->factor           = A->factor;
33516ad4291fSHong Zhang 
3352416022c9SBarry Smith   c->row            = 0;
3353416022c9SBarry Smith   c->col            = 0;
335482bf6240SBarry Smith   c->icol           = 0;
33556ad4291fSHong Zhang   c->reallocs       = 0;
335617ab2063SBarry Smith 
33576ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
335817ab2063SBarry Smith 
335926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
336026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
336126283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
336226283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3363eec197d1SBarry Smith 
336433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
33659518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
336617ab2063SBarry Smith   for (i=0; i<m; i++) {
3367416022c9SBarry Smith     c->imax[i] = a->imax[i];
3368416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
336917ab2063SBarry Smith   }
337017ab2063SBarry Smith 
337117ab2063SBarry Smith   /* allocate the matrix space */
3372f77e22a1SHong Zhang   if (mallocmatspace){
3373a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
33749518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3375f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
337697f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
337717ab2063SBarry Smith     if (m > 0) {
337897f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3379be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3380bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3381be6bf707SBarry Smith       } else {
3382bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
338317ab2063SBarry Smith       }
338408480c60SBarry Smith     }
3385f77e22a1SHong Zhang   }
338617ab2063SBarry Smith 
33876ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3388416022c9SBarry Smith   c->roworiented       = a->roworiented;
3389416022c9SBarry Smith   c->nonew             = a->nonew;
3390416022c9SBarry Smith   if (a->diag) {
339197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
339252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
339317ab2063SBarry Smith     for (i=0; i<m; i++) {
3394416022c9SBarry Smith       c->diag[i] = a->diag[i];
339517ab2063SBarry Smith     }
33963a40ed3dSBarry Smith   } else c->diag           = 0;
33976ad4291fSHong Zhang   c->solve_work            = 0;
33986ad4291fSHong Zhang   c->saved_values          = 0;
33996ad4291fSHong Zhang   c->idiag                 = 0;
340071f1c65dSBarry Smith   c->ssor_work             = 0;
3401a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3402e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3403e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34046ad4291fSHong Zhang   c->xtoy                  = 0;
34056ad4291fSHong Zhang   c->XtoY                  = 0;
34066ad4291fSHong Zhang 
3407416022c9SBarry Smith   c->nz                 = a->nz;
3408416022c9SBarry Smith   c->maxnz              = a->maxnz;
3409273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3410754ec7b1SSatish Balay 
34116ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34126ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
34136ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
34146ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
34156ad4291fSHong Zhang     i = a->compressedrow.nrows;
34166ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
34176ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
34186ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
34196ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
342027ea64f8SHong Zhang   } else {
342127ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
342227ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
342327ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
34246ad4291fSHong Zhang   }
342588e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
34264108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
34274846f1f5SKris Buschelman 
34287adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
34293a40ed3dSBarry Smith   PetscFunctionReturn(0);
343017ab2063SBarry Smith }
343117ab2063SBarry Smith 
34324a2ae208SSatish Balay #undef __FUNCT__
3433b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3434b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3435b24902e0SBarry Smith {
3436b24902e0SBarry Smith   PetscErrorCode ierr;
3437d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
3438b24902e0SBarry Smith 
3439b24902e0SBarry Smith   PetscFunctionBegin;
3440b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3441b24902e0SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
3442b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3443f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3444b24902e0SBarry Smith   PetscFunctionReturn(0);
3445b24902e0SBarry Smith }
3446b24902e0SBarry Smith 
3447b24902e0SBarry Smith #undef __FUNCT__
34484a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3449a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
345017ab2063SBarry Smith {
3451416022c9SBarry Smith   Mat_SeqAIJ     *a;
3452416022c9SBarry Smith   Mat            B;
34536849ba73SBarry Smith   PetscErrorCode ierr;
34543c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
345538baddfdSBarry Smith   int            fd;
345638baddfdSBarry Smith   PetscMPIInt    size;
3457bcd2baecSBarry Smith   MPI_Comm       comm;
345817ab2063SBarry Smith 
34593a40ed3dSBarry Smith   PetscFunctionBegin;
3460e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3461d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
346229bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3463b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
34640752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3465552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
346617ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
346717ab2063SBarry Smith 
3468d64ed03dSBarry Smith   if (nz < 0) {
346929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3470d64ed03dSBarry Smith   }
3471d64ed03dSBarry Smith 
347217ab2063SBarry Smith   /* read in row lengths */
347397f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
34740752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
347517ab2063SBarry Smith 
34763c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
34773c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
34783c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
34793c601197SSatish Balay 
348017ab2063SBarry Smith   /* create our matrix */
3481f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3482f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3483b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3484ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3485416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
348617ab2063SBarry Smith 
34870752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
348817ab2063SBarry Smith 
348917ab2063SBarry Smith   /* read in nonzero values */
34900752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
349117ab2063SBarry Smith 
349217ab2063SBarry Smith   /* set matrix "i" values */
3493efb16452SHong Zhang   a->i[0] = 0;
349417ab2063SBarry Smith   for (i=1; i<= M; i++) {
3495416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3496416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
349717ab2063SBarry Smith   }
3498606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
349917ab2063SBarry Smith 
35006d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35016d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3502b3a1e11cSKris Buschelman   *A = B;
35033a40ed3dSBarry Smith   PetscFunctionReturn(0);
350417ab2063SBarry Smith }
350517ab2063SBarry Smith 
35064a2ae208SSatish Balay #undef __FUNCT__
3507b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3508dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35097264ac53SSatish Balay {
35107264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3511dfbe8321SBarry Smith   PetscErrorCode ierr;
3512*eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3513*eeffb40dSHong Zhang   PetscInt k;
3514*eeffb40dSHong Zhang #endif
35157264ac53SSatish Balay 
35163a40ed3dSBarry Smith   PetscFunctionBegin;
3517bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3518d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3519ca44d042SBarry Smith     *flg = PETSC_FALSE;
3520ca44d042SBarry Smith     PetscFunctionReturn(0);
3521bcd2baecSBarry Smith   }
35227264ac53SSatish Balay 
35237264ac53SSatish Balay   /* if the a->i are the same */
3524d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3525abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
35267264ac53SSatish Balay 
35277264ac53SSatish Balay   /* if a->j are the same */
352897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3529abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3530bcd2baecSBarry Smith 
3531bcd2baecSBarry Smith   /* if a->a are the same */
3532*eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3533*eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3534*eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3535*eeffb40dSHong Zhang       *flg = PETSC_FALSE;
35363a40ed3dSBarry Smith       PetscFunctionReturn(0);
3537*eeffb40dSHong Zhang     }
3538*eeffb40dSHong Zhang   }
3539*eeffb40dSHong Zhang #else
3540*eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3541*eeffb40dSHong Zhang #endif
3542*eeffb40dSHong Zhang   PetscFunctionReturn(0);
35437264ac53SSatish Balay }
354436db0b34SBarry Smith 
35454a2ae208SSatish Balay #undef __FUNCT__
35464a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
354705869f15SSatish Balay /*@
354836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
354936db0b34SBarry Smith               provided by the user.
355036db0b34SBarry Smith 
3551c75a6043SHong Zhang       Collective on MPI_Comm
355236db0b34SBarry Smith 
355336db0b34SBarry Smith    Input Parameters:
355436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
355536db0b34SBarry Smith .   m - number of rows
355636db0b34SBarry Smith .   n - number of columns
355736db0b34SBarry Smith .   i - row indices
355836db0b34SBarry Smith .   j - column indices
355936db0b34SBarry Smith -   a - matrix values
356036db0b34SBarry Smith 
356136db0b34SBarry Smith    Output Parameter:
356236db0b34SBarry Smith .   mat - the matrix
356336db0b34SBarry Smith 
356436db0b34SBarry Smith    Level: intermediate
356536db0b34SBarry Smith 
356636db0b34SBarry Smith    Notes:
35670551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
356836db0b34SBarry Smith     once the matrix is destroyed
356936db0b34SBarry Smith 
357036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
357136db0b34SBarry Smith 
3572bfeeae90SHong Zhang        The i and j indices are 0 based
357336db0b34SBarry Smith 
3574a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3575a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3576a4552177SSatish Balay     as shown:
3577a4552177SSatish Balay 
3578a4552177SSatish Balay         1 0 0
3579a4552177SSatish Balay         2 0 3
3580a4552177SSatish Balay         4 5 6
3581a4552177SSatish Balay 
3582a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
35839985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3584a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3585a4552177SSatish Balay 
35869985e31cSBarry Smith 
35872fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
358836db0b34SBarry Smith 
358936db0b34SBarry Smith @*/
3590a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
359136db0b34SBarry Smith {
3592dfbe8321SBarry Smith   PetscErrorCode ierr;
3593cbcfb4deSHong Zhang   PetscInt       ii;
359436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3595cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3596cbcfb4deSHong Zhang   PetscInt       jj;
3597cbcfb4deSHong Zhang #endif
359836db0b34SBarry Smith 
359936db0b34SBarry Smith   PetscFunctionBegin;
3600a96a251dSBarry Smith   if (i[0]) {
3601634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
360236db0b34SBarry Smith   }
3603f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3604f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3605ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3606ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3607ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3608ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3609ab93d7beSBarry Smith 
361036db0b34SBarry Smith   aij->i = i;
361136db0b34SBarry Smith   aij->j = j;
361236db0b34SBarry Smith   aij->a = a;
361336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
361436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3615e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3616e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
361736db0b34SBarry Smith 
361836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
361936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
36202515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
362179a5c55eSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
36229985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
36239985e31cSBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
36249985e31cSBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(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);
36259985e31cSBarry Smith     }
362636db0b34SBarry Smith #endif
362736db0b34SBarry Smith   }
36282515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
362936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
363079a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
363179a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
363236db0b34SBarry Smith   }
363336db0b34SBarry Smith #endif
363436db0b34SBarry Smith 
3635b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3636b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
363736db0b34SBarry Smith   PetscFunctionReturn(0);
363836db0b34SBarry Smith }
363936db0b34SBarry Smith 
3640cc8ba8e1SBarry Smith #undef __FUNCT__
3641ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3642dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3643cc8ba8e1SBarry Smith {
3644dfbe8321SBarry Smith   PetscErrorCode ierr;
3645cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
364636db0b34SBarry Smith 
3647cc8ba8e1SBarry Smith   PetscFunctionBegin;
36488ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3649cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3650cc8ba8e1SBarry Smith     a->coloring = coloring;
365112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
365297f1f81fSBarry Smith     PetscInt             i,*larray;
365312c595b3SBarry Smith     ISColoring      ocoloring;
365408b6dcc0SBarry Smith     ISColoringValue *colors;
365512c595b3SBarry Smith 
365612c595b3SBarry Smith     /* set coloring for diagonal portion */
3657d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3658d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
365912c595b3SBarry Smith       larray[i] = i;
366012c595b3SBarry Smith     }
3661d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3662d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3663d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
366412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
366512c595b3SBarry Smith     }
366612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3667d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
366812c595b3SBarry Smith     a->coloring = ocoloring;
366912c595b3SBarry Smith   }
3670cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3671cc8ba8e1SBarry Smith }
3672cc8ba8e1SBarry Smith 
3673dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3674ee4f033dSBarry Smith EXTERN_C_BEGIN
367529c1e371SBarry Smith #include "adic/ad_utils.h"
3676ee4f033dSBarry Smith EXTERN_C_END
3677cc8ba8e1SBarry Smith 
3678cc8ba8e1SBarry Smith #undef __FUNCT__
3679ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3680dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3681cc8ba8e1SBarry Smith {
3682cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3683d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
36844440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
368508b6dcc0SBarry Smith   ISColoringValue *color;
3686cc8ba8e1SBarry Smith 
3687cc8ba8e1SBarry Smith   PetscFunctionBegin;
3688e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
36894440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3690cc8ba8e1SBarry Smith   color = a->coloring->colors;
3691cc8ba8e1SBarry Smith   /* loop over rows */
3692cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3693cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3694cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3695cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3696cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3697cc8ba8e1SBarry Smith     }
36984440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3699ee4f033dSBarry Smith   }
3700ee4f033dSBarry Smith   PetscFunctionReturn(0);
3701ee4f033dSBarry Smith }
3702ee4f033dSBarry Smith #endif
3703ee4f033dSBarry Smith 
3704ee4f033dSBarry Smith #undef __FUNCT__
3705ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
370697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3707ee4f033dSBarry Smith {
3708ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3709d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
371054f21887SBarry Smith   MatScalar       *v = a->a;
371154f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
371208b6dcc0SBarry Smith   ISColoringValue *color;
3713ee4f033dSBarry Smith 
3714ee4f033dSBarry Smith   PetscFunctionBegin;
3715e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3716ee4f033dSBarry Smith   color = a->coloring->colors;
3717ee4f033dSBarry Smith   /* loop over rows */
3718ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3719ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3720ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3721ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3722ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3723ee4f033dSBarry Smith     }
3724ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3725cc8ba8e1SBarry Smith   }
3726cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3727cc8ba8e1SBarry Smith }
372836db0b34SBarry Smith 
372981824310SBarry Smith /*
373081824310SBarry Smith     Special version for direct calls from Fortran
373181824310SBarry Smith */
373281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
373381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
373481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
373581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
373681824310SBarry Smith #endif
373781824310SBarry Smith 
373881824310SBarry Smith /* Change these macros so can be used in void function */
373981824310SBarry Smith #undef CHKERRQ
37407adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
374181824310SBarry Smith #undef SETERRQ2
37427adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
374381824310SBarry Smith 
374481824310SBarry Smith EXTERN_C_BEGIN
374581824310SBarry Smith #undef __FUNCT__
374681824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
37471f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
374881824310SBarry Smith {
374981824310SBarry Smith   Mat            A = *AA;
375081824310SBarry Smith   PetscInt       m = *mm, n = *nn;
375181824310SBarry Smith   InsertMode     is = *isis;
375281824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
375381824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
375481824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
375581824310SBarry Smith   PetscErrorCode ierr;
375681824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
375754f21887SBarry Smith   MatScalar      *ap,value,*aa;
3758edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
375981824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
376081824310SBarry Smith 
376181824310SBarry Smith   PetscFunctionBegin;
3762d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
376381824310SBarry Smith   imax = a->imax;
376481824310SBarry Smith   ai = a->i;
376581824310SBarry Smith   ailen = a->ilen;
376681824310SBarry Smith   aj = a->j;
376781824310SBarry Smith   aa = a->a;
376881824310SBarry Smith 
376981824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
377081824310SBarry Smith     row  = im[k];
377181824310SBarry Smith     if (row < 0) continue;
377281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3773d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
377481824310SBarry Smith #endif
377581824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
377681824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
377781824310SBarry Smith     low  = 0;
377881824310SBarry Smith     high = nrow;
377981824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
378081824310SBarry Smith       if (in[l] < 0) continue;
378181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3782d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
378381824310SBarry Smith #endif
378481824310SBarry Smith       col = in[l];
378581824310SBarry Smith       if (roworiented) {
378681824310SBarry Smith         value = v[l + k*n];
378781824310SBarry Smith       } else {
378881824310SBarry Smith         value = v[k + l*m];
378981824310SBarry Smith       }
379081824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
379181824310SBarry Smith 
379281824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
379381824310SBarry Smith       lastcol = col;
379481824310SBarry Smith       while (high-low > 5) {
379581824310SBarry Smith         t = (low+high)/2;
379681824310SBarry Smith         if (rp[t] > col) high = t;
379781824310SBarry Smith         else             low  = t;
379881824310SBarry Smith       }
379981824310SBarry Smith       for (i=low; i<high; i++) {
380081824310SBarry Smith         if (rp[i] > col) break;
380181824310SBarry Smith         if (rp[i] == col) {
380281824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
380381824310SBarry Smith           else                  ap[i] = value;
380481824310SBarry Smith           goto noinsert;
380581824310SBarry Smith         }
380681824310SBarry Smith       }
380781824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
380881824310SBarry Smith       if (nonew == 1) goto noinsert;
38097adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3810d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
381181824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
381281824310SBarry Smith       /* shift up all the later entries in this row */
381381824310SBarry Smith       for (ii=N; ii>=i; ii--) {
381481824310SBarry Smith         rp[ii+1] = rp[ii];
381581824310SBarry Smith         ap[ii+1] = ap[ii];
381681824310SBarry Smith       }
381781824310SBarry Smith       rp[i] = col;
381881824310SBarry Smith       ap[i] = value;
381981824310SBarry Smith       noinsert:;
382081824310SBarry Smith       low = i + 1;
382181824310SBarry Smith     }
382281824310SBarry Smith     ailen[row] = nrow;
382381824310SBarry Smith   }
382481824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
382581824310SBarry Smith   PetscFunctionReturnVoid();
382681824310SBarry Smith }
382781824310SBarry Smith EXTERN_C_END
3828