xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 7408324e65da5a885f55472ebb5e2ecd89f8b08d)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
87c4f633dSBarry Smith 
97c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
107c4f633dSBarry Smith #include "../src/inline/spops.h"
117c4f633dSBarry Smith #include "../src/inline/dot.h"
120a835dfdSSatish Balay #include "petscbt.h"
1317ab2063SBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
1579299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1679299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1779299369SBarry Smith {
1879299369SBarry Smith   PetscErrorCode ierr;
1979299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
20d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
2154f21887SBarry Smith   MatScalar      *aa = aij->a;
2254f21887SBarry Smith   PetscScalar    *v;
2309f38230SBarry Smith   PetscTruth     missing;
2479299369SBarry Smith 
2579299369SBarry Smith   PetscFunctionBegin;
2609f38230SBarry Smith   if (Y->assembled) {
2709f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2809f38230SBarry Smith     if (!missing) {
2979299369SBarry Smith       diag = aij->diag;
3079299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3179299369SBarry Smith       if (is == INSERT_VALUES) {
3279299369SBarry Smith 	for (i=0; i<m; i++) {
3379299369SBarry Smith 	  aa[diag[i]] = v[i];
3479299369SBarry Smith 	}
3579299369SBarry Smith       } else {
3679299369SBarry Smith 	for (i=0; i<m; i++) {
3779299369SBarry Smith 	  aa[diag[i]] += v[i];
3879299369SBarry Smith 	}
3979299369SBarry Smith       }
4079299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4179299369SBarry Smith       PetscFunctionReturn(0);
4279299369SBarry Smith     }
4309f38230SBarry Smith   }
4409f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4509f38230SBarry Smith   PetscFunctionReturn(0);
4609f38230SBarry Smith }
4779299369SBarry Smith 
4879299369SBarry Smith #undef __FUNCT__
494a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
508f7157efSSatish Balay PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5117ab2063SBarry Smith {
52416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
53dfbe8321SBarry Smith   PetscErrorCode ierr;
5497f1f81fSBarry Smith   PetscInt       i,ishift;
5517ab2063SBarry Smith 
563a40ed3dSBarry Smith   PetscFunctionBegin;
57d0f46423SBarry Smith   *m     = A->rmap->n;
583a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
59bfeeae90SHong Zhang   ishift = 0;
6053e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
61d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
62bfeeae90SHong Zhang   } else if (oshift == 1) {
63d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
643b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
65d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
66d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
67ecc77c7aSBarry Smith     if (ja) {
6897f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
693b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
70ecc77c7aSBarry Smith     }
716945ee14SBarry Smith   } else {
72ecc77c7aSBarry Smith     *ia = a->i;
73ecc77c7aSBarry Smith     if (ja) *ja = a->j;
74a2ce50c7SBarry Smith   }
753a40ed3dSBarry Smith   PetscFunctionReturn(0);
76a2744918SBarry Smith }
77a2744918SBarry Smith 
784a2ae208SSatish Balay #undef __FUNCT__
794a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
808f7157efSSatish Balay PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
816945ee14SBarry Smith {
82dfbe8321SBarry Smith   PetscErrorCode ierr;
836945ee14SBarry Smith 
843a40ed3dSBarry Smith   PetscFunctionBegin;
853a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
86bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
87606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
88ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
89bcd2baecSBarry Smith   }
903a40ed3dSBarry Smith   PetscFunctionReturn(0);
9117ab2063SBarry Smith }
9217ab2063SBarry Smith 
934a2ae208SSatish Balay #undef __FUNCT__
944a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
958f7157efSSatish Balay PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
963b2fbd54SBarry Smith {
973b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
98dfbe8321SBarry Smith   PetscErrorCode ierr;
99d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
10097f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1013b2fbd54SBarry Smith 
1023a40ed3dSBarry Smith   PetscFunctionBegin;
103899cda47SBarry Smith   *nn = n;
1043a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1053b2fbd54SBarry Smith   if (symmetric) {
106d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1073b2fbd54SBarry Smith   } else {
10897f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11197f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1123b2fbd54SBarry Smith     jj = a->j;
1133b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
114bfeeae90SHong Zhang       collengths[jj[i]]++;
1153b2fbd54SBarry Smith     }
1163b2fbd54SBarry Smith     cia[0] = oshift;
1173b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1183b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1193b2fbd54SBarry Smith     }
12097f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1213b2fbd54SBarry Smith     jj   = a->j;
122a93ec695SBarry Smith     for (row=0; row<m; row++) {
123a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
124a93ec695SBarry Smith       for (i=0; i<mr; i++) {
125bfeeae90SHong Zhang         col = *jj++;
1263b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1273b2fbd54SBarry Smith       }
1283b2fbd54SBarry Smith     }
129606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1303b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1313b2fbd54SBarry Smith   }
1323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1333b2fbd54SBarry Smith }
1343b2fbd54SBarry Smith 
1354a2ae208SSatish Balay #undef __FUNCT__
1364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1378f7157efSSatish Balay PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1383b2fbd54SBarry Smith {
139dfbe8321SBarry Smith   PetscErrorCode ierr;
140606d414cSSatish Balay 
1413a40ed3dSBarry Smith   PetscFunctionBegin;
1423a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1433b2fbd54SBarry Smith 
144606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
145606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1463b2fbd54SBarry Smith 
1473a40ed3dSBarry Smith   PetscFunctionReturn(0);
1483b2fbd54SBarry Smith }
1493b2fbd54SBarry Smith 
15087d4246cSBarry Smith #undef __FUNCT__
15187d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15287d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15387d4246cSBarry Smith {
15487d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15587d4246cSBarry Smith   PetscInt       *ai = a->i;
15687d4246cSBarry Smith   PetscErrorCode ierr;
15787d4246cSBarry Smith 
15887d4246cSBarry Smith   PetscFunctionBegin;
15987d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
16087d4246cSBarry Smith   PetscFunctionReturn(0);
16187d4246cSBarry Smith }
16287d4246cSBarry Smith 
163227d817aSBarry Smith #define CHUNKSIZE   15
16417ab2063SBarry Smith 
1654a2ae208SSatish Balay #undef __FUNCT__
1664a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16797f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16817ab2063SBarry Smith {
169416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
170e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
17197f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1726849ba73SBarry Smith   PetscErrorCode ierr;
173e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17454f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
175edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
176273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17717ab2063SBarry Smith 
1783a40ed3dSBarry Smith   PetscFunctionBegin;
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 {
20216371a99SBarry 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 {
477b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
47817ab2063SBarry Smith     for (i=0; i<m; i++) {
47977431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
480416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
481aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48236db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
483a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48436db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
485a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4863a40ed3dSBarry Smith         } else {
487a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
48817ab2063SBarry Smith         }
48917ab2063SBarry Smith #else
490a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49117ab2063SBarry Smith #endif
49217ab2063SBarry Smith       }
493b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
49417ab2063SBarry Smith     }
495b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
49617ab2063SBarry Smith   }
497b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4983a40ed3dSBarry Smith   PetscFunctionReturn(0);
499416022c9SBarry Smith }
500416022c9SBarry Smith 
5014a2ae208SSatish Balay #undef __FUNCT__
5024a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
503dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
504416022c9SBarry Smith {
505480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
506416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
507dfbe8321SBarry Smith   PetscErrorCode    ierr;
508d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
50936db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
510b0a32e0cSBarry Smith   PetscViewer       viewer;
511f3ef73ceSBarry Smith   PetscViewerFormat format;
512cddf8d76SBarry Smith 
5133a40ed3dSBarry Smith   PetscFunctionBegin;
514480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
515b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
51619bcc07fSBarry Smith 
517b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
518416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5190513a670SBarry Smith 
520fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5210513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
522b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
523416022c9SBarry Smith     for (i=0; i<m; i++) {
524cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
525bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
526bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
527aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
529cddf8d76SBarry Smith #else
530cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
531cddf8d76SBarry Smith #endif
532b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
533cddf8d76SBarry Smith       }
534cddf8d76SBarry Smith     }
535b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
536cddf8d76SBarry Smith     for (i=0; i<m; i++) {
537cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
538bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
539bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
540cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
541b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
542cddf8d76SBarry Smith       }
543cddf8d76SBarry Smith     }
544b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
545cddf8d76SBarry Smith     for (i=0; i<m; i++) {
546cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
547bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
548bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
549aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
55036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
551cddf8d76SBarry Smith #else
552cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
553cddf8d76SBarry Smith #endif
554b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
555416022c9SBarry Smith       }
556416022c9SBarry Smith     }
5570513a670SBarry Smith   } else {
5580513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5590513a670SBarry Smith     /* first determine max of all nonzero values */
56097f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
561b0a32e0cSBarry Smith     PetscDraw   popup;
56236db0b34SBarry Smith     PetscReal scale;
5630513a670SBarry Smith 
5640513a670SBarry Smith     for (i=0; i<nz; i++) {
5650513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5660513a670SBarry Smith     }
567b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
568b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
569b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5700513a670SBarry Smith     count = 0;
5710513a670SBarry Smith     for (i=0; i<m; i++) {
5720513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
573bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
574bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
57597f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
576b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5770513a670SBarry Smith         count++;
5780513a670SBarry Smith       }
5790513a670SBarry Smith     }
5800513a670SBarry Smith   }
581480ef9eaSBarry Smith   PetscFunctionReturn(0);
582480ef9eaSBarry Smith }
583cddf8d76SBarry Smith 
5844a2ae208SSatish Balay #undef __FUNCT__
5854a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
586dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
587480ef9eaSBarry Smith {
588dfbe8321SBarry Smith   PetscErrorCode ierr;
589b0a32e0cSBarry Smith   PetscDraw      draw;
59036db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
591480ef9eaSBarry Smith   PetscTruth     isnull;
592480ef9eaSBarry Smith 
593480ef9eaSBarry Smith   PetscFunctionBegin;
594b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
595b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
596480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
597480ef9eaSBarry Smith 
598480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
599d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
600480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
601b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
602b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
603480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6043a40ed3dSBarry Smith   PetscFunctionReturn(0);
605416022c9SBarry Smith }
606416022c9SBarry Smith 
6074a2ae208SSatish Balay #undef __FUNCT__
6084a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
609dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
610416022c9SBarry Smith {
611dfbe8321SBarry Smith   PetscErrorCode ierr;
6126805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
613416022c9SBarry Smith 
6143a40ed3dSBarry Smith   PetscFunctionBegin;
61532077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
616fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
617fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
618c45a1595SBarry Smith   if (iascii) {
6193a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6200f5bd95cSBarry Smith   } else if (isbinary) {
6213a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6220f5bd95cSBarry Smith   } else if (isdraw) {
6233a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6245cd90555SBarry Smith   } else {
625958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
62617ab2063SBarry Smith   }
6274846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
6283a40ed3dSBarry Smith   PetscFunctionReturn(0);
62917ab2063SBarry Smith }
63019bcc07fSBarry Smith 
6314a2ae208SSatish Balay #undef __FUNCT__
6324a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
633dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
63417ab2063SBarry Smith {
635416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6366849ba73SBarry Smith   PetscErrorCode ierr;
63797f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
638d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
63954f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6403447b6efSHong Zhang   PetscReal      ratio=0.6;
64117ab2063SBarry Smith 
6423a40ed3dSBarry Smith   PetscFunctionBegin;
6433a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
64417ab2063SBarry Smith 
64543ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
64617ab2063SBarry Smith   for (i=1; i<m; i++) {
647416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
64817ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
64994a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
65017ab2063SBarry Smith     if (fshift) {
651bfeeae90SHong Zhang       ip = aj + ai[i] ;
652bfeeae90SHong Zhang       ap = aa + ai[i] ;
65317ab2063SBarry Smith       N  = ailen[i];
65417ab2063SBarry Smith       for (j=0; j<N; j++) {
65517ab2063SBarry Smith         ip[j-fshift] = ip[j];
65617ab2063SBarry Smith         ap[j-fshift] = ap[j];
65717ab2063SBarry Smith       }
65817ab2063SBarry Smith     }
65917ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
66017ab2063SBarry Smith   }
66117ab2063SBarry Smith   if (m) {
66217ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
66317ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
66417ab2063SBarry Smith   }
66517ab2063SBarry Smith   /* reset ilen and imax for each row */
66617ab2063SBarry Smith   for (i=0; i<m; i++) {
66717ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
66817ab2063SBarry Smith   }
669bfeeae90SHong Zhang   a->nz = ai[m];
67028b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
67128b2fa4aSMatthew Knepley     SETERRQ3(PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
67228b2fa4aSMatthew Knepley   }
67317ab2063SBarry Smith 
67409f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
675d0f46423SBarry 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);
676ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
677ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
678a0e1a203SBarry Smith 
679dd5f02e7SSatish Balay   a->reallocs          = 0;
6804e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
68136db0b34SBarry Smith   a->rmax              = rmax;
6824e220ebcSLois Curfman McInnes 
683cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
684317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
68588e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
68671c2f376SKris Buschelman 
6874846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
68871f1c65dSBarry Smith 
68971f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6903a40ed3dSBarry Smith   PetscFunctionReturn(0);
69117ab2063SBarry Smith }
69217ab2063SBarry Smith 
6934a2ae208SSatish Balay #undef __FUNCT__
69499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
69599cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
69699cafbc1SBarry Smith {
69799cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
69899cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
69954f21887SBarry Smith   MatScalar      *aa = a->a;
70099cafbc1SBarry Smith 
70199cafbc1SBarry Smith   PetscFunctionBegin;
70299cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
70399cafbc1SBarry Smith   PetscFunctionReturn(0);
70499cafbc1SBarry Smith }
70599cafbc1SBarry Smith 
70699cafbc1SBarry Smith #undef __FUNCT__
70799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
70899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
70999cafbc1SBarry Smith {
71099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
71199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
71254f21887SBarry Smith   MatScalar      *aa = a->a;
71399cafbc1SBarry Smith 
71499cafbc1SBarry Smith   PetscFunctionBegin;
71599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
71699cafbc1SBarry Smith   PetscFunctionReturn(0);
71799cafbc1SBarry Smith }
71899cafbc1SBarry Smith 
71999cafbc1SBarry Smith #undef __FUNCT__
7204a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
721dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
72217ab2063SBarry Smith {
723416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
724dfbe8321SBarry Smith   PetscErrorCode ierr;
7253a40ed3dSBarry Smith 
7263a40ed3dSBarry Smith   PetscFunctionBegin;
727d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7283a40ed3dSBarry Smith   PetscFunctionReturn(0);
72917ab2063SBarry Smith }
730416022c9SBarry Smith 
7314a2ae208SSatish Balay #undef __FUNCT__
7324a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
733dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
73417ab2063SBarry Smith {
735416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
736dfbe8321SBarry Smith   PetscErrorCode ierr;
737d5d45c9bSBarry Smith 
7383a40ed3dSBarry Smith   PetscFunctionBegin;
739aa482453SBarry Smith #if defined(PETSC_USE_LOG)
740d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
74117ab2063SBarry Smith #endif
742e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
743c38d4ed2SBarry Smith   if (a->row) {
744c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
745c38d4ed2SBarry Smith   }
746c38d4ed2SBarry Smith   if (a->col) {
747c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
748c38d4ed2SBarry Smith   }
74905b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
75005b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
75171f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
75205b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
75382bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
75405b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
755cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
75605b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
757407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
758d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
759a30b2313SHong Zhang 
7604846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7614846f1f5SKris Buschelman 
762606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
763901853e0SKris Buschelman 
764dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
765901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
766901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
767901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
768901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
769901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
770ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
771901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
772901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
773a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
774901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7753a40ed3dSBarry Smith   PetscFunctionReturn(0);
77617ab2063SBarry Smith }
77717ab2063SBarry Smith 
7784a2ae208SSatish Balay #undef __FUNCT__
7794a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
780dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
78117ab2063SBarry Smith {
7823a40ed3dSBarry Smith   PetscFunctionBegin;
7833a40ed3dSBarry Smith   PetscFunctionReturn(0);
78417ab2063SBarry Smith }
78517ab2063SBarry Smith 
7864a2ae208SSatish Balay #undef __FUNCT__
7874a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7884e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
78917ab2063SBarry Smith {
790416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7914846f1f5SKris Buschelman   PetscErrorCode ierr;
7923a40ed3dSBarry Smith 
7933a40ed3dSBarry Smith   PetscFunctionBegin;
794a65d3064SKris Buschelman   switch (op) {
795a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7964e0d8c25SBarry Smith       a->roworiented       = flg;
797a65d3064SKris Buschelman       break;
798a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
7994e0d8c25SBarry Smith       a->keepzeroedrows    = flg;
800a65d3064SKris Buschelman       break;
801512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
802512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
803a65d3064SKris Buschelman       break;
804a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
8054e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
806a65d3064SKris Buschelman       break;
807a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8084e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
809a65d3064SKris Buschelman       break;
81028b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
81128b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
81228b2fa4aSMatthew Knepley       break;
813a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8144e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8150df259c2SBarry Smith       break;
816d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8174e0d8c25SBarry Smith       a->compressedrow.use = flg;
818d487561eSHong Zhang       break;
8194e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
820a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
821a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
822290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
823a65d3064SKris Buschelman       break;
824a65d3064SKris Buschelman     default:
82571c2f376SKris Buschelman       break;
826a65d3064SKris Buschelman   }
8274e0d8c25SBarry Smith   ierr = MatSetOption_Inode(A,op,flg);CHKERRQ(ierr);
8283a40ed3dSBarry Smith   PetscFunctionReturn(0);
82917ab2063SBarry Smith }
83017ab2063SBarry Smith 
8314a2ae208SSatish Balay #undef __FUNCT__
8324a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
833dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
83417ab2063SBarry Smith {
835416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8366849ba73SBarry Smith   PetscErrorCode ierr;
83797f1f81fSBarry Smith   PetscInt       i,j,n;
83887828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
83917ab2063SBarry Smith 
8403a40ed3dSBarry Smith   PetscFunctionBegin;
8412dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8421ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
84336db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
844d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
845d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
846bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
847bfeeae90SHong Zhang       if (a->j[j] == i) {
848416022c9SBarry Smith         x[i] = a->a[j];
84917ab2063SBarry Smith         break;
85017ab2063SBarry Smith       }
85117ab2063SBarry Smith     }
85217ab2063SBarry Smith   }
8531ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8543a40ed3dSBarry Smith   PetscFunctionReturn(0);
85517ab2063SBarry Smith }
85617ab2063SBarry Smith 
8574a2ae208SSatish Balay #undef __FUNCT__
8584a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
859dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
86017ab2063SBarry Smith {
861416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8625c897100SBarry Smith   PetscScalar       *x,*y;
863dfbe8321SBarry Smith   PetscErrorCode    ierr;
864d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
8655c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
866a77337e4SBarry Smith   MatScalar         *v;
867a77337e4SBarry Smith   PetscScalar       alpha;
8687b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8693447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8704eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8715c897100SBarry Smith #endif
87217ab2063SBarry Smith 
8733a40ed3dSBarry Smith   PetscFunctionBegin;
8742e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8751ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8761ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8775c897100SBarry Smith 
8785c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
879bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8805c897100SBarry Smith #else
8813447b6efSHong Zhang   if (usecprow){
8823447b6efSHong Zhang     m    = cprow.nrows;
8833447b6efSHong Zhang     ii   = cprow.i;
8847b2bb3b9SHong Zhang     ridx = cprow.rindex;
8853447b6efSHong Zhang   } else {
8863447b6efSHong Zhang     ii = a->i;
8873447b6efSHong Zhang   }
88817ab2063SBarry Smith   for (i=0; i<m; i++) {
8893447b6efSHong Zhang     idx   = a->j + ii[i] ;
8903447b6efSHong Zhang     v     = a->a + ii[i] ;
8913447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8923447b6efSHong Zhang     if (usecprow){
8937b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8943447b6efSHong Zhang     } else {
89517ab2063SBarry Smith       alpha = x[i];
8963447b6efSHong Zhang     }
89717ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
89817ab2063SBarry Smith   }
8995c897100SBarry Smith #endif
900efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
9011ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9021ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9033a40ed3dSBarry Smith   PetscFunctionReturn(0);
90417ab2063SBarry Smith }
90517ab2063SBarry Smith 
9064a2ae208SSatish Balay #undef __FUNCT__
9075c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
908dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9095c897100SBarry Smith {
910dfbe8321SBarry Smith   PetscErrorCode ierr;
9115c897100SBarry Smith 
9125c897100SBarry Smith   PetscFunctionBegin;
913170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9145c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9155c897100SBarry Smith   PetscFunctionReturn(0);
9165c897100SBarry Smith }
9175c897100SBarry Smith 
9185c897100SBarry Smith 
9195c897100SBarry Smith #undef __FUNCT__
9204a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
921dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
92217ab2063SBarry Smith {
923416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
924d9fead3dSBarry Smith   PetscScalar       *y;
92554f21887SBarry Smith   const PetscScalar *x;
92654f21887SBarry Smith   const MatScalar   *aa;
927dfbe8321SBarry Smith   PetscErrorCode    ierr;
928d0f46423SBarry Smith   PetscInt          m=A->rmap->n,*aj,*ii;
929a46b3154SVictor Eijkhout   PetscInt          n,i,j,nonzerorow=0,*ridx=PETSC_NULL;
930362ced78SSatish Balay   PetscScalar       sum;
93197952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
932f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
933f2e71c43SSatish Balay   PetscInt         jrow;
934e36a17ebSSatish Balay #endif
93517ab2063SBarry Smith 
936b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
93797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
938fee21e36SBarry Smith #endif
939fee21e36SBarry Smith 
9403a40ed3dSBarry Smith   PetscFunctionBegin;
941d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9421ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
94397952fefSHong Zhang   aj  = a->j;
94497952fefSHong Zhang   aa  = a->a;
945416022c9SBarry Smith   ii  = a->i;
9464eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
94797952fefSHong Zhang     m    = a->compressedrow.nrows;
94897952fefSHong Zhang     ii   = a->compressedrow.i;
94997952fefSHong Zhang     ridx = a->compressedrow.rindex;
95097952fefSHong Zhang     for (i=0; i<m; i++){
95197952fefSHong Zhang       n   = ii[i+1] - ii[i];
95297952fefSHong Zhang       aj  = a->j + ii[i];
95397952fefSHong Zhang       aa  = a->a + ii[i];
95497952fefSHong Zhang       sum = 0.0;
955a46b3154SVictor Eijkhout       nonzerorow += (n>0);
95697952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
95797952fefSHong Zhang       y[*ridx++] = sum;
95897952fefSHong Zhang     }
95997952fefSHong Zhang   } else { /* do not use compressed row format */
960b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
961b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
962b05257ddSBarry Smith #else
96317ab2063SBarry Smith     for (i=0; i<m; i++) {
9649ea0dfa2SSatish Balay       jrow = ii[i];
9659ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
96617ab2063SBarry Smith       sum  = 0.0;
967a46b3154SVictor Eijkhout       nonzerorow += (n>0);
9689ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
96997952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9709ea0dfa2SSatish Balay       }
97117ab2063SBarry Smith       y[i] = sum;
97217ab2063SBarry Smith     }
9738d195f9aSBarry Smith #endif
974b05257ddSBarry Smith   }
975a46b3154SVictor Eijkhout   ierr = PetscLogFlops(2*a->nz - nonzerorow);CHKERRQ(ierr);
976d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9771ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9783a40ed3dSBarry Smith   PetscFunctionReturn(0);
97917ab2063SBarry Smith }
98017ab2063SBarry Smith 
9814a2ae208SSatish Balay #undef __FUNCT__
9824a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
983dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
98417ab2063SBarry Smith {
985416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
98654f21887SBarry Smith   PetscScalar     *x,*y,*z;
98754f21887SBarry Smith   const MatScalar *aa;
988dfbe8321SBarry Smith   PetscErrorCode  ierr;
989d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
990aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
99197952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
992362ced78SSatish Balay   PetscScalar     sum;
99397952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
994e36a17ebSSatish Balay #endif
9959ea0dfa2SSatish Balay 
9963a40ed3dSBarry Smith   PetscFunctionBegin;
9971ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9981ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9992e8a6d31SBarry Smith   if (zz != yy) {
10001ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
10012e8a6d31SBarry Smith   } else {
10022e8a6d31SBarry Smith     z = y;
10032e8a6d31SBarry Smith   }
1004bfeeae90SHong Zhang 
100597952fefSHong Zhang   aj  = a->j;
100697952fefSHong Zhang   aa  = a->a;
1007cddf8d76SBarry Smith   ii  = a->i;
1008aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
100997952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
101002ab625aSSatish Balay #else
10114eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10124eb6d288SHong Zhang     if (zz != yy){
10134eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10144eb6d288SHong Zhang     }
101597952fefSHong Zhang     m    = a->compressedrow.nrows;
101697952fefSHong Zhang     ii   = a->compressedrow.i;
101797952fefSHong Zhang     ridx = a->compressedrow.rindex;
101897952fefSHong Zhang     for (i=0; i<m; i++){
101997952fefSHong Zhang       n  = ii[i+1] - ii[i];
102097952fefSHong Zhang       aj  = a->j + ii[i];
102197952fefSHong Zhang       aa  = a->a + ii[i];
102297952fefSHong Zhang       sum = y[*ridx];
102397952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
102497952fefSHong Zhang       z[*ridx++] = sum;
102597952fefSHong Zhang     }
102697952fefSHong Zhang   } else { /* do not use compressed row format */
102717ab2063SBarry Smith     for (i=0; i<m; i++) {
10289ea0dfa2SSatish Balay       jrow = ii[i];
10299ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
103017ab2063SBarry Smith       sum  = y[i];
10319ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
103297952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10339ea0dfa2SSatish Balay       }
103417ab2063SBarry Smith       z[i] = sum;
103517ab2063SBarry Smith     }
103697952fefSHong Zhang   }
103702ab625aSSatish Balay #endif
1038efee365bSSatish Balay   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
10391ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10401ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10412e8a6d31SBarry Smith   if (zz != yy) {
10421ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10432e8a6d31SBarry Smith   }
10443a40ed3dSBarry Smith   PetscFunctionReturn(0);
104517ab2063SBarry Smith }
104617ab2063SBarry Smith 
104717ab2063SBarry Smith /*
104817ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
104917ab2063SBarry Smith */
10504a2ae208SSatish Balay #undef __FUNCT__
10514a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1052dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
105317ab2063SBarry Smith {
1054416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10556849ba73SBarry Smith   PetscErrorCode ierr;
1056d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
105717ab2063SBarry Smith 
10583a40ed3dSBarry Smith   PetscFunctionBegin;
105909f38230SBarry Smith   if (!a->diag) {
106009f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10619518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
106209f38230SBarry Smith   }
1063d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
106409f38230SBarry Smith     a->diag[i] = a->i[i+1];
1065bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1066bfeeae90SHong Zhang       if (a->j[j] == i) {
106709f38230SBarry Smith         a->diag[i] = j;
106817ab2063SBarry Smith         break;
106917ab2063SBarry Smith       }
107017ab2063SBarry Smith     }
107117ab2063SBarry Smith   }
10723a40ed3dSBarry Smith   PetscFunctionReturn(0);
107317ab2063SBarry Smith }
107417ab2063SBarry Smith 
1075be5855fcSBarry Smith /*
1076be5855fcSBarry Smith      Checks for missing diagonals
1077be5855fcSBarry Smith */
10784a2ae208SSatish Balay #undef __FUNCT__
10794a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
108009f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1081be5855fcSBarry Smith {
1082be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
108397f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1084be5855fcSBarry Smith 
1085be5855fcSBarry Smith   PetscFunctionBegin;
108609f38230SBarry Smith   *missing = PETSC_FALSE;
1087d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
108809f38230SBarry Smith     *missing  = PETSC_TRUE;
108909f38230SBarry Smith     if (d) *d = 0;
109009f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
109109f38230SBarry Smith   } else {
1092f1e2ffcdSBarry Smith     diag = a->diag;
1093d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1094bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
109509f38230SBarry Smith 	*missing = PETSC_TRUE;
109609f38230SBarry Smith 	if (d) *d = i;
109709f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
109809f38230SBarry Smith       }
1099be5855fcSBarry Smith     }
1100be5855fcSBarry Smith   }
1101be5855fcSBarry Smith   PetscFunctionReturn(0);
1102be5855fcSBarry Smith }
1103be5855fcSBarry Smith 
110471f1c65dSBarry Smith EXTERN_C_BEGIN
110571f1c65dSBarry Smith #undef __FUNCT__
110671f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
110771f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
110871f1c65dSBarry Smith {
110971f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
111071f1c65dSBarry Smith   PetscErrorCode ierr;
1111d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
111254f21887SBarry Smith   MatScalar      *v = a->a;
111354f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
111471f1c65dSBarry Smith 
111571f1c65dSBarry Smith   PetscFunctionBegin;
111671f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
111771f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
111871f1c65dSBarry Smith   diag = a->diag;
111971f1c65dSBarry Smith   if (!a->idiag) {
112071f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
112171f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
112271f1c65dSBarry Smith     v        = a->a;
112371f1c65dSBarry Smith   }
112471f1c65dSBarry Smith   mdiag = a->mdiag;
112571f1c65dSBarry Smith   idiag = a->idiag;
112671f1c65dSBarry Smith 
1127028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
112871f1c65dSBarry Smith     for (i=0; i<m; i++) {
112971f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
113071f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
113171f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
113271f1c65dSBarry Smith     }
113371f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
113471f1c65dSBarry Smith   } else {
113571f1c65dSBarry Smith     for (i=0; i<m; i++) {
113671f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
113771f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
113871f1c65dSBarry Smith     }
113971f1c65dSBarry Smith     ierr = PetscLogFlops(2*m);CHKERRQ(ierr);
114071f1c65dSBarry Smith   }
114171f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
114271f1c65dSBarry Smith   PetscFunctionReturn(0);
114371f1c65dSBarry Smith }
11445a9745a3SMatthew Knepley EXTERN_C_END
114571f1c65dSBarry Smith 
11464a2ae208SSatish Balay #undef __FUNCT__
11474a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
114897f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
114917ab2063SBarry Smith {
1150416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1151beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
115254f21887SBarry Smith   const MatScalar    *v = a->a;
115354f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1154dfbe8321SBarry Smith   PetscErrorCode     ierr;
1155d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
115697f1f81fSBarry Smith   const PetscInt     *idx,*diag;
115717ab2063SBarry Smith 
11583a40ed3dSBarry Smith   PetscFunctionBegin;
1159b965ef7fSBarry Smith   its = its*lits;
116091723122SBarry Smith 
116171f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
116271f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
116371f1c65dSBarry Smith   a->fshift = fshift;
116471f1c65dSBarry Smith   a->omega  = omega;
1165ed480e8bSBarry Smith 
116671f1c65dSBarry Smith   diag = a->diag;
116771f1c65dSBarry Smith   t     = a->ssor_work;
1168ed480e8bSBarry Smith   idiag = a->idiag;
116971f1c65dSBarry Smith   mdiag = a->mdiag;
1170ed480e8bSBarry Smith 
11711ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1172fb2e594dSBarry Smith   if (xx != bb) {
11731ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1174fb2e594dSBarry Smith   } else {
1175fb2e594dSBarry Smith     b = x;
1176fb2e594dSBarry Smith   }
117771f1c65dSBarry Smith   CHKMEMQ;
1178ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1179ed480e8bSBarry Smith   xs   = x;
118017ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
118117ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1182ed480e8bSBarry Smith     bs = b;
118317ab2063SBarry Smith     for (i=0; i<m; i++) {
118471f1c65dSBarry Smith         d    = fshift + mdiag[i];
1185416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1186ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1187ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
118817ab2063SBarry Smith         sum  = b[i]*d/omega;
118917ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
119017ab2063SBarry Smith         x[i] = sum;
119117ab2063SBarry Smith     }
11921ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11931ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1194efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11953a40ed3dSBarry Smith     PetscFunctionReturn(0);
119617ab2063SBarry Smith   }
1197c783ea89SBarry Smith 
119848af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
119929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
12003a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
120117ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
120217ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
120317ab2063SBarry Smith 
120417ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
120517ab2063SBarry Smith 
120617ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
120717ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
120817ab2063SBarry Smith     is the relaxation factor.
120917ab2063SBarry Smith     */
121017ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
121117ab2063SBarry Smith 
121217ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
121317ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1214416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1215ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1216ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
121717ab2063SBarry Smith       sum  = b[i];
121817ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1219ed480e8bSBarry Smith       x[i] = sum*idiag[i];
122017ab2063SBarry Smith     }
122117ab2063SBarry Smith 
122217ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1223416022c9SBarry Smith     v = a->a;
1224ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
122517ab2063SBarry Smith 
122617ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1227ed480e8bSBarry Smith     ts = t;
1228416022c9SBarry Smith     diag = a->diag;
122917ab2063SBarry Smith     for (i=0; i<m; i++) {
1230416022c9SBarry Smith       n    = diag[i] - a->i[i];
1231ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1232ed480e8bSBarry Smith       v    = a->a + a->i[i];
123317ab2063SBarry Smith       sum  = t[i];
123417ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1235ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1236733d66baSBarry Smith       /*  x = x + t */
1237733d66baSBarry Smith       x[i] += t[i];
123817ab2063SBarry Smith     }
123917ab2063SBarry Smith 
1240efee365bSSatish Balay     ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr);
12411ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12421ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12433a40ed3dSBarry Smith     PetscFunctionReturn(0);
124417ab2063SBarry Smith   }
124517ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
124617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
124777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
124897f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
124977d8c4bbSBarry Smith #else
125017ab2063SBarry Smith       for (i=0; i<m; i++) {
1251416022c9SBarry Smith         n    = diag[i] - a->i[i];
1252ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1253ed480e8bSBarry Smith         v    = a->a + a->i[i];
125417ab2063SBarry Smith         sum  = b[i];
125517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1256ed480e8bSBarry Smith         x[i] = sum*idiag[i];
125717ab2063SBarry Smith       }
125877d8c4bbSBarry Smith #endif
125917ab2063SBarry Smith       xb = x;
1260efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12613a40ed3dSBarry Smith     } else xb = b;
126217ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
126317ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
126417ab2063SBarry Smith       for (i=0; i<m; i++) {
1265ed480e8bSBarry Smith         x[i] *= mdiag[i];
126617ab2063SBarry Smith       }
1267efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
126817ab2063SBarry Smith     }
126917ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
127077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
127197f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
127277d8c4bbSBarry Smith #else
127317ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1274416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1275ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1276ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
127717ab2063SBarry Smith         sum  = xb[i];
127817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1279ed480e8bSBarry Smith         x[i] = sum*idiag[i];
128017ab2063SBarry Smith       }
128177d8c4bbSBarry Smith #endif
1282efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
128317ab2063SBarry Smith     }
128417ab2063SBarry Smith     its--;
128517ab2063SBarry Smith   }
128617ab2063SBarry Smith   while (its--) {
128717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
128877d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
128997f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
129077d8c4bbSBarry Smith #else
129117ab2063SBarry Smith       for (i=0; i<m; i++) {
1292416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1293ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1294ed480e8bSBarry Smith         v    = a->a + a->i[i];
129517ab2063SBarry Smith         sum  = b[i];
129617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1297ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
129817ab2063SBarry Smith       }
129977d8c4bbSBarry Smith #endif
1300efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
130117ab2063SBarry Smith     }
130217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
130377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
130497f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
130577d8c4bbSBarry Smith #else
130617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1307416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1308ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1309ed480e8bSBarry Smith         v    = a->a + a->i[i];
131017ab2063SBarry Smith         sum  = b[i];
131117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1312ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
131317ab2063SBarry Smith       }
131477d8c4bbSBarry Smith #endif
1315efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
131617ab2063SBarry Smith     }
131717ab2063SBarry Smith   }
13181ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13191ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
132071f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
132117ab2063SBarry Smith }
132217ab2063SBarry Smith 
13232af78befSBarry Smith 
13244a2ae208SSatish Balay #undef __FUNCT__
13254a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1326dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
132717ab2063SBarry Smith {
1328416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13294e220ebcSLois Curfman McInnes 
13303a40ed3dSBarry Smith   PetscFunctionBegin;
13314e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13324e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13334e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13344e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13354e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13364e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13377adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13384e220ebcSLois Curfman McInnes   if (A->factor) {
13394e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13404e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13414e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13424e220ebcSLois Curfman McInnes   } else {
13434e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13444e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13454e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13464e220ebcSLois Curfman McInnes   }
13473a40ed3dSBarry Smith   PetscFunctionReturn(0);
134817ab2063SBarry Smith }
134917ab2063SBarry Smith 
13504a2ae208SSatish Balay #undef __FUNCT__
13514a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1352f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
135317ab2063SBarry Smith {
1354416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1355d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d;
13566849ba73SBarry Smith   PetscErrorCode ierr;
135709f38230SBarry Smith   PetscTruth     missing;
135817ab2063SBarry Smith 
13593a40ed3dSBarry Smith   PetscFunctionBegin;
1360f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1361f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
136277431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1363bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1364f1e2ffcdSBarry Smith     }
1365f4df32b1SMatthew Knepley     if (diag != 0.0) {
136609f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
136709f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1368f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1369f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1370f1e2ffcdSBarry Smith       }
1371f1e2ffcdSBarry Smith     }
137288e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1373f1e2ffcdSBarry Smith   } else {
1374f4df32b1SMatthew Knepley     if (diag != 0.0) {
137517ab2063SBarry Smith       for (i=0; i<N; i++) {
137677431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13777ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1378416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1379f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1380bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13817ae801bdSBarry Smith         } else { /* in case row was completely empty */
1382f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
138317ab2063SBarry Smith         }
138417ab2063SBarry Smith       }
13853a40ed3dSBarry Smith     } else {
138617ab2063SBarry Smith       for (i=0; i<N; i++) {
138777431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1388416022c9SBarry Smith         a->ilen[rows[i]] = 0;
138917ab2063SBarry Smith       }
139017ab2063SBarry Smith     }
139188e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1392f1e2ffcdSBarry Smith   }
139343a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13943a40ed3dSBarry Smith   PetscFunctionReturn(0);
139517ab2063SBarry Smith }
139617ab2063SBarry Smith 
13974a2ae208SSatish Balay #undef __FUNCT__
13984a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1399a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
140017ab2063SBarry Smith {
1401416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
140297f1f81fSBarry Smith   PetscInt   *itmp;
140317ab2063SBarry Smith 
14043a40ed3dSBarry Smith   PetscFunctionBegin;
1405d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
140617ab2063SBarry Smith 
1407416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1408bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
140917ab2063SBarry Smith   if (idx) {
1410bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1411bfeeae90SHong Zhang     if (*nz) {
14124e093b46SBarry Smith       *idx = itmp;
141317ab2063SBarry Smith     }
141417ab2063SBarry Smith     else *idx = 0;
141517ab2063SBarry Smith   }
14163a40ed3dSBarry Smith   PetscFunctionReturn(0);
141717ab2063SBarry Smith }
141817ab2063SBarry Smith 
1419bfeeae90SHong Zhang /* remove this function? */
14204a2ae208SSatish Balay #undef __FUNCT__
14214a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1422a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
142317ab2063SBarry Smith {
14243a40ed3dSBarry Smith   PetscFunctionBegin;
14253a40ed3dSBarry Smith   PetscFunctionReturn(0);
142617ab2063SBarry Smith }
142717ab2063SBarry Smith 
14284a2ae208SSatish Balay #undef __FUNCT__
14294a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1430dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
143117ab2063SBarry Smith {
1432416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
143354f21887SBarry Smith   MatScalar      *v = a->a;
143436db0b34SBarry Smith   PetscReal      sum = 0.0;
14356849ba73SBarry Smith   PetscErrorCode ierr;
143697f1f81fSBarry Smith   PetscInt       i,j;
143717ab2063SBarry Smith 
14383a40ed3dSBarry Smith   PetscFunctionBegin;
143917ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1440416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1441aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
144236db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
144317ab2063SBarry Smith #else
144417ab2063SBarry Smith       sum += (*v)*(*v); v++;
144517ab2063SBarry Smith #endif
144617ab2063SBarry Smith     }
1447064f8208SBarry Smith     *nrm = sqrt(sum);
14483a40ed3dSBarry Smith   } else if (type == NORM_1) {
144936db0b34SBarry Smith     PetscReal *tmp;
145097f1f81fSBarry Smith     PetscInt    *jj = a->j;
1451d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1452d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1453064f8208SBarry Smith     *nrm = 0.0;
1454416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1455bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
145617ab2063SBarry Smith     }
1457d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1458064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
145917ab2063SBarry Smith     }
1460606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14613a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1462064f8208SBarry Smith     *nrm = 0.0;
1463d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1464bfeeae90SHong Zhang       v = a->a + a->i[j];
146517ab2063SBarry Smith       sum = 0.0;
1466416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1467cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
146817ab2063SBarry Smith       }
1469064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
147017ab2063SBarry Smith     }
14713a40ed3dSBarry Smith   } else {
147229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
147317ab2063SBarry Smith   }
14743a40ed3dSBarry Smith   PetscFunctionReturn(0);
147517ab2063SBarry Smith }
147617ab2063SBarry Smith 
14774a2ae208SSatish Balay #undef __FUNCT__
14784a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1479fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
148017ab2063SBarry Smith {
1481416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1482416022c9SBarry Smith   Mat            C;
14836849ba73SBarry Smith   PetscErrorCode ierr;
1484d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
148554f21887SBarry Smith   MatScalar      *array = a->a;
148617ab2063SBarry Smith 
14873a40ed3dSBarry Smith   PetscFunctionBegin;
1488d0f46423SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1489fc4dec0aSBarry Smith 
1490fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1491d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1492d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1493bfeeae90SHong Zhang 
1494bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14957adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1496d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
14977adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1498ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1499606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1500a541d17aSBarry Smith   } else {
1501a541d17aSBarry Smith     C = *B;
1502a541d17aSBarry Smith   }
1503a541d17aSBarry Smith 
150417ab2063SBarry Smith   for (i=0; i<m; i++) {
150517ab2063SBarry Smith     len    = ai[i+1]-ai[i];
150687d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1507b9b97703SBarry Smith     array += len;
1508b9b97703SBarry Smith     aj    += len;
150917ab2063SBarry Smith   }
15106d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15116d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
151217ab2063SBarry Smith 
1513815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1514416022c9SBarry Smith     *B = C;
151517ab2063SBarry Smith   } else {
1516273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
151717ab2063SBarry Smith   }
15183a40ed3dSBarry Smith   PetscFunctionReturn(0);
151917ab2063SBarry Smith }
152017ab2063SBarry Smith 
1521cd0d46ebSvictorle EXTERN_C_BEGIN
1522cd0d46ebSvictorle #undef __FUNCT__
15235fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1524be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1525cd0d46ebSvictorle {
1526cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
152754f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
152854f21887SBarry Smith   MatScalar      *va,*vb;
15296849ba73SBarry Smith   PetscErrorCode ierr;
153097f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1531cd0d46ebSvictorle 
1532cd0d46ebSvictorle   PetscFunctionBegin;
1533cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1534cd0d46ebSvictorle 
1535cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1536cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15375485867bSBarry Smith   if (ma!=nb || na!=mb){
15385485867bSBarry Smith     *f = PETSC_FALSE;
15395485867bSBarry Smith     PetscFunctionReturn(0);
15405485867bSBarry Smith   }
1541cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1542cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1543cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
154497f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
154597f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1546cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1547cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1548cd0d46ebSvictorle 
1549cd0d46ebSvictorle   *f = PETSC_TRUE;
1550cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1551cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
155297f1f81fSBarry Smith       PetscInt         idc,idr;
15535485867bSBarry Smith       PetscScalar vc,vr;
1554cd0d46ebSvictorle       /* column/row index/value */
15555485867bSBarry Smith       idc = adx[aptr[i]];
15565485867bSBarry Smith       idr = bdx[bptr[idc]];
15575485867bSBarry Smith       vc  = va[aptr[i]];
15585485867bSBarry Smith       vr  = vb[bptr[idc]];
15595485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15605485867bSBarry Smith 	*f = PETSC_FALSE;
15615485867bSBarry Smith         goto done;
1562cd0d46ebSvictorle       } else {
15635485867bSBarry Smith 	aptr[i]++;
15645485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1565cd0d46ebSvictorle       }
1566cd0d46ebSvictorle     }
1567cd0d46ebSvictorle   }
1568cd0d46ebSvictorle  done:
1569cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15703aeef889SHong Zhang   if (B) {
15713aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15723aeef889SHong Zhang   }
1573cd0d46ebSvictorle   PetscFunctionReturn(0);
1574cd0d46ebSvictorle }
1575cd0d46ebSvictorle EXTERN_C_END
1576cd0d46ebSvictorle 
15771cbb95d3SBarry Smith EXTERN_C_BEGIN
15781cbb95d3SBarry Smith #undef __FUNCT__
15791cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15801cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15811cbb95d3SBarry Smith {
15821cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
158354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
158454f21887SBarry Smith   MatScalar      *va,*vb;
15851cbb95d3SBarry Smith   PetscErrorCode ierr;
15861cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15871cbb95d3SBarry Smith 
15881cbb95d3SBarry Smith   PetscFunctionBegin;
15891cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15901cbb95d3SBarry Smith 
15911cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15921cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15931cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15941cbb95d3SBarry Smith     *f = PETSC_FALSE;
15951cbb95d3SBarry Smith     PetscFunctionReturn(0);
15961cbb95d3SBarry Smith   }
15971cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15981cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15991cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
16001cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
16011cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
16021cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
16031cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
16041cbb95d3SBarry Smith 
16051cbb95d3SBarry Smith   *f = PETSC_TRUE;
16061cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
16071cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
16081cbb95d3SBarry Smith       PetscInt         idc,idr;
16091cbb95d3SBarry Smith       PetscScalar vc,vr;
16101cbb95d3SBarry Smith       /* column/row index/value */
16111cbb95d3SBarry Smith       idc = adx[aptr[i]];
16121cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16131cbb95d3SBarry Smith       vc  = va[aptr[i]];
16141cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16151cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16161cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16171cbb95d3SBarry Smith         goto done;
16181cbb95d3SBarry Smith       } else {
16191cbb95d3SBarry Smith 	aptr[i]++;
16201cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16211cbb95d3SBarry Smith       }
16221cbb95d3SBarry Smith     }
16231cbb95d3SBarry Smith   }
16241cbb95d3SBarry Smith  done:
16251cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16261cbb95d3SBarry Smith   if (B) {
16271cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16281cbb95d3SBarry Smith   }
16291cbb95d3SBarry Smith   PetscFunctionReturn(0);
16301cbb95d3SBarry Smith }
16311cbb95d3SBarry Smith EXTERN_C_END
16321cbb95d3SBarry Smith 
16339e29f15eSvictorle #undef __FUNCT__
16349e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1635dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16369e29f15eSvictorle {
1637dfbe8321SBarry Smith   PetscErrorCode ierr;
16389e29f15eSvictorle   PetscFunctionBegin;
16395485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16409e29f15eSvictorle   PetscFunctionReturn(0);
16419e29f15eSvictorle }
16429e29f15eSvictorle 
16434a2ae208SSatish Balay #undef __FUNCT__
16441cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16451cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16461cbb95d3SBarry Smith {
16471cbb95d3SBarry Smith   PetscErrorCode ierr;
16481cbb95d3SBarry Smith   PetscFunctionBegin;
16491cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16501cbb95d3SBarry Smith   PetscFunctionReturn(0);
16511cbb95d3SBarry Smith }
16521cbb95d3SBarry Smith 
16531cbb95d3SBarry Smith #undef __FUNCT__
16544a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1655dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
165617ab2063SBarry Smith {
1657416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
165854f21887SBarry Smith   PetscScalar    *l,*r,x;
165954f21887SBarry Smith   MatScalar      *v;
1660dfbe8321SBarry Smith   PetscErrorCode ierr;
1661d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
166217ab2063SBarry Smith 
16633a40ed3dSBarry Smith   PetscFunctionBegin;
166417ab2063SBarry Smith   if (ll) {
16653ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16663ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1667e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1668d0f46423SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16691ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1670416022c9SBarry Smith     v = a->a;
167117ab2063SBarry Smith     for (i=0; i<m; i++) {
167217ab2063SBarry Smith       x = l[i];
1673416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
167417ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
167517ab2063SBarry Smith     }
16761ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1677efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
167817ab2063SBarry Smith   }
167917ab2063SBarry Smith   if (rr) {
1680e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1681d0f46423SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16821ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1683416022c9SBarry Smith     v = a->a; jj = a->j;
168417ab2063SBarry Smith     for (i=0; i<nz; i++) {
1685bfeeae90SHong Zhang       (*v++) *= r[*jj++];
168617ab2063SBarry Smith     }
16871ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1688efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
168917ab2063SBarry Smith   }
16903a40ed3dSBarry Smith   PetscFunctionReturn(0);
169117ab2063SBarry Smith }
169217ab2063SBarry Smith 
16934a2ae208SSatish Balay #undef __FUNCT__
16944a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
169597f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
169617ab2063SBarry Smith {
1697db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16986849ba73SBarry Smith   PetscErrorCode ierr;
1699d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
170097f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
17015d0c19d7SBarry Smith   const PetscInt *irow,*icol;
17025d0c19d7SBarry Smith   PetscInt       nrows,ncols;
170397f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
170454f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1705416022c9SBarry Smith   Mat            C;
170614ca34e6SBarry Smith   PetscTruth     stride,sorted;
170717ab2063SBarry Smith 
17083a40ed3dSBarry Smith   PetscFunctionBegin;
170914ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
171014ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
171114ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
171214ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
171399141d43SSatish Balay 
171417ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1715b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1716b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
171717ab2063SBarry Smith 
1718fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1719fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1720fee21e36SBarry Smith   if (stride && step == 1) {
172102834360SBarry Smith     /* special case of contiguous rows */
172297f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
172331ebf83bSSatish Balay     starts = lens + nrows;
172402834360SBarry Smith     /* loop over new rows determining lens and starting points */
172502834360SBarry Smith     for (i=0; i<nrows; i++) {
1726bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1727a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
172802834360SBarry Smith       for (k=kstart; k<kend; k++) {
1729bfeeae90SHong Zhang         if (aj[k] >= first) {
173002834360SBarry Smith           starts[i] = k;
173102834360SBarry Smith           break;
173202834360SBarry Smith 	}
173302834360SBarry Smith       }
1734a2744918SBarry Smith       sum = 0;
173502834360SBarry Smith       while (k < kend) {
1736bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1737a2744918SBarry Smith         sum++;
173802834360SBarry Smith       }
1739a2744918SBarry Smith       lens[i] = sum;
174002834360SBarry Smith     }
174102834360SBarry Smith     /* create submatrix */
1742cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
174397f1f81fSBarry Smith       PetscInt n_cols,n_rows;
174408480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
174529bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1746d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
174708480c60SBarry Smith       C = *B;
17483a40ed3dSBarry Smith     } else {
17497adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1750f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17517adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1752ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
175308480c60SBarry Smith     }
1754db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1755db02288aSLois Curfman McInnes 
175602834360SBarry Smith     /* loop over rows inserting into submatrix */
1757db02288aSLois Curfman McInnes     a_new    = c->a;
1758db02288aSLois Curfman McInnes     j_new    = c->j;
1759db02288aSLois Curfman McInnes     i_new    = c->i;
1760bfeeae90SHong Zhang 
176102834360SBarry Smith     for (i=0; i<nrows; i++) {
1762a2744918SBarry Smith       ii    = starts[i];
1763a2744918SBarry Smith       lensi = lens[i];
1764a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1765a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
176602834360SBarry Smith       }
176787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1768a2744918SBarry Smith       a_new      += lensi;
1769a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1770a2744918SBarry Smith       c->ilen[i]  = lensi;
177102834360SBarry Smith     }
1772606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17733a40ed3dSBarry Smith   } else {
177402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
177597f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1776bfeeae90SHong Zhang 
177797f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
177897f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
177917ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
178002834360SBarry Smith     /* determine lens of each row */
178102834360SBarry Smith     for (i=0; i<nrows; i++) {
1782bfeeae90SHong Zhang       kstart  = ai[irow[i]];
178302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
178402834360SBarry Smith       lens[i] = 0;
178502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1786bfeeae90SHong Zhang         if (smap[aj[k]]) {
178702834360SBarry Smith           lens[i]++;
178802834360SBarry Smith         }
178902834360SBarry Smith       }
179002834360SBarry Smith     }
179117ab2063SBarry Smith     /* Create and fill new matrix */
1792a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17930f5bd95cSBarry Smith       PetscTruth equal;
17940f5bd95cSBarry Smith 
179599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1796d0f46423SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1797d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17980f5bd95cSBarry Smith       if (!equal) {
179929bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
180099141d43SSatish Balay       }
1801d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
180208480c60SBarry Smith       C = *B;
18033a40ed3dSBarry Smith     } else {
18047adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1805f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
18067adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1807ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
180808480c60SBarry Smith     }
180999141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
181017ab2063SBarry Smith     for (i=0; i<nrows; i++) {
181199141d43SSatish Balay       row    = irow[i];
1812bfeeae90SHong Zhang       kstart = ai[row];
181399141d43SSatish Balay       kend   = kstart + a->ilen[row];
1814bfeeae90SHong Zhang       mat_i  = c->i[i];
181599141d43SSatish Balay       mat_j  = c->j + mat_i;
181699141d43SSatish Balay       mat_a  = c->a + mat_i;
181799141d43SSatish Balay       mat_ilen = c->ilen + i;
181817ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1819bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1820ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
182199141d43SSatish Balay           *mat_a++ = a->a[k];
182299141d43SSatish Balay           (*mat_ilen)++;
182399141d43SSatish Balay 
182417ab2063SBarry Smith         }
182517ab2063SBarry Smith       }
182617ab2063SBarry Smith     }
182702834360SBarry Smith     /* Free work space */
182802834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1829606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1830606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
183102834360SBarry Smith   }
18326d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18336d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
183417ab2063SBarry Smith 
183517ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1836416022c9SBarry Smith   *B = C;
18373a40ed3dSBarry Smith   PetscFunctionReturn(0);
183817ab2063SBarry Smith }
183917ab2063SBarry Smith 
1840a871dcd8SBarry Smith /*
1841a871dcd8SBarry Smith */
18424a2ae208SSatish Balay #undef __FUNCT__
18434a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18440481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1845a871dcd8SBarry Smith {
184663b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1847dfbe8321SBarry Smith   PetscErrorCode ierr;
184863b91edcSBarry Smith   Mat            outA;
1849b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
185063b91edcSBarry Smith 
18513a40ed3dSBarry Smith   PetscFunctionBegin;
1852d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1853b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1854b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1855a871dcd8SBarry Smith 
185663b91edcSBarry Smith   outA          = inA;
18575c9eb25fSBarry Smith   inA->factor   = MAT_FACTOR_LU;
1858c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1859c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row); CHKERRQ(ierr);}
1860c3122656SLisandro Dalcin   a->row = row;
1861c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1862c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col); CHKERRQ(ierr);}
1863c3122656SLisandro Dalcin   a->col = col;
186463b91edcSBarry Smith 
186536db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1866b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18674c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
186852e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1869f0ec6fceSSatish Balay 
187094a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1871d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1872d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
187394a9d846SBarry Smith   }
187463b91edcSBarry Smith 
1875f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1876137fb511SHong Zhang   if (row_identity && col_identity) {
1877719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ(outA,inA,info);CHKERRQ(ierr);
1878137fb511SHong Zhang   } else {
1879719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1880137fb511SHong Zhang   }
18813a40ed3dSBarry Smith   PetscFunctionReturn(0);
1882a871dcd8SBarry Smith }
1883a871dcd8SBarry Smith 
1884d9eff348SSatish Balay #include "petscblaslapack.h"
18854a2ae208SSatish Balay #undef __FUNCT__
18864a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1887f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1888f0b747eeSBarry Smith {
1889f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1890f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1891efee365bSSatish Balay   PetscErrorCode ierr;
18920805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18933a40ed3dSBarry Smith 
18943a40ed3dSBarry Smith   PetscFunctionBegin;
1895f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1896efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18973a40ed3dSBarry Smith   PetscFunctionReturn(0);
1898f0b747eeSBarry Smith }
1899f0b747eeSBarry Smith 
19004a2ae208SSatish Balay #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
190297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1903cddf8d76SBarry Smith {
1904dfbe8321SBarry Smith   PetscErrorCode ierr;
190597f1f81fSBarry Smith   PetscInt       i;
1906cddf8d76SBarry Smith 
19073a40ed3dSBarry Smith   PetscFunctionBegin;
1908cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1909b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1910cddf8d76SBarry Smith   }
1911cddf8d76SBarry Smith 
1912cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19136a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1914cddf8d76SBarry Smith   }
19153a40ed3dSBarry Smith   PetscFunctionReturn(0);
1916cddf8d76SBarry Smith }
1917cddf8d76SBarry Smith 
19184a2ae208SSatish Balay #undef __FUNCT__
19194a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
192097f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19214dcbc457SBarry Smith {
1922e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19236849ba73SBarry Smith   PetscErrorCode ierr;
19245d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19255d0c19d7SBarry Smith   const PetscInt *idx;
192697f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1927f1af5d2fSBarry Smith   PetscBT        table;
1928bbd702dbSSatish Balay 
19293a40ed3dSBarry Smith   PetscFunctionBegin;
1930d0f46423SBarry Smith   m     = A->rmap->n;
1931e4d965acSSatish Balay   ai    = a->i;
1932bfeeae90SHong Zhang   aj    = a->j;
19338a047759SSatish Balay 
1934a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
193506763907SSatish Balay 
193697f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19376831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
193806763907SSatish Balay 
1939e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1940b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1941e4d965acSSatish Balay     isz  = 0;
19426831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1943e4d965acSSatish Balay 
1944e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19454dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1946b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1947e4d965acSSatish Balay 
1948dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1949e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1950f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19514dcbc457SBarry Smith     }
195206763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
195306763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1954e4d965acSSatish Balay 
195504a348a9SBarry Smith     k = 0;
195604a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
195704a348a9SBarry Smith       n = isz;
195806763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1959e4d965acSSatish Balay         row   = nidx[k];
1960e4d965acSSatish Balay         start = ai[row];
1961e4d965acSSatish Balay         end   = ai[row+1];
196204a348a9SBarry Smith         for (l = start; l<end ; l++){
1963efb16452SHong Zhang           val = aj[l] ;
1964f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1965e4d965acSSatish Balay         }
1966e4d965acSSatish Balay       }
1967e4d965acSSatish Balay     }
1968029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1969e4d965acSSatish Balay   }
19706831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1971606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19723a40ed3dSBarry Smith   PetscFunctionReturn(0);
19734dcbc457SBarry Smith }
197417ab2063SBarry Smith 
19750513a670SBarry Smith /* -------------------------------------------------------------- */
19764a2ae208SSatish Balay #undef __FUNCT__
19774a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1978dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19790513a670SBarry Smith {
19800513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19816849ba73SBarry Smith   PetscErrorCode ierr;
19825d0c19d7SBarry Smith   PetscInt       i,nz,m = A->rmap->n,n = A->cmap->n;
19835d0c19d7SBarry Smith   const PetscInt *row,*col;
19845d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
198556cd22aeSBarry Smith   IS             icolp,irowp;
198697f1f81fSBarry Smith   PetscInt       *cwork;
198732ec9ce4SBarry Smith   PetscScalar    *vwork;
19880513a670SBarry Smith 
19893a40ed3dSBarry Smith   PetscFunctionBegin;
19904c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
199156cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19924c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
199356cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19940513a670SBarry Smith 
19950513a670SBarry Smith   /* determine lengths of permuted rows */
199697f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19970513a670SBarry Smith   for (i=0; i<m; i++) {
19980513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19990513a670SBarry Smith   }
20007adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2001f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
20027adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2003ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2004606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
20050513a670SBarry Smith 
200697f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
20070513a670SBarry Smith   for (i=0; i<m; i++) {
200832ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20090513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2010cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
201132ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20120513a670SBarry Smith   }
2013606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20143c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20150513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20160513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
201756cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
201856cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
201956cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
202056cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20213a40ed3dSBarry Smith   PetscFunctionReturn(0);
20220513a670SBarry Smith }
20230513a670SBarry Smith 
20244a2ae208SSatish Balay #undef __FUNCT__
20254a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2026dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2027cb5b572fSBarry Smith {
2028dfbe8321SBarry Smith   PetscErrorCode ierr;
2029cb5b572fSBarry Smith 
2030cb5b572fSBarry Smith   PetscFunctionBegin;
203133f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
203233f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2033be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2034be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2035be6bf707SBarry Smith 
2036d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2037634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2038cb5b572fSBarry Smith     }
2039d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2040cb5b572fSBarry Smith   } else {
2041cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2042cb5b572fSBarry Smith   }
2043cb5b572fSBarry Smith   PetscFunctionReturn(0);
2044cb5b572fSBarry Smith }
2045cb5b572fSBarry Smith 
20464a2ae208SSatish Balay #undef __FUNCT__
20474a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2048dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2049273d9f13SBarry Smith {
2050dfbe8321SBarry Smith   PetscErrorCode ierr;
2051273d9f13SBarry Smith 
2052273d9f13SBarry Smith   PetscFunctionBegin;
2053ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2054273d9f13SBarry Smith   PetscFunctionReturn(0);
2055273d9f13SBarry Smith }
2056273d9f13SBarry Smith 
20574a2ae208SSatish Balay #undef __FUNCT__
20584a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2059a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20606c0721eeSBarry Smith {
20616c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20626c0721eeSBarry Smith   PetscFunctionBegin;
20636c0721eeSBarry Smith   *array = a->a;
20646c0721eeSBarry Smith   PetscFunctionReturn(0);
20656c0721eeSBarry Smith }
20666c0721eeSBarry Smith 
20674a2ae208SSatish Balay #undef __FUNCT__
20684a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2069dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20706c0721eeSBarry Smith {
20716c0721eeSBarry Smith   PetscFunctionBegin;
20726c0721eeSBarry Smith   PetscFunctionReturn(0);
20736c0721eeSBarry Smith }
2074273d9f13SBarry Smith 
2075ee4f033dSBarry Smith #undef __FUNCT__
2076ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2077dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2078ee4f033dSBarry Smith {
20796849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20806849ba73SBarry Smith   PetscErrorCode ierr;
208197f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2082efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
208387828ca2SBarry Smith   PetscScalar    *vscale_array;
2084ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2085ee4f033dSBarry Smith   Vec            w1,w2,w3;
2086ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2087ee4f033dSBarry Smith   PetscTruth     flg;
2088ee4f033dSBarry Smith 
2089ee4f033dSBarry Smith   PetscFunctionBegin;
2090ee4f033dSBarry Smith   if (!coloring->w1) {
2091ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
209252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2093ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
209452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2095ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
209652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2097ee4f033dSBarry Smith   }
2098ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2099ee4f033dSBarry Smith 
2100ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
21017adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
2102ee4f033dSBarry Smith   if (flg) {
2103ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2104ee4f033dSBarry Smith   } else {
21050b9b6f31SBarry Smith     PetscTruth assembled;
21060b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
21070b9b6f31SBarry Smith     if (assembled) {
2108ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2109ee4f033dSBarry Smith     }
21100b9b6f31SBarry Smith   }
2111ee4f033dSBarry Smith 
2112ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2113ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2114ee4f033dSBarry Smith 
2115ee4f033dSBarry Smith   /*
2116ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2117ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2118ee4f033dSBarry Smith   */
2119ee4f033dSBarry Smith   if (coloring->F) {
2120ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2121ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2122ee4f033dSBarry Smith     if (m1 != m2) {
2123ee4f033dSBarry Smith       coloring->F = 0;
2124ee4f033dSBarry Smith     }
2125ee4f033dSBarry Smith   }
2126ee4f033dSBarry Smith 
2127ee4f033dSBarry Smith   if (coloring->F) {
2128ee4f033dSBarry Smith     w1          = coloring->F;
2129ee4f033dSBarry Smith     coloring->F = 0;
2130ee4f033dSBarry Smith   } else {
213166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2132ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
213366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2134ee4f033dSBarry Smith   }
2135ee4f033dSBarry Smith 
2136ee4f033dSBarry Smith   /*
2137ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2138ee4f033dSBarry Smith   */
21391ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21401ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2141ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2142ee4f033dSBarry Smith     /*
2143ee4f033dSBarry Smith        Loop over each column associated with color adding the
2144ee4f033dSBarry Smith        perturbation to the vector w3.
2145ee4f033dSBarry Smith     */
2146ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2147ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2148ee4f033dSBarry Smith       dx  = xx[col];
2149ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2150ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2151ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2152ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2153ee4f033dSBarry Smith #else
2154ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2155ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2156ee4f033dSBarry Smith #endif
2157ee4f033dSBarry Smith       dx                *= epsilon;
2158ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2159ee4f033dSBarry Smith     }
2160ee4f033dSBarry Smith   }
21611ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2162ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2163ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2164ee4f033dSBarry Smith 
2165ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2166ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2167ee4f033dSBarry Smith 
2168ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2169ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2170ee4f033dSBarry Smith 
21711ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2172ee4f033dSBarry Smith   /*
2173ee4f033dSBarry Smith       Loop over each color
2174ee4f033dSBarry Smith   */
2175ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
217649b058dcSBarry Smith     coloring->currentcolor = k;
2177ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21781ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2179ee4f033dSBarry Smith     /*
2180ee4f033dSBarry Smith        Loop over each column associated with color adding the
2181ee4f033dSBarry Smith        perturbation to the vector w3.
2182ee4f033dSBarry Smith     */
2183ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2184ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2185ee4f033dSBarry Smith       dx  = xx[col];
21865b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2187ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2188ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2189ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2190ee4f033dSBarry Smith #else
2191ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2192ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2193ee4f033dSBarry Smith #endif
2194ee4f033dSBarry Smith       dx            *= epsilon;
2195634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2196ee4f033dSBarry Smith       w3_array[col] += dx;
2197ee4f033dSBarry Smith     }
21981ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2199ee4f033dSBarry Smith 
2200ee4f033dSBarry Smith     /*
2201ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2202ee4f033dSBarry Smith     */
2203ee4f033dSBarry Smith 
220466f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2205ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
220666f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2207efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2208ee4f033dSBarry Smith 
2209ee4f033dSBarry Smith     /*
2210ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2211ee4f033dSBarry Smith     */
22121ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2213ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2214ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2215ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2216ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2217ee4f033dSBarry Smith       srow   = row + start;
2218ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2219ee4f033dSBarry Smith     }
22201ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2221ee4f033dSBarry Smith   }
222249b058dcSBarry Smith   coloring->currentcolor = k;
22231ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22241ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2225ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2226ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2227ee4f033dSBarry Smith   PetscFunctionReturn(0);
2228ee4f033dSBarry Smith }
2229ee4f033dSBarry Smith 
2230ac90fabeSBarry Smith #include "petscblaslapack.h"
2231ac90fabeSBarry Smith #undef __FUNCT__
2232ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2233f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2234ac90fabeSBarry Smith {
2235dfbe8321SBarry Smith   PetscErrorCode ierr;
223697f1f81fSBarry Smith   PetscInt       i;
2237ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22380805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2239ac90fabeSBarry Smith 
2240ac90fabeSBarry Smith   PetscFunctionBegin;
2241ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2242f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2243f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2244c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2245a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2246a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2247a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2248a30b2313SHong Zhang     }
2249a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2250d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2251a30b2313SHong Zhang       y->XtoY = X;
2252407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2253c537a176SHong Zhang     }
2254f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22551e2582c4SBarry 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);
2256ac90fabeSBarry Smith   } else {
2257f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2258ac90fabeSBarry Smith   }
2259ac90fabeSBarry Smith   PetscFunctionReturn(0);
2260ac90fabeSBarry Smith }
2261ac90fabeSBarry Smith 
2262521d7252SBarry Smith #undef __FUNCT__
2263521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2264521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2265521d7252SBarry Smith {
2266521d7252SBarry Smith   PetscFunctionBegin;
2267521d7252SBarry Smith   PetscFunctionReturn(0);
2268521d7252SBarry Smith }
2269521d7252SBarry Smith 
2270354c94deSBarry Smith #undef __FUNCT__
2271354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2272354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2273354c94deSBarry Smith {
2274354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2275354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2276354c94deSBarry Smith   PetscInt    i,nz;
2277354c94deSBarry Smith   PetscScalar *a;
2278354c94deSBarry Smith 
2279354c94deSBarry Smith   PetscFunctionBegin;
2280354c94deSBarry Smith   nz = aij->nz;
2281354c94deSBarry Smith   a  = aij->a;
2282354c94deSBarry Smith   for (i=0; i<nz; i++) {
2283354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2284354c94deSBarry Smith   }
2285354c94deSBarry Smith #else
2286354c94deSBarry Smith   PetscFunctionBegin;
2287354c94deSBarry Smith #endif
2288354c94deSBarry Smith   PetscFunctionReturn(0);
2289354c94deSBarry Smith }
2290354c94deSBarry Smith 
2291e34fafa9SBarry Smith #undef __FUNCT__
2292985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2293985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2294e34fafa9SBarry Smith {
2295e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2296e34fafa9SBarry Smith   PetscErrorCode ierr;
2297d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2298e34fafa9SBarry Smith   PetscReal      atmp;
2299985db425SBarry Smith   PetscScalar    *x;
2300e34fafa9SBarry Smith   MatScalar      *aa;
2301e34fafa9SBarry Smith 
2302e34fafa9SBarry Smith   PetscFunctionBegin;
2303e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2304e34fafa9SBarry Smith   aa   = a->a;
2305e34fafa9SBarry Smith   ai   = a->i;
2306e34fafa9SBarry Smith   aj   = a->j;
2307e34fafa9SBarry Smith 
2308985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2309e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2310e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2311d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2312e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2313e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23149189402eSHong Zhang     x[i] = 0.0;
2315e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2316985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2317985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2318985db425SBarry Smith       aa++; aj++;
2319985db425SBarry Smith     }
2320985db425SBarry Smith   }
2321985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2322985db425SBarry Smith   PetscFunctionReturn(0);
2323985db425SBarry Smith }
2324985db425SBarry Smith 
2325985db425SBarry Smith #undef __FUNCT__
2326985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2327985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2328985db425SBarry Smith {
2329985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2330985db425SBarry Smith   PetscErrorCode ierr;
2331d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2332985db425SBarry Smith   PetscScalar    *x;
2333985db425SBarry Smith   MatScalar      *aa;
2334985db425SBarry Smith 
2335985db425SBarry Smith   PetscFunctionBegin;
2336985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2337985db425SBarry Smith   aa   = a->a;
2338985db425SBarry Smith   ai   = a->i;
2339985db425SBarry Smith   aj   = a->j;
2340985db425SBarry Smith 
2341985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2342985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2343985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2344d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2345985db425SBarry Smith   for (i=0; i<m; i++) {
2346985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2347d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2348985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2349985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2350985db425SBarry Smith       x[i] = 0.0;
2351985db425SBarry Smith       if (idx) {
2352985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2353985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2354985db425SBarry Smith           if (aj[j] > j) {
2355985db425SBarry Smith             idx[i] = j;
2356985db425SBarry Smith             break;
2357985db425SBarry Smith           }
2358985db425SBarry Smith         }
2359985db425SBarry Smith       }
2360985db425SBarry Smith     }
2361985db425SBarry Smith     for (j=0; j<ncols; j++){
2362985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2363985db425SBarry Smith       aa++; aj++;
2364985db425SBarry Smith     }
2365985db425SBarry Smith   }
2366985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2367985db425SBarry Smith   PetscFunctionReturn(0);
2368985db425SBarry Smith }
2369985db425SBarry Smith 
2370985db425SBarry Smith #undef __FUNCT__
2371c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2372c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2373c87e5d42SMatthew Knepley {
2374c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2375c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2376c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2377c87e5d42SMatthew Knepley   PetscReal      atmp;
2378c87e5d42SMatthew Knepley   PetscScalar    *x;
2379c87e5d42SMatthew Knepley   MatScalar      *aa;
2380c87e5d42SMatthew Knepley 
2381c87e5d42SMatthew Knepley   PetscFunctionBegin;
2382c87e5d42SMatthew Knepley   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2383c87e5d42SMatthew Knepley   aa   = a->a;
2384c87e5d42SMatthew Knepley   ai   = a->i;
2385c87e5d42SMatthew Knepley   aj   = a->j;
2386c87e5d42SMatthew Knepley 
2387c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2388c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2389c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2390c87e5d42SMatthew Knepley   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2391c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2392c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2393289a08f5SMatthew Knepley     if (ncols) {
2394289a08f5SMatthew Knepley       /* Get first nonzero */
2395289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2396289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2397289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2398289a08f5SMatthew Knepley       }
2399289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2400289a08f5SMatthew Knepley     } else {
2401289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2402289a08f5SMatthew Knepley     }
2403c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2404c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2405289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2406c87e5d42SMatthew Knepley       aa++; aj++;
2407c87e5d42SMatthew Knepley     }
2408c87e5d42SMatthew Knepley   }
2409c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2410c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2411c87e5d42SMatthew Knepley }
2412c87e5d42SMatthew Knepley 
2413c87e5d42SMatthew Knepley #undef __FUNCT__
2414985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2415985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2416985db425SBarry Smith {
2417985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2418985db425SBarry Smith   PetscErrorCode ierr;
2419d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2420985db425SBarry Smith   PetscScalar    *x;
2421985db425SBarry Smith   MatScalar      *aa;
2422985db425SBarry Smith 
2423985db425SBarry Smith   PetscFunctionBegin;
2424985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2425985db425SBarry Smith   aa   = a->a;
2426985db425SBarry Smith   ai   = a->i;
2427985db425SBarry Smith   aj   = a->j;
2428985db425SBarry Smith 
2429985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2430985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2431985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2432d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2433985db425SBarry Smith   for (i=0; i<m; i++) {
2434985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2435d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2436985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2437985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2438985db425SBarry Smith       x[i] = 0.0;
2439985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2440985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2441985db425SBarry Smith         for (j=0;j<ncols;j++) {
2442985db425SBarry Smith           if (aj[j] > j) {
2443985db425SBarry Smith             idx[i] = j;
2444985db425SBarry Smith             break;
2445985db425SBarry Smith           }
2446985db425SBarry Smith         }
2447985db425SBarry Smith       }
2448985db425SBarry Smith     }
2449985db425SBarry Smith     for (j=0; j<ncols; j++){
2450985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2451985db425SBarry Smith       aa++; aj++;
2452e34fafa9SBarry Smith     }
2453e34fafa9SBarry Smith   }
2454e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2455e34fafa9SBarry Smith   PetscFunctionReturn(0);
2456e34fafa9SBarry Smith }
2457e34fafa9SBarry Smith 
2458682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24590a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2460cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2461cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2462cb5b572fSBarry Smith        MatMult_SeqAIJ,
246397304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
24647c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
24657c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2466db4efbfdSBarry Smith        0,
2467db4efbfdSBarry Smith        0,
2468db4efbfdSBarry Smith        0,
2469db4efbfdSBarry Smith /*10*/ 0,
2470cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2471cb5b572fSBarry Smith        0,
247217ab2063SBarry Smith        MatRelax_SeqAIJ,
247317ab2063SBarry Smith        MatTranspose_SeqAIJ,
247497304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2475cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2476cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2477cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2478cb5b572fSBarry Smith        MatNorm_SeqAIJ,
247997304618SKris Buschelman /*20*/ 0,
2480cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
248117ab2063SBarry Smith        MatCompress_SeqAIJ,
2482cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2483cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
248497304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2485db4efbfdSBarry Smith        0,
2486db4efbfdSBarry Smith        0,
2487db4efbfdSBarry Smith        0,
2488db4efbfdSBarry Smith        0,
248997304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2490db4efbfdSBarry Smith        0,
2491db4efbfdSBarry Smith        0,
24926c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24936c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
249497304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2495cb5b572fSBarry Smith        0,
2496cb5b572fSBarry Smith        0,
2497cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2498cb5b572fSBarry Smith        0,
249997304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2500cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2501cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2502cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2503cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2504985db425SBarry Smith /*45*/ MatGetRowMax_SeqAIJ,
2505cb5b572fSBarry Smith        MatScale_SeqAIJ,
2506cb5b572fSBarry Smith        0,
250779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
25086945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2509521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
25103b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
25113b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
25123b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2513a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
251497304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2515b9617806SBarry Smith        0,
25160513a670SBarry Smith        0,
2517cda55fadSBarry Smith        MatPermute_SeqAIJ,
2518cda55fadSBarry Smith        0,
251997304618SKris Buschelman /*60*/ 0,
2520b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2521b9b97703SBarry Smith        MatView_SeqAIJ,
2522357abbc8SBarry Smith        0,
2523ee4f033dSBarry Smith        0,
252497304618SKris Buschelman /*65*/ 0,
2525ee4f033dSBarry Smith        0,
2526ee4f033dSBarry Smith        0,
2527ee4f033dSBarry Smith        0,
2528ee4f033dSBarry Smith        0,
2529985db425SBarry Smith /*70*/ MatGetRowMaxAbs_SeqAIJ,
2530c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2531ee4f033dSBarry Smith        0,
2532ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2533dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2534ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2535dcf5cc72SBarry Smith #else
2536dcf5cc72SBarry Smith        0,
2537dcf5cc72SBarry Smith #endif
2538c87e5d42SMatthew Knepley /*75*/ MatSetValuesAdifor_SeqAIJ,
253997304618SKris Buschelman        0,
254097304618SKris Buschelman        0,
254197304618SKris Buschelman        0,
254297304618SKris Buschelman        0,
254397304618SKris Buschelman /*80*/ 0,
254497304618SKris Buschelman        0,
254597304618SKris Buschelman        0,
254697304618SKris Buschelman        0,
2547bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2548bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
25491cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25506284ec50SHong Zhang        0,
25516284ec50SHong Zhang        0,
2552bc011b1eSHong Zhang        0,
2553bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
255426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
255526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2556d439da42SKris Buschelman        MatPtAP_Basic,
25577ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
25587ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2559bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2560bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2561bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25627ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
25637ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2564609c6c4dSKris Buschelman        0,
2565609c6c4dSKris Buschelman        0,
256687d4246cSBarry Smith        MatConjugate_SeqAIJ,
256787d4246cSBarry Smith        0,
256899cafbc1SBarry Smith /*105*/MatSetValuesRow_SeqAIJ,
256999cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2570f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2571f5edf698SHong Zhang        0,
25722bebee5dSHong Zhang        0,
2573de26497bSBarry Smith /*110*/0,
2574985db425SBarry Smith        0,
25752af78befSBarry Smith        MatGetRowMin_SeqAIJ,
25762af78befSBarry Smith        0,
25772af78befSBarry Smith        MatMissingDiagonal_SeqAIJ
25789e29f15eSvictorle };
257917ab2063SBarry Smith 
2580fb2e594dSBarry Smith EXTERN_C_BEGIN
25814a2ae208SSatish Balay #undef __FUNCT__
25824a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2583be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2584bef8e0ddSBarry Smith {
2585bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
258697f1f81fSBarry Smith   PetscInt   i,nz,n;
2587bef8e0ddSBarry Smith 
2588bef8e0ddSBarry Smith   PetscFunctionBegin;
2589bef8e0ddSBarry Smith 
2590bef8e0ddSBarry Smith   nz = aij->maxnz;
2591d0f46423SBarry Smith   n  = mat->rmap->n;
2592bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2593bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2594bef8e0ddSBarry Smith   }
2595bef8e0ddSBarry Smith   aij->nz = nz;
2596bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2597bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2598bef8e0ddSBarry Smith   }
2599bef8e0ddSBarry Smith 
2600bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2601bef8e0ddSBarry Smith }
2602fb2e594dSBarry Smith EXTERN_C_END
2603bef8e0ddSBarry Smith 
26044a2ae208SSatish Balay #undef __FUNCT__
26054a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2606bef8e0ddSBarry Smith /*@
2607bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2608bef8e0ddSBarry Smith        in the matrix.
2609bef8e0ddSBarry Smith 
2610bef8e0ddSBarry Smith   Input Parameters:
2611bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2612bef8e0ddSBarry Smith -  indices - the column indices
2613bef8e0ddSBarry Smith 
261415091d37SBarry Smith   Level: advanced
261515091d37SBarry Smith 
2616bef8e0ddSBarry Smith   Notes:
2617bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2618bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2619bef8e0ddSBarry Smith   of the MatSetValues() operation.
2620bef8e0ddSBarry Smith 
2621bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2622d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2623bef8e0ddSBarry Smith 
2624bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2625bef8e0ddSBarry Smith 
2626b9617806SBarry Smith     The indices should start with zero, not one.
2627b9617806SBarry Smith 
2628bef8e0ddSBarry Smith @*/
2629be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2630bef8e0ddSBarry Smith {
263197f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2632bef8e0ddSBarry Smith 
2633bef8e0ddSBarry Smith   PetscFunctionBegin;
26344482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
26354482741eSBarry Smith   PetscValidPointer(indices,2);
2636c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2637bef8e0ddSBarry Smith   if (f) {
2638bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2639bef8e0ddSBarry Smith   } else {
2640634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2641bef8e0ddSBarry Smith   }
2642bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2643bef8e0ddSBarry Smith }
2644bef8e0ddSBarry Smith 
2645be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2646be6bf707SBarry Smith 
2647fb2e594dSBarry Smith EXTERN_C_BEGIN
26484a2ae208SSatish Balay #undef __FUNCT__
26494a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2650be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2651be6bf707SBarry Smith {
2652be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26536849ba73SBarry Smith   PetscErrorCode ierr;
2654d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2655be6bf707SBarry Smith 
2656be6bf707SBarry Smith   PetscFunctionBegin;
2657be6bf707SBarry Smith   if (aij->nonew != 1) {
2658512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2659be6bf707SBarry Smith   }
2660be6bf707SBarry Smith 
2661be6bf707SBarry Smith   /* allocate space for values if not already there */
2662be6bf707SBarry Smith   if (!aij->saved_values) {
266387828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
26649518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2665be6bf707SBarry Smith   }
2666be6bf707SBarry Smith 
2667be6bf707SBarry Smith   /* copy values over */
266887828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2669be6bf707SBarry Smith   PetscFunctionReturn(0);
2670be6bf707SBarry Smith }
2671fb2e594dSBarry Smith EXTERN_C_END
2672be6bf707SBarry Smith 
26734a2ae208SSatish Balay #undef __FUNCT__
2674b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2675be6bf707SBarry Smith /*@
2676be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2677be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2678be6bf707SBarry Smith        nonlinear portion.
2679be6bf707SBarry Smith 
2680be6bf707SBarry Smith    Collect on Mat
2681be6bf707SBarry Smith 
2682be6bf707SBarry Smith   Input Parameters:
26830e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2684be6bf707SBarry Smith 
268515091d37SBarry Smith   Level: advanced
268615091d37SBarry Smith 
2687be6bf707SBarry Smith   Common Usage, with SNESSolve():
2688be6bf707SBarry Smith $    Create Jacobian matrix
2689be6bf707SBarry Smith $    Set linear terms into matrix
2690be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2691be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2692be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2693512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2694be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2695be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2696be6bf707SBarry Smith $    In your Jacobian routine
2697be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2698be6bf707SBarry Smith $      Set nonlinear terms in matrix
2699be6bf707SBarry Smith 
2700be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2701be6bf707SBarry Smith $    // build linear portion of Jacobian
2702512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2703be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2704be6bf707SBarry Smith $    loop over nonlinear iterations
2705be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2706be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2707be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2708be6bf707SBarry Smith $       Solve linear system with Jacobian
2709be6bf707SBarry Smith $    endloop
2710be6bf707SBarry Smith 
2711be6bf707SBarry Smith   Notes:
2712be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2713512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2714be6bf707SBarry Smith     calling this routine.
2715be6bf707SBarry Smith 
27160c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
27170c468ba9SBarry Smith     and does not allocated additional space.
27180c468ba9SBarry Smith 
2719be6bf707SBarry Smith .seealso: MatRetrieveValues()
2720be6bf707SBarry Smith 
2721be6bf707SBarry Smith @*/
2722be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2723be6bf707SBarry Smith {
2724dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2725be6bf707SBarry Smith 
2726be6bf707SBarry Smith   PetscFunctionBegin;
27274482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
272829bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
272929bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2730be6bf707SBarry Smith 
2731c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2732be6bf707SBarry Smith   if (f) {
2733be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2734be6bf707SBarry Smith   } else {
2735634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2736be6bf707SBarry Smith   }
2737be6bf707SBarry Smith   PetscFunctionReturn(0);
2738be6bf707SBarry Smith }
2739be6bf707SBarry Smith 
2740fb2e594dSBarry Smith EXTERN_C_BEGIN
27414a2ae208SSatish Balay #undef __FUNCT__
27424a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2743be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2744be6bf707SBarry Smith {
2745be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27466849ba73SBarry Smith   PetscErrorCode ierr;
2747d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2748be6bf707SBarry Smith 
2749be6bf707SBarry Smith   PetscFunctionBegin;
2750be6bf707SBarry Smith   if (aij->nonew != 1) {
2751512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2752be6bf707SBarry Smith   }
2753be6bf707SBarry Smith   if (!aij->saved_values) {
2754634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2755be6bf707SBarry Smith   }
2756be6bf707SBarry Smith   /* copy values over */
275787828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2758be6bf707SBarry Smith   PetscFunctionReturn(0);
2759be6bf707SBarry Smith }
2760fb2e594dSBarry Smith EXTERN_C_END
2761be6bf707SBarry Smith 
27624a2ae208SSatish Balay #undef __FUNCT__
27634a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2764be6bf707SBarry Smith /*@
2765be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2766be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2767be6bf707SBarry Smith        nonlinear portion.
2768be6bf707SBarry Smith 
2769be6bf707SBarry Smith    Collect on Mat
2770be6bf707SBarry Smith 
2771be6bf707SBarry Smith   Input Parameters:
2772be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2773be6bf707SBarry Smith 
277415091d37SBarry Smith   Level: advanced
277515091d37SBarry Smith 
2776be6bf707SBarry Smith .seealso: MatStoreValues()
2777be6bf707SBarry Smith 
2778be6bf707SBarry Smith @*/
2779be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2780be6bf707SBarry Smith {
2781dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2782be6bf707SBarry Smith 
2783be6bf707SBarry Smith   PetscFunctionBegin;
27844482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
278529bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
278629bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2787be6bf707SBarry Smith 
2788c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2789be6bf707SBarry Smith   if (f) {
2790be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2791be6bf707SBarry Smith   } else {
2792634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2793be6bf707SBarry Smith   }
2794be6bf707SBarry Smith   PetscFunctionReturn(0);
2795be6bf707SBarry Smith }
2796be6bf707SBarry Smith 
2797f83d6046SBarry Smith 
2798be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
27994a2ae208SSatish Balay #undef __FUNCT__
28004a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
280117ab2063SBarry Smith /*@C
2802682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
28030d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
28046e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
280551c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
28062bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
280717ab2063SBarry Smith 
2808db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2809db81eaa0SLois Curfman McInnes 
281017ab2063SBarry Smith    Input Parameters:
2811db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
281217ab2063SBarry Smith .  m - number of rows
281317ab2063SBarry Smith .  n - number of columns
281417ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
281551c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
28162bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
281717ab2063SBarry Smith 
281817ab2063SBarry Smith    Output Parameter:
2819416022c9SBarry Smith .  A - the matrix
282017ab2063SBarry Smith 
2821175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2822175b88e8SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly. This is definitely
2823175b88e8SBarry Smith    true if you plan to use the external direct solvers such as SuperLU, MUMPS or Spooles.
2824175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2825175b88e8SBarry Smith 
2826b259b22eSLois Curfman McInnes    Notes:
282749a6f317SBarry Smith    If nnz is given then nz is ignored
282849a6f317SBarry Smith 
282917ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
283017ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28310002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
283244cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
283317ab2063SBarry Smith 
283417ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2835a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28363d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28376da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
283817ab2063SBarry Smith 
2839682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
28404fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2841682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
28426c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
28436c7ebb05SLois Curfman McInnes 
28446c7ebb05SLois Curfman McInnes    Options Database Keys:
2845698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2846698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2847db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2848db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2849db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
285017ab2063SBarry Smith 
2851027ccd11SLois Curfman McInnes    Level: intermediate
2852027ccd11SLois Curfman McInnes 
285336db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
285436db0b34SBarry Smith 
285517ab2063SBarry Smith @*/
2856be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
285717ab2063SBarry Smith {
2858dfbe8321SBarry Smith   PetscErrorCode ierr;
28596945ee14SBarry Smith 
28603a40ed3dSBarry Smith   PetscFunctionBegin;
2861f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2862117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2863c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2864ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2865273d9f13SBarry Smith   PetscFunctionReturn(0);
2866273d9f13SBarry Smith }
2867273d9f13SBarry Smith 
28684a2ae208SSatish Balay #undef __FUNCT__
28694a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2870273d9f13SBarry Smith /*@C
2871273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2872273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2873273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2874273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2875273d9f13SBarry Smith 
2876273d9f13SBarry Smith    Collective on MPI_Comm
2877273d9f13SBarry Smith 
2878273d9f13SBarry Smith    Input Parameters:
2879117016b1SBarry Smith +  B - The matrix-free
2880273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2881273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2882273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2883273d9f13SBarry Smith 
2884273d9f13SBarry Smith    Notes:
288549a6f317SBarry Smith      If nnz is given then nz is ignored
288649a6f317SBarry Smith 
2887273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2888273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2889273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2890273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2891273d9f13SBarry Smith 
2892273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2893273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2894273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2895273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2896273d9f13SBarry Smith 
2897aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2898aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2899aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2900aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2901aa95bbe8SBarry Smith 
2902a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2903a96a251dSBarry Smith    entries or columns indices
2904a96a251dSBarry Smith 
2905273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2906273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2907273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2908273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2909273d9f13SBarry Smith 
2910273d9f13SBarry Smith    Options Database Keys:
2911698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2912698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2913273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2914273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2915273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2916273d9f13SBarry Smith 
2917273d9f13SBarry Smith    Level: intermediate
2918273d9f13SBarry Smith 
2919aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2920273d9f13SBarry Smith 
2921273d9f13SBarry Smith @*/
2922be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2923273d9f13SBarry Smith {
292497f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2925a23d5eceSKris Buschelman 
2926a23d5eceSKris Buschelman   PetscFunctionBegin;
2927a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2928a23d5eceSKris Buschelman   if (f) {
2929a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2930a23d5eceSKris Buschelman   }
2931a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2932a23d5eceSKris Buschelman }
2933a23d5eceSKris Buschelman 
2934a23d5eceSKris Buschelman EXTERN_C_BEGIN
2935a23d5eceSKris Buschelman #undef __FUNCT__
2936a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2937be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2938a23d5eceSKris Buschelman {
2939273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2940a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
29416849ba73SBarry Smith   PetscErrorCode ierr;
294297f1f81fSBarry Smith   PetscInt       i;
2943273d9f13SBarry Smith 
2944273d9f13SBarry Smith   PetscFunctionBegin;
2945d5d45c9bSBarry Smith 
2946a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2947c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2948c461c341SBarry Smith     nz             = 0;
2949c461c341SBarry Smith   }
2950c461c341SBarry Smith 
2951*7408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(B->rmap,1);CHKERRQ(ierr);
2952*7408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(B->cmap,1);CHKERRQ(ierr);
2953d0f46423SBarry Smith   ierr = PetscMapSetUp(B->rmap);CHKERRQ(ierr);
2954d0f46423SBarry Smith   ierr = PetscMapSetUp(B->cmap);CHKERRQ(ierr);
2955899cda47SBarry Smith 
2956435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2957435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2958b73539f3SBarry Smith   if (nnz) {
2959d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
296029bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2961d0f46423SBarry 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);
2962b73539f3SBarry Smith     }
2963b73539f3SBarry Smith   }
2964b73539f3SBarry Smith 
2965273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2966273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2967273d9f13SBarry Smith 
2968ab93d7beSBarry Smith   if (!skipallocation) {
29692ee49352SLisandro Dalcin     if (!b->imax) {
2970d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
2971d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
29722ee49352SLisandro Dalcin     }
2973273d9f13SBarry Smith     if (!nnz) {
2974435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2975273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2976d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
2977d0f46423SBarry Smith       nz = nz*B->rmap->n;
2978273d9f13SBarry Smith     } else {
2979273d9f13SBarry Smith       nz = 0;
2980d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2981273d9f13SBarry Smith     }
2982ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2983d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
2984ab93d7beSBarry Smith 
2985273d9f13SBarry Smith     /* allocate the matrix space */
29862ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2987d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
2988d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2989bfeeae90SHong Zhang     b->i[0] = 0;
2990d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
29915da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29925da197adSKris Buschelman     }
2993273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2994e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2995e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2996c461c341SBarry Smith   } else {
2997e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2998e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2999c461c341SBarry Smith   }
3000273d9f13SBarry Smith 
3001273d9f13SBarry Smith   b->nz                = 0;
3002273d9f13SBarry Smith   b->maxnz             = nz;
3003273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3004273d9f13SBarry Smith   PetscFunctionReturn(0);
3005273d9f13SBarry Smith }
3006a23d5eceSKris Buschelman EXTERN_C_END
3007273d9f13SBarry Smith 
3008a1661176SMatthew Knepley #undef  __FUNCT__
3009a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
301058d36128SBarry Smith /*@
3011a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3012a1661176SMatthew Knepley 
3013a1661176SMatthew Knepley    Input Parameters:
3014a1661176SMatthew Knepley +  B - the matrix
3015a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3016a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3017a1661176SMatthew Knepley -  v - optional values in the matrix
3018a1661176SMatthew Knepley 
3019d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
3020d58b2322SMatthew Knepley 
3021a1661176SMatthew Knepley    Level: developer
3022a1661176SMatthew Knepley 
302358d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
302458d36128SBarry Smith 
3025a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3026a1661176SMatthew Knepley 
3027a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3028a1661176SMatthew Knepley @*/
3029a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3030a1661176SMatthew Knepley {
3031a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3032a1661176SMatthew Knepley   PetscErrorCode ierr;
3033a1661176SMatthew Knepley 
3034a1661176SMatthew Knepley   PetscFunctionBegin;
3035a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
3036a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3037a1661176SMatthew Knepley   if (f) {
3038a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3039a1661176SMatthew Knepley   }
3040a1661176SMatthew Knepley   PetscFunctionReturn(0);
3041a1661176SMatthew Knepley }
3042a1661176SMatthew Knepley 
3043a1661176SMatthew Knepley EXTERN_C_BEGIN
3044a1661176SMatthew Knepley #undef  __FUNCT__
3045a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3046b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3047a1661176SMatthew Knepley {
3048a1661176SMatthew Knepley   PetscInt       i;
3049a1661176SMatthew Knepley   PetscInt       m,n;
3050a1661176SMatthew Knepley   PetscInt       nz;
3051a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3052a1661176SMatthew Knepley   PetscScalar    *values;
3053a1661176SMatthew Knepley   PetscErrorCode ierr;
3054a1661176SMatthew Knepley 
3055a1661176SMatthew Knepley   PetscFunctionBegin;
3056a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3057a1661176SMatthew Knepley 
3058b7940d39SSatish Balay   if (Ii[0]) {
3059b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3060a1661176SMatthew Knepley   }
3061a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3062a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3063b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3064a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
3065a1661176SMatthew Knepley     if (nz < 0) {
3066a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3067a1661176SMatthew Knepley     }
3068a1661176SMatthew Knepley     nnz[i] = nz;
3069a1661176SMatthew Knepley   }
3070a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3071a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3072a1661176SMatthew Knepley 
3073a1661176SMatthew Knepley   if (v) {
3074a1661176SMatthew Knepley     values = (PetscScalar*) v;
3075a1661176SMatthew Knepley   } else {
3076a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3077a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3078a1661176SMatthew Knepley   }
3079a1661176SMatthew Knepley 
3080a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3081b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3082b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3083a1661176SMatthew Knepley   }
3084a1661176SMatthew Knepley 
3085a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3086a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3087a1661176SMatthew Knepley 
3088a1661176SMatthew Knepley   if (!v) {
3089a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3090a1661176SMatthew Knepley   }
3091a1661176SMatthew Knepley   PetscFunctionReturn(0);
3092a1661176SMatthew Knepley }
3093a1661176SMatthew Knepley EXTERN_C_END
3094a1661176SMatthew Knepley 
30957c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
30967c4f633dSBarry Smith #include "../src/inline/axpy.h"
3097170fe5c8SBarry Smith 
3098170fe5c8SBarry Smith #undef __FUNCT__
3099170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3100170fe5c8SBarry Smith /*
3101170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3102170fe5c8SBarry Smith 
3103170fe5c8SBarry Smith                n                       p                          p
3104170fe5c8SBarry Smith         (              )       (              )         (                  )
3105170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3106170fe5c8SBarry Smith         (              )       (              )         (                  )
3107170fe5c8SBarry Smith 
3108170fe5c8SBarry Smith */
3109170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3110170fe5c8SBarry Smith {
3111170fe5c8SBarry Smith   PetscErrorCode     ierr;
3112170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3113170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3114170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
31151de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3116170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3117170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3118170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3119170fe5c8SBarry Smith 
3120170fe5c8SBarry Smith   PetscFunctionBegin;
3121d0f46423SBarry Smith   m = A->rmap->n;
3122d0f46423SBarry Smith   n = A->cmap->n;
3123d0f46423SBarry Smith   p = B->cmap->n;
3124170fe5c8SBarry Smith   a = sub_a->v;
3125170fe5c8SBarry Smith   b = sub_b->a;
3126170fe5c8SBarry Smith   c = sub_c->v;
3127170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3128170fe5c8SBarry Smith 
3129170fe5c8SBarry Smith   ii  = sub_b->i;
3130170fe5c8SBarry Smith   idx = sub_b->j;
3131170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3132170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3133170fe5c8SBarry Smith     while (q-->0) {
3134170fe5c8SBarry Smith       c_q = c + m*(*idx);
3135170fe5c8SBarry Smith       a_q = a + m*i;
31361de00fd4SBarry Smith       APXY(c_q,*b,a_q,m);
3137170fe5c8SBarry Smith       idx++;
3138170fe5c8SBarry Smith       b++;
3139170fe5c8SBarry Smith     }
3140170fe5c8SBarry Smith   }
3141170fe5c8SBarry Smith   PetscFunctionReturn(0);
3142170fe5c8SBarry Smith }
3143170fe5c8SBarry Smith 
3144170fe5c8SBarry Smith #undef __FUNCT__
3145170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3146170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3147170fe5c8SBarry Smith {
3148170fe5c8SBarry Smith   PetscErrorCode ierr;
3149d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3150170fe5c8SBarry Smith   Mat            Cmat;
3151170fe5c8SBarry Smith 
3152170fe5c8SBarry Smith   PetscFunctionBegin;
3153d0f46423SBarry 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);
315439804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3155170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3156170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3157170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3158170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3159170fe5c8SBarry Smith   *C = Cmat;
3160170fe5c8SBarry Smith   PetscFunctionReturn(0);
3161170fe5c8SBarry Smith }
3162170fe5c8SBarry Smith 
3163170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3164170fe5c8SBarry Smith #undef __FUNCT__
3165170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3166170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3167170fe5c8SBarry Smith {
3168170fe5c8SBarry Smith   PetscErrorCode ierr;
3169170fe5c8SBarry Smith 
3170170fe5c8SBarry Smith   PetscFunctionBegin;
3171170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3172170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3173170fe5c8SBarry Smith   }
3174170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3175170fe5c8SBarry Smith   PetscFunctionReturn(0);
3176170fe5c8SBarry Smith }
3177170fe5c8SBarry Smith 
3178170fe5c8SBarry Smith 
31790bad9183SKris Buschelman /*MC
3180fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31810bad9183SKris Buschelman    based on compressed sparse row format.
31820bad9183SKris Buschelman 
31830bad9183SKris Buschelman    Options Database Keys:
31840bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31850bad9183SKris Buschelman 
31860bad9183SKris Buschelman   Level: beginner
31870bad9183SKris Buschelman 
3188f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31890bad9183SKris Buschelman M*/
31900bad9183SKris Buschelman 
3191a6175056SHong Zhang EXTERN_C_BEGIN
319217667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3193b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3194db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3195611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
31965c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3197611f576cSBarry Smith #endif
3198611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
31995c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3200611f576cSBarry Smith #endif
3201f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3202f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3203f3c0ef26SHong Zhang #endif
3204611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32055c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3206611f576cSBarry Smith #endif
3207eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3208eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3209eb3b5408SSatish Balay #endif
3210719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3211719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3212719d5645SBarry Smith #endif
321317667f90SBarry Smith EXTERN_C_END
321417667f90SBarry Smith 
3215b24902e0SBarry Smith 
321617667f90SBarry Smith EXTERN_C_BEGIN
32174a2ae208SSatish Balay #undef __FUNCT__
32184a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3219be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3220273d9f13SBarry Smith {
3221273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3222dfbe8321SBarry Smith   PetscErrorCode ierr;
322338baddfdSBarry Smith   PetscMPIInt    size;
3224273d9f13SBarry Smith 
3225273d9f13SBarry Smith   PetscFunctionBegin;
32267adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3227273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3228273d9f13SBarry Smith 
322938f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3230b0a32e0cSBarry Smith   B->data             = (void*)b;
3231549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
323290f02eecSBarry Smith   B->mapping          = 0;
3233416022c9SBarry Smith   b->row              = 0;
3234416022c9SBarry Smith   b->col              = 0;
323582bf6240SBarry Smith   b->icol             = 0;
3236b810aeb4SBarry Smith   b->reallocs         = 0;
323736db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3238f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3239416022c9SBarry Smith   b->nonew             = 0;
3240416022c9SBarry Smith   b->diag              = 0;
3241416022c9SBarry Smith   b->solve_work        = 0;
32422a1b7f2aSHong Zhang   B->spptr             = 0;
3243be6bf707SBarry Smith   b->saved_values      = 0;
3244d7f994e1SBarry Smith   b->idiag             = 0;
324571f1c65dSBarry Smith   b->mdiag             = 0;
324671f1c65dSBarry Smith   b->ssor_work         = 0;
324771f1c65dSBarry Smith   b->omega             = 1.0;
324871f1c65dSBarry Smith   b->fshift            = 0.0;
324971f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3250f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
3251a30b2313SHong Zhang   b->xtoy              = 0;
3252a30b2313SHong Zhang   b->XtoY              = 0;
325373e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3254d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3255d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3256d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3257d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
325888e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
325917ab2063SBarry Smith 
326035d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3261719d5645SBarry Smith #if defined(PETSC_HAVE_ESSL)
3262719d5645SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_essl_C",
3263719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3264719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3265719d5645SBarry Smith #endif
3266611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32675c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_C",
32685c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
32695c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3270611f576cSBarry Smith #endif
3271f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3272f3c0ef26SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_dist_C",
3273f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3274f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3275f3c0ef26SHong Zhang #endif
3276611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32775c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_spooles_C",
32785c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
32795c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3280611f576cSBarry Smith #endif
3281611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
32825c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_mumps_C",
32835c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
32845c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3285611f576cSBarry Smith #endif
3286eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3287eb3b5408SSatish Balay     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_umfpack_C",
3288eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3289eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3290eb3b5408SSatish Balay #endif
3291719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3292719d5645SBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_lusol_C",
3293719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3294719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3295719d5645SBarry Smith #endif
3296b24902e0SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_petsc_C",
3297b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3298b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3299db4efbfdSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_seqaij_petsc_C",
3300db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3301db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3302f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3303bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3304bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3305f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3306be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3307bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3308f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3309be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3310bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3311a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3312a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3313a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
331485fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
331585fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
331685fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3317ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3318ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3319ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
332017667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
332117667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
332217667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33235fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33245fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33255fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33261cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33271cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33281cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3329a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3330a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3331a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3332a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3333a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3334a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
333505b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
333605b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
333705b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3338170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3339170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3340170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3341170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3342170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3343170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3344170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3345170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3346170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
33474846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
334817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
33493a40ed3dSBarry Smith   PetscFunctionReturn(0);
335017ab2063SBarry Smith }
3351273d9f13SBarry Smith EXTERN_C_END
335217ab2063SBarry Smith 
33534a2ae208SSatish Balay #undef __FUNCT__
3354b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3355b24902e0SBarry Smith /*
3356b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3357b24902e0SBarry Smith */
3358719d5645SBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues)
335917ab2063SBarry Smith {
3360416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
33616849ba73SBarry Smith   PetscErrorCode ierr;
3362d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
336317ab2063SBarry Smith 
33643a40ed3dSBarry Smith   PetscFunctionBegin;
33651d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
33661d5dac46SHong Zhang 
3367273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3368273d9f13SBarry Smith 
3369416022c9SBarry Smith   C->factor           = A->factor;
33706ad4291fSHong Zhang 
3371416022c9SBarry Smith   c->row            = 0;
3372416022c9SBarry Smith   c->col            = 0;
337382bf6240SBarry Smith   c->icol           = 0;
33746ad4291fSHong Zhang   c->reallocs       = 0;
337517ab2063SBarry Smith 
33766ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
337717ab2063SBarry Smith 
3378*7408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(C->rmap,1);CHKERRQ(ierr);
3379*7408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(C->cmap,1);CHKERRQ(ierr);
3380d0f46423SBarry Smith   ierr = PetscMapSetUp(C->rmap);CHKERRQ(ierr);
3381d0f46423SBarry Smith   ierr = PetscMapSetUp(C->cmap);CHKERRQ(ierr);
3382eec197d1SBarry Smith 
338333b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
33849518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
338517ab2063SBarry Smith   for (i=0; i<m; i++) {
3386416022c9SBarry Smith     c->imax[i] = a->imax[i];
3387416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
338817ab2063SBarry Smith   }
338917ab2063SBarry Smith 
339017ab2063SBarry Smith   /* allocate the matrix space */
3391a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
33929518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3393f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
339497f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
339517ab2063SBarry Smith   if (m > 0) {
339697f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3397be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
3398bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3399be6bf707SBarry Smith     } else {
3400bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
340117ab2063SBarry Smith     }
340208480c60SBarry Smith   }
340317ab2063SBarry Smith 
34046ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3405416022c9SBarry Smith   c->roworiented       = a->roworiented;
3406416022c9SBarry Smith   c->nonew             = a->nonew;
3407416022c9SBarry Smith   if (a->diag) {
340897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
340952e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
341017ab2063SBarry Smith     for (i=0; i<m; i++) {
3411416022c9SBarry Smith       c->diag[i] = a->diag[i];
341217ab2063SBarry Smith     }
34133a40ed3dSBarry Smith   } else c->diag           = 0;
34146ad4291fSHong Zhang   c->solve_work            = 0;
34156ad4291fSHong Zhang   c->saved_values          = 0;
34166ad4291fSHong Zhang   c->idiag                 = 0;
341771f1c65dSBarry Smith   c->ssor_work             = 0;
34186ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
3419e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3420e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34216ad4291fSHong Zhang   c->xtoy                  = 0;
34226ad4291fSHong Zhang   c->XtoY                  = 0;
34236ad4291fSHong Zhang 
3424416022c9SBarry Smith   c->nz                 = a->nz;
3425416022c9SBarry Smith   c->maxnz              = a->maxnz;
3426273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3427754ec7b1SSatish Balay 
34286ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34296ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
34306ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
34316ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
34326ad4291fSHong Zhang     i = a->compressedrow.nrows;
34336ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
34346ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
34356ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
34366ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
343727ea64f8SHong Zhang   } else {
343827ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
343927ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
344027ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
34416ad4291fSHong Zhang   }
344288e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
34434846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
34444846f1f5SKris Buschelman 
34457adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
34463a40ed3dSBarry Smith   PetscFunctionReturn(0);
344717ab2063SBarry Smith }
344817ab2063SBarry Smith 
34494a2ae208SSatish Balay #undef __FUNCT__
3450b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3451b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3452b24902e0SBarry Smith {
3453b24902e0SBarry Smith   PetscErrorCode ierr;
3454d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
3455b24902e0SBarry Smith 
3456b24902e0SBarry Smith   PetscFunctionBegin;
3457b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3458b24902e0SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
3459b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3460719d5645SBarry Smith   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues);CHKERRQ(ierr);
3461b24902e0SBarry Smith   PetscFunctionReturn(0);
3462b24902e0SBarry Smith }
3463b24902e0SBarry Smith 
3464b24902e0SBarry Smith #undef __FUNCT__
34654a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3466a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
346717ab2063SBarry Smith {
3468416022c9SBarry Smith   Mat_SeqAIJ     *a;
3469416022c9SBarry Smith   Mat            B;
34706849ba73SBarry Smith   PetscErrorCode ierr;
34713c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
347238baddfdSBarry Smith   int            fd;
347338baddfdSBarry Smith   PetscMPIInt    size;
3474bcd2baecSBarry Smith   MPI_Comm       comm;
347517ab2063SBarry Smith 
34763a40ed3dSBarry Smith   PetscFunctionBegin;
3477e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3478d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
347929bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3480b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
34810752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3482552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
348317ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
348417ab2063SBarry Smith 
3485d64ed03dSBarry Smith   if (nz < 0) {
348629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3487d64ed03dSBarry Smith   }
3488d64ed03dSBarry Smith 
348917ab2063SBarry Smith   /* read in row lengths */
349097f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
34910752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
349217ab2063SBarry Smith 
34933c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
34943c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
34953c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
34963c601197SSatish Balay 
349717ab2063SBarry Smith   /* create our matrix */
3498f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3499f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3500b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3501ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3502416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
350317ab2063SBarry Smith 
35040752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
350517ab2063SBarry Smith 
350617ab2063SBarry Smith   /* read in nonzero values */
35070752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
350817ab2063SBarry Smith 
350917ab2063SBarry Smith   /* set matrix "i" values */
3510efb16452SHong Zhang   a->i[0] = 0;
351117ab2063SBarry Smith   for (i=1; i<= M; i++) {
3512416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3513416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
351417ab2063SBarry Smith   }
3515606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
351617ab2063SBarry Smith 
35176d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35186d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3519b3a1e11cSKris Buschelman   *A = B;
35203a40ed3dSBarry Smith   PetscFunctionReturn(0);
352117ab2063SBarry Smith }
352217ab2063SBarry Smith 
35234a2ae208SSatish Balay #undef __FUNCT__
3524b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3525dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35267264ac53SSatish Balay {
35277264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3528dfbe8321SBarry Smith   PetscErrorCode ierr;
35297264ac53SSatish Balay 
35303a40ed3dSBarry Smith   PetscFunctionBegin;
3531bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3532d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3533ca44d042SBarry Smith     *flg = PETSC_FALSE;
3534ca44d042SBarry Smith     PetscFunctionReturn(0);
3535bcd2baecSBarry Smith   }
35367264ac53SSatish Balay 
35377264ac53SSatish Balay   /* if the a->i are the same */
3538d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3539abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
35407264ac53SSatish Balay 
35417264ac53SSatish Balay   /* if a->j are the same */
354297f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3543abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3544bcd2baecSBarry Smith 
3545bcd2baecSBarry Smith   /* if a->a are the same */
354687828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
35470f5bd95cSBarry Smith 
35483a40ed3dSBarry Smith   PetscFunctionReturn(0);
35497264ac53SSatish Balay 
35507264ac53SSatish Balay }
355136db0b34SBarry Smith 
35524a2ae208SSatish Balay #undef __FUNCT__
35534a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
355405869f15SSatish Balay /*@
355536db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
355636db0b34SBarry Smith               provided by the user.
355736db0b34SBarry Smith 
3558c75a6043SHong Zhang       Collective on MPI_Comm
355936db0b34SBarry Smith 
356036db0b34SBarry Smith    Input Parameters:
356136db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
356236db0b34SBarry Smith .   m - number of rows
356336db0b34SBarry Smith .   n - number of columns
356436db0b34SBarry Smith .   i - row indices
356536db0b34SBarry Smith .   j - column indices
356636db0b34SBarry Smith -   a - matrix values
356736db0b34SBarry Smith 
356836db0b34SBarry Smith    Output Parameter:
356936db0b34SBarry Smith .   mat - the matrix
357036db0b34SBarry Smith 
357136db0b34SBarry Smith    Level: intermediate
357236db0b34SBarry Smith 
357336db0b34SBarry Smith    Notes:
35740551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
357536db0b34SBarry Smith     once the matrix is destroyed
357636db0b34SBarry Smith 
357736db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
357836db0b34SBarry Smith 
3579bfeeae90SHong Zhang        The i and j indices are 0 based
358036db0b34SBarry Smith 
3581a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3582a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3583a4552177SSatish Balay     as shown:
3584a4552177SSatish Balay 
3585a4552177SSatish Balay         1 0 0
3586a4552177SSatish Balay         2 0 3
3587a4552177SSatish Balay         4 5 6
3588a4552177SSatish Balay 
3589a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
35909985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3591a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3592a4552177SSatish Balay 
35939985e31cSBarry Smith 
35942fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
359536db0b34SBarry Smith 
359636db0b34SBarry Smith @*/
3597a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
359836db0b34SBarry Smith {
3599dfbe8321SBarry Smith   PetscErrorCode ierr;
3600cbcfb4deSHong Zhang   PetscInt       ii;
360136db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3602cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3603cbcfb4deSHong Zhang   PetscInt       jj;
3604cbcfb4deSHong Zhang #endif
360536db0b34SBarry Smith 
360636db0b34SBarry Smith   PetscFunctionBegin;
3607a96a251dSBarry Smith   if (i[0]) {
3608634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
360936db0b34SBarry Smith   }
3610f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3611f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3612ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3613ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3614ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3615ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3616ab93d7beSBarry Smith 
361736db0b34SBarry Smith   aij->i = i;
361836db0b34SBarry Smith   aij->j = j;
361936db0b34SBarry Smith   aij->a = a;
362036db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
362136db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3622e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3623e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
362436db0b34SBarry Smith 
362536db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
362636db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
36272515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
362879a5c55eSBarry 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]);
36299985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
36309985e31cSBarry 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);
36319985e31cSBarry 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);
36329985e31cSBarry Smith     }
363336db0b34SBarry Smith #endif
363436db0b34SBarry Smith   }
36352515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
363636db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
363779a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
363879a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
363936db0b34SBarry Smith   }
364036db0b34SBarry Smith #endif
364136db0b34SBarry Smith 
3642b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3643b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
364436db0b34SBarry Smith   PetscFunctionReturn(0);
364536db0b34SBarry Smith }
364636db0b34SBarry Smith 
3647cc8ba8e1SBarry Smith #undef __FUNCT__
3648ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3649dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3650cc8ba8e1SBarry Smith {
3651dfbe8321SBarry Smith   PetscErrorCode ierr;
3652cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
365336db0b34SBarry Smith 
3654cc8ba8e1SBarry Smith   PetscFunctionBegin;
36558ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3656cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3657cc8ba8e1SBarry Smith     a->coloring = coloring;
365812c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
365997f1f81fSBarry Smith     PetscInt             i,*larray;
366012c595b3SBarry Smith     ISColoring      ocoloring;
366108b6dcc0SBarry Smith     ISColoringValue *colors;
366212c595b3SBarry Smith 
366312c595b3SBarry Smith     /* set coloring for diagonal portion */
3664d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3665d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
366612c595b3SBarry Smith       larray[i] = i;
366712c595b3SBarry Smith     }
3668d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3669d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3670d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
367112c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
367212c595b3SBarry Smith     }
367312c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3674d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
367512c595b3SBarry Smith     a->coloring = ocoloring;
367612c595b3SBarry Smith   }
3677cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3678cc8ba8e1SBarry Smith }
3679cc8ba8e1SBarry Smith 
3680dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3681ee4f033dSBarry Smith EXTERN_C_BEGIN
368229c1e371SBarry Smith #include "adic/ad_utils.h"
3683ee4f033dSBarry Smith EXTERN_C_END
3684cc8ba8e1SBarry Smith 
3685cc8ba8e1SBarry Smith #undef __FUNCT__
3686ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3687dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3688cc8ba8e1SBarry Smith {
3689cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3690d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
36914440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
369208b6dcc0SBarry Smith   ISColoringValue *color;
3693cc8ba8e1SBarry Smith 
3694cc8ba8e1SBarry Smith   PetscFunctionBegin;
3695e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
36964440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3697cc8ba8e1SBarry Smith   color = a->coloring->colors;
3698cc8ba8e1SBarry Smith   /* loop over rows */
3699cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3700cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3701cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3702cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3703cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3704cc8ba8e1SBarry Smith     }
37054440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3706ee4f033dSBarry Smith   }
3707ee4f033dSBarry Smith   PetscFunctionReturn(0);
3708ee4f033dSBarry Smith }
3709ee4f033dSBarry Smith #endif
3710ee4f033dSBarry Smith 
3711ee4f033dSBarry Smith #undef __FUNCT__
3712ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
371397f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3714ee4f033dSBarry Smith {
3715ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3716d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
371754f21887SBarry Smith   MatScalar       *v = a->a;
371854f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
371908b6dcc0SBarry Smith   ISColoringValue *color;
3720ee4f033dSBarry Smith 
3721ee4f033dSBarry Smith   PetscFunctionBegin;
3722e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3723ee4f033dSBarry Smith   color = a->coloring->colors;
3724ee4f033dSBarry Smith   /* loop over rows */
3725ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3726ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3727ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3728ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3729ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3730ee4f033dSBarry Smith     }
3731ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3732cc8ba8e1SBarry Smith   }
3733cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3734cc8ba8e1SBarry Smith }
373536db0b34SBarry Smith 
373681824310SBarry Smith /*
373781824310SBarry Smith     Special version for direct calls from Fortran
373881824310SBarry Smith */
373981824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
374081824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
374181824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
374281824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
374381824310SBarry Smith #endif
374481824310SBarry Smith 
374581824310SBarry Smith /* Change these macros so can be used in void function */
374681824310SBarry Smith #undef CHKERRQ
37477adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
374881824310SBarry Smith #undef SETERRQ2
37497adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
375081824310SBarry Smith 
375181824310SBarry Smith EXTERN_C_BEGIN
375281824310SBarry Smith #undef __FUNCT__
375381824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
37541f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
375581824310SBarry Smith {
375681824310SBarry Smith   Mat            A = *AA;
375781824310SBarry Smith   PetscInt       m = *mm, n = *nn;
375881824310SBarry Smith   InsertMode     is = *isis;
375981824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
376081824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
376181824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
376281824310SBarry Smith   PetscErrorCode ierr;
376381824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
376454f21887SBarry Smith   MatScalar      *ap,value,*aa;
3765edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
376681824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
376781824310SBarry Smith 
376881824310SBarry Smith   PetscFunctionBegin;
376981824310SBarry Smith   MatPreallocated(A);
377081824310SBarry Smith   imax = a->imax;
377181824310SBarry Smith   ai = a->i;
377281824310SBarry Smith   ailen = a->ilen;
377381824310SBarry Smith   aj = a->j;
377481824310SBarry Smith   aa = a->a;
377581824310SBarry Smith 
377681824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
377781824310SBarry Smith     row  = im[k];
377881824310SBarry Smith     if (row < 0) continue;
377981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3780d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
378181824310SBarry Smith #endif
378281824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
378381824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
378481824310SBarry Smith     low  = 0;
378581824310SBarry Smith     high = nrow;
378681824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
378781824310SBarry Smith       if (in[l] < 0) continue;
378881824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3789d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
379081824310SBarry Smith #endif
379181824310SBarry Smith       col = in[l];
379281824310SBarry Smith       if (roworiented) {
379381824310SBarry Smith         value = v[l + k*n];
379481824310SBarry Smith       } else {
379581824310SBarry Smith         value = v[k + l*m];
379681824310SBarry Smith       }
379781824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
379881824310SBarry Smith 
379981824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
380081824310SBarry Smith       lastcol = col;
380181824310SBarry Smith       while (high-low > 5) {
380281824310SBarry Smith         t = (low+high)/2;
380381824310SBarry Smith         if (rp[t] > col) high = t;
380481824310SBarry Smith         else             low  = t;
380581824310SBarry Smith       }
380681824310SBarry Smith       for (i=low; i<high; i++) {
380781824310SBarry Smith         if (rp[i] > col) break;
380881824310SBarry Smith         if (rp[i] == col) {
380981824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
381081824310SBarry Smith           else                  ap[i] = value;
381181824310SBarry Smith           goto noinsert;
381281824310SBarry Smith         }
381381824310SBarry Smith       }
381481824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
381581824310SBarry Smith       if (nonew == 1) goto noinsert;
38167adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3817d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
381881824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
381981824310SBarry Smith       /* shift up all the later entries in this row */
382081824310SBarry Smith       for (ii=N; ii>=i; ii--) {
382181824310SBarry Smith         rp[ii+1] = rp[ii];
382281824310SBarry Smith         ap[ii+1] = ap[ii];
382381824310SBarry Smith       }
382481824310SBarry Smith       rp[i] = col;
382581824310SBarry Smith       ap[i] = value;
382681824310SBarry Smith       noinsert:;
382781824310SBarry Smith       low = i + 1;
382881824310SBarry Smith     }
382981824310SBarry Smith     ailen[row] = nrow;
383081824310SBarry Smith   }
383181824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
383281824310SBarry Smith   PetscFunctionReturnVoid();
383381824310SBarry Smith }
383481824310SBarry Smith EXTERN_C_END
3835