xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 0ca81413ba2e908e73df59769d30584e082b69ad)
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 
1150*0ca81413SKerry Stevens //*******************
1151*0ca81413SKerry Stevens typedef struct {
1152*0ca81413SKerry Stevens   const MatScalar* matdata;
1153*0ca81413SKerry Stevens   const PetscScalar* vecdata;
1154*0ca81413SKerry Stevens   PetscScalar* vecout;
1155*0ca81413SKerry Stevens   const PetscInt* colindnz;
1156*0ca81413SKerry Stevens   const PetscInt* rownumnz;
1157*0ca81413SKerry Stevens   PetscInt numrows;
1158*0ca81413SKerry Stevens   const PetscInt* specidx;
1159*0ca81413SKerry Stevens   PetscInt nzr;
1160*0ca81413SKerry Stevens } MatMult_KernelData;
1161*0ca81413SKerry Stevens 
1162*0ca81413SKerry Stevens void *MatMult_Kernel(void *arg)
1163*0ca81413SKerry Stevens {
1164*0ca81413SKerry Stevens   MatMult_KernelData *data = (MatMult_KernelData*)arg;
1165*0ca81413SKerry Stevens   PetscScalar       sum;
1166*0ca81413SKerry Stevens   const MatScalar   *aabase = data->matdata,*aa;
1167*0ca81413SKerry Stevens   const PetscScalar *x = data->vecdata;
1168*0ca81413SKerry Stevens   PetscScalar       *y = data->vecout;
1169*0ca81413SKerry Stevens   const PetscInt    *ajbase = data->colindnz,*aj;
1170*0ca81413SKerry Stevens   const PetscInt    *ii = data->rownumnz;
1171*0ca81413SKerry Stevens   PetscInt          m  = data->numrows;
1172*0ca81413SKerry Stevens   const PetscInt    *ridx = data->specidx;
1173*0ca81413SKerry Stevens   PetscInt          i,n,nonzerorow = 0;
1174*0ca81413SKerry Stevens 
1175*0ca81413SKerry Stevens   if(ridx!=NULL) {
1176*0ca81413SKerry Stevens     for (i=0; i<m; i++){
1177*0ca81413SKerry Stevens       n   = ii[i+1] - ii[i];
1178*0ca81413SKerry Stevens       aj  = ajbase + ii[i];
1179*0ca81413SKerry Stevens       aa  = aabase + ii[i];
1180*0ca81413SKerry Stevens       sum = 0.0;
1181*0ca81413SKerry Stevens       nonzerorow += (n>0);
1182*0ca81413SKerry Stevens       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1183*0ca81413SKerry Stevens       y[*ridx++] = sum;
1184*0ca81413SKerry Stevens     }
1185*0ca81413SKerry Stevens   }
1186*0ca81413SKerry Stevens   else {
1187*0ca81413SKerry Stevens     for (i=0; i<m; i++) {
1188*0ca81413SKerry Stevens       n   = ii[i+1] - ii[i];
1189*0ca81413SKerry Stevens       aj  = ajbase + ii[i];
1190*0ca81413SKerry Stevens       aa  = aabase + ii[i];
1191*0ca81413SKerry Stevens       sum  = 0.0;
1192*0ca81413SKerry Stevens       nonzerorow += (n>0);
1193*0ca81413SKerry Stevens       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1194*0ca81413SKerry Stevens       y[i] = sum;
1195*0ca81413SKerry Stevens     }
1196*0ca81413SKerry Stevens   }
1197*0ca81413SKerry Stevens   data->nzr = nonzerorow;
1198*0ca81413SKerry Stevens   return NULL;
1199*0ca81413SKerry Stevens }
1200*0ca81413SKerry Stevens 
1201*0ca81413SKerry Stevens extern PetscMPIInt PetscMaxThreads;
1202*0ca81413SKerry Stevens void           MainJob(void* (*pFunc)(void*),void**,PetscInt);
1203*0ca81413SKerry Stevens #undef __FUNCT__
1204*0ca81413SKerry Stevens #define __FUNCT__ "MatMult_SeqAIJPThread"
1205*0ca81413SKerry Stevens PetscErrorCode MatMult_SeqAIJPThread(Mat A,Vec xx,Vec yy)
1206*0ca81413SKerry Stevens {
1207*0ca81413SKerry Stevens   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1208*0ca81413SKerry Stevens   PetscScalar       *y;
1209*0ca81413SKerry Stevens   const PetscScalar *x;
1210*0ca81413SKerry Stevens   PetscErrorCode    ierr;
1211*0ca81413SKerry Stevens   PetscInt          m=A->rmap->n,nonzerorow=0;
1212*0ca81413SKerry Stevens   PetscBool         usecprow=a->compressedrow.use;
1213*0ca81413SKerry Stevens 
1214*0ca81413SKerry Stevens #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1215*0ca81413SKerry Stevens #pragma disjoint(*x,*y,*aa)
1216*0ca81413SKerry Stevens #endif
1217*0ca81413SKerry Stevens 
1218*0ca81413SKerry Stevens   PetscFunctionBegin;
1219*0ca81413SKerry Stevens   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1220*0ca81413SKerry Stevens   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1221*0ca81413SKerry Stevens 
1222*0ca81413SKerry Stevens   if(usecprow) {
1223*0ca81413SKerry Stevens     PetscInt          NumPerThread,iindex;
1224*0ca81413SKerry Stevens     const MatScalar   *aa = a->a;
1225*0ca81413SKerry Stevens     const PetscInt    *aj = a->j,*ii = a->compressedrow.i,*ridx=a->compressedrow.rindex;
1226*0ca81413SKerry Stevens     PetscInt          i,iStartVal,iEndVal,iStartIndex,iEndIndex;
1227*0ca81413SKerry Stevens     const PetscInt    iNumThreads = PetscMaxThreads;  //this number could be different
1228*0ca81413SKerry Stevens     MatMult_KernelData* kerneldatap = (MatMult_KernelData*)malloc(iNumThreads*sizeof(MatMult_KernelData));
1229*0ca81413SKerry Stevens     MatMult_KernelData** pdata = (MatMult_KernelData**)malloc(iNumThreads*sizeof(MatMult_KernelData*));
1230*0ca81413SKerry Stevens 
1231*0ca81413SKerry Stevens     m    = a->compressedrow.nrows;
1232*0ca81413SKerry Stevens     NumPerThread = ii[m]/iNumThreads;
1233*0ca81413SKerry Stevens     iindex = 0;
1234*0ca81413SKerry Stevens     for(i=0; i<iNumThreads;i++) {
1235*0ca81413SKerry Stevens       iStartIndex = iindex;
1236*0ca81413SKerry Stevens       iStartVal = ii[iStartIndex];
1237*0ca81413SKerry Stevens       iEndVal = iStartVal;
1238*0ca81413SKerry Stevens       //determine number of rows to process
1239*0ca81413SKerry Stevens       while(iEndVal-iStartVal<NumPerThread) {
1240*0ca81413SKerry Stevens 	iindex++;
1241*0ca81413SKerry Stevens 	iEndVal = ii[iindex];
1242*0ca81413SKerry Stevens       }
1243*0ca81413SKerry Stevens       //determine whether to go back 1
1244*0ca81413SKerry Stevens       if(iEndVal-iStartVal-NumPerThread>NumPerThread-(ii[iindex-1]-iStartVal)) {
1245*0ca81413SKerry Stevens 	iindex--;
1246*0ca81413SKerry Stevens 	iEndVal = ii[iindex];
1247*0ca81413SKerry Stevens       }
1248*0ca81413SKerry Stevens       iEndIndex = iindex;
1249*0ca81413SKerry Stevens       kerneldatap[i].matdata  = aa;
1250*0ca81413SKerry Stevens       kerneldatap[i].vecdata  = x;
1251*0ca81413SKerry Stevens       kerneldatap[i].vecout   = y;
1252*0ca81413SKerry Stevens       kerneldatap[i].colindnz = aj;
1253*0ca81413SKerry Stevens       kerneldatap[i].rownumnz = ii + iStartIndex;
1254*0ca81413SKerry Stevens       kerneldatap[i].numrows  = iEndIndex - iStartIndex + 1;
1255*0ca81413SKerry Stevens       kerneldatap[i].specidx  = ridx + iStartVal;
1256*0ca81413SKerry Stevens       kerneldatap[i].nzr      = 0;
1257*0ca81413SKerry Stevens       pdata[i] = &kerneldatap[i];
1258*0ca81413SKerry Stevens       iindex++;
1259*0ca81413SKerry Stevens     }
1260*0ca81413SKerry Stevens     MainJob(MatMult_Kernel,(void**)pdata,iNumThreads);
1261*0ca81413SKerry Stevens     //collect results
1262*0ca81413SKerry Stevens     for(i=0; i<iNumThreads; i++) {
1263*0ca81413SKerry Stevens       nonzerorow += kerneldatap[i].nzr;
1264*0ca81413SKerry Stevens     }
1265*0ca81413SKerry Stevens   }
1266*0ca81413SKerry Stevens   else {
1267*0ca81413SKerry Stevens #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1268*0ca81413SKerry Stevens   fortranmultaij_(&m,x,a->i,a->j,a->a,y);
1269*0ca81413SKerry Stevens #else
1270*0ca81413SKerry Stevens     PetscInt          NumPerThread,iindex;
1271*0ca81413SKerry Stevens     const MatScalar   *aa = a->a;
1272*0ca81413SKerry Stevens     const PetscInt    *aj = a->j,*ii = a->i,*ridx=PETSC_NULL;
1273*0ca81413SKerry Stevens     PetscInt          i,iStartVal,iEndVal,iStartIndex,iEndIndex;
1274*0ca81413SKerry Stevens     const PetscInt    iNumThreads = PetscMaxThreads;  //this number could be different
1275*0ca81413SKerry Stevens     MatMult_KernelData* kerneldatap = (MatMult_KernelData*)malloc(iNumThreads*sizeof(MatMult_KernelData));
1276*0ca81413SKerry Stevens     MatMult_KernelData** pdata = (MatMult_KernelData**)malloc(iNumThreads*sizeof(MatMult_KernelData*));
1277*0ca81413SKerry Stevens 
1278*0ca81413SKerry Stevens     NumPerThread = ii[m]/iNumThreads;
1279*0ca81413SKerry Stevens     iindex = 0;
1280*0ca81413SKerry Stevens     for(i=0; i<iNumThreads;i++) {
1281*0ca81413SKerry Stevens       iStartIndex = iindex;
1282*0ca81413SKerry Stevens       iStartVal = ii[iStartIndex];
1283*0ca81413SKerry Stevens       iEndVal = iStartVal;
1284*0ca81413SKerry Stevens       //determine number of rows to process
1285*0ca81413SKerry Stevens       while(iEndVal-iStartVal<NumPerThread) {
1286*0ca81413SKerry Stevens 	iindex++;
1287*0ca81413SKerry Stevens 	iEndVal = ii[iindex];
1288*0ca81413SKerry Stevens       }
1289*0ca81413SKerry Stevens       //determine whether to go back 1
1290*0ca81413SKerry Stevens       if(iEndVal-iStartVal-NumPerThread>NumPerThread-(ii[iindex-1]-iStartVal)) {
1291*0ca81413SKerry Stevens 	iindex--;
1292*0ca81413SKerry Stevens 	iEndVal = ii[iindex];
1293*0ca81413SKerry Stevens       }
1294*0ca81413SKerry Stevens       iEndIndex = iindex;
1295*0ca81413SKerry Stevens       kerneldatap[i].matdata  = aa;
1296*0ca81413SKerry Stevens       kerneldatap[i].vecdata  = x;
1297*0ca81413SKerry Stevens       kerneldatap[i].vecout   = y;
1298*0ca81413SKerry Stevens       kerneldatap[i].colindnz = aj;
1299*0ca81413SKerry Stevens       kerneldatap[i].rownumnz = ii + iStartIndex;
1300*0ca81413SKerry Stevens       kerneldatap[i].numrows  = iEndIndex - iStartIndex + 1;
1301*0ca81413SKerry Stevens       kerneldatap[i].specidx  = ridx + iStartVal;
1302*0ca81413SKerry Stevens       kerneldatap[i].nzr      = 0;
1303*0ca81413SKerry Stevens       pdata[i] = &kerneldatap[i];
1304*0ca81413SKerry Stevens       iindex++;
1305*0ca81413SKerry Stevens     }
1306*0ca81413SKerry Stevens     MainJob(MatMult_Kernel,(void**)pdata,iNumThreads);
1307*0ca81413SKerry Stevens     //collect results
1308*0ca81413SKerry Stevens     for(i=0; i<iNumThreads; i++) {
1309*0ca81413SKerry Stevens       nonzerorow += kerneldatap[i].nzr;
1310*0ca81413SKerry Stevens     }
1311*0ca81413SKerry Stevens #endif
1312*0ca81413SKerry Stevens   }
1313*0ca81413SKerry Stevens 
1314*0ca81413SKerry Stevens   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1315*0ca81413SKerry Stevens   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1316*0ca81413SKerry Stevens   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1317*0ca81413SKerry Stevens   PetscFunctionReturn(0);
1318*0ca81413SKerry Stevens }
1319*0ca81413SKerry Stevens //*******************
1320*0ca81413SKerry Stevens 
1321c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
13224a2ae208SSatish Balay #undef __FUNCT__
13234a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1324dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
132517ab2063SBarry Smith {
1326416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
132754f21887SBarry Smith   PetscScalar     *x,*y,*z;
132854f21887SBarry Smith   const MatScalar *aa;
1329dfbe8321SBarry Smith   PetscErrorCode  ierr;
1330d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1331aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
133297952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1333362ced78SSatish Balay   PetscScalar     sum;
1334ace3abfcSBarry Smith   PetscBool       usecprow=a->compressedrow.use;
1335e36a17ebSSatish Balay #endif
13369ea0dfa2SSatish Balay 
13373a40ed3dSBarry Smith   PetscFunctionBegin;
13381ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
13391ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13402e8a6d31SBarry Smith   if (zz != yy) {
13411ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
13422e8a6d31SBarry Smith   } else {
13432e8a6d31SBarry Smith     z = y;
13442e8a6d31SBarry Smith   }
1345bfeeae90SHong Zhang 
134697952fefSHong Zhang   aj  = a->j;
134797952fefSHong Zhang   aa  = a->a;
1348cddf8d76SBarry Smith   ii  = a->i;
1349aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
135097952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
135102ab625aSSatish Balay #else
13524eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
13534eb6d288SHong Zhang     if (zz != yy){
13544eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
13554eb6d288SHong Zhang     }
135697952fefSHong Zhang     m    = a->compressedrow.nrows;
135797952fefSHong Zhang     ii   = a->compressedrow.i;
135897952fefSHong Zhang     ridx = a->compressedrow.rindex;
135997952fefSHong Zhang     for (i=0; i<m; i++){
136097952fefSHong Zhang       n  = ii[i+1] - ii[i];
136197952fefSHong Zhang       aj  = a->j + ii[i];
136297952fefSHong Zhang       aa  = a->a + ii[i];
136397952fefSHong Zhang       sum = y[*ridx];
136497952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
136597952fefSHong Zhang       z[*ridx++] = sum;
136697952fefSHong Zhang     }
136797952fefSHong Zhang   } else { /* do not use compressed row format */
136817ab2063SBarry Smith     for (i=0; i<m; i++) {
13699ea0dfa2SSatish Balay       jrow = ii[i];
13709ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
137117ab2063SBarry Smith       sum  = y[i];
13729ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
137397952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
13749ea0dfa2SSatish Balay       }
137517ab2063SBarry Smith       z[i] = sum;
137617ab2063SBarry Smith     }
137797952fefSHong Zhang   }
137802ab625aSSatish Balay #endif
1379dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
13801ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13811ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13822e8a6d31SBarry Smith   if (zz != yy) {
13831ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
13842e8a6d31SBarry Smith   }
13858154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
13866b375ea7SVictor Minden   /*
1387918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1388918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1389918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
13906b375ea7SVictor Minden   */
1391918e98c3SVictor Minden #endif
13923a40ed3dSBarry Smith   PetscFunctionReturn(0);
139317ab2063SBarry Smith }
139417ab2063SBarry Smith 
139517ab2063SBarry Smith /*
139617ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
139717ab2063SBarry Smith */
13984a2ae208SSatish Balay #undef __FUNCT__
13994a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1400dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
140117ab2063SBarry Smith {
1402416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
14036849ba73SBarry Smith   PetscErrorCode ierr;
1404d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
140517ab2063SBarry Smith 
14063a40ed3dSBarry Smith   PetscFunctionBegin;
140709f38230SBarry Smith   if (!a->diag) {
140809f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
14099518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
141009f38230SBarry Smith   }
1411d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
141209f38230SBarry Smith     a->diag[i] = a->i[i+1];
1413bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1414bfeeae90SHong Zhang       if (a->j[j] == i) {
141509f38230SBarry Smith         a->diag[i] = j;
141617ab2063SBarry Smith         break;
141717ab2063SBarry Smith       }
141817ab2063SBarry Smith     }
141917ab2063SBarry Smith   }
14203a40ed3dSBarry Smith   PetscFunctionReturn(0);
142117ab2063SBarry Smith }
142217ab2063SBarry Smith 
1423be5855fcSBarry Smith /*
1424be5855fcSBarry Smith      Checks for missing diagonals
1425be5855fcSBarry Smith */
14264a2ae208SSatish Balay #undef __FUNCT__
14274a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1428ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1429be5855fcSBarry Smith {
1430be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
143197f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1432be5855fcSBarry Smith 
1433be5855fcSBarry Smith   PetscFunctionBegin;
143409f38230SBarry Smith   *missing = PETSC_FALSE;
1435d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
143609f38230SBarry Smith     *missing  = PETSC_TRUE;
143709f38230SBarry Smith     if (d) *d = 0;
143809f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
143909f38230SBarry Smith   } else {
1440f1e2ffcdSBarry Smith     diag = a->diag;
1441d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1442bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
144309f38230SBarry Smith 	*missing = PETSC_TRUE;
144409f38230SBarry Smith 	if (d) *d = i;
144509f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
144609f38230SBarry Smith       }
1447be5855fcSBarry Smith     }
1448be5855fcSBarry Smith   }
1449be5855fcSBarry Smith   PetscFunctionReturn(0);
1450be5855fcSBarry Smith }
1451be5855fcSBarry Smith 
145271f1c65dSBarry Smith EXTERN_C_BEGIN
145371f1c65dSBarry Smith #undef __FUNCT__
145471f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
14557087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
145671f1c65dSBarry Smith {
145771f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
145871f1c65dSBarry Smith   PetscErrorCode ierr;
1459d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
146054f21887SBarry Smith   MatScalar      *v = a->a;
146154f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
146271f1c65dSBarry Smith 
146371f1c65dSBarry Smith   PetscFunctionBegin;
146471f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
146571f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
146671f1c65dSBarry Smith   diag = a->diag;
146771f1c65dSBarry Smith   if (!a->idiag) {
146871f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
146971f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
147071f1c65dSBarry Smith     v        = a->a;
147171f1c65dSBarry Smith   }
147271f1c65dSBarry Smith   mdiag = a->mdiag;
147371f1c65dSBarry Smith   idiag = a->idiag;
147471f1c65dSBarry Smith 
1475028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
147671f1c65dSBarry Smith     for (i=0; i<m; i++) {
147771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1478e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
147971f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
148071f1c65dSBarry Smith     }
148171f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
148271f1c65dSBarry Smith   } else {
148371f1c65dSBarry Smith     for (i=0; i<m; i++) {
148471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
148571f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
148671f1c65dSBarry Smith     }
1487dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
148871f1c65dSBarry Smith   }
148971f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
149071f1c65dSBarry Smith   PetscFunctionReturn(0);
149171f1c65dSBarry Smith }
14925a9745a3SMatthew Knepley EXTERN_C_END
149371f1c65dSBarry Smith 
1494c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
14954a2ae208SSatish Balay #undef __FUNCT__
149641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
149741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
149817ab2063SBarry Smith {
1499416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1500e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1501e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
150254f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1503dfbe8321SBarry Smith   PetscErrorCode     ierr;
1504d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
150597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
150617ab2063SBarry Smith 
15073a40ed3dSBarry Smith   PetscFunctionBegin;
1508b965ef7fSBarry Smith   its = its*lits;
150991723122SBarry Smith 
151071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
151171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
151271f1c65dSBarry Smith   a->fshift = fshift;
151371f1c65dSBarry Smith   a->omega  = omega;
1514ed480e8bSBarry Smith 
151571f1c65dSBarry Smith   diag = a->diag;
151671f1c65dSBarry Smith   t     = a->ssor_work;
1517ed480e8bSBarry Smith   idiag = a->idiag;
151871f1c65dSBarry Smith   mdiag = a->mdiag;
1519ed480e8bSBarry Smith 
15201ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
15213649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
152271f1c65dSBarry Smith   CHKMEMQ;
1523ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
152417ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
152517ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1526ed480e8bSBarry Smith     bs = b;
152717ab2063SBarry Smith     for (i=0; i<m; i++) {
152871f1c65dSBarry Smith         d    = fshift + mdiag[i];
1529416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1530ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1531ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
153217ab2063SBarry Smith         sum  = b[i]*d/omega;
1533003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
153417ab2063SBarry Smith         x[i] = sum;
153517ab2063SBarry Smith     }
15361ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15373649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1538efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15393a40ed3dSBarry Smith     PetscFunctionReturn(0);
154017ab2063SBarry Smith   }
1541c783ea89SBarry Smith 
154248af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1543e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
15443a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
154517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1546887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
154717ab2063SBarry Smith 
154817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
154917ab2063SBarry Smith 
1550887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
155117ab2063SBarry Smith     */
155217ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
155317ab2063SBarry Smith 
155417ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
155517ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1556416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1557ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1558ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
155917ab2063SBarry Smith       sum  = b[i];
1560e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1561ed480e8bSBarry Smith       x[i] = sum*idiag[i];
156217ab2063SBarry Smith     }
156317ab2063SBarry Smith 
156417ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1565416022c9SBarry Smith     v = a->a;
1566ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
156717ab2063SBarry Smith 
156817ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1569ed480e8bSBarry Smith     ts = t;
1570416022c9SBarry Smith     diag = a->diag;
157117ab2063SBarry Smith     for (i=0; i<m; i++) {
1572416022c9SBarry Smith       n    = diag[i] - a->i[i];
1573ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1574ed480e8bSBarry Smith       v    = a->a + a->i[i];
157517ab2063SBarry Smith       sum  = t[i];
1576003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1577ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1578733d66baSBarry Smith       /*  x = x + t */
1579733d66baSBarry Smith       x[i] += t[i];
158017ab2063SBarry Smith     }
158117ab2063SBarry Smith 
1582dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
15831ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15843649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
15853a40ed3dSBarry Smith     PetscFunctionReturn(0);
158617ab2063SBarry Smith   }
158717ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
158817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
158917ab2063SBarry Smith       for (i=0; i<m; i++) {
1590416022c9SBarry Smith         n    = diag[i] - a->i[i];
1591ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1592ed480e8bSBarry Smith         v    = a->a + a->i[i];
159317ab2063SBarry Smith         sum  = b[i];
1594e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15955c99c7daSBarry Smith         t[i] = sum;
1596ed480e8bSBarry Smith         x[i] = sum*idiag[i];
159717ab2063SBarry Smith       }
15985c99c7daSBarry Smith       xb = t;
1599efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16003a40ed3dSBarry Smith     } else xb = b;
160117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
160217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1603416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1604ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1605ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
160617ab2063SBarry Smith         sum  = xb[i];
1607e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
16085c99c7daSBarry Smith         if (xb == b) {
1609ed480e8bSBarry Smith           x[i] = sum*idiag[i];
16105c99c7daSBarry Smith         } else {
16115c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
161217ab2063SBarry Smith         }
16135c99c7daSBarry Smith       }
1614efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
161517ab2063SBarry Smith     }
161617ab2063SBarry Smith     its--;
161717ab2063SBarry Smith   }
161817ab2063SBarry Smith   while (its--) {
161917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
162017ab2063SBarry Smith       for (i=0; i<m; i++) {
1621416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1622ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1623ed480e8bSBarry Smith         v    = a->a + a->i[i];
162417ab2063SBarry Smith         sum  = b[i];
1625e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1626ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
162717ab2063SBarry Smith       }
16289f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
162917ab2063SBarry Smith     }
163017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
163117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1632416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1633ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1634ed480e8bSBarry Smith         v    = a->a + a->i[i];
163517ab2063SBarry Smith         sum  = b[i];
1636e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1637ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
163817ab2063SBarry Smith       }
16399f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
164017ab2063SBarry Smith     }
164117ab2063SBarry Smith   }
16421ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16433649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
164471f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
164517ab2063SBarry Smith }
164617ab2063SBarry Smith 
16472af78befSBarry Smith 
16484a2ae208SSatish Balay #undef __FUNCT__
16494a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1650dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
165117ab2063SBarry Smith {
1652416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16534e220ebcSLois Curfman McInnes 
16543a40ed3dSBarry Smith   PetscFunctionBegin;
16554e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
16564e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
16574e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
16584e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
16594e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
16608e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
16617adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1662d5f3da31SBarry Smith   if (A->factortype) {
16634e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
16644e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
16654e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
16664e220ebcSLois Curfman McInnes   } else {
16674e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
16684e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
16694e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
16704e220ebcSLois Curfman McInnes   }
16713a40ed3dSBarry Smith   PetscFunctionReturn(0);
167217ab2063SBarry Smith }
167317ab2063SBarry Smith 
16744a2ae208SSatish Balay #undef __FUNCT__
16754a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
16762b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
167717ab2063SBarry Smith {
1678416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
16793b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
16806849ba73SBarry Smith   PetscErrorCode    ierr;
168197b48c8fSBarry Smith   const PetscScalar *xx;
168297b48c8fSBarry Smith   PetscScalar       *bb;
1683ace3abfcSBarry Smith   PetscBool         missing;
168417ab2063SBarry Smith 
16853a40ed3dSBarry Smith   PetscFunctionBegin;
168697b48c8fSBarry Smith   if (x && b) {
168797b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
168897b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
168997b48c8fSBarry Smith     for (i=0; i<N; i++) {
169097b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
169197b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
169297b48c8fSBarry Smith     }
169397b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
169497b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
169597b48c8fSBarry Smith   }
169697b48c8fSBarry Smith 
1697a9817697SBarry Smith   if (a->keepnonzeropattern) {
1698f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1699e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1700bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1701f1e2ffcdSBarry Smith     }
1702f4df32b1SMatthew Knepley     if (diag != 0.0) {
170309f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1704e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1705f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1706f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1707f1e2ffcdSBarry Smith       }
1708f1e2ffcdSBarry Smith     }
170988e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1710f1e2ffcdSBarry Smith   } else {
1711f4df32b1SMatthew Knepley     if (diag != 0.0) {
171217ab2063SBarry Smith       for (i=0; i<N; i++) {
1713e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17147ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1715416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1716f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1717bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
17187ae801bdSBarry Smith         } else { /* in case row was completely empty */
1719f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
172017ab2063SBarry Smith         }
172117ab2063SBarry Smith       }
17223a40ed3dSBarry Smith     } else {
172317ab2063SBarry Smith       for (i=0; i<N; i++) {
1724e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1725416022c9SBarry Smith         a->ilen[rows[i]] = 0;
172617ab2063SBarry Smith       }
172717ab2063SBarry Smith     }
172888e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1729f1e2ffcdSBarry Smith   }
173043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17313a40ed3dSBarry Smith   PetscFunctionReturn(0);
173217ab2063SBarry Smith }
173317ab2063SBarry Smith 
17344a2ae208SSatish Balay #undef __FUNCT__
17356e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
17366e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17376e169961SBarry Smith {
17386e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17396e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
17406e169961SBarry Smith   PetscErrorCode    ierr;
17412b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
17426e169961SBarry Smith   const PetscScalar *xx;
17436e169961SBarry Smith   PetscScalar       *bb;
17446e169961SBarry Smith 
17456e169961SBarry Smith   PetscFunctionBegin;
17466e169961SBarry Smith   if (x && b) {
17476e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
17486e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
17492b40b63fSBarry Smith     vecs = PETSC_TRUE;
17506e169961SBarry Smith   }
17516e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
17526e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
17536e169961SBarry Smith   for (i=0; i<N; i++) {
17546e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17556e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
17566e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
17576e169961SBarry Smith   }
17586e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
17596e169961SBarry Smith     if (!zeroed[i]) {
17606e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
17616e169961SBarry Smith         if (zeroed[a->j[j]]) {
17622b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
17636e169961SBarry Smith           a->a[j] = 0.0;
17646e169961SBarry Smith         }
17656e169961SBarry Smith       }
17662b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
17676e169961SBarry Smith   }
17686e169961SBarry Smith   if (x && b) {
17696e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
17706e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
17716e169961SBarry Smith   }
17726e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
17736e169961SBarry Smith   if (diag != 0.0) {
17746e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
17756e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
17766e169961SBarry Smith     for (i=0; i<N; i++) {
17776e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
17786e169961SBarry Smith     }
17796e169961SBarry Smith   }
17806e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
17816e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17826e169961SBarry Smith   PetscFunctionReturn(0);
17836e169961SBarry Smith }
17846e169961SBarry Smith 
17856e169961SBarry Smith #undef __FUNCT__
17864a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1787a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
178817ab2063SBarry Smith {
1789416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
179097f1f81fSBarry Smith   PetscInt   *itmp;
179117ab2063SBarry Smith 
17923a40ed3dSBarry Smith   PetscFunctionBegin;
1793e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
179417ab2063SBarry Smith 
1795416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1796bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
179717ab2063SBarry Smith   if (idx) {
1798bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1799bfeeae90SHong Zhang     if (*nz) {
18004e093b46SBarry Smith       *idx = itmp;
180117ab2063SBarry Smith     }
180217ab2063SBarry Smith     else *idx = 0;
180317ab2063SBarry Smith   }
18043a40ed3dSBarry Smith   PetscFunctionReturn(0);
180517ab2063SBarry Smith }
180617ab2063SBarry Smith 
1807bfeeae90SHong Zhang /* remove this function? */
18084a2ae208SSatish Balay #undef __FUNCT__
18094a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1810a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
181117ab2063SBarry Smith {
18123a40ed3dSBarry Smith   PetscFunctionBegin;
18133a40ed3dSBarry Smith   PetscFunctionReturn(0);
181417ab2063SBarry Smith }
181517ab2063SBarry Smith 
18164a2ae208SSatish Balay #undef __FUNCT__
18174a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1818dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
181917ab2063SBarry Smith {
1820416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
182154f21887SBarry Smith   MatScalar      *v = a->a;
182236db0b34SBarry Smith   PetscReal      sum = 0.0;
18236849ba73SBarry Smith   PetscErrorCode ierr;
182497f1f81fSBarry Smith   PetscInt       i,j;
182517ab2063SBarry Smith 
18263a40ed3dSBarry Smith   PetscFunctionBegin;
182717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1828416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1829aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
183036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
183117ab2063SBarry Smith #else
183217ab2063SBarry Smith       sum += (*v)*(*v); v++;
183317ab2063SBarry Smith #endif
183417ab2063SBarry Smith     }
1835064f8208SBarry Smith     *nrm = sqrt(sum);
18363a40ed3dSBarry Smith   } else if (type == NORM_1) {
183736db0b34SBarry Smith     PetscReal *tmp;
183897f1f81fSBarry Smith     PetscInt    *jj = a->j;
1839d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1840d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1841064f8208SBarry Smith     *nrm = 0.0;
1842416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1843bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
184417ab2063SBarry Smith     }
1845d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1846064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
184717ab2063SBarry Smith     }
1848606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
18493a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1850064f8208SBarry Smith     *nrm = 0.0;
1851d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1852bfeeae90SHong Zhang       v = a->a + a->i[j];
185317ab2063SBarry Smith       sum = 0.0;
1854416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1855cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
185617ab2063SBarry Smith       }
1857064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
185817ab2063SBarry Smith     }
18593a40ed3dSBarry Smith   } else {
1860e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
186117ab2063SBarry Smith   }
18623a40ed3dSBarry Smith   PetscFunctionReturn(0);
186317ab2063SBarry Smith }
186417ab2063SBarry Smith 
18654a2ae208SSatish Balay #undef __FUNCT__
18664a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1867fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
186817ab2063SBarry Smith {
1869416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1870416022c9SBarry Smith   Mat            C;
18716849ba73SBarry Smith   PetscErrorCode ierr;
1872d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
187354f21887SBarry Smith   MatScalar      *array = a->a;
187417ab2063SBarry Smith 
18753a40ed3dSBarry Smith   PetscFunctionBegin;
1876e32f2f54SBarry 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");
1877fc4dec0aSBarry Smith 
1878fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1879d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1880d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1881bfeeae90SHong Zhang 
1882bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
18837adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1884d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
18857adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1886ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1887606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1888a541d17aSBarry Smith   } else {
1889a541d17aSBarry Smith     C = *B;
1890a541d17aSBarry Smith   }
1891a541d17aSBarry Smith 
189217ab2063SBarry Smith   for (i=0; i<m; i++) {
189317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
189487d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1895b9b97703SBarry Smith     array += len;
1896b9b97703SBarry Smith     aj    += len;
189717ab2063SBarry Smith   }
18986d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18996d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
190017ab2063SBarry Smith 
1901815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1902416022c9SBarry Smith     *B = C;
190317ab2063SBarry Smith   } else {
1904eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
190517ab2063SBarry Smith   }
19063a40ed3dSBarry Smith   PetscFunctionReturn(0);
190717ab2063SBarry Smith }
190817ab2063SBarry Smith 
1909cd0d46ebSvictorle EXTERN_C_BEGIN
1910cd0d46ebSvictorle #undef __FUNCT__
19115fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
19127087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1913cd0d46ebSvictorle {
1914cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
191554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
191654f21887SBarry Smith   MatScalar      *va,*vb;
19176849ba73SBarry Smith   PetscErrorCode ierr;
191897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1919cd0d46ebSvictorle 
1920cd0d46ebSvictorle   PetscFunctionBegin;
1921cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1922cd0d46ebSvictorle 
1923cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1924cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19255485867bSBarry Smith   if (ma!=nb || na!=mb){
19265485867bSBarry Smith     *f = PETSC_FALSE;
19275485867bSBarry Smith     PetscFunctionReturn(0);
19285485867bSBarry Smith   }
1929cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1930cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1931cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
193297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
193397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1934cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1935cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1936cd0d46ebSvictorle 
1937cd0d46ebSvictorle   *f = PETSC_TRUE;
1938cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1939cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
194097f1f81fSBarry Smith       PetscInt         idc,idr;
19415485867bSBarry Smith       PetscScalar vc,vr;
1942cd0d46ebSvictorle       /* column/row index/value */
19435485867bSBarry Smith       idc = adx[aptr[i]];
19445485867bSBarry Smith       idr = bdx[bptr[idc]];
19455485867bSBarry Smith       vc  = va[aptr[i]];
19465485867bSBarry Smith       vr  = vb[bptr[idc]];
19475485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
19485485867bSBarry Smith         *f = PETSC_FALSE;
19495485867bSBarry Smith         goto done;
1950cd0d46ebSvictorle       } else {
19515485867bSBarry Smith         aptr[i]++;
19525485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1953cd0d46ebSvictorle       }
1954cd0d46ebSvictorle     }
1955cd0d46ebSvictorle   }
1956cd0d46ebSvictorle  done:
1957cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
19583aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1959cd0d46ebSvictorle   PetscFunctionReturn(0);
1960cd0d46ebSvictorle }
1961cd0d46ebSvictorle EXTERN_C_END
1962cd0d46ebSvictorle 
19631cbb95d3SBarry Smith EXTERN_C_BEGIN
19641cbb95d3SBarry Smith #undef __FUNCT__
19651cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
19667087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
19671cbb95d3SBarry Smith {
19681cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
196954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
197054f21887SBarry Smith   MatScalar      *va,*vb;
19711cbb95d3SBarry Smith   PetscErrorCode ierr;
19721cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
19731cbb95d3SBarry Smith 
19741cbb95d3SBarry Smith   PetscFunctionBegin;
19751cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
19761cbb95d3SBarry Smith 
19771cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
19781cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19791cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
19801cbb95d3SBarry Smith     *f = PETSC_FALSE;
19811cbb95d3SBarry Smith     PetscFunctionReturn(0);
19821cbb95d3SBarry Smith   }
19831cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
19841cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
19851cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
19861cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
19871cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
19881cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
19891cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
19901cbb95d3SBarry Smith 
19911cbb95d3SBarry Smith   *f = PETSC_TRUE;
19921cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
19931cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
19941cbb95d3SBarry Smith       PetscInt         idc,idr;
19951cbb95d3SBarry Smith       PetscScalar vc,vr;
19961cbb95d3SBarry Smith       /* column/row index/value */
19971cbb95d3SBarry Smith       idc = adx[aptr[i]];
19981cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
19991cbb95d3SBarry Smith       vc  = va[aptr[i]];
20001cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
20011cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
20021cbb95d3SBarry Smith         *f = PETSC_FALSE;
20031cbb95d3SBarry Smith         goto done;
20041cbb95d3SBarry Smith       } else {
20051cbb95d3SBarry Smith         aptr[i]++;
20061cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
20071cbb95d3SBarry Smith       }
20081cbb95d3SBarry Smith     }
20091cbb95d3SBarry Smith   }
20101cbb95d3SBarry Smith  done:
20111cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
20121cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
20131cbb95d3SBarry Smith   PetscFunctionReturn(0);
20141cbb95d3SBarry Smith }
20151cbb95d3SBarry Smith EXTERN_C_END
20161cbb95d3SBarry Smith 
20179e29f15eSvictorle #undef __FUNCT__
20189e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2019ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20209e29f15eSvictorle {
2021dfbe8321SBarry Smith   PetscErrorCode ierr;
20229e29f15eSvictorle   PetscFunctionBegin;
20235485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20249e29f15eSvictorle   PetscFunctionReturn(0);
20259e29f15eSvictorle }
20269e29f15eSvictorle 
20274a2ae208SSatish Balay #undef __FUNCT__
20281cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2029ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20301cbb95d3SBarry Smith {
20311cbb95d3SBarry Smith   PetscErrorCode ierr;
20321cbb95d3SBarry Smith   PetscFunctionBegin;
20331cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20341cbb95d3SBarry Smith   PetscFunctionReturn(0);
20351cbb95d3SBarry Smith }
20361cbb95d3SBarry Smith 
20371cbb95d3SBarry Smith #undef __FUNCT__
20384a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2039dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
204017ab2063SBarry Smith {
2041416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
204254f21887SBarry Smith   PetscScalar    *l,*r,x;
204354f21887SBarry Smith   MatScalar      *v;
2044dfbe8321SBarry Smith   PetscErrorCode ierr;
2045d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
204617ab2063SBarry Smith 
20473a40ed3dSBarry Smith   PetscFunctionBegin;
204817ab2063SBarry Smith   if (ll) {
20493ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
20503ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2051e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2052e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
20531ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2054416022c9SBarry Smith     v = a->a;
205517ab2063SBarry Smith     for (i=0; i<m; i++) {
205617ab2063SBarry Smith       x = l[i];
2057416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
205817ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
205917ab2063SBarry Smith     }
20601ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2061efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
206217ab2063SBarry Smith   }
206317ab2063SBarry Smith   if (rr) {
2064e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2065e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
20661ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2067416022c9SBarry Smith     v = a->a; jj = a->j;
206817ab2063SBarry Smith     for (i=0; i<nz; i++) {
2069bfeeae90SHong Zhang       (*v++) *= r[*jj++];
207017ab2063SBarry Smith     }
20711ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2072efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
207317ab2063SBarry Smith   }
20743a40ed3dSBarry Smith   PetscFunctionReturn(0);
207517ab2063SBarry Smith }
207617ab2063SBarry Smith 
20774a2ae208SSatish Balay #undef __FUNCT__
20784a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
207997f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
208017ab2063SBarry Smith {
2081db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
20826849ba73SBarry Smith   PetscErrorCode ierr;
2083d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
208497f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
20855d0c19d7SBarry Smith   const PetscInt *irow,*icol;
20865d0c19d7SBarry Smith   PetscInt       nrows,ncols;
208797f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
208854f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2089416022c9SBarry Smith   Mat            C;
2090ace3abfcSBarry Smith   PetscBool      stride,sorted;
209117ab2063SBarry Smith 
20923a40ed3dSBarry Smith   PetscFunctionBegin;
209314ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2094e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
209514ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2096e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
209799141d43SSatish Balay 
209817ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2099b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2100b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
210117ab2063SBarry Smith 
2102fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
21030dbe5b1eSSatish Balay   ierr = PetscTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2104fee21e36SBarry Smith   if (stride && step == 1) {
210502834360SBarry Smith     /* special case of contiguous rows */
21060e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
210702834360SBarry Smith     /* loop over new rows determining lens and starting points */
210802834360SBarry Smith     for (i=0; i<nrows; i++) {
2109bfeeae90SHong Zhang       kstart  = ai[irow[i]];
2110a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
211102834360SBarry Smith       for (k=kstart; k<kend; k++) {
2112bfeeae90SHong Zhang         if (aj[k] >= first) {
211302834360SBarry Smith           starts[i] = k;
211402834360SBarry Smith           break;
211502834360SBarry Smith 	}
211602834360SBarry Smith       }
2117a2744918SBarry Smith       sum = 0;
211802834360SBarry Smith       while (k < kend) {
2119bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2120a2744918SBarry Smith         sum++;
212102834360SBarry Smith       }
2122a2744918SBarry Smith       lens[i] = sum;
212302834360SBarry Smith     }
212402834360SBarry Smith     /* create submatrix */
2125cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
212697f1f81fSBarry Smith       PetscInt n_cols,n_rows;
212708480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2128e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2129d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
213008480c60SBarry Smith       C = *B;
21313a40ed3dSBarry Smith     } else {
21327adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2133f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21347adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2135ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
213608480c60SBarry Smith     }
2137db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2138db02288aSLois Curfman McInnes 
213902834360SBarry Smith     /* loop over rows inserting into submatrix */
2140db02288aSLois Curfman McInnes     a_new    = c->a;
2141db02288aSLois Curfman McInnes     j_new    = c->j;
2142db02288aSLois Curfman McInnes     i_new    = c->i;
2143bfeeae90SHong Zhang 
214402834360SBarry Smith     for (i=0; i<nrows; i++) {
2145a2744918SBarry Smith       ii    = starts[i];
2146a2744918SBarry Smith       lensi = lens[i];
2147a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2148a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
214902834360SBarry Smith       }
215087828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2151a2744918SBarry Smith       a_new      += lensi;
2152a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
2153a2744918SBarry Smith       c->ilen[i]  = lensi;
215402834360SBarry Smith     }
21550e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
21563a40ed3dSBarry Smith   } else {
215702834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
21580e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
215997f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
21600e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
216117ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
216202834360SBarry Smith     /* determine lens of each row */
216302834360SBarry Smith     for (i=0; i<nrows; i++) {
2164bfeeae90SHong Zhang       kstart  = ai[irow[i]];
216502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
216602834360SBarry Smith       lens[i] = 0;
216702834360SBarry Smith       for (k=kstart; k<kend; k++) {
2168bfeeae90SHong Zhang         if (smap[aj[k]]) {
216902834360SBarry Smith           lens[i]++;
217002834360SBarry Smith         }
217102834360SBarry Smith       }
217202834360SBarry Smith     }
217317ab2063SBarry Smith     /* Create and fill new matrix */
2174a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2175ace3abfcSBarry Smith       PetscBool  equal;
21760f5bd95cSBarry Smith 
217799141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2178e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2179d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
21800f5bd95cSBarry Smith       if (!equal) {
2181e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
218299141d43SSatish Balay       }
2183d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
218408480c60SBarry Smith       C = *B;
21853a40ed3dSBarry Smith     } else {
21867adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2187f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21887adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2189ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
219008480c60SBarry Smith     }
219199141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
219217ab2063SBarry Smith     for (i=0; i<nrows; i++) {
219399141d43SSatish Balay       row    = irow[i];
2194bfeeae90SHong Zhang       kstart = ai[row];
219599141d43SSatish Balay       kend   = kstart + a->ilen[row];
2196bfeeae90SHong Zhang       mat_i  = c->i[i];
219799141d43SSatish Balay       mat_j  = c->j + mat_i;
219899141d43SSatish Balay       mat_a  = c->a + mat_i;
219999141d43SSatish Balay       mat_ilen = c->ilen + i;
220017ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2201bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2202ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
220399141d43SSatish Balay           *mat_a++ = a->a[k];
220499141d43SSatish Balay           (*mat_ilen)++;
220599141d43SSatish Balay 
220617ab2063SBarry Smith         }
220717ab2063SBarry Smith       }
220817ab2063SBarry Smith     }
220902834360SBarry Smith     /* Free work space */
221002834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2211606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2212606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
221302834360SBarry Smith   }
22146d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22156d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
221617ab2063SBarry Smith 
221717ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2218416022c9SBarry Smith   *B = C;
22193a40ed3dSBarry Smith   PetscFunctionReturn(0);
222017ab2063SBarry Smith }
222117ab2063SBarry Smith 
22221df811f5SHong Zhang #undef __FUNCT__
222382d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
222482d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
222582d44351SHong Zhang {
222682d44351SHong Zhang   PetscErrorCode ierr;
222782d44351SHong Zhang   Mat            B;
222882d44351SHong Zhang 
222982d44351SHong Zhang   PetscFunctionBegin;
223082d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
223182d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
223282d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
223382d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
223482d44351SHong Zhang   *subMat = B;
223582d44351SHong Zhang   PetscFunctionReturn(0);
223682d44351SHong Zhang }
223782d44351SHong Zhang 
223882d44351SHong Zhang #undef __FUNCT__
22394a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
22400481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2241a871dcd8SBarry Smith {
224263b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2243dfbe8321SBarry Smith   PetscErrorCode ierr;
224463b91edcSBarry Smith   Mat            outA;
2245ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
224663b91edcSBarry Smith 
22473a40ed3dSBarry Smith   PetscFunctionBegin;
2248e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
22491df811f5SHong Zhang 
2250b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2251b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2252a871dcd8SBarry Smith 
225363b91edcSBarry Smith   outA              = inA;
2254d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2255c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
22566bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2257c3122656SLisandro Dalcin   a->row = row;
2258c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
22596bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2260c3122656SLisandro Dalcin   a->col = col;
226163b91edcSBarry Smith 
226236db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
22636bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
22644c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
226552e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2266f0ec6fceSSatish Balay 
226794a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2268d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2269d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
227094a9d846SBarry Smith   }
227163b91edcSBarry Smith 
2272f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2273137fb511SHong Zhang   if (row_identity && col_identity) {
2274ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2275137fb511SHong Zhang   } else {
2276719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2277137fb511SHong Zhang   }
22783a40ed3dSBarry Smith   PetscFunctionReturn(0);
2279a871dcd8SBarry Smith }
2280a871dcd8SBarry Smith 
22814a2ae208SSatish Balay #undef __FUNCT__
22824a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2283f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2284f0b747eeSBarry Smith {
2285f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2286f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2287efee365bSSatish Balay   PetscErrorCode ierr;
22880805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
22893a40ed3dSBarry Smith 
22903a40ed3dSBarry Smith   PetscFunctionBegin;
2291f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2292efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
22933a40ed3dSBarry Smith   PetscFunctionReturn(0);
2294f0b747eeSBarry Smith }
2295f0b747eeSBarry Smith 
22964a2ae208SSatish Balay #undef __FUNCT__
22974a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
229897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2299cddf8d76SBarry Smith {
2300dfbe8321SBarry Smith   PetscErrorCode ierr;
230197f1f81fSBarry Smith   PetscInt       i;
2302cddf8d76SBarry Smith 
23033a40ed3dSBarry Smith   PetscFunctionBegin;
2304cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2305b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2306cddf8d76SBarry Smith   }
2307cddf8d76SBarry Smith 
2308cddf8d76SBarry Smith   for (i=0; i<n; i++) {
23096a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2310cddf8d76SBarry Smith   }
23113a40ed3dSBarry Smith   PetscFunctionReturn(0);
2312cddf8d76SBarry Smith }
2313cddf8d76SBarry Smith 
23144a2ae208SSatish Balay #undef __FUNCT__
23154a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
231697f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
23174dcbc457SBarry Smith {
2318e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23196849ba73SBarry Smith   PetscErrorCode ierr;
23205d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
23215d0c19d7SBarry Smith   const PetscInt *idx;
232297f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2323f1af5d2fSBarry Smith   PetscBT        table;
2324bbd702dbSSatish Balay 
23253a40ed3dSBarry Smith   PetscFunctionBegin;
2326d0f46423SBarry Smith   m     = A->rmap->n;
2327e4d965acSSatish Balay   ai    = a->i;
2328bfeeae90SHong Zhang   aj    = a->j;
23298a047759SSatish Balay 
2330e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
233106763907SSatish Balay 
233297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
23336831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
233406763907SSatish Balay 
2335e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2336b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2337e4d965acSSatish Balay     isz  = 0;
23386831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2339e4d965acSSatish Balay 
2340e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
23414dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2342b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2343e4d965acSSatish Balay 
2344dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2345e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2346f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
23474dcbc457SBarry Smith     }
234806763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
23496bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2350e4d965acSSatish Balay 
235104a348a9SBarry Smith     k = 0;
235204a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
235304a348a9SBarry Smith       n = isz;
235406763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2355e4d965acSSatish Balay         row   = nidx[k];
2356e4d965acSSatish Balay         start = ai[row];
2357e4d965acSSatish Balay         end   = ai[row+1];
235804a348a9SBarry Smith         for (l = start; l<end ; l++){
2359efb16452SHong Zhang           val = aj[l] ;
2360f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2361e4d965acSSatish Balay         }
2362e4d965acSSatish Balay       }
2363e4d965acSSatish Balay     }
236470b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2365e4d965acSSatish Balay   }
23666831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2367606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
23683a40ed3dSBarry Smith   PetscFunctionReturn(0);
23694dcbc457SBarry Smith }
237017ab2063SBarry Smith 
23710513a670SBarry Smith /* -------------------------------------------------------------- */
23724a2ae208SSatish Balay #undef __FUNCT__
23734a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2374dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
23750513a670SBarry Smith {
23760513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23776849ba73SBarry Smith   PetscErrorCode ierr;
23783b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
23795d0c19d7SBarry Smith   const PetscInt *row,*col;
23805d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
238156cd22aeSBarry Smith   IS             icolp,irowp;
23823b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
23833b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
23840513a670SBarry Smith 
23853a40ed3dSBarry Smith   PetscFunctionBegin;
23864c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
238756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
23884c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
238956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
23900513a670SBarry Smith 
23910513a670SBarry Smith   /* determine lengths of permuted rows */
239297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
23930513a670SBarry Smith   for (i=0; i<m; i++) {
23940513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
23950513a670SBarry Smith   }
23967adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2397f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
23987adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2399ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2400606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
24010513a670SBarry Smith 
240297f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
24030513a670SBarry Smith   for (i=0; i<m; i++) {
240432ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24050513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2406cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
240732ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24080513a670SBarry Smith   }
2409606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
24103c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
24110513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24120513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
241356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
241456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
24156bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
24166bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
24173a40ed3dSBarry Smith   PetscFunctionReturn(0);
24180513a670SBarry Smith }
24190513a670SBarry Smith 
24204a2ae208SSatish Balay #undef __FUNCT__
24214a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2422dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2423cb5b572fSBarry Smith {
2424dfbe8321SBarry Smith   PetscErrorCode ierr;
2425cb5b572fSBarry Smith 
2426cb5b572fSBarry Smith   PetscFunctionBegin;
242733f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
242833f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2429be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2430be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2431be6bf707SBarry Smith 
2432700c5bfcSBarry 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");
2433d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2434cb5b572fSBarry Smith   } else {
2435cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2436cb5b572fSBarry Smith   }
2437cb5b572fSBarry Smith   PetscFunctionReturn(0);
2438cb5b572fSBarry Smith }
2439cb5b572fSBarry Smith 
24404a2ae208SSatish Balay #undef __FUNCT__
24414a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2442dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2443273d9f13SBarry Smith {
2444dfbe8321SBarry Smith   PetscErrorCode ierr;
2445273d9f13SBarry Smith 
2446273d9f13SBarry Smith   PetscFunctionBegin;
2447ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2448273d9f13SBarry Smith   PetscFunctionReturn(0);
2449273d9f13SBarry Smith }
2450273d9f13SBarry Smith 
24514a2ae208SSatish Balay #undef __FUNCT__
24524a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2453a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
24546c0721eeSBarry Smith {
24556c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
24566c0721eeSBarry Smith   PetscFunctionBegin;
24576c0721eeSBarry Smith   *array = a->a;
24586c0721eeSBarry Smith   PetscFunctionReturn(0);
24596c0721eeSBarry Smith }
24606c0721eeSBarry Smith 
24614a2ae208SSatish Balay #undef __FUNCT__
24624a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2463dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
24646c0721eeSBarry Smith {
24656c0721eeSBarry Smith   PetscFunctionBegin;
24666c0721eeSBarry Smith   PetscFunctionReturn(0);
24676c0721eeSBarry Smith }
2468273d9f13SBarry Smith 
2469ee4f033dSBarry Smith #undef __FUNCT__
2470ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2471dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2472ee4f033dSBarry Smith {
24736849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
24746849ba73SBarry Smith   PetscErrorCode ierr;
247597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2476efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
247787828ca2SBarry Smith   PetscScalar    *vscale_array;
2478ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2479ee4f033dSBarry Smith   Vec            w1,w2,w3;
2480ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2481ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2482ee4f033dSBarry Smith 
2483ee4f033dSBarry Smith   PetscFunctionBegin;
2484ee4f033dSBarry Smith   if (!coloring->w1) {
2485ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
248652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2487ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
248852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2489ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
249052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2491ee4f033dSBarry Smith   }
2492ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2493ee4f033dSBarry Smith 
2494ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2495acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2496ee4f033dSBarry Smith   if (flg) {
2497ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2498ee4f033dSBarry Smith   } else {
2499ace3abfcSBarry Smith     PetscBool  assembled;
25000b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
25010b9b6f31SBarry Smith     if (assembled) {
2502ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2503ee4f033dSBarry Smith     }
25040b9b6f31SBarry Smith   }
2505ee4f033dSBarry Smith 
2506ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2507ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2508ee4f033dSBarry Smith 
2509ee4f033dSBarry Smith   /*
2510ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2511ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2512ee4f033dSBarry Smith   */
2513ee4f033dSBarry Smith   if (coloring->F) {
2514ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2515ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2516ee4f033dSBarry Smith     if (m1 != m2) {
2517ee4f033dSBarry Smith       coloring->F = 0;
2518ee4f033dSBarry Smith     }
2519ee4f033dSBarry Smith   }
2520ee4f033dSBarry Smith 
2521ee4f033dSBarry Smith   if (coloring->F) {
2522ee4f033dSBarry Smith     w1          = coloring->F;
2523ee4f033dSBarry Smith     coloring->F = 0;
2524ee4f033dSBarry Smith   } else {
252566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2526ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
252766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2528ee4f033dSBarry Smith   }
2529ee4f033dSBarry Smith 
2530ee4f033dSBarry Smith   /*
2531ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2532ee4f033dSBarry Smith   */
25331ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
25341ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2535ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2536ee4f033dSBarry Smith     /*
2537ee4f033dSBarry Smith        Loop over each column associated with color adding the
2538ee4f033dSBarry Smith        perturbation to the vector w3.
2539ee4f033dSBarry Smith     */
2540ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2541ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2542ee4f033dSBarry Smith       dx  = xx[col];
2543ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2544ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2545ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2546ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2547ee4f033dSBarry Smith #else
2548ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2549ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2550ee4f033dSBarry Smith #endif
2551ee4f033dSBarry Smith       dx                *= epsilon;
2552ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2553ee4f033dSBarry Smith     }
2554ee4f033dSBarry Smith   }
25551ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2556ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2557ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2558ee4f033dSBarry Smith 
2559ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2560ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2561ee4f033dSBarry Smith 
2562ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2563ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2564ee4f033dSBarry Smith 
25651ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2566ee4f033dSBarry Smith   /*
2567ee4f033dSBarry Smith       Loop over each color
2568ee4f033dSBarry Smith   */
2569ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
257049b058dcSBarry Smith     coloring->currentcolor = k;
2571ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
25721ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2573ee4f033dSBarry Smith     /*
2574ee4f033dSBarry Smith        Loop over each column associated with color adding the
2575ee4f033dSBarry Smith        perturbation to the vector w3.
2576ee4f033dSBarry Smith     */
2577ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2578ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2579ee4f033dSBarry Smith       dx  = xx[col];
25805b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2581ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2582ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2583ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2584ee4f033dSBarry Smith #else
2585ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2586ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2587ee4f033dSBarry Smith #endif
2588ee4f033dSBarry Smith       dx            *= epsilon;
2589e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2590ee4f033dSBarry Smith       w3_array[col] += dx;
2591ee4f033dSBarry Smith     }
25921ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2593ee4f033dSBarry Smith 
2594ee4f033dSBarry Smith     /*
2595ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2596ee4f033dSBarry Smith     */
2597ee4f033dSBarry Smith 
259866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2599ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
260066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2601efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2602ee4f033dSBarry Smith 
2603ee4f033dSBarry Smith     /*
2604ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2605ee4f033dSBarry Smith     */
26061ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2607ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2608ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2609ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2610ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2611ee4f033dSBarry Smith       srow   = row + start;
2612ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2613ee4f033dSBarry Smith     }
26141ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2615ee4f033dSBarry Smith   }
261649b058dcSBarry Smith   coloring->currentcolor = k;
26171ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
26181ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2619ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2620ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2621ee4f033dSBarry Smith   PetscFunctionReturn(0);
2622ee4f033dSBarry Smith }
2623ee4f033dSBarry Smith 
26248229c054SShri Abhyankar /*
26258229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26268229c054SShri Abhyankar    have different nonzero structure.
26278229c054SShri Abhyankar */
2628ac90fabeSBarry Smith #undef __FUNCT__
26298229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
26308229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2631ec7775f6SShri Abhyankar {
26328229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2633ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2634ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2635ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2636ec7775f6SShri Abhyankar 
2637ec7775f6SShri Abhyankar   PetscFunctionBegin;
2638ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2639ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
26408af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
26418af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
26428af7cee1SJed Brown     nnz[i] = 0;
26438af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
26448af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
26458af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
26468af7cee1SJed Brown       nnz[i]++;
26478af7cee1SJed Brown     }
26488af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2649ec7775f6SShri Abhyankar   }
2650ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2651ec7775f6SShri Abhyankar }
2652ec7775f6SShri Abhyankar 
2653ec7775f6SShri Abhyankar #undef __FUNCT__
2654ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2655f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2656ac90fabeSBarry Smith {
2657dfbe8321SBarry Smith   PetscErrorCode ierr;
265897f1f81fSBarry Smith   PetscInt       i;
2659ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
26600805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2661ac90fabeSBarry Smith 
2662ac90fabeSBarry Smith   PetscFunctionBegin;
2663ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2664f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2665f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2666c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2667a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2668a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
26696bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2670a30b2313SHong Zhang     }
2671a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2672d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2673a30b2313SHong Zhang       y->XtoY = X;
2674407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2675c537a176SHong Zhang     }
2676f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
26771e2582c4SBarry 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);
2678ac90fabeSBarry Smith   } else {
26798229c054SShri Abhyankar     Mat      B;
26808229c054SShri Abhyankar     PetscInt *nnz;
268116b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2682ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2683bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
26844aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2685ec7775f6SShri Abhyankar     ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
26868229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
26878229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2688ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2689ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
26908229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2691ac90fabeSBarry Smith   }
2692ac90fabeSBarry Smith   PetscFunctionReturn(0);
2693ac90fabeSBarry Smith }
2694ac90fabeSBarry Smith 
2695521d7252SBarry Smith #undef __FUNCT__
2696521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2697521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2698521d7252SBarry Smith {
269941c166b1SJed Brown   PetscErrorCode ierr;
270041c166b1SJed Brown 
2701521d7252SBarry Smith   PetscFunctionBegin;
270241c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
270341c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2704521d7252SBarry Smith   PetscFunctionReturn(0);
2705521d7252SBarry Smith }
2706521d7252SBarry Smith 
2707354c94deSBarry Smith #undef __FUNCT__
2708354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27097087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2710354c94deSBarry Smith {
2711354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2712354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2713354c94deSBarry Smith   PetscInt    i,nz;
2714354c94deSBarry Smith   PetscScalar *a;
2715354c94deSBarry Smith 
2716354c94deSBarry Smith   PetscFunctionBegin;
2717354c94deSBarry Smith   nz = aij->nz;
2718354c94deSBarry Smith   a  = aij->a;
2719354c94deSBarry Smith   for (i=0; i<nz; i++) {
2720354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2721354c94deSBarry Smith   }
2722354c94deSBarry Smith #else
2723354c94deSBarry Smith   PetscFunctionBegin;
2724354c94deSBarry Smith #endif
2725354c94deSBarry Smith   PetscFunctionReturn(0);
2726354c94deSBarry Smith }
2727354c94deSBarry Smith 
2728e34fafa9SBarry Smith #undef __FUNCT__
2729985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2730985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2731e34fafa9SBarry Smith {
2732e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2733e34fafa9SBarry Smith   PetscErrorCode ierr;
2734d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2735e34fafa9SBarry Smith   PetscReal      atmp;
2736985db425SBarry Smith   PetscScalar    *x;
2737e34fafa9SBarry Smith   MatScalar      *aa;
2738e34fafa9SBarry Smith 
2739e34fafa9SBarry Smith   PetscFunctionBegin;
2740e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2741e34fafa9SBarry Smith   aa   = a->a;
2742e34fafa9SBarry Smith   ai   = a->i;
2743e34fafa9SBarry Smith   aj   = a->j;
2744e34fafa9SBarry Smith 
2745985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2746e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2747e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2748e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2749e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2750e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27519189402eSHong Zhang     x[i] = 0.0;
2752e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2753985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2754985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2755985db425SBarry Smith       aa++; aj++;
2756985db425SBarry Smith     }
2757985db425SBarry Smith   }
2758985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2759985db425SBarry Smith   PetscFunctionReturn(0);
2760985db425SBarry Smith }
2761985db425SBarry Smith 
2762985db425SBarry Smith #undef __FUNCT__
2763985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2764985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2765985db425SBarry Smith {
2766985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2767985db425SBarry Smith   PetscErrorCode ierr;
2768d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2769985db425SBarry Smith   PetscScalar    *x;
2770985db425SBarry Smith   MatScalar      *aa;
2771985db425SBarry Smith 
2772985db425SBarry Smith   PetscFunctionBegin;
2773e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2774985db425SBarry Smith   aa   = a->a;
2775985db425SBarry Smith   ai   = a->i;
2776985db425SBarry Smith   aj   = a->j;
2777985db425SBarry Smith 
2778985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2779985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2780985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2781e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2782985db425SBarry Smith   for (i=0; i<m; i++) {
2783985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2784d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2785985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2786985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2787985db425SBarry Smith       x[i] = 0.0;
2788985db425SBarry Smith       if (idx) {
2789985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2790985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2791985db425SBarry Smith           if (aj[j] > j) {
2792985db425SBarry Smith             idx[i] = j;
2793985db425SBarry Smith             break;
2794985db425SBarry Smith           }
2795985db425SBarry Smith         }
2796985db425SBarry Smith       }
2797985db425SBarry Smith     }
2798985db425SBarry Smith     for (j=0; j<ncols; j++){
2799985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2800985db425SBarry Smith       aa++; aj++;
2801985db425SBarry Smith     }
2802985db425SBarry Smith   }
2803985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2804985db425SBarry Smith   PetscFunctionReturn(0);
2805985db425SBarry Smith }
2806985db425SBarry Smith 
2807985db425SBarry Smith #undef __FUNCT__
2808c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2809c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2810c87e5d42SMatthew Knepley {
2811c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2812c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2813c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2814c87e5d42SMatthew Knepley   PetscReal      atmp;
2815c87e5d42SMatthew Knepley   PetscScalar    *x;
2816c87e5d42SMatthew Knepley   MatScalar      *aa;
2817c87e5d42SMatthew Knepley 
2818c87e5d42SMatthew Knepley   PetscFunctionBegin;
2819e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2820c87e5d42SMatthew Knepley   aa   = a->a;
2821c87e5d42SMatthew Knepley   ai   = a->i;
2822c87e5d42SMatthew Knepley   aj   = a->j;
2823c87e5d42SMatthew Knepley 
2824c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2825c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2826c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2827e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2828c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2829c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2830289a08f5SMatthew Knepley     if (ncols) {
2831289a08f5SMatthew Knepley       /* Get first nonzero */
2832289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2833289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2834289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2835289a08f5SMatthew Knepley       }
2836289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2837289a08f5SMatthew Knepley     } else {
2838289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2839289a08f5SMatthew Knepley     }
2840c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2841c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2842289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2843c87e5d42SMatthew Knepley       aa++; aj++;
2844c87e5d42SMatthew Knepley     }
2845c87e5d42SMatthew Knepley   }
2846c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2847c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2848c87e5d42SMatthew Knepley }
2849c87e5d42SMatthew Knepley 
2850c87e5d42SMatthew Knepley #undef __FUNCT__
2851985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2852985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2853985db425SBarry Smith {
2854985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2855985db425SBarry Smith   PetscErrorCode ierr;
2856d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2857985db425SBarry Smith   PetscScalar    *x;
2858985db425SBarry Smith   MatScalar      *aa;
2859985db425SBarry Smith 
2860985db425SBarry Smith   PetscFunctionBegin;
2861e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2862985db425SBarry Smith   aa   = a->a;
2863985db425SBarry Smith   ai   = a->i;
2864985db425SBarry Smith   aj   = a->j;
2865985db425SBarry Smith 
2866985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2867985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2868985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2869e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2870985db425SBarry Smith   for (i=0; i<m; i++) {
2871985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2872d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2873985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2874985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2875985db425SBarry Smith       x[i] = 0.0;
2876985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2877985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2878985db425SBarry Smith         for (j=0;j<ncols;j++) {
2879985db425SBarry Smith           if (aj[j] > j) {
2880985db425SBarry Smith             idx[i] = j;
2881985db425SBarry Smith             break;
2882985db425SBarry Smith           }
2883985db425SBarry Smith         }
2884985db425SBarry Smith       }
2885985db425SBarry Smith     }
2886985db425SBarry Smith     for (j=0; j<ncols; j++){
2887985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2888985db425SBarry Smith       aa++; aj++;
2889e34fafa9SBarry Smith     }
2890e34fafa9SBarry Smith   }
2891e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2892e34fafa9SBarry Smith   PetscFunctionReturn(0);
2893e34fafa9SBarry Smith }
28947087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2895682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
28960a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2897cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2898cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2899cb5b572fSBarry Smith        MatMult_SeqAIJ,
290097304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
29017c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
29027c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2903db4efbfdSBarry Smith        0,
2904db4efbfdSBarry Smith        0,
2905db4efbfdSBarry Smith        0,
2906db4efbfdSBarry Smith /*10*/ 0,
2907cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2908cb5b572fSBarry Smith        0,
290941f059aeSBarry Smith        MatSOR_SeqAIJ,
291017ab2063SBarry Smith        MatTranspose_SeqAIJ,
291197304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2912cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2913cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2914cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2915cb5b572fSBarry Smith        MatNorm_SeqAIJ,
291697304618SKris Buschelman /*20*/ 0,
2917cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2918cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2919cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2920d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2921db4efbfdSBarry Smith        0,
2922db4efbfdSBarry Smith        0,
2923db4efbfdSBarry Smith        0,
2924db4efbfdSBarry Smith        0,
2925d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2926db4efbfdSBarry Smith        0,
2927db4efbfdSBarry Smith        0,
29286c0721eeSBarry Smith        MatGetArray_SeqAIJ,
29296c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2930d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2931cb5b572fSBarry Smith        0,
2932cb5b572fSBarry Smith        0,
2933cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2934cb5b572fSBarry Smith        0,
2935d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2936cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2937cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2938cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2939cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2940d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2941cb5b572fSBarry Smith        MatScale_SeqAIJ,
2942cb5b572fSBarry Smith        0,
294379299369SBarry Smith        MatDiagonalSet_SeqAIJ,
29446e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
2945d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
29463b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
29473b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
29483b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2949a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2950d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2951b9617806SBarry Smith        0,
29520513a670SBarry Smith        0,
2953cda55fadSBarry Smith        MatPermute_SeqAIJ,
2954cda55fadSBarry Smith        0,
2955d519adbfSMatthew Knepley /*59*/ 0,
2956b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2957b9b97703SBarry Smith        MatView_SeqAIJ,
2958357abbc8SBarry Smith        0,
2959ee4f033dSBarry Smith        0,
2960d519adbfSMatthew Knepley /*64*/ 0,
2961ee4f033dSBarry Smith        0,
2962ee4f033dSBarry Smith        0,
2963ee4f033dSBarry Smith        0,
2964ee4f033dSBarry Smith        0,
2965d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2966c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2967ee4f033dSBarry Smith        0,
2968ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2969dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2970ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2971dcf5cc72SBarry Smith #else
2972dcf5cc72SBarry Smith        0,
2973dcf5cc72SBarry Smith #endif
2974d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
29753acb8795SBarry Smith        MatFDColoringApply_AIJ,
297697304618SKris Buschelman        0,
297797304618SKris Buschelman        0,
297897304618SKris Buschelman        0,
29796ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
298097304618SKris Buschelman        0,
298197304618SKris Buschelman        0,
298297304618SKris Buschelman        0,
2983bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2984d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
29851cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
29866284ec50SHong Zhang        0,
29876284ec50SHong Zhang        0,
2988bc011b1eSHong Zhang        0,
2989d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
299026be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
299126be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2992d439da42SKris Buschelman        MatPtAP_Basic,
29937ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2994d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2995bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2996bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2997bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
29987ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2999d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3000609c6c4dSKris Buschelman        0,
3001609c6c4dSKris Buschelman        0,
300287d4246cSBarry Smith        MatConjugate_SeqAIJ,
300387d4246cSBarry Smith        0,
3004d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
300599cafbc1SBarry Smith        MatRealPart_SeqAIJ,
3006f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
3007f5edf698SHong Zhang        0,
30082bebee5dSHong Zhang        0,
3009cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
3010985db425SBarry Smith        0,
30112af78befSBarry Smith        MatGetRowMin_SeqAIJ,
30122af78befSBarry Smith        0,
3013599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
3014d519adbfSMatthew Knepley /*114*/0,
3015599ef60dSHong Zhang        0,
30163c2a7987SHong Zhang        0,
3017fe97e370SBarry Smith        0,
3018fbdbba38SShri Abhyankar        0,
3019fbdbba38SShri Abhyankar /*119*/0,
3020fbdbba38SShri Abhyankar        0,
3021fbdbba38SShri Abhyankar        0,
302282d44351SHong Zhang        0,
3023b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
30240716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
30250716a85fSBarry Smith        MatGetColumnNorms_SeqAIJ
30269e29f15eSvictorle };
302717ab2063SBarry Smith 
3028fb2e594dSBarry Smith EXTERN_C_BEGIN
30294a2ae208SSatish Balay #undef __FUNCT__
30304a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
30317087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3032bef8e0ddSBarry Smith {
3033bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
303497f1f81fSBarry Smith   PetscInt   i,nz,n;
3035bef8e0ddSBarry Smith 
3036bef8e0ddSBarry Smith   PetscFunctionBegin;
3037bef8e0ddSBarry Smith 
3038bef8e0ddSBarry Smith   nz = aij->maxnz;
3039d0f46423SBarry Smith   n  = mat->rmap->n;
3040bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3041bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3042bef8e0ddSBarry Smith   }
3043bef8e0ddSBarry Smith   aij->nz = nz;
3044bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3045bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3046bef8e0ddSBarry Smith   }
3047bef8e0ddSBarry Smith 
3048bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3049bef8e0ddSBarry Smith }
3050fb2e594dSBarry Smith EXTERN_C_END
3051bef8e0ddSBarry Smith 
30524a2ae208SSatish Balay #undef __FUNCT__
30534a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3054bef8e0ddSBarry Smith /*@
3055bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3056bef8e0ddSBarry Smith        in the matrix.
3057bef8e0ddSBarry Smith 
3058bef8e0ddSBarry Smith   Input Parameters:
3059bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3060bef8e0ddSBarry Smith -  indices - the column indices
3061bef8e0ddSBarry Smith 
306215091d37SBarry Smith   Level: advanced
306315091d37SBarry Smith 
3064bef8e0ddSBarry Smith   Notes:
3065bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3066bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3067bef8e0ddSBarry Smith   of the MatSetValues() operation.
3068bef8e0ddSBarry Smith 
3069bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3070d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3071bef8e0ddSBarry Smith 
3072bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3073bef8e0ddSBarry Smith 
3074b9617806SBarry Smith     The indices should start with zero, not one.
3075b9617806SBarry Smith 
3076bef8e0ddSBarry Smith @*/
30777087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3078bef8e0ddSBarry Smith {
30794ac538c5SBarry Smith   PetscErrorCode ierr;
3080bef8e0ddSBarry Smith 
3081bef8e0ddSBarry Smith   PetscFunctionBegin;
30820700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
30834482741eSBarry Smith   PetscValidPointer(indices,2);
30844ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
3085bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3086bef8e0ddSBarry Smith }
3087bef8e0ddSBarry Smith 
3088be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3089be6bf707SBarry Smith 
3090fb2e594dSBarry Smith EXTERN_C_BEGIN
30914a2ae208SSatish Balay #undef __FUNCT__
30924a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
30937087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3094be6bf707SBarry Smith {
3095be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
30966849ba73SBarry Smith   PetscErrorCode ierr;
3097d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3098be6bf707SBarry Smith 
3099be6bf707SBarry Smith   PetscFunctionBegin;
3100be6bf707SBarry Smith   if (aij->nonew != 1) {
3101e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3102be6bf707SBarry Smith   }
3103be6bf707SBarry Smith 
3104be6bf707SBarry Smith   /* allocate space for values if not already there */
3105be6bf707SBarry Smith   if (!aij->saved_values) {
310687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
31079518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3108be6bf707SBarry Smith   }
3109be6bf707SBarry Smith 
3110be6bf707SBarry Smith   /* copy values over */
311187828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3112be6bf707SBarry Smith   PetscFunctionReturn(0);
3113be6bf707SBarry Smith }
3114fb2e594dSBarry Smith EXTERN_C_END
3115be6bf707SBarry Smith 
31164a2ae208SSatish Balay #undef __FUNCT__
3117b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3118be6bf707SBarry Smith /*@
3119be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3120be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3121be6bf707SBarry Smith        nonlinear portion.
3122be6bf707SBarry Smith 
3123be6bf707SBarry Smith    Collect on Mat
3124be6bf707SBarry Smith 
3125be6bf707SBarry Smith   Input Parameters:
31260e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3127be6bf707SBarry Smith 
312815091d37SBarry Smith   Level: advanced
312915091d37SBarry Smith 
3130be6bf707SBarry Smith   Common Usage, with SNESSolve():
3131be6bf707SBarry Smith $    Create Jacobian matrix
3132be6bf707SBarry Smith $    Set linear terms into matrix
3133be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3134be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3135be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3136512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3137be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3138be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3139be6bf707SBarry Smith $    In your Jacobian routine
3140be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3141be6bf707SBarry Smith $      Set nonlinear terms in matrix
3142be6bf707SBarry Smith 
3143be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3144be6bf707SBarry Smith $    // build linear portion of Jacobian
3145512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3146be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3147be6bf707SBarry Smith $    loop over nonlinear iterations
3148be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3149be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3150be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3151be6bf707SBarry Smith $       Solve linear system with Jacobian
3152be6bf707SBarry Smith $    endloop
3153be6bf707SBarry Smith 
3154be6bf707SBarry Smith   Notes:
3155be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3156512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3157be6bf707SBarry Smith     calling this routine.
3158be6bf707SBarry Smith 
31590c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
31600c468ba9SBarry Smith     and does not allocated additional space.
31610c468ba9SBarry Smith 
3162be6bf707SBarry Smith .seealso: MatRetrieveValues()
3163be6bf707SBarry Smith 
3164be6bf707SBarry Smith @*/
31657087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3166be6bf707SBarry Smith {
31674ac538c5SBarry Smith   PetscErrorCode ierr;
3168be6bf707SBarry Smith 
3169be6bf707SBarry Smith   PetscFunctionBegin;
31700700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3171e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3172e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
31734ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3174be6bf707SBarry Smith   PetscFunctionReturn(0);
3175be6bf707SBarry Smith }
3176be6bf707SBarry Smith 
3177fb2e594dSBarry Smith EXTERN_C_BEGIN
31784a2ae208SSatish Balay #undef __FUNCT__
31794a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
31807087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3181be6bf707SBarry Smith {
3182be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
31836849ba73SBarry Smith   PetscErrorCode ierr;
3184d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3185be6bf707SBarry Smith 
3186be6bf707SBarry Smith   PetscFunctionBegin;
3187be6bf707SBarry Smith   if (aij->nonew != 1) {
3188e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3189be6bf707SBarry Smith   }
3190be6bf707SBarry Smith   if (!aij->saved_values) {
3191e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3192be6bf707SBarry Smith   }
3193be6bf707SBarry Smith   /* copy values over */
319487828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3195be6bf707SBarry Smith   PetscFunctionReturn(0);
3196be6bf707SBarry Smith }
3197fb2e594dSBarry Smith EXTERN_C_END
3198be6bf707SBarry Smith 
31994a2ae208SSatish Balay #undef __FUNCT__
32004a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3201be6bf707SBarry Smith /*@
3202be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3203be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3204be6bf707SBarry Smith        nonlinear portion.
3205be6bf707SBarry Smith 
3206be6bf707SBarry Smith    Collect on Mat
3207be6bf707SBarry Smith 
3208be6bf707SBarry Smith   Input Parameters:
3209be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3210be6bf707SBarry Smith 
321115091d37SBarry Smith   Level: advanced
321215091d37SBarry Smith 
3213be6bf707SBarry Smith .seealso: MatStoreValues()
3214be6bf707SBarry Smith 
3215be6bf707SBarry Smith @*/
32167087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3217be6bf707SBarry Smith {
32184ac538c5SBarry Smith   PetscErrorCode ierr;
3219be6bf707SBarry Smith 
3220be6bf707SBarry Smith   PetscFunctionBegin;
32210700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3222e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3223e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
32244ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3225be6bf707SBarry Smith   PetscFunctionReturn(0);
3226be6bf707SBarry Smith }
3227be6bf707SBarry Smith 
3228f83d6046SBarry Smith 
3229be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
32304a2ae208SSatish Balay #undef __FUNCT__
32314a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
323217ab2063SBarry Smith /*@C
3233682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
32340d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
32356e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
323651c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
32372bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
323817ab2063SBarry Smith 
3239db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3240db81eaa0SLois Curfman McInnes 
324117ab2063SBarry Smith    Input Parameters:
3242db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
324317ab2063SBarry Smith .  m - number of rows
324417ab2063SBarry Smith .  n - number of columns
324517ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
324651c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
32472bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
324817ab2063SBarry Smith 
324917ab2063SBarry Smith    Output Parameter:
3250416022c9SBarry Smith .  A - the matrix
325117ab2063SBarry Smith 
3252175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3253ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3254175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3255175b88e8SBarry Smith 
3256b259b22eSLois Curfman McInnes    Notes:
325749a6f317SBarry Smith    If nnz is given then nz is ignored
325849a6f317SBarry Smith 
325917ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
326017ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
32610002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
326244cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
326317ab2063SBarry Smith 
326417ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3265a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
32663d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
32676da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
326817ab2063SBarry Smith 
3269682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
32704fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3271682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
32726c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
32736c7ebb05SLois Curfman McInnes 
32746c7ebb05SLois Curfman McInnes    Options Database Keys:
3275698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
32769db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
327717ab2063SBarry Smith 
3278027ccd11SLois Curfman McInnes    Level: intermediate
3279027ccd11SLois Curfman McInnes 
328036db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
328136db0b34SBarry Smith 
328217ab2063SBarry Smith @*/
32837087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
328417ab2063SBarry Smith {
3285dfbe8321SBarry Smith   PetscErrorCode ierr;
32866945ee14SBarry Smith 
32873a40ed3dSBarry Smith   PetscFunctionBegin;
3288f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3289117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3290c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3291d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3292273d9f13SBarry Smith   PetscFunctionReturn(0);
3293273d9f13SBarry Smith }
3294273d9f13SBarry Smith 
32954a2ae208SSatish Balay #undef __FUNCT__
32964a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3297273d9f13SBarry Smith /*@C
3298273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3299273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3300273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3301273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3302273d9f13SBarry Smith 
3303273d9f13SBarry Smith    Collective on MPI_Comm
3304273d9f13SBarry Smith 
3305273d9f13SBarry Smith    Input Parameters:
3306117016b1SBarry Smith +  B - The matrix-free
3307273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3308273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3309273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3310273d9f13SBarry Smith 
3311273d9f13SBarry Smith    Notes:
331249a6f317SBarry Smith      If nnz is given then nz is ignored
331349a6f317SBarry Smith 
3314273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3315273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3316273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3317273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3318273d9f13SBarry Smith 
3319273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3320273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3321273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3322273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3323273d9f13SBarry Smith 
3324aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3325aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3326aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3327aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3328aa95bbe8SBarry Smith 
3329a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3330a96a251dSBarry Smith    entries or columns indices
3331a96a251dSBarry Smith 
3332273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3333273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3334273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3335273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3336273d9f13SBarry Smith 
3337273d9f13SBarry Smith    Options Database Keys:
3338698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3339698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3340273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3341273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3342273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3343273d9f13SBarry Smith 
3344273d9f13SBarry Smith    Level: intermediate
3345273d9f13SBarry Smith 
3346aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3347273d9f13SBarry Smith 
3348273d9f13SBarry Smith @*/
33497087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3350273d9f13SBarry Smith {
33514ac538c5SBarry Smith   PetscErrorCode ierr;
3352a23d5eceSKris Buschelman 
3353a23d5eceSKris Buschelman   PetscFunctionBegin;
33544ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3355a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3356a23d5eceSKris Buschelman }
3357a23d5eceSKris Buschelman 
3358a23d5eceSKris Buschelman EXTERN_C_BEGIN
3359a23d5eceSKris Buschelman #undef __FUNCT__
3360a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
33617087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3362a23d5eceSKris Buschelman {
3363273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3364ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
33656849ba73SBarry Smith   PetscErrorCode ierr;
336697f1f81fSBarry Smith   PetscInt       i;
3367273d9f13SBarry Smith 
3368273d9f13SBarry Smith   PetscFunctionBegin;
3369d5d45c9bSBarry Smith 
3370a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3371c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3372c461c341SBarry Smith     nz             = 0;
3373c461c341SBarry Smith   }
3374c461c341SBarry Smith 
337526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
337626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
337726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
337826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3379899cda47SBarry Smith 
3380435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3381e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3382b73539f3SBarry Smith   if (nnz) {
3383d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3384e32f2f54SBarry 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]);
3385e32f2f54SBarry 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);
3386b73539f3SBarry Smith     }
3387b73539f3SBarry Smith   }
3388b73539f3SBarry Smith 
3389273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3390273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3391273d9f13SBarry Smith 
3392ab93d7beSBarry Smith   if (!skipallocation) {
33932ee49352SLisandro Dalcin     if (!b->imax) {
3394d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3395d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
33962ee49352SLisandro Dalcin     }
3397273d9f13SBarry Smith     if (!nnz) {
3398435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3399c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3400d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3401d0f46423SBarry Smith       nz = nz*B->rmap->n;
3402273d9f13SBarry Smith     } else {
3403273d9f13SBarry Smith       nz = 0;
3404d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3405273d9f13SBarry Smith     }
3406ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3407d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3408ab93d7beSBarry Smith 
3409273d9f13SBarry Smith     /* allocate the matrix space */
34102ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3411d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3412d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3413bfeeae90SHong Zhang     b->i[0] = 0;
3414d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
34155da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
34165da197adSKris Buschelman     }
3417273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3418e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3419e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3420c461c341SBarry Smith   } else {
3421e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3422e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3423c461c341SBarry Smith   }
3424273d9f13SBarry Smith 
3425273d9f13SBarry Smith   b->nz                = 0;
3426273d9f13SBarry Smith   b->maxnz             = nz;
3427273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3428273d9f13SBarry Smith   PetscFunctionReturn(0);
3429273d9f13SBarry Smith }
3430a23d5eceSKris Buschelman EXTERN_C_END
3431273d9f13SBarry Smith 
3432a1661176SMatthew Knepley #undef  __FUNCT__
3433a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
343458d36128SBarry Smith /*@
3435a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3436a1661176SMatthew Knepley 
3437a1661176SMatthew Knepley    Input Parameters:
3438a1661176SMatthew Knepley +  B - the matrix
3439a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3440a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3441a1661176SMatthew Knepley -  v - optional values in the matrix
3442a1661176SMatthew Knepley 
3443a1661176SMatthew Knepley    Level: developer
3444a1661176SMatthew Knepley 
344558d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
344658d36128SBarry Smith 
3447a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3448a1661176SMatthew Knepley 
3449a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3450a1661176SMatthew Knepley @*/
3451a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3452a1661176SMatthew Knepley {
3453a1661176SMatthew Knepley   PetscErrorCode ierr;
3454a1661176SMatthew Knepley 
3455a1661176SMatthew Knepley   PetscFunctionBegin;
34560700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
34574ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3458a1661176SMatthew Knepley   PetscFunctionReturn(0);
3459a1661176SMatthew Knepley }
3460a1661176SMatthew Knepley 
3461a1661176SMatthew Knepley EXTERN_C_BEGIN
3462a1661176SMatthew Knepley #undef  __FUNCT__
3463a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
34647087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3465a1661176SMatthew Knepley {
3466a1661176SMatthew Knepley   PetscInt       i;
3467a1661176SMatthew Knepley   PetscInt       m,n;
3468a1661176SMatthew Knepley   PetscInt       nz;
3469a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3470a1661176SMatthew Knepley   PetscScalar    *values;
3471a1661176SMatthew Knepley   PetscErrorCode ierr;
3472a1661176SMatthew Knepley 
3473a1661176SMatthew Knepley   PetscFunctionBegin;
3474a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3475a1661176SMatthew Knepley 
347665e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3477a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3478a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3479b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3480a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
348165e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3482a1661176SMatthew Knepley     nnz[i] = nz;
3483a1661176SMatthew Knepley   }
3484a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3485a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3486a1661176SMatthew Knepley 
3487a1661176SMatthew Knepley   if (v) {
3488a1661176SMatthew Knepley     values = (PetscScalar*) v;
3489a1661176SMatthew Knepley   } else {
34900e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3491a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3492a1661176SMatthew Knepley   }
3493a1661176SMatthew Knepley 
3494a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3495b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3496b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3497a1661176SMatthew Knepley   }
3498a1661176SMatthew Knepley 
3499a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3500a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3501a1661176SMatthew Knepley 
3502a1661176SMatthew Knepley   if (!v) {
3503a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3504a1661176SMatthew Knepley   }
3505a1661176SMatthew Knepley   PetscFunctionReturn(0);
3506a1661176SMatthew Knepley }
3507a1661176SMatthew Knepley EXTERN_C_END
3508a1661176SMatthew Knepley 
3509c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3510c6db04a5SJed Brown #include <private/petscaxpy.h>
3511170fe5c8SBarry Smith 
3512170fe5c8SBarry Smith #undef __FUNCT__
3513170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3514170fe5c8SBarry Smith /*
3515170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3516170fe5c8SBarry Smith 
3517170fe5c8SBarry Smith                n                       p                          p
3518170fe5c8SBarry Smith         (              )       (              )         (                  )
3519170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3520170fe5c8SBarry Smith         (              )       (              )         (                  )
3521170fe5c8SBarry Smith 
3522170fe5c8SBarry Smith */
3523170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3524170fe5c8SBarry Smith {
3525170fe5c8SBarry Smith   PetscErrorCode     ierr;
3526170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3527170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3528170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
35291de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3530170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3531170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3532170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3533170fe5c8SBarry Smith 
3534170fe5c8SBarry Smith   PetscFunctionBegin;
3535d0f46423SBarry Smith   m = A->rmap->n;
3536d0f46423SBarry Smith   n = A->cmap->n;
3537d0f46423SBarry Smith   p = B->cmap->n;
3538170fe5c8SBarry Smith   a = sub_a->v;
3539170fe5c8SBarry Smith   b = sub_b->a;
3540170fe5c8SBarry Smith   c = sub_c->v;
3541170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3542170fe5c8SBarry Smith 
3543170fe5c8SBarry Smith   ii  = sub_b->i;
3544170fe5c8SBarry Smith   idx = sub_b->j;
3545170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3546170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3547170fe5c8SBarry Smith     while (q-->0) {
3548170fe5c8SBarry Smith       c_q = c + m*(*idx);
3549170fe5c8SBarry Smith       a_q = a + m*i;
3550be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3551170fe5c8SBarry Smith       idx++;
3552170fe5c8SBarry Smith       b++;
3553170fe5c8SBarry Smith     }
3554170fe5c8SBarry Smith   }
3555170fe5c8SBarry Smith   PetscFunctionReturn(0);
3556170fe5c8SBarry Smith }
3557170fe5c8SBarry Smith 
3558170fe5c8SBarry Smith #undef __FUNCT__
3559170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3560170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3561170fe5c8SBarry Smith {
3562170fe5c8SBarry Smith   PetscErrorCode ierr;
3563d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3564170fe5c8SBarry Smith   Mat            Cmat;
3565170fe5c8SBarry Smith 
3566170fe5c8SBarry Smith   PetscFunctionBegin;
3567e32f2f54SBarry 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);
356839804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3569170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3570170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3571170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3572170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3573170fe5c8SBarry Smith   *C = Cmat;
3574170fe5c8SBarry Smith   PetscFunctionReturn(0);
3575170fe5c8SBarry Smith }
3576170fe5c8SBarry Smith 
3577170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3578170fe5c8SBarry Smith #undef __FUNCT__
3579170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3580170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3581170fe5c8SBarry Smith {
3582170fe5c8SBarry Smith   PetscErrorCode ierr;
3583170fe5c8SBarry Smith 
3584170fe5c8SBarry Smith   PetscFunctionBegin;
3585170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3586170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3587170fe5c8SBarry Smith   }
3588170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3589170fe5c8SBarry Smith   PetscFunctionReturn(0);
3590170fe5c8SBarry Smith }
3591170fe5c8SBarry Smith 
3592170fe5c8SBarry Smith 
35930bad9183SKris Buschelman /*MC
3594fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
35950bad9183SKris Buschelman    based on compressed sparse row format.
35960bad9183SKris Buschelman 
35970bad9183SKris Buschelman    Options Database Keys:
35980bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
35990bad9183SKris Buschelman 
36000bad9183SKris Buschelman   Level: beginner
36010bad9183SKris Buschelman 
3602f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
36030bad9183SKris Buschelman M*/
36040bad9183SKris Buschelman 
3605a6175056SHong Zhang EXTERN_C_BEGIN
3606b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3607b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3608b5e56a35SBarry Smith #endif
3609ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3610af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3611af1023dbSSatish Balay #endif
36127087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
36137087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
36147087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
36157087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3616611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
36177087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3618611f576cSBarry Smith #endif
3619611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
36207087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3621611f576cSBarry Smith #endif
3622f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3623f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3624f3c0ef26SHong Zhang #endif
3625611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
36267087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3627611f576cSBarry Smith #endif
3628eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
36297087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3630eb3b5408SSatish Balay #endif
3631586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
36327087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3633586621ddSJed Brown #endif
3634719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
36357087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3636719d5645SBarry Smith #endif
3637b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
36387087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
36397087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
36407087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3641b3866ffcSBarry Smith #endif
364217667f90SBarry Smith EXTERN_C_END
364317667f90SBarry Smith 
364417667f90SBarry Smith EXTERN_C_BEGIN
36454a2ae208SSatish Balay #undef __FUNCT__
36464a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
36477087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3648273d9f13SBarry Smith {
3649273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3650dfbe8321SBarry Smith   PetscErrorCode ierr;
365138baddfdSBarry Smith   PetscMPIInt    size;
3652273d9f13SBarry Smith 
3653273d9f13SBarry Smith   PetscFunctionBegin;
36547adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3655e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3656273d9f13SBarry Smith 
365738f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3658b0a32e0cSBarry Smith   B->data             = (void*)b;
3659549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3660416022c9SBarry Smith   b->row              = 0;
3661416022c9SBarry Smith   b->col              = 0;
366282bf6240SBarry Smith   b->icol             = 0;
3663b810aeb4SBarry Smith   b->reallocs         = 0;
366436db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3665f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3666416022c9SBarry Smith   b->nonew             = 0;
3667416022c9SBarry Smith   b->diag              = 0;
3668416022c9SBarry Smith   b->solve_work        = 0;
36692a1b7f2aSHong Zhang   B->spptr             = 0;
3670be6bf707SBarry Smith   b->saved_values      = 0;
3671d7f994e1SBarry Smith   b->idiag             = 0;
367271f1c65dSBarry Smith   b->mdiag             = 0;
367371f1c65dSBarry Smith   b->ssor_work         = 0;
367471f1c65dSBarry Smith   b->omega             = 1.0;
367571f1c65dSBarry Smith   b->fshift            = 0.0;
367671f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3677a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3678a30b2313SHong Zhang   b->xtoy              = 0;
3679a30b2313SHong Zhang   b->XtoY              = 0;
368088e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
368117ab2063SBarry Smith 
368235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3683b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3684700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3685b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3686b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3687b3866ffcSBarry Smith #endif
3688b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3689700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3690b5e56a35SBarry Smith #endif
3691ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3692700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3693719d5645SBarry Smith #endif
3694611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3695700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3696611f576cSBarry Smith #endif
3697f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3698700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3699f3c0ef26SHong Zhang #endif
3700611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3701700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3702611f576cSBarry Smith #endif
3703611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3704700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3705611f576cSBarry Smith #endif
3706eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3707700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3708eb3b5408SSatish Balay #endif
3709586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3710700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3711586621ddSJed Brown #endif
3712719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3713700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3714719d5645SBarry Smith #endif
3715700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3716700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3717700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3718700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3719700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3720700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3721700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3722700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3723700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3724700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3725700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3726700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3727700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3728700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3729700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3730700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3731700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3732700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
37334108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
373417667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
37353a40ed3dSBarry Smith   PetscFunctionReturn(0);
373617ab2063SBarry Smith }
3737273d9f13SBarry Smith EXTERN_C_END
373817ab2063SBarry Smith 
37394a2ae208SSatish Balay #undef __FUNCT__
3740b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3741b24902e0SBarry Smith /*
3742b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3743b24902e0SBarry Smith */
3744ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
374517ab2063SBarry Smith {
3746416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
37476849ba73SBarry Smith   PetscErrorCode ierr;
3748d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
374917ab2063SBarry Smith 
37503a40ed3dSBarry Smith   PetscFunctionBegin;
3751273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3752273d9f13SBarry Smith 
3753d5f3da31SBarry Smith   C->factortype     = A->factortype;
3754416022c9SBarry Smith   c->row            = 0;
3755416022c9SBarry Smith   c->col            = 0;
375682bf6240SBarry Smith   c->icol           = 0;
37576ad4291fSHong Zhang   c->reallocs       = 0;
375817ab2063SBarry Smith 
37596ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
376017ab2063SBarry Smith 
3761aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
3762aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
3763eec197d1SBarry Smith 
376433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
37659518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
376617ab2063SBarry Smith   for (i=0; i<m; i++) {
3767416022c9SBarry Smith     c->imax[i] = a->imax[i];
3768416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
376917ab2063SBarry Smith   }
377017ab2063SBarry Smith 
377117ab2063SBarry Smith   /* allocate the matrix space */
3772f77e22a1SHong Zhang   if (mallocmatspace){
3773a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
37749518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3775f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
377697f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
377717ab2063SBarry Smith     if (m > 0) {
377897f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3779be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3780bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3781be6bf707SBarry Smith       } else {
3782bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
378317ab2063SBarry Smith       }
378408480c60SBarry Smith     }
3785f77e22a1SHong Zhang   }
378617ab2063SBarry Smith 
37876ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3788416022c9SBarry Smith   c->roworiented       = a->roworiented;
3789416022c9SBarry Smith   c->nonew             = a->nonew;
3790416022c9SBarry Smith   if (a->diag) {
379197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
379252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
379317ab2063SBarry Smith     for (i=0; i<m; i++) {
3794416022c9SBarry Smith       c->diag[i] = a->diag[i];
379517ab2063SBarry Smith     }
37963a40ed3dSBarry Smith   } else c->diag           = 0;
37976ad4291fSHong Zhang   c->solve_work            = 0;
37986ad4291fSHong Zhang   c->saved_values          = 0;
37996ad4291fSHong Zhang   c->idiag                 = 0;
380071f1c65dSBarry Smith   c->ssor_work             = 0;
3801a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3802e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3803e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
38046ad4291fSHong Zhang   c->xtoy                  = 0;
38056ad4291fSHong Zhang   c->XtoY                  = 0;
38066ad4291fSHong Zhang 
3807416022c9SBarry Smith   c->nz                 = a->nz;
38088ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3809273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3810754ec7b1SSatish Balay 
38116ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
38126ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3813cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3814cd6b891eSBarry Smith   if (a->compressedrow.use){
38156ad4291fSHong Zhang     i = a->compressedrow.nrows;
38160e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
38176ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
38186ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
381927ea64f8SHong Zhang   } else {
382027ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
382127ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
382227ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
38236ad4291fSHong Zhang   }
382488e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
38254108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
38264846f1f5SKris Buschelman 
38277adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
38283a40ed3dSBarry Smith   PetscFunctionReturn(0);
382917ab2063SBarry Smith }
383017ab2063SBarry Smith 
38314a2ae208SSatish Balay #undef __FUNCT__
3832b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3833b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3834b24902e0SBarry Smith {
3835b24902e0SBarry Smith   PetscErrorCode ierr;
3836b24902e0SBarry Smith 
3837b24902e0SBarry Smith   PetscFunctionBegin;
3838b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
38394b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3840b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3841f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3842b24902e0SBarry Smith   PetscFunctionReturn(0);
3843b24902e0SBarry Smith }
3844b24902e0SBarry Smith 
3845b24902e0SBarry Smith #undef __FUNCT__
38464a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3847112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3848fbdbba38SShri Abhyankar {
3849fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3850fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3851fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3852fbdbba38SShri Abhyankar   int            fd;
3853fbdbba38SShri Abhyankar   PetscMPIInt    size;
3854fbdbba38SShri Abhyankar   MPI_Comm       comm;
3855fbdbba38SShri Abhyankar 
3856fbdbba38SShri Abhyankar   PetscFunctionBegin;
3857fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3858fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3859fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3860fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3861fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3862fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
3863fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3864fbdbba38SShri Abhyankar 
3865fbdbba38SShri Abhyankar   if (nz < 0) {
3866fbdbba38SShri Abhyankar     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3867fbdbba38SShri Abhyankar   }
3868fbdbba38SShri Abhyankar 
3869fbdbba38SShri Abhyankar   /* read in row lengths */
3870fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3871fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3872fbdbba38SShri Abhyankar 
3873fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
3874fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3875fbdbba38SShri 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);
3876fbdbba38SShri Abhyankar 
3877fbdbba38SShri Abhyankar   /* set global size if not set already*/
3878f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
3879fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3880aabbc4fbSShri Abhyankar   } else {
3881fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
3882fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
3883f501eaabSShri 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);
3884aabbc4fbSShri Abhyankar   }
3885fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
3886fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
3887fbdbba38SShri Abhyankar 
3888fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
3889fbdbba38SShri Abhyankar 
3890fbdbba38SShri Abhyankar   /* read in nonzero values */
3891fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
3892fbdbba38SShri Abhyankar 
3893fbdbba38SShri Abhyankar   /* set matrix "i" values */
3894fbdbba38SShri Abhyankar   a->i[0] = 0;
3895fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
3896fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3897fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
3898fbdbba38SShri Abhyankar   }
3899fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3900fbdbba38SShri Abhyankar 
3901fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3902fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3903fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
3904fbdbba38SShri Abhyankar }
3905fbdbba38SShri Abhyankar 
3906fbdbba38SShri Abhyankar #undef __FUNCT__
3907b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3908ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
39097264ac53SSatish Balay {
39107264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3911dfbe8321SBarry Smith   PetscErrorCode ierr;
3912eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3913eeffb40dSHong Zhang   PetscInt k;
3914eeffb40dSHong Zhang #endif
39157264ac53SSatish Balay 
39163a40ed3dSBarry Smith   PetscFunctionBegin;
3917bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3918d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3919ca44d042SBarry Smith     *flg = PETSC_FALSE;
3920ca44d042SBarry Smith     PetscFunctionReturn(0);
3921bcd2baecSBarry Smith   }
39227264ac53SSatish Balay 
39237264ac53SSatish Balay   /* if the a->i are the same */
3924d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3925abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
39267264ac53SSatish Balay 
39277264ac53SSatish Balay   /* if a->j are the same */
392897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3929abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3930bcd2baecSBarry Smith 
3931bcd2baecSBarry Smith   /* if a->a are the same */
3932eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3933eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3934eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3935eeffb40dSHong Zhang       *flg = PETSC_FALSE;
39363a40ed3dSBarry Smith       PetscFunctionReturn(0);
3937eeffb40dSHong Zhang     }
3938eeffb40dSHong Zhang   }
3939eeffb40dSHong Zhang #else
3940eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3941eeffb40dSHong Zhang #endif
3942eeffb40dSHong Zhang   PetscFunctionReturn(0);
39437264ac53SSatish Balay }
394436db0b34SBarry Smith 
39454a2ae208SSatish Balay #undef __FUNCT__
39464a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
394705869f15SSatish Balay /*@
394836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
394936db0b34SBarry Smith               provided by the user.
395036db0b34SBarry Smith 
3951c75a6043SHong Zhang       Collective on MPI_Comm
395236db0b34SBarry Smith 
395336db0b34SBarry Smith    Input Parameters:
395436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
395536db0b34SBarry Smith .   m - number of rows
395636db0b34SBarry Smith .   n - number of columns
395736db0b34SBarry Smith .   i - row indices
395836db0b34SBarry Smith .   j - column indices
395936db0b34SBarry Smith -   a - matrix values
396036db0b34SBarry Smith 
396136db0b34SBarry Smith    Output Parameter:
396236db0b34SBarry Smith .   mat - the matrix
396336db0b34SBarry Smith 
396436db0b34SBarry Smith    Level: intermediate
396536db0b34SBarry Smith 
396636db0b34SBarry Smith    Notes:
39670551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
3968292fb18eSBarry Smith     once the matrix is destroyed and not before
396936db0b34SBarry Smith 
397036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
397136db0b34SBarry Smith 
3972bfeeae90SHong Zhang        The i and j indices are 0 based
397336db0b34SBarry Smith 
3974a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3975a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3976a4552177SSatish Balay     as shown:
3977a4552177SSatish Balay 
3978a4552177SSatish Balay         1 0 0
3979a4552177SSatish Balay         2 0 3
3980a4552177SSatish Balay         4 5 6
3981a4552177SSatish Balay 
3982a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
39839985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3984a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3985a4552177SSatish Balay 
39869985e31cSBarry Smith 
39872fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
398836db0b34SBarry Smith 
398936db0b34SBarry Smith @*/
39907087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
399136db0b34SBarry Smith {
3992dfbe8321SBarry Smith   PetscErrorCode ierr;
3993cbcfb4deSHong Zhang   PetscInt       ii;
399436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3995cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3996cbcfb4deSHong Zhang   PetscInt       jj;
3997cbcfb4deSHong Zhang #endif
399836db0b34SBarry Smith 
399936db0b34SBarry Smith   PetscFunctionBegin;
4000a96a251dSBarry Smith   if (i[0]) {
4001e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
400236db0b34SBarry Smith   }
4003f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4004f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4005ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4006ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4007ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4008ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4009ab93d7beSBarry Smith 
401036db0b34SBarry Smith   aij->i = i;
401136db0b34SBarry Smith   aij->j = j;
401236db0b34SBarry Smith   aij->a = a;
401336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
401436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4015e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4016e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
401736db0b34SBarry Smith 
401836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
401936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
40202515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4021e32f2f54SBarry 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]);
40229985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4023e32f2f54SBarry 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);
4024e32f2f54SBarry 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);
40259985e31cSBarry Smith     }
402636db0b34SBarry Smith #endif
402736db0b34SBarry Smith   }
40282515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
402936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4030e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4031e32f2f54SBarry 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]);
403236db0b34SBarry Smith   }
403336db0b34SBarry Smith #endif
403436db0b34SBarry Smith 
4035b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4036b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
403736db0b34SBarry Smith   PetscFunctionReturn(0);
403836db0b34SBarry Smith }
403936db0b34SBarry Smith 
4040cc8ba8e1SBarry Smith #undef __FUNCT__
4041ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4042dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4043cc8ba8e1SBarry Smith {
4044dfbe8321SBarry Smith   PetscErrorCode ierr;
4045cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
404636db0b34SBarry Smith 
4047cc8ba8e1SBarry Smith   PetscFunctionBegin;
40488ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4049cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4050cc8ba8e1SBarry Smith     a->coloring = coloring;
405112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
405297f1f81fSBarry Smith     PetscInt             i,*larray;
405312c595b3SBarry Smith     ISColoring      ocoloring;
405408b6dcc0SBarry Smith     ISColoringValue *colors;
405512c595b3SBarry Smith 
405612c595b3SBarry Smith     /* set coloring for diagonal portion */
40570e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4058d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
405912c595b3SBarry Smith       larray[i] = i;
406012c595b3SBarry Smith     }
4061992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
40620e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4063d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
406412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
406512c595b3SBarry Smith     }
406612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4067d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
406812c595b3SBarry Smith     a->coloring = ocoloring;
406912c595b3SBarry Smith   }
4070cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4071cc8ba8e1SBarry Smith }
4072cc8ba8e1SBarry Smith 
4073dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
4074ee4f033dSBarry Smith EXTERN_C_BEGIN
4075c6db04a5SJed Brown #include <adic/ad_utils.h>
4076ee4f033dSBarry Smith EXTERN_C_END
4077cc8ba8e1SBarry Smith 
4078cc8ba8e1SBarry Smith #undef __FUNCT__
4079ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
4080dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
4081cc8ba8e1SBarry Smith {
4082cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4083d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
40844440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
408508b6dcc0SBarry Smith   ISColoringValue *color;
4086cc8ba8e1SBarry Smith 
4087cc8ba8e1SBarry Smith   PetscFunctionBegin;
4088e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
40894440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
4090cc8ba8e1SBarry Smith   color = a->coloring->colors;
4091cc8ba8e1SBarry Smith   /* loop over rows */
4092cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
4093cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
4094cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
4095cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
4096cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
4097cc8ba8e1SBarry Smith     }
40984440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
4099ee4f033dSBarry Smith   }
4100ee4f033dSBarry Smith   PetscFunctionReturn(0);
4101ee4f033dSBarry Smith }
4102ee4f033dSBarry Smith #endif
4103ee4f033dSBarry Smith 
4104ee4f033dSBarry Smith #undef __FUNCT__
4105ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
410697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4107ee4f033dSBarry Smith {
4108ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4109d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
411054f21887SBarry Smith   MatScalar       *v = a->a;
411154f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
411208b6dcc0SBarry Smith   ISColoringValue *color;
4113ee4f033dSBarry Smith 
4114ee4f033dSBarry Smith   PetscFunctionBegin;
4115e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4116ee4f033dSBarry Smith   color = a->coloring->colors;
4117ee4f033dSBarry Smith   /* loop over rows */
4118ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4119ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4120ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
4121ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
4122ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
4123ee4f033dSBarry Smith     }
4124ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4125cc8ba8e1SBarry Smith   }
4126cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4127cc8ba8e1SBarry Smith }
412836db0b34SBarry Smith 
412981824310SBarry Smith /*
413081824310SBarry Smith     Special version for direct calls from Fortran
413181824310SBarry Smith */
4132c6db04a5SJed Brown #include <private/fortranimpl.h>
413381824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
413481824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
413581824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
413681824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
413781824310SBarry Smith #endif
413881824310SBarry Smith 
413981824310SBarry Smith /* Change these macros so can be used in void function */
414081824310SBarry Smith #undef CHKERRQ
41417adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
414281824310SBarry Smith #undef SETERRQ2
4143e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
414481824310SBarry Smith 
414581824310SBarry Smith EXTERN_C_BEGIN
414681824310SBarry Smith #undef __FUNCT__
414781824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
41481f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
414981824310SBarry Smith {
415081824310SBarry Smith   Mat            A = *AA;
415181824310SBarry Smith   PetscInt       m = *mm, n = *nn;
415281824310SBarry Smith   InsertMode     is = *isis;
415381824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
415481824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
415581824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
415681824310SBarry Smith   PetscErrorCode ierr;
415781824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
415854f21887SBarry Smith   MatScalar      *ap,value,*aa;
4159ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4160ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
416181824310SBarry Smith 
416281824310SBarry Smith   PetscFunctionBegin;
4163d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
416481824310SBarry Smith   imax = a->imax;
416581824310SBarry Smith   ai = a->i;
416681824310SBarry Smith   ailen = a->ilen;
416781824310SBarry Smith   aj = a->j;
416881824310SBarry Smith   aa = a->a;
416981824310SBarry Smith 
417081824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
417181824310SBarry Smith     row  = im[k];
417281824310SBarry Smith     if (row < 0) continue;
417381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4174d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
417581824310SBarry Smith #endif
417681824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
417781824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
417881824310SBarry Smith     low  = 0;
417981824310SBarry Smith     high = nrow;
418081824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
418181824310SBarry Smith       if (in[l] < 0) continue;
418281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4183d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
418481824310SBarry Smith #endif
418581824310SBarry Smith       col = in[l];
418681824310SBarry Smith       if (roworiented) {
418781824310SBarry Smith         value = v[l + k*n];
418881824310SBarry Smith       } else {
418981824310SBarry Smith         value = v[k + l*m];
419081824310SBarry Smith       }
419181824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
419281824310SBarry Smith 
419381824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
419481824310SBarry Smith       lastcol = col;
419581824310SBarry Smith       while (high-low > 5) {
419681824310SBarry Smith         t = (low+high)/2;
419781824310SBarry Smith         if (rp[t] > col) high = t;
419881824310SBarry Smith         else             low  = t;
419981824310SBarry Smith       }
420081824310SBarry Smith       for (i=low; i<high; i++) {
420181824310SBarry Smith         if (rp[i] > col) break;
420281824310SBarry Smith         if (rp[i] == col) {
420381824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
420481824310SBarry Smith           else                  ap[i] = value;
420581824310SBarry Smith           goto noinsert;
420681824310SBarry Smith         }
420781824310SBarry Smith       }
420881824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
420981824310SBarry Smith       if (nonew == 1) goto noinsert;
42107adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4211fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
421281824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
421381824310SBarry Smith       /* shift up all the later entries in this row */
421481824310SBarry Smith       for (ii=N; ii>=i; ii--) {
421581824310SBarry Smith         rp[ii+1] = rp[ii];
421681824310SBarry Smith         ap[ii+1] = ap[ii];
421781824310SBarry Smith       }
421881824310SBarry Smith       rp[i] = col;
421981824310SBarry Smith       ap[i] = value;
422081824310SBarry Smith       noinsert:;
422181824310SBarry Smith       low = i + 1;
422281824310SBarry Smith     }
422381824310SBarry Smith     ailen[row] = nrow;
422481824310SBarry Smith   }
422581824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
422681824310SBarry Smith   PetscFunctionReturnVoid();
422781824310SBarry Smith }
422881824310SBarry Smith EXTERN_C_END
422962298a1eSBarry Smith 
4230