xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 992144d0cb3c842d97cc8e5d51faae10c27f3335)
1b377110cSBarry Smith 
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
77c4f633dSBarry Smith 
8c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9c6db04a5SJed Brown #include <petscblaslapack.h>
10c6db04a5SJed Brown #include <petscbt.h>
1117ab2063SBarry Smith 
120716a85fSBarry Smith 
130716a85fSBarry Smith #undef __FUNCT__
140716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
150716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
160716a85fSBarry Smith {
170716a85fSBarry Smith   PetscErrorCode ierr;
180716a85fSBarry Smith   PetscInt       i,m,n;
190716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
200716a85fSBarry Smith 
210716a85fSBarry Smith   PetscFunctionBegin;
220716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
230716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
240716a85fSBarry Smith   if (type == NORM_2) {
250716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
260716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
270716a85fSBarry Smith     }
280716a85fSBarry Smith   } else if (type == NORM_1) {
290716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
300716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
310716a85fSBarry Smith     }
320716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
330716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
340716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
350716a85fSBarry Smith     }
360716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
370716a85fSBarry Smith 
380716a85fSBarry Smith   if (type == NORM_2) {
390716a85fSBarry Smith     for (i=0; i<n; i++) norms[i] = sqrt(norms[i]);
400716a85fSBarry Smith   }
410716a85fSBarry Smith   PetscFunctionReturn(0);
420716a85fSBarry Smith }
430716a85fSBarry Smith 
444a2ae208SSatish Balay #undef __FUNCT__
456ce1633cSBarry Smith #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
466ce1633cSBarry Smith PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
476ce1633cSBarry Smith {
486ce1633cSBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
496ce1633cSBarry Smith   const MatScalar   *aa = a->a;
506ce1633cSBarry Smith   PetscInt          i,m=A->rmap->n,cnt = 0;
516ce1633cSBarry Smith   const PetscInt    *jj = a->j,*diag;
526ce1633cSBarry Smith   PetscInt          *rows;
536ce1633cSBarry Smith   PetscErrorCode    ierr;
546ce1633cSBarry Smith 
556ce1633cSBarry Smith   PetscFunctionBegin;
566ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
576ce1633cSBarry Smith   diag = a->diag;
586ce1633cSBarry Smith   for (i=0; i<m; i++) {
596ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
606ce1633cSBarry Smith       cnt++;
616ce1633cSBarry Smith     }
626ce1633cSBarry Smith   }
636ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
646ce1633cSBarry Smith   cnt  = 0;
656ce1633cSBarry Smith   for (i=0; i<m; i++) {
666ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
676ce1633cSBarry Smith       rows[cnt++] = i;
686ce1633cSBarry Smith     }
696ce1633cSBarry Smith   }
706ce1633cSBarry Smith   ierr = ISCreateGeneral(((PetscObject)A)->comm,cnt,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
716ce1633cSBarry Smith   PetscFunctionReturn(0);
726ce1633cSBarry Smith }
736ce1633cSBarry Smith 
746ce1633cSBarry Smith #undef __FUNCT__
75b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
76b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
77b3a44c85SBarry Smith {
78b3a44c85SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
79b3a44c85SBarry Smith   const MatScalar   *aa;
80b3a44c85SBarry Smith   PetscInt          m=A->rmap->n,cnt = 0;
81b3a44c85SBarry Smith   const PetscInt    *ii;
82b3a44c85SBarry Smith   PetscInt          n,i,j,*rows;
83b3a44c85SBarry Smith   PetscErrorCode    ierr;
84b3a44c85SBarry Smith 
85b3a44c85SBarry Smith   PetscFunctionBegin;
86b3a44c85SBarry Smith   *keptrows = 0;
87b3a44c85SBarry Smith   ii        = a->i;
88b3a44c85SBarry Smith   for (i=0; i<m; i++) {
89b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
90b3a44c85SBarry Smith     if (!n) {
91b3a44c85SBarry Smith       cnt++;
92b3a44c85SBarry Smith       goto ok1;
93b3a44c85SBarry Smith     }
94b3a44c85SBarry Smith     aa  = a->a + ii[i];
95b3a44c85SBarry Smith     for (j=0; j<n; j++) {
96b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
97b3a44c85SBarry Smith     }
98b3a44c85SBarry Smith     cnt++;
99b3a44c85SBarry Smith     ok1:;
100b3a44c85SBarry Smith   }
101b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
102b3a44c85SBarry Smith   ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
103b3a44c85SBarry Smith   cnt  = 0;
104b3a44c85SBarry Smith   for (i=0; i<m; i++) {
105b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
106b3a44c85SBarry Smith     if (!n) continue;
107b3a44c85SBarry Smith     aa  = a->a + ii[i];
108b3a44c85SBarry Smith     for (j=0; j<n; j++) {
109b3a44c85SBarry Smith       if (aa[j] != 0.0) {
110b3a44c85SBarry Smith         rows[cnt++] = i;
111b3a44c85SBarry Smith         break;
112b3a44c85SBarry Smith       }
113b3a44c85SBarry Smith     }
114b3a44c85SBarry Smith   }
115b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
116b3a44c85SBarry Smith   PetscFunctionReturn(0);
117b3a44c85SBarry Smith }
118b3a44c85SBarry Smith 
119b3a44c85SBarry Smith #undef __FUNCT__
12079299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1217087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
12279299369SBarry Smith {
12379299369SBarry Smith   PetscErrorCode ierr;
12479299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
125d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
12654f21887SBarry Smith   MatScalar      *aa = aij->a;
12754f21887SBarry Smith   PetscScalar    *v;
128ace3abfcSBarry Smith   PetscBool      missing;
12979299369SBarry Smith 
13079299369SBarry Smith   PetscFunctionBegin;
13109f38230SBarry Smith   if (Y->assembled) {
13209f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
13309f38230SBarry Smith     if (!missing) {
13479299369SBarry Smith       diag = aij->diag;
13579299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
13679299369SBarry Smith       if (is == INSERT_VALUES) {
13779299369SBarry Smith 	for (i=0; i<m; i++) {
13879299369SBarry Smith 	  aa[diag[i]] = v[i];
13979299369SBarry Smith 	}
14079299369SBarry Smith       } else {
14179299369SBarry Smith 	for (i=0; i<m; i++) {
14279299369SBarry Smith 	  aa[diag[i]] += v[i];
14379299369SBarry Smith 	}
14479299369SBarry Smith       }
14579299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
14679299369SBarry Smith       PetscFunctionReturn(0);
14779299369SBarry Smith     }
14809f38230SBarry Smith   }
14909f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
15009f38230SBarry Smith   PetscFunctionReturn(0);
15109f38230SBarry Smith }
15279299369SBarry Smith 
15379299369SBarry Smith #undef __FUNCT__
1544a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
155ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
15617ab2063SBarry Smith {
157416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
158dfbe8321SBarry Smith   PetscErrorCode ierr;
15997f1f81fSBarry Smith   PetscInt       i,ishift;
16017ab2063SBarry Smith 
1613a40ed3dSBarry Smith   PetscFunctionBegin;
162d0f46423SBarry Smith   *m     = A->rmap->n;
1633a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
164bfeeae90SHong Zhang   ishift = 0;
16553e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
166d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
167bfeeae90SHong Zhang   } else if (oshift == 1) {
168d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
1693b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
170d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
171d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
172ecc77c7aSBarry Smith     if (ja) {
17397f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
1743b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
175ecc77c7aSBarry Smith     }
1766945ee14SBarry Smith   } else {
177ecc77c7aSBarry Smith     *ia = a->i;
178ecc77c7aSBarry Smith     if (ja) *ja = a->j;
179a2ce50c7SBarry Smith   }
1803a40ed3dSBarry Smith   PetscFunctionReturn(0);
181a2744918SBarry Smith }
182a2744918SBarry Smith 
1834a2ae208SSatish Balay #undef __FUNCT__
1844a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
185ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
1866945ee14SBarry Smith {
187dfbe8321SBarry Smith   PetscErrorCode ierr;
1886945ee14SBarry Smith 
1893a40ed3dSBarry Smith   PetscFunctionBegin;
1903a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
191bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
192606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
193ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
194bcd2baecSBarry Smith   }
1953a40ed3dSBarry Smith   PetscFunctionReturn(0);
19617ab2063SBarry Smith }
19717ab2063SBarry Smith 
1984a2ae208SSatish Balay #undef __FUNCT__
1994a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
200ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2013b2fbd54SBarry Smith {
2023b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
203dfbe8321SBarry Smith   PetscErrorCode ierr;
204d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
20597f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2063b2fbd54SBarry Smith 
2073a40ed3dSBarry Smith   PetscFunctionBegin;
208899cda47SBarry Smith   *nn = n;
2093a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2103b2fbd54SBarry Smith   if (symmetric) {
211d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
2123b2fbd54SBarry Smith   } else {
21397f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
21497f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
21597f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
21697f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
2173b2fbd54SBarry Smith     jj = a->j;
2183b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
219bfeeae90SHong Zhang       collengths[jj[i]]++;
2203b2fbd54SBarry Smith     }
2213b2fbd54SBarry Smith     cia[0] = oshift;
2223b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2233b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2243b2fbd54SBarry Smith     }
22597f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2263b2fbd54SBarry Smith     jj   = a->j;
227a93ec695SBarry Smith     for (row=0; row<m; row++) {
228a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
229a93ec695SBarry Smith       for (i=0; i<mr; i++) {
230bfeeae90SHong Zhang         col = *jj++;
2313b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2323b2fbd54SBarry Smith       }
2333b2fbd54SBarry Smith     }
234606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2353b2fbd54SBarry Smith     *ia = cia; *ja = cja;
2363b2fbd54SBarry Smith   }
2373a40ed3dSBarry Smith   PetscFunctionReturn(0);
2383b2fbd54SBarry Smith }
2393b2fbd54SBarry Smith 
2404a2ae208SSatish Balay #undef __FUNCT__
2414a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
242ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2433b2fbd54SBarry Smith {
244dfbe8321SBarry Smith   PetscErrorCode ierr;
245606d414cSSatish Balay 
2463a40ed3dSBarry Smith   PetscFunctionBegin;
2473a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2483b2fbd54SBarry Smith 
249606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
250606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2513b2fbd54SBarry Smith 
2523a40ed3dSBarry Smith   PetscFunctionReturn(0);
2533b2fbd54SBarry Smith }
2543b2fbd54SBarry Smith 
25587d4246cSBarry Smith #undef __FUNCT__
25687d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
25787d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
25887d4246cSBarry Smith {
25987d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26087d4246cSBarry Smith   PetscInt       *ai = a->i;
26187d4246cSBarry Smith   PetscErrorCode ierr;
26287d4246cSBarry Smith 
26387d4246cSBarry Smith   PetscFunctionBegin;
26487d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
26587d4246cSBarry Smith   PetscFunctionReturn(0);
26687d4246cSBarry Smith }
26787d4246cSBarry Smith 
2684a2ae208SSatish Balay #undef __FUNCT__
2694a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
27097f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
27117ab2063SBarry Smith {
272416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
273e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
27497f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
2756849ba73SBarry Smith   PetscErrorCode ierr;
276e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
27754f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
278ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
279ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
28017ab2063SBarry Smith 
2813a40ed3dSBarry Smith   PetscFunctionBegin;
28271fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
28317ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
284416022c9SBarry Smith     row  = im[k];
2855ef9f2a5SBarry Smith     if (row < 0) continue;
2862515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
287e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
2883b2fbd54SBarry Smith #endif
289bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
29017ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
291416022c9SBarry Smith     low  = 0;
292c71e6ed7SBarry Smith     high = nrow;
29317ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
2945ef9f2a5SBarry Smith       if (in[l] < 0) continue;
2952515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
296e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
2973b2fbd54SBarry Smith #endif
298bfeeae90SHong Zhang       col = in[l];
29916371a99SBarry Smith       if (v) {
3004b0e389bSBarry Smith 	if (roworiented) {
3015ef9f2a5SBarry Smith 	  value = v[l + k*n];
302bef8e0ddSBarry Smith 	} else {
3034b0e389bSBarry Smith 	  value = v[k + l*m];
3044b0e389bSBarry Smith 	}
30516371a99SBarry Smith       } else {
30675567043SBarry Smith         value = 0.;
30716371a99SBarry Smith       }
308abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
30936db0b34SBarry Smith 
3107cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
311e2ee6c50SBarry Smith       lastcol = col;
312416022c9SBarry Smith       while (high-low > 5) {
313416022c9SBarry Smith         t = (low+high)/2;
314416022c9SBarry Smith         if (rp[t] > col) high = t;
315416022c9SBarry Smith         else             low  = t;
31617ab2063SBarry Smith       }
317416022c9SBarry Smith       for (i=low; i<high; i++) {
31817ab2063SBarry Smith         if (rp[i] > col) break;
31917ab2063SBarry Smith         if (rp[i] == col) {
320416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
32117ab2063SBarry Smith           else                  ap[i] = value;
322e44c0bd4SBarry Smith           low = i + 1;
32317ab2063SBarry Smith           goto noinsert;
32417ab2063SBarry Smith         }
32517ab2063SBarry Smith       }
326abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
327c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
328e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
329fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
330c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
331416022c9SBarry Smith       /* shift up all the later entries in this row */
332416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
33317ab2063SBarry Smith         rp[ii+1] = rp[ii];
33417ab2063SBarry Smith         ap[ii+1] = ap[ii];
33517ab2063SBarry Smith       }
33617ab2063SBarry Smith       rp[i] = col;
33717ab2063SBarry Smith       ap[i] = value;
338416022c9SBarry Smith       low   = i + 1;
339e44c0bd4SBarry Smith       noinsert:;
34017ab2063SBarry Smith     }
34117ab2063SBarry Smith     ailen[row] = nrow;
34217ab2063SBarry Smith   }
34388e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3443a40ed3dSBarry Smith   PetscFunctionReturn(0);
34517ab2063SBarry Smith }
34617ab2063SBarry Smith 
34781824310SBarry Smith 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
350a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3517eb43aa7SLois Curfman McInnes {
3527eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
35397f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
35497f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
35554f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
3567eb43aa7SLois Curfman McInnes 
3573a40ed3dSBarry Smith   PetscFunctionBegin;
3587eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3597eb43aa7SLois Curfman McInnes     row  = im[k];
360e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
361e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
362bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3637eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3647eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
365e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
366e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
367bfeeae90SHong Zhang       col = in[l] ;
3687eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3697eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3707eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3717eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3727eb43aa7SLois Curfman McInnes         else             low  = t;
3737eb43aa7SLois Curfman McInnes       }
3747eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3757eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
3767eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
377b49de8d1SLois Curfman McInnes           *v++ = ap[i];
3787eb43aa7SLois Curfman McInnes           goto finished;
3797eb43aa7SLois Curfman McInnes         }
3807eb43aa7SLois Curfman McInnes       }
38197e567efSBarry Smith       *v++ = 0.0;
3827eb43aa7SLois Curfman McInnes       finished:;
3837eb43aa7SLois Curfman McInnes     }
3847eb43aa7SLois Curfman McInnes   }
3853a40ed3dSBarry Smith   PetscFunctionReturn(0);
3867eb43aa7SLois Curfman McInnes }
3877eb43aa7SLois Curfman McInnes 
38817ab2063SBarry Smith 
3894a2ae208SSatish Balay #undef __FUNCT__
3904a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
391dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
39217ab2063SBarry Smith {
393416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3946849ba73SBarry Smith   PetscErrorCode ierr;
3956f69ff64SBarry Smith   PetscInt       i,*col_lens;
3966f69ff64SBarry Smith   int            fd;
39717ab2063SBarry Smith 
3983a40ed3dSBarry Smith   PetscFunctionBegin;
399b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
400d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
4010700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
402d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
403d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
404416022c9SBarry Smith   col_lens[3] = a->nz;
405416022c9SBarry Smith 
406416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
407d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
408416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
40917ab2063SBarry Smith   }
410d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
411606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
412416022c9SBarry Smith 
413416022c9SBarry Smith   /* store column indices (zero start index) */
4146f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
415416022c9SBarry Smith 
416416022c9SBarry Smith   /* store nonzero values */
4176f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
4183a40ed3dSBarry Smith   PetscFunctionReturn(0);
41917ab2063SBarry Smith }
420416022c9SBarry Smith 
42109573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
422cd155464SBarry Smith 
4234a2ae208SSatish Balay #undef __FUNCT__
4244a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
425dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
426416022c9SBarry Smith {
427416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
428dfbe8321SBarry Smith   PetscErrorCode    ierr;
429d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
430e060cb09SBarry Smith   const char        *name;
431f3ef73ceSBarry Smith   PetscViewerFormat format;
43217ab2063SBarry Smith 
4333a40ed3dSBarry Smith   PetscFunctionBegin;
434b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
43571c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
43697f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
437d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
438d00d2cf4SBarry Smith       nofinalvalue = 1;
439d00d2cf4SBarry Smith     }
440d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
441d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
44277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
44377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
444b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
44517ab2063SBarry Smith 
44617ab2063SBarry Smith     for (i=0; i<m; i++) {
447416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
448aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44977431f27SBarry 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);
45017ab2063SBarry Smith #else
45177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
45217ab2063SBarry Smith #endif
45317ab2063SBarry Smith       }
45417ab2063SBarry Smith     }
455d00d2cf4SBarry Smith     if (nofinalvalue) {
456d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
457d00d2cf4SBarry Smith     }
458317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
459fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
460d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
46168369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
462cd155464SBarry Smith      PetscFunctionReturn(0);
463fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
464d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4657566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
46644cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
46777431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
46844cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
469aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
47036db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
471a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
47236db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
473a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
47436db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
475a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4766831982aSBarry Smith         }
47744cd7ae7SLois Curfman McInnes #else
478a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
47944cd7ae7SLois Curfman McInnes #endif
48044cd7ae7SLois Curfman McInnes       }
481b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
48244cd7ae7SLois Curfman McInnes     }
483d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
484fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
48597f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
486d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4877566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
48897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
489496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
490496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
491496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
492496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
493aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
495496be53dSLois Curfman McInnes #else
496496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
497496be53dSLois Curfman McInnes #endif
498496be53dSLois Curfman McInnes         }
499496be53dSLois Curfman McInnes       }
500496be53dSLois Curfman McInnes     }
5012e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
50277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
5032e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
50477431f27SBarry 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);}
50577431f27SBarry 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);}
50677431f27SBarry 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);}
50777431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
50877431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
50977431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
510496be53dSLois Curfman McInnes     }
511b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
512606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
513496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
514496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
51577431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
516496be53dSLois Curfman McInnes       }
517b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
518496be53dSLois Curfman McInnes     }
519b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
520496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
521496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
522496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
523aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
525b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5266831982aSBarry Smith           }
527496be53dSLois Curfman McInnes #else
528b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
529496be53dSLois Curfman McInnes #endif
530496be53dSLois Curfman McInnes         }
531496be53dSLois Curfman McInnes       }
532b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
533496be53dSLois Curfman McInnes     }
534d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
535fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
53697f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
53787828ca2SBarry Smith     PetscScalar value;
53802594712SBarry Smith 
539d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5407566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
54102594712SBarry Smith     for (i=0; i<m; i++) {
54202594712SBarry Smith       jcnt = 0;
543d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
544e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
54502594712SBarry Smith           value = a->a[cnt++];
546e24b481bSBarry Smith           jcnt++;
54702594712SBarry Smith         } else {
54802594712SBarry Smith           value = 0.0;
54902594712SBarry Smith         }
550aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
551b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
55202594712SBarry Smith #else
553b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
55402594712SBarry Smith #endif
55502594712SBarry Smith       }
556b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
55702594712SBarry Smith     }
558d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5593c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
560d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5617566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5623c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5633c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5643c215bfdSMatthew Knepley #else
5653c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5663c215bfdSMatthew Knepley #endif
567d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5683c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5693c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5703c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5713c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
5723c215bfdSMatthew 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);
5733c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
5743c215bfdSMatthew 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);
5753c215bfdSMatthew Knepley         } else {
5763c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5773c215bfdSMatthew Knepley         }
5783c215bfdSMatthew Knepley #else
5793c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
5803c215bfdSMatthew Knepley #endif
5813c215bfdSMatthew Knepley       }
5823c215bfdSMatthew Knepley     }
583d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5843a40ed3dSBarry Smith   } else {
585d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5867566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
587d5f3da31SBarry Smith     if (A->factortype){
58816cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
58916cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
59016cd7e1dSShri Abhyankar         /* L part */
59116cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
59216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
59316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
59416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
59516cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
59616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
59716cd7e1dSShri Abhyankar           } else {
59816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
59916cd7e1dSShri Abhyankar           }
60016cd7e1dSShri Abhyankar #else
60116cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
60216cd7e1dSShri Abhyankar #endif
60316cd7e1dSShri Abhyankar         }
60416cd7e1dSShri Abhyankar 	/* diagonal */
60516cd7e1dSShri Abhyankar 	j = a->diag[i];
60616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
60716cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
60816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
60916cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
61016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
61116cd7e1dSShri Abhyankar           } else {
61216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
61316cd7e1dSShri Abhyankar           }
61416cd7e1dSShri Abhyankar #else
61516cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
61616cd7e1dSShri Abhyankar #endif
61716cd7e1dSShri Abhyankar 
61816cd7e1dSShri Abhyankar 	/* U part */
61916cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
62016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
62116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
62216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62316cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
62416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62516cd7e1dSShri Abhyankar           } else {
62616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
62716cd7e1dSShri Abhyankar           }
62816cd7e1dSShri Abhyankar #else
62916cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
63016cd7e1dSShri Abhyankar #endif
63116cd7e1dSShri Abhyankar }
63216cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
63316cd7e1dSShri Abhyankar         }
63416cd7e1dSShri Abhyankar     } else {
63517ab2063SBarry Smith       for (i=0; i<m; i++) {
63677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
637416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
638aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
63936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
640a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64136db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
642a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6433a40ed3dSBarry Smith           } else {
644a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
64517ab2063SBarry Smith           }
64617ab2063SBarry Smith #else
647a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
64817ab2063SBarry Smith #endif
64917ab2063SBarry Smith         }
650b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
65117ab2063SBarry Smith       }
65216cd7e1dSShri Abhyankar     }
653d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
65417ab2063SBarry Smith   }
655b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6563a40ed3dSBarry Smith   PetscFunctionReturn(0);
657416022c9SBarry Smith }
658416022c9SBarry Smith 
6594a2ae208SSatish Balay #undef __FUNCT__
6604a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
661dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
662416022c9SBarry Smith {
663480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
664416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
665dfbe8321SBarry Smith   PetscErrorCode    ierr;
666d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
66736db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
668b0a32e0cSBarry Smith   PetscViewer       viewer;
669f3ef73ceSBarry Smith   PetscViewerFormat format;
670cddf8d76SBarry Smith 
6713a40ed3dSBarry Smith   PetscFunctionBegin;
672480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
673b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
67419bcc07fSBarry Smith 
675b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
676416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
6770513a670SBarry Smith 
678fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
6790513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
680b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
681416022c9SBarry Smith     for (i=0; i<m; i++) {
682cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
683bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
684bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
685aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
68636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
687cddf8d76SBarry Smith #else
688cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
689cddf8d76SBarry Smith #endif
690b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
691cddf8d76SBarry Smith       }
692cddf8d76SBarry Smith     }
693b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
694cddf8d76SBarry Smith     for (i=0; i<m; i++) {
695cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
696bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
697bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
698cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
699b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
700cddf8d76SBarry Smith       }
701cddf8d76SBarry Smith     }
702b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
703cddf8d76SBarry Smith     for (i=0; i<m; i++) {
704cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
705bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
706bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
707aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
70836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
709cddf8d76SBarry Smith #else
710cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
711cddf8d76SBarry Smith #endif
712b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
713416022c9SBarry Smith       }
714416022c9SBarry Smith     }
7150513a670SBarry Smith   } else {
7160513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7170513a670SBarry Smith     /* first determine max of all nonzero values */
71897f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
719b0a32e0cSBarry Smith     PetscDraw   popup;
72036db0b34SBarry Smith     PetscReal scale;
7210513a670SBarry Smith 
7220513a670SBarry Smith     for (i=0; i<nz; i++) {
7230513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7240513a670SBarry Smith     }
725b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
726b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
727b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
7280513a670SBarry Smith     count = 0;
7290513a670SBarry Smith     for (i=0; i<m; i++) {
7300513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
731bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
732bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
73397f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
734b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7350513a670SBarry Smith         count++;
7360513a670SBarry Smith       }
7370513a670SBarry Smith     }
7380513a670SBarry Smith   }
739480ef9eaSBarry Smith   PetscFunctionReturn(0);
740480ef9eaSBarry Smith }
741cddf8d76SBarry Smith 
7424a2ae208SSatish Balay #undef __FUNCT__
7434a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
744dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
745480ef9eaSBarry Smith {
746dfbe8321SBarry Smith   PetscErrorCode ierr;
747b0a32e0cSBarry Smith   PetscDraw      draw;
74836db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
749ace3abfcSBarry Smith   PetscBool      isnull;
750480ef9eaSBarry Smith 
751480ef9eaSBarry Smith   PetscFunctionBegin;
752b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
753b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
754480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
755480ef9eaSBarry Smith 
756480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
757d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
758480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
759b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
760b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
761480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7623a40ed3dSBarry Smith   PetscFunctionReturn(0);
763416022c9SBarry Smith }
764416022c9SBarry Smith 
7654a2ae208SSatish Balay #undef __FUNCT__
7664a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
767dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
768416022c9SBarry Smith {
769dfbe8321SBarry Smith   PetscErrorCode ierr;
770ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
771416022c9SBarry Smith 
7723a40ed3dSBarry Smith   PetscFunctionBegin;
7732692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
7742692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
7752692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
776c45a1595SBarry Smith   if (iascii) {
7773a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
7780f5bd95cSBarry Smith   } else if (isbinary) {
7793a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
7800f5bd95cSBarry Smith   } else if (isdraw) {
7813a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
7825cd90555SBarry Smith   } else {
783e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
78417ab2063SBarry Smith   }
7854108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
7863a40ed3dSBarry Smith   PetscFunctionReturn(0);
78717ab2063SBarry Smith }
78819bcc07fSBarry Smith 
7894a2ae208SSatish Balay #undef __FUNCT__
7904a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
791dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
79217ab2063SBarry Smith {
793416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7946849ba73SBarry Smith   PetscErrorCode ierr;
79597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
796d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
79754f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
7983447b6efSHong Zhang   PetscReal      ratio=0.6;
79917ab2063SBarry Smith 
8003a40ed3dSBarry Smith   PetscFunctionBegin;
8013a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
80217ab2063SBarry Smith 
80343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
80417ab2063SBarry Smith   for (i=1; i<m; i++) {
805416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
80617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
80794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
80817ab2063SBarry Smith     if (fshift) {
809bfeeae90SHong Zhang       ip = aj + ai[i] ;
810bfeeae90SHong Zhang       ap = aa + ai[i] ;
81117ab2063SBarry Smith       N  = ailen[i];
81217ab2063SBarry Smith       for (j=0; j<N; j++) {
81317ab2063SBarry Smith         ip[j-fshift] = ip[j];
81417ab2063SBarry Smith         ap[j-fshift] = ap[j];
81517ab2063SBarry Smith       }
81617ab2063SBarry Smith     }
81717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
81817ab2063SBarry Smith   }
81917ab2063SBarry Smith   if (m) {
82017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
82117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
82217ab2063SBarry Smith   }
82317ab2063SBarry Smith   /* reset ilen and imax for each row */
82417ab2063SBarry Smith   for (i=0; i<m; i++) {
82517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
82617ab2063SBarry Smith   }
827bfeeae90SHong Zhang   a->nz = ai[m];
82865e19b50SBarry Smith   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
82917ab2063SBarry Smith 
83009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
831d0f46423SBarry 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);
832ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
833ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8348e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
835dd5f02e7SSatish Balay   a->reallocs          = 0;
8364e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
83736db0b34SBarry Smith   a->rmax              = rmax;
8384e220ebcSLois Curfman McInnes 
839cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
84088e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
84171c2f376SKris Buschelman 
8424108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
84371f1c65dSBarry Smith 
84471f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
8453a40ed3dSBarry Smith   PetscFunctionReturn(0);
84617ab2063SBarry Smith }
84717ab2063SBarry Smith 
8484a2ae208SSatish Balay #undef __FUNCT__
84999cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
85099cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
85199cafbc1SBarry Smith {
85299cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
85399cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
85454f21887SBarry Smith   MatScalar      *aa = a->a;
85599cafbc1SBarry Smith 
85699cafbc1SBarry Smith   PetscFunctionBegin;
85799cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
85899cafbc1SBarry Smith   PetscFunctionReturn(0);
85999cafbc1SBarry Smith }
86099cafbc1SBarry Smith 
86199cafbc1SBarry Smith #undef __FUNCT__
86299cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
86399cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
86499cafbc1SBarry Smith {
86599cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
86699cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
86754f21887SBarry Smith   MatScalar      *aa = a->a;
86899cafbc1SBarry Smith 
86999cafbc1SBarry Smith   PetscFunctionBegin;
87099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
87199cafbc1SBarry Smith   PetscFunctionReturn(0);
87299cafbc1SBarry Smith }
87399cafbc1SBarry Smith 
87499cafbc1SBarry Smith #undef __FUNCT__
8754a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
876dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
87717ab2063SBarry Smith {
878416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
879dfbe8321SBarry Smith   PetscErrorCode ierr;
8803a40ed3dSBarry Smith 
8813a40ed3dSBarry Smith   PetscFunctionBegin;
882d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
8833a40ed3dSBarry Smith   PetscFunctionReturn(0);
88417ab2063SBarry Smith }
885416022c9SBarry Smith 
8864a2ae208SSatish Balay #undef __FUNCT__
8874a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
888dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
88917ab2063SBarry Smith {
890416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
891dfbe8321SBarry Smith   PetscErrorCode ierr;
892d5d45c9bSBarry Smith 
8933a40ed3dSBarry Smith   PetscFunctionBegin;
894aa482453SBarry Smith #if defined(PETSC_USE_LOG)
895d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
89617ab2063SBarry Smith #endif
897e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
8986bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
8996bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
90005b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
90105b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
90271f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
90305b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9046bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
90505b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9066bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
90705b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9086bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
909cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
910a30b2313SHong Zhang 
9114108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
912bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
913901853e0SKris Buschelman 
914dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
915901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
916901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
917901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
918901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
919901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
9205a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
921901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
922901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
923a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
924901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9253a40ed3dSBarry Smith   PetscFunctionReturn(0);
92617ab2063SBarry Smith }
92717ab2063SBarry Smith 
9284a2ae208SSatish Balay #undef __FUNCT__
9294a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
930ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
93117ab2063SBarry Smith {
932416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9334846f1f5SKris Buschelman   PetscErrorCode ierr;
9343a40ed3dSBarry Smith 
9353a40ed3dSBarry Smith   PetscFunctionBegin;
936a65d3064SKris Buschelman   switch (op) {
937a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
9384e0d8c25SBarry Smith       a->roworiented       = flg;
939a65d3064SKris Buschelman       break;
940a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
941a9817697SBarry Smith       a->keepnonzeropattern    = flg;
942a65d3064SKris Buschelman       break;
943512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
944512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
945a65d3064SKris Buschelman       break;
946a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
9474e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
948a65d3064SKris Buschelman       break;
949a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
9504e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
951a65d3064SKris Buschelman       break;
95228b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
95328b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
95428b2fa4aSMatthew Knepley       break;
955a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
9564e0d8c25SBarry Smith       a->ignorezeroentries = flg;
9570df259c2SBarry Smith       break;
958cd6b891eSBarry Smith     case MAT_CHECK_COMPRESSED_ROW:
959cd6b891eSBarry Smith       a->compressedrow.check = flg;
960d487561eSHong Zhang       break;
9613d472b54SHong Zhang     case MAT_SPD:
9623d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
9633d472b54SHong Zhang       A->spd                             = flg;
9643d472b54SHong Zhang       if (flg) {
9653d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
9663d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
9673d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
9683d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
9693d472b54SHong Zhang       }
9703d472b54SHong Zhang       break;
971b1646e73SJed Brown     case MAT_SYMMETRIC:
972b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
973b1646e73SJed Brown     case MAT_HERMITIAN:
974b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
9754e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
976a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
977a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
978290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
979a65d3064SKris Buschelman       break;
980b87ac2d8SJed Brown     case MAT_USE_INODES:
981b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
982b87ac2d8SJed Brown       break;
983a65d3064SKris Buschelman     default:
984e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
985a65d3064SKris Buschelman   }
9864108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
9873a40ed3dSBarry Smith   PetscFunctionReturn(0);
98817ab2063SBarry Smith }
98917ab2063SBarry Smith 
9904a2ae208SSatish Balay #undef __FUNCT__
9914a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
992dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
99317ab2063SBarry Smith {
994416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9956849ba73SBarry Smith   PetscErrorCode ierr;
996d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
99735e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
99817ab2063SBarry Smith 
9993a40ed3dSBarry Smith   PetscFunctionBegin;
1000d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1001e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
100235e7444dSHong Zhang 
1003d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
1004d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
100535e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
100635e7444dSHong Zhang     for (i=0; i<n; i++) x[i] = aa[diag[i]];
100735e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
100835e7444dSHong Zhang     PetscFunctionReturn(0);
100935e7444dSHong Zhang   }
101035e7444dSHong Zhang 
10112dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10121ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
101335e7444dSHong Zhang   for (i=0; i<n; i++) {
101435e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10152f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
101635e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
101735e7444dSHong Zhang       if (aj[j] == i) {
101835e7444dSHong Zhang         x[i] = aa[j];
101917ab2063SBarry Smith         break;
102017ab2063SBarry Smith       }
102117ab2063SBarry Smith     }
102217ab2063SBarry Smith   }
10231ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10243a40ed3dSBarry Smith   PetscFunctionReturn(0);
102517ab2063SBarry Smith }
102617ab2063SBarry Smith 
1027c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10284a2ae208SSatish Balay #undef __FUNCT__
10294a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1030dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
103117ab2063SBarry Smith {
1032416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10335c897100SBarry Smith   PetscScalar       *x,*y;
1034dfbe8321SBarry Smith   PetscErrorCode    ierr;
1035d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10365c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1037a77337e4SBarry Smith   MatScalar         *v;
1038a77337e4SBarry Smith   PetscScalar       alpha;
103904fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10403447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1041ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10425c897100SBarry Smith #endif
104317ab2063SBarry Smith 
10443a40ed3dSBarry Smith   PetscFunctionBegin;
10452e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
10461ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10471ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10485c897100SBarry Smith 
10495c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1050bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
10515c897100SBarry Smith #else
10523447b6efSHong Zhang   if (usecprow){
10533447b6efSHong Zhang     m    = cprow.nrows;
10543447b6efSHong Zhang     ii   = cprow.i;
10557b2bb3b9SHong Zhang     ridx = cprow.rindex;
10563447b6efSHong Zhang   } else {
10573447b6efSHong Zhang     ii = a->i;
10583447b6efSHong Zhang   }
105917ab2063SBarry Smith   for (i=0; i<m; i++) {
10603447b6efSHong Zhang     idx   = a->j + ii[i] ;
10613447b6efSHong Zhang     v     = a->a + ii[i] ;
10623447b6efSHong Zhang     n     = ii[i+1] - ii[i];
10633447b6efSHong Zhang     if (usecprow){
10647b2bb3b9SHong Zhang       alpha = x[ridx[i]];
10653447b6efSHong Zhang     } else {
106617ab2063SBarry Smith       alpha = x[i];
10673447b6efSHong Zhang     }
106804fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
106917ab2063SBarry Smith   }
10705c897100SBarry Smith #endif
1071dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10721ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10731ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10743a40ed3dSBarry Smith   PetscFunctionReturn(0);
107517ab2063SBarry Smith }
107617ab2063SBarry Smith 
10774a2ae208SSatish Balay #undef __FUNCT__
10785c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1079dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
10805c897100SBarry Smith {
1081dfbe8321SBarry Smith   PetscErrorCode ierr;
10825c897100SBarry Smith 
10835c897100SBarry Smith   PetscFunctionBegin;
1084170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
10855c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
10865c897100SBarry Smith   PetscFunctionReturn(0);
10875c897100SBarry Smith }
10885c897100SBarry Smith 
1089c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10905c897100SBarry Smith #undef __FUNCT__
10914a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1092dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
109317ab2063SBarry Smith {
1094416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1095d9fead3dSBarry Smith   PetscScalar       *y;
109654f21887SBarry Smith   const PetscScalar *x;
109754f21887SBarry Smith   const MatScalar   *aa;
1098dfbe8321SBarry Smith   PetscErrorCode    ierr;
1099003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1100003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
11018aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1102362ced78SSatish Balay   PetscScalar       sum;
1103ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
110417ab2063SBarry Smith 
1105b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
110697952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1107fee21e36SBarry Smith #endif
1108fee21e36SBarry Smith 
11093a40ed3dSBarry Smith   PetscFunctionBegin;
11103649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
11111ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
111297952fefSHong Zhang   aj  = a->j;
111397952fefSHong Zhang   aa  = a->a;
1114416022c9SBarry Smith   ii  = a->i;
11154eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
111697952fefSHong Zhang     m    = a->compressedrow.nrows;
111797952fefSHong Zhang     ii   = a->compressedrow.i;
111897952fefSHong Zhang     ridx = a->compressedrow.rindex;
111997952fefSHong Zhang     for (i=0; i<m; i++){
112097952fefSHong Zhang       n   = ii[i+1] - ii[i];
112197952fefSHong Zhang       aj  = a->j + ii[i];
112297952fefSHong Zhang       aa  = a->a + ii[i];
112397952fefSHong Zhang       sum = 0.0;
1124a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1125003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1126003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
112797952fefSHong Zhang       y[*ridx++] = sum;
112897952fefSHong Zhang     }
112997952fefSHong Zhang   } else { /* do not use compressed row format */
1130b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1131b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1132b05257ddSBarry Smith #else
113317ab2063SBarry Smith     for (i=0; i<m; i++) {
1134003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1135003131ecSBarry Smith       aj  = a->j + ii[i];
1136003131ecSBarry Smith       aa  = a->a + ii[i];
113717ab2063SBarry Smith       sum  = 0.0;
1138a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1139003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
114017ab2063SBarry Smith       y[i] = sum;
114117ab2063SBarry Smith     }
11428d195f9aSBarry Smith #endif
1143b05257ddSBarry Smith   }
1144dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
11453649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
11461ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11473a40ed3dSBarry Smith   PetscFunctionReturn(0);
114817ab2063SBarry Smith }
114917ab2063SBarry Smith 
1150c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
11514a2ae208SSatish Balay #undef __FUNCT__
11524a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1153dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
115417ab2063SBarry Smith {
1155416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
115654f21887SBarry Smith   PetscScalar     *x,*y,*z;
115754f21887SBarry Smith   const MatScalar *aa;
1158dfbe8321SBarry Smith   PetscErrorCode  ierr;
1159d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1160aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
116197952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1162362ced78SSatish Balay   PetscScalar     sum;
1163ace3abfcSBarry Smith   PetscBool       usecprow=a->compressedrow.use;
1164e36a17ebSSatish Balay #endif
11659ea0dfa2SSatish Balay 
11663a40ed3dSBarry Smith   PetscFunctionBegin;
11671ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
11681ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11692e8a6d31SBarry Smith   if (zz != yy) {
11701ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
11712e8a6d31SBarry Smith   } else {
11722e8a6d31SBarry Smith     z = y;
11732e8a6d31SBarry Smith   }
1174bfeeae90SHong Zhang 
117597952fefSHong Zhang   aj  = a->j;
117697952fefSHong Zhang   aa  = a->a;
1177cddf8d76SBarry Smith   ii  = a->i;
1178aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
117997952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
118002ab625aSSatish Balay #else
11814eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
11824eb6d288SHong Zhang     if (zz != yy){
11834eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
11844eb6d288SHong Zhang     }
118597952fefSHong Zhang     m    = a->compressedrow.nrows;
118697952fefSHong Zhang     ii   = a->compressedrow.i;
118797952fefSHong Zhang     ridx = a->compressedrow.rindex;
118897952fefSHong Zhang     for (i=0; i<m; i++){
118997952fefSHong Zhang       n  = ii[i+1] - ii[i];
119097952fefSHong Zhang       aj  = a->j + ii[i];
119197952fefSHong Zhang       aa  = a->a + ii[i];
119297952fefSHong Zhang       sum = y[*ridx];
119397952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
119497952fefSHong Zhang       z[*ridx++] = sum;
119597952fefSHong Zhang     }
119697952fefSHong Zhang   } else { /* do not use compressed row format */
119717ab2063SBarry Smith     for (i=0; i<m; i++) {
11989ea0dfa2SSatish Balay       jrow = ii[i];
11999ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
120017ab2063SBarry Smith       sum  = y[i];
12019ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
120297952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
12039ea0dfa2SSatish Balay       }
120417ab2063SBarry Smith       z[i] = sum;
120517ab2063SBarry Smith     }
120697952fefSHong Zhang   }
120702ab625aSSatish Balay #endif
1208dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
12091ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12101ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12112e8a6d31SBarry Smith   if (zz != yy) {
12121ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
12132e8a6d31SBarry Smith   }
12148154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
12156b375ea7SVictor Minden   /*
1216918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1217918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1218918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
12196b375ea7SVictor Minden   */
1220918e98c3SVictor Minden #endif
12213a40ed3dSBarry Smith   PetscFunctionReturn(0);
122217ab2063SBarry Smith }
122317ab2063SBarry Smith 
122417ab2063SBarry Smith /*
122517ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
122617ab2063SBarry Smith */
12274a2ae208SSatish Balay #undef __FUNCT__
12284a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1229dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
123017ab2063SBarry Smith {
1231416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12326849ba73SBarry Smith   PetscErrorCode ierr;
1233d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
123417ab2063SBarry Smith 
12353a40ed3dSBarry Smith   PetscFunctionBegin;
123609f38230SBarry Smith   if (!a->diag) {
123709f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
12389518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
123909f38230SBarry Smith   }
1240d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
124109f38230SBarry Smith     a->diag[i] = a->i[i+1];
1242bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1243bfeeae90SHong Zhang       if (a->j[j] == i) {
124409f38230SBarry Smith         a->diag[i] = j;
124517ab2063SBarry Smith         break;
124617ab2063SBarry Smith       }
124717ab2063SBarry Smith     }
124817ab2063SBarry Smith   }
12493a40ed3dSBarry Smith   PetscFunctionReturn(0);
125017ab2063SBarry Smith }
125117ab2063SBarry Smith 
1252be5855fcSBarry Smith /*
1253be5855fcSBarry Smith      Checks for missing diagonals
1254be5855fcSBarry Smith */
12554a2ae208SSatish Balay #undef __FUNCT__
12564a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1257ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1258be5855fcSBarry Smith {
1259be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
126097f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1261be5855fcSBarry Smith 
1262be5855fcSBarry Smith   PetscFunctionBegin;
126309f38230SBarry Smith   *missing = PETSC_FALSE;
1264d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
126509f38230SBarry Smith     *missing  = PETSC_TRUE;
126609f38230SBarry Smith     if (d) *d = 0;
126709f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
126809f38230SBarry Smith   } else {
1269f1e2ffcdSBarry Smith     diag = a->diag;
1270d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1271bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
127209f38230SBarry Smith 	*missing = PETSC_TRUE;
127309f38230SBarry Smith 	if (d) *d = i;
127409f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
127509f38230SBarry Smith       }
1276be5855fcSBarry Smith     }
1277be5855fcSBarry Smith   }
1278be5855fcSBarry Smith   PetscFunctionReturn(0);
1279be5855fcSBarry Smith }
1280be5855fcSBarry Smith 
128171f1c65dSBarry Smith EXTERN_C_BEGIN
128271f1c65dSBarry Smith #undef __FUNCT__
128371f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
12847087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
128571f1c65dSBarry Smith {
128671f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
128771f1c65dSBarry Smith   PetscErrorCode ierr;
1288d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
128954f21887SBarry Smith   MatScalar      *v = a->a;
129054f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
129171f1c65dSBarry Smith 
129271f1c65dSBarry Smith   PetscFunctionBegin;
129371f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
129471f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
129571f1c65dSBarry Smith   diag = a->diag;
129671f1c65dSBarry Smith   if (!a->idiag) {
129771f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
129871f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
129971f1c65dSBarry Smith     v        = a->a;
130071f1c65dSBarry Smith   }
130171f1c65dSBarry Smith   mdiag = a->mdiag;
130271f1c65dSBarry Smith   idiag = a->idiag;
130371f1c65dSBarry Smith 
1304028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
130571f1c65dSBarry Smith     for (i=0; i<m; i++) {
130671f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1307e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
130871f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
130971f1c65dSBarry Smith     }
131071f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
131171f1c65dSBarry Smith   } else {
131271f1c65dSBarry Smith     for (i=0; i<m; i++) {
131371f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
131471f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
131571f1c65dSBarry Smith     }
1316dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
131771f1c65dSBarry Smith   }
131871f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
131971f1c65dSBarry Smith   PetscFunctionReturn(0);
132071f1c65dSBarry Smith }
13215a9745a3SMatthew Knepley EXTERN_C_END
132271f1c65dSBarry Smith 
1323c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
13244a2ae208SSatish Balay #undef __FUNCT__
132541f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
132641f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
132717ab2063SBarry Smith {
1328416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1329e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1330e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
133154f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1332dfbe8321SBarry Smith   PetscErrorCode     ierr;
1333d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
133497f1f81fSBarry Smith   const PetscInt     *idx,*diag;
133517ab2063SBarry Smith 
13363a40ed3dSBarry Smith   PetscFunctionBegin;
1337b965ef7fSBarry Smith   its = its*lits;
133891723122SBarry Smith 
133971f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
134071f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
134171f1c65dSBarry Smith   a->fshift = fshift;
134271f1c65dSBarry Smith   a->omega  = omega;
1343ed480e8bSBarry Smith 
134471f1c65dSBarry Smith   diag = a->diag;
134571f1c65dSBarry Smith   t     = a->ssor_work;
1346ed480e8bSBarry Smith   idiag = a->idiag;
134771f1c65dSBarry Smith   mdiag = a->mdiag;
1348ed480e8bSBarry Smith 
13491ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
13503649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
135171f1c65dSBarry Smith   CHKMEMQ;
1352ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
135317ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
135417ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1355ed480e8bSBarry Smith     bs = b;
135617ab2063SBarry Smith     for (i=0; i<m; i++) {
135771f1c65dSBarry Smith         d    = fshift + mdiag[i];
1358416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1359ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1360ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
136117ab2063SBarry Smith         sum  = b[i]*d/omega;
1362003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
136317ab2063SBarry Smith         x[i] = sum;
136417ab2063SBarry Smith     }
13651ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13663649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1367efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13683a40ed3dSBarry Smith     PetscFunctionReturn(0);
136917ab2063SBarry Smith   }
1370c783ea89SBarry Smith 
137148af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1372e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
13733a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
137417ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1375887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
137617ab2063SBarry Smith 
137717ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
137817ab2063SBarry Smith 
1379887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
138017ab2063SBarry Smith     */
138117ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
138217ab2063SBarry Smith 
138317ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
138417ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1385416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1386ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1387ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
138817ab2063SBarry Smith       sum  = b[i];
1389e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1390ed480e8bSBarry Smith       x[i] = sum*idiag[i];
139117ab2063SBarry Smith     }
139217ab2063SBarry Smith 
139317ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1394416022c9SBarry Smith     v = a->a;
1395ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
139617ab2063SBarry Smith 
139717ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1398ed480e8bSBarry Smith     ts = t;
1399416022c9SBarry Smith     diag = a->diag;
140017ab2063SBarry Smith     for (i=0; i<m; i++) {
1401416022c9SBarry Smith       n    = diag[i] - a->i[i];
1402ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1403ed480e8bSBarry Smith       v    = a->a + a->i[i];
140417ab2063SBarry Smith       sum  = t[i];
1405003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1406ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1407733d66baSBarry Smith       /*  x = x + t */
1408733d66baSBarry Smith       x[i] += t[i];
140917ab2063SBarry Smith     }
141017ab2063SBarry Smith 
1411dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
14121ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
14133649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
14143a40ed3dSBarry Smith     PetscFunctionReturn(0);
141517ab2063SBarry Smith   }
141617ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
141717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
141817ab2063SBarry Smith       for (i=0; i<m; i++) {
1419416022c9SBarry Smith         n    = diag[i] - a->i[i];
1420ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1421ed480e8bSBarry Smith         v    = a->a + a->i[i];
142217ab2063SBarry Smith         sum  = b[i];
1423e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
14245c99c7daSBarry Smith         t[i] = sum;
1425ed480e8bSBarry Smith         x[i] = sum*idiag[i];
142617ab2063SBarry Smith       }
14275c99c7daSBarry Smith       xb = t;
1428efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
14293a40ed3dSBarry Smith     } else xb = b;
143017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
143117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1432416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1433ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1434ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
143517ab2063SBarry Smith         sum  = xb[i];
1436e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
14375c99c7daSBarry Smith         if (xb == b) {
1438ed480e8bSBarry Smith           x[i] = sum*idiag[i];
14395c99c7daSBarry Smith         } else {
14405c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
144117ab2063SBarry Smith         }
14425c99c7daSBarry Smith       }
1443efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
144417ab2063SBarry Smith     }
144517ab2063SBarry Smith     its--;
144617ab2063SBarry Smith   }
144717ab2063SBarry Smith   while (its--) {
144817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
144917ab2063SBarry Smith       for (i=0; i<m; i++) {
1450416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1451ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1452ed480e8bSBarry Smith         v    = a->a + a->i[i];
145317ab2063SBarry Smith         sum  = b[i];
1454e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1455ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
145617ab2063SBarry Smith       }
14579f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
145817ab2063SBarry Smith     }
145917ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
146017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1461416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1462ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1463ed480e8bSBarry Smith         v    = a->a + a->i[i];
146417ab2063SBarry Smith         sum  = b[i];
1465e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1466ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
146717ab2063SBarry Smith       }
14689f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
146917ab2063SBarry Smith     }
147017ab2063SBarry Smith   }
14711ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
14723649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
147371f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
147417ab2063SBarry Smith }
147517ab2063SBarry Smith 
14762af78befSBarry Smith 
14774a2ae208SSatish Balay #undef __FUNCT__
14784a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1479dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
148017ab2063SBarry Smith {
1481416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
14824e220ebcSLois Curfman McInnes 
14833a40ed3dSBarry Smith   PetscFunctionBegin;
14844e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
14854e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
14864e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
14874e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
14884e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
14898e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
14907adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1491d5f3da31SBarry Smith   if (A->factortype) {
14924e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
14934e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
14944e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
14954e220ebcSLois Curfman McInnes   } else {
14964e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
14974e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
14984e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
14994e220ebcSLois Curfman McInnes   }
15003a40ed3dSBarry Smith   PetscFunctionReturn(0);
150117ab2063SBarry Smith }
150217ab2063SBarry Smith 
15034a2ae208SSatish Balay #undef __FUNCT__
15044a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
15052b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
150617ab2063SBarry Smith {
1507416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
15083b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
15096849ba73SBarry Smith   PetscErrorCode    ierr;
151097b48c8fSBarry Smith   const PetscScalar *xx;
151197b48c8fSBarry Smith   PetscScalar       *bb;
1512ace3abfcSBarry Smith   PetscBool         missing;
151317ab2063SBarry Smith 
15143a40ed3dSBarry Smith   PetscFunctionBegin;
151597b48c8fSBarry Smith   if (x && b) {
151697b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
151797b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
151897b48c8fSBarry Smith     for (i=0; i<N; i++) {
151997b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
152097b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
152197b48c8fSBarry Smith     }
152297b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
152397b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
152497b48c8fSBarry Smith   }
152597b48c8fSBarry Smith 
1526a9817697SBarry Smith   if (a->keepnonzeropattern) {
1527f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1528e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1529bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1530f1e2ffcdSBarry Smith     }
1531f4df32b1SMatthew Knepley     if (diag != 0.0) {
153209f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1533e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1534f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1535f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1536f1e2ffcdSBarry Smith       }
1537f1e2ffcdSBarry Smith     }
153888e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1539f1e2ffcdSBarry Smith   } else {
1540f4df32b1SMatthew Knepley     if (diag != 0.0) {
154117ab2063SBarry Smith       for (i=0; i<N; i++) {
1542e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15437ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1544416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1545f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1546bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
15477ae801bdSBarry Smith         } else { /* in case row was completely empty */
1548f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
154917ab2063SBarry Smith         }
155017ab2063SBarry Smith       }
15513a40ed3dSBarry Smith     } else {
155217ab2063SBarry Smith       for (i=0; i<N; i++) {
1553e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1554416022c9SBarry Smith         a->ilen[rows[i]] = 0;
155517ab2063SBarry Smith       }
155617ab2063SBarry Smith     }
155788e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1558f1e2ffcdSBarry Smith   }
155943a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15603a40ed3dSBarry Smith   PetscFunctionReturn(0);
156117ab2063SBarry Smith }
156217ab2063SBarry Smith 
15634a2ae208SSatish Balay #undef __FUNCT__
15646e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
15656e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
15666e169961SBarry Smith {
15676e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
15686e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
15696e169961SBarry Smith   PetscErrorCode    ierr;
15702b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
15716e169961SBarry Smith   const PetscScalar *xx;
15726e169961SBarry Smith   PetscScalar       *bb;
15736e169961SBarry Smith 
15746e169961SBarry Smith   PetscFunctionBegin;
15756e169961SBarry Smith   if (x && b) {
15766e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
15776e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
15782b40b63fSBarry Smith     vecs = PETSC_TRUE;
15796e169961SBarry Smith   }
15806e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
15816e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
15826e169961SBarry Smith   for (i=0; i<N; i++) {
15836e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15846e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
15856e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
15866e169961SBarry Smith   }
15876e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
15886e169961SBarry Smith     if (!zeroed[i]) {
15896e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
15906e169961SBarry Smith         if (zeroed[a->j[j]]) {
15912b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
15926e169961SBarry Smith           a->a[j] = 0.0;
15936e169961SBarry Smith         }
15946e169961SBarry Smith       }
15952b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
15966e169961SBarry Smith   }
15976e169961SBarry Smith   if (x && b) {
15986e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
15996e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
16006e169961SBarry Smith   }
16016e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
16026e169961SBarry Smith   if (diag != 0.0) {
16036e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
16046e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
16056e169961SBarry Smith     for (i=0; i<N; i++) {
16066e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
16076e169961SBarry Smith     }
16086e169961SBarry Smith   }
16096e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
16106e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16116e169961SBarry Smith   PetscFunctionReturn(0);
16126e169961SBarry Smith }
16136e169961SBarry Smith 
16146e169961SBarry Smith #undef __FUNCT__
16154a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1616a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
161717ab2063SBarry Smith {
1618416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
161997f1f81fSBarry Smith   PetscInt   *itmp;
162017ab2063SBarry Smith 
16213a40ed3dSBarry Smith   PetscFunctionBegin;
1622e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
162317ab2063SBarry Smith 
1624416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1625bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
162617ab2063SBarry Smith   if (idx) {
1627bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1628bfeeae90SHong Zhang     if (*nz) {
16294e093b46SBarry Smith       *idx = itmp;
163017ab2063SBarry Smith     }
163117ab2063SBarry Smith     else *idx = 0;
163217ab2063SBarry Smith   }
16333a40ed3dSBarry Smith   PetscFunctionReturn(0);
163417ab2063SBarry Smith }
163517ab2063SBarry Smith 
1636bfeeae90SHong Zhang /* remove this function? */
16374a2ae208SSatish Balay #undef __FUNCT__
16384a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1639a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
164017ab2063SBarry Smith {
16413a40ed3dSBarry Smith   PetscFunctionBegin;
16423a40ed3dSBarry Smith   PetscFunctionReturn(0);
164317ab2063SBarry Smith }
164417ab2063SBarry Smith 
16454a2ae208SSatish Balay #undef __FUNCT__
16464a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1647dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
164817ab2063SBarry Smith {
1649416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
165054f21887SBarry Smith   MatScalar      *v = a->a;
165136db0b34SBarry Smith   PetscReal      sum = 0.0;
16526849ba73SBarry Smith   PetscErrorCode ierr;
165397f1f81fSBarry Smith   PetscInt       i,j;
165417ab2063SBarry Smith 
16553a40ed3dSBarry Smith   PetscFunctionBegin;
165617ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1657416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1658aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
165936db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
166017ab2063SBarry Smith #else
166117ab2063SBarry Smith       sum += (*v)*(*v); v++;
166217ab2063SBarry Smith #endif
166317ab2063SBarry Smith     }
1664064f8208SBarry Smith     *nrm = sqrt(sum);
16653a40ed3dSBarry Smith   } else if (type == NORM_1) {
166636db0b34SBarry Smith     PetscReal *tmp;
166797f1f81fSBarry Smith     PetscInt    *jj = a->j;
1668d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1669d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1670064f8208SBarry Smith     *nrm = 0.0;
1671416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1672bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
167317ab2063SBarry Smith     }
1674d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1675064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
167617ab2063SBarry Smith     }
1677606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
16783a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1679064f8208SBarry Smith     *nrm = 0.0;
1680d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1681bfeeae90SHong Zhang       v = a->a + a->i[j];
168217ab2063SBarry Smith       sum = 0.0;
1683416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1684cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
168517ab2063SBarry Smith       }
1686064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
168717ab2063SBarry Smith     }
16883a40ed3dSBarry Smith   } else {
1689e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
169017ab2063SBarry Smith   }
16913a40ed3dSBarry Smith   PetscFunctionReturn(0);
169217ab2063SBarry Smith }
169317ab2063SBarry Smith 
16944a2ae208SSatish Balay #undef __FUNCT__
16954a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1696fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
169717ab2063SBarry Smith {
1698416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1699416022c9SBarry Smith   Mat            C;
17006849ba73SBarry Smith   PetscErrorCode ierr;
1701d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
170254f21887SBarry Smith   MatScalar      *array = a->a;
170317ab2063SBarry Smith 
17043a40ed3dSBarry Smith   PetscFunctionBegin;
1705e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1706fc4dec0aSBarry Smith 
1707fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1708d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1709d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1710bfeeae90SHong Zhang 
1711bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
17127adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1713d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
17147adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1715ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1716606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1717a541d17aSBarry Smith   } else {
1718a541d17aSBarry Smith     C = *B;
1719a541d17aSBarry Smith   }
1720a541d17aSBarry Smith 
172117ab2063SBarry Smith   for (i=0; i<m; i++) {
172217ab2063SBarry Smith     len    = ai[i+1]-ai[i];
172387d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1724b9b97703SBarry Smith     array += len;
1725b9b97703SBarry Smith     aj    += len;
172617ab2063SBarry Smith   }
17276d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17286d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
172917ab2063SBarry Smith 
1730815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1731416022c9SBarry Smith     *B = C;
173217ab2063SBarry Smith   } else {
1733eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
173417ab2063SBarry Smith   }
17353a40ed3dSBarry Smith   PetscFunctionReturn(0);
173617ab2063SBarry Smith }
173717ab2063SBarry Smith 
1738cd0d46ebSvictorle EXTERN_C_BEGIN
1739cd0d46ebSvictorle #undef __FUNCT__
17405fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
17417087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1742cd0d46ebSvictorle {
1743cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
174454f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
174554f21887SBarry Smith   MatScalar      *va,*vb;
17466849ba73SBarry Smith   PetscErrorCode ierr;
174797f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1748cd0d46ebSvictorle 
1749cd0d46ebSvictorle   PetscFunctionBegin;
1750cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1751cd0d46ebSvictorle 
1752cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1753cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
17545485867bSBarry Smith   if (ma!=nb || na!=mb){
17555485867bSBarry Smith     *f = PETSC_FALSE;
17565485867bSBarry Smith     PetscFunctionReturn(0);
17575485867bSBarry Smith   }
1758cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1759cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1760cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
176197f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
176297f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1763cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1764cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1765cd0d46ebSvictorle 
1766cd0d46ebSvictorle   *f = PETSC_TRUE;
1767cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1768cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
176997f1f81fSBarry Smith       PetscInt         idc,idr;
17705485867bSBarry Smith       PetscScalar vc,vr;
1771cd0d46ebSvictorle       /* column/row index/value */
17725485867bSBarry Smith       idc = adx[aptr[i]];
17735485867bSBarry Smith       idr = bdx[bptr[idc]];
17745485867bSBarry Smith       vc  = va[aptr[i]];
17755485867bSBarry Smith       vr  = vb[bptr[idc]];
17765485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
17775485867bSBarry Smith         *f = PETSC_FALSE;
17785485867bSBarry Smith         goto done;
1779cd0d46ebSvictorle       } else {
17805485867bSBarry Smith         aptr[i]++;
17815485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1782cd0d46ebSvictorle       }
1783cd0d46ebSvictorle     }
1784cd0d46ebSvictorle   }
1785cd0d46ebSvictorle  done:
1786cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
17873aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1788cd0d46ebSvictorle   PetscFunctionReturn(0);
1789cd0d46ebSvictorle }
1790cd0d46ebSvictorle EXTERN_C_END
1791cd0d46ebSvictorle 
17921cbb95d3SBarry Smith EXTERN_C_BEGIN
17931cbb95d3SBarry Smith #undef __FUNCT__
17941cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
17957087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
17961cbb95d3SBarry Smith {
17971cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
179854f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
179954f21887SBarry Smith   MatScalar      *va,*vb;
18001cbb95d3SBarry Smith   PetscErrorCode ierr;
18011cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
18021cbb95d3SBarry Smith 
18031cbb95d3SBarry Smith   PetscFunctionBegin;
18041cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
18051cbb95d3SBarry Smith 
18061cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
18071cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
18081cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
18091cbb95d3SBarry Smith     *f = PETSC_FALSE;
18101cbb95d3SBarry Smith     PetscFunctionReturn(0);
18111cbb95d3SBarry Smith   }
18121cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
18131cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
18141cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
18151cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
18161cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
18171cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
18181cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
18191cbb95d3SBarry Smith 
18201cbb95d3SBarry Smith   *f = PETSC_TRUE;
18211cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
18221cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
18231cbb95d3SBarry Smith       PetscInt         idc,idr;
18241cbb95d3SBarry Smith       PetscScalar vc,vr;
18251cbb95d3SBarry Smith       /* column/row index/value */
18261cbb95d3SBarry Smith       idc = adx[aptr[i]];
18271cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
18281cbb95d3SBarry Smith       vc  = va[aptr[i]];
18291cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
18301cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
18311cbb95d3SBarry Smith         *f = PETSC_FALSE;
18321cbb95d3SBarry Smith         goto done;
18331cbb95d3SBarry Smith       } else {
18341cbb95d3SBarry Smith         aptr[i]++;
18351cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
18361cbb95d3SBarry Smith       }
18371cbb95d3SBarry Smith     }
18381cbb95d3SBarry Smith   }
18391cbb95d3SBarry Smith  done:
18401cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
18411cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
18421cbb95d3SBarry Smith   PetscFunctionReturn(0);
18431cbb95d3SBarry Smith }
18441cbb95d3SBarry Smith EXTERN_C_END
18451cbb95d3SBarry Smith 
18469e29f15eSvictorle #undef __FUNCT__
18479e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1848ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
18499e29f15eSvictorle {
1850dfbe8321SBarry Smith   PetscErrorCode ierr;
18519e29f15eSvictorle   PetscFunctionBegin;
18525485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
18539e29f15eSvictorle   PetscFunctionReturn(0);
18549e29f15eSvictorle }
18559e29f15eSvictorle 
18564a2ae208SSatish Balay #undef __FUNCT__
18571cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
1858ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
18591cbb95d3SBarry Smith {
18601cbb95d3SBarry Smith   PetscErrorCode ierr;
18611cbb95d3SBarry Smith   PetscFunctionBegin;
18621cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
18631cbb95d3SBarry Smith   PetscFunctionReturn(0);
18641cbb95d3SBarry Smith }
18651cbb95d3SBarry Smith 
18661cbb95d3SBarry Smith #undef __FUNCT__
18674a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1868dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
186917ab2063SBarry Smith {
1870416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
187154f21887SBarry Smith   PetscScalar    *l,*r,x;
187254f21887SBarry Smith   MatScalar      *v;
1873dfbe8321SBarry Smith   PetscErrorCode ierr;
1874d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
187517ab2063SBarry Smith 
18763a40ed3dSBarry Smith   PetscFunctionBegin;
187717ab2063SBarry Smith   if (ll) {
18783ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
18793ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1880e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1881e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
18821ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1883416022c9SBarry Smith     v = a->a;
188417ab2063SBarry Smith     for (i=0; i<m; i++) {
188517ab2063SBarry Smith       x = l[i];
1886416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
188717ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
188817ab2063SBarry Smith     }
18891ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1890efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
189117ab2063SBarry Smith   }
189217ab2063SBarry Smith   if (rr) {
1893e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1894e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
18951ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1896416022c9SBarry Smith     v = a->a; jj = a->j;
189717ab2063SBarry Smith     for (i=0; i<nz; i++) {
1898bfeeae90SHong Zhang       (*v++) *= r[*jj++];
189917ab2063SBarry Smith     }
19001ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1901efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
190217ab2063SBarry Smith   }
19033a40ed3dSBarry Smith   PetscFunctionReturn(0);
190417ab2063SBarry Smith }
190517ab2063SBarry Smith 
19064a2ae208SSatish Balay #undef __FUNCT__
19074a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
190897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
190917ab2063SBarry Smith {
1910db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
19116849ba73SBarry Smith   PetscErrorCode ierr;
1912d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
191397f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
19145d0c19d7SBarry Smith   const PetscInt *irow,*icol;
19155d0c19d7SBarry Smith   PetscInt       nrows,ncols;
191697f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
191754f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1918416022c9SBarry Smith   Mat            C;
1919ace3abfcSBarry Smith   PetscBool      stride,sorted;
192017ab2063SBarry Smith 
19213a40ed3dSBarry Smith   PetscFunctionBegin;
192214ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1923e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
192414ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1925e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
192699141d43SSatish Balay 
192717ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1928b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1929b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
193017ab2063SBarry Smith 
1931fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
19320dbe5b1eSSatish Balay   ierr = PetscTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
1933fee21e36SBarry Smith   if (stride && step == 1) {
193402834360SBarry Smith     /* special case of contiguous rows */
19350e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
193602834360SBarry Smith     /* loop over new rows determining lens and starting points */
193702834360SBarry Smith     for (i=0; i<nrows; i++) {
1938bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1939a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
194002834360SBarry Smith       for (k=kstart; k<kend; k++) {
1941bfeeae90SHong Zhang         if (aj[k] >= first) {
194202834360SBarry Smith           starts[i] = k;
194302834360SBarry Smith           break;
194402834360SBarry Smith 	}
194502834360SBarry Smith       }
1946a2744918SBarry Smith       sum = 0;
194702834360SBarry Smith       while (k < kend) {
1948bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1949a2744918SBarry Smith         sum++;
195002834360SBarry Smith       }
1951a2744918SBarry Smith       lens[i] = sum;
195202834360SBarry Smith     }
195302834360SBarry Smith     /* create submatrix */
1954cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
195597f1f81fSBarry Smith       PetscInt n_cols,n_rows;
195608480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1957e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1958d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
195908480c60SBarry Smith       C = *B;
19603a40ed3dSBarry Smith     } else {
19617adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1962f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
19637adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1964ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
196508480c60SBarry Smith     }
1966db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1967db02288aSLois Curfman McInnes 
196802834360SBarry Smith     /* loop over rows inserting into submatrix */
1969db02288aSLois Curfman McInnes     a_new    = c->a;
1970db02288aSLois Curfman McInnes     j_new    = c->j;
1971db02288aSLois Curfman McInnes     i_new    = c->i;
1972bfeeae90SHong Zhang 
197302834360SBarry Smith     for (i=0; i<nrows; i++) {
1974a2744918SBarry Smith       ii    = starts[i];
1975a2744918SBarry Smith       lensi = lens[i];
1976a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1977a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
197802834360SBarry Smith       }
197987828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1980a2744918SBarry Smith       a_new      += lensi;
1981a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1982a2744918SBarry Smith       c->ilen[i]  = lensi;
198302834360SBarry Smith     }
19840e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
19853a40ed3dSBarry Smith   } else {
198602834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
19870e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
198897f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
19890e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
199017ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
199102834360SBarry Smith     /* determine lens of each row */
199202834360SBarry Smith     for (i=0; i<nrows; i++) {
1993bfeeae90SHong Zhang       kstart  = ai[irow[i]];
199402834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
199502834360SBarry Smith       lens[i] = 0;
199602834360SBarry Smith       for (k=kstart; k<kend; k++) {
1997bfeeae90SHong Zhang         if (smap[aj[k]]) {
199802834360SBarry Smith           lens[i]++;
199902834360SBarry Smith         }
200002834360SBarry Smith       }
200102834360SBarry Smith     }
200217ab2063SBarry Smith     /* Create and fill new matrix */
2003a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2004ace3abfcSBarry Smith       PetscBool  equal;
20050f5bd95cSBarry Smith 
200699141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2007e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2008d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
20090f5bd95cSBarry Smith       if (!equal) {
2010e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
201199141d43SSatish Balay       }
2012d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
201308480c60SBarry Smith       C = *B;
20143a40ed3dSBarry Smith     } else {
20157adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2016f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
20177adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2018ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
201908480c60SBarry Smith     }
202099141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
202117ab2063SBarry Smith     for (i=0; i<nrows; i++) {
202299141d43SSatish Balay       row    = irow[i];
2023bfeeae90SHong Zhang       kstart = ai[row];
202499141d43SSatish Balay       kend   = kstart + a->ilen[row];
2025bfeeae90SHong Zhang       mat_i  = c->i[i];
202699141d43SSatish Balay       mat_j  = c->j + mat_i;
202799141d43SSatish Balay       mat_a  = c->a + mat_i;
202899141d43SSatish Balay       mat_ilen = c->ilen + i;
202917ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2030bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2031ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
203299141d43SSatish Balay           *mat_a++ = a->a[k];
203399141d43SSatish Balay           (*mat_ilen)++;
203499141d43SSatish Balay 
203517ab2063SBarry Smith         }
203617ab2063SBarry Smith       }
203717ab2063SBarry Smith     }
203802834360SBarry Smith     /* Free work space */
203902834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2040606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2041606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
204202834360SBarry Smith   }
20436d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20446d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
204517ab2063SBarry Smith 
204617ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2047416022c9SBarry Smith   *B = C;
20483a40ed3dSBarry Smith   PetscFunctionReturn(0);
204917ab2063SBarry Smith }
205017ab2063SBarry Smith 
20511df811f5SHong Zhang #undef __FUNCT__
205282d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
205382d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
205482d44351SHong Zhang {
205582d44351SHong Zhang   PetscErrorCode ierr;
205682d44351SHong Zhang   Mat            B;
205782d44351SHong Zhang 
205882d44351SHong Zhang   PetscFunctionBegin;
205982d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
206082d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
206182d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
206282d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
206382d44351SHong Zhang   *subMat = B;
206482d44351SHong Zhang   PetscFunctionReturn(0);
206582d44351SHong Zhang }
206682d44351SHong Zhang 
206782d44351SHong Zhang #undef __FUNCT__
20684a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
20690481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2070a871dcd8SBarry Smith {
207163b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2072dfbe8321SBarry Smith   PetscErrorCode ierr;
207363b91edcSBarry Smith   Mat            outA;
2074ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
207563b91edcSBarry Smith 
20763a40ed3dSBarry Smith   PetscFunctionBegin;
2077e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
20781df811f5SHong Zhang 
2079b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2080b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2081a871dcd8SBarry Smith 
208263b91edcSBarry Smith   outA              = inA;
2083d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2084c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
20856bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2086c3122656SLisandro Dalcin   a->row = row;
2087c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
20886bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2089c3122656SLisandro Dalcin   a->col = col;
209063b91edcSBarry Smith 
209136db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
20926bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
20934c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
209452e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2095f0ec6fceSSatish Balay 
209694a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2097d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2098d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
209994a9d846SBarry Smith   }
210063b91edcSBarry Smith 
2101f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2102137fb511SHong Zhang   if (row_identity && col_identity) {
2103ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2104137fb511SHong Zhang   } else {
2105719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2106137fb511SHong Zhang   }
21073a40ed3dSBarry Smith   PetscFunctionReturn(0);
2108a871dcd8SBarry Smith }
2109a871dcd8SBarry Smith 
21104a2ae208SSatish Balay #undef __FUNCT__
21114a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2112f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2113f0b747eeSBarry Smith {
2114f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2115f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2116efee365bSSatish Balay   PetscErrorCode ierr;
21170805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
21183a40ed3dSBarry Smith 
21193a40ed3dSBarry Smith   PetscFunctionBegin;
2120f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2121efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
21223a40ed3dSBarry Smith   PetscFunctionReturn(0);
2123f0b747eeSBarry Smith }
2124f0b747eeSBarry Smith 
21254a2ae208SSatish Balay #undef __FUNCT__
21264a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
212797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2128cddf8d76SBarry Smith {
2129dfbe8321SBarry Smith   PetscErrorCode ierr;
213097f1f81fSBarry Smith   PetscInt       i;
2131cddf8d76SBarry Smith 
21323a40ed3dSBarry Smith   PetscFunctionBegin;
2133cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2134b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2135cddf8d76SBarry Smith   }
2136cddf8d76SBarry Smith 
2137cddf8d76SBarry Smith   for (i=0; i<n; i++) {
21386a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2139cddf8d76SBarry Smith   }
21403a40ed3dSBarry Smith   PetscFunctionReturn(0);
2141cddf8d76SBarry Smith }
2142cddf8d76SBarry Smith 
21434a2ae208SSatish Balay #undef __FUNCT__
21444a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
214597f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
21464dcbc457SBarry Smith {
2147e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
21486849ba73SBarry Smith   PetscErrorCode ierr;
21495d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
21505d0c19d7SBarry Smith   const PetscInt *idx;
215197f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2152f1af5d2fSBarry Smith   PetscBT        table;
2153bbd702dbSSatish Balay 
21543a40ed3dSBarry Smith   PetscFunctionBegin;
2155d0f46423SBarry Smith   m     = A->rmap->n;
2156e4d965acSSatish Balay   ai    = a->i;
2157bfeeae90SHong Zhang   aj    = a->j;
21588a047759SSatish Balay 
2159e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
216006763907SSatish Balay 
216197f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
21626831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
216306763907SSatish Balay 
2164e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2165b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2166e4d965acSSatish Balay     isz  = 0;
21676831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2168e4d965acSSatish Balay 
2169e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
21704dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2171b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2172e4d965acSSatish Balay 
2173dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2174e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2175f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
21764dcbc457SBarry Smith     }
217706763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
21786bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2179e4d965acSSatish Balay 
218004a348a9SBarry Smith     k = 0;
218104a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
218204a348a9SBarry Smith       n = isz;
218306763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2184e4d965acSSatish Balay         row   = nidx[k];
2185e4d965acSSatish Balay         start = ai[row];
2186e4d965acSSatish Balay         end   = ai[row+1];
218704a348a9SBarry Smith         for (l = start; l<end ; l++){
2188efb16452SHong Zhang           val = aj[l] ;
2189f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2190e4d965acSSatish Balay         }
2191e4d965acSSatish Balay       }
2192e4d965acSSatish Balay     }
219370b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2194e4d965acSSatish Balay   }
21956831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2196606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
21973a40ed3dSBarry Smith   PetscFunctionReturn(0);
21984dcbc457SBarry Smith }
219917ab2063SBarry Smith 
22000513a670SBarry Smith /* -------------------------------------------------------------- */
22014a2ae208SSatish Balay #undef __FUNCT__
22024a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2203dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
22040513a670SBarry Smith {
22050513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
22066849ba73SBarry Smith   PetscErrorCode ierr;
22073b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
22085d0c19d7SBarry Smith   const PetscInt *row,*col;
22095d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
221056cd22aeSBarry Smith   IS             icolp,irowp;
22113b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
22123b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
22130513a670SBarry Smith 
22143a40ed3dSBarry Smith   PetscFunctionBegin;
22154c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
221656cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
22174c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
221856cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
22190513a670SBarry Smith 
22200513a670SBarry Smith   /* determine lengths of permuted rows */
222197f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
22220513a670SBarry Smith   for (i=0; i<m; i++) {
22230513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
22240513a670SBarry Smith   }
22257adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2226f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
22277adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2228ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2229606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
22300513a670SBarry Smith 
223197f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
22320513a670SBarry Smith   for (i=0; i<m; i++) {
223332ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
22340513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2235cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
223632ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
22370513a670SBarry Smith   }
2238606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
22393c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
22400513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22410513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
224256cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
224356cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
22446bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
22456bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
22463a40ed3dSBarry Smith   PetscFunctionReturn(0);
22470513a670SBarry Smith }
22480513a670SBarry Smith 
22494a2ae208SSatish Balay #undef __FUNCT__
22504a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2251dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2252cb5b572fSBarry Smith {
2253dfbe8321SBarry Smith   PetscErrorCode ierr;
2254cb5b572fSBarry Smith 
2255cb5b572fSBarry Smith   PetscFunctionBegin;
225633f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
225733f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2258be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2259be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2260be6bf707SBarry Smith 
2261700c5bfcSBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2262d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2263cb5b572fSBarry Smith   } else {
2264cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2265cb5b572fSBarry Smith   }
2266cb5b572fSBarry Smith   PetscFunctionReturn(0);
2267cb5b572fSBarry Smith }
2268cb5b572fSBarry Smith 
22694a2ae208SSatish Balay #undef __FUNCT__
22704a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2271dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2272273d9f13SBarry Smith {
2273dfbe8321SBarry Smith   PetscErrorCode ierr;
2274273d9f13SBarry Smith 
2275273d9f13SBarry Smith   PetscFunctionBegin;
2276ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2277273d9f13SBarry Smith   PetscFunctionReturn(0);
2278273d9f13SBarry Smith }
2279273d9f13SBarry Smith 
22804a2ae208SSatish Balay #undef __FUNCT__
22814a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2282a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
22836c0721eeSBarry Smith {
22846c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
22856c0721eeSBarry Smith   PetscFunctionBegin;
22866c0721eeSBarry Smith   *array = a->a;
22876c0721eeSBarry Smith   PetscFunctionReturn(0);
22886c0721eeSBarry Smith }
22896c0721eeSBarry Smith 
22904a2ae208SSatish Balay #undef __FUNCT__
22914a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2292dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
22936c0721eeSBarry Smith {
22946c0721eeSBarry Smith   PetscFunctionBegin;
22956c0721eeSBarry Smith   PetscFunctionReturn(0);
22966c0721eeSBarry Smith }
2297273d9f13SBarry Smith 
2298ee4f033dSBarry Smith #undef __FUNCT__
2299ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2300dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2301ee4f033dSBarry Smith {
23026849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
23036849ba73SBarry Smith   PetscErrorCode ierr;
230497f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2305efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
230687828ca2SBarry Smith   PetscScalar    *vscale_array;
2307ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2308ee4f033dSBarry Smith   Vec            w1,w2,w3;
2309ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2310ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2311ee4f033dSBarry Smith 
2312ee4f033dSBarry Smith   PetscFunctionBegin;
2313ee4f033dSBarry Smith   if (!coloring->w1) {
2314ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
231552e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2316ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
231752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2318ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
231952e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2320ee4f033dSBarry Smith   }
2321ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2322ee4f033dSBarry Smith 
2323ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2324acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2325ee4f033dSBarry Smith   if (flg) {
2326ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2327ee4f033dSBarry Smith   } else {
2328ace3abfcSBarry Smith     PetscBool  assembled;
23290b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
23300b9b6f31SBarry Smith     if (assembled) {
2331ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2332ee4f033dSBarry Smith     }
23330b9b6f31SBarry Smith   }
2334ee4f033dSBarry Smith 
2335ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2336ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2337ee4f033dSBarry Smith 
2338ee4f033dSBarry Smith   /*
2339ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2340ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2341ee4f033dSBarry Smith   */
2342ee4f033dSBarry Smith   if (coloring->F) {
2343ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2344ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2345ee4f033dSBarry Smith     if (m1 != m2) {
2346ee4f033dSBarry Smith       coloring->F = 0;
2347ee4f033dSBarry Smith     }
2348ee4f033dSBarry Smith   }
2349ee4f033dSBarry Smith 
2350ee4f033dSBarry Smith   if (coloring->F) {
2351ee4f033dSBarry Smith     w1          = coloring->F;
2352ee4f033dSBarry Smith     coloring->F = 0;
2353ee4f033dSBarry Smith   } else {
235466f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2355ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
235666f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2357ee4f033dSBarry Smith   }
2358ee4f033dSBarry Smith 
2359ee4f033dSBarry Smith   /*
2360ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2361ee4f033dSBarry Smith   */
23621ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
23631ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2364ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2365ee4f033dSBarry Smith     /*
2366ee4f033dSBarry Smith        Loop over each column associated with color adding the
2367ee4f033dSBarry Smith        perturbation to the vector w3.
2368ee4f033dSBarry Smith     */
2369ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2370ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2371ee4f033dSBarry Smith       dx  = xx[col];
2372ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2373ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2374ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2375ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2376ee4f033dSBarry Smith #else
2377ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2378ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2379ee4f033dSBarry Smith #endif
2380ee4f033dSBarry Smith       dx                *= epsilon;
2381ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2382ee4f033dSBarry Smith     }
2383ee4f033dSBarry Smith   }
23841ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2385ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2386ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2387ee4f033dSBarry Smith 
2388ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2389ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2390ee4f033dSBarry Smith 
2391ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2392ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2393ee4f033dSBarry Smith 
23941ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2395ee4f033dSBarry Smith   /*
2396ee4f033dSBarry Smith       Loop over each color
2397ee4f033dSBarry Smith   */
2398ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
239949b058dcSBarry Smith     coloring->currentcolor = k;
2400ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
24011ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2402ee4f033dSBarry Smith     /*
2403ee4f033dSBarry Smith        Loop over each column associated with color adding the
2404ee4f033dSBarry Smith        perturbation to the vector w3.
2405ee4f033dSBarry Smith     */
2406ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2407ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2408ee4f033dSBarry Smith       dx  = xx[col];
24095b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2410ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2411ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2412ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2413ee4f033dSBarry Smith #else
2414ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2415ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2416ee4f033dSBarry Smith #endif
2417ee4f033dSBarry Smith       dx            *= epsilon;
2418e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2419ee4f033dSBarry Smith       w3_array[col] += dx;
2420ee4f033dSBarry Smith     }
24211ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2422ee4f033dSBarry Smith 
2423ee4f033dSBarry Smith     /*
2424ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2425ee4f033dSBarry Smith     */
2426ee4f033dSBarry Smith 
242766f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2428ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
242966f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2430efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2431ee4f033dSBarry Smith 
2432ee4f033dSBarry Smith     /*
2433ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2434ee4f033dSBarry Smith     */
24351ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2436ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2437ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2438ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2439ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2440ee4f033dSBarry Smith       srow   = row + start;
2441ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2442ee4f033dSBarry Smith     }
24431ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2444ee4f033dSBarry Smith   }
244549b058dcSBarry Smith   coloring->currentcolor = k;
24461ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
24471ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2448ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2449ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2450ee4f033dSBarry Smith   PetscFunctionReturn(0);
2451ee4f033dSBarry Smith }
2452ee4f033dSBarry Smith 
24538229c054SShri Abhyankar /*
24548229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
24558229c054SShri Abhyankar    have different nonzero structure.
24568229c054SShri Abhyankar */
2457ac90fabeSBarry Smith #undef __FUNCT__
24588229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
24598229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2460ec7775f6SShri Abhyankar {
24618229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2462ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2463ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2464ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2465ec7775f6SShri Abhyankar 
2466ec7775f6SShri Abhyankar   PetscFunctionBegin;
2467ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2468ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
24698af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
24708af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
24718af7cee1SJed Brown     nnz[i] = 0;
24728af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
24738af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
24748af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
24758af7cee1SJed Brown       nnz[i]++;
24768af7cee1SJed Brown     }
24778af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2478ec7775f6SShri Abhyankar   }
2479ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2480ec7775f6SShri Abhyankar }
2481ec7775f6SShri Abhyankar 
2482ec7775f6SShri Abhyankar #undef __FUNCT__
2483ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2484f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2485ac90fabeSBarry Smith {
2486dfbe8321SBarry Smith   PetscErrorCode ierr;
248797f1f81fSBarry Smith   PetscInt       i;
2488ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
24890805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2490ac90fabeSBarry Smith 
2491ac90fabeSBarry Smith   PetscFunctionBegin;
2492ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2493f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2494f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2495c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2496a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2497a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
24986bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2499a30b2313SHong Zhang     }
2500a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2501d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2502a30b2313SHong Zhang       y->XtoY = X;
2503407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2504c537a176SHong Zhang     }
2505f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
25061e2582c4SBarry 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);
2507ac90fabeSBarry Smith   } else {
25088229c054SShri Abhyankar     Mat      B;
25098229c054SShri Abhyankar     PetscInt *nnz;
251016b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2511ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2512bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
25134aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2514ec7775f6SShri Abhyankar     ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
25158229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
25168229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2517ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2518ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
25198229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2520ac90fabeSBarry Smith   }
2521ac90fabeSBarry Smith   PetscFunctionReturn(0);
2522ac90fabeSBarry Smith }
2523ac90fabeSBarry Smith 
2524521d7252SBarry Smith #undef __FUNCT__
2525521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2526521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2527521d7252SBarry Smith {
252841c166b1SJed Brown   PetscErrorCode ierr;
252941c166b1SJed Brown 
2530521d7252SBarry Smith   PetscFunctionBegin;
253141c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
253241c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2533521d7252SBarry Smith   PetscFunctionReturn(0);
2534521d7252SBarry Smith }
2535521d7252SBarry Smith 
2536354c94deSBarry Smith #undef __FUNCT__
2537354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
25387087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2539354c94deSBarry Smith {
2540354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2541354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2542354c94deSBarry Smith   PetscInt    i,nz;
2543354c94deSBarry Smith   PetscScalar *a;
2544354c94deSBarry Smith 
2545354c94deSBarry Smith   PetscFunctionBegin;
2546354c94deSBarry Smith   nz = aij->nz;
2547354c94deSBarry Smith   a  = aij->a;
2548354c94deSBarry Smith   for (i=0; i<nz; i++) {
2549354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2550354c94deSBarry Smith   }
2551354c94deSBarry Smith #else
2552354c94deSBarry Smith   PetscFunctionBegin;
2553354c94deSBarry Smith #endif
2554354c94deSBarry Smith   PetscFunctionReturn(0);
2555354c94deSBarry Smith }
2556354c94deSBarry Smith 
2557e34fafa9SBarry Smith #undef __FUNCT__
2558985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2559985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2560e34fafa9SBarry Smith {
2561e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2562e34fafa9SBarry Smith   PetscErrorCode ierr;
2563d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2564e34fafa9SBarry Smith   PetscReal      atmp;
2565985db425SBarry Smith   PetscScalar    *x;
2566e34fafa9SBarry Smith   MatScalar      *aa;
2567e34fafa9SBarry Smith 
2568e34fafa9SBarry Smith   PetscFunctionBegin;
2569e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2570e34fafa9SBarry Smith   aa   = a->a;
2571e34fafa9SBarry Smith   ai   = a->i;
2572e34fafa9SBarry Smith   aj   = a->j;
2573e34fafa9SBarry Smith 
2574985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2575e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2576e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2577e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2578e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2579e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
25809189402eSHong Zhang     x[i] = 0.0;
2581e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2582985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2583985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2584985db425SBarry Smith       aa++; aj++;
2585985db425SBarry Smith     }
2586985db425SBarry Smith   }
2587985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2588985db425SBarry Smith   PetscFunctionReturn(0);
2589985db425SBarry Smith }
2590985db425SBarry Smith 
2591985db425SBarry Smith #undef __FUNCT__
2592985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2593985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2594985db425SBarry Smith {
2595985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2596985db425SBarry Smith   PetscErrorCode ierr;
2597d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2598985db425SBarry Smith   PetscScalar    *x;
2599985db425SBarry Smith   MatScalar      *aa;
2600985db425SBarry Smith 
2601985db425SBarry Smith   PetscFunctionBegin;
2602e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2603985db425SBarry Smith   aa   = a->a;
2604985db425SBarry Smith   ai   = a->i;
2605985db425SBarry Smith   aj   = a->j;
2606985db425SBarry Smith 
2607985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2608985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2609985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2610e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2611985db425SBarry Smith   for (i=0; i<m; i++) {
2612985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2613d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2614985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2615985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2616985db425SBarry Smith       x[i] = 0.0;
2617985db425SBarry Smith       if (idx) {
2618985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2619985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2620985db425SBarry Smith           if (aj[j] > j) {
2621985db425SBarry Smith             idx[i] = j;
2622985db425SBarry Smith             break;
2623985db425SBarry Smith           }
2624985db425SBarry Smith         }
2625985db425SBarry Smith       }
2626985db425SBarry Smith     }
2627985db425SBarry Smith     for (j=0; j<ncols; j++){
2628985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2629985db425SBarry Smith       aa++; aj++;
2630985db425SBarry Smith     }
2631985db425SBarry Smith   }
2632985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2633985db425SBarry Smith   PetscFunctionReturn(0);
2634985db425SBarry Smith }
2635985db425SBarry Smith 
2636985db425SBarry Smith #undef __FUNCT__
2637c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2638c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2639c87e5d42SMatthew Knepley {
2640c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2641c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2642c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2643c87e5d42SMatthew Knepley   PetscReal      atmp;
2644c87e5d42SMatthew Knepley   PetscScalar    *x;
2645c87e5d42SMatthew Knepley   MatScalar      *aa;
2646c87e5d42SMatthew Knepley 
2647c87e5d42SMatthew Knepley   PetscFunctionBegin;
2648e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2649c87e5d42SMatthew Knepley   aa   = a->a;
2650c87e5d42SMatthew Knepley   ai   = a->i;
2651c87e5d42SMatthew Knepley   aj   = a->j;
2652c87e5d42SMatthew Knepley 
2653c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2654c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2655c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2656e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2657c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2658c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2659289a08f5SMatthew Knepley     if (ncols) {
2660289a08f5SMatthew Knepley       /* Get first nonzero */
2661289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2662289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2663289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2664289a08f5SMatthew Knepley       }
2665289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2666289a08f5SMatthew Knepley     } else {
2667289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2668289a08f5SMatthew Knepley     }
2669c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2670c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2671289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2672c87e5d42SMatthew Knepley       aa++; aj++;
2673c87e5d42SMatthew Knepley     }
2674c87e5d42SMatthew Knepley   }
2675c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2676c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2677c87e5d42SMatthew Knepley }
2678c87e5d42SMatthew Knepley 
2679c87e5d42SMatthew Knepley #undef __FUNCT__
2680985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2681985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2682985db425SBarry Smith {
2683985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2684985db425SBarry Smith   PetscErrorCode ierr;
2685d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2686985db425SBarry Smith   PetscScalar    *x;
2687985db425SBarry Smith   MatScalar      *aa;
2688985db425SBarry Smith 
2689985db425SBarry Smith   PetscFunctionBegin;
2690e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2691985db425SBarry Smith   aa   = a->a;
2692985db425SBarry Smith   ai   = a->i;
2693985db425SBarry Smith   aj   = a->j;
2694985db425SBarry Smith 
2695985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2696985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2697985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2698e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2699985db425SBarry Smith   for (i=0; i<m; i++) {
2700985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2701d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2702985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2703985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2704985db425SBarry Smith       x[i] = 0.0;
2705985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2706985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2707985db425SBarry Smith         for (j=0;j<ncols;j++) {
2708985db425SBarry Smith           if (aj[j] > j) {
2709985db425SBarry Smith             idx[i] = j;
2710985db425SBarry Smith             break;
2711985db425SBarry Smith           }
2712985db425SBarry Smith         }
2713985db425SBarry Smith       }
2714985db425SBarry Smith     }
2715985db425SBarry Smith     for (j=0; j<ncols; j++){
2716985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2717985db425SBarry Smith       aa++; aj++;
2718e34fafa9SBarry Smith     }
2719e34fafa9SBarry Smith   }
2720e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2721e34fafa9SBarry Smith   PetscFunctionReturn(0);
2722e34fafa9SBarry Smith }
27237087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2724682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
27250a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2726cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2727cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2728cb5b572fSBarry Smith        MatMult_SeqAIJ,
272997304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
27307c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
27317c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2732db4efbfdSBarry Smith        0,
2733db4efbfdSBarry Smith        0,
2734db4efbfdSBarry Smith        0,
2735db4efbfdSBarry Smith /*10*/ 0,
2736cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2737cb5b572fSBarry Smith        0,
273841f059aeSBarry Smith        MatSOR_SeqAIJ,
273917ab2063SBarry Smith        MatTranspose_SeqAIJ,
274097304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2741cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2742cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2743cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2744cb5b572fSBarry Smith        MatNorm_SeqAIJ,
274597304618SKris Buschelman /*20*/ 0,
2746cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2747cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2748cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2749d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2750db4efbfdSBarry Smith        0,
2751db4efbfdSBarry Smith        0,
2752db4efbfdSBarry Smith        0,
2753db4efbfdSBarry Smith        0,
2754d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2755db4efbfdSBarry Smith        0,
2756db4efbfdSBarry Smith        0,
27576c0721eeSBarry Smith        MatGetArray_SeqAIJ,
27586c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2759d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2760cb5b572fSBarry Smith        0,
2761cb5b572fSBarry Smith        0,
2762cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2763cb5b572fSBarry Smith        0,
2764d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2765cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2766cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2767cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2768cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2769d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2770cb5b572fSBarry Smith        MatScale_SeqAIJ,
2771cb5b572fSBarry Smith        0,
277279299369SBarry Smith        MatDiagonalSet_SeqAIJ,
27736e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
2774d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
27753b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
27763b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
27773b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2778a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2779d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2780b9617806SBarry Smith        0,
27810513a670SBarry Smith        0,
2782cda55fadSBarry Smith        MatPermute_SeqAIJ,
2783cda55fadSBarry Smith        0,
2784d519adbfSMatthew Knepley /*59*/ 0,
2785b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2786b9b97703SBarry Smith        MatView_SeqAIJ,
2787357abbc8SBarry Smith        0,
2788ee4f033dSBarry Smith        0,
2789d519adbfSMatthew Knepley /*64*/ 0,
2790ee4f033dSBarry Smith        0,
2791ee4f033dSBarry Smith        0,
2792ee4f033dSBarry Smith        0,
2793ee4f033dSBarry Smith        0,
2794d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2795c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2796ee4f033dSBarry Smith        0,
2797ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2798dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2799ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2800dcf5cc72SBarry Smith #else
2801dcf5cc72SBarry Smith        0,
2802dcf5cc72SBarry Smith #endif
2803d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
28043acb8795SBarry Smith        MatFDColoringApply_AIJ,
280597304618SKris Buschelman        0,
280697304618SKris Buschelman        0,
280797304618SKris Buschelman        0,
28086ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
280997304618SKris Buschelman        0,
281097304618SKris Buschelman        0,
281197304618SKris Buschelman        0,
2812bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2813d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
28141cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
28156284ec50SHong Zhang        0,
28166284ec50SHong Zhang        0,
2817bc011b1eSHong Zhang        0,
2818d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
281926be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
282026be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2821d439da42SKris Buschelman        MatPtAP_Basic,
28227ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2823d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2824bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2825bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2826bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
28277ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2828d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2829609c6c4dSKris Buschelman        0,
2830609c6c4dSKris Buschelman        0,
283187d4246cSBarry Smith        MatConjugate_SeqAIJ,
283287d4246cSBarry Smith        0,
2833d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
283499cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2835f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2836f5edf698SHong Zhang        0,
28372bebee5dSHong Zhang        0,
2838cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
2839985db425SBarry Smith        0,
28402af78befSBarry Smith        MatGetRowMin_SeqAIJ,
28412af78befSBarry Smith        0,
2842599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2843d519adbfSMatthew Knepley /*114*/0,
2844599ef60dSHong Zhang        0,
28453c2a7987SHong Zhang        0,
2846fe97e370SBarry Smith        0,
2847fbdbba38SShri Abhyankar        0,
2848fbdbba38SShri Abhyankar /*119*/0,
2849fbdbba38SShri Abhyankar        0,
2850fbdbba38SShri Abhyankar        0,
285182d44351SHong Zhang        0,
2852b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
28530716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
28540716a85fSBarry Smith        MatGetColumnNorms_SeqAIJ
28559e29f15eSvictorle };
285617ab2063SBarry Smith 
2857fb2e594dSBarry Smith EXTERN_C_BEGIN
28584a2ae208SSatish Balay #undef __FUNCT__
28594a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
28607087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2861bef8e0ddSBarry Smith {
2862bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
286397f1f81fSBarry Smith   PetscInt   i,nz,n;
2864bef8e0ddSBarry Smith 
2865bef8e0ddSBarry Smith   PetscFunctionBegin;
2866bef8e0ddSBarry Smith 
2867bef8e0ddSBarry Smith   nz = aij->maxnz;
2868d0f46423SBarry Smith   n  = mat->rmap->n;
2869bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2870bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2871bef8e0ddSBarry Smith   }
2872bef8e0ddSBarry Smith   aij->nz = nz;
2873bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2874bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2875bef8e0ddSBarry Smith   }
2876bef8e0ddSBarry Smith 
2877bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2878bef8e0ddSBarry Smith }
2879fb2e594dSBarry Smith EXTERN_C_END
2880bef8e0ddSBarry Smith 
28814a2ae208SSatish Balay #undef __FUNCT__
28824a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2883bef8e0ddSBarry Smith /*@
2884bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2885bef8e0ddSBarry Smith        in the matrix.
2886bef8e0ddSBarry Smith 
2887bef8e0ddSBarry Smith   Input Parameters:
2888bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2889bef8e0ddSBarry Smith -  indices - the column indices
2890bef8e0ddSBarry Smith 
289115091d37SBarry Smith   Level: advanced
289215091d37SBarry Smith 
2893bef8e0ddSBarry Smith   Notes:
2894bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2895bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2896bef8e0ddSBarry Smith   of the MatSetValues() operation.
2897bef8e0ddSBarry Smith 
2898bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2899d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2900bef8e0ddSBarry Smith 
2901bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2902bef8e0ddSBarry Smith 
2903b9617806SBarry Smith     The indices should start with zero, not one.
2904b9617806SBarry Smith 
2905bef8e0ddSBarry Smith @*/
29067087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2907bef8e0ddSBarry Smith {
29084ac538c5SBarry Smith   PetscErrorCode ierr;
2909bef8e0ddSBarry Smith 
2910bef8e0ddSBarry Smith   PetscFunctionBegin;
29110700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
29124482741eSBarry Smith   PetscValidPointer(indices,2);
29134ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
2914bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2915bef8e0ddSBarry Smith }
2916bef8e0ddSBarry Smith 
2917be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2918be6bf707SBarry Smith 
2919fb2e594dSBarry Smith EXTERN_C_BEGIN
29204a2ae208SSatish Balay #undef __FUNCT__
29214a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
29227087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
2923be6bf707SBarry Smith {
2924be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
29256849ba73SBarry Smith   PetscErrorCode ierr;
2926d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2927be6bf707SBarry Smith 
2928be6bf707SBarry Smith   PetscFunctionBegin;
2929be6bf707SBarry Smith   if (aij->nonew != 1) {
2930e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2931be6bf707SBarry Smith   }
2932be6bf707SBarry Smith 
2933be6bf707SBarry Smith   /* allocate space for values if not already there */
2934be6bf707SBarry Smith   if (!aij->saved_values) {
293587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
29369518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2937be6bf707SBarry Smith   }
2938be6bf707SBarry Smith 
2939be6bf707SBarry Smith   /* copy values over */
294087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2941be6bf707SBarry Smith   PetscFunctionReturn(0);
2942be6bf707SBarry Smith }
2943fb2e594dSBarry Smith EXTERN_C_END
2944be6bf707SBarry Smith 
29454a2ae208SSatish Balay #undef __FUNCT__
2946b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2947be6bf707SBarry Smith /*@
2948be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2949be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2950be6bf707SBarry Smith        nonlinear portion.
2951be6bf707SBarry Smith 
2952be6bf707SBarry Smith    Collect on Mat
2953be6bf707SBarry Smith 
2954be6bf707SBarry Smith   Input Parameters:
29550e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2956be6bf707SBarry Smith 
295715091d37SBarry Smith   Level: advanced
295815091d37SBarry Smith 
2959be6bf707SBarry Smith   Common Usage, with SNESSolve():
2960be6bf707SBarry Smith $    Create Jacobian matrix
2961be6bf707SBarry Smith $    Set linear terms into matrix
2962be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2963be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2964be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2965512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2966be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2967be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2968be6bf707SBarry Smith $    In your Jacobian routine
2969be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2970be6bf707SBarry Smith $      Set nonlinear terms in matrix
2971be6bf707SBarry Smith 
2972be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2973be6bf707SBarry Smith $    // build linear portion of Jacobian
2974512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2975be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2976be6bf707SBarry Smith $    loop over nonlinear iterations
2977be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2978be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2979be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2980be6bf707SBarry Smith $       Solve linear system with Jacobian
2981be6bf707SBarry Smith $    endloop
2982be6bf707SBarry Smith 
2983be6bf707SBarry Smith   Notes:
2984be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2985512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2986be6bf707SBarry Smith     calling this routine.
2987be6bf707SBarry Smith 
29880c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
29890c468ba9SBarry Smith     and does not allocated additional space.
29900c468ba9SBarry Smith 
2991be6bf707SBarry Smith .seealso: MatRetrieveValues()
2992be6bf707SBarry Smith 
2993be6bf707SBarry Smith @*/
29947087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
2995be6bf707SBarry Smith {
29964ac538c5SBarry Smith   PetscErrorCode ierr;
2997be6bf707SBarry Smith 
2998be6bf707SBarry Smith   PetscFunctionBegin;
29990700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3000e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3001e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
30024ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3003be6bf707SBarry Smith   PetscFunctionReturn(0);
3004be6bf707SBarry Smith }
3005be6bf707SBarry Smith 
3006fb2e594dSBarry Smith EXTERN_C_BEGIN
30074a2ae208SSatish Balay #undef __FUNCT__
30084a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
30097087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3010be6bf707SBarry Smith {
3011be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
30126849ba73SBarry Smith   PetscErrorCode ierr;
3013d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3014be6bf707SBarry Smith 
3015be6bf707SBarry Smith   PetscFunctionBegin;
3016be6bf707SBarry Smith   if (aij->nonew != 1) {
3017e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3018be6bf707SBarry Smith   }
3019be6bf707SBarry Smith   if (!aij->saved_values) {
3020e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3021be6bf707SBarry Smith   }
3022be6bf707SBarry Smith   /* copy values over */
302387828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3024be6bf707SBarry Smith   PetscFunctionReturn(0);
3025be6bf707SBarry Smith }
3026fb2e594dSBarry Smith EXTERN_C_END
3027be6bf707SBarry Smith 
30284a2ae208SSatish Balay #undef __FUNCT__
30294a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3030be6bf707SBarry Smith /*@
3031be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3032be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3033be6bf707SBarry Smith        nonlinear portion.
3034be6bf707SBarry Smith 
3035be6bf707SBarry Smith    Collect on Mat
3036be6bf707SBarry Smith 
3037be6bf707SBarry Smith   Input Parameters:
3038be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3039be6bf707SBarry Smith 
304015091d37SBarry Smith   Level: advanced
304115091d37SBarry Smith 
3042be6bf707SBarry Smith .seealso: MatStoreValues()
3043be6bf707SBarry Smith 
3044be6bf707SBarry Smith @*/
30457087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3046be6bf707SBarry Smith {
30474ac538c5SBarry Smith   PetscErrorCode ierr;
3048be6bf707SBarry Smith 
3049be6bf707SBarry Smith   PetscFunctionBegin;
30500700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3051e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3052e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
30534ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3054be6bf707SBarry Smith   PetscFunctionReturn(0);
3055be6bf707SBarry Smith }
3056be6bf707SBarry Smith 
3057f83d6046SBarry Smith 
3058be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
30594a2ae208SSatish Balay #undef __FUNCT__
30604a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
306117ab2063SBarry Smith /*@C
3062682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
30630d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
30646e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
306551c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
30662bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
306717ab2063SBarry Smith 
3068db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3069db81eaa0SLois Curfman McInnes 
307017ab2063SBarry Smith    Input Parameters:
3071db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
307217ab2063SBarry Smith .  m - number of rows
307317ab2063SBarry Smith .  n - number of columns
307417ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
307551c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
30762bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
307717ab2063SBarry Smith 
307817ab2063SBarry Smith    Output Parameter:
3079416022c9SBarry Smith .  A - the matrix
308017ab2063SBarry Smith 
3081175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3082ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3083175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3084175b88e8SBarry Smith 
3085b259b22eSLois Curfman McInnes    Notes:
308649a6f317SBarry Smith    If nnz is given then nz is ignored
308749a6f317SBarry Smith 
308817ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
308917ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
30900002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
309144cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
309217ab2063SBarry Smith 
309317ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3094a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
30953d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
30966da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
309717ab2063SBarry Smith 
3098682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
30994fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3100682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
31016c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
31026c7ebb05SLois Curfman McInnes 
31036c7ebb05SLois Curfman McInnes    Options Database Keys:
3104698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
31059db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
310617ab2063SBarry Smith 
3107027ccd11SLois Curfman McInnes    Level: intermediate
3108027ccd11SLois Curfman McInnes 
310936db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
311036db0b34SBarry Smith 
311117ab2063SBarry Smith @*/
31127087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
311317ab2063SBarry Smith {
3114dfbe8321SBarry Smith   PetscErrorCode ierr;
31156945ee14SBarry Smith 
31163a40ed3dSBarry Smith   PetscFunctionBegin;
3117f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3118117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3119c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3120d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3121273d9f13SBarry Smith   PetscFunctionReturn(0);
3122273d9f13SBarry Smith }
3123273d9f13SBarry Smith 
31244a2ae208SSatish Balay #undef __FUNCT__
31254a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3126273d9f13SBarry Smith /*@C
3127273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3128273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3129273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3130273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3131273d9f13SBarry Smith 
3132273d9f13SBarry Smith    Collective on MPI_Comm
3133273d9f13SBarry Smith 
3134273d9f13SBarry Smith    Input Parameters:
3135117016b1SBarry Smith +  B - The matrix-free
3136273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3137273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3138273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3139273d9f13SBarry Smith 
3140273d9f13SBarry Smith    Notes:
314149a6f317SBarry Smith      If nnz is given then nz is ignored
314249a6f317SBarry Smith 
3143273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3144273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3145273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3146273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3147273d9f13SBarry Smith 
3148273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3149273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3150273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3151273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3152273d9f13SBarry Smith 
3153aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3154aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3155aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3156aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3157aa95bbe8SBarry Smith 
3158a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3159a96a251dSBarry Smith    entries or columns indices
3160a96a251dSBarry Smith 
3161273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3162273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3163273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3164273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3165273d9f13SBarry Smith 
3166273d9f13SBarry Smith    Options Database Keys:
3167698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3168698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3169273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3170273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3171273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3172273d9f13SBarry Smith 
3173273d9f13SBarry Smith    Level: intermediate
3174273d9f13SBarry Smith 
3175aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3176273d9f13SBarry Smith 
3177273d9f13SBarry Smith @*/
31787087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3179273d9f13SBarry Smith {
31804ac538c5SBarry Smith   PetscErrorCode ierr;
3181a23d5eceSKris Buschelman 
3182a23d5eceSKris Buschelman   PetscFunctionBegin;
31834ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3184a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3185a23d5eceSKris Buschelman }
3186a23d5eceSKris Buschelman 
3187a23d5eceSKris Buschelman EXTERN_C_BEGIN
3188a23d5eceSKris Buschelman #undef __FUNCT__
3189a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
31907087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3191a23d5eceSKris Buschelman {
3192273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3193ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
31946849ba73SBarry Smith   PetscErrorCode ierr;
319597f1f81fSBarry Smith   PetscInt       i;
3196273d9f13SBarry Smith 
3197273d9f13SBarry Smith   PetscFunctionBegin;
3198d5d45c9bSBarry Smith 
3199a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3200c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3201c461c341SBarry Smith     nz             = 0;
3202c461c341SBarry Smith   }
3203c461c341SBarry Smith 
320426283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
320526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
320626283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
320726283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3208899cda47SBarry Smith 
3209435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3210e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3211b73539f3SBarry Smith   if (nnz) {
3212d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3213e32f2f54SBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
3214e32f2f54SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
3215b73539f3SBarry Smith     }
3216b73539f3SBarry Smith   }
3217b73539f3SBarry Smith 
3218273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3219273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3220273d9f13SBarry Smith 
3221ab93d7beSBarry Smith   if (!skipallocation) {
32222ee49352SLisandro Dalcin     if (!b->imax) {
3223d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3224d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
32252ee49352SLisandro Dalcin     }
3226273d9f13SBarry Smith     if (!nnz) {
3227435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3228c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3229d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3230d0f46423SBarry Smith       nz = nz*B->rmap->n;
3231273d9f13SBarry Smith     } else {
3232273d9f13SBarry Smith       nz = 0;
3233d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3234273d9f13SBarry Smith     }
3235ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3236d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3237ab93d7beSBarry Smith 
3238273d9f13SBarry Smith     /* allocate the matrix space */
32392ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3240d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3241d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3242bfeeae90SHong Zhang     b->i[0] = 0;
3243d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
32445da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
32455da197adSKris Buschelman     }
3246273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3247e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3248e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3249c461c341SBarry Smith   } else {
3250e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3251e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3252c461c341SBarry Smith   }
3253273d9f13SBarry Smith 
3254273d9f13SBarry Smith   b->nz                = 0;
3255273d9f13SBarry Smith   b->maxnz             = nz;
3256273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3257273d9f13SBarry Smith   PetscFunctionReturn(0);
3258273d9f13SBarry Smith }
3259a23d5eceSKris Buschelman EXTERN_C_END
3260273d9f13SBarry Smith 
3261a1661176SMatthew Knepley #undef  __FUNCT__
3262a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
326358d36128SBarry Smith /*@
3264a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3265a1661176SMatthew Knepley 
3266a1661176SMatthew Knepley    Input Parameters:
3267a1661176SMatthew Knepley +  B - the matrix
3268a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3269a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3270a1661176SMatthew Knepley -  v - optional values in the matrix
3271a1661176SMatthew Knepley 
3272a1661176SMatthew Knepley    Level: developer
3273a1661176SMatthew Knepley 
327458d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
327558d36128SBarry Smith 
3276a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3277a1661176SMatthew Knepley 
3278a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3279a1661176SMatthew Knepley @*/
3280a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3281a1661176SMatthew Knepley {
3282a1661176SMatthew Knepley   PetscErrorCode ierr;
3283a1661176SMatthew Knepley 
3284a1661176SMatthew Knepley   PetscFunctionBegin;
32850700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
32864ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3287a1661176SMatthew Knepley   PetscFunctionReturn(0);
3288a1661176SMatthew Knepley }
3289a1661176SMatthew Knepley 
3290a1661176SMatthew Knepley EXTERN_C_BEGIN
3291a1661176SMatthew Knepley #undef  __FUNCT__
3292a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
32937087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3294a1661176SMatthew Knepley {
3295a1661176SMatthew Knepley   PetscInt       i;
3296a1661176SMatthew Knepley   PetscInt       m,n;
3297a1661176SMatthew Knepley   PetscInt       nz;
3298a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3299a1661176SMatthew Knepley   PetscScalar    *values;
3300a1661176SMatthew Knepley   PetscErrorCode ierr;
3301a1661176SMatthew Knepley 
3302a1661176SMatthew Knepley   PetscFunctionBegin;
3303a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3304a1661176SMatthew Knepley 
330565e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3306a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3307a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3308b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3309a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
331065e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3311a1661176SMatthew Knepley     nnz[i] = nz;
3312a1661176SMatthew Knepley   }
3313a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3314a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3315a1661176SMatthew Knepley 
3316a1661176SMatthew Knepley   if (v) {
3317a1661176SMatthew Knepley     values = (PetscScalar*) v;
3318a1661176SMatthew Knepley   } else {
33190e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3320a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3321a1661176SMatthew Knepley   }
3322a1661176SMatthew Knepley 
3323a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3324b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3325b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3326a1661176SMatthew Knepley   }
3327a1661176SMatthew Knepley 
3328a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3329a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3330a1661176SMatthew Knepley 
3331a1661176SMatthew Knepley   if (!v) {
3332a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3333a1661176SMatthew Knepley   }
3334a1661176SMatthew Knepley   PetscFunctionReturn(0);
3335a1661176SMatthew Knepley }
3336a1661176SMatthew Knepley EXTERN_C_END
3337a1661176SMatthew Knepley 
3338c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3339c6db04a5SJed Brown #include <private/petscaxpy.h>
3340170fe5c8SBarry Smith 
3341170fe5c8SBarry Smith #undef __FUNCT__
3342170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3343170fe5c8SBarry Smith /*
3344170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3345170fe5c8SBarry Smith 
3346170fe5c8SBarry Smith                n                       p                          p
3347170fe5c8SBarry Smith         (              )       (              )         (                  )
3348170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3349170fe5c8SBarry Smith         (              )       (              )         (                  )
3350170fe5c8SBarry Smith 
3351170fe5c8SBarry Smith */
3352170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3353170fe5c8SBarry Smith {
3354170fe5c8SBarry Smith   PetscErrorCode     ierr;
3355170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3356170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3357170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
33581de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3359170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3360170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3361170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3362170fe5c8SBarry Smith 
3363170fe5c8SBarry Smith   PetscFunctionBegin;
3364d0f46423SBarry Smith   m = A->rmap->n;
3365d0f46423SBarry Smith   n = A->cmap->n;
3366d0f46423SBarry Smith   p = B->cmap->n;
3367170fe5c8SBarry Smith   a = sub_a->v;
3368170fe5c8SBarry Smith   b = sub_b->a;
3369170fe5c8SBarry Smith   c = sub_c->v;
3370170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3371170fe5c8SBarry Smith 
3372170fe5c8SBarry Smith   ii  = sub_b->i;
3373170fe5c8SBarry Smith   idx = sub_b->j;
3374170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3375170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3376170fe5c8SBarry Smith     while (q-->0) {
3377170fe5c8SBarry Smith       c_q = c + m*(*idx);
3378170fe5c8SBarry Smith       a_q = a + m*i;
3379be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3380170fe5c8SBarry Smith       idx++;
3381170fe5c8SBarry Smith       b++;
3382170fe5c8SBarry Smith     }
3383170fe5c8SBarry Smith   }
3384170fe5c8SBarry Smith   PetscFunctionReturn(0);
3385170fe5c8SBarry Smith }
3386170fe5c8SBarry Smith 
3387170fe5c8SBarry Smith #undef __FUNCT__
3388170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3389170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3390170fe5c8SBarry Smith {
3391170fe5c8SBarry Smith   PetscErrorCode ierr;
3392d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3393170fe5c8SBarry Smith   Mat            Cmat;
3394170fe5c8SBarry Smith 
3395170fe5c8SBarry Smith   PetscFunctionBegin;
3396e32f2f54SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
339739804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3398170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3399170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3400170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3401170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3402170fe5c8SBarry Smith   *C = Cmat;
3403170fe5c8SBarry Smith   PetscFunctionReturn(0);
3404170fe5c8SBarry Smith }
3405170fe5c8SBarry Smith 
3406170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3407170fe5c8SBarry Smith #undef __FUNCT__
3408170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3409170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3410170fe5c8SBarry Smith {
3411170fe5c8SBarry Smith   PetscErrorCode ierr;
3412170fe5c8SBarry Smith 
3413170fe5c8SBarry Smith   PetscFunctionBegin;
3414170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3415170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3416170fe5c8SBarry Smith   }
3417170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3418170fe5c8SBarry Smith   PetscFunctionReturn(0);
3419170fe5c8SBarry Smith }
3420170fe5c8SBarry Smith 
3421170fe5c8SBarry Smith 
34220bad9183SKris Buschelman /*MC
3423fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
34240bad9183SKris Buschelman    based on compressed sparse row format.
34250bad9183SKris Buschelman 
34260bad9183SKris Buschelman    Options Database Keys:
34270bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
34280bad9183SKris Buschelman 
34290bad9183SKris Buschelman   Level: beginner
34300bad9183SKris Buschelman 
3431f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
34320bad9183SKris Buschelman M*/
34330bad9183SKris Buschelman 
3434a6175056SHong Zhang EXTERN_C_BEGIN
3435b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3436b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3437b5e56a35SBarry Smith #endif
3438ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3439af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3440af1023dbSSatish Balay #endif
34417087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
34427087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
34437087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
34447087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3445611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
34467087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3447611f576cSBarry Smith #endif
3448611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
34497087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3450611f576cSBarry Smith #endif
3451f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3452f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3453f3c0ef26SHong Zhang #endif
3454611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
34557087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3456611f576cSBarry Smith #endif
3457eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
34587087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3459eb3b5408SSatish Balay #endif
3460586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
34617087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3462586621ddSJed Brown #endif
3463719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
34647087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3465719d5645SBarry Smith #endif
3466b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
34677087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
34687087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
34697087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3470b3866ffcSBarry Smith #endif
347117667f90SBarry Smith EXTERN_C_END
347217667f90SBarry Smith 
347317667f90SBarry Smith EXTERN_C_BEGIN
34744a2ae208SSatish Balay #undef __FUNCT__
34754a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
34767087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3477273d9f13SBarry Smith {
3478273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3479dfbe8321SBarry Smith   PetscErrorCode ierr;
348038baddfdSBarry Smith   PetscMPIInt    size;
3481273d9f13SBarry Smith 
3482273d9f13SBarry Smith   PetscFunctionBegin;
34837adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3484e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3485273d9f13SBarry Smith 
348638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3487b0a32e0cSBarry Smith   B->data             = (void*)b;
3488549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3489416022c9SBarry Smith   b->row              = 0;
3490416022c9SBarry Smith   b->col              = 0;
349182bf6240SBarry Smith   b->icol             = 0;
3492b810aeb4SBarry Smith   b->reallocs         = 0;
349336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3494f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3495416022c9SBarry Smith   b->nonew             = 0;
3496416022c9SBarry Smith   b->diag              = 0;
3497416022c9SBarry Smith   b->solve_work        = 0;
34982a1b7f2aSHong Zhang   B->spptr             = 0;
3499be6bf707SBarry Smith   b->saved_values      = 0;
3500d7f994e1SBarry Smith   b->idiag             = 0;
350171f1c65dSBarry Smith   b->mdiag             = 0;
350271f1c65dSBarry Smith   b->ssor_work         = 0;
350371f1c65dSBarry Smith   b->omega             = 1.0;
350471f1c65dSBarry Smith   b->fshift            = 0.0;
350571f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3506a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3507a30b2313SHong Zhang   b->xtoy              = 0;
3508a30b2313SHong Zhang   b->XtoY              = 0;
350988e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
351017ab2063SBarry Smith 
351135d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3512b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3513700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3514b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3515b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3516b3866ffcSBarry Smith #endif
3517b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3518700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3519b5e56a35SBarry Smith #endif
3520ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3521700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3522719d5645SBarry Smith #endif
3523611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3524700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3525611f576cSBarry Smith #endif
3526f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3527700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3528f3c0ef26SHong Zhang #endif
3529611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3530700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3531611f576cSBarry Smith #endif
3532611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3533700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3534611f576cSBarry Smith #endif
3535eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3536700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3537eb3b5408SSatish Balay #endif
3538586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3539700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3540586621ddSJed Brown #endif
3541719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3542700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3543719d5645SBarry Smith #endif
3544700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3545700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3546700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3547700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3548700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3549700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3550700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3551700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3552700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3553700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3554700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3555700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3556700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3557700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3558700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3559700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3560700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3561700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
35624108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
356317667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
35643a40ed3dSBarry Smith   PetscFunctionReturn(0);
356517ab2063SBarry Smith }
3566273d9f13SBarry Smith EXTERN_C_END
356717ab2063SBarry Smith 
35684a2ae208SSatish Balay #undef __FUNCT__
3569b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3570b24902e0SBarry Smith /*
3571b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3572b24902e0SBarry Smith */
3573ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
357417ab2063SBarry Smith {
3575416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
35766849ba73SBarry Smith   PetscErrorCode ierr;
3577d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
357817ab2063SBarry Smith 
35793a40ed3dSBarry Smith   PetscFunctionBegin;
3580273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3581273d9f13SBarry Smith 
3582d5f3da31SBarry Smith   C->factortype     = A->factortype;
3583416022c9SBarry Smith   c->row            = 0;
3584416022c9SBarry Smith   c->col            = 0;
358582bf6240SBarry Smith   c->icol           = 0;
35866ad4291fSHong Zhang   c->reallocs       = 0;
358717ab2063SBarry Smith 
35886ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
358917ab2063SBarry Smith 
3590aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
3591aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
3592eec197d1SBarry Smith 
359333b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
35949518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
359517ab2063SBarry Smith   for (i=0; i<m; i++) {
3596416022c9SBarry Smith     c->imax[i] = a->imax[i];
3597416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
359817ab2063SBarry Smith   }
359917ab2063SBarry Smith 
360017ab2063SBarry Smith   /* allocate the matrix space */
3601f77e22a1SHong Zhang   if (mallocmatspace){
3602a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
36039518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3604f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
360597f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
360617ab2063SBarry Smith     if (m > 0) {
360797f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3608be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3609bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3610be6bf707SBarry Smith       } else {
3611bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
361217ab2063SBarry Smith       }
361308480c60SBarry Smith     }
3614f77e22a1SHong Zhang   }
361517ab2063SBarry Smith 
36166ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3617416022c9SBarry Smith   c->roworiented       = a->roworiented;
3618416022c9SBarry Smith   c->nonew             = a->nonew;
3619416022c9SBarry Smith   if (a->diag) {
362097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
362152e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
362217ab2063SBarry Smith     for (i=0; i<m; i++) {
3623416022c9SBarry Smith       c->diag[i] = a->diag[i];
362417ab2063SBarry Smith     }
36253a40ed3dSBarry Smith   } else c->diag           = 0;
36266ad4291fSHong Zhang   c->solve_work            = 0;
36276ad4291fSHong Zhang   c->saved_values          = 0;
36286ad4291fSHong Zhang   c->idiag                 = 0;
362971f1c65dSBarry Smith   c->ssor_work             = 0;
3630a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3631e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3632e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
36336ad4291fSHong Zhang   c->xtoy                  = 0;
36346ad4291fSHong Zhang   c->XtoY                  = 0;
36356ad4291fSHong Zhang 
3636416022c9SBarry Smith   c->nz                 = a->nz;
36378ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3638273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3639754ec7b1SSatish Balay 
36406ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
36416ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3642cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3643cd6b891eSBarry Smith   if (a->compressedrow.use){
36446ad4291fSHong Zhang     i = a->compressedrow.nrows;
36450e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
36466ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
36476ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
364827ea64f8SHong Zhang   } else {
364927ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
365027ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
365127ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
36526ad4291fSHong Zhang   }
365388e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
36544108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
36554846f1f5SKris Buschelman 
36567adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
36573a40ed3dSBarry Smith   PetscFunctionReturn(0);
365817ab2063SBarry Smith }
365917ab2063SBarry Smith 
36604a2ae208SSatish Balay #undef __FUNCT__
3661b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3662b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3663b24902e0SBarry Smith {
3664b24902e0SBarry Smith   PetscErrorCode ierr;
3665b24902e0SBarry Smith 
3666b24902e0SBarry Smith   PetscFunctionBegin;
3667b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
36684b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3669b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3670f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3671b24902e0SBarry Smith   PetscFunctionReturn(0);
3672b24902e0SBarry Smith }
3673b24902e0SBarry Smith 
3674b24902e0SBarry Smith #undef __FUNCT__
36754a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3676112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3677fbdbba38SShri Abhyankar {
3678fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3679fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3680fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3681fbdbba38SShri Abhyankar   int            fd;
3682fbdbba38SShri Abhyankar   PetscMPIInt    size;
3683fbdbba38SShri Abhyankar   MPI_Comm       comm;
3684fbdbba38SShri Abhyankar 
3685fbdbba38SShri Abhyankar   PetscFunctionBegin;
3686fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3687fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3688fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3689fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3690fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3691fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
3692fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3693fbdbba38SShri Abhyankar 
3694fbdbba38SShri Abhyankar   if (nz < 0) {
3695fbdbba38SShri Abhyankar     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3696fbdbba38SShri Abhyankar   }
3697fbdbba38SShri Abhyankar 
3698fbdbba38SShri Abhyankar   /* read in row lengths */
3699fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3700fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3701fbdbba38SShri Abhyankar 
3702fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
3703fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3704fbdbba38SShri Abhyankar   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
3705fbdbba38SShri Abhyankar 
3706fbdbba38SShri Abhyankar   /* set global size if not set already*/
3707f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
3708fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3709aabbc4fbSShri Abhyankar   } else {
3710fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
3711fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
3712f501eaabSShri Abhyankar     if (M != rows ||  N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols);
3713aabbc4fbSShri Abhyankar   }
3714fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
3715fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
3716fbdbba38SShri Abhyankar 
3717fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
3718fbdbba38SShri Abhyankar 
3719fbdbba38SShri Abhyankar   /* read in nonzero values */
3720fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
3721fbdbba38SShri Abhyankar 
3722fbdbba38SShri Abhyankar   /* set matrix "i" values */
3723fbdbba38SShri Abhyankar   a->i[0] = 0;
3724fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
3725fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3726fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
3727fbdbba38SShri Abhyankar   }
3728fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3729fbdbba38SShri Abhyankar 
3730fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3731fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3732fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
3733fbdbba38SShri Abhyankar }
3734fbdbba38SShri Abhyankar 
3735fbdbba38SShri Abhyankar #undef __FUNCT__
3736b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3737ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
37387264ac53SSatish Balay {
37397264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3740dfbe8321SBarry Smith   PetscErrorCode ierr;
3741eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3742eeffb40dSHong Zhang   PetscInt k;
3743eeffb40dSHong Zhang #endif
37447264ac53SSatish Balay 
37453a40ed3dSBarry Smith   PetscFunctionBegin;
3746bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3747d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3748ca44d042SBarry Smith     *flg = PETSC_FALSE;
3749ca44d042SBarry Smith     PetscFunctionReturn(0);
3750bcd2baecSBarry Smith   }
37517264ac53SSatish Balay 
37527264ac53SSatish Balay   /* if the a->i are the same */
3753d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3754abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
37557264ac53SSatish Balay 
37567264ac53SSatish Balay   /* if a->j are the same */
375797f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3758abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3759bcd2baecSBarry Smith 
3760bcd2baecSBarry Smith   /* if a->a are the same */
3761eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3762eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3763eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3764eeffb40dSHong Zhang       *flg = PETSC_FALSE;
37653a40ed3dSBarry Smith       PetscFunctionReturn(0);
3766eeffb40dSHong Zhang     }
3767eeffb40dSHong Zhang   }
3768eeffb40dSHong Zhang #else
3769eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3770eeffb40dSHong Zhang #endif
3771eeffb40dSHong Zhang   PetscFunctionReturn(0);
37727264ac53SSatish Balay }
377336db0b34SBarry Smith 
37744a2ae208SSatish Balay #undef __FUNCT__
37754a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
377605869f15SSatish Balay /*@
377736db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
377836db0b34SBarry Smith               provided by the user.
377936db0b34SBarry Smith 
3780c75a6043SHong Zhang       Collective on MPI_Comm
378136db0b34SBarry Smith 
378236db0b34SBarry Smith    Input Parameters:
378336db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
378436db0b34SBarry Smith .   m - number of rows
378536db0b34SBarry Smith .   n - number of columns
378636db0b34SBarry Smith .   i - row indices
378736db0b34SBarry Smith .   j - column indices
378836db0b34SBarry Smith -   a - matrix values
378936db0b34SBarry Smith 
379036db0b34SBarry Smith    Output Parameter:
379136db0b34SBarry Smith .   mat - the matrix
379236db0b34SBarry Smith 
379336db0b34SBarry Smith    Level: intermediate
379436db0b34SBarry Smith 
379536db0b34SBarry Smith    Notes:
37960551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
3797292fb18eSBarry Smith     once the matrix is destroyed and not before
379836db0b34SBarry Smith 
379936db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
380036db0b34SBarry Smith 
3801bfeeae90SHong Zhang        The i and j indices are 0 based
380236db0b34SBarry Smith 
3803a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3804a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3805a4552177SSatish Balay     as shown:
3806a4552177SSatish Balay 
3807a4552177SSatish Balay         1 0 0
3808a4552177SSatish Balay         2 0 3
3809a4552177SSatish Balay         4 5 6
3810a4552177SSatish Balay 
3811a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
38129985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3813a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3814a4552177SSatish Balay 
38159985e31cSBarry Smith 
38162fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
381736db0b34SBarry Smith 
381836db0b34SBarry Smith @*/
38197087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
382036db0b34SBarry Smith {
3821dfbe8321SBarry Smith   PetscErrorCode ierr;
3822cbcfb4deSHong Zhang   PetscInt       ii;
382336db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3824cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3825cbcfb4deSHong Zhang   PetscInt       jj;
3826cbcfb4deSHong Zhang #endif
382736db0b34SBarry Smith 
382836db0b34SBarry Smith   PetscFunctionBegin;
3829a96a251dSBarry Smith   if (i[0]) {
3830e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
383136db0b34SBarry Smith   }
3832f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3833f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3834ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3835ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3836ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3837ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3838ab93d7beSBarry Smith 
383936db0b34SBarry Smith   aij->i = i;
384036db0b34SBarry Smith   aij->j = j;
384136db0b34SBarry Smith   aij->a = a;
384236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
384336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3844e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3845e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
384636db0b34SBarry Smith 
384736db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
384836db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
38492515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
3850e32f2f54SBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
38519985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
3852e32f2f54SBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
3853e32f2f54SBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
38549985e31cSBarry Smith     }
385536db0b34SBarry Smith #endif
385636db0b34SBarry Smith   }
38572515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
385836db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3859e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3860e32f2f54SBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
386136db0b34SBarry Smith   }
386236db0b34SBarry Smith #endif
386336db0b34SBarry Smith 
3864b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3865b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
386636db0b34SBarry Smith   PetscFunctionReturn(0);
386736db0b34SBarry Smith }
386836db0b34SBarry Smith 
3869cc8ba8e1SBarry Smith #undef __FUNCT__
3870ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3871dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3872cc8ba8e1SBarry Smith {
3873dfbe8321SBarry Smith   PetscErrorCode ierr;
3874cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
387536db0b34SBarry Smith 
3876cc8ba8e1SBarry Smith   PetscFunctionBegin;
38778ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3878cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3879cc8ba8e1SBarry Smith     a->coloring = coloring;
388012c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
388197f1f81fSBarry Smith     PetscInt             i,*larray;
388212c595b3SBarry Smith     ISColoring      ocoloring;
388308b6dcc0SBarry Smith     ISColoringValue *colors;
388412c595b3SBarry Smith 
388512c595b3SBarry Smith     /* set coloring for diagonal portion */
38860e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3887d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
388812c595b3SBarry Smith       larray[i] = i;
388912c595b3SBarry Smith     }
3890*992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
38910e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3892d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
389312c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
389412c595b3SBarry Smith     }
389512c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3896d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
389712c595b3SBarry Smith     a->coloring = ocoloring;
389812c595b3SBarry Smith   }
3899cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3900cc8ba8e1SBarry Smith }
3901cc8ba8e1SBarry Smith 
3902dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3903ee4f033dSBarry Smith EXTERN_C_BEGIN
3904c6db04a5SJed Brown #include <adic/ad_utils.h>
3905ee4f033dSBarry Smith EXTERN_C_END
3906cc8ba8e1SBarry Smith 
3907cc8ba8e1SBarry Smith #undef __FUNCT__
3908ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3909dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3910cc8ba8e1SBarry Smith {
3911cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3912d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
39134440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
391408b6dcc0SBarry Smith   ISColoringValue *color;
3915cc8ba8e1SBarry Smith 
3916cc8ba8e1SBarry Smith   PetscFunctionBegin;
3917e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
39184440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3919cc8ba8e1SBarry Smith   color = a->coloring->colors;
3920cc8ba8e1SBarry Smith   /* loop over rows */
3921cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3922cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3923cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3924cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3925cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3926cc8ba8e1SBarry Smith     }
39274440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3928ee4f033dSBarry Smith   }
3929ee4f033dSBarry Smith   PetscFunctionReturn(0);
3930ee4f033dSBarry Smith }
3931ee4f033dSBarry Smith #endif
3932ee4f033dSBarry Smith 
3933ee4f033dSBarry Smith #undef __FUNCT__
3934ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
393597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3936ee4f033dSBarry Smith {
3937ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3938d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
393954f21887SBarry Smith   MatScalar       *v = a->a;
394054f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
394108b6dcc0SBarry Smith   ISColoringValue *color;
3942ee4f033dSBarry Smith 
3943ee4f033dSBarry Smith   PetscFunctionBegin;
3944e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3945ee4f033dSBarry Smith   color = a->coloring->colors;
3946ee4f033dSBarry Smith   /* loop over rows */
3947ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3948ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3949ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3950ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3951ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3952ee4f033dSBarry Smith     }
3953ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3954cc8ba8e1SBarry Smith   }
3955cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3956cc8ba8e1SBarry Smith }
395736db0b34SBarry Smith 
395881824310SBarry Smith /*
395981824310SBarry Smith     Special version for direct calls from Fortran
396081824310SBarry Smith */
3961c6db04a5SJed Brown #include <private/fortranimpl.h>
396281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
396381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
396481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
396581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
396681824310SBarry Smith #endif
396781824310SBarry Smith 
396881824310SBarry Smith /* Change these macros so can be used in void function */
396981824310SBarry Smith #undef CHKERRQ
39707adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
397181824310SBarry Smith #undef SETERRQ2
3972e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
397381824310SBarry Smith 
397481824310SBarry Smith EXTERN_C_BEGIN
397581824310SBarry Smith #undef __FUNCT__
397681824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
39771f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
397881824310SBarry Smith {
397981824310SBarry Smith   Mat            A = *AA;
398081824310SBarry Smith   PetscInt       m = *mm, n = *nn;
398181824310SBarry Smith   InsertMode     is = *isis;
398281824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
398381824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
398481824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
398581824310SBarry Smith   PetscErrorCode ierr;
398681824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
398754f21887SBarry Smith   MatScalar      *ap,value,*aa;
3988ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
3989ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
399081824310SBarry Smith 
399181824310SBarry Smith   PetscFunctionBegin;
3992d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
399381824310SBarry Smith   imax = a->imax;
399481824310SBarry Smith   ai = a->i;
399581824310SBarry Smith   ailen = a->ilen;
399681824310SBarry Smith   aj = a->j;
399781824310SBarry Smith   aa = a->a;
399881824310SBarry Smith 
399981824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
400081824310SBarry Smith     row  = im[k];
400181824310SBarry Smith     if (row < 0) continue;
400281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4003d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
400481824310SBarry Smith #endif
400581824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
400681824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
400781824310SBarry Smith     low  = 0;
400881824310SBarry Smith     high = nrow;
400981824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
401081824310SBarry Smith       if (in[l] < 0) continue;
401181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4012d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
401381824310SBarry Smith #endif
401481824310SBarry Smith       col = in[l];
401581824310SBarry Smith       if (roworiented) {
401681824310SBarry Smith         value = v[l + k*n];
401781824310SBarry Smith       } else {
401881824310SBarry Smith         value = v[k + l*m];
401981824310SBarry Smith       }
402081824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
402181824310SBarry Smith 
402281824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
402381824310SBarry Smith       lastcol = col;
402481824310SBarry Smith       while (high-low > 5) {
402581824310SBarry Smith         t = (low+high)/2;
402681824310SBarry Smith         if (rp[t] > col) high = t;
402781824310SBarry Smith         else             low  = t;
402881824310SBarry Smith       }
402981824310SBarry Smith       for (i=low; i<high; i++) {
403081824310SBarry Smith         if (rp[i] > col) break;
403181824310SBarry Smith         if (rp[i] == col) {
403281824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
403381824310SBarry Smith           else                  ap[i] = value;
403481824310SBarry Smith           goto noinsert;
403581824310SBarry Smith         }
403681824310SBarry Smith       }
403781824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
403881824310SBarry Smith       if (nonew == 1) goto noinsert;
40397adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4040fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
404181824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
404281824310SBarry Smith       /* shift up all the later entries in this row */
404381824310SBarry Smith       for (ii=N; ii>=i; ii--) {
404481824310SBarry Smith         rp[ii+1] = rp[ii];
404581824310SBarry Smith         ap[ii+1] = ap[ii];
404681824310SBarry Smith       }
404781824310SBarry Smith       rp[i] = col;
404881824310SBarry Smith       ap[i] = value;
404981824310SBarry Smith       noinsert:;
405081824310SBarry Smith       low = i + 1;
405181824310SBarry Smith     }
405281824310SBarry Smith     ailen[row] = nrow;
405381824310SBarry Smith   }
405481824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
405581824310SBarry Smith   PetscFunctionReturnVoid();
405681824310SBarry Smith }
405781824310SBarry Smith EXTERN_C_END
405862298a1eSBarry Smith 
4059