xref: /petsc/src/mat/impls/aij/seq/aij.c (revision b1646e7380629b38e5e6012c393b38f01f3c1f42)
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;
17871fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
17917ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
180416022c9SBarry Smith     row  = im[k];
1815ef9f2a5SBarry Smith     if (row < 0) continue;
1822515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
183d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
1843b2fbd54SBarry Smith #endif
185bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18617ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
187416022c9SBarry Smith     low  = 0;
188c71e6ed7SBarry Smith     high = nrow;
18917ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1905ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1912515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
192d0f46423SBarry 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);
1933b2fbd54SBarry Smith #endif
194bfeeae90SHong Zhang       col = in[l];
19516371a99SBarry Smith       if (v) {
1964b0e389bSBarry Smith 	if (roworiented) {
1975ef9f2a5SBarry Smith 	  value = v[l + k*n];
198bef8e0ddSBarry Smith 	} else {
1994b0e389bSBarry Smith 	  value = v[k + l*m];
2004b0e389bSBarry Smith 	}
20116371a99SBarry Smith       } else {
20275567043SBarry Smith         value = 0.;
20316371a99SBarry Smith       }
204abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
20536db0b34SBarry Smith 
2067cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
207e2ee6c50SBarry Smith       lastcol = col;
208416022c9SBarry Smith       while (high-low > 5) {
209416022c9SBarry Smith         t = (low+high)/2;
210416022c9SBarry Smith         if (rp[t] > col) high = t;
211416022c9SBarry Smith         else             low  = t;
21217ab2063SBarry Smith       }
213416022c9SBarry Smith       for (i=low; i<high; i++) {
21417ab2063SBarry Smith         if (rp[i] > col) break;
21517ab2063SBarry Smith         if (rp[i] == col) {
216416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
21717ab2063SBarry Smith           else                  ap[i] = value;
218e44c0bd4SBarry Smith           low = i + 1;
21917ab2063SBarry Smith           goto noinsert;
22017ab2063SBarry Smith         }
22117ab2063SBarry Smith       }
222abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
223c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
224cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
225d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
226c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
227416022c9SBarry Smith       /* shift up all the later entries in this row */
228416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
22917ab2063SBarry Smith         rp[ii+1] = rp[ii];
23017ab2063SBarry Smith         ap[ii+1] = ap[ii];
23117ab2063SBarry Smith       }
23217ab2063SBarry Smith       rp[i] = col;
23317ab2063SBarry Smith       ap[i] = value;
234416022c9SBarry Smith       low   = i + 1;
235e44c0bd4SBarry Smith       noinsert:;
23617ab2063SBarry Smith     }
23717ab2063SBarry Smith     ailen[row] = nrow;
23817ab2063SBarry Smith   }
23988e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2403a40ed3dSBarry Smith   PetscFunctionReturn(0);
24117ab2063SBarry Smith }
24217ab2063SBarry Smith 
24381824310SBarry Smith 
2444a2ae208SSatish Balay #undef __FUNCT__
2454a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
246a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2477eb43aa7SLois Curfman McInnes {
2487eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
24997f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
25097f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
25154f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2527eb43aa7SLois Curfman McInnes 
2533a40ed3dSBarry Smith   PetscFunctionBegin;
2547eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2557eb43aa7SLois Curfman McInnes     row  = im[k];
25697e567efSBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
257d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
258bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2597eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2607eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
26197e567efSBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
262d0f46423SBarry 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);
263bfeeae90SHong Zhang       col = in[l] ;
2647eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2657eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2667eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2677eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2687eb43aa7SLois Curfman McInnes         else             low  = t;
2697eb43aa7SLois Curfman McInnes       }
2707eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2717eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2727eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
273b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2747eb43aa7SLois Curfman McInnes           goto finished;
2757eb43aa7SLois Curfman McInnes         }
2767eb43aa7SLois Curfman McInnes       }
27797e567efSBarry Smith       *v++ = 0.0;
2787eb43aa7SLois Curfman McInnes       finished:;
2797eb43aa7SLois Curfman McInnes     }
2807eb43aa7SLois Curfman McInnes   }
2813a40ed3dSBarry Smith   PetscFunctionReturn(0);
2827eb43aa7SLois Curfman McInnes }
2837eb43aa7SLois Curfman McInnes 
28417ab2063SBarry Smith 
2854a2ae208SSatish Balay #undef __FUNCT__
2864a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
287dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28817ab2063SBarry Smith {
289416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2906849ba73SBarry Smith   PetscErrorCode ierr;
2916f69ff64SBarry Smith   PetscInt       i,*col_lens;
2926f69ff64SBarry Smith   int            fd;
29317ab2063SBarry Smith 
2943a40ed3dSBarry Smith   PetscFunctionBegin;
295b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
296d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
297552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
298d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
299d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
300416022c9SBarry Smith   col_lens[3] = a->nz;
301416022c9SBarry Smith 
302416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
303d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
304416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30517ab2063SBarry Smith   }
306d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
307606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
308416022c9SBarry Smith 
309416022c9SBarry Smith   /* store column indices (zero start index) */
3106f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
311416022c9SBarry Smith 
312416022c9SBarry Smith   /* store nonzero values */
3136f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3143a40ed3dSBarry Smith   PetscFunctionReturn(0);
31517ab2063SBarry Smith }
316416022c9SBarry Smith 
317dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
318cd155464SBarry Smith 
3194a2ae208SSatish Balay #undef __FUNCT__
3204a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
321dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
322416022c9SBarry Smith {
323416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
324dfbe8321SBarry Smith   PetscErrorCode    ierr;
325d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
326e060cb09SBarry Smith   const char        *name;
327f3ef73ceSBarry Smith   PetscViewerFormat format;
32817ab2063SBarry Smith 
3293a40ed3dSBarry Smith   PetscFunctionBegin;
330435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
331b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33271c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33397f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
334d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
335d00d2cf4SBarry Smith       nofinalvalue = 1;
336d00d2cf4SBarry Smith     }
337b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
338d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
33977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
34077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
341b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34217ab2063SBarry Smith 
34317ab2063SBarry Smith     for (i=0; i<m; i++) {
344416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
345aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34677431f27SBarry 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);
34717ab2063SBarry Smith #else
34877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34917ab2063SBarry Smith #endif
35017ab2063SBarry Smith       }
35117ab2063SBarry Smith     }
352d00d2cf4SBarry Smith     if (nofinalvalue) {
353d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
354d00d2cf4SBarry Smith     }
355fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
356b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35768369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
358cd155464SBarry Smith      PetscFunctionReturn(0);
359fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
360b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36144cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36344cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
364aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36536db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
366a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36736db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
368a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36936db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
370a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3716831982aSBarry Smith         }
37244cd7ae7SLois Curfman McInnes #else
373a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37444cd7ae7SLois Curfman McInnes #endif
37544cd7ae7SLois Curfman McInnes       }
376b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37744cd7ae7SLois Curfman McInnes     }
378b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
379fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
38097f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
381b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
38297f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
383496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
384496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
385496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
386496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
387aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38836db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
389496be53dSLois Curfman McInnes #else
390496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
391496be53dSLois Curfman McInnes #endif
392496be53dSLois Curfman McInnes         }
393496be53dSLois Curfman McInnes       }
394496be53dSLois Curfman McInnes     }
3952e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3972e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39877431f27SBarry 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);}
39977431f27SBarry 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);}
40077431f27SBarry 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);}
40177431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40277431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40377431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
404496be53dSLois Curfman McInnes     }
405b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
406606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
407496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
408496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
40977431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
410496be53dSLois Curfman McInnes       }
411b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
412496be53dSLois Curfman McInnes     }
413b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
414496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
415496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
416496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
417aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41836db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
419b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4206831982aSBarry Smith           }
421496be53dSLois Curfman McInnes #else
422b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
423496be53dSLois Curfman McInnes #endif
424496be53dSLois Curfman McInnes         }
425496be53dSLois Curfman McInnes       }
426b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
427496be53dSLois Curfman McInnes     }
428b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
429fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
43097f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
43187828ca2SBarry Smith     PetscScalar value;
43202594712SBarry Smith 
433b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43402594712SBarry Smith     for (i=0; i<m; i++) {
43502594712SBarry Smith       jcnt = 0;
436d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
437e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43802594712SBarry Smith           value = a->a[cnt++];
439e24b481bSBarry Smith           jcnt++;
44002594712SBarry Smith         } else {
44102594712SBarry Smith           value = 0.0;
44202594712SBarry Smith         }
443aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
444b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44502594712SBarry Smith #else
446b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44702594712SBarry Smith #endif
44802594712SBarry Smith       }
449b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45002594712SBarry Smith     }
451b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4523c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4533c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4543c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4553c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4563c215bfdSMatthew Knepley #else
4573c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4583c215bfdSMatthew Knepley #endif
459d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4603c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4613c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4623c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4633c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4643c215bfdSMatthew 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);
4653c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
4663c215bfdSMatthew 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);
4673c215bfdSMatthew Knepley         } else {
4683c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4693c215bfdSMatthew Knepley         }
4703c215bfdSMatthew Knepley #else
4713c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4723c215bfdSMatthew Knepley #endif
4733c215bfdSMatthew Knepley       }
4743c215bfdSMatthew Knepley     }
4753c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4763a40ed3dSBarry Smith   } else {
47716cd7e1dSShri Abhyankar     PetscTruth newdatastruct=PETSC_FALSE,newdatastruct1=PETSC_FALSE;
478d6e78c31SSatish Balay     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
47916cd7e1dSShri Abhyankar     ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr);
48016cd7e1dSShri Abhyankar     ierr = PetscOptionsGetTruth(PETSC_NULL,"-lu_new",&newdatastruct1,PETSC_NULL);CHKERRQ(ierr);
48116cd7e1dSShri Abhyankar     if (A->factor && (newdatastruct || newdatastruct1)){
48216cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
48316cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
48416cd7e1dSShri Abhyankar         /* L part */
48516cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
48616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
48716cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
48816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48916cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
49016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
49116cd7e1dSShri Abhyankar           } else {
49216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
49316cd7e1dSShri Abhyankar           }
49416cd7e1dSShri Abhyankar #else
49516cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49616cd7e1dSShri Abhyankar #endif
49716cd7e1dSShri Abhyankar         }
49816cd7e1dSShri Abhyankar 	/* diagonal */
49916cd7e1dSShri Abhyankar 	j = a->diag[i];
50016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
50116cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
50216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50316cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
50416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50516cd7e1dSShri Abhyankar           } else {
50616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
50716cd7e1dSShri Abhyankar           }
50816cd7e1dSShri Abhyankar #else
50916cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
51016cd7e1dSShri Abhyankar #endif
51116cd7e1dSShri Abhyankar 
51216cd7e1dSShri Abhyankar 	/* U part */
51316cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
51416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
51516cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
51616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51716cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
51816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
51916cd7e1dSShri Abhyankar           } else {
52016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
52116cd7e1dSShri Abhyankar           }
52216cd7e1dSShri Abhyankar #else
52316cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
52416cd7e1dSShri Abhyankar #endif
52516cd7e1dSShri Abhyankar }
52616cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
52716cd7e1dSShri Abhyankar         }
52816cd7e1dSShri Abhyankar     } else {
52917ab2063SBarry Smith       for (i=0; i<m; i++) {
53077431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
531416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
532aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
53336db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
534a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
53536db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
536a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5373a40ed3dSBarry Smith           } else {
538a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
53917ab2063SBarry Smith           }
54017ab2063SBarry Smith #else
541a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
54217ab2063SBarry Smith #endif
54317ab2063SBarry Smith         }
544b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
54517ab2063SBarry Smith       }
54616cd7e1dSShri Abhyankar     }
547b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
54817ab2063SBarry Smith   }
549b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5503a40ed3dSBarry Smith   PetscFunctionReturn(0);
551416022c9SBarry Smith }
552416022c9SBarry Smith 
5534a2ae208SSatish Balay #undef __FUNCT__
5544a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
555dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
556416022c9SBarry Smith {
557480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
558416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
559dfbe8321SBarry Smith   PetscErrorCode    ierr;
560d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
56136db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
562b0a32e0cSBarry Smith   PetscViewer       viewer;
563f3ef73ceSBarry Smith   PetscViewerFormat format;
564cddf8d76SBarry Smith 
5653a40ed3dSBarry Smith   PetscFunctionBegin;
566480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
567b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
56819bcc07fSBarry Smith 
569b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
570416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5710513a670SBarry Smith 
572fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5730513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
574b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
575416022c9SBarry Smith     for (i=0; i<m; i++) {
576cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
577bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
578bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
579aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
58036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
581cddf8d76SBarry Smith #else
582cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
583cddf8d76SBarry Smith #endif
584b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
585cddf8d76SBarry Smith       }
586cddf8d76SBarry Smith     }
587b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
588cddf8d76SBarry Smith     for (i=0; i<m; i++) {
589cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
590bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
591bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
592cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
593b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
594cddf8d76SBarry Smith       }
595cddf8d76SBarry Smith     }
596b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
597cddf8d76SBarry Smith     for (i=0; i<m; i++) {
598cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
599bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
600bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
601aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
60236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
603cddf8d76SBarry Smith #else
604cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
605cddf8d76SBarry Smith #endif
606b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
607416022c9SBarry Smith       }
608416022c9SBarry Smith     }
6090513a670SBarry Smith   } else {
6100513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
6110513a670SBarry Smith     /* first determine max of all nonzero values */
61297f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
613b0a32e0cSBarry Smith     PetscDraw   popup;
61436db0b34SBarry Smith     PetscReal scale;
6150513a670SBarry Smith 
6160513a670SBarry Smith     for (i=0; i<nz; i++) {
6170513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
6180513a670SBarry Smith     }
619b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
620b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
621b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
6220513a670SBarry Smith     count = 0;
6230513a670SBarry Smith     for (i=0; i<m; i++) {
6240513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
625bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
626bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
62797f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
628b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
6290513a670SBarry Smith         count++;
6300513a670SBarry Smith       }
6310513a670SBarry Smith     }
6320513a670SBarry Smith   }
633480ef9eaSBarry Smith   PetscFunctionReturn(0);
634480ef9eaSBarry Smith }
635cddf8d76SBarry Smith 
6364a2ae208SSatish Balay #undef __FUNCT__
6374a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
638dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
639480ef9eaSBarry Smith {
640dfbe8321SBarry Smith   PetscErrorCode ierr;
641b0a32e0cSBarry Smith   PetscDraw      draw;
64236db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
643480ef9eaSBarry Smith   PetscTruth     isnull;
644480ef9eaSBarry Smith 
645480ef9eaSBarry Smith   PetscFunctionBegin;
646b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
647b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
648480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
649480ef9eaSBarry Smith 
650480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
651d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
652480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
653b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
654b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
655480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6563a40ed3dSBarry Smith   PetscFunctionReturn(0);
657416022c9SBarry Smith }
658416022c9SBarry Smith 
6594a2ae208SSatish Balay #undef __FUNCT__
6604a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
661dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
662416022c9SBarry Smith {
663dfbe8321SBarry Smith   PetscErrorCode ierr;
6646805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
665416022c9SBarry Smith 
6663a40ed3dSBarry Smith   PetscFunctionBegin;
66732077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
668fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
669fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
670c45a1595SBarry Smith   if (iascii) {
6713a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6720f5bd95cSBarry Smith   } else if (isbinary) {
6733a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6740f5bd95cSBarry Smith   } else if (isdraw) {
6753a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6765cd90555SBarry Smith   } else {
677958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
67817ab2063SBarry Smith   }
6794108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
6803a40ed3dSBarry Smith   PetscFunctionReturn(0);
68117ab2063SBarry Smith }
68219bcc07fSBarry Smith 
6834a2ae208SSatish Balay #undef __FUNCT__
6844a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
685dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
68617ab2063SBarry Smith {
687416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6886849ba73SBarry Smith   PetscErrorCode ierr;
68997f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
690d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
69154f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6923447b6efSHong Zhang   PetscReal      ratio=0.6;
69317ab2063SBarry Smith 
6943a40ed3dSBarry Smith   PetscFunctionBegin;
6953a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
69617ab2063SBarry Smith 
69743ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
69817ab2063SBarry Smith   for (i=1; i<m; i++) {
699416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
70017ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
70194a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
70217ab2063SBarry Smith     if (fshift) {
703bfeeae90SHong Zhang       ip = aj + ai[i] ;
704bfeeae90SHong Zhang       ap = aa + ai[i] ;
70517ab2063SBarry Smith       N  = ailen[i];
70617ab2063SBarry Smith       for (j=0; j<N; j++) {
70717ab2063SBarry Smith         ip[j-fshift] = ip[j];
70817ab2063SBarry Smith         ap[j-fshift] = ap[j];
70917ab2063SBarry Smith       }
71017ab2063SBarry Smith     }
71117ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
71217ab2063SBarry Smith   }
71317ab2063SBarry Smith   if (m) {
71417ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
71517ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
71617ab2063SBarry Smith   }
71717ab2063SBarry Smith   /* reset ilen and imax for each row */
71817ab2063SBarry Smith   for (i=0; i<m; i++) {
71917ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
72017ab2063SBarry Smith   }
721bfeeae90SHong Zhang   a->nz = ai[m];
72228b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
72328b2fa4aSMatthew Knepley     SETERRQ3(PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
72428b2fa4aSMatthew Knepley   }
72517ab2063SBarry Smith 
72609f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
727d0f46423SBarry 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);
728ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
729ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
730a0e1a203SBarry Smith 
731dd5f02e7SSatish Balay   a->reallocs          = 0;
7324e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
73336db0b34SBarry Smith   a->rmax              = rmax;
7344e220ebcSLois Curfman McInnes 
735cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
736317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
73788e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
73871c2f376SKris Buschelman 
7394108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
74071f1c65dSBarry Smith 
74171f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
7423a40ed3dSBarry Smith   PetscFunctionReturn(0);
74317ab2063SBarry Smith }
74417ab2063SBarry Smith 
7454a2ae208SSatish Balay #undef __FUNCT__
74699cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
74799cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
74899cafbc1SBarry Smith {
74999cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
75099cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
75154f21887SBarry Smith   MatScalar      *aa = a->a;
75299cafbc1SBarry Smith 
75399cafbc1SBarry Smith   PetscFunctionBegin;
75499cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
75599cafbc1SBarry Smith   PetscFunctionReturn(0);
75699cafbc1SBarry Smith }
75799cafbc1SBarry Smith 
75899cafbc1SBarry Smith #undef __FUNCT__
75999cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
76099cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
76199cafbc1SBarry Smith {
76299cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
76399cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
76454f21887SBarry Smith   MatScalar      *aa = a->a;
76599cafbc1SBarry Smith 
76699cafbc1SBarry Smith   PetscFunctionBegin;
76799cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
76899cafbc1SBarry Smith   PetscFunctionReturn(0);
76999cafbc1SBarry Smith }
77099cafbc1SBarry Smith 
77199cafbc1SBarry Smith #undef __FUNCT__
7724a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
773dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
77417ab2063SBarry Smith {
775416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
776dfbe8321SBarry Smith   PetscErrorCode ierr;
7773a40ed3dSBarry Smith 
7783a40ed3dSBarry Smith   PetscFunctionBegin;
779d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7803a40ed3dSBarry Smith   PetscFunctionReturn(0);
78117ab2063SBarry Smith }
782416022c9SBarry Smith 
7834a2ae208SSatish Balay #undef __FUNCT__
7844a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
785dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
78617ab2063SBarry Smith {
787416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
788dfbe8321SBarry Smith   PetscErrorCode ierr;
789d5d45c9bSBarry Smith 
7903a40ed3dSBarry Smith   PetscFunctionBegin;
791aa482453SBarry Smith #if defined(PETSC_USE_LOG)
792d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
79317ab2063SBarry Smith #endif
794e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
795c38d4ed2SBarry Smith   if (a->row) {
796c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
797c38d4ed2SBarry Smith   }
798c38d4ed2SBarry Smith   if (a->col) {
799c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
800c38d4ed2SBarry Smith   }
80105b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
80205b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
80371f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
80405b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
80582bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
80605b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
807cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
80805b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
809407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
8100e83c824SBarry Smith   if (a->compressedrow.checked && a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);}
811a30b2313SHong Zhang 
8124108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
8134846f1f5SKris Buschelman 
814606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
815901853e0SKris Buschelman 
816dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
817901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
818901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
819901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
820901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
821901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
822ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
823901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
824901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
825a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
826901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
8273a40ed3dSBarry Smith   PetscFunctionReturn(0);
82817ab2063SBarry Smith }
82917ab2063SBarry Smith 
8304a2ae208SSatish Balay #undef __FUNCT__
8314a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
8324e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
83317ab2063SBarry Smith {
834416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8354846f1f5SKris Buschelman   PetscErrorCode ierr;
8363a40ed3dSBarry Smith 
8373a40ed3dSBarry Smith   PetscFunctionBegin;
838a65d3064SKris Buschelman   switch (op) {
839a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
8404e0d8c25SBarry Smith       a->roworiented       = flg;
841a65d3064SKris Buschelman       break;
842a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
843a9817697SBarry Smith       a->keepnonzeropattern    = flg;
844a65d3064SKris Buschelman       break;
845512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
846512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
847a65d3064SKris Buschelman       break;
848a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
8494e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
850a65d3064SKris Buschelman       break;
851a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8524e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
853a65d3064SKris Buschelman       break;
85428b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
85528b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
85628b2fa4aSMatthew Knepley       break;
857a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8584e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8590df259c2SBarry Smith       break;
860d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8614e0d8c25SBarry Smith       a->compressedrow.use = flg;
862d487561eSHong Zhang       break;
863*b1646e73SJed Brown     case MAT_SYMMETRIC:
864*b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
865*b1646e73SJed Brown     case MAT_HERMITIAN:
866*b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
8674e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
868a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
869a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
870290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
871a65d3064SKris Buschelman       break;
872b87ac2d8SJed Brown     case MAT_USE_INODES:
873b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
874b87ac2d8SJed Brown       break;
875a65d3064SKris Buschelman     default:
87677b7860eSJed Brown       SETERRQ1(PETSC_ERR_SUP,"unknown option %d",op);
877a65d3064SKris Buschelman   }
8784108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
8793a40ed3dSBarry Smith   PetscFunctionReturn(0);
88017ab2063SBarry Smith }
88117ab2063SBarry Smith 
8824a2ae208SSatish Balay #undef __FUNCT__
8834a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
884dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
88517ab2063SBarry Smith {
886416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8876849ba73SBarry Smith   PetscErrorCode ierr;
88897f1f81fSBarry Smith   PetscInt       i,j,n;
88987828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
89017ab2063SBarry Smith 
8913a40ed3dSBarry Smith   PetscFunctionBegin;
8922dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8931ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
89436db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
895d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
896d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
897bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
898bfeeae90SHong Zhang       if (a->j[j] == i) {
899416022c9SBarry Smith         x[i] = a->a[j];
90017ab2063SBarry Smith         break;
90117ab2063SBarry Smith       }
90217ab2063SBarry Smith     }
90317ab2063SBarry Smith   }
9041ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
9053a40ed3dSBarry Smith   PetscFunctionReturn(0);
90617ab2063SBarry Smith }
90717ab2063SBarry Smith 
908b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9094a2ae208SSatish Balay #undef __FUNCT__
9104a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
911dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
91217ab2063SBarry Smith {
913416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
9145c897100SBarry Smith   PetscScalar       *x,*y;
915dfbe8321SBarry Smith   PetscErrorCode    ierr;
916d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
9175c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
918a77337e4SBarry Smith   MatScalar         *v;
919a77337e4SBarry Smith   PetscScalar       alpha;
92004fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
9213447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
9224eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
9235c897100SBarry Smith #endif
92417ab2063SBarry Smith 
9253a40ed3dSBarry Smith   PetscFunctionBegin;
9262e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
9271ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9281ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9295c897100SBarry Smith 
9305c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
931bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
9325c897100SBarry Smith #else
9333447b6efSHong Zhang   if (usecprow){
9343447b6efSHong Zhang     m    = cprow.nrows;
9353447b6efSHong Zhang     ii   = cprow.i;
9367b2bb3b9SHong Zhang     ridx = cprow.rindex;
9373447b6efSHong Zhang   } else {
9383447b6efSHong Zhang     ii = a->i;
9393447b6efSHong Zhang   }
94017ab2063SBarry Smith   for (i=0; i<m; i++) {
9413447b6efSHong Zhang     idx   = a->j + ii[i] ;
9423447b6efSHong Zhang     v     = a->a + ii[i] ;
9433447b6efSHong Zhang     n     = ii[i+1] - ii[i];
9443447b6efSHong Zhang     if (usecprow){
9457b2bb3b9SHong Zhang       alpha = x[ridx[i]];
9463447b6efSHong Zhang     } else {
94717ab2063SBarry Smith       alpha = x[i];
9483447b6efSHong Zhang     }
94904fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
95017ab2063SBarry Smith   }
9515c897100SBarry Smith #endif
952dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
9531ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9541ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9553a40ed3dSBarry Smith   PetscFunctionReturn(0);
95617ab2063SBarry Smith }
95717ab2063SBarry Smith 
9584a2ae208SSatish Balay #undef __FUNCT__
9595c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
960dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9615c897100SBarry Smith {
962dfbe8321SBarry Smith   PetscErrorCode ierr;
9635c897100SBarry Smith 
9645c897100SBarry Smith   PetscFunctionBegin;
965170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9665c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9675c897100SBarry Smith   PetscFunctionReturn(0);
9685c897100SBarry Smith }
9695c897100SBarry Smith 
970a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9715c897100SBarry Smith #undef __FUNCT__
9724a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
973dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
97417ab2063SBarry Smith {
975416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
976d9fead3dSBarry Smith   PetscScalar       *y;
97754f21887SBarry Smith   const PetscScalar *x;
97854f21887SBarry Smith   const MatScalar   *aa;
979dfbe8321SBarry Smith   PetscErrorCode    ierr;
980003131ecSBarry Smith   PetscInt          m=A->rmap->n;
981003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9828aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
983362ced78SSatish Balay   PetscScalar       sum;
98497952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
98517ab2063SBarry Smith 
986b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
98797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
988fee21e36SBarry Smith #endif
989fee21e36SBarry Smith 
9903a40ed3dSBarry Smith   PetscFunctionBegin;
991d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9921ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
99397952fefSHong Zhang   aj  = a->j;
99497952fefSHong Zhang   aa  = a->a;
995416022c9SBarry Smith   ii  = a->i;
9964eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
99797952fefSHong Zhang     m    = a->compressedrow.nrows;
99897952fefSHong Zhang     ii   = a->compressedrow.i;
99997952fefSHong Zhang     ridx = a->compressedrow.rindex;
100097952fefSHong Zhang     for (i=0; i<m; i++){
100197952fefSHong Zhang       n   = ii[i+1] - ii[i];
100297952fefSHong Zhang       aj  = a->j + ii[i];
100397952fefSHong Zhang       aa  = a->a + ii[i];
100497952fefSHong Zhang       sum = 0.0;
1005a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1006003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1007003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
100897952fefSHong Zhang       y[*ridx++] = sum;
100997952fefSHong Zhang     }
101097952fefSHong Zhang   } else { /* do not use compressed row format */
1011b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1012b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1013b05257ddSBarry Smith #else
101417ab2063SBarry Smith     for (i=0; i<m; i++) {
1015003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1016003131ecSBarry Smith       aj  = a->j + ii[i];
1017003131ecSBarry Smith       aa  = a->a + ii[i];
101817ab2063SBarry Smith       sum  = 0.0;
1019a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1020003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
102117ab2063SBarry Smith       y[i] = sum;
102217ab2063SBarry Smith     }
10238d195f9aSBarry Smith #endif
1024b05257ddSBarry Smith   }
1025dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1026d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
10271ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10283a40ed3dSBarry Smith   PetscFunctionReturn(0);
102917ab2063SBarry Smith }
103017ab2063SBarry Smith 
1031a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
10324a2ae208SSatish Balay #undef __FUNCT__
10334a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1034dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
103517ab2063SBarry Smith {
1036416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
103754f21887SBarry Smith   PetscScalar     *x,*y,*z;
103854f21887SBarry Smith   const MatScalar *aa;
1039dfbe8321SBarry Smith   PetscErrorCode  ierr;
1040d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1041aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
104297952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1043362ced78SSatish Balay   PetscScalar     sum;
104497952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
1045e36a17ebSSatish Balay #endif
10469ea0dfa2SSatish Balay 
10473a40ed3dSBarry Smith   PetscFunctionBegin;
10481ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10491ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10502e8a6d31SBarry Smith   if (zz != yy) {
10511ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
10522e8a6d31SBarry Smith   } else {
10532e8a6d31SBarry Smith     z = y;
10542e8a6d31SBarry Smith   }
1055bfeeae90SHong Zhang 
105697952fefSHong Zhang   aj  = a->j;
105797952fefSHong Zhang   aa  = a->a;
1058cddf8d76SBarry Smith   ii  = a->i;
1059aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
106097952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
106102ab625aSSatish Balay #else
10624eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10634eb6d288SHong Zhang     if (zz != yy){
10644eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10654eb6d288SHong Zhang     }
106697952fefSHong Zhang     m    = a->compressedrow.nrows;
106797952fefSHong Zhang     ii   = a->compressedrow.i;
106897952fefSHong Zhang     ridx = a->compressedrow.rindex;
106997952fefSHong Zhang     for (i=0; i<m; i++){
107097952fefSHong Zhang       n  = ii[i+1] - ii[i];
107197952fefSHong Zhang       aj  = a->j + ii[i];
107297952fefSHong Zhang       aa  = a->a + ii[i];
107397952fefSHong Zhang       sum = y[*ridx];
107497952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
107597952fefSHong Zhang       z[*ridx++] = sum;
107697952fefSHong Zhang     }
107797952fefSHong Zhang   } else { /* do not use compressed row format */
107817ab2063SBarry Smith     for (i=0; i<m; i++) {
10799ea0dfa2SSatish Balay       jrow = ii[i];
10809ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
108117ab2063SBarry Smith       sum  = y[i];
10829ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
108397952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10849ea0dfa2SSatish Balay       }
108517ab2063SBarry Smith       z[i] = sum;
108617ab2063SBarry Smith     }
108797952fefSHong Zhang   }
108802ab625aSSatish Balay #endif
1089dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10901ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10911ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10922e8a6d31SBarry Smith   if (zz != yy) {
10931ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10942e8a6d31SBarry Smith   }
10953a40ed3dSBarry Smith   PetscFunctionReturn(0);
109617ab2063SBarry Smith }
109717ab2063SBarry Smith 
109817ab2063SBarry Smith /*
109917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
110017ab2063SBarry Smith */
11014a2ae208SSatish Balay #undef __FUNCT__
11024a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1103dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
110417ab2063SBarry Smith {
1105416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11066849ba73SBarry Smith   PetscErrorCode ierr;
1107d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
110817ab2063SBarry Smith 
11093a40ed3dSBarry Smith   PetscFunctionBegin;
111009f38230SBarry Smith   if (!a->diag) {
111109f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
11129518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
111309f38230SBarry Smith   }
1114d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
111509f38230SBarry Smith     a->diag[i] = a->i[i+1];
1116bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1117bfeeae90SHong Zhang       if (a->j[j] == i) {
111809f38230SBarry Smith         a->diag[i] = j;
111917ab2063SBarry Smith         break;
112017ab2063SBarry Smith       }
112117ab2063SBarry Smith     }
112217ab2063SBarry Smith   }
11233a40ed3dSBarry Smith   PetscFunctionReturn(0);
112417ab2063SBarry Smith }
112517ab2063SBarry Smith 
1126be5855fcSBarry Smith /*
1127be5855fcSBarry Smith      Checks for missing diagonals
1128be5855fcSBarry Smith */
11294a2ae208SSatish Balay #undef __FUNCT__
11304a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
113109f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1132be5855fcSBarry Smith {
1133be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
113497f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1135be5855fcSBarry Smith 
1136be5855fcSBarry Smith   PetscFunctionBegin;
113709f38230SBarry Smith   *missing = PETSC_FALSE;
1138d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
113909f38230SBarry Smith     *missing  = PETSC_TRUE;
114009f38230SBarry Smith     if (d) *d = 0;
114109f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
114209f38230SBarry Smith   } else {
1143f1e2ffcdSBarry Smith     diag = a->diag;
1144d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1145bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
114609f38230SBarry Smith 	*missing = PETSC_TRUE;
114709f38230SBarry Smith 	if (d) *d = i;
114809f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
114909f38230SBarry Smith       }
1150be5855fcSBarry Smith     }
1151be5855fcSBarry Smith   }
1152be5855fcSBarry Smith   PetscFunctionReturn(0);
1153be5855fcSBarry Smith }
1154be5855fcSBarry Smith 
115571f1c65dSBarry Smith EXTERN_C_BEGIN
115671f1c65dSBarry Smith #undef __FUNCT__
115771f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
115871f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
115971f1c65dSBarry Smith {
116071f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
116171f1c65dSBarry Smith   PetscErrorCode ierr;
1162d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
116354f21887SBarry Smith   MatScalar      *v = a->a;
116454f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
116571f1c65dSBarry Smith 
116671f1c65dSBarry Smith   PetscFunctionBegin;
116771f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
116871f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
116971f1c65dSBarry Smith   diag = a->diag;
117071f1c65dSBarry Smith   if (!a->idiag) {
117171f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
117271f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
117371f1c65dSBarry Smith     v        = a->a;
117471f1c65dSBarry Smith   }
117571f1c65dSBarry Smith   mdiag = a->mdiag;
117671f1c65dSBarry Smith   idiag = a->idiag;
117771f1c65dSBarry Smith 
1178028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
117971f1c65dSBarry Smith     for (i=0; i<m; i++) {
118071f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
118171f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
118271f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
118371f1c65dSBarry Smith     }
118471f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
118571f1c65dSBarry Smith   } else {
118671f1c65dSBarry Smith     for (i=0; i<m; i++) {
118771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
118871f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
118971f1c65dSBarry Smith     }
1190dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
119171f1c65dSBarry Smith   }
119271f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
119371f1c65dSBarry Smith   PetscFunctionReturn(0);
119471f1c65dSBarry Smith }
11955a9745a3SMatthew Knepley EXTERN_C_END
119671f1c65dSBarry Smith 
1197a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
11984a2ae208SSatish Balay #undef __FUNCT__
119941f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
120041f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
120117ab2063SBarry Smith {
1202416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1203e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1204e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
120554f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1206dfbe8321SBarry Smith   PetscErrorCode     ierr;
1207d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
120897f1f81fSBarry Smith   const PetscInt     *idx,*diag;
120917ab2063SBarry Smith 
12103a40ed3dSBarry Smith   PetscFunctionBegin;
1211b965ef7fSBarry Smith   its = its*lits;
121291723122SBarry Smith 
121371f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
121471f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
121571f1c65dSBarry Smith   a->fshift = fshift;
121671f1c65dSBarry Smith   a->omega  = omega;
1217ed480e8bSBarry Smith 
121871f1c65dSBarry Smith   diag = a->diag;
121971f1c65dSBarry Smith   t     = a->ssor_work;
1220ed480e8bSBarry Smith   idiag = a->idiag;
122171f1c65dSBarry Smith   mdiag = a->mdiag;
1222ed480e8bSBarry Smith 
12231ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1224fb2e594dSBarry Smith   if (xx != bb) {
12251ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1226fb2e594dSBarry Smith   } else {
1227fb2e594dSBarry Smith     b = x;
1228fb2e594dSBarry Smith   }
122971f1c65dSBarry Smith   CHKMEMQ;
1230ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
123117ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
123217ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1233ed480e8bSBarry Smith     bs = b;
123417ab2063SBarry Smith     for (i=0; i<m; i++) {
123571f1c65dSBarry Smith         d    = fshift + mdiag[i];
1236416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1237ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1238ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
123917ab2063SBarry Smith         sum  = b[i]*d/omega;
1240003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
124117ab2063SBarry Smith         x[i] = sum;
124217ab2063SBarry Smith     }
12431ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12441ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1245efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12463a40ed3dSBarry Smith     PetscFunctionReturn(0);
124717ab2063SBarry Smith   }
1248c783ea89SBarry Smith 
124948af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
125029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
12513a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
125217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1253887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
125417ab2063SBarry Smith 
125517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
125617ab2063SBarry Smith 
1257887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
125817ab2063SBarry Smith     */
125917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
126017ab2063SBarry Smith 
126117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
126217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1263416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1264ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1265ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
126617ab2063SBarry Smith       sum  = b[i];
1267e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1268ed480e8bSBarry Smith       x[i] = sum*idiag[i];
126917ab2063SBarry Smith     }
127017ab2063SBarry Smith 
127117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1272416022c9SBarry Smith     v = a->a;
1273ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
127417ab2063SBarry Smith 
127517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1276ed480e8bSBarry Smith     ts = t;
1277416022c9SBarry Smith     diag = a->diag;
127817ab2063SBarry Smith     for (i=0; i<m; i++) {
1279416022c9SBarry Smith       n    = diag[i] - a->i[i];
1280ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1281ed480e8bSBarry Smith       v    = a->a + a->i[i];
128217ab2063SBarry Smith       sum  = t[i];
1283003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1284ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1285733d66baSBarry Smith       /*  x = x + t */
1286733d66baSBarry Smith       x[i] += t[i];
128717ab2063SBarry Smith     }
128817ab2063SBarry Smith 
1289dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
12901ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12911ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12923a40ed3dSBarry Smith     PetscFunctionReturn(0);
129317ab2063SBarry Smith   }
129417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
129517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
129617ab2063SBarry Smith       for (i=0; i<m; i++) {
1297416022c9SBarry Smith         n    = diag[i] - a->i[i];
1298ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1299ed480e8bSBarry Smith         v    = a->a + a->i[i];
130017ab2063SBarry Smith         sum  = b[i];
1301e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13025c99c7daSBarry Smith         t[i] = sum;
1303ed480e8bSBarry Smith         x[i] = sum*idiag[i];
130417ab2063SBarry Smith       }
13055c99c7daSBarry Smith       xb = t;
1306efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13073a40ed3dSBarry Smith     } else xb = b;
130817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
130917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1310416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1311ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1312ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
131317ab2063SBarry Smith         sum  = xb[i];
1314e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13155c99c7daSBarry Smith         if (xb == b) {
1316ed480e8bSBarry Smith           x[i] = sum*idiag[i];
13175c99c7daSBarry Smith         } else {
13185c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
131917ab2063SBarry Smith         }
13205c99c7daSBarry Smith       }
1321efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
132217ab2063SBarry Smith     }
132317ab2063SBarry Smith     its--;
132417ab2063SBarry Smith   }
132517ab2063SBarry Smith   while (its--) {
132617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
132717ab2063SBarry Smith       for (i=0; i<m; i++) {
1328416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1329ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1330ed480e8bSBarry Smith         v    = a->a + a->i[i];
133117ab2063SBarry Smith         sum  = b[i];
1332e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1333ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
133417ab2063SBarry Smith       }
13359f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
133617ab2063SBarry Smith     }
133717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
133817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1339416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1340ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1341ed480e8bSBarry Smith         v    = a->a + a->i[i];
134217ab2063SBarry Smith         sum  = b[i];
1343e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1344ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
134517ab2063SBarry Smith       }
13469f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
134717ab2063SBarry Smith     }
134817ab2063SBarry Smith   }
13491ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13501ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
135171f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
135217ab2063SBarry Smith }
135317ab2063SBarry Smith 
13542af78befSBarry Smith 
13554a2ae208SSatish Balay #undef __FUNCT__
13564a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1357dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
135817ab2063SBarry Smith {
1359416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13604e220ebcSLois Curfman McInnes 
13613a40ed3dSBarry Smith   PetscFunctionBegin;
13624e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13634e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13644e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13654e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13664e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13674e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13687adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13694e220ebcSLois Curfman McInnes   if (A->factor) {
13704e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13714e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13724e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13734e220ebcSLois Curfman McInnes   } else {
13744e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13754e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13764e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13774e220ebcSLois Curfman McInnes   }
13783a40ed3dSBarry Smith   PetscFunctionReturn(0);
137917ab2063SBarry Smith }
138017ab2063SBarry Smith 
13814a2ae208SSatish Balay #undef __FUNCT__
13824a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1383f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
138417ab2063SBarry Smith {
1385416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1386d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d;
13876849ba73SBarry Smith   PetscErrorCode ierr;
138809f38230SBarry Smith   PetscTruth     missing;
138917ab2063SBarry Smith 
13903a40ed3dSBarry Smith   PetscFunctionBegin;
1391a9817697SBarry Smith   if (a->keepnonzeropattern) {
1392f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
139377431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1394bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1395f1e2ffcdSBarry Smith     }
1396f4df32b1SMatthew Knepley     if (diag != 0.0) {
139709f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
139809f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1399f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1400f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1401f1e2ffcdSBarry Smith       }
1402f1e2ffcdSBarry Smith     }
140388e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1404f1e2ffcdSBarry Smith   } else {
1405f4df32b1SMatthew Knepley     if (diag != 0.0) {
140617ab2063SBarry Smith       for (i=0; i<N; i++) {
140777431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
14087ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1409416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1410f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1411bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
14127ae801bdSBarry Smith         } else { /* in case row was completely empty */
1413f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
141417ab2063SBarry Smith         }
141517ab2063SBarry Smith       }
14163a40ed3dSBarry Smith     } else {
141717ab2063SBarry Smith       for (i=0; i<N; i++) {
141877431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1419416022c9SBarry Smith         a->ilen[rows[i]] = 0;
142017ab2063SBarry Smith       }
142117ab2063SBarry Smith     }
142288e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1423f1e2ffcdSBarry Smith   }
142443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14253a40ed3dSBarry Smith   PetscFunctionReturn(0);
142617ab2063SBarry Smith }
142717ab2063SBarry Smith 
14284a2ae208SSatish Balay #undef __FUNCT__
14294a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1430a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
143117ab2063SBarry Smith {
1432416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
143397f1f81fSBarry Smith   PetscInt   *itmp;
143417ab2063SBarry Smith 
14353a40ed3dSBarry Smith   PetscFunctionBegin;
1436d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
143717ab2063SBarry Smith 
1438416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1439bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
144017ab2063SBarry Smith   if (idx) {
1441bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1442bfeeae90SHong Zhang     if (*nz) {
14434e093b46SBarry Smith       *idx = itmp;
144417ab2063SBarry Smith     }
144517ab2063SBarry Smith     else *idx = 0;
144617ab2063SBarry Smith   }
14473a40ed3dSBarry Smith   PetscFunctionReturn(0);
144817ab2063SBarry Smith }
144917ab2063SBarry Smith 
1450bfeeae90SHong Zhang /* remove this function? */
14514a2ae208SSatish Balay #undef __FUNCT__
14524a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1453a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
145417ab2063SBarry Smith {
14553a40ed3dSBarry Smith   PetscFunctionBegin;
14563a40ed3dSBarry Smith   PetscFunctionReturn(0);
145717ab2063SBarry Smith }
145817ab2063SBarry Smith 
14594a2ae208SSatish Balay #undef __FUNCT__
14604a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1461dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
146217ab2063SBarry Smith {
1463416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
146454f21887SBarry Smith   MatScalar      *v = a->a;
146536db0b34SBarry Smith   PetscReal      sum = 0.0;
14666849ba73SBarry Smith   PetscErrorCode ierr;
146797f1f81fSBarry Smith   PetscInt       i,j;
146817ab2063SBarry Smith 
14693a40ed3dSBarry Smith   PetscFunctionBegin;
147017ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1471416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1472aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
147336db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
147417ab2063SBarry Smith #else
147517ab2063SBarry Smith       sum += (*v)*(*v); v++;
147617ab2063SBarry Smith #endif
147717ab2063SBarry Smith     }
1478064f8208SBarry Smith     *nrm = sqrt(sum);
14793a40ed3dSBarry Smith   } else if (type == NORM_1) {
148036db0b34SBarry Smith     PetscReal *tmp;
148197f1f81fSBarry Smith     PetscInt    *jj = a->j;
1482d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1483d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1484064f8208SBarry Smith     *nrm = 0.0;
1485416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1486bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
148717ab2063SBarry Smith     }
1488d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1489064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
149017ab2063SBarry Smith     }
1491606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14923a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1493064f8208SBarry Smith     *nrm = 0.0;
1494d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1495bfeeae90SHong Zhang       v = a->a + a->i[j];
149617ab2063SBarry Smith       sum = 0.0;
1497416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1498cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
149917ab2063SBarry Smith       }
1500064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
150117ab2063SBarry Smith     }
15023a40ed3dSBarry Smith   } else {
150329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
150417ab2063SBarry Smith   }
15053a40ed3dSBarry Smith   PetscFunctionReturn(0);
150617ab2063SBarry Smith }
150717ab2063SBarry Smith 
15084a2ae208SSatish Balay #undef __FUNCT__
15094a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1510fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
151117ab2063SBarry Smith {
1512416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1513416022c9SBarry Smith   Mat            C;
15146849ba73SBarry Smith   PetscErrorCode ierr;
1515d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
151654f21887SBarry Smith   MatScalar      *array = a->a;
151717ab2063SBarry Smith 
15183a40ed3dSBarry Smith   PetscFunctionBegin;
1519d0f46423SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1520fc4dec0aSBarry Smith 
1521fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1522d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1523d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1524bfeeae90SHong Zhang 
1525bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
15267adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1527d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
15287adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1529ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1530606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1531a541d17aSBarry Smith   } else {
1532a541d17aSBarry Smith     C = *B;
1533a541d17aSBarry Smith   }
1534a541d17aSBarry Smith 
153517ab2063SBarry Smith   for (i=0; i<m; i++) {
153617ab2063SBarry Smith     len    = ai[i+1]-ai[i];
153787d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1538b9b97703SBarry Smith     array += len;
1539b9b97703SBarry Smith     aj    += len;
154017ab2063SBarry Smith   }
15416d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15426d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
154317ab2063SBarry Smith 
1544815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1545416022c9SBarry Smith     *B = C;
154617ab2063SBarry Smith   } else {
1547273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
154817ab2063SBarry Smith   }
15493a40ed3dSBarry Smith   PetscFunctionReturn(0);
155017ab2063SBarry Smith }
155117ab2063SBarry Smith 
1552cd0d46ebSvictorle EXTERN_C_BEGIN
1553cd0d46ebSvictorle #undef __FUNCT__
15545fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1555be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1556cd0d46ebSvictorle {
1557cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
155854f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
155954f21887SBarry Smith   MatScalar      *va,*vb;
15606849ba73SBarry Smith   PetscErrorCode ierr;
156197f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1562cd0d46ebSvictorle 
1563cd0d46ebSvictorle   PetscFunctionBegin;
1564cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1565cd0d46ebSvictorle 
1566cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1567cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15685485867bSBarry Smith   if (ma!=nb || na!=mb){
15695485867bSBarry Smith     *f = PETSC_FALSE;
15705485867bSBarry Smith     PetscFunctionReturn(0);
15715485867bSBarry Smith   }
1572cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1573cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1574cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
157597f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
157697f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1577cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1578cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1579cd0d46ebSvictorle 
1580cd0d46ebSvictorle   *f = PETSC_TRUE;
1581cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1582cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
158397f1f81fSBarry Smith       PetscInt         idc,idr;
15845485867bSBarry Smith       PetscScalar vc,vr;
1585cd0d46ebSvictorle       /* column/row index/value */
15865485867bSBarry Smith       idc = adx[aptr[i]];
15875485867bSBarry Smith       idr = bdx[bptr[idc]];
15885485867bSBarry Smith       vc  = va[aptr[i]];
15895485867bSBarry Smith       vr  = vb[bptr[idc]];
15905485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15915485867bSBarry Smith 	*f = PETSC_FALSE;
15925485867bSBarry Smith         goto done;
1593cd0d46ebSvictorle       } else {
15945485867bSBarry Smith 	aptr[i]++;
15955485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1596cd0d46ebSvictorle       }
1597cd0d46ebSvictorle     }
1598cd0d46ebSvictorle   }
1599cd0d46ebSvictorle  done:
1600cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
16013aeef889SHong Zhang   if (B) {
16023aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
16033aeef889SHong Zhang   }
1604cd0d46ebSvictorle   PetscFunctionReturn(0);
1605cd0d46ebSvictorle }
1606cd0d46ebSvictorle EXTERN_C_END
1607cd0d46ebSvictorle 
16081cbb95d3SBarry Smith EXTERN_C_BEGIN
16091cbb95d3SBarry Smith #undef __FUNCT__
16101cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
16111cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
16121cbb95d3SBarry Smith {
16131cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
161454f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
161554f21887SBarry Smith   MatScalar      *va,*vb;
16161cbb95d3SBarry Smith   PetscErrorCode ierr;
16171cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
16181cbb95d3SBarry Smith 
16191cbb95d3SBarry Smith   PetscFunctionBegin;
16201cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
16211cbb95d3SBarry Smith 
16221cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
16231cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
16241cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
16251cbb95d3SBarry Smith     *f = PETSC_FALSE;
16261cbb95d3SBarry Smith     PetscFunctionReturn(0);
16271cbb95d3SBarry Smith   }
16281cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
16291cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
16301cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
16311cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
16321cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
16331cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
16341cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
16351cbb95d3SBarry Smith 
16361cbb95d3SBarry Smith   *f = PETSC_TRUE;
16371cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
16381cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
16391cbb95d3SBarry Smith       PetscInt         idc,idr;
16401cbb95d3SBarry Smith       PetscScalar vc,vr;
16411cbb95d3SBarry Smith       /* column/row index/value */
16421cbb95d3SBarry Smith       idc = adx[aptr[i]];
16431cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16441cbb95d3SBarry Smith       vc  = va[aptr[i]];
16451cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16461cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16471cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16481cbb95d3SBarry Smith         goto done;
16491cbb95d3SBarry Smith       } else {
16501cbb95d3SBarry Smith 	aptr[i]++;
16511cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16521cbb95d3SBarry Smith       }
16531cbb95d3SBarry Smith     }
16541cbb95d3SBarry Smith   }
16551cbb95d3SBarry Smith  done:
16561cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16571cbb95d3SBarry Smith   if (B) {
16581cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16591cbb95d3SBarry Smith   }
16601cbb95d3SBarry Smith   PetscFunctionReturn(0);
16611cbb95d3SBarry Smith }
16621cbb95d3SBarry Smith EXTERN_C_END
16631cbb95d3SBarry Smith 
16649e29f15eSvictorle #undef __FUNCT__
16659e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1666dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16679e29f15eSvictorle {
1668dfbe8321SBarry Smith   PetscErrorCode ierr;
16699e29f15eSvictorle   PetscFunctionBegin;
16705485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16719e29f15eSvictorle   PetscFunctionReturn(0);
16729e29f15eSvictorle }
16739e29f15eSvictorle 
16744a2ae208SSatish Balay #undef __FUNCT__
16751cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16761cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16771cbb95d3SBarry Smith {
16781cbb95d3SBarry Smith   PetscErrorCode ierr;
16791cbb95d3SBarry Smith   PetscFunctionBegin;
16801cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16811cbb95d3SBarry Smith   PetscFunctionReturn(0);
16821cbb95d3SBarry Smith }
16831cbb95d3SBarry Smith 
16841cbb95d3SBarry Smith #undef __FUNCT__
16854a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1686dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
168717ab2063SBarry Smith {
1688416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
168954f21887SBarry Smith   PetscScalar    *l,*r,x;
169054f21887SBarry Smith   MatScalar      *v;
1691dfbe8321SBarry Smith   PetscErrorCode ierr;
1692d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
169317ab2063SBarry Smith 
16943a40ed3dSBarry Smith   PetscFunctionBegin;
169517ab2063SBarry Smith   if (ll) {
16963ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16973ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1698e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1699d0f46423SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
17001ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1701416022c9SBarry Smith     v = a->a;
170217ab2063SBarry Smith     for (i=0; i<m; i++) {
170317ab2063SBarry Smith       x = l[i];
1704416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
170517ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
170617ab2063SBarry Smith     }
17071ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1708efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
170917ab2063SBarry Smith   }
171017ab2063SBarry Smith   if (rr) {
1711e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1712d0f46423SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
17131ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1714416022c9SBarry Smith     v = a->a; jj = a->j;
171517ab2063SBarry Smith     for (i=0; i<nz; i++) {
1716bfeeae90SHong Zhang       (*v++) *= r[*jj++];
171717ab2063SBarry Smith     }
17181ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1719efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
172017ab2063SBarry Smith   }
17213a40ed3dSBarry Smith   PetscFunctionReturn(0);
172217ab2063SBarry Smith }
172317ab2063SBarry Smith 
17244a2ae208SSatish Balay #undef __FUNCT__
17254a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
172697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
172717ab2063SBarry Smith {
1728db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
17296849ba73SBarry Smith   PetscErrorCode ierr;
1730d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
173197f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
17325d0c19d7SBarry Smith   const PetscInt *irow,*icol;
17335d0c19d7SBarry Smith   PetscInt       nrows,ncols;
173497f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
173554f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1736416022c9SBarry Smith   Mat            C;
173714ca34e6SBarry Smith   PetscTruth     stride,sorted;
173817ab2063SBarry Smith 
17393a40ed3dSBarry Smith   PetscFunctionBegin;
174014ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
174114ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
174214ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
174314ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
174499141d43SSatish Balay 
174517ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1746b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1747b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
174817ab2063SBarry Smith 
1749fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1750fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1751fee21e36SBarry Smith   if (stride && step == 1) {
175202834360SBarry Smith     /* special case of contiguous rows */
17530e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
175402834360SBarry Smith     /* loop over new rows determining lens and starting points */
175502834360SBarry Smith     for (i=0; i<nrows; i++) {
1756bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1757a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
175802834360SBarry Smith       for (k=kstart; k<kend; k++) {
1759bfeeae90SHong Zhang         if (aj[k] >= first) {
176002834360SBarry Smith           starts[i] = k;
176102834360SBarry Smith           break;
176202834360SBarry Smith 	}
176302834360SBarry Smith       }
1764a2744918SBarry Smith       sum = 0;
176502834360SBarry Smith       while (k < kend) {
1766bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1767a2744918SBarry Smith         sum++;
176802834360SBarry Smith       }
1769a2744918SBarry Smith       lens[i] = sum;
177002834360SBarry Smith     }
177102834360SBarry Smith     /* create submatrix */
1772cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
177397f1f81fSBarry Smith       PetscInt n_cols,n_rows;
177408480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
177529bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1776d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
177708480c60SBarry Smith       C = *B;
17783a40ed3dSBarry Smith     } else {
17797adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1780f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17817adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1782ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
178308480c60SBarry Smith     }
1784db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1785db02288aSLois Curfman McInnes 
178602834360SBarry Smith     /* loop over rows inserting into submatrix */
1787db02288aSLois Curfman McInnes     a_new    = c->a;
1788db02288aSLois Curfman McInnes     j_new    = c->j;
1789db02288aSLois Curfman McInnes     i_new    = c->i;
1790bfeeae90SHong Zhang 
179102834360SBarry Smith     for (i=0; i<nrows; i++) {
1792a2744918SBarry Smith       ii    = starts[i];
1793a2744918SBarry Smith       lensi = lens[i];
1794a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1795a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
179602834360SBarry Smith       }
179787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1798a2744918SBarry Smith       a_new      += lensi;
1799a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1800a2744918SBarry Smith       c->ilen[i]  = lensi;
180102834360SBarry Smith     }
18020e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
18033a40ed3dSBarry Smith   } else {
180402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
18050e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
180697f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
18070e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
180817ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
180902834360SBarry Smith     /* determine lens of each row */
181002834360SBarry Smith     for (i=0; i<nrows; i++) {
1811bfeeae90SHong Zhang       kstart  = ai[irow[i]];
181202834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
181302834360SBarry Smith       lens[i] = 0;
181402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1815bfeeae90SHong Zhang         if (smap[aj[k]]) {
181602834360SBarry Smith           lens[i]++;
181702834360SBarry Smith         }
181802834360SBarry Smith       }
181902834360SBarry Smith     }
182017ab2063SBarry Smith     /* Create and fill new matrix */
1821a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
18220f5bd95cSBarry Smith       PetscTruth equal;
18230f5bd95cSBarry Smith 
182499141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1825d0f46423SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1826d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
18270f5bd95cSBarry Smith       if (!equal) {
182829bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
182999141d43SSatish Balay       }
1830d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
183108480c60SBarry Smith       C = *B;
18323a40ed3dSBarry Smith     } else {
18337adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1834f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
18357adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1836ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
183708480c60SBarry Smith     }
183899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
183917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
184099141d43SSatish Balay       row    = irow[i];
1841bfeeae90SHong Zhang       kstart = ai[row];
184299141d43SSatish Balay       kend   = kstart + a->ilen[row];
1843bfeeae90SHong Zhang       mat_i  = c->i[i];
184499141d43SSatish Balay       mat_j  = c->j + mat_i;
184599141d43SSatish Balay       mat_a  = c->a + mat_i;
184699141d43SSatish Balay       mat_ilen = c->ilen + i;
184717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1848bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1849ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
185099141d43SSatish Balay           *mat_a++ = a->a[k];
185199141d43SSatish Balay           (*mat_ilen)++;
185299141d43SSatish Balay 
185317ab2063SBarry Smith         }
185417ab2063SBarry Smith       }
185517ab2063SBarry Smith     }
185602834360SBarry Smith     /* Free work space */
185702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1858606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1859606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
186002834360SBarry Smith   }
18616d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18626d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
186317ab2063SBarry Smith 
186417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1865416022c9SBarry Smith   *B = C;
18663a40ed3dSBarry Smith   PetscFunctionReturn(0);
186717ab2063SBarry Smith }
186817ab2063SBarry Smith 
18691df811f5SHong Zhang #undef __FUNCT__
18701df811f5SHong Zhang #define __FUNCT__ "MatILUFactor_SeqAIJ_newdatastruct"
18711df811f5SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ_newdatastruct(Mat inA,IS row,IS col,const MatFactorInfo *info)
18721df811f5SHong Zhang {
18731df811f5SHong Zhang   PetscErrorCode ierr;
18741df811f5SHong Zhang   Mat            C;
18751df811f5SHong Zhang 
18761df811f5SHong Zhang   PetscFunctionBegin;
18771df811f5SHong Zhang   /* A new factored matrix C replaces original matrix inA, not a truely in-place ILUFactor yet */
18781df811f5SHong Zhang   ierr = MatGetFactor(inA,MAT_SOLVER_PETSC,MAT_FACTOR_LU,&C);CHKERRQ(ierr);
18791df811f5SHong Zhang   ierr = MatILUFactorSymbolic_SeqAIJ_newdatastruct(C,inA,row,col,info);CHKERRQ(ierr);
18801df811f5SHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ_newdatastruct(C,inA,info);CHKERRQ(ierr);
18811df811f5SHong Zhang   inA->ops->solve            = C->ops->solve;
18821df811f5SHong Zhang   inA->ops->solvetranspose   = C->ops->solvetranspose;
18831df811f5SHong Zhang   ierr = MatHeaderCopy(inA,C);CHKERRQ(ierr);
18841df811f5SHong Zhang   ierr = PetscLogObjectParent(inA,((Mat_SeqAIJ*)(inA->data))->icol);CHKERRQ(ierr);
18851df811f5SHong Zhang   PetscFunctionReturn(0);
18861df811f5SHong Zhang }
18871df811f5SHong Zhang 
18884a2ae208SSatish Balay #undef __FUNCT__
18894a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18900481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1891a871dcd8SBarry Smith {
189263b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1893dfbe8321SBarry Smith   PetscErrorCode ierr;
189463b91edcSBarry Smith   Mat            outA;
1895b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
18961df811f5SHong Zhang   PetscTruth     newdatastruct=PETSC_FALSE;
189763b91edcSBarry Smith 
18983a40ed3dSBarry Smith   PetscFunctionBegin;
18991df811f5SHong Zhang   ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr);
19001df811f5SHong Zhang   if(newdatastruct){
19011df811f5SHong Zhang     ierr = MatILUFactor_SeqAIJ_newdatastruct(inA,row,col,info);CHKERRQ(ierr);
19021df811f5SHong Zhang     PetscFunctionReturn(0);
19031df811f5SHong Zhang   }
19041df811f5SHong Zhang 
1905d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
19061df811f5SHong Zhang 
1907b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1908b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1909a871dcd8SBarry Smith 
191063b91edcSBarry Smith   outA          = inA;
19111df811f5SHong Zhang   outA->factor  = MAT_FACTOR_LU;
1912c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1913c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1914c3122656SLisandro Dalcin   a->row = row;
1915c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1916c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1917c3122656SLisandro Dalcin   a->col = col;
191863b91edcSBarry Smith 
191936db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1920b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
19214c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
192252e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1923f0ec6fceSSatish Balay 
192494a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1925d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1926d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
192794a9d846SBarry Smith   }
192863b91edcSBarry Smith 
1929f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1930137fb511SHong Zhang   if (row_identity && col_identity) {
1931719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ(outA,inA,info);CHKERRQ(ierr);
1932137fb511SHong Zhang   } else {
1933719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1934137fb511SHong Zhang   }
19353a40ed3dSBarry Smith   PetscFunctionReturn(0);
1936a871dcd8SBarry Smith }
1937a871dcd8SBarry Smith 
1938d9eff348SSatish Balay #include "petscblaslapack.h"
19394a2ae208SSatish Balay #undef __FUNCT__
19404a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1941f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1942f0b747eeSBarry Smith {
1943f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1944f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1945efee365bSSatish Balay   PetscErrorCode ierr;
19460805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
19473a40ed3dSBarry Smith 
19483a40ed3dSBarry Smith   PetscFunctionBegin;
1949f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1950efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
19513a40ed3dSBarry Smith   PetscFunctionReturn(0);
1952f0b747eeSBarry Smith }
1953f0b747eeSBarry Smith 
19544a2ae208SSatish Balay #undef __FUNCT__
19554a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
195697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1957cddf8d76SBarry Smith {
1958dfbe8321SBarry Smith   PetscErrorCode ierr;
195997f1f81fSBarry Smith   PetscInt       i;
1960cddf8d76SBarry Smith 
19613a40ed3dSBarry Smith   PetscFunctionBegin;
1962cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1963b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1964cddf8d76SBarry Smith   }
1965cddf8d76SBarry Smith 
1966cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19676a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1968cddf8d76SBarry Smith   }
19693a40ed3dSBarry Smith   PetscFunctionReturn(0);
1970cddf8d76SBarry Smith }
1971cddf8d76SBarry Smith 
19724a2ae208SSatish Balay #undef __FUNCT__
19734a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
197497f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19754dcbc457SBarry Smith {
1976e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19776849ba73SBarry Smith   PetscErrorCode ierr;
19785d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19795d0c19d7SBarry Smith   const PetscInt *idx;
198097f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1981f1af5d2fSBarry Smith   PetscBT        table;
1982bbd702dbSSatish Balay 
19833a40ed3dSBarry Smith   PetscFunctionBegin;
1984d0f46423SBarry Smith   m     = A->rmap->n;
1985e4d965acSSatish Balay   ai    = a->i;
1986bfeeae90SHong Zhang   aj    = a->j;
19878a047759SSatish Balay 
1988a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
198906763907SSatish Balay 
199097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19916831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
199206763907SSatish Balay 
1993e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1994b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1995e4d965acSSatish Balay     isz  = 0;
19966831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1997e4d965acSSatish Balay 
1998e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19994dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2000b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2001e4d965acSSatish Balay 
2002dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2003e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2004f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
20054dcbc457SBarry Smith     }
200606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
200706763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
2008e4d965acSSatish Balay 
200904a348a9SBarry Smith     k = 0;
201004a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
201104a348a9SBarry Smith       n = isz;
201206763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2013e4d965acSSatish Balay         row   = nidx[k];
2014e4d965acSSatish Balay         start = ai[row];
2015e4d965acSSatish Balay         end   = ai[row+1];
201604a348a9SBarry Smith         for (l = start; l<end ; l++){
2017efb16452SHong Zhang           val = aj[l] ;
2018f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2019e4d965acSSatish Balay         }
2020e4d965acSSatish Balay       }
2021e4d965acSSatish Balay     }
2022029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
2023e4d965acSSatish Balay   }
20246831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2025606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
20263a40ed3dSBarry Smith   PetscFunctionReturn(0);
20274dcbc457SBarry Smith }
202817ab2063SBarry Smith 
20290513a670SBarry Smith /* -------------------------------------------------------------- */
20304a2ae208SSatish Balay #undef __FUNCT__
20314a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2032dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
20330513a670SBarry Smith {
20340513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
20356849ba73SBarry Smith   PetscErrorCode ierr;
20365d0c19d7SBarry Smith   PetscInt       i,nz,m = A->rmap->n,n = A->cmap->n;
20375d0c19d7SBarry Smith   const PetscInt *row,*col;
20385d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
203956cd22aeSBarry Smith   IS             icolp,irowp;
204097f1f81fSBarry Smith   PetscInt       *cwork;
204132ec9ce4SBarry Smith   PetscScalar    *vwork;
20420513a670SBarry Smith 
20433a40ed3dSBarry Smith   PetscFunctionBegin;
20444c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
204556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
20464c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
204756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
20480513a670SBarry Smith 
20490513a670SBarry Smith   /* determine lengths of permuted rows */
205097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
20510513a670SBarry Smith   for (i=0; i<m; i++) {
20520513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
20530513a670SBarry Smith   }
20547adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2055f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
20567adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2057ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2058606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
20590513a670SBarry Smith 
206097f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
20610513a670SBarry Smith   for (i=0; i<m; i++) {
206232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20630513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2064cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
206532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20660513a670SBarry Smith   }
2067606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20683c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20690513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20700513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
207156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
207256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
207356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
207456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20753a40ed3dSBarry Smith   PetscFunctionReturn(0);
20760513a670SBarry Smith }
20770513a670SBarry Smith 
20784a2ae208SSatish Balay #undef __FUNCT__
20794a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2080dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2081cb5b572fSBarry Smith {
2082dfbe8321SBarry Smith   PetscErrorCode ierr;
2083cb5b572fSBarry Smith 
2084cb5b572fSBarry Smith   PetscFunctionBegin;
208533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
208633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2087be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2088be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2089be6bf707SBarry Smith 
2090d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2091634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2092cb5b572fSBarry Smith     }
2093d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2094cb5b572fSBarry Smith   } else {
2095cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2096cb5b572fSBarry Smith   }
2097cb5b572fSBarry Smith   PetscFunctionReturn(0);
2098cb5b572fSBarry Smith }
2099cb5b572fSBarry Smith 
21004a2ae208SSatish Balay #undef __FUNCT__
21014a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2102dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2103273d9f13SBarry Smith {
2104dfbe8321SBarry Smith   PetscErrorCode ierr;
2105273d9f13SBarry Smith 
2106273d9f13SBarry Smith   PetscFunctionBegin;
2107ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2108273d9f13SBarry Smith   PetscFunctionReturn(0);
2109273d9f13SBarry Smith }
2110273d9f13SBarry Smith 
21114a2ae208SSatish Balay #undef __FUNCT__
21124a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2113a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
21146c0721eeSBarry Smith {
21156c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
21166c0721eeSBarry Smith   PetscFunctionBegin;
21176c0721eeSBarry Smith   *array = a->a;
21186c0721eeSBarry Smith   PetscFunctionReturn(0);
21196c0721eeSBarry Smith }
21206c0721eeSBarry Smith 
21214a2ae208SSatish Balay #undef __FUNCT__
21224a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2123dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
21246c0721eeSBarry Smith {
21256c0721eeSBarry Smith   PetscFunctionBegin;
21266c0721eeSBarry Smith   PetscFunctionReturn(0);
21276c0721eeSBarry Smith }
2128273d9f13SBarry Smith 
2129ee4f033dSBarry Smith #undef __FUNCT__
2130ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2131dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2132ee4f033dSBarry Smith {
21336849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
21346849ba73SBarry Smith   PetscErrorCode ierr;
213597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2136efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
213787828ca2SBarry Smith   PetscScalar    *vscale_array;
2138ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2139ee4f033dSBarry Smith   Vec            w1,w2,w3;
2140ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
214190d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2142ee4f033dSBarry Smith 
2143ee4f033dSBarry Smith   PetscFunctionBegin;
2144ee4f033dSBarry Smith   if (!coloring->w1) {
2145ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
214652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2147ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
214852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2149ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
215052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2151ee4f033dSBarry Smith   }
2152ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2153ee4f033dSBarry Smith 
2154ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
215590d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2156ee4f033dSBarry Smith   if (flg) {
2157ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2158ee4f033dSBarry Smith   } else {
21590b9b6f31SBarry Smith     PetscTruth assembled;
21600b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
21610b9b6f31SBarry Smith     if (assembled) {
2162ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2163ee4f033dSBarry Smith     }
21640b9b6f31SBarry Smith   }
2165ee4f033dSBarry Smith 
2166ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2167ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2168ee4f033dSBarry Smith 
2169ee4f033dSBarry Smith   /*
2170ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2171ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2172ee4f033dSBarry Smith   */
2173ee4f033dSBarry Smith   if (coloring->F) {
2174ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2175ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2176ee4f033dSBarry Smith     if (m1 != m2) {
2177ee4f033dSBarry Smith       coloring->F = 0;
2178ee4f033dSBarry Smith     }
2179ee4f033dSBarry Smith   }
2180ee4f033dSBarry Smith 
2181ee4f033dSBarry Smith   if (coloring->F) {
2182ee4f033dSBarry Smith     w1          = coloring->F;
2183ee4f033dSBarry Smith     coloring->F = 0;
2184ee4f033dSBarry Smith   } else {
218566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2186ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
218766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2188ee4f033dSBarry Smith   }
2189ee4f033dSBarry Smith 
2190ee4f033dSBarry Smith   /*
2191ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2192ee4f033dSBarry Smith   */
21931ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21941ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2195ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2196ee4f033dSBarry Smith     /*
2197ee4f033dSBarry Smith        Loop over each column associated with color adding the
2198ee4f033dSBarry Smith        perturbation to the vector w3.
2199ee4f033dSBarry Smith     */
2200ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2201ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2202ee4f033dSBarry Smith       dx  = xx[col];
2203ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2204ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2205ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2206ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2207ee4f033dSBarry Smith #else
2208ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2209ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2210ee4f033dSBarry Smith #endif
2211ee4f033dSBarry Smith       dx                *= epsilon;
2212ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2213ee4f033dSBarry Smith     }
2214ee4f033dSBarry Smith   }
22151ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2216ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2217ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2218ee4f033dSBarry Smith 
2219ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2220ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2221ee4f033dSBarry Smith 
2222ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2223ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2224ee4f033dSBarry Smith 
22251ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2226ee4f033dSBarry Smith   /*
2227ee4f033dSBarry Smith       Loop over each color
2228ee4f033dSBarry Smith   */
2229ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
223049b058dcSBarry Smith     coloring->currentcolor = k;
2231ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
22321ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2233ee4f033dSBarry Smith     /*
2234ee4f033dSBarry Smith        Loop over each column associated with color adding the
2235ee4f033dSBarry Smith        perturbation to the vector w3.
2236ee4f033dSBarry Smith     */
2237ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2238ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2239ee4f033dSBarry Smith       dx  = xx[col];
22405b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2241ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2242ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2243ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2244ee4f033dSBarry Smith #else
2245ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2246ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2247ee4f033dSBarry Smith #endif
2248ee4f033dSBarry Smith       dx            *= epsilon;
2249634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2250ee4f033dSBarry Smith       w3_array[col] += dx;
2251ee4f033dSBarry Smith     }
22521ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2253ee4f033dSBarry Smith 
2254ee4f033dSBarry Smith     /*
2255ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2256ee4f033dSBarry Smith     */
2257ee4f033dSBarry Smith 
225866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2259ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
226066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2261efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2262ee4f033dSBarry Smith 
2263ee4f033dSBarry Smith     /*
2264ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2265ee4f033dSBarry Smith     */
22661ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2267ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2268ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2269ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2270ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2271ee4f033dSBarry Smith       srow   = row + start;
2272ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2273ee4f033dSBarry Smith     }
22741ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2275ee4f033dSBarry Smith   }
227649b058dcSBarry Smith   coloring->currentcolor = k;
22771ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22781ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2279ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2280ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2281ee4f033dSBarry Smith   PetscFunctionReturn(0);
2282ee4f033dSBarry Smith }
2283ee4f033dSBarry Smith 
2284ac90fabeSBarry Smith #include "petscblaslapack.h"
2285ac90fabeSBarry Smith #undef __FUNCT__
2286ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2287f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2288ac90fabeSBarry Smith {
2289dfbe8321SBarry Smith   PetscErrorCode ierr;
229097f1f81fSBarry Smith   PetscInt       i;
2291ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22920805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2293ac90fabeSBarry Smith 
2294ac90fabeSBarry Smith   PetscFunctionBegin;
2295ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2296f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2297f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2298c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2299a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2300a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2301a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2302a30b2313SHong Zhang     }
2303a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2304d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2305a30b2313SHong Zhang       y->XtoY = X;
2306407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2307c537a176SHong Zhang     }
2308f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
23091e2582c4SBarry 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);
2310ac90fabeSBarry Smith   } else {
2311f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2312ac90fabeSBarry Smith   }
2313ac90fabeSBarry Smith   PetscFunctionReturn(0);
2314ac90fabeSBarry Smith }
2315ac90fabeSBarry Smith 
2316521d7252SBarry Smith #undef __FUNCT__
2317521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2318521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2319521d7252SBarry Smith {
232041c166b1SJed Brown   PetscErrorCode ierr;
232141c166b1SJed Brown 
2322521d7252SBarry Smith   PetscFunctionBegin;
232341c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
232441c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2325521d7252SBarry Smith   PetscFunctionReturn(0);
2326521d7252SBarry Smith }
2327521d7252SBarry Smith 
2328354c94deSBarry Smith #undef __FUNCT__
2329354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2330354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2331354c94deSBarry Smith {
2332354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2333354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2334354c94deSBarry Smith   PetscInt    i,nz;
2335354c94deSBarry Smith   PetscScalar *a;
2336354c94deSBarry Smith 
2337354c94deSBarry Smith   PetscFunctionBegin;
2338354c94deSBarry Smith   nz = aij->nz;
2339354c94deSBarry Smith   a  = aij->a;
2340354c94deSBarry Smith   for (i=0; i<nz; i++) {
2341354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2342354c94deSBarry Smith   }
2343354c94deSBarry Smith #else
2344354c94deSBarry Smith   PetscFunctionBegin;
2345354c94deSBarry Smith #endif
2346354c94deSBarry Smith   PetscFunctionReturn(0);
2347354c94deSBarry Smith }
2348354c94deSBarry Smith 
2349e34fafa9SBarry Smith #undef __FUNCT__
2350985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2351985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2352e34fafa9SBarry Smith {
2353e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2354e34fafa9SBarry Smith   PetscErrorCode ierr;
2355d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2356e34fafa9SBarry Smith   PetscReal      atmp;
2357985db425SBarry Smith   PetscScalar    *x;
2358e34fafa9SBarry Smith   MatScalar      *aa;
2359e34fafa9SBarry Smith 
2360e34fafa9SBarry Smith   PetscFunctionBegin;
2361e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2362e34fafa9SBarry Smith   aa   = a->a;
2363e34fafa9SBarry Smith   ai   = a->i;
2364e34fafa9SBarry Smith   aj   = a->j;
2365e34fafa9SBarry Smith 
2366985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2367e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2368e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2369d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2370e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2371e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23729189402eSHong Zhang     x[i] = 0.0;
2373e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2374985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2375985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2376985db425SBarry Smith       aa++; aj++;
2377985db425SBarry Smith     }
2378985db425SBarry Smith   }
2379985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2380985db425SBarry Smith   PetscFunctionReturn(0);
2381985db425SBarry Smith }
2382985db425SBarry Smith 
2383985db425SBarry Smith #undef __FUNCT__
2384985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2385985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2386985db425SBarry Smith {
2387985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2388985db425SBarry Smith   PetscErrorCode ierr;
2389d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2390985db425SBarry Smith   PetscScalar    *x;
2391985db425SBarry Smith   MatScalar      *aa;
2392985db425SBarry Smith 
2393985db425SBarry Smith   PetscFunctionBegin;
2394985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2395985db425SBarry Smith   aa   = a->a;
2396985db425SBarry Smith   ai   = a->i;
2397985db425SBarry Smith   aj   = a->j;
2398985db425SBarry Smith 
2399985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2400985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2401985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2402d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2403985db425SBarry Smith   for (i=0; i<m; i++) {
2404985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2405d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2406985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2407985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2408985db425SBarry Smith       x[i] = 0.0;
2409985db425SBarry Smith       if (idx) {
2410985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2411985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2412985db425SBarry Smith           if (aj[j] > j) {
2413985db425SBarry Smith             idx[i] = j;
2414985db425SBarry Smith             break;
2415985db425SBarry Smith           }
2416985db425SBarry Smith         }
2417985db425SBarry Smith       }
2418985db425SBarry Smith     }
2419985db425SBarry Smith     for (j=0; j<ncols; j++){
2420985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2421985db425SBarry Smith       aa++; aj++;
2422985db425SBarry Smith     }
2423985db425SBarry Smith   }
2424985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2425985db425SBarry Smith   PetscFunctionReturn(0);
2426985db425SBarry Smith }
2427985db425SBarry Smith 
2428985db425SBarry Smith #undef __FUNCT__
2429c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2430c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2431c87e5d42SMatthew Knepley {
2432c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2433c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2434c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2435c87e5d42SMatthew Knepley   PetscReal      atmp;
2436c87e5d42SMatthew Knepley   PetscScalar    *x;
2437c87e5d42SMatthew Knepley   MatScalar      *aa;
2438c87e5d42SMatthew Knepley 
2439c87e5d42SMatthew Knepley   PetscFunctionBegin;
2440c87e5d42SMatthew Knepley   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2441c87e5d42SMatthew Knepley   aa   = a->a;
2442c87e5d42SMatthew Knepley   ai   = a->i;
2443c87e5d42SMatthew Knepley   aj   = a->j;
2444c87e5d42SMatthew Knepley 
2445c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2446c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2447c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2448c87e5d42SMatthew Knepley   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2449c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2450c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2451289a08f5SMatthew Knepley     if (ncols) {
2452289a08f5SMatthew Knepley       /* Get first nonzero */
2453289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2454289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2455289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2456289a08f5SMatthew Knepley       }
2457289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2458289a08f5SMatthew Knepley     } else {
2459289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2460289a08f5SMatthew Knepley     }
2461c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2462c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2463289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2464c87e5d42SMatthew Knepley       aa++; aj++;
2465c87e5d42SMatthew Knepley     }
2466c87e5d42SMatthew Knepley   }
2467c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2468c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2469c87e5d42SMatthew Knepley }
2470c87e5d42SMatthew Knepley 
2471c87e5d42SMatthew Knepley #undef __FUNCT__
2472985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2473985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2474985db425SBarry Smith {
2475985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2476985db425SBarry Smith   PetscErrorCode ierr;
2477d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2478985db425SBarry Smith   PetscScalar    *x;
2479985db425SBarry Smith   MatScalar      *aa;
2480985db425SBarry Smith 
2481985db425SBarry Smith   PetscFunctionBegin;
2482985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2483985db425SBarry Smith   aa   = a->a;
2484985db425SBarry Smith   ai   = a->i;
2485985db425SBarry Smith   aj   = a->j;
2486985db425SBarry Smith 
2487985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2488985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2489985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2490d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2491985db425SBarry Smith   for (i=0; i<m; i++) {
2492985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2493d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2494985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2495985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2496985db425SBarry Smith       x[i] = 0.0;
2497985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2498985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2499985db425SBarry Smith         for (j=0;j<ncols;j++) {
2500985db425SBarry Smith           if (aj[j] > j) {
2501985db425SBarry Smith             idx[i] = j;
2502985db425SBarry Smith             break;
2503985db425SBarry Smith           }
2504985db425SBarry Smith         }
2505985db425SBarry Smith       }
2506985db425SBarry Smith     }
2507985db425SBarry Smith     for (j=0; j<ncols; j++){
2508985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2509985db425SBarry Smith       aa++; aj++;
2510e34fafa9SBarry Smith     }
2511e34fafa9SBarry Smith   }
2512e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2513e34fafa9SBarry Smith   PetscFunctionReturn(0);
2514e34fafa9SBarry Smith }
25153acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2516682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
25170a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2518cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2519cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2520cb5b572fSBarry Smith        MatMult_SeqAIJ,
252197304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
25227c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
25237c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2524db4efbfdSBarry Smith        0,
2525db4efbfdSBarry Smith        0,
2526db4efbfdSBarry Smith        0,
2527db4efbfdSBarry Smith /*10*/ 0,
2528cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2529cb5b572fSBarry Smith        0,
253041f059aeSBarry Smith        MatSOR_SeqAIJ,
253117ab2063SBarry Smith        MatTranspose_SeqAIJ,
253297304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2533cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2534cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2535cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2536cb5b572fSBarry Smith        MatNorm_SeqAIJ,
253797304618SKris Buschelman /*20*/ 0,
2538cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2539cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2540cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2541d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2542db4efbfdSBarry Smith        0,
2543db4efbfdSBarry Smith        0,
2544db4efbfdSBarry Smith        0,
2545db4efbfdSBarry Smith        0,
2546d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2547db4efbfdSBarry Smith        0,
2548db4efbfdSBarry Smith        0,
25496c0721eeSBarry Smith        MatGetArray_SeqAIJ,
25506c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2551d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2552cb5b572fSBarry Smith        0,
2553cb5b572fSBarry Smith        0,
2554cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2555cb5b572fSBarry Smith        0,
2556d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2557cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2558cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2559cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2560cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2561d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2562cb5b572fSBarry Smith        MatScale_SeqAIJ,
2563cb5b572fSBarry Smith        0,
256479299369SBarry Smith        MatDiagonalSet_SeqAIJ,
25656945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2566d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
25673b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
25683b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
25693b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2570a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2571d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2572b9617806SBarry Smith        0,
25730513a670SBarry Smith        0,
2574cda55fadSBarry Smith        MatPermute_SeqAIJ,
2575cda55fadSBarry Smith        0,
2576d519adbfSMatthew Knepley /*59*/ 0,
2577b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2578b9b97703SBarry Smith        MatView_SeqAIJ,
2579357abbc8SBarry Smith        0,
2580ee4f033dSBarry Smith        0,
2581d519adbfSMatthew Knepley /*64*/ 0,
2582ee4f033dSBarry Smith        0,
2583ee4f033dSBarry Smith        0,
2584ee4f033dSBarry Smith        0,
2585ee4f033dSBarry Smith        0,
2586d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2587c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2588ee4f033dSBarry Smith        0,
2589ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2590dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2591ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2592dcf5cc72SBarry Smith #else
2593dcf5cc72SBarry Smith        0,
2594dcf5cc72SBarry Smith #endif
2595d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
25963acb8795SBarry Smith        MatFDColoringApply_AIJ,
259797304618SKris Buschelman        0,
259897304618SKris Buschelman        0,
259997304618SKris Buschelman        0,
2600d519adbfSMatthew Knepley /*79*/ 0,
260197304618SKris Buschelman        0,
260297304618SKris Buschelman        0,
260397304618SKris Buschelman        0,
2604bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2605d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
26061cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
26076284ec50SHong Zhang        0,
26086284ec50SHong Zhang        0,
2609bc011b1eSHong Zhang        0,
2610d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
261126be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
261226be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2613d439da42SKris Buschelman        MatPtAP_Basic,
26147ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2615d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2616bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2617bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2618bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
26197ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2620d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2621609c6c4dSKris Buschelman        0,
2622609c6c4dSKris Buschelman        0,
262387d4246cSBarry Smith        MatConjugate_SeqAIJ,
262487d4246cSBarry Smith        0,
2625d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
262699cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2627f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2628f5edf698SHong Zhang        0,
26292bebee5dSHong Zhang        0,
2630d519adbfSMatthew Knepley /*109*/0,
2631985db425SBarry Smith        0,
26322af78befSBarry Smith        MatGetRowMin_SeqAIJ,
26332af78befSBarry Smith        0,
2634599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2635d519adbfSMatthew Knepley /*114*/0,
2636599ef60dSHong Zhang        0,
26373c2a7987SHong Zhang        0,
26383c2a7987SHong Zhang        MatILUDTFactorSymbolic_SeqAIJ,
26393c2a7987SHong Zhang        MatILUDTFactorNumeric_SeqAIJ
26409e29f15eSvictorle };
264117ab2063SBarry Smith 
2642fb2e594dSBarry Smith EXTERN_C_BEGIN
26434a2ae208SSatish Balay #undef __FUNCT__
26444a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2645be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2646bef8e0ddSBarry Smith {
2647bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
264897f1f81fSBarry Smith   PetscInt   i,nz,n;
2649bef8e0ddSBarry Smith 
2650bef8e0ddSBarry Smith   PetscFunctionBegin;
2651bef8e0ddSBarry Smith 
2652bef8e0ddSBarry Smith   nz = aij->maxnz;
2653d0f46423SBarry Smith   n  = mat->rmap->n;
2654bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2655bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2656bef8e0ddSBarry Smith   }
2657bef8e0ddSBarry Smith   aij->nz = nz;
2658bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2659bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2660bef8e0ddSBarry Smith   }
2661bef8e0ddSBarry Smith 
2662bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2663bef8e0ddSBarry Smith }
2664fb2e594dSBarry Smith EXTERN_C_END
2665bef8e0ddSBarry Smith 
26664a2ae208SSatish Balay #undef __FUNCT__
26674a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2668bef8e0ddSBarry Smith /*@
2669bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2670bef8e0ddSBarry Smith        in the matrix.
2671bef8e0ddSBarry Smith 
2672bef8e0ddSBarry Smith   Input Parameters:
2673bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2674bef8e0ddSBarry Smith -  indices - the column indices
2675bef8e0ddSBarry Smith 
267615091d37SBarry Smith   Level: advanced
267715091d37SBarry Smith 
2678bef8e0ddSBarry Smith   Notes:
2679bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2680bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2681bef8e0ddSBarry Smith   of the MatSetValues() operation.
2682bef8e0ddSBarry Smith 
2683bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2684d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2685bef8e0ddSBarry Smith 
2686bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2687bef8e0ddSBarry Smith 
2688b9617806SBarry Smith     The indices should start with zero, not one.
2689b9617806SBarry Smith 
2690bef8e0ddSBarry Smith @*/
2691be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2692bef8e0ddSBarry Smith {
269397f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2694bef8e0ddSBarry Smith 
2695bef8e0ddSBarry Smith   PetscFunctionBegin;
26964482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
26974482741eSBarry Smith   PetscValidPointer(indices,2);
2698c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2699bef8e0ddSBarry Smith   if (f) {
2700bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2701bef8e0ddSBarry Smith   } else {
2702634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2703bef8e0ddSBarry Smith   }
2704bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2705bef8e0ddSBarry Smith }
2706bef8e0ddSBarry Smith 
2707be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2708be6bf707SBarry Smith 
2709fb2e594dSBarry Smith EXTERN_C_BEGIN
27104a2ae208SSatish Balay #undef __FUNCT__
27114a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2712be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2713be6bf707SBarry Smith {
2714be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27156849ba73SBarry Smith   PetscErrorCode ierr;
2716d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2717be6bf707SBarry Smith 
2718be6bf707SBarry Smith   PetscFunctionBegin;
2719be6bf707SBarry Smith   if (aij->nonew != 1) {
2720512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2721be6bf707SBarry Smith   }
2722be6bf707SBarry Smith 
2723be6bf707SBarry Smith   /* allocate space for values if not already there */
2724be6bf707SBarry Smith   if (!aij->saved_values) {
272587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
27269518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2727be6bf707SBarry Smith   }
2728be6bf707SBarry Smith 
2729be6bf707SBarry Smith   /* copy values over */
273087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2731be6bf707SBarry Smith   PetscFunctionReturn(0);
2732be6bf707SBarry Smith }
2733fb2e594dSBarry Smith EXTERN_C_END
2734be6bf707SBarry Smith 
27354a2ae208SSatish Balay #undef __FUNCT__
2736b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2737be6bf707SBarry Smith /*@
2738be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2739be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2740be6bf707SBarry Smith        nonlinear portion.
2741be6bf707SBarry Smith 
2742be6bf707SBarry Smith    Collect on Mat
2743be6bf707SBarry Smith 
2744be6bf707SBarry Smith   Input Parameters:
27450e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2746be6bf707SBarry Smith 
274715091d37SBarry Smith   Level: advanced
274815091d37SBarry Smith 
2749be6bf707SBarry Smith   Common Usage, with SNESSolve():
2750be6bf707SBarry Smith $    Create Jacobian matrix
2751be6bf707SBarry Smith $    Set linear terms into matrix
2752be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2753be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2754be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2755512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2756be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2757be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2758be6bf707SBarry Smith $    In your Jacobian routine
2759be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2760be6bf707SBarry Smith $      Set nonlinear terms in matrix
2761be6bf707SBarry Smith 
2762be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2763be6bf707SBarry Smith $    // build linear portion of Jacobian
2764512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2765be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2766be6bf707SBarry Smith $    loop over nonlinear iterations
2767be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2768be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2769be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2770be6bf707SBarry Smith $       Solve linear system with Jacobian
2771be6bf707SBarry Smith $    endloop
2772be6bf707SBarry Smith 
2773be6bf707SBarry Smith   Notes:
2774be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2775512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2776be6bf707SBarry Smith     calling this routine.
2777be6bf707SBarry Smith 
27780c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
27790c468ba9SBarry Smith     and does not allocated additional space.
27800c468ba9SBarry Smith 
2781be6bf707SBarry Smith .seealso: MatRetrieveValues()
2782be6bf707SBarry Smith 
2783be6bf707SBarry Smith @*/
2784be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2785be6bf707SBarry Smith {
2786dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2787be6bf707SBarry Smith 
2788be6bf707SBarry Smith   PetscFunctionBegin;
27894482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
279029bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
279129bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2792be6bf707SBarry Smith 
2793c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2794be6bf707SBarry Smith   if (f) {
2795be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2796be6bf707SBarry Smith   } else {
2797634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2798be6bf707SBarry Smith   }
2799be6bf707SBarry Smith   PetscFunctionReturn(0);
2800be6bf707SBarry Smith }
2801be6bf707SBarry Smith 
2802fb2e594dSBarry Smith EXTERN_C_BEGIN
28034a2ae208SSatish Balay #undef __FUNCT__
28044a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2805be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2806be6bf707SBarry Smith {
2807be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
28086849ba73SBarry Smith   PetscErrorCode ierr;
2809d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2810be6bf707SBarry Smith 
2811be6bf707SBarry Smith   PetscFunctionBegin;
2812be6bf707SBarry Smith   if (aij->nonew != 1) {
2813512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2814be6bf707SBarry Smith   }
2815be6bf707SBarry Smith   if (!aij->saved_values) {
2816634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2817be6bf707SBarry Smith   }
2818be6bf707SBarry Smith   /* copy values over */
281987828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2820be6bf707SBarry Smith   PetscFunctionReturn(0);
2821be6bf707SBarry Smith }
2822fb2e594dSBarry Smith EXTERN_C_END
2823be6bf707SBarry Smith 
28244a2ae208SSatish Balay #undef __FUNCT__
28254a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2826be6bf707SBarry Smith /*@
2827be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2828be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2829be6bf707SBarry Smith        nonlinear portion.
2830be6bf707SBarry Smith 
2831be6bf707SBarry Smith    Collect on Mat
2832be6bf707SBarry Smith 
2833be6bf707SBarry Smith   Input Parameters:
2834be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2835be6bf707SBarry Smith 
283615091d37SBarry Smith   Level: advanced
283715091d37SBarry Smith 
2838be6bf707SBarry Smith .seealso: MatStoreValues()
2839be6bf707SBarry Smith 
2840be6bf707SBarry Smith @*/
2841be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2842be6bf707SBarry Smith {
2843dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2844be6bf707SBarry Smith 
2845be6bf707SBarry Smith   PetscFunctionBegin;
28464482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
284729bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
284829bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2849be6bf707SBarry Smith 
2850c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2851be6bf707SBarry Smith   if (f) {
2852be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2853be6bf707SBarry Smith   } else {
2854634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2855be6bf707SBarry Smith   }
2856be6bf707SBarry Smith   PetscFunctionReturn(0);
2857be6bf707SBarry Smith }
2858be6bf707SBarry Smith 
2859f83d6046SBarry Smith 
2860be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
28614a2ae208SSatish Balay #undef __FUNCT__
28624a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
286317ab2063SBarry Smith /*@C
2864682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
28650d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
28666e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
286751c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
28682bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
286917ab2063SBarry Smith 
2870db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2871db81eaa0SLois Curfman McInnes 
287217ab2063SBarry Smith    Input Parameters:
2873db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
287417ab2063SBarry Smith .  m - number of rows
287517ab2063SBarry Smith .  n - number of columns
287617ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
287751c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
28782bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
287917ab2063SBarry Smith 
288017ab2063SBarry Smith    Output Parameter:
2881416022c9SBarry Smith .  A - the matrix
288217ab2063SBarry Smith 
2883175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2884ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2885175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2886175b88e8SBarry Smith 
2887b259b22eSLois Curfman McInnes    Notes:
288849a6f317SBarry Smith    If nnz is given then nz is ignored
288949a6f317SBarry Smith 
289017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
289117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28920002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
289344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
289417ab2063SBarry Smith 
289517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2896a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28973d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28986da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
289917ab2063SBarry Smith 
2900682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
29014fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2902682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
29036c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
29046c7ebb05SLois Curfman McInnes 
29056c7ebb05SLois Curfman McInnes    Options Database Keys:
2906698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2907698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2908db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2909db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2910db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
291117ab2063SBarry Smith 
2912027ccd11SLois Curfman McInnes    Level: intermediate
2913027ccd11SLois Curfman McInnes 
291436db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
291536db0b34SBarry Smith 
291617ab2063SBarry Smith @*/
2917be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
291817ab2063SBarry Smith {
2919dfbe8321SBarry Smith   PetscErrorCode ierr;
29206945ee14SBarry Smith 
29213a40ed3dSBarry Smith   PetscFunctionBegin;
2922f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2923117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2924c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2925ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2926273d9f13SBarry Smith   PetscFunctionReturn(0);
2927273d9f13SBarry Smith }
2928273d9f13SBarry Smith 
29294a2ae208SSatish Balay #undef __FUNCT__
29304a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2931273d9f13SBarry Smith /*@C
2932273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2933273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2934273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2935273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2936273d9f13SBarry Smith 
2937273d9f13SBarry Smith    Collective on MPI_Comm
2938273d9f13SBarry Smith 
2939273d9f13SBarry Smith    Input Parameters:
2940117016b1SBarry Smith +  B - The matrix-free
2941273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2942273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2943273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2944273d9f13SBarry Smith 
2945273d9f13SBarry Smith    Notes:
294649a6f317SBarry Smith      If nnz is given then nz is ignored
294749a6f317SBarry Smith 
2948273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2949273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2950273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2951273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2952273d9f13SBarry Smith 
2953273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2954273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2955273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2956273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2957273d9f13SBarry Smith 
2958aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2959aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2960aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2961aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2962aa95bbe8SBarry Smith 
2963a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2964a96a251dSBarry Smith    entries or columns indices
2965a96a251dSBarry Smith 
2966273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2967273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2968273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2969273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2970273d9f13SBarry Smith 
2971273d9f13SBarry Smith    Options Database Keys:
2972698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2973698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2974273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2975273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2976273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2977273d9f13SBarry Smith 
2978273d9f13SBarry Smith    Level: intermediate
2979273d9f13SBarry Smith 
2980aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2981273d9f13SBarry Smith 
2982273d9f13SBarry Smith @*/
2983be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2984273d9f13SBarry Smith {
298597f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2986a23d5eceSKris Buschelman 
2987a23d5eceSKris Buschelman   PetscFunctionBegin;
2988a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2989a23d5eceSKris Buschelman   if (f) {
2990a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2991a23d5eceSKris Buschelman   }
2992a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2993a23d5eceSKris Buschelman }
2994a23d5eceSKris Buschelman 
2995a23d5eceSKris Buschelman EXTERN_C_BEGIN
2996a23d5eceSKris Buschelman #undef __FUNCT__
2997a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2998be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2999a23d5eceSKris Buschelman {
3000273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3001a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
30026849ba73SBarry Smith   PetscErrorCode ierr;
300397f1f81fSBarry Smith   PetscInt       i;
3004273d9f13SBarry Smith 
3005273d9f13SBarry Smith   PetscFunctionBegin;
3006d5d45c9bSBarry Smith 
3007a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3008c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3009c461c341SBarry Smith     nz             = 0;
3010c461c341SBarry Smith   }
3011c461c341SBarry Smith 
301226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
301326283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
301426283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
301526283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3016899cda47SBarry Smith 
3017435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3018435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3019b73539f3SBarry Smith   if (nnz) {
3020d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
302129bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
3022d0f46423SBarry 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);
3023b73539f3SBarry Smith     }
3024b73539f3SBarry Smith   }
3025b73539f3SBarry Smith 
3026273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3027273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3028273d9f13SBarry Smith 
3029ab93d7beSBarry Smith   if (!skipallocation) {
30302ee49352SLisandro Dalcin     if (!b->imax) {
3031d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3032d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
30332ee49352SLisandro Dalcin     }
3034273d9f13SBarry Smith     if (!nnz) {
3035435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3036273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
3037d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3038d0f46423SBarry Smith       nz = nz*B->rmap->n;
3039273d9f13SBarry Smith     } else {
3040273d9f13SBarry Smith       nz = 0;
3041d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3042273d9f13SBarry Smith     }
3043ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3044d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3045ab93d7beSBarry Smith 
3046273d9f13SBarry Smith     /* allocate the matrix space */
30472ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3048d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3049d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3050bfeeae90SHong Zhang     b->i[0] = 0;
3051d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
30525da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
30535da197adSKris Buschelman     }
3054273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3055e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3056e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3057c461c341SBarry Smith   } else {
3058e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3059e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3060c461c341SBarry Smith   }
3061273d9f13SBarry Smith 
3062273d9f13SBarry Smith   b->nz                = 0;
3063273d9f13SBarry Smith   b->maxnz             = nz;
3064273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3065273d9f13SBarry Smith   PetscFunctionReturn(0);
3066273d9f13SBarry Smith }
3067a23d5eceSKris Buschelman EXTERN_C_END
3068273d9f13SBarry Smith 
3069a1661176SMatthew Knepley #undef  __FUNCT__
3070a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
307158d36128SBarry Smith /*@
3072a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3073a1661176SMatthew Knepley 
3074a1661176SMatthew Knepley    Input Parameters:
3075a1661176SMatthew Knepley +  B - the matrix
3076a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3077a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3078a1661176SMatthew Knepley -  v - optional values in the matrix
3079a1661176SMatthew Knepley 
3080a1661176SMatthew Knepley    Level: developer
3081a1661176SMatthew Knepley 
308258d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
308358d36128SBarry Smith 
3084a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3085a1661176SMatthew Knepley 
3086a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3087a1661176SMatthew Knepley @*/
3088a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3089a1661176SMatthew Knepley {
3090a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3091a1661176SMatthew Knepley   PetscErrorCode ierr;
3092a1661176SMatthew Knepley 
3093a1661176SMatthew Knepley   PetscFunctionBegin;
3094a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
3095a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3096a1661176SMatthew Knepley   if (f) {
3097a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3098a1661176SMatthew Knepley   }
3099a1661176SMatthew Knepley   PetscFunctionReturn(0);
3100a1661176SMatthew Knepley }
3101a1661176SMatthew Knepley 
3102a1661176SMatthew Knepley EXTERN_C_BEGIN
3103a1661176SMatthew Knepley #undef  __FUNCT__
3104a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3105b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3106a1661176SMatthew Knepley {
3107a1661176SMatthew Knepley   PetscInt       i;
3108a1661176SMatthew Knepley   PetscInt       m,n;
3109a1661176SMatthew Knepley   PetscInt       nz;
3110a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3111a1661176SMatthew Knepley   PetscScalar    *values;
3112a1661176SMatthew Knepley   PetscErrorCode ierr;
3113a1661176SMatthew Knepley 
3114a1661176SMatthew Knepley   PetscFunctionBegin;
3115a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3116a1661176SMatthew Knepley 
3117b7940d39SSatish Balay   if (Ii[0]) {
3118b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3119a1661176SMatthew Knepley   }
3120a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3121a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3122b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3123a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
3124a1661176SMatthew Knepley     if (nz < 0) {
3125a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3126a1661176SMatthew Knepley     }
3127a1661176SMatthew Knepley     nnz[i] = nz;
3128a1661176SMatthew Knepley   }
3129a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3130a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3131a1661176SMatthew Knepley 
3132a1661176SMatthew Knepley   if (v) {
3133a1661176SMatthew Knepley     values = (PetscScalar*) v;
3134a1661176SMatthew Knepley   } else {
31350e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3136a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3137a1661176SMatthew Knepley   }
3138a1661176SMatthew Knepley 
3139a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3140b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3141b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3142a1661176SMatthew Knepley   }
3143a1661176SMatthew Knepley 
3144a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3145a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3146a1661176SMatthew Knepley 
3147a1661176SMatthew Knepley   if (!v) {
3148a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3149a1661176SMatthew Knepley   }
3150a1661176SMatthew Knepley   PetscFunctionReturn(0);
3151a1661176SMatthew Knepley }
3152a1661176SMatthew Knepley EXTERN_C_END
3153a1661176SMatthew Knepley 
31547c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3155be7314b0SBarry Smith #include "private/petscaxpy.h"
3156170fe5c8SBarry Smith 
3157170fe5c8SBarry Smith #undef __FUNCT__
3158170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3159170fe5c8SBarry Smith /*
3160170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3161170fe5c8SBarry Smith 
3162170fe5c8SBarry Smith                n                       p                          p
3163170fe5c8SBarry Smith         (              )       (              )         (                  )
3164170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3165170fe5c8SBarry Smith         (              )       (              )         (                  )
3166170fe5c8SBarry Smith 
3167170fe5c8SBarry Smith */
3168170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3169170fe5c8SBarry Smith {
3170170fe5c8SBarry Smith   PetscErrorCode     ierr;
3171170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3172170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3173170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
31741de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3175170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3176170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3177170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3178170fe5c8SBarry Smith 
3179170fe5c8SBarry Smith   PetscFunctionBegin;
3180d0f46423SBarry Smith   m = A->rmap->n;
3181d0f46423SBarry Smith   n = A->cmap->n;
3182d0f46423SBarry Smith   p = B->cmap->n;
3183170fe5c8SBarry Smith   a = sub_a->v;
3184170fe5c8SBarry Smith   b = sub_b->a;
3185170fe5c8SBarry Smith   c = sub_c->v;
3186170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3187170fe5c8SBarry Smith 
3188170fe5c8SBarry Smith   ii  = sub_b->i;
3189170fe5c8SBarry Smith   idx = sub_b->j;
3190170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3191170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3192170fe5c8SBarry Smith     while (q-->0) {
3193170fe5c8SBarry Smith       c_q = c + m*(*idx);
3194170fe5c8SBarry Smith       a_q = a + m*i;
3195be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3196170fe5c8SBarry Smith       idx++;
3197170fe5c8SBarry Smith       b++;
3198170fe5c8SBarry Smith     }
3199170fe5c8SBarry Smith   }
3200170fe5c8SBarry Smith   PetscFunctionReturn(0);
3201170fe5c8SBarry Smith }
3202170fe5c8SBarry Smith 
3203170fe5c8SBarry Smith #undef __FUNCT__
3204170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3205170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3206170fe5c8SBarry Smith {
3207170fe5c8SBarry Smith   PetscErrorCode ierr;
3208d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3209170fe5c8SBarry Smith   Mat            Cmat;
3210170fe5c8SBarry Smith 
3211170fe5c8SBarry Smith   PetscFunctionBegin;
3212d0f46423SBarry 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);
321339804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3214170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3215170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3216170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3217170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3218170fe5c8SBarry Smith   *C = Cmat;
3219170fe5c8SBarry Smith   PetscFunctionReturn(0);
3220170fe5c8SBarry Smith }
3221170fe5c8SBarry Smith 
3222170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3223170fe5c8SBarry Smith #undef __FUNCT__
3224170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3225170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3226170fe5c8SBarry Smith {
3227170fe5c8SBarry Smith   PetscErrorCode ierr;
3228170fe5c8SBarry Smith 
3229170fe5c8SBarry Smith   PetscFunctionBegin;
3230170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3231170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3232170fe5c8SBarry Smith   }
3233170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3234170fe5c8SBarry Smith   PetscFunctionReturn(0);
3235170fe5c8SBarry Smith }
3236170fe5c8SBarry Smith 
3237170fe5c8SBarry Smith 
32380bad9183SKris Buschelman /*MC
3239fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
32400bad9183SKris Buschelman    based on compressed sparse row format.
32410bad9183SKris Buschelman 
32420bad9183SKris Buschelman    Options Database Keys:
32430bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
32440bad9183SKris Buschelman 
32450bad9183SKris Buschelman   Level: beginner
32460bad9183SKris Buschelman 
3247f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
32480bad9183SKris Buschelman M*/
32490bad9183SKris Buschelman 
3250a6175056SHong Zhang EXTERN_C_BEGIN
3251b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3252b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3253b5e56a35SBarry Smith #endif
325465460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3255af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3256af1023dbSSatish Balay #endif
325717667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3258b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3259db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3260611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
32615c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3262611f576cSBarry Smith #endif
3263611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32645c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3265611f576cSBarry Smith #endif
3266f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3267f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3268f3c0ef26SHong Zhang #endif
3269611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32705c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3271611f576cSBarry Smith #endif
3272eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3273eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3274eb3b5408SSatish Balay #endif
3275719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3276719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3277719d5645SBarry Smith #endif
327817667f90SBarry Smith EXTERN_C_END
327917667f90SBarry Smith 
3280b24902e0SBarry Smith 
328117667f90SBarry Smith EXTERN_C_BEGIN
32824a2ae208SSatish Balay #undef __FUNCT__
32834a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3284be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3285273d9f13SBarry Smith {
3286273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3287dfbe8321SBarry Smith   PetscErrorCode ierr;
328838baddfdSBarry Smith   PetscMPIInt    size;
3289273d9f13SBarry Smith 
3290273d9f13SBarry Smith   PetscFunctionBegin;
32917adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3292273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3293273d9f13SBarry Smith 
329438f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3295b0a32e0cSBarry Smith   B->data             = (void*)b;
3296549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
329790f02eecSBarry Smith   B->mapping          = 0;
3298416022c9SBarry Smith   b->row              = 0;
3299416022c9SBarry Smith   b->col              = 0;
330082bf6240SBarry Smith   b->icol             = 0;
3301b810aeb4SBarry Smith   b->reallocs         = 0;
330236db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3303f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3304416022c9SBarry Smith   b->nonew             = 0;
3305416022c9SBarry Smith   b->diag              = 0;
3306416022c9SBarry Smith   b->solve_work        = 0;
33072a1b7f2aSHong Zhang   B->spptr             = 0;
3308be6bf707SBarry Smith   b->saved_values      = 0;
3309d7f994e1SBarry Smith   b->idiag             = 0;
331071f1c65dSBarry Smith   b->mdiag             = 0;
331171f1c65dSBarry Smith   b->ssor_work         = 0;
331271f1c65dSBarry Smith   b->omega             = 1.0;
331371f1c65dSBarry Smith   b->fshift            = 0.0;
331471f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3315a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3316a30b2313SHong Zhang   b->xtoy              = 0;
3317a30b2313SHong Zhang   b->XtoY              = 0;
331873e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3319d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3320d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3321d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3322d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
332388e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
332417ab2063SBarry Smith 
332535d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3326b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3327ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
3328b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3329b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3330b5e56a35SBarry Smith #endif
333165460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3332ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C",
3333719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3334719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3335719d5645SBarry Smith #endif
3336611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3337ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C",
33385c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
33395c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3340611f576cSBarry Smith #endif
3341f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3342ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
3343f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3344f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3345f3c0ef26SHong Zhang #endif
3346611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3347ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
33485c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
33495c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3350611f576cSBarry Smith #endif
3351611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3352ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
33535c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
33545c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3355611f576cSBarry Smith #endif
3356eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3357ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C",
3358eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3359eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3360eb3b5408SSatish Balay #endif
3361719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3362ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C",
3363719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3364719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3365719d5645SBarry Smith #endif
3366ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3367b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3368b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3369ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3370db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3371db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3372f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3373bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3374bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3375f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3376be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3377bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3378f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3379be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3380bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3381a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3382a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3383a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
338485fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
338585fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
338685fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3387ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3388ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3389ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
339017667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
339117667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
339217667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33935fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33945fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33955fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33961cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33971cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33981cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3399a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3400a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3401a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3402a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3403a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3404a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
340505b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
340605b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
340705b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3408170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3409170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3410170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3411170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3412170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3413170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3414170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3415170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3416170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
34174108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
341817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
34193a40ed3dSBarry Smith   PetscFunctionReturn(0);
342017ab2063SBarry Smith }
3421273d9f13SBarry Smith EXTERN_C_END
342217ab2063SBarry Smith 
34234a2ae208SSatish Balay #undef __FUNCT__
3424b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3425b24902e0SBarry Smith /*
3426b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3427b24902e0SBarry Smith */
3428f77e22a1SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
342917ab2063SBarry Smith {
3430416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
34316849ba73SBarry Smith   PetscErrorCode ierr;
3432d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
343317ab2063SBarry Smith 
34343a40ed3dSBarry Smith   PetscFunctionBegin;
3435273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3436273d9f13SBarry Smith 
3437416022c9SBarry Smith   C->factor           = A->factor;
34386ad4291fSHong Zhang 
3439416022c9SBarry Smith   c->row            = 0;
3440416022c9SBarry Smith   c->col            = 0;
344182bf6240SBarry Smith   c->icol           = 0;
34426ad4291fSHong Zhang   c->reallocs       = 0;
344317ab2063SBarry Smith 
34446ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
344517ab2063SBarry Smith 
344626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
344726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
344826283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
344926283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3450eec197d1SBarry Smith 
345133b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
34529518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
345317ab2063SBarry Smith   for (i=0; i<m; i++) {
3454416022c9SBarry Smith     c->imax[i] = a->imax[i];
3455416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
345617ab2063SBarry Smith   }
345717ab2063SBarry Smith 
345817ab2063SBarry Smith   /* allocate the matrix space */
3459f77e22a1SHong Zhang   if (mallocmatspace){
3460a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
34619518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3462f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
346397f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
346417ab2063SBarry Smith     if (m > 0) {
346597f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3466be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3467bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3468be6bf707SBarry Smith       } else {
3469bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
347017ab2063SBarry Smith       }
347108480c60SBarry Smith     }
3472f77e22a1SHong Zhang   }
347317ab2063SBarry Smith 
34746ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3475416022c9SBarry Smith   c->roworiented       = a->roworiented;
3476416022c9SBarry Smith   c->nonew             = a->nonew;
3477416022c9SBarry Smith   if (a->diag) {
347897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
347952e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
348017ab2063SBarry Smith     for (i=0; i<m; i++) {
3481416022c9SBarry Smith       c->diag[i] = a->diag[i];
348217ab2063SBarry Smith     }
34833a40ed3dSBarry Smith   } else c->diag           = 0;
34846ad4291fSHong Zhang   c->solve_work            = 0;
34856ad4291fSHong Zhang   c->saved_values          = 0;
34866ad4291fSHong Zhang   c->idiag                 = 0;
348771f1c65dSBarry Smith   c->ssor_work             = 0;
3488a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3489e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3490e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34916ad4291fSHong Zhang   c->xtoy                  = 0;
34926ad4291fSHong Zhang   c->XtoY                  = 0;
34936ad4291fSHong Zhang 
3494416022c9SBarry Smith   c->nz                 = a->nz;
3495416022c9SBarry Smith   c->maxnz              = a->maxnz;
3496273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3497754ec7b1SSatish Balay 
34986ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34996ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
35006ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
35016ad4291fSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
35026ad4291fSHong Zhang     i = a->compressedrow.nrows;
35030e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
35046ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
35056ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
350627ea64f8SHong Zhang   } else {
350727ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
350827ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
350927ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
35106ad4291fSHong Zhang   }
351188e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
35124108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
35134846f1f5SKris Buschelman 
35147adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
35153a40ed3dSBarry Smith   PetscFunctionReturn(0);
351617ab2063SBarry Smith }
351717ab2063SBarry Smith 
35184a2ae208SSatish Balay #undef __FUNCT__
3519b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3520b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3521b24902e0SBarry Smith {
3522b24902e0SBarry Smith   PetscErrorCode ierr;
3523b24902e0SBarry Smith 
3524b24902e0SBarry Smith   PetscFunctionBegin;
3525b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
35264b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3527b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3528f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3529b24902e0SBarry Smith   PetscFunctionReturn(0);
3530b24902e0SBarry Smith }
3531b24902e0SBarry Smith 
3532b24902e0SBarry Smith #undef __FUNCT__
35334a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3534a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
353517ab2063SBarry Smith {
3536416022c9SBarry Smith   Mat_SeqAIJ     *a;
3537416022c9SBarry Smith   Mat            B;
35386849ba73SBarry Smith   PetscErrorCode ierr;
35393c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
354038baddfdSBarry Smith   int            fd;
354138baddfdSBarry Smith   PetscMPIInt    size;
3542bcd2baecSBarry Smith   MPI_Comm       comm;
354317ab2063SBarry Smith 
35443a40ed3dSBarry Smith   PetscFunctionBegin;
3545e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3546d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
354729bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3548b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
35490752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3550552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
355117ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
355217ab2063SBarry Smith 
3553d64ed03dSBarry Smith   if (nz < 0) {
355429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3555d64ed03dSBarry Smith   }
3556d64ed03dSBarry Smith 
355717ab2063SBarry Smith   /* read in row lengths */
355897f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
35590752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
356017ab2063SBarry Smith 
35613c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
35623c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
35633c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
35643c601197SSatish Balay 
356517ab2063SBarry Smith   /* create our matrix */
3566f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3567f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3568b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3569ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3570416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
357117ab2063SBarry Smith 
35720752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
357317ab2063SBarry Smith 
357417ab2063SBarry Smith   /* read in nonzero values */
35750752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
357617ab2063SBarry Smith 
357717ab2063SBarry Smith   /* set matrix "i" values */
3578efb16452SHong Zhang   a->i[0] = 0;
357917ab2063SBarry Smith   for (i=1; i<= M; i++) {
3580416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3581416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
358217ab2063SBarry Smith   }
3583606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
358417ab2063SBarry Smith 
35856d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35866d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3587b3a1e11cSKris Buschelman   *A = B;
35883a40ed3dSBarry Smith   PetscFunctionReturn(0);
358917ab2063SBarry Smith }
359017ab2063SBarry Smith 
35914a2ae208SSatish Balay #undef __FUNCT__
3592b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3593dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35947264ac53SSatish Balay {
35957264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3596dfbe8321SBarry Smith   PetscErrorCode ierr;
3597eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3598eeffb40dSHong Zhang   PetscInt k;
3599eeffb40dSHong Zhang #endif
36007264ac53SSatish Balay 
36013a40ed3dSBarry Smith   PetscFunctionBegin;
3602bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3603d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3604ca44d042SBarry Smith     *flg = PETSC_FALSE;
3605ca44d042SBarry Smith     PetscFunctionReturn(0);
3606bcd2baecSBarry Smith   }
36077264ac53SSatish Balay 
36087264ac53SSatish Balay   /* if the a->i are the same */
3609d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3610abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
36117264ac53SSatish Balay 
36127264ac53SSatish Balay   /* if a->j are the same */
361397f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3614abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3615bcd2baecSBarry Smith 
3616bcd2baecSBarry Smith   /* if a->a are the same */
3617eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3618eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3619eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3620eeffb40dSHong Zhang       *flg = PETSC_FALSE;
36213a40ed3dSBarry Smith       PetscFunctionReturn(0);
3622eeffb40dSHong Zhang     }
3623eeffb40dSHong Zhang   }
3624eeffb40dSHong Zhang #else
3625eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3626eeffb40dSHong Zhang #endif
3627eeffb40dSHong Zhang   PetscFunctionReturn(0);
36287264ac53SSatish Balay }
362936db0b34SBarry Smith 
36304a2ae208SSatish Balay #undef __FUNCT__
36314a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
363205869f15SSatish Balay /*@
363336db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
363436db0b34SBarry Smith               provided by the user.
363536db0b34SBarry Smith 
3636c75a6043SHong Zhang       Collective on MPI_Comm
363736db0b34SBarry Smith 
363836db0b34SBarry Smith    Input Parameters:
363936db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
364036db0b34SBarry Smith .   m - number of rows
364136db0b34SBarry Smith .   n - number of columns
364236db0b34SBarry Smith .   i - row indices
364336db0b34SBarry Smith .   j - column indices
364436db0b34SBarry Smith -   a - matrix values
364536db0b34SBarry Smith 
364636db0b34SBarry Smith    Output Parameter:
364736db0b34SBarry Smith .   mat - the matrix
364836db0b34SBarry Smith 
364936db0b34SBarry Smith    Level: intermediate
365036db0b34SBarry Smith 
365136db0b34SBarry Smith    Notes:
36520551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
365336db0b34SBarry Smith     once the matrix is destroyed
365436db0b34SBarry Smith 
365536db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
365636db0b34SBarry Smith 
3657bfeeae90SHong Zhang        The i and j indices are 0 based
365836db0b34SBarry Smith 
3659a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3660a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3661a4552177SSatish Balay     as shown:
3662a4552177SSatish Balay 
3663a4552177SSatish Balay         1 0 0
3664a4552177SSatish Balay         2 0 3
3665a4552177SSatish Balay         4 5 6
3666a4552177SSatish Balay 
3667a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
36689985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3669a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3670a4552177SSatish Balay 
36719985e31cSBarry Smith 
36722fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
367336db0b34SBarry Smith 
367436db0b34SBarry Smith @*/
3675a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
367636db0b34SBarry Smith {
3677dfbe8321SBarry Smith   PetscErrorCode ierr;
3678cbcfb4deSHong Zhang   PetscInt       ii;
367936db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3680cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3681cbcfb4deSHong Zhang   PetscInt       jj;
3682cbcfb4deSHong Zhang #endif
368336db0b34SBarry Smith 
368436db0b34SBarry Smith   PetscFunctionBegin;
3685a96a251dSBarry Smith   if (i[0]) {
3686634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
368736db0b34SBarry Smith   }
3688f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3689f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3690ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3691ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3692ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3693ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3694ab93d7beSBarry Smith 
369536db0b34SBarry Smith   aij->i = i;
369636db0b34SBarry Smith   aij->j = j;
369736db0b34SBarry Smith   aij->a = a;
369836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
369936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3700e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3701e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
370236db0b34SBarry Smith 
370336db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
370436db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
37052515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
370679a5c55eSBarry 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]);
37079985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
37089985e31cSBarry 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);
37099985e31cSBarry 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);
37109985e31cSBarry Smith     }
371136db0b34SBarry Smith #endif
371236db0b34SBarry Smith   }
37132515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
371436db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
371579a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
371679a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
371736db0b34SBarry Smith   }
371836db0b34SBarry Smith #endif
371936db0b34SBarry Smith 
3720b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3721b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
372236db0b34SBarry Smith   PetscFunctionReturn(0);
372336db0b34SBarry Smith }
372436db0b34SBarry Smith 
3725cc8ba8e1SBarry Smith #undef __FUNCT__
3726ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3727dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3728cc8ba8e1SBarry Smith {
3729dfbe8321SBarry Smith   PetscErrorCode ierr;
3730cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
373136db0b34SBarry Smith 
3732cc8ba8e1SBarry Smith   PetscFunctionBegin;
37338ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3734cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3735cc8ba8e1SBarry Smith     a->coloring = coloring;
373612c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
373797f1f81fSBarry Smith     PetscInt             i,*larray;
373812c595b3SBarry Smith     ISColoring      ocoloring;
373908b6dcc0SBarry Smith     ISColoringValue *colors;
374012c595b3SBarry Smith 
374112c595b3SBarry Smith     /* set coloring for diagonal portion */
37420e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3743d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
374412c595b3SBarry Smith       larray[i] = i;
374512c595b3SBarry Smith     }
3746d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
37470e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3748d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
374912c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
375012c595b3SBarry Smith     }
375112c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3752d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
375312c595b3SBarry Smith     a->coloring = ocoloring;
375412c595b3SBarry Smith   }
3755cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3756cc8ba8e1SBarry Smith }
3757cc8ba8e1SBarry Smith 
3758dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3759ee4f033dSBarry Smith EXTERN_C_BEGIN
376029c1e371SBarry Smith #include "adic/ad_utils.h"
3761ee4f033dSBarry Smith EXTERN_C_END
3762cc8ba8e1SBarry Smith 
3763cc8ba8e1SBarry Smith #undef __FUNCT__
3764ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3765dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3766cc8ba8e1SBarry Smith {
3767cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3768d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
37694440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
377008b6dcc0SBarry Smith   ISColoringValue *color;
3771cc8ba8e1SBarry Smith 
3772cc8ba8e1SBarry Smith   PetscFunctionBegin;
3773e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
37744440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3775cc8ba8e1SBarry Smith   color = a->coloring->colors;
3776cc8ba8e1SBarry Smith   /* loop over rows */
3777cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3778cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3779cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3780cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3781cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3782cc8ba8e1SBarry Smith     }
37834440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3784ee4f033dSBarry Smith   }
3785ee4f033dSBarry Smith   PetscFunctionReturn(0);
3786ee4f033dSBarry Smith }
3787ee4f033dSBarry Smith #endif
3788ee4f033dSBarry Smith 
3789ee4f033dSBarry Smith #undef __FUNCT__
3790ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
379197f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3792ee4f033dSBarry Smith {
3793ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3794d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
379554f21887SBarry Smith   MatScalar       *v = a->a;
379654f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
379708b6dcc0SBarry Smith   ISColoringValue *color;
3798ee4f033dSBarry Smith 
3799ee4f033dSBarry Smith   PetscFunctionBegin;
3800e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3801ee4f033dSBarry Smith   color = a->coloring->colors;
3802ee4f033dSBarry Smith   /* loop over rows */
3803ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3804ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3805ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3806ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3807ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3808ee4f033dSBarry Smith     }
3809ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3810cc8ba8e1SBarry Smith   }
3811cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3812cc8ba8e1SBarry Smith }
381336db0b34SBarry Smith 
381481824310SBarry Smith /*
381581824310SBarry Smith     Special version for direct calls from Fortran
381681824310SBarry Smith */
381781824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
381881824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
381981824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
382081824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
382181824310SBarry Smith #endif
382281824310SBarry Smith 
382381824310SBarry Smith /* Change these macros so can be used in void function */
382481824310SBarry Smith #undef CHKERRQ
38257adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
382681824310SBarry Smith #undef SETERRQ2
38277adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
382881824310SBarry Smith 
382981824310SBarry Smith EXTERN_C_BEGIN
383081824310SBarry Smith #undef __FUNCT__
383181824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
38321f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
383381824310SBarry Smith {
383481824310SBarry Smith   Mat            A = *AA;
383581824310SBarry Smith   PetscInt       m = *mm, n = *nn;
383681824310SBarry Smith   InsertMode     is = *isis;
383781824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
383881824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
383981824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
384081824310SBarry Smith   PetscErrorCode ierr;
384181824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
384254f21887SBarry Smith   MatScalar      *ap,value,*aa;
3843edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
384481824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
384581824310SBarry Smith 
384681824310SBarry Smith   PetscFunctionBegin;
3847d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
384881824310SBarry Smith   imax = a->imax;
384981824310SBarry Smith   ai = a->i;
385081824310SBarry Smith   ailen = a->ilen;
385181824310SBarry Smith   aj = a->j;
385281824310SBarry Smith   aa = a->a;
385381824310SBarry Smith 
385481824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
385581824310SBarry Smith     row  = im[k];
385681824310SBarry Smith     if (row < 0) continue;
385781824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3858d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
385981824310SBarry Smith #endif
386081824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
386181824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
386281824310SBarry Smith     low  = 0;
386381824310SBarry Smith     high = nrow;
386481824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
386581824310SBarry Smith       if (in[l] < 0) continue;
386681824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3867d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
386881824310SBarry Smith #endif
386981824310SBarry Smith       col = in[l];
387081824310SBarry Smith       if (roworiented) {
387181824310SBarry Smith         value = v[l + k*n];
387281824310SBarry Smith       } else {
387381824310SBarry Smith         value = v[k + l*m];
387481824310SBarry Smith       }
387581824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
387681824310SBarry Smith 
387781824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
387881824310SBarry Smith       lastcol = col;
387981824310SBarry Smith       while (high-low > 5) {
388081824310SBarry Smith         t = (low+high)/2;
388181824310SBarry Smith         if (rp[t] > col) high = t;
388281824310SBarry Smith         else             low  = t;
388381824310SBarry Smith       }
388481824310SBarry Smith       for (i=low; i<high; i++) {
388581824310SBarry Smith         if (rp[i] > col) break;
388681824310SBarry Smith         if (rp[i] == col) {
388781824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
388881824310SBarry Smith           else                  ap[i] = value;
388981824310SBarry Smith           goto noinsert;
389081824310SBarry Smith         }
389181824310SBarry Smith       }
389281824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
389381824310SBarry Smith       if (nonew == 1) goto noinsert;
38947adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3895d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
389681824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
389781824310SBarry Smith       /* shift up all the later entries in this row */
389881824310SBarry Smith       for (ii=N; ii>=i; ii--) {
389981824310SBarry Smith         rp[ii+1] = rp[ii];
390081824310SBarry Smith         ap[ii+1] = ap[ii];
390181824310SBarry Smith       }
390281824310SBarry Smith       rp[i] = col;
390381824310SBarry Smith       ap[i] = value;
390481824310SBarry Smith       noinsert:;
390581824310SBarry Smith       low = i + 1;
390681824310SBarry Smith     }
390781824310SBarry Smith     ailen[row] = nrow;
390881824310SBarry Smith   }
390981824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
391081824310SBarry Smith   PetscFunctionReturnVoid();
391181824310SBarry Smith }
391281824310SBarry Smith EXTERN_C_END
3913