xref: /petsc/src/mat/impls/aij/seq/aij.c (revision d48dcb1437bc73ef288020238c795a838f6deb30)
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) {
398f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(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) {
6082c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
60916cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
6102c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),-PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
61116cd7e1dSShri Abhyankar           } else {
6122c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
61316cd7e1dSShri Abhyankar           }
61416cd7e1dSShri Abhyankar #else
6152c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,1.0/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;
845bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_FALSE;
8463a40ed3dSBarry Smith   PetscFunctionReturn(0);
84717ab2063SBarry Smith }
84817ab2063SBarry Smith 
8494a2ae208SSatish Balay #undef __FUNCT__
85099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
85199cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
85299cafbc1SBarry Smith {
85399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
85499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
85554f21887SBarry Smith   MatScalar      *aa = a->a;
85699cafbc1SBarry Smith 
85799cafbc1SBarry Smith   PetscFunctionBegin;
85899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
85999cafbc1SBarry Smith   PetscFunctionReturn(0);
86099cafbc1SBarry Smith }
86199cafbc1SBarry Smith 
86299cafbc1SBarry Smith #undef __FUNCT__
86399cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
86499cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
86599cafbc1SBarry Smith {
86699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
86799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
86854f21887SBarry Smith   MatScalar      *aa = a->a;
86999cafbc1SBarry Smith 
87099cafbc1SBarry Smith   PetscFunctionBegin;
87199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
87299cafbc1SBarry Smith   PetscFunctionReturn(0);
87399cafbc1SBarry Smith }
87499cafbc1SBarry Smith 
87599cafbc1SBarry Smith #undef __FUNCT__
8764a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
877dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
87817ab2063SBarry Smith {
879416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
880dfbe8321SBarry Smith   PetscErrorCode ierr;
8813a40ed3dSBarry Smith 
8823a40ed3dSBarry Smith   PetscFunctionBegin;
883d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
8843a40ed3dSBarry Smith   PetscFunctionReturn(0);
88517ab2063SBarry Smith }
886416022c9SBarry Smith 
8874a2ae208SSatish Balay #undef __FUNCT__
8884a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
889dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
89017ab2063SBarry Smith {
891416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
892dfbe8321SBarry Smith   PetscErrorCode ierr;
893d5d45c9bSBarry Smith 
8943a40ed3dSBarry Smith   PetscFunctionBegin;
895aa482453SBarry Smith #if defined(PETSC_USE_LOG)
896d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
89717ab2063SBarry Smith #endif
898e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
8996bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9006bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
90105b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
902*d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
90305b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
90471f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
90505b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9066bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
90705b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9086bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
90905b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9106bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
911cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
912a30b2313SHong Zhang 
9134108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
914bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
915901853e0SKris Buschelman 
916dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
917901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
918901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
919901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
920901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
921901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
9225a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
923901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
924901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
925a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
926901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9273a40ed3dSBarry Smith   PetscFunctionReturn(0);
92817ab2063SBarry Smith }
92917ab2063SBarry Smith 
9304a2ae208SSatish Balay #undef __FUNCT__
9314a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
932ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
93317ab2063SBarry Smith {
934416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9354846f1f5SKris Buschelman   PetscErrorCode ierr;
9363a40ed3dSBarry Smith 
9373a40ed3dSBarry Smith   PetscFunctionBegin;
938a65d3064SKris Buschelman   switch (op) {
939a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
9404e0d8c25SBarry Smith       a->roworiented       = flg;
941a65d3064SKris Buschelman       break;
942a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
943a9817697SBarry Smith       a->keepnonzeropattern    = flg;
944a65d3064SKris Buschelman       break;
945512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
946512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
947a65d3064SKris Buschelman       break;
948a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
9494e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
950a65d3064SKris Buschelman       break;
951a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
9524e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
953a65d3064SKris Buschelman       break;
95428b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
95528b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
95628b2fa4aSMatthew Knepley       break;
957a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
9584e0d8c25SBarry Smith       a->ignorezeroentries = flg;
9590df259c2SBarry Smith       break;
960cd6b891eSBarry Smith     case MAT_CHECK_COMPRESSED_ROW:
961cd6b891eSBarry Smith       a->compressedrow.check = flg;
962d487561eSHong Zhang       break;
9633d472b54SHong Zhang     case MAT_SPD:
9643d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
9653d472b54SHong Zhang       A->spd                             = flg;
9663d472b54SHong Zhang       if (flg) {
9673d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
9683d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
9693d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
9703d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
9713d472b54SHong Zhang       }
9723d472b54SHong Zhang       break;
973b1646e73SJed Brown     case MAT_SYMMETRIC:
974b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
975b1646e73SJed Brown     case MAT_HERMITIAN:
976b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
9774e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
978a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
979a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
980290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
981a65d3064SKris Buschelman       break;
982b87ac2d8SJed Brown     case MAT_USE_INODES:
983b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
984b87ac2d8SJed Brown       break;
985a65d3064SKris Buschelman     default:
986e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
987a65d3064SKris Buschelman   }
9884108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
9893a40ed3dSBarry Smith   PetscFunctionReturn(0);
99017ab2063SBarry Smith }
99117ab2063SBarry Smith 
9924a2ae208SSatish Balay #undef __FUNCT__
9934a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
994dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
99517ab2063SBarry Smith {
996416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9976849ba73SBarry Smith   PetscErrorCode ierr;
998d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
99935e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
100017ab2063SBarry Smith 
10013a40ed3dSBarry Smith   PetscFunctionBegin;
1002d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1003e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
100435e7444dSHong Zhang 
1005d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
1006d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
100735e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10082c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
100935e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
101035e7444dSHong Zhang     PetscFunctionReturn(0);
101135e7444dSHong Zhang   }
101235e7444dSHong Zhang 
10132dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10141ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
101535e7444dSHong Zhang   for (i=0; i<n; i++) {
101635e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10172f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
101835e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
101935e7444dSHong Zhang       if (aj[j] == i) {
102035e7444dSHong Zhang         x[i] = aa[j];
102117ab2063SBarry Smith         break;
102217ab2063SBarry Smith       }
102317ab2063SBarry Smith     }
102417ab2063SBarry Smith   }
10251ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10263a40ed3dSBarry Smith   PetscFunctionReturn(0);
102717ab2063SBarry Smith }
102817ab2063SBarry Smith 
1029c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10304a2ae208SSatish Balay #undef __FUNCT__
10314a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1032dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
103317ab2063SBarry Smith {
1034416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10355c897100SBarry Smith   PetscScalar       *x,*y;
1036dfbe8321SBarry Smith   PetscErrorCode    ierr;
1037d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10385c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1039a77337e4SBarry Smith   MatScalar         *v;
1040a77337e4SBarry Smith   PetscScalar       alpha;
104104fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10423447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1043ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10445c897100SBarry Smith #endif
104517ab2063SBarry Smith 
10463a40ed3dSBarry Smith   PetscFunctionBegin;
10472e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
10481ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10491ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10505c897100SBarry Smith 
10515c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1052bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
10535c897100SBarry Smith #else
10543447b6efSHong Zhang   if (usecprow){
10553447b6efSHong Zhang     m    = cprow.nrows;
10563447b6efSHong Zhang     ii   = cprow.i;
10577b2bb3b9SHong Zhang     ridx = cprow.rindex;
10583447b6efSHong Zhang   } else {
10593447b6efSHong Zhang     ii = a->i;
10603447b6efSHong Zhang   }
106117ab2063SBarry Smith   for (i=0; i<m; i++) {
10623447b6efSHong Zhang     idx   = a->j + ii[i] ;
10633447b6efSHong Zhang     v     = a->a + ii[i] ;
10643447b6efSHong Zhang     n     = ii[i+1] - ii[i];
10653447b6efSHong Zhang     if (usecprow){
10667b2bb3b9SHong Zhang       alpha = x[ridx[i]];
10673447b6efSHong Zhang     } else {
106817ab2063SBarry Smith       alpha = x[i];
10693447b6efSHong Zhang     }
107004fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
107117ab2063SBarry Smith   }
10725c897100SBarry Smith #endif
1073dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10741ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10751ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10763a40ed3dSBarry Smith   PetscFunctionReturn(0);
107717ab2063SBarry Smith }
107817ab2063SBarry Smith 
10794a2ae208SSatish Balay #undef __FUNCT__
10805c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1081dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
10825c897100SBarry Smith {
1083dfbe8321SBarry Smith   PetscErrorCode ierr;
10845c897100SBarry Smith 
10855c897100SBarry Smith   PetscFunctionBegin;
1086170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
10875c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
10885c897100SBarry Smith   PetscFunctionReturn(0);
10895c897100SBarry Smith }
10905c897100SBarry Smith 
1091c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10925c897100SBarry Smith #undef __FUNCT__
10934a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1094dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
109517ab2063SBarry Smith {
1096416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1097d9fead3dSBarry Smith   PetscScalar       *y;
109854f21887SBarry Smith   const PetscScalar *x;
109954f21887SBarry Smith   const MatScalar   *aa;
1100dfbe8321SBarry Smith   PetscErrorCode    ierr;
1101003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1102003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
11038aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1104362ced78SSatish Balay   PetscScalar       sum;
1105ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
110617ab2063SBarry Smith 
1107b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
110897952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1109fee21e36SBarry Smith #endif
1110fee21e36SBarry Smith 
11113a40ed3dSBarry Smith   PetscFunctionBegin;
11123649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
11131ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
111497952fefSHong Zhang   aj  = a->j;
111597952fefSHong Zhang   aa  = a->a;
1116416022c9SBarry Smith   ii  = a->i;
11174eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
111897952fefSHong Zhang     m    = a->compressedrow.nrows;
111997952fefSHong Zhang     ii   = a->compressedrow.i;
112097952fefSHong Zhang     ridx = a->compressedrow.rindex;
112197952fefSHong Zhang     for (i=0; i<m; i++){
112297952fefSHong Zhang       n   = ii[i+1] - ii[i];
112397952fefSHong Zhang       aj  = a->j + ii[i];
112497952fefSHong Zhang       aa  = a->a + ii[i];
112597952fefSHong Zhang       sum = 0.0;
1126a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1127003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1128003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
112997952fefSHong Zhang       y[*ridx++] = sum;
113097952fefSHong Zhang     }
113197952fefSHong Zhang   } else { /* do not use compressed row format */
1132b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1133b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1134b05257ddSBarry Smith #else
113517ab2063SBarry Smith     for (i=0; i<m; i++) {
1136003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1137003131ecSBarry Smith       aj  = a->j + ii[i];
1138003131ecSBarry Smith       aa  = a->a + ii[i];
113917ab2063SBarry Smith       sum  = 0.0;
1140a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1141003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
114217ab2063SBarry Smith       y[i] = sum;
114317ab2063SBarry Smith     }
11448d195f9aSBarry Smith #endif
1145b05257ddSBarry Smith   }
1146dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
11473649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
11481ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11493a40ed3dSBarry Smith   PetscFunctionReturn(0);
115017ab2063SBarry Smith }
115117ab2063SBarry Smith 
115263db405bSBarry Smith #if defined(PETSC_HAVE_PTHREADCLASSES)
115363db405bSBarry Smith 
11540ca81413SKerry Stevens //*******************
1155700485ddSSatish Balay #if defined(PETSC_HAVE_PTHREADCLASSES)
1156cfcfc605SKerry Stevens extern PetscBool    PetscUseThreadPool;
1157cfcfc605SKerry Stevens void* DoCoreAffinity(void);
1158cfcfc605SKerry Stevens 
11590ca81413SKerry Stevens typedef struct {
11600ca81413SKerry Stevens   const MatScalar* matdata;
11610ca81413SKerry Stevens   const PetscScalar* vecdata;
11620ca81413SKerry Stevens   PetscScalar* vecout;
11630ca81413SKerry Stevens   const PetscInt* colindnz;
11640ca81413SKerry Stevens   const PetscInt* rownumnz;
11650ca81413SKerry Stevens   PetscInt numrows;
11660ca81413SKerry Stevens   const PetscInt* specidx;
11670ca81413SKerry Stevens   PetscInt nzr;
11680ca81413SKerry Stevens } MatMult_KernelData;
11690ca81413SKerry Stevens 
11700ca81413SKerry Stevens void* MatMult_Kernel(void *arg)
11710ca81413SKerry Stevens {
1172cfcfc605SKerry Stevens   if(PetscUseThreadPool==PETSC_FALSE) {
1173cfcfc605SKerry Stevens     DoCoreAffinity();
1174cfcfc605SKerry Stevens   }
11750ca81413SKerry Stevens   MatMult_KernelData *data = (MatMult_KernelData*)arg;
11760ca81413SKerry Stevens   PetscScalar       sum;
11770ca81413SKerry Stevens   const MatScalar   *aabase = data->matdata,*aa;
11780ca81413SKerry Stevens   const PetscScalar *x = data->vecdata;
11790ca81413SKerry Stevens   PetscScalar       *y = data->vecout;
11800ca81413SKerry Stevens   const PetscInt    *ajbase = data->colindnz,*aj;
11810ca81413SKerry Stevens   const PetscInt    *ii = data->rownumnz;
11820ca81413SKerry Stevens   PetscInt          m  = data->numrows;
11830ca81413SKerry Stevens   const PetscInt    *ridx = data->specidx;
11840ca81413SKerry Stevens   PetscInt          i,n,nonzerorow = 0;
11850ca81413SKerry Stevens 
11860ca81413SKerry Stevens   if(ridx!=NULL) {
11870ca81413SKerry Stevens     for (i=0; i<m; i++){
11880ca81413SKerry Stevens       n   = ii[i+1] - ii[i];
11890ca81413SKerry Stevens       aj  = ajbase + ii[i];
11900ca81413SKerry Stevens       aa  = aabase + ii[i];
11910ca81413SKerry Stevens       sum = 0.0;
1192cfcfc605SKerry Stevens       if(n>0) {
119351d315f7SKerry Stevens         PetscSparseDensePlusDot(sum,x,aa,aj,n);
119451d315f7SKerry Stevens         nonzerorow++;
1195cfcfc605SKerry Stevens       }
11960ca81413SKerry Stevens       y[*ridx++] = sum;
11970ca81413SKerry Stevens     }
11980ca81413SKerry Stevens   }
11990ca81413SKerry Stevens   else {
120051d315f7SKerry Stevens     PetscInt ibase = data->nzr;
12010ca81413SKerry Stevens     for (i=0; i<m; i++) {
12020ca81413SKerry Stevens       n   = ii[i+1] - ii[i];
12030ca81413SKerry Stevens       aj  = ajbase + ii[i];
12040ca81413SKerry Stevens       aa  = aabase + ii[i];
12050ca81413SKerry Stevens       sum  = 0.0;
1206cfcfc605SKerry Stevens       if(n>0) {
120751d315f7SKerry Stevens         PetscSparseDensePlusDot(sum,x,aa,aj,n);
120851d315f7SKerry Stevens         nonzerorow++;
1209cfcfc605SKerry Stevens       }
121051d315f7SKerry Stevens       y[i+ibase] = sum;
12110ca81413SKerry Stevens     }
12120ca81413SKerry Stevens   }
12130ca81413SKerry Stevens   data->nzr = nonzerorow;
12140ca81413SKerry Stevens   return NULL;
12150ca81413SKerry Stevens }
1216700485ddSSatish Balay #endif
12170ca81413SKerry Stevens 
12180ca81413SKerry Stevens extern PetscMPIInt PetscMaxThreads;
12194b83fb64SBarry Smith extern PetscErrorCode (*MainJob)(void* (*pFunc)(void*),void**,PetscInt);
122051d315f7SKerry Stevens 
12210ca81413SKerry Stevens #undef __FUNCT__
12227d6a0e61SBarry Smith #define __FUNCT__ "MatMult_SeqAIJPThread"
12237d6a0e61SBarry Smith PetscErrorCode MatMult_SeqAIJPThread(Mat A,Vec xx,Vec yy)
12240ca81413SKerry Stevens {
12250ca81413SKerry Stevens   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
12260ca81413SKerry Stevens   PetscScalar       *y;
12270ca81413SKerry Stevens   const PetscScalar *x;
12280ca81413SKerry Stevens   PetscErrorCode    ierr;
12290ca81413SKerry Stevens   PetscInt          m=A->rmap->n,nonzerorow=0;
12300ca81413SKerry Stevens   PetscBool         usecprow=a->compressedrow.use;
12310ca81413SKerry Stevens 
12320ca81413SKerry Stevens #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
12330ca81413SKerry Stevens #pragma disjoint(*x,*y,*aa)
12340ca81413SKerry Stevens #endif
12350ca81413SKerry Stevens 
12360ca81413SKerry Stevens   PetscFunctionBegin;
12370ca81413SKerry Stevens   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12380ca81413SKerry Stevens   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12390ca81413SKerry Stevens 
12400ca81413SKerry Stevens   if(usecprow) {
12410ca81413SKerry Stevens     PetscInt          NumPerThread,iindex;
12420ca81413SKerry Stevens     const MatScalar   *aa = a->a;
12430ca81413SKerry Stevens     const PetscInt    *aj = a->j,*ii = a->compressedrow.i,*ridx=a->compressedrow.rindex;
12440ca81413SKerry Stevens     PetscInt          i,iStartVal,iEndVal,iStartIndex,iEndIndex;
12450ca81413SKerry Stevens     const PetscInt    iNumThreads = PetscMaxThreads;  //this number could be different
1246cfcfc605SKerry Stevens     MatMult_KernelData* kerneldatap = (MatMult_KernelData*)malloc(iNumThreads*sizeof(MatMult_KernelData));
1247cfcfc605SKerry Stevens     MatMult_KernelData** pdata = (MatMult_KernelData**)malloc(iNumThreads*sizeof(MatMult_KernelData*));
12480ca81413SKerry Stevens 
12490ca81413SKerry Stevens     m    = a->compressedrow.nrows;
12500ca81413SKerry Stevens     NumPerThread = ii[m]/iNumThreads;
12510ca81413SKerry Stevens     iindex = 0;
12520ca81413SKerry Stevens     for(i=0; i<iNumThreads;i++) {
12530ca81413SKerry Stevens       iStartIndex = iindex;
12540ca81413SKerry Stevens       iStartVal = ii[iStartIndex];
12550ca81413SKerry Stevens       iEndVal = iStartVal;
12560ca81413SKerry Stevens       //determine number of rows to process
12570ca81413SKerry Stevens       while(iEndVal-iStartVal<NumPerThread) {
12580ca81413SKerry Stevens 	iindex++;
12590ca81413SKerry Stevens 	iEndVal = ii[iindex];
12600ca81413SKerry Stevens       }
12610ca81413SKerry Stevens       //determine whether to go back 1
12620ca81413SKerry Stevens       if(iEndVal-iStartVal-NumPerThread>NumPerThread-(ii[iindex-1]-iStartVal)) {
12630ca81413SKerry Stevens 	iindex--;
12640ca81413SKerry Stevens 	iEndVal = ii[iindex];
12650ca81413SKerry Stevens       }
12660ca81413SKerry Stevens       iEndIndex = iindex;
1267cfcfc605SKerry Stevens       kerneldatap[i].matdata  = aa;
12680ca81413SKerry Stevens       kerneldatap[i].vecdata  = x;
12690ca81413SKerry Stevens       kerneldatap[i].vecout   = y;
12700ca81413SKerry Stevens       kerneldatap[i].colindnz = aj;
12710ca81413SKerry Stevens       kerneldatap[i].rownumnz = ii + iStartIndex;
12720ca81413SKerry Stevens       kerneldatap[i].numrows  = iEndIndex - iStartIndex + 1;
12730ca81413SKerry Stevens       kerneldatap[i].specidx  = ridx + iStartVal;
12740ca81413SKerry Stevens       kerneldatap[i].nzr      = 0;
1275cfcfc605SKerry Stevens       pdata[i] = &kerneldatap[i];
12760ca81413SKerry Stevens       iindex++;
12770ca81413SKerry Stevens     }
1278cfcfc605SKerry Stevens     ierr = MainJob(MatMult_Kernel,(void**)pdata,iNumThreads);
1279cfcfc605SKerry Stevens     /* collect results */
128051d315f7SKerry Stevens     for(i=0; i<iNumThreads; i++) {
1281cfcfc605SKerry Stevens       nonzerorow += kerneldatap[i].nzr;
128251d315f7SKerry Stevens     }
1283cfcfc605SKerry Stevens     free(kerneldatap);
1284cfcfc605SKerry Stevens     free(pdata);
128551d315f7SKerry Stevens   }
128651d315f7SKerry Stevens   else {
128751d315f7SKerry Stevens #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
128851d315f7SKerry Stevens   fortranmultaij_(&m,x,a->i,a->j,a->a,y);
128951d315f7SKerry Stevens #else
129051d315f7SKerry Stevens   PetscInt            i,iindex;
129151d315f7SKerry Stevens     const MatScalar   *aa = a->a;
129251d315f7SKerry Stevens     const PetscInt    *aj = a->j,*ii = a->i;
129351d315f7SKerry Stevens     const PetscInt    iNumThreads = PetscMaxThreads;  //this number could be different
129451d315f7SKerry Stevens     PetscInt          Q = m/iNumThreads;
129551d315f7SKerry Stevens     PetscInt          R = m-Q*iNumThreads;
129651d315f7SKerry Stevens     PetscBool         S;
129751d315f7SKerry Stevens 
129851d315f7SKerry Stevens     MatMult_KernelData* kerneldatap = (MatMult_KernelData*)malloc(iNumThreads*sizeof(MatMult_KernelData));
129951d315f7SKerry Stevens     MatMult_KernelData** pdata = (MatMult_KernelData**)malloc(iNumThreads*sizeof(MatMult_KernelData*));
130051d315f7SKerry Stevens 
130151d315f7SKerry Stevens     iindex = 0;
130251d315f7SKerry Stevens     for(i=0; i<iNumThreads;i++) {
1303b50af74fSBarry Smith       S = (PetscBool)(i<R);
130451d315f7SKerry Stevens       kerneldatap[i].matdata  = aa;
130551d315f7SKerry Stevens       kerneldatap[i].vecdata  = x;
130651d315f7SKerry Stevens       kerneldatap[i].vecout   = y;
130751d315f7SKerry Stevens       kerneldatap[i].colindnz = aj;
130851d315f7SKerry Stevens       kerneldatap[i].rownumnz = ii + iindex;
130951d315f7SKerry Stevens       kerneldatap[i].numrows  = S?Q+1:Q;
131051d315f7SKerry Stevens       kerneldatap[i].specidx  = PETSC_NULL;
131151d315f7SKerry Stevens       kerneldatap[i].nzr      = iindex; //serves as the 'base' row (needed to access correctly into output vector y)
131251d315f7SKerry Stevens       pdata[i] = &kerneldatap[i];
131351d315f7SKerry Stevens       iindex += kerneldatap[i].numrows;
131451d315f7SKerry Stevens     }
13150ca81413SKerry Stevens     MainJob(MatMult_Kernel,(void**)pdata,iNumThreads);
13160ca81413SKerry Stevens     //collect results
13170ca81413SKerry Stevens     for(i=0; i<iNumThreads; i++) {
13180ca81413SKerry Stevens       nonzerorow += kerneldatap[i].nzr;
13190ca81413SKerry Stevens     }
132051d315f7SKerry Stevens     free(kerneldatap);
132151d315f7SKerry Stevens     free(pdata);
13220ca81413SKerry Stevens #endif
13230ca81413SKerry Stevens   }
13240ca81413SKerry Stevens 
13250ca81413SKerry Stevens   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
13260ca81413SKerry Stevens   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13270ca81413SKerry Stevens   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13280ca81413SKerry Stevens   PetscFunctionReturn(0);
13290ca81413SKerry Stevens }
13300ca81413SKerry Stevens //*******************
1331ba61063dSBarry Smith #endif
13320ca81413SKerry Stevens 
1333c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
13344a2ae208SSatish Balay #undef __FUNCT__
13354a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1336dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
133717ab2063SBarry Smith {
1338416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1339f15663dcSBarry Smith   PetscScalar       *y,*z;
1340f15663dcSBarry Smith   const PetscScalar *x;
134154f21887SBarry Smith   const MatScalar   *aa;
1342dfbe8321SBarry Smith   PetscErrorCode    ierr;
1343d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
1344f15663dcSBarry Smith   PetscInt          n,i,*ridx=PETSC_NULL;
1345362ced78SSatish Balay   PetscScalar       sum;
1346ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
13479ea0dfa2SSatish Balay 
13483a40ed3dSBarry Smith   PetscFunctionBegin;
1349f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13501ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13512e8a6d31SBarry Smith   if (zz != yy) {
13521ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
13532e8a6d31SBarry Smith   } else {
13542e8a6d31SBarry Smith     z = y;
13552e8a6d31SBarry Smith   }
1356bfeeae90SHong Zhang 
135797952fefSHong Zhang   aj  = a->j;
135897952fefSHong Zhang   aa  = a->a;
1359cddf8d76SBarry Smith   ii  = a->i;
13604eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
13614eb6d288SHong Zhang     if (zz != yy){
13624eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
13634eb6d288SHong Zhang     }
136497952fefSHong Zhang     m    = a->compressedrow.nrows;
136597952fefSHong Zhang     ii   = a->compressedrow.i;
136697952fefSHong Zhang     ridx = a->compressedrow.rindex;
136797952fefSHong Zhang     for (i=0; i<m; i++){
136897952fefSHong Zhang       n  = ii[i+1] - ii[i];
136997952fefSHong Zhang       aj  = a->j + ii[i];
137097952fefSHong Zhang       aa  = a->a + ii[i];
137197952fefSHong Zhang       sum = y[*ridx];
1372f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
137397952fefSHong Zhang       z[*ridx++] = sum;
137497952fefSHong Zhang     }
137597952fefSHong Zhang   } else { /* do not use compressed row format */
1376f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1377f15663dcSBarry Smith   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1378f15663dcSBarry Smith #else
137917ab2063SBarry Smith     for (i=0; i<m; i++) {
1380f15663dcSBarry Smith       n    = ii[i+1] - ii[i];
1381f15663dcSBarry Smith       aj  = a->j + ii[i];
1382f15663dcSBarry Smith       aa  = a->a + ii[i];
138317ab2063SBarry Smith       sum  = y[i];
1384f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
138517ab2063SBarry Smith       z[i] = sum;
138617ab2063SBarry Smith     }
138702ab625aSSatish Balay #endif
1388f15663dcSBarry Smith   }
1389dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1390f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13911ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13922e8a6d31SBarry Smith   if (zz != yy) {
13931ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
13942e8a6d31SBarry Smith   }
13958154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
13966b375ea7SVictor Minden   /*
1397918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1398918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1399918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
14006b375ea7SVictor Minden   */
1401918e98c3SVictor Minden #endif
14023a40ed3dSBarry Smith   PetscFunctionReturn(0);
140317ab2063SBarry Smith }
140417ab2063SBarry Smith 
140517ab2063SBarry Smith /*
140617ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
140717ab2063SBarry Smith */
14084a2ae208SSatish Balay #undef __FUNCT__
14094a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1410dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
141117ab2063SBarry Smith {
1412416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
14136849ba73SBarry Smith   PetscErrorCode ierr;
1414d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
141517ab2063SBarry Smith 
14163a40ed3dSBarry Smith   PetscFunctionBegin;
141709f38230SBarry Smith   if (!a->diag) {
141809f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
14199518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
142009f38230SBarry Smith   }
1421d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
142209f38230SBarry Smith     a->diag[i] = a->i[i+1];
1423bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1424bfeeae90SHong Zhang       if (a->j[j] == i) {
142509f38230SBarry Smith         a->diag[i] = j;
142617ab2063SBarry Smith         break;
142717ab2063SBarry Smith       }
142817ab2063SBarry Smith     }
142917ab2063SBarry Smith   }
14303a40ed3dSBarry Smith   PetscFunctionReturn(0);
143117ab2063SBarry Smith }
143217ab2063SBarry Smith 
1433be5855fcSBarry Smith /*
1434be5855fcSBarry Smith      Checks for missing diagonals
1435be5855fcSBarry Smith */
14364a2ae208SSatish Balay #undef __FUNCT__
14374a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1438ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1439be5855fcSBarry Smith {
1440be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
144197f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1442be5855fcSBarry Smith 
1443be5855fcSBarry Smith   PetscFunctionBegin;
144409f38230SBarry Smith   *missing = PETSC_FALSE;
1445d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
144609f38230SBarry Smith     *missing  = PETSC_TRUE;
144709f38230SBarry Smith     if (d) *d = 0;
144809f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
144909f38230SBarry Smith   } else {
1450f1e2ffcdSBarry Smith     diag = a->diag;
1451d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1452bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
145309f38230SBarry Smith 	*missing = PETSC_TRUE;
145409f38230SBarry Smith 	if (d) *d = i;
145509f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
145609f38230SBarry Smith       }
1457be5855fcSBarry Smith     }
1458be5855fcSBarry Smith   }
1459be5855fcSBarry Smith   PetscFunctionReturn(0);
1460be5855fcSBarry Smith }
1461be5855fcSBarry Smith 
146271f1c65dSBarry Smith EXTERN_C_BEGIN
146371f1c65dSBarry Smith #undef __FUNCT__
146471f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
14657087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
146671f1c65dSBarry Smith {
146771f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
146871f1c65dSBarry Smith   PetscErrorCode ierr;
1469d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
147054f21887SBarry Smith   MatScalar      *v = a->a;
147154f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
147271f1c65dSBarry Smith 
147371f1c65dSBarry Smith   PetscFunctionBegin;
147471f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
147571f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
147671f1c65dSBarry Smith   diag = a->diag;
147771f1c65dSBarry Smith   if (!a->idiag) {
147871f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
147971f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
148071f1c65dSBarry Smith     v        = a->a;
148171f1c65dSBarry Smith   }
148271f1c65dSBarry Smith   mdiag = a->mdiag;
148371f1c65dSBarry Smith   idiag = a->idiag;
148471f1c65dSBarry Smith 
1485028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
148671f1c65dSBarry Smith     for (i=0; i<m; i++) {
148771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1488e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
148971f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
149071f1c65dSBarry Smith     }
149171f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
149271f1c65dSBarry Smith   } else {
149371f1c65dSBarry Smith     for (i=0; i<m; i++) {
149471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
149571f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
149671f1c65dSBarry Smith     }
1497dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
149871f1c65dSBarry Smith   }
149971f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
150071f1c65dSBarry Smith   PetscFunctionReturn(0);
150171f1c65dSBarry Smith }
15025a9745a3SMatthew Knepley EXTERN_C_END
150371f1c65dSBarry Smith 
1504c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
15054a2ae208SSatish Balay #undef __FUNCT__
150641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
150741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
150817ab2063SBarry Smith {
1509416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1510e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1511e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
151254f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1513dfbe8321SBarry Smith   PetscErrorCode     ierr;
1514d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
151597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
151617ab2063SBarry Smith 
15173a40ed3dSBarry Smith   PetscFunctionBegin;
1518b965ef7fSBarry Smith   its = its*lits;
151991723122SBarry Smith 
152071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
152171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
152271f1c65dSBarry Smith   a->fshift = fshift;
152371f1c65dSBarry Smith   a->omega  = omega;
1524ed480e8bSBarry Smith 
152571f1c65dSBarry Smith   diag = a->diag;
152671f1c65dSBarry Smith   t     = a->ssor_work;
1527ed480e8bSBarry Smith   idiag = a->idiag;
152871f1c65dSBarry Smith   mdiag = a->mdiag;
1529ed480e8bSBarry Smith 
15301ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
15313649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
153271f1c65dSBarry Smith   CHKMEMQ;
1533ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
153417ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
153517ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1536ed480e8bSBarry Smith     bs = b;
153717ab2063SBarry Smith     for (i=0; i<m; i++) {
153871f1c65dSBarry Smith         d    = fshift + mdiag[i];
1539416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1540ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1541ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
154217ab2063SBarry Smith         sum  = b[i]*d/omega;
1543003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
154417ab2063SBarry Smith         x[i] = sum;
154517ab2063SBarry Smith     }
15461ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15473649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1548efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15493a40ed3dSBarry Smith     PetscFunctionReturn(0);
155017ab2063SBarry Smith   }
1551c783ea89SBarry Smith 
155248af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1553e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
15543a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
155517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1556887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
155717ab2063SBarry Smith 
155817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
155917ab2063SBarry Smith 
1560887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
156117ab2063SBarry Smith     */
156217ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
156317ab2063SBarry Smith 
156417ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
156517ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1566416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1567ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1568ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
156917ab2063SBarry Smith       sum  = b[i];
1570e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1571ed480e8bSBarry Smith       x[i] = sum*idiag[i];
157217ab2063SBarry Smith     }
157317ab2063SBarry Smith 
157417ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1575416022c9SBarry Smith     v = a->a;
1576ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
157717ab2063SBarry Smith 
157817ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1579ed480e8bSBarry Smith     ts = t;
1580416022c9SBarry Smith     diag = a->diag;
158117ab2063SBarry Smith     for (i=0; i<m; i++) {
1582416022c9SBarry Smith       n    = diag[i] - a->i[i];
1583ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1584ed480e8bSBarry Smith       v    = a->a + a->i[i];
158517ab2063SBarry Smith       sum  = t[i];
1586003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1587ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1588733d66baSBarry Smith       /*  x = x + t */
1589733d66baSBarry Smith       x[i] += t[i];
159017ab2063SBarry Smith     }
159117ab2063SBarry Smith 
1592dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
15931ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15943649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
15953a40ed3dSBarry Smith     PetscFunctionReturn(0);
159617ab2063SBarry Smith   }
159717ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
159817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
159917ab2063SBarry Smith       for (i=0; i<m; i++) {
1600416022c9SBarry Smith         n    = diag[i] - a->i[i];
1601ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1602ed480e8bSBarry Smith         v    = a->a + a->i[i];
160317ab2063SBarry Smith         sum  = b[i];
1604e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
16055c99c7daSBarry Smith         t[i] = sum;
1606ed480e8bSBarry Smith         x[i] = sum*idiag[i];
160717ab2063SBarry Smith       }
16085c99c7daSBarry Smith       xb = t;
1609efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16103a40ed3dSBarry Smith     } else xb = b;
161117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
161217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1613416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1614ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1615ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
161617ab2063SBarry Smith         sum  = xb[i];
1617e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
16185c99c7daSBarry Smith         if (xb == b) {
1619ed480e8bSBarry Smith           x[i] = sum*idiag[i];
16205c99c7daSBarry Smith         } else {
16215c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
162217ab2063SBarry Smith         }
16235c99c7daSBarry Smith       }
1624efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
162517ab2063SBarry Smith     }
162617ab2063SBarry Smith     its--;
162717ab2063SBarry Smith   }
162817ab2063SBarry Smith   while (its--) {
162917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
163017ab2063SBarry Smith       for (i=0; i<m; i++) {
1631416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1632ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1633ed480e8bSBarry Smith         v    = a->a + a->i[i];
163417ab2063SBarry Smith         sum  = b[i];
1635e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1636ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
163717ab2063SBarry Smith       }
16389f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
163917ab2063SBarry Smith     }
164017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
164117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1642416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1643ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1644ed480e8bSBarry Smith         v    = a->a + a->i[i];
164517ab2063SBarry Smith         sum  = b[i];
1646e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1647ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
164817ab2063SBarry Smith       }
16499f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
165017ab2063SBarry Smith     }
165117ab2063SBarry Smith   }
16521ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16533649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
165471f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
165517ab2063SBarry Smith }
165617ab2063SBarry Smith 
16572af78befSBarry Smith 
16584a2ae208SSatish Balay #undef __FUNCT__
16594a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1660dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
166117ab2063SBarry Smith {
1662416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16634e220ebcSLois Curfman McInnes 
16643a40ed3dSBarry Smith   PetscFunctionBegin;
16654e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
16664e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
16674e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
16684e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
16694e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
16708e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
16717adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1672d5f3da31SBarry Smith   if (A->factortype) {
16734e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
16744e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
16754e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
16764e220ebcSLois Curfman McInnes   } else {
16774e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
16784e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
16794e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
16804e220ebcSLois Curfman McInnes   }
16813a40ed3dSBarry Smith   PetscFunctionReturn(0);
168217ab2063SBarry Smith }
168317ab2063SBarry Smith 
16844a2ae208SSatish Balay #undef __FUNCT__
16854a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
16862b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
168717ab2063SBarry Smith {
1688416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
16893b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
16906849ba73SBarry Smith   PetscErrorCode    ierr;
169197b48c8fSBarry Smith   const PetscScalar *xx;
169297b48c8fSBarry Smith   PetscScalar       *bb;
1693ace3abfcSBarry Smith   PetscBool         missing;
169417ab2063SBarry Smith 
16953a40ed3dSBarry Smith   PetscFunctionBegin;
169697b48c8fSBarry Smith   if (x && b) {
169797b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
169897b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
169997b48c8fSBarry Smith     for (i=0; i<N; i++) {
170097b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
170197b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
170297b48c8fSBarry Smith     }
170397b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
170497b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
170597b48c8fSBarry Smith   }
170697b48c8fSBarry Smith 
1707a9817697SBarry Smith   if (a->keepnonzeropattern) {
1708f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1709e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1710bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1711f1e2ffcdSBarry Smith     }
1712f4df32b1SMatthew Knepley     if (diag != 0.0) {
171309f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1714e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1715f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1716f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1717f1e2ffcdSBarry Smith       }
1718f1e2ffcdSBarry Smith     }
171988e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1720f1e2ffcdSBarry Smith   } else {
1721f4df32b1SMatthew Knepley     if (diag != 0.0) {
172217ab2063SBarry Smith       for (i=0; i<N; i++) {
1723e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17247ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1725416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1726f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1727bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
17287ae801bdSBarry Smith         } else { /* in case row was completely empty */
1729f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
173017ab2063SBarry Smith         }
173117ab2063SBarry Smith       }
17323a40ed3dSBarry Smith     } else {
173317ab2063SBarry Smith       for (i=0; i<N; i++) {
1734e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1735416022c9SBarry Smith         a->ilen[rows[i]] = 0;
173617ab2063SBarry Smith       }
173717ab2063SBarry Smith     }
173888e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1739f1e2ffcdSBarry Smith   }
174043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17413a40ed3dSBarry Smith   PetscFunctionReturn(0);
174217ab2063SBarry Smith }
174317ab2063SBarry Smith 
17444a2ae208SSatish Balay #undef __FUNCT__
17456e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
17466e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17476e169961SBarry Smith {
17486e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17496e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
17506e169961SBarry Smith   PetscErrorCode    ierr;
17512b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
17526e169961SBarry Smith   const PetscScalar *xx;
17536e169961SBarry Smith   PetscScalar       *bb;
17546e169961SBarry Smith 
17556e169961SBarry Smith   PetscFunctionBegin;
17566e169961SBarry Smith   if (x && b) {
17576e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
17586e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
17592b40b63fSBarry Smith     vecs = PETSC_TRUE;
17606e169961SBarry Smith   }
17616e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
17626e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
17636e169961SBarry Smith   for (i=0; i<N; i++) {
17646e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17656e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
17666e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
17676e169961SBarry Smith   }
17686e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
17696e169961SBarry Smith     if (!zeroed[i]) {
17706e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
17716e169961SBarry Smith         if (zeroed[a->j[j]]) {
17722b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
17736e169961SBarry Smith           a->a[j] = 0.0;
17746e169961SBarry Smith         }
17756e169961SBarry Smith       }
17762b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
17776e169961SBarry Smith   }
17786e169961SBarry Smith   if (x && b) {
17796e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
17806e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
17816e169961SBarry Smith   }
17826e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
17836e169961SBarry Smith   if (diag != 0.0) {
17846e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
17856e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
17866e169961SBarry Smith     for (i=0; i<N; i++) {
17876e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
17886e169961SBarry Smith     }
17896e169961SBarry Smith   }
17906e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
17916e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17926e169961SBarry Smith   PetscFunctionReturn(0);
17936e169961SBarry Smith }
17946e169961SBarry Smith 
17956e169961SBarry Smith #undef __FUNCT__
17964a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1797a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
179817ab2063SBarry Smith {
1799416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
180097f1f81fSBarry Smith   PetscInt   *itmp;
180117ab2063SBarry Smith 
18023a40ed3dSBarry Smith   PetscFunctionBegin;
1803e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
180417ab2063SBarry Smith 
1805416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1806bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
180717ab2063SBarry Smith   if (idx) {
1808bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1809bfeeae90SHong Zhang     if (*nz) {
18104e093b46SBarry Smith       *idx = itmp;
181117ab2063SBarry Smith     }
181217ab2063SBarry Smith     else *idx = 0;
181317ab2063SBarry Smith   }
18143a40ed3dSBarry Smith   PetscFunctionReturn(0);
181517ab2063SBarry Smith }
181617ab2063SBarry Smith 
1817bfeeae90SHong Zhang /* remove this function? */
18184a2ae208SSatish Balay #undef __FUNCT__
18194a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1820a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
182117ab2063SBarry Smith {
18223a40ed3dSBarry Smith   PetscFunctionBegin;
18233a40ed3dSBarry Smith   PetscFunctionReturn(0);
182417ab2063SBarry Smith }
182517ab2063SBarry Smith 
18264a2ae208SSatish Balay #undef __FUNCT__
18274a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1828dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
182917ab2063SBarry Smith {
1830416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
183154f21887SBarry Smith   MatScalar      *v = a->a;
183236db0b34SBarry Smith   PetscReal      sum = 0.0;
18336849ba73SBarry Smith   PetscErrorCode ierr;
183497f1f81fSBarry Smith   PetscInt       i,j;
183517ab2063SBarry Smith 
18363a40ed3dSBarry Smith   PetscFunctionBegin;
183717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1838416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1839aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
184036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
184117ab2063SBarry Smith #else
184217ab2063SBarry Smith       sum += (*v)*(*v); v++;
184317ab2063SBarry Smith #endif
184417ab2063SBarry Smith     }
18458f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
18463a40ed3dSBarry Smith   } else if (type == NORM_1) {
184736db0b34SBarry Smith     PetscReal *tmp;
184897f1f81fSBarry Smith     PetscInt    *jj = a->j;
1849d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1850d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1851064f8208SBarry Smith     *nrm = 0.0;
1852416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1853bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
185417ab2063SBarry Smith     }
1855d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1856064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
185717ab2063SBarry Smith     }
1858606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
18593a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1860064f8208SBarry Smith     *nrm = 0.0;
1861d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1862bfeeae90SHong Zhang       v = a->a + a->i[j];
186317ab2063SBarry Smith       sum = 0.0;
1864416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1865cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
186617ab2063SBarry Smith       }
1867064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
186817ab2063SBarry Smith     }
18693a40ed3dSBarry Smith   } else {
1870e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
187117ab2063SBarry Smith   }
18723a40ed3dSBarry Smith   PetscFunctionReturn(0);
187317ab2063SBarry Smith }
187417ab2063SBarry Smith 
18754a2ae208SSatish Balay #undef __FUNCT__
18764a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1877fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
187817ab2063SBarry Smith {
1879416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1880416022c9SBarry Smith   Mat            C;
18816849ba73SBarry Smith   PetscErrorCode ierr;
1882d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
188354f21887SBarry Smith   MatScalar      *array = a->a;
188417ab2063SBarry Smith 
18853a40ed3dSBarry Smith   PetscFunctionBegin;
1886e32f2f54SBarry 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");
1887fc4dec0aSBarry Smith 
1888fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1889d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1890d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1891bfeeae90SHong Zhang 
1892bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
18937adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1894d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
18957adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1896ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1897606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1898a541d17aSBarry Smith   } else {
1899a541d17aSBarry Smith     C = *B;
1900a541d17aSBarry Smith   }
1901a541d17aSBarry Smith 
190217ab2063SBarry Smith   for (i=0; i<m; i++) {
190317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
190487d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1905b9b97703SBarry Smith     array += len;
1906b9b97703SBarry Smith     aj    += len;
190717ab2063SBarry Smith   }
19086d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19096d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
191017ab2063SBarry Smith 
1911815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1912416022c9SBarry Smith     *B = C;
191317ab2063SBarry Smith   } else {
1914eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
191517ab2063SBarry Smith   }
19163a40ed3dSBarry Smith   PetscFunctionReturn(0);
191717ab2063SBarry Smith }
191817ab2063SBarry Smith 
1919cd0d46ebSvictorle EXTERN_C_BEGIN
1920cd0d46ebSvictorle #undef __FUNCT__
19215fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
19227087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1923cd0d46ebSvictorle {
1924cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
192554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
192654f21887SBarry Smith   MatScalar      *va,*vb;
19276849ba73SBarry Smith   PetscErrorCode ierr;
192897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1929cd0d46ebSvictorle 
1930cd0d46ebSvictorle   PetscFunctionBegin;
1931cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1932cd0d46ebSvictorle 
1933cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1934cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19355485867bSBarry Smith   if (ma!=nb || na!=mb){
19365485867bSBarry Smith     *f = PETSC_FALSE;
19375485867bSBarry Smith     PetscFunctionReturn(0);
19385485867bSBarry Smith   }
1939cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1940cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1941cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
194297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
194397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1944cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1945cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1946cd0d46ebSvictorle 
1947cd0d46ebSvictorle   *f = PETSC_TRUE;
1948cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1949cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
195097f1f81fSBarry Smith       PetscInt         idc,idr;
19515485867bSBarry Smith       PetscScalar vc,vr;
1952cd0d46ebSvictorle       /* column/row index/value */
19535485867bSBarry Smith       idc = adx[aptr[i]];
19545485867bSBarry Smith       idr = bdx[bptr[idc]];
19555485867bSBarry Smith       vc  = va[aptr[i]];
19565485867bSBarry Smith       vr  = vb[bptr[idc]];
19575485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
19585485867bSBarry Smith         *f = PETSC_FALSE;
19595485867bSBarry Smith         goto done;
1960cd0d46ebSvictorle       } else {
19615485867bSBarry Smith         aptr[i]++;
19625485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1963cd0d46ebSvictorle       }
1964cd0d46ebSvictorle     }
1965cd0d46ebSvictorle   }
1966cd0d46ebSvictorle  done:
1967cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
19683aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1969cd0d46ebSvictorle   PetscFunctionReturn(0);
1970cd0d46ebSvictorle }
1971cd0d46ebSvictorle EXTERN_C_END
1972cd0d46ebSvictorle 
19731cbb95d3SBarry Smith EXTERN_C_BEGIN
19741cbb95d3SBarry Smith #undef __FUNCT__
19751cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
19767087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
19771cbb95d3SBarry Smith {
19781cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
197954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
198054f21887SBarry Smith   MatScalar      *va,*vb;
19811cbb95d3SBarry Smith   PetscErrorCode ierr;
19821cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
19831cbb95d3SBarry Smith 
19841cbb95d3SBarry Smith   PetscFunctionBegin;
19851cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
19861cbb95d3SBarry Smith 
19871cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
19881cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19891cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
19901cbb95d3SBarry Smith     *f = PETSC_FALSE;
19911cbb95d3SBarry Smith     PetscFunctionReturn(0);
19921cbb95d3SBarry Smith   }
19931cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
19941cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
19951cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
19961cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
19971cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
19981cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
19991cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
20001cbb95d3SBarry Smith 
20011cbb95d3SBarry Smith   *f = PETSC_TRUE;
20021cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
20031cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
20041cbb95d3SBarry Smith       PetscInt         idc,idr;
20051cbb95d3SBarry Smith       PetscScalar vc,vr;
20061cbb95d3SBarry Smith       /* column/row index/value */
20071cbb95d3SBarry Smith       idc = adx[aptr[i]];
20081cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
20091cbb95d3SBarry Smith       vc  = va[aptr[i]];
20101cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
20111cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
20121cbb95d3SBarry Smith         *f = PETSC_FALSE;
20131cbb95d3SBarry Smith         goto done;
20141cbb95d3SBarry Smith       } else {
20151cbb95d3SBarry Smith         aptr[i]++;
20161cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
20171cbb95d3SBarry Smith       }
20181cbb95d3SBarry Smith     }
20191cbb95d3SBarry Smith   }
20201cbb95d3SBarry Smith  done:
20211cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
20221cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
20231cbb95d3SBarry Smith   PetscFunctionReturn(0);
20241cbb95d3SBarry Smith }
20251cbb95d3SBarry Smith EXTERN_C_END
20261cbb95d3SBarry Smith 
20279e29f15eSvictorle #undef __FUNCT__
20289e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2029ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20309e29f15eSvictorle {
2031dfbe8321SBarry Smith   PetscErrorCode ierr;
20329e29f15eSvictorle   PetscFunctionBegin;
20335485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20349e29f15eSvictorle   PetscFunctionReturn(0);
20359e29f15eSvictorle }
20369e29f15eSvictorle 
20374a2ae208SSatish Balay #undef __FUNCT__
20381cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2039ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20401cbb95d3SBarry Smith {
20411cbb95d3SBarry Smith   PetscErrorCode ierr;
20421cbb95d3SBarry Smith   PetscFunctionBegin;
20431cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20441cbb95d3SBarry Smith   PetscFunctionReturn(0);
20451cbb95d3SBarry Smith }
20461cbb95d3SBarry Smith 
20471cbb95d3SBarry Smith #undef __FUNCT__
20484a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2049dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
205017ab2063SBarry Smith {
2051416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
205254f21887SBarry Smith   PetscScalar    *l,*r,x;
205354f21887SBarry Smith   MatScalar      *v;
2054dfbe8321SBarry Smith   PetscErrorCode ierr;
2055d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
205617ab2063SBarry Smith 
20573a40ed3dSBarry Smith   PetscFunctionBegin;
205817ab2063SBarry Smith   if (ll) {
20593ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
20603ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2061e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2062e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
20631ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2064416022c9SBarry Smith     v = a->a;
206517ab2063SBarry Smith     for (i=0; i<m; i++) {
206617ab2063SBarry Smith       x = l[i];
2067416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
206817ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
206917ab2063SBarry Smith     }
20701ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2071efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
207217ab2063SBarry Smith   }
207317ab2063SBarry Smith   if (rr) {
2074e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2075e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
20761ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2077416022c9SBarry Smith     v = a->a; jj = a->j;
207817ab2063SBarry Smith     for (i=0; i<nz; i++) {
2079bfeeae90SHong Zhang       (*v++) *= r[*jj++];
208017ab2063SBarry Smith     }
20811ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2082efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
208317ab2063SBarry Smith   }
20843a40ed3dSBarry Smith   PetscFunctionReturn(0);
208517ab2063SBarry Smith }
208617ab2063SBarry Smith 
20874a2ae208SSatish Balay #undef __FUNCT__
20884a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
208997f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
209017ab2063SBarry Smith {
2091db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
20926849ba73SBarry Smith   PetscErrorCode ierr;
2093d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
209497f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
20955d0c19d7SBarry Smith   const PetscInt *irow,*icol;
20965d0c19d7SBarry Smith   PetscInt       nrows,ncols;
209797f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
209854f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2099416022c9SBarry Smith   Mat            C;
2100ace3abfcSBarry Smith   PetscBool      stride,sorted;
210117ab2063SBarry Smith 
21023a40ed3dSBarry Smith   PetscFunctionBegin;
210314ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2104e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
210514ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2106e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
210799141d43SSatish Balay 
210817ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2109b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2110b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
211117ab2063SBarry Smith 
2112fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
21130dbe5b1eSSatish Balay   ierr = PetscTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2114fee21e36SBarry Smith   if (stride && step == 1) {
211502834360SBarry Smith     /* special case of contiguous rows */
21160e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
211702834360SBarry Smith     /* loop over new rows determining lens and starting points */
211802834360SBarry Smith     for (i=0; i<nrows; i++) {
2119bfeeae90SHong Zhang       kstart  = ai[irow[i]];
2120a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
212102834360SBarry Smith       for (k=kstart; k<kend; k++) {
2122bfeeae90SHong Zhang         if (aj[k] >= first) {
212302834360SBarry Smith           starts[i] = k;
212402834360SBarry Smith           break;
212502834360SBarry Smith 	}
212602834360SBarry Smith       }
2127a2744918SBarry Smith       sum = 0;
212802834360SBarry Smith       while (k < kend) {
2129bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2130a2744918SBarry Smith         sum++;
213102834360SBarry Smith       }
2132a2744918SBarry Smith       lens[i] = sum;
213302834360SBarry Smith     }
213402834360SBarry Smith     /* create submatrix */
2135cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
213697f1f81fSBarry Smith       PetscInt n_cols,n_rows;
213708480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2138e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2139d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
214008480c60SBarry Smith       C = *B;
21413a40ed3dSBarry Smith     } else {
21427adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2143f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21447adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2145ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
214608480c60SBarry Smith     }
2147db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2148db02288aSLois Curfman McInnes 
214902834360SBarry Smith     /* loop over rows inserting into submatrix */
2150db02288aSLois Curfman McInnes     a_new    = c->a;
2151db02288aSLois Curfman McInnes     j_new    = c->j;
2152db02288aSLois Curfman McInnes     i_new    = c->i;
2153bfeeae90SHong Zhang 
215402834360SBarry Smith     for (i=0; i<nrows; i++) {
2155a2744918SBarry Smith       ii    = starts[i];
2156a2744918SBarry Smith       lensi = lens[i];
2157a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2158a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
215902834360SBarry Smith       }
216087828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2161a2744918SBarry Smith       a_new      += lensi;
2162a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
2163a2744918SBarry Smith       c->ilen[i]  = lensi;
216402834360SBarry Smith     }
21650e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
21663a40ed3dSBarry Smith   } else {
216702834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
21680e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
216997f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
21700e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
21714dcab191SBarry Smith     for (i=0; i<ncols; i++) {
21724dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
21734dcab191SBarry Smith       if (icol[i] >= oldcols) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requesting column beyond largest column icol[%D] %D <= A->cmap->n %D",i,icol[i],oldcols);
21744dcab191SBarry Smith #endif
21754dcab191SBarry Smith       smap[icol[i]] = i+1;
21764dcab191SBarry Smith     }
21774dcab191SBarry Smith 
217802834360SBarry Smith     /* determine lens of each row */
217902834360SBarry Smith     for (i=0; i<nrows; i++) {
2180bfeeae90SHong Zhang       kstart  = ai[irow[i]];
218102834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
218202834360SBarry Smith       lens[i] = 0;
218302834360SBarry Smith       for (k=kstart; k<kend; k++) {
2184bfeeae90SHong Zhang         if (smap[aj[k]]) {
218502834360SBarry Smith           lens[i]++;
218602834360SBarry Smith         }
218702834360SBarry Smith       }
218802834360SBarry Smith     }
218917ab2063SBarry Smith     /* Create and fill new matrix */
2190a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2191ace3abfcSBarry Smith       PetscBool  equal;
21920f5bd95cSBarry Smith 
219399141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2194e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2195d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
21960f5bd95cSBarry Smith       if (!equal) {
2197e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
219899141d43SSatish Balay       }
2199d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
220008480c60SBarry Smith       C = *B;
22013a40ed3dSBarry Smith     } else {
22027adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2203f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22047adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2205ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
220608480c60SBarry Smith     }
220799141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
220817ab2063SBarry Smith     for (i=0; i<nrows; i++) {
220999141d43SSatish Balay       row    = irow[i];
2210bfeeae90SHong Zhang       kstart = ai[row];
221199141d43SSatish Balay       kend   = kstart + a->ilen[row];
2212bfeeae90SHong Zhang       mat_i  = c->i[i];
221399141d43SSatish Balay       mat_j  = c->j + mat_i;
221499141d43SSatish Balay       mat_a  = c->a + mat_i;
221599141d43SSatish Balay       mat_ilen = c->ilen + i;
221617ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2217bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2218ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
221999141d43SSatish Balay           *mat_a++ = a->a[k];
222099141d43SSatish Balay           (*mat_ilen)++;
222199141d43SSatish Balay 
222217ab2063SBarry Smith         }
222317ab2063SBarry Smith       }
222417ab2063SBarry Smith     }
222502834360SBarry Smith     /* Free work space */
222602834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2227606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2228606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
222902834360SBarry Smith   }
22306d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22316d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
223217ab2063SBarry Smith 
223317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2234416022c9SBarry Smith   *B = C;
22353a40ed3dSBarry Smith   PetscFunctionReturn(0);
223617ab2063SBarry Smith }
223717ab2063SBarry Smith 
22381df811f5SHong Zhang #undef __FUNCT__
223982d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
224082d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
224182d44351SHong Zhang {
224282d44351SHong Zhang   PetscErrorCode ierr;
224382d44351SHong Zhang   Mat            B;
224482d44351SHong Zhang 
224582d44351SHong Zhang   PetscFunctionBegin;
224682d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
224782d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
224882d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
224982d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
225082d44351SHong Zhang   *subMat = B;
225182d44351SHong Zhang   PetscFunctionReturn(0);
225282d44351SHong Zhang }
225382d44351SHong Zhang 
225482d44351SHong Zhang #undef __FUNCT__
22554a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
22560481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2257a871dcd8SBarry Smith {
225863b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2259dfbe8321SBarry Smith   PetscErrorCode ierr;
226063b91edcSBarry Smith   Mat            outA;
2261ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
226263b91edcSBarry Smith 
22633a40ed3dSBarry Smith   PetscFunctionBegin;
2264e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
22651df811f5SHong Zhang 
2266b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2267b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2268a871dcd8SBarry Smith 
226963b91edcSBarry Smith   outA              = inA;
2270d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2271c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
22726bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2273c3122656SLisandro Dalcin   a->row = row;
2274c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
22756bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2276c3122656SLisandro Dalcin   a->col = col;
227763b91edcSBarry Smith 
227836db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
22796bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
22804c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
228152e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2282f0ec6fceSSatish Balay 
228394a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2284d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2285d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
228694a9d846SBarry Smith   }
228763b91edcSBarry Smith 
2288f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2289137fb511SHong Zhang   if (row_identity && col_identity) {
2290ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2291137fb511SHong Zhang   } else {
2292719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2293137fb511SHong Zhang   }
22943a40ed3dSBarry Smith   PetscFunctionReturn(0);
2295a871dcd8SBarry Smith }
2296a871dcd8SBarry Smith 
22974a2ae208SSatish Balay #undef __FUNCT__
22984a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2299f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2300f0b747eeSBarry Smith {
2301f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2302f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2303efee365bSSatish Balay   PetscErrorCode ierr;
23040805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
23053a40ed3dSBarry Smith 
23063a40ed3dSBarry Smith   PetscFunctionBegin;
2307f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2308efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
23093a40ed3dSBarry Smith   PetscFunctionReturn(0);
2310f0b747eeSBarry Smith }
2311f0b747eeSBarry Smith 
23124a2ae208SSatish Balay #undef __FUNCT__
23134a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
231497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2315cddf8d76SBarry Smith {
2316dfbe8321SBarry Smith   PetscErrorCode ierr;
231797f1f81fSBarry Smith   PetscInt       i;
2318cddf8d76SBarry Smith 
23193a40ed3dSBarry Smith   PetscFunctionBegin;
2320cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2321b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2322cddf8d76SBarry Smith   }
2323cddf8d76SBarry Smith 
2324cddf8d76SBarry Smith   for (i=0; i<n; i++) {
23256a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2326cddf8d76SBarry Smith   }
23273a40ed3dSBarry Smith   PetscFunctionReturn(0);
2328cddf8d76SBarry Smith }
2329cddf8d76SBarry Smith 
23304a2ae208SSatish Balay #undef __FUNCT__
23314a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
233297f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
23334dcbc457SBarry Smith {
2334e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23356849ba73SBarry Smith   PetscErrorCode ierr;
23365d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
23375d0c19d7SBarry Smith   const PetscInt *idx;
233897f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2339f1af5d2fSBarry Smith   PetscBT        table;
2340bbd702dbSSatish Balay 
23413a40ed3dSBarry Smith   PetscFunctionBegin;
2342d0f46423SBarry Smith   m     = A->rmap->n;
2343e4d965acSSatish Balay   ai    = a->i;
2344bfeeae90SHong Zhang   aj    = a->j;
23458a047759SSatish Balay 
2346e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
234706763907SSatish Balay 
234897f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
23496831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
235006763907SSatish Balay 
2351e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2352b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2353e4d965acSSatish Balay     isz  = 0;
23546831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2355e4d965acSSatish Balay 
2356e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
23574dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2358b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2359e4d965acSSatish Balay 
2360dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2361e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2362f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
23634dcbc457SBarry Smith     }
236406763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
23656bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2366e4d965acSSatish Balay 
236704a348a9SBarry Smith     k = 0;
236804a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
236904a348a9SBarry Smith       n = isz;
237006763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2371e4d965acSSatish Balay         row   = nidx[k];
2372e4d965acSSatish Balay         start = ai[row];
2373e4d965acSSatish Balay         end   = ai[row+1];
237404a348a9SBarry Smith         for (l = start; l<end ; l++){
2375efb16452SHong Zhang           val = aj[l] ;
2376f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2377e4d965acSSatish Balay         }
2378e4d965acSSatish Balay       }
2379e4d965acSSatish Balay     }
238070b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2381e4d965acSSatish Balay   }
23826831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2383606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
23843a40ed3dSBarry Smith   PetscFunctionReturn(0);
23854dcbc457SBarry Smith }
238617ab2063SBarry Smith 
23870513a670SBarry Smith /* -------------------------------------------------------------- */
23884a2ae208SSatish Balay #undef __FUNCT__
23894a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2390dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
23910513a670SBarry Smith {
23920513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23936849ba73SBarry Smith   PetscErrorCode ierr;
23943b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
23955d0c19d7SBarry Smith   const PetscInt *row,*col;
23965d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
239756cd22aeSBarry Smith   IS             icolp,irowp;
23983b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
23993b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
24000513a670SBarry Smith 
24013a40ed3dSBarry Smith   PetscFunctionBegin;
24024c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
240356cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
24044c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
240556cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
24060513a670SBarry Smith 
24070513a670SBarry Smith   /* determine lengths of permuted rows */
240897f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
24090513a670SBarry Smith   for (i=0; i<m; i++) {
24100513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
24110513a670SBarry Smith   }
24127adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2413f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
24147adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2415ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2416606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
24170513a670SBarry Smith 
241897f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
24190513a670SBarry Smith   for (i=0; i<m; i++) {
242032ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24210513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2422cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
242332ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24240513a670SBarry Smith   }
2425606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
24263c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
24270513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24280513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
242956cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
243056cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
24316bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
24326bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
24333a40ed3dSBarry Smith   PetscFunctionReturn(0);
24340513a670SBarry Smith }
24350513a670SBarry Smith 
24364a2ae208SSatish Balay #undef __FUNCT__
24374a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2438dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2439cb5b572fSBarry Smith {
2440dfbe8321SBarry Smith   PetscErrorCode ierr;
2441cb5b572fSBarry Smith 
2442cb5b572fSBarry Smith   PetscFunctionBegin;
244333f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
244433f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2445be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2446be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2447be6bf707SBarry Smith 
2448700c5bfcSBarry 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");
2449d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2450cb5b572fSBarry Smith   } else {
2451cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2452cb5b572fSBarry Smith   }
2453cb5b572fSBarry Smith   PetscFunctionReturn(0);
2454cb5b572fSBarry Smith }
2455cb5b572fSBarry Smith 
24564a2ae208SSatish Balay #undef __FUNCT__
24574a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2458dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2459273d9f13SBarry Smith {
2460dfbe8321SBarry Smith   PetscErrorCode ierr;
2461273d9f13SBarry Smith 
2462273d9f13SBarry Smith   PetscFunctionBegin;
2463ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2464273d9f13SBarry Smith   PetscFunctionReturn(0);
2465273d9f13SBarry Smith }
2466273d9f13SBarry Smith 
24674a2ae208SSatish Balay #undef __FUNCT__
24684a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2469a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
24706c0721eeSBarry Smith {
24716c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
24726c0721eeSBarry Smith   PetscFunctionBegin;
24736c0721eeSBarry Smith   *array = a->a;
24746c0721eeSBarry Smith   PetscFunctionReturn(0);
24756c0721eeSBarry Smith }
24766c0721eeSBarry Smith 
24774a2ae208SSatish Balay #undef __FUNCT__
24784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2479dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
24806c0721eeSBarry Smith {
24816c0721eeSBarry Smith   PetscFunctionBegin;
24826c0721eeSBarry Smith   PetscFunctionReturn(0);
24836c0721eeSBarry Smith }
2484273d9f13SBarry Smith 
2485ee4f033dSBarry Smith #undef __FUNCT__
2486ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2487dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2488ee4f033dSBarry Smith {
24896849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
24906849ba73SBarry Smith   PetscErrorCode ierr;
249197f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2492efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
249387828ca2SBarry Smith   PetscScalar    *vscale_array;
2494ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2495ee4f033dSBarry Smith   Vec            w1,w2,w3;
2496ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2497ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2498ee4f033dSBarry Smith 
2499ee4f033dSBarry Smith   PetscFunctionBegin;
2500ee4f033dSBarry Smith   if (!coloring->w1) {
2501ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
250252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2503ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
250452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2505ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
250652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2507ee4f033dSBarry Smith   }
2508ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2509ee4f033dSBarry Smith 
2510ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2511acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2512ee4f033dSBarry Smith   if (flg) {
2513ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2514ee4f033dSBarry Smith   } else {
2515ace3abfcSBarry Smith     PetscBool  assembled;
25160b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
25170b9b6f31SBarry Smith     if (assembled) {
2518ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2519ee4f033dSBarry Smith     }
25200b9b6f31SBarry Smith   }
2521ee4f033dSBarry Smith 
2522ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2523ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2524ee4f033dSBarry Smith 
2525ee4f033dSBarry Smith   /*
2526ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2527ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2528ee4f033dSBarry Smith   */
2529ee4f033dSBarry Smith   if (coloring->F) {
2530ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2531ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2532ee4f033dSBarry Smith     if (m1 != m2) {
2533ee4f033dSBarry Smith       coloring->F = 0;
2534ee4f033dSBarry Smith     }
2535ee4f033dSBarry Smith   }
2536ee4f033dSBarry Smith 
2537ee4f033dSBarry Smith   if (coloring->F) {
2538ee4f033dSBarry Smith     w1          = coloring->F;
2539ee4f033dSBarry Smith     coloring->F = 0;
2540ee4f033dSBarry Smith   } else {
254166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2542ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
254366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2544ee4f033dSBarry Smith   }
2545ee4f033dSBarry Smith 
2546ee4f033dSBarry Smith   /*
2547ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2548ee4f033dSBarry Smith   */
25491ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
25501ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2551ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2552ee4f033dSBarry Smith     /*
2553ee4f033dSBarry Smith        Loop over each column associated with color adding the
2554ee4f033dSBarry Smith        perturbation to the vector w3.
2555ee4f033dSBarry Smith     */
2556ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2557ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2558ee4f033dSBarry Smith       dx  = xx[col];
2559ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2560ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2561ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2562ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2563ee4f033dSBarry Smith #else
2564ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2565ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2566ee4f033dSBarry Smith #endif
2567ee4f033dSBarry Smith       dx                *= epsilon;
2568ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2569ee4f033dSBarry Smith     }
2570ee4f033dSBarry Smith   }
25711ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2572ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2573ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2574ee4f033dSBarry Smith 
2575ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2576ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2577ee4f033dSBarry Smith 
2578ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2579ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2580ee4f033dSBarry Smith 
25811ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2582ee4f033dSBarry Smith   /*
2583ee4f033dSBarry Smith       Loop over each color
2584ee4f033dSBarry Smith   */
2585ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
258649b058dcSBarry Smith     coloring->currentcolor = k;
2587ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
25881ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2589ee4f033dSBarry Smith     /*
2590ee4f033dSBarry Smith        Loop over each column associated with color adding the
2591ee4f033dSBarry Smith        perturbation to the vector w3.
2592ee4f033dSBarry Smith     */
2593ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2594ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2595ee4f033dSBarry Smith       dx  = xx[col];
25965b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2597ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2598ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2599ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2600ee4f033dSBarry Smith #else
2601ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2602ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2603ee4f033dSBarry Smith #endif
2604ee4f033dSBarry Smith       dx            *= epsilon;
2605e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2606ee4f033dSBarry Smith       w3_array[col] += dx;
2607ee4f033dSBarry Smith     }
26081ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2609ee4f033dSBarry Smith 
2610ee4f033dSBarry Smith     /*
2611ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2612ee4f033dSBarry Smith     */
2613ee4f033dSBarry Smith 
261466f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2615ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
261666f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2617efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2618ee4f033dSBarry Smith 
2619ee4f033dSBarry Smith     /*
2620ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2621ee4f033dSBarry Smith     */
26221ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2623ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2624ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2625ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2626ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2627ee4f033dSBarry Smith       srow   = row + start;
2628ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2629ee4f033dSBarry Smith     }
26301ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2631ee4f033dSBarry Smith   }
263249b058dcSBarry Smith   coloring->currentcolor = k;
26331ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
26341ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2635ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2636ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2637ee4f033dSBarry Smith   PetscFunctionReturn(0);
2638ee4f033dSBarry Smith }
2639ee4f033dSBarry Smith 
26408229c054SShri Abhyankar /*
26418229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26428229c054SShri Abhyankar    have different nonzero structure.
26438229c054SShri Abhyankar */
2644ac90fabeSBarry Smith #undef __FUNCT__
26458229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
26468229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2647ec7775f6SShri Abhyankar {
26488229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2649ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2650ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2651ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2652ec7775f6SShri Abhyankar 
2653ec7775f6SShri Abhyankar   PetscFunctionBegin;
2654ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2655ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
26568af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
26578af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
26588af7cee1SJed Brown     nnz[i] = 0;
26598af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
26608af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
26618af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
26628af7cee1SJed Brown       nnz[i]++;
26638af7cee1SJed Brown     }
26648af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2665ec7775f6SShri Abhyankar   }
2666ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2667ec7775f6SShri Abhyankar }
2668ec7775f6SShri Abhyankar 
2669ec7775f6SShri Abhyankar #undef __FUNCT__
2670ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2671f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2672ac90fabeSBarry Smith {
2673dfbe8321SBarry Smith   PetscErrorCode ierr;
267497f1f81fSBarry Smith   PetscInt       i;
2675ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
26760805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2677ac90fabeSBarry Smith 
2678ac90fabeSBarry Smith   PetscFunctionBegin;
2679ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2680f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2681f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2682c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2683a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2684a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
26856bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2686a30b2313SHong Zhang     }
2687a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2688d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2689a30b2313SHong Zhang       y->XtoY = X;
2690407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2691c537a176SHong Zhang     }
2692f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
26931e2582c4SBarry 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);
2694ac90fabeSBarry Smith   } else {
26958229c054SShri Abhyankar     Mat      B;
26968229c054SShri Abhyankar     PetscInt *nnz;
269716b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2698ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2699bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27004aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2701ec7775f6SShri Abhyankar     ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
27028229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
27038229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2704ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2705ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
27068229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2707ac90fabeSBarry Smith   }
2708ac90fabeSBarry Smith   PetscFunctionReturn(0);
2709ac90fabeSBarry Smith }
2710ac90fabeSBarry Smith 
2711521d7252SBarry Smith #undef __FUNCT__
2712521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2713521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2714521d7252SBarry Smith {
271541c166b1SJed Brown   PetscErrorCode ierr;
271641c166b1SJed Brown 
2717521d7252SBarry Smith   PetscFunctionBegin;
271841c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
271941c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2720521d7252SBarry Smith   PetscFunctionReturn(0);
2721521d7252SBarry Smith }
2722521d7252SBarry Smith 
2723354c94deSBarry Smith #undef __FUNCT__
2724354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27257087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2726354c94deSBarry Smith {
2727354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2728354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2729354c94deSBarry Smith   PetscInt    i,nz;
2730354c94deSBarry Smith   PetscScalar *a;
2731354c94deSBarry Smith 
2732354c94deSBarry Smith   PetscFunctionBegin;
2733354c94deSBarry Smith   nz = aij->nz;
2734354c94deSBarry Smith   a  = aij->a;
2735354c94deSBarry Smith   for (i=0; i<nz; i++) {
2736354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2737354c94deSBarry Smith   }
2738354c94deSBarry Smith #else
2739354c94deSBarry Smith   PetscFunctionBegin;
2740354c94deSBarry Smith #endif
2741354c94deSBarry Smith   PetscFunctionReturn(0);
2742354c94deSBarry Smith }
2743354c94deSBarry Smith 
2744e34fafa9SBarry Smith #undef __FUNCT__
2745985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2746985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2747e34fafa9SBarry Smith {
2748e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2749e34fafa9SBarry Smith   PetscErrorCode ierr;
2750d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2751e34fafa9SBarry Smith   PetscReal      atmp;
2752985db425SBarry Smith   PetscScalar    *x;
2753e34fafa9SBarry Smith   MatScalar      *aa;
2754e34fafa9SBarry Smith 
2755e34fafa9SBarry Smith   PetscFunctionBegin;
2756e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2757e34fafa9SBarry Smith   aa   = a->a;
2758e34fafa9SBarry Smith   ai   = a->i;
2759e34fafa9SBarry Smith   aj   = a->j;
2760e34fafa9SBarry Smith 
2761985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2762e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2763e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2764e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2765e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2766e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27679189402eSHong Zhang     x[i] = 0.0;
2768e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2769985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2770985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2771985db425SBarry Smith       aa++; aj++;
2772985db425SBarry Smith     }
2773985db425SBarry Smith   }
2774985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2775985db425SBarry Smith   PetscFunctionReturn(0);
2776985db425SBarry Smith }
2777985db425SBarry Smith 
2778985db425SBarry Smith #undef __FUNCT__
2779985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2780985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2781985db425SBarry Smith {
2782985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2783985db425SBarry Smith   PetscErrorCode ierr;
2784d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2785985db425SBarry Smith   PetscScalar    *x;
2786985db425SBarry Smith   MatScalar      *aa;
2787985db425SBarry Smith 
2788985db425SBarry Smith   PetscFunctionBegin;
2789e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2790985db425SBarry Smith   aa   = a->a;
2791985db425SBarry Smith   ai   = a->i;
2792985db425SBarry Smith   aj   = a->j;
2793985db425SBarry Smith 
2794985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2795985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2796985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2797e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2798985db425SBarry Smith   for (i=0; i<m; i++) {
2799985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2800d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2801985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2802985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2803985db425SBarry Smith       x[i] = 0.0;
2804985db425SBarry Smith       if (idx) {
2805985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2806985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2807985db425SBarry Smith           if (aj[j] > j) {
2808985db425SBarry Smith             idx[i] = j;
2809985db425SBarry Smith             break;
2810985db425SBarry Smith           }
2811985db425SBarry Smith         }
2812985db425SBarry Smith       }
2813985db425SBarry Smith     }
2814985db425SBarry Smith     for (j=0; j<ncols; j++){
2815985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2816985db425SBarry Smith       aa++; aj++;
2817985db425SBarry Smith     }
2818985db425SBarry Smith   }
2819985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2820985db425SBarry Smith   PetscFunctionReturn(0);
2821985db425SBarry Smith }
2822985db425SBarry Smith 
2823985db425SBarry Smith #undef __FUNCT__
2824c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2825c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2826c87e5d42SMatthew Knepley {
2827c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2828c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2829c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2830c87e5d42SMatthew Knepley   PetscReal      atmp;
2831c87e5d42SMatthew Knepley   PetscScalar    *x;
2832c87e5d42SMatthew Knepley   MatScalar      *aa;
2833c87e5d42SMatthew Knepley 
2834c87e5d42SMatthew Knepley   PetscFunctionBegin;
2835e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2836c87e5d42SMatthew Knepley   aa   = a->a;
2837c87e5d42SMatthew Knepley   ai   = a->i;
2838c87e5d42SMatthew Knepley   aj   = a->j;
2839c87e5d42SMatthew Knepley 
2840c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2841c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2842c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2843e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2844c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2845c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2846289a08f5SMatthew Knepley     if (ncols) {
2847289a08f5SMatthew Knepley       /* Get first nonzero */
2848289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2849289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2850289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2851289a08f5SMatthew Knepley       }
2852289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2853289a08f5SMatthew Knepley     } else {
2854289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2855289a08f5SMatthew Knepley     }
2856c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2857c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2858289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2859c87e5d42SMatthew Knepley       aa++; aj++;
2860c87e5d42SMatthew Knepley     }
2861c87e5d42SMatthew Knepley   }
2862c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2863c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2864c87e5d42SMatthew Knepley }
2865c87e5d42SMatthew Knepley 
2866c87e5d42SMatthew Knepley #undef __FUNCT__
2867985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2868985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2869985db425SBarry Smith {
2870985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2871985db425SBarry Smith   PetscErrorCode ierr;
2872d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2873985db425SBarry Smith   PetscScalar    *x;
2874985db425SBarry Smith   MatScalar      *aa;
2875985db425SBarry Smith 
2876985db425SBarry Smith   PetscFunctionBegin;
2877e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2878985db425SBarry Smith   aa   = a->a;
2879985db425SBarry Smith   ai   = a->i;
2880985db425SBarry Smith   aj   = a->j;
2881985db425SBarry Smith 
2882985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2883985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2884985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2885e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2886985db425SBarry Smith   for (i=0; i<m; i++) {
2887985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2888d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2889985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2890985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2891985db425SBarry Smith       x[i] = 0.0;
2892985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2893985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2894985db425SBarry Smith         for (j=0;j<ncols;j++) {
2895985db425SBarry Smith           if (aj[j] > j) {
2896985db425SBarry Smith             idx[i] = j;
2897985db425SBarry Smith             break;
2898985db425SBarry Smith           }
2899985db425SBarry Smith         }
2900985db425SBarry Smith       }
2901985db425SBarry Smith     }
2902985db425SBarry Smith     for (j=0; j<ncols; j++){
2903985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2904985db425SBarry Smith       aa++; aj++;
2905e34fafa9SBarry Smith     }
2906e34fafa9SBarry Smith   }
2907e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2908e34fafa9SBarry Smith   PetscFunctionReturn(0);
2909e34fafa9SBarry Smith }
2910bbead8a2SBarry Smith 
2911bbead8a2SBarry Smith #include <petscblaslapack.h>
2912bbead8a2SBarry Smith #include <../src/mat/blockinvert.h>
2913bbead8a2SBarry Smith 
2914bbead8a2SBarry Smith #undef __FUNCT__
2915bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2916bbead8a2SBarry Smith PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,PetscScalar **values)
2917bbead8a2SBarry Smith {
2918bbead8a2SBarry Smith   Mat_SeqAIJ    *a = (Mat_SeqAIJ*) A->data;
2919bbead8a2SBarry Smith   PetscErrorCode ierr;
292034fc4b71SJed Brown   PetscInt       i,bs = A->rmap->bs,mbs = A->rmap->n/A->rmap->bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
2921bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2922bbead8a2SBarry Smith   PetscReal      shift = 0.0;
2923bbead8a2SBarry Smith 
2924bbead8a2SBarry Smith   PetscFunctionBegin;
29254a0d0026SBarry Smith   if (a->ibdiagvalid) {
29264a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29274a0d0026SBarry Smith     PetscFunctionReturn(0);
29284a0d0026SBarry Smith   }
2929bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2930bbead8a2SBarry Smith   if (!a->ibdiag) {
2931bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
2932bbead8a2SBarry Smith     ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2933bbead8a2SBarry Smith   }
2934bbead8a2SBarry Smith   diag    = a->ibdiag;
2935bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2936bbead8a2SBarry Smith   /* factor and invert each block */
2937bbead8a2SBarry Smith   switch (bs){
2938bbead8a2SBarry Smith     case 1:
2939bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2940bbead8a2SBarry Smith         ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2941bbead8a2SBarry Smith         diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2942bbead8a2SBarry Smith       }
2943bbead8a2SBarry Smith       break;
2944bbead8a2SBarry Smith     case 2:
2945bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2946bbead8a2SBarry Smith         ij[0] = 2*i; ij[1] = 2*i + 1;
2947bbead8a2SBarry Smith         ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
2948bbead8a2SBarry Smith 	ierr  = Kernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
2949bbead8a2SBarry Smith 	diag  += 4;
2950bbead8a2SBarry Smith       }
2951bbead8a2SBarry Smith       break;
2952bbead8a2SBarry Smith     case 3:
2953bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2954bbead8a2SBarry Smith         ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2955bbead8a2SBarry Smith         ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
2956bbead8a2SBarry Smith 	ierr     = Kernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
2957bbead8a2SBarry Smith 	diag    += 9;
2958bbead8a2SBarry Smith       }
2959bbead8a2SBarry Smith       break;
2960bbead8a2SBarry Smith     case 4:
2961bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2962bbead8a2SBarry Smith         ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
2963bbead8a2SBarry Smith         ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
2964bbead8a2SBarry Smith 	ierr   = Kernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
2965bbead8a2SBarry Smith 	diag  += 16;
2966bbead8a2SBarry Smith       }
2967bbead8a2SBarry Smith       break;
2968bbead8a2SBarry Smith     case 5:
2969bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2970bbead8a2SBarry Smith         ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
2971bbead8a2SBarry Smith         ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
2972bbead8a2SBarry Smith 	ierr   = Kernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
2973bbead8a2SBarry Smith 	diag  += 25;
2974bbead8a2SBarry Smith       }
2975bbead8a2SBarry Smith       break;
2976bbead8a2SBarry Smith     case 6:
2977bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2978bbead8a2SBarry Smith         ij[0] = 6*i; ij[1] = 6*i + 1; ij[2] = 6*i + 2; ij[3] = 6*i + 3; ij[4] = 6*i + 4; ij[5] = 6*i + 5;
2979bbead8a2SBarry Smith         ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
2980bbead8a2SBarry Smith 	ierr   = Kernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
2981bbead8a2SBarry Smith 	diag  += 36;
2982bbead8a2SBarry Smith       }
2983bbead8a2SBarry Smith       break;
2984bbead8a2SBarry Smith     case 7:
2985bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2986bbead8a2SBarry Smith         ij[0] = 7*i; ij[1] = 7*i + 1; ij[2] = 7*i + 2; ij[3] = 7*i + 3; ij[4] = 7*i + 4; ij[5] = 7*i + 5; ij[5] = 7*i + 6;
2987bbead8a2SBarry Smith         ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
2988bbead8a2SBarry Smith 	ierr   = Kernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
2989bbead8a2SBarry Smith 	diag  += 49;
2990bbead8a2SBarry Smith       }
2991bbead8a2SBarry Smith       break;
2992bbead8a2SBarry Smith     default:
2993bbead8a2SBarry Smith       ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
2994bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2995bbead8a2SBarry Smith         for (j=0; j<bs; j++) {
2996bbead8a2SBarry Smith           IJ[j] = bs*i + j;
2997bbead8a2SBarry Smith         }
2998bbead8a2SBarry Smith         ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
2999bbead8a2SBarry Smith         ierr   = Kernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
3000bbead8a2SBarry Smith 	diag  += bs2;
3001bbead8a2SBarry Smith       }
3002bbead8a2SBarry Smith       ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3003bbead8a2SBarry Smith   }
3004bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3005bbead8a2SBarry Smith   PetscFunctionReturn(0);
3006bbead8a2SBarry Smith }
3007bbead8a2SBarry Smith 
30087087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3009682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
30100a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3011cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
3012cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
3013cb5b572fSBarry Smith        MatMult_SeqAIJ,
301497304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
30157c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
30167c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
3017db4efbfdSBarry Smith        0,
3018db4efbfdSBarry Smith        0,
3019db4efbfdSBarry Smith        0,
3020db4efbfdSBarry Smith /*10*/ 0,
3021cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
3022cb5b572fSBarry Smith        0,
302341f059aeSBarry Smith        MatSOR_SeqAIJ,
302417ab2063SBarry Smith        MatTranspose_SeqAIJ,
302597304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
3026cb5b572fSBarry Smith        MatEqual_SeqAIJ,
3027cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
3028cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
3029cb5b572fSBarry Smith        MatNorm_SeqAIJ,
303097304618SKris Buschelman /*20*/ 0,
3031cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
3032cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
3033cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
3034d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
3035db4efbfdSBarry Smith        0,
3036db4efbfdSBarry Smith        0,
3037db4efbfdSBarry Smith        0,
3038db4efbfdSBarry Smith        0,
3039d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
3040db4efbfdSBarry Smith        0,
3041db4efbfdSBarry Smith        0,
30426c0721eeSBarry Smith        MatGetArray_SeqAIJ,
30436c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
3044d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
3045cb5b572fSBarry Smith        0,
3046cb5b572fSBarry Smith        0,
3047cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
3048cb5b572fSBarry Smith        0,
3049d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
3050cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
3051cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
3052cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
3053cb5b572fSBarry Smith        MatCopy_SeqAIJ,
3054d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
3055cb5b572fSBarry Smith        MatScale_SeqAIJ,
3056cb5b572fSBarry Smith        0,
305779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
30586e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
3059d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
30603b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
30613b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
30623b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
3063a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
3064d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
3065b9617806SBarry Smith        0,
30660513a670SBarry Smith        0,
3067cda55fadSBarry Smith        MatPermute_SeqAIJ,
3068cda55fadSBarry Smith        0,
3069d519adbfSMatthew Knepley /*59*/ 0,
3070b9b97703SBarry Smith        MatDestroy_SeqAIJ,
3071b9b97703SBarry Smith        MatView_SeqAIJ,
3072357abbc8SBarry Smith        0,
3073ee4f033dSBarry Smith        0,
3074d519adbfSMatthew Knepley /*64*/ 0,
3075ee4f033dSBarry Smith        0,
3076ee4f033dSBarry Smith        0,
3077ee4f033dSBarry Smith        0,
3078ee4f033dSBarry Smith        0,
3079d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
3080c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
3081ee4f033dSBarry Smith        0,
3082ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
3083dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3084ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
3085dcf5cc72SBarry Smith #else
3086dcf5cc72SBarry Smith        0,
3087dcf5cc72SBarry Smith #endif
3088d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
30893acb8795SBarry Smith        MatFDColoringApply_AIJ,
309097304618SKris Buschelman        0,
309197304618SKris Buschelman        0,
309297304618SKris Buschelman        0,
30936ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
309497304618SKris Buschelman        0,
309597304618SKris Buschelman        0,
309697304618SKris Buschelman        0,
3097bc011b1eSHong Zhang        MatLoad_SeqAIJ,
3098d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
30991cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
31006284ec50SHong Zhang        0,
31016284ec50SHong Zhang        0,
3102bc011b1eSHong Zhang        0,
3103d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
310426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
310526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
3106d439da42SKris Buschelman        MatPtAP_Basic,
31077ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
3108d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
3109bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
3110bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
3111bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
31127ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
3113d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3114609c6c4dSKris Buschelman        0,
3115609c6c4dSKris Buschelman        0,
311687d4246cSBarry Smith        MatConjugate_SeqAIJ,
311787d4246cSBarry Smith        0,
3118d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
311999cafbc1SBarry Smith        MatRealPart_SeqAIJ,
3120f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
3121f5edf698SHong Zhang        0,
31222bebee5dSHong Zhang        0,
3123cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
3124985db425SBarry Smith        0,
31252af78befSBarry Smith        MatGetRowMin_SeqAIJ,
31262af78befSBarry Smith        0,
3127599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
3128d519adbfSMatthew Knepley /*114*/0,
3129599ef60dSHong Zhang        0,
31303c2a7987SHong Zhang        0,
3131fe97e370SBarry Smith        0,
3132fbdbba38SShri Abhyankar        0,
3133fbdbba38SShri Abhyankar /*119*/0,
3134fbdbba38SShri Abhyankar        0,
3135fbdbba38SShri Abhyankar        0,
313682d44351SHong Zhang        0,
3137b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
31380716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
3139bbead8a2SBarry Smith        MatGetColumnNorms_SeqAIJ,
314037868618SMatthew G Knepley        MatInvertBlockDiagonal_SeqAIJ,
314137868618SMatthew G Knepley        0,
314237868618SMatthew G Knepley        0,
314337868618SMatthew G Knepley /*129*/0
31449e29f15eSvictorle };
314517ab2063SBarry Smith 
3146fb2e594dSBarry Smith EXTERN_C_BEGIN
31474a2ae208SSatish Balay #undef __FUNCT__
31484a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
31497087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3150bef8e0ddSBarry Smith {
3151bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
315297f1f81fSBarry Smith   PetscInt   i,nz,n;
3153bef8e0ddSBarry Smith 
3154bef8e0ddSBarry Smith   PetscFunctionBegin;
3155bef8e0ddSBarry Smith 
3156bef8e0ddSBarry Smith   nz = aij->maxnz;
3157d0f46423SBarry Smith   n  = mat->rmap->n;
3158bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3159bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3160bef8e0ddSBarry Smith   }
3161bef8e0ddSBarry Smith   aij->nz = nz;
3162bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3163bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3164bef8e0ddSBarry Smith   }
3165bef8e0ddSBarry Smith 
3166bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3167bef8e0ddSBarry Smith }
3168fb2e594dSBarry Smith EXTERN_C_END
3169bef8e0ddSBarry Smith 
31704a2ae208SSatish Balay #undef __FUNCT__
31714a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3172bef8e0ddSBarry Smith /*@
3173bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3174bef8e0ddSBarry Smith        in the matrix.
3175bef8e0ddSBarry Smith 
3176bef8e0ddSBarry Smith   Input Parameters:
3177bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3178bef8e0ddSBarry Smith -  indices - the column indices
3179bef8e0ddSBarry Smith 
318015091d37SBarry Smith   Level: advanced
318115091d37SBarry Smith 
3182bef8e0ddSBarry Smith   Notes:
3183bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3184bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3185bef8e0ddSBarry Smith   of the MatSetValues() operation.
3186bef8e0ddSBarry Smith 
3187bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3188d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3189bef8e0ddSBarry Smith 
3190bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3191bef8e0ddSBarry Smith 
3192b9617806SBarry Smith     The indices should start with zero, not one.
3193b9617806SBarry Smith 
3194bef8e0ddSBarry Smith @*/
31957087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3196bef8e0ddSBarry Smith {
31974ac538c5SBarry Smith   PetscErrorCode ierr;
3198bef8e0ddSBarry Smith 
3199bef8e0ddSBarry Smith   PetscFunctionBegin;
32000700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
32014482741eSBarry Smith   PetscValidPointer(indices,2);
32024ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
3203bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3204bef8e0ddSBarry Smith }
3205bef8e0ddSBarry Smith 
3206be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3207be6bf707SBarry Smith 
3208fb2e594dSBarry Smith EXTERN_C_BEGIN
32094a2ae208SSatish Balay #undef __FUNCT__
32104a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
32117087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3212be6bf707SBarry Smith {
3213be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
32146849ba73SBarry Smith   PetscErrorCode ierr;
3215d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3216be6bf707SBarry Smith 
3217be6bf707SBarry Smith   PetscFunctionBegin;
3218be6bf707SBarry Smith   if (aij->nonew != 1) {
3219e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3220be6bf707SBarry Smith   }
3221be6bf707SBarry Smith 
3222be6bf707SBarry Smith   /* allocate space for values if not already there */
3223be6bf707SBarry Smith   if (!aij->saved_values) {
322487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
32259518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3226be6bf707SBarry Smith   }
3227be6bf707SBarry Smith 
3228be6bf707SBarry Smith   /* copy values over */
322987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3230be6bf707SBarry Smith   PetscFunctionReturn(0);
3231be6bf707SBarry Smith }
3232fb2e594dSBarry Smith EXTERN_C_END
3233be6bf707SBarry Smith 
32344a2ae208SSatish Balay #undef __FUNCT__
3235b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3236be6bf707SBarry Smith /*@
3237be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3238be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3239be6bf707SBarry Smith        nonlinear portion.
3240be6bf707SBarry Smith 
3241be6bf707SBarry Smith    Collect on Mat
3242be6bf707SBarry Smith 
3243be6bf707SBarry Smith   Input Parameters:
32440e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3245be6bf707SBarry Smith 
324615091d37SBarry Smith   Level: advanced
324715091d37SBarry Smith 
3248be6bf707SBarry Smith   Common Usage, with SNESSolve():
3249be6bf707SBarry Smith $    Create Jacobian matrix
3250be6bf707SBarry Smith $    Set linear terms into matrix
3251be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3252be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3253be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3254512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3255be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3256be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3257be6bf707SBarry Smith $    In your Jacobian routine
3258be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3259be6bf707SBarry Smith $      Set nonlinear terms in matrix
3260be6bf707SBarry Smith 
3261be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3262be6bf707SBarry Smith $    // build linear portion of Jacobian
3263512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3264be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3265be6bf707SBarry Smith $    loop over nonlinear iterations
3266be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3267be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3268be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3269be6bf707SBarry Smith $       Solve linear system with Jacobian
3270be6bf707SBarry Smith $    endloop
3271be6bf707SBarry Smith 
3272be6bf707SBarry Smith   Notes:
3273be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3274512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3275be6bf707SBarry Smith     calling this routine.
3276be6bf707SBarry Smith 
32770c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
32780c468ba9SBarry Smith     and does not allocated additional space.
32790c468ba9SBarry Smith 
3280be6bf707SBarry Smith .seealso: MatRetrieveValues()
3281be6bf707SBarry Smith 
3282be6bf707SBarry Smith @*/
32837087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3284be6bf707SBarry Smith {
32854ac538c5SBarry Smith   PetscErrorCode ierr;
3286be6bf707SBarry Smith 
3287be6bf707SBarry Smith   PetscFunctionBegin;
32880700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3289e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3290e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
32914ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3292be6bf707SBarry Smith   PetscFunctionReturn(0);
3293be6bf707SBarry Smith }
3294be6bf707SBarry Smith 
3295fb2e594dSBarry Smith EXTERN_C_BEGIN
32964a2ae208SSatish Balay #undef __FUNCT__
32974a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
32987087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3299be6bf707SBarry Smith {
3300be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
33016849ba73SBarry Smith   PetscErrorCode ierr;
3302d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3303be6bf707SBarry Smith 
3304be6bf707SBarry Smith   PetscFunctionBegin;
3305be6bf707SBarry Smith   if (aij->nonew != 1) {
3306e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3307be6bf707SBarry Smith   }
3308be6bf707SBarry Smith   if (!aij->saved_values) {
3309e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3310be6bf707SBarry Smith   }
3311be6bf707SBarry Smith   /* copy values over */
331287828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3313be6bf707SBarry Smith   PetscFunctionReturn(0);
3314be6bf707SBarry Smith }
3315fb2e594dSBarry Smith EXTERN_C_END
3316be6bf707SBarry Smith 
33174a2ae208SSatish Balay #undef __FUNCT__
33184a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3319be6bf707SBarry Smith /*@
3320be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3321be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3322be6bf707SBarry Smith        nonlinear portion.
3323be6bf707SBarry Smith 
3324be6bf707SBarry Smith    Collect on Mat
3325be6bf707SBarry Smith 
3326be6bf707SBarry Smith   Input Parameters:
3327be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3328be6bf707SBarry Smith 
332915091d37SBarry Smith   Level: advanced
333015091d37SBarry Smith 
3331be6bf707SBarry Smith .seealso: MatStoreValues()
3332be6bf707SBarry Smith 
3333be6bf707SBarry Smith @*/
33347087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3335be6bf707SBarry Smith {
33364ac538c5SBarry Smith   PetscErrorCode ierr;
3337be6bf707SBarry Smith 
3338be6bf707SBarry Smith   PetscFunctionBegin;
33390700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3340e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3341e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33424ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3343be6bf707SBarry Smith   PetscFunctionReturn(0);
3344be6bf707SBarry Smith }
3345be6bf707SBarry Smith 
3346f83d6046SBarry Smith 
3347be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
33484a2ae208SSatish Balay #undef __FUNCT__
33494a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
335017ab2063SBarry Smith /*@C
3351682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
33520d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
33536e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
335451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
33552bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
335617ab2063SBarry Smith 
3357db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3358db81eaa0SLois Curfman McInnes 
335917ab2063SBarry Smith    Input Parameters:
3360db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
336117ab2063SBarry Smith .  m - number of rows
336217ab2063SBarry Smith .  n - number of columns
336317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
336451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
33652bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
336617ab2063SBarry Smith 
336717ab2063SBarry Smith    Output Parameter:
3368416022c9SBarry Smith .  A - the matrix
336917ab2063SBarry Smith 
3370175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3371ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3372175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3373175b88e8SBarry Smith 
3374b259b22eSLois Curfman McInnes    Notes:
337549a6f317SBarry Smith    If nnz is given then nz is ignored
337649a6f317SBarry Smith 
337717ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
337817ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
33790002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
338044cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
338117ab2063SBarry Smith 
338217ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3383a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
33843d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
33856da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
338617ab2063SBarry Smith 
3387682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
33884fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3389682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
33906c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
33916c7ebb05SLois Curfman McInnes 
33926c7ebb05SLois Curfman McInnes    Options Database Keys:
3393698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
33949db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
339517ab2063SBarry Smith 
3396027ccd11SLois Curfman McInnes    Level: intermediate
3397027ccd11SLois Curfman McInnes 
339836db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
339936db0b34SBarry Smith 
340017ab2063SBarry Smith @*/
34017087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
340217ab2063SBarry Smith {
3403dfbe8321SBarry Smith   PetscErrorCode ierr;
34046945ee14SBarry Smith 
34053a40ed3dSBarry Smith   PetscFunctionBegin;
3406f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3407117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3408c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3409d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3410273d9f13SBarry Smith   PetscFunctionReturn(0);
3411273d9f13SBarry Smith }
3412273d9f13SBarry Smith 
34134a2ae208SSatish Balay #undef __FUNCT__
34144a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3415273d9f13SBarry Smith /*@C
3416273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3417273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3418273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3419273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3420273d9f13SBarry Smith 
3421273d9f13SBarry Smith    Collective on MPI_Comm
3422273d9f13SBarry Smith 
3423273d9f13SBarry Smith    Input Parameters:
3424117016b1SBarry Smith +  B - The matrix-free
3425273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3426273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3427273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3428273d9f13SBarry Smith 
3429273d9f13SBarry Smith    Notes:
343049a6f317SBarry Smith      If nnz is given then nz is ignored
343149a6f317SBarry Smith 
3432273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3433273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3434273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3435273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3436273d9f13SBarry Smith 
3437273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3438273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3439273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3440273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3441273d9f13SBarry Smith 
3442aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3443aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3444aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3445aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3446aa95bbe8SBarry Smith 
3447a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3448a96a251dSBarry Smith    entries or columns indices
3449a96a251dSBarry Smith 
3450273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3451273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3452273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3453273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3454273d9f13SBarry Smith 
3455273d9f13SBarry Smith    Options Database Keys:
3456698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3457698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3458273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3459273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3460273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3461273d9f13SBarry Smith 
3462273d9f13SBarry Smith    Level: intermediate
3463273d9f13SBarry Smith 
3464aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3465273d9f13SBarry Smith 
3466273d9f13SBarry Smith @*/
34677087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3468273d9f13SBarry Smith {
34694ac538c5SBarry Smith   PetscErrorCode ierr;
3470a23d5eceSKris Buschelman 
3471a23d5eceSKris Buschelman   PetscFunctionBegin;
34724ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3473a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3474a23d5eceSKris Buschelman }
3475a23d5eceSKris Buschelman 
3476a23d5eceSKris Buschelman EXTERN_C_BEGIN
3477a23d5eceSKris Buschelman #undef __FUNCT__
3478a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
34797087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3480a23d5eceSKris Buschelman {
3481273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3482ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
34836849ba73SBarry Smith   PetscErrorCode ierr;
348497f1f81fSBarry Smith   PetscInt       i;
3485273d9f13SBarry Smith 
3486273d9f13SBarry Smith   PetscFunctionBegin;
3487d5d45c9bSBarry Smith 
3488a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3489c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3490c461c341SBarry Smith     nz             = 0;
3491c461c341SBarry Smith   }
3492c461c341SBarry Smith 
349326283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
349426283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
349526283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
349626283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3497899cda47SBarry Smith 
3498435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3499e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3500b73539f3SBarry Smith   if (nnz) {
3501d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3502e32f2f54SBarry 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]);
3503e32f2f54SBarry 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);
3504b73539f3SBarry Smith     }
3505b73539f3SBarry Smith   }
3506b73539f3SBarry Smith 
3507273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3508273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3509273d9f13SBarry Smith 
3510ab93d7beSBarry Smith   if (!skipallocation) {
35112ee49352SLisandro Dalcin     if (!b->imax) {
3512d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3513d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
35142ee49352SLisandro Dalcin     }
3515273d9f13SBarry Smith     if (!nnz) {
3516435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3517c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3518d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3519d0f46423SBarry Smith       nz = nz*B->rmap->n;
3520273d9f13SBarry Smith     } else {
3521273d9f13SBarry Smith       nz = 0;
3522d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3523273d9f13SBarry Smith     }
3524ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3525d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3526ab93d7beSBarry Smith 
3527273d9f13SBarry Smith     /* allocate the matrix space */
35282ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3529d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3530d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3531bfeeae90SHong Zhang     b->i[0] = 0;
3532d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
35335da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
35345da197adSKris Buschelman     }
3535273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3536e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3537e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3538c461c341SBarry Smith   } else {
3539e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3540e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3541c461c341SBarry Smith   }
3542273d9f13SBarry Smith 
3543273d9f13SBarry Smith   b->nz                = 0;
3544273d9f13SBarry Smith   b->maxnz             = nz;
3545273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3546273d9f13SBarry Smith   PetscFunctionReturn(0);
3547273d9f13SBarry Smith }
3548a23d5eceSKris Buschelman EXTERN_C_END
3549273d9f13SBarry Smith 
3550a1661176SMatthew Knepley #undef  __FUNCT__
3551a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
355258d36128SBarry Smith /*@
3553a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3554a1661176SMatthew Knepley 
3555a1661176SMatthew Knepley    Input Parameters:
3556a1661176SMatthew Knepley +  B - the matrix
3557a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3558a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3559a1661176SMatthew Knepley -  v - optional values in the matrix
3560a1661176SMatthew Knepley 
3561a1661176SMatthew Knepley    Level: developer
3562a1661176SMatthew Knepley 
356358d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
356458d36128SBarry Smith 
3565a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3566a1661176SMatthew Knepley 
3567a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3568a1661176SMatthew Knepley @*/
3569a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3570a1661176SMatthew Knepley {
3571a1661176SMatthew Knepley   PetscErrorCode ierr;
3572a1661176SMatthew Knepley 
3573a1661176SMatthew Knepley   PetscFunctionBegin;
35740700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35754ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3576a1661176SMatthew Knepley   PetscFunctionReturn(0);
3577a1661176SMatthew Knepley }
3578a1661176SMatthew Knepley 
3579a1661176SMatthew Knepley EXTERN_C_BEGIN
3580a1661176SMatthew Knepley #undef  __FUNCT__
3581a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
35827087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3583a1661176SMatthew Knepley {
3584a1661176SMatthew Knepley   PetscInt       i;
3585a1661176SMatthew Knepley   PetscInt       m,n;
3586a1661176SMatthew Knepley   PetscInt       nz;
3587a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3588a1661176SMatthew Knepley   PetscScalar    *values;
3589a1661176SMatthew Knepley   PetscErrorCode ierr;
3590a1661176SMatthew Knepley 
3591a1661176SMatthew Knepley   PetscFunctionBegin;
3592a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3593a1661176SMatthew Knepley 
359465e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3595a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3596a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3597b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3598a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
359965e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3600a1661176SMatthew Knepley     nnz[i] = nz;
3601a1661176SMatthew Knepley   }
3602a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3603a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3604a1661176SMatthew Knepley 
3605a1661176SMatthew Knepley   if (v) {
3606a1661176SMatthew Knepley     values = (PetscScalar*) v;
3607a1661176SMatthew Knepley   } else {
36080e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3609a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3610a1661176SMatthew Knepley   }
3611a1661176SMatthew Knepley 
3612a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3613b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3614b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3615a1661176SMatthew Knepley   }
3616a1661176SMatthew Knepley 
3617a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3618a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3619a1661176SMatthew Knepley 
3620a1661176SMatthew Knepley   if (!v) {
3621a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3622a1661176SMatthew Knepley   }
3623a1661176SMatthew Knepley   PetscFunctionReturn(0);
3624a1661176SMatthew Knepley }
3625a1661176SMatthew Knepley EXTERN_C_END
3626a1661176SMatthew Knepley 
3627c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3628c6db04a5SJed Brown #include <private/petscaxpy.h>
3629170fe5c8SBarry Smith 
3630170fe5c8SBarry Smith #undef __FUNCT__
3631170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3632170fe5c8SBarry Smith /*
3633170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3634170fe5c8SBarry Smith 
3635170fe5c8SBarry Smith                n                       p                          p
3636170fe5c8SBarry Smith         (              )       (              )         (                  )
3637170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3638170fe5c8SBarry Smith         (              )       (              )         (                  )
3639170fe5c8SBarry Smith 
3640170fe5c8SBarry Smith */
3641170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3642170fe5c8SBarry Smith {
3643170fe5c8SBarry Smith   PetscErrorCode     ierr;
3644170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3645170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3646170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
36471de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3648170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3649170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3650170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3651170fe5c8SBarry Smith 
3652170fe5c8SBarry Smith   PetscFunctionBegin;
3653d0f46423SBarry Smith   m = A->rmap->n;
3654d0f46423SBarry Smith   n = A->cmap->n;
3655d0f46423SBarry Smith   p = B->cmap->n;
3656170fe5c8SBarry Smith   a = sub_a->v;
3657170fe5c8SBarry Smith   b = sub_b->a;
3658170fe5c8SBarry Smith   c = sub_c->v;
3659170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3660170fe5c8SBarry Smith 
3661170fe5c8SBarry Smith   ii  = sub_b->i;
3662170fe5c8SBarry Smith   idx = sub_b->j;
3663170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3664170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3665170fe5c8SBarry Smith     while (q-->0) {
3666170fe5c8SBarry Smith       c_q = c + m*(*idx);
3667170fe5c8SBarry Smith       a_q = a + m*i;
3668be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3669170fe5c8SBarry Smith       idx++;
3670170fe5c8SBarry Smith       b++;
3671170fe5c8SBarry Smith     }
3672170fe5c8SBarry Smith   }
3673170fe5c8SBarry Smith   PetscFunctionReturn(0);
3674170fe5c8SBarry Smith }
3675170fe5c8SBarry Smith 
3676170fe5c8SBarry Smith #undef __FUNCT__
3677170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3678170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3679170fe5c8SBarry Smith {
3680170fe5c8SBarry Smith   PetscErrorCode ierr;
3681d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3682170fe5c8SBarry Smith   Mat            Cmat;
3683170fe5c8SBarry Smith 
3684170fe5c8SBarry Smith   PetscFunctionBegin;
3685e32f2f54SBarry 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);
368639804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3687170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3688170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3689170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3690170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3691170fe5c8SBarry Smith   *C = Cmat;
3692170fe5c8SBarry Smith   PetscFunctionReturn(0);
3693170fe5c8SBarry Smith }
3694170fe5c8SBarry Smith 
3695170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3696170fe5c8SBarry Smith #undef __FUNCT__
3697170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3698170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3699170fe5c8SBarry Smith {
3700170fe5c8SBarry Smith   PetscErrorCode ierr;
3701170fe5c8SBarry Smith 
3702170fe5c8SBarry Smith   PetscFunctionBegin;
3703170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3704170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3705170fe5c8SBarry Smith   }
3706170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3707170fe5c8SBarry Smith   PetscFunctionReturn(0);
3708170fe5c8SBarry Smith }
3709170fe5c8SBarry Smith 
3710170fe5c8SBarry Smith 
37110bad9183SKris Buschelman /*MC
3712fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
37130bad9183SKris Buschelman    based on compressed sparse row format.
37140bad9183SKris Buschelman 
37150bad9183SKris Buschelman    Options Database Keys:
37160bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
37170bad9183SKris Buschelman 
37180bad9183SKris Buschelman   Level: beginner
37190bad9183SKris Buschelman 
3720f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
37210bad9183SKris Buschelman M*/
37220bad9183SKris Buschelman 
3723a6175056SHong Zhang EXTERN_C_BEGIN
3724b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3725b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3726b5e56a35SBarry Smith #endif
3727ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3728af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3729af1023dbSSatish Balay #endif
37307087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
37317087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
37327087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
37337087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3734611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
37357087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3736611f576cSBarry Smith #endif
3737611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
37387087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3739611f576cSBarry Smith #endif
3740f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3741f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3742f3c0ef26SHong Zhang #endif
3743611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
37447087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3745611f576cSBarry Smith #endif
3746eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
37477087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3748eb3b5408SSatish Balay #endif
3749586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
37507087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3751586621ddSJed Brown #endif
3752719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
37537087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3754719d5645SBarry Smith #endif
3755b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
37567087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
37577087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
37587087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3759b3866ffcSBarry Smith #endif
376017667f90SBarry Smith EXTERN_C_END
376117667f90SBarry Smith 
376217667f90SBarry Smith EXTERN_C_BEGIN
37634a2ae208SSatish Balay #undef __FUNCT__
37644a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
37657087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3766273d9f13SBarry Smith {
3767273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3768dfbe8321SBarry Smith   PetscErrorCode ierr;
376938baddfdSBarry Smith   PetscMPIInt    size;
3770273d9f13SBarry Smith 
3771273d9f13SBarry Smith   PetscFunctionBegin;
37727adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3773e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3774273d9f13SBarry Smith 
377538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3776b0a32e0cSBarry Smith   B->data             = (void*)b;
3777549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3778416022c9SBarry Smith   b->row              = 0;
3779416022c9SBarry Smith   b->col              = 0;
378082bf6240SBarry Smith   b->icol             = 0;
3781b810aeb4SBarry Smith   b->reallocs         = 0;
378236db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3783f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3784416022c9SBarry Smith   b->nonew             = 0;
3785416022c9SBarry Smith   b->diag              = 0;
3786416022c9SBarry Smith   b->solve_work        = 0;
37872a1b7f2aSHong Zhang   B->spptr             = 0;
3788be6bf707SBarry Smith   b->saved_values      = 0;
3789d7f994e1SBarry Smith   b->idiag             = 0;
379071f1c65dSBarry Smith   b->mdiag             = 0;
379171f1c65dSBarry Smith   b->ssor_work         = 0;
379271f1c65dSBarry Smith   b->omega             = 1.0;
379371f1c65dSBarry Smith   b->fshift            = 0.0;
379471f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3795bbead8a2SBarry Smith   b->ibdiagvalid       = PETSC_FALSE;
3796a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3797a30b2313SHong Zhang   b->xtoy              = 0;
3798a30b2313SHong Zhang   b->XtoY              = 0;
379988e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
380017ab2063SBarry Smith 
380135d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3802b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3803700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3804b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3805b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3806b3866ffcSBarry Smith #endif
3807b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3808700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3809b5e56a35SBarry Smith #endif
3810ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3811700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3812719d5645SBarry Smith #endif
3813611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3814700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3815611f576cSBarry Smith #endif
3816f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3817700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3818f3c0ef26SHong Zhang #endif
3819611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3820700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3821611f576cSBarry Smith #endif
3822611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3823700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3824611f576cSBarry Smith #endif
3825eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3826700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3827eb3b5408SSatish Balay #endif
3828586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3829700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3830586621ddSJed Brown #endif
3831719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3832700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3833719d5645SBarry Smith #endif
3834700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3835700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3836700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3837700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3838700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3839700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3840700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3841700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3842700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3843700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3844700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3845700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3846700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3847700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3848700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3849700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3850700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3851700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
38524108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
385317667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
38543a40ed3dSBarry Smith   PetscFunctionReturn(0);
385517ab2063SBarry Smith }
3856273d9f13SBarry Smith EXTERN_C_END
385717ab2063SBarry Smith 
3858ff34cdc8SBarry Smith #if defined(PETSC_HAVE_PTHREADCLASSES)
385951d315f7SKerry Stevens EXTERN_C_BEGIN
386051d315f7SKerry Stevens #undef __FUNCT__
38617d6a0e61SBarry Smith #define __FUNCT__ "MatCreate_SeqAIJPThread"
38627d6a0e61SBarry Smith PetscErrorCode  MatCreate_SeqAIJPThread(Mat B)
386351d315f7SKerry Stevens {
386451d315f7SKerry Stevens   PetscErrorCode ierr;
386551d315f7SKerry Stevens 
386651d315f7SKerry Stevens   PetscFunctionBegin;
386751d315f7SKerry Stevens   ierr = MatCreate_SeqAIJ(B);
386851d315f7SKerry Stevens   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
38697d6a0e61SBarry Smith   B->ops->mult = MatMult_SeqAIJPThread;
38707d6a0e61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJPTHREAD);CHKERRQ(ierr);
387151d315f7SKerry Stevens   PetscFunctionReturn(0);
387251d315f7SKerry Stevens }
387351d315f7SKerry Stevens EXTERN_C_END
3874ba61063dSBarry Smith #endif
387551d315f7SKerry Stevens 
38764a2ae208SSatish Balay #undef __FUNCT__
3877b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3878b24902e0SBarry Smith /*
3879b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3880b24902e0SBarry Smith */
3881ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
388217ab2063SBarry Smith {
3883416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
38846849ba73SBarry Smith   PetscErrorCode ierr;
3885d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
388617ab2063SBarry Smith 
38873a40ed3dSBarry Smith   PetscFunctionBegin;
3888273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3889273d9f13SBarry Smith 
3890d5f3da31SBarry Smith   C->factortype     = A->factortype;
3891416022c9SBarry Smith   c->row            = 0;
3892416022c9SBarry Smith   c->col            = 0;
389382bf6240SBarry Smith   c->icol           = 0;
38946ad4291fSHong Zhang   c->reallocs       = 0;
389517ab2063SBarry Smith 
38966ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
389717ab2063SBarry Smith 
3898aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
3899aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
3900eec197d1SBarry Smith 
390133b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
39029518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
390317ab2063SBarry Smith   for (i=0; i<m; i++) {
3904416022c9SBarry Smith     c->imax[i] = a->imax[i];
3905416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
390617ab2063SBarry Smith   }
390717ab2063SBarry Smith 
390817ab2063SBarry Smith   /* allocate the matrix space */
3909f77e22a1SHong Zhang   if (mallocmatspace){
3910a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
39119518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3912f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
391397f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
391417ab2063SBarry Smith     if (m > 0) {
391597f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3916be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3917bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3918be6bf707SBarry Smith       } else {
3919bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
392017ab2063SBarry Smith       }
392108480c60SBarry Smith     }
3922f77e22a1SHong Zhang   }
392317ab2063SBarry Smith 
39246ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3925416022c9SBarry Smith   c->roworiented       = a->roworiented;
3926416022c9SBarry Smith   c->nonew             = a->nonew;
3927416022c9SBarry Smith   if (a->diag) {
392897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
392952e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
393017ab2063SBarry Smith     for (i=0; i<m; i++) {
3931416022c9SBarry Smith       c->diag[i] = a->diag[i];
393217ab2063SBarry Smith     }
39333a40ed3dSBarry Smith   } else c->diag           = 0;
39346ad4291fSHong Zhang   c->solve_work            = 0;
39356ad4291fSHong Zhang   c->saved_values          = 0;
39366ad4291fSHong Zhang   c->idiag                 = 0;
393771f1c65dSBarry Smith   c->ssor_work             = 0;
3938a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3939e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3940e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
39416ad4291fSHong Zhang   c->xtoy                  = 0;
39426ad4291fSHong Zhang   c->XtoY                  = 0;
39436ad4291fSHong Zhang 
3944416022c9SBarry Smith   c->nz                 = a->nz;
39458ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3946273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3947754ec7b1SSatish Balay 
39486ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
39496ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3950cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3951cd6b891eSBarry Smith   if (a->compressedrow.use){
39526ad4291fSHong Zhang     i = a->compressedrow.nrows;
39530e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
39546ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
39556ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
395627ea64f8SHong Zhang   } else {
395727ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
395827ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
395927ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
39606ad4291fSHong Zhang   }
396188e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
39624108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
39634846f1f5SKris Buschelman 
39647adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
39653a40ed3dSBarry Smith   PetscFunctionReturn(0);
396617ab2063SBarry Smith }
396717ab2063SBarry Smith 
39684a2ae208SSatish Balay #undef __FUNCT__
3969b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3970b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3971b24902e0SBarry Smith {
3972b24902e0SBarry Smith   PetscErrorCode ierr;
3973b24902e0SBarry Smith 
3974b24902e0SBarry Smith   PetscFunctionBegin;
3975b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
39764b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3977b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3978f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3979b24902e0SBarry Smith   PetscFunctionReturn(0);
3980b24902e0SBarry Smith }
3981b24902e0SBarry Smith 
3982b24902e0SBarry Smith #undef __FUNCT__
39834a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3984112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3985fbdbba38SShri Abhyankar {
3986fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3987fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3988fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3989fbdbba38SShri Abhyankar   int            fd;
3990fbdbba38SShri Abhyankar   PetscMPIInt    size;
3991fbdbba38SShri Abhyankar   MPI_Comm       comm;
3992bbead8a2SBarry Smith   PetscInt       bs = 1;
3993fbdbba38SShri Abhyankar 
3994fbdbba38SShri Abhyankar   PetscFunctionBegin;
3995fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3996fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3997fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3998bbead8a2SBarry Smith 
3999bbead8a2SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
4000bbead8a2SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
4001bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4002bbead8a2SBarry Smith 
4003fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4004fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4005fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4006fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4007fbdbba38SShri Abhyankar 
4008bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4009fbdbba38SShri Abhyankar 
4010fbdbba38SShri Abhyankar   /* read in row lengths */
4011fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
4012fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4013fbdbba38SShri Abhyankar 
4014fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4015fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4016fbdbba38SShri 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);
4017fbdbba38SShri Abhyankar 
4018fbdbba38SShri Abhyankar   /* set global size if not set already*/
4019f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4020fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4021aabbc4fbSShri Abhyankar   } else {
4022fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4023fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
40244c5b953cSHong Zhang     if (rows < 0 && cols < 0){ /* user might provide local size instead of global size */
40254c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
40264c5b953cSHong Zhang     }
4027f501eaabSShri 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);
4028aabbc4fbSShri Abhyankar   }
4029fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4030fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
4031fbdbba38SShri Abhyankar 
4032fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4033fbdbba38SShri Abhyankar 
4034fbdbba38SShri Abhyankar   /* read in nonzero values */
4035fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4036fbdbba38SShri Abhyankar 
4037fbdbba38SShri Abhyankar   /* set matrix "i" values */
4038fbdbba38SShri Abhyankar   a->i[0] = 0;
4039fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4040fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4041fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4042fbdbba38SShri Abhyankar   }
4043fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4044fbdbba38SShri Abhyankar 
4045fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4046fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4047bbead8a2SBarry Smith   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4048fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4049fbdbba38SShri Abhyankar }
4050fbdbba38SShri Abhyankar 
4051fbdbba38SShri Abhyankar #undef __FUNCT__
4052b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4053ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
40547264ac53SSatish Balay {
40557264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
4056dfbe8321SBarry Smith   PetscErrorCode ierr;
4057eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4058eeffb40dSHong Zhang   PetscInt k;
4059eeffb40dSHong Zhang #endif
40607264ac53SSatish Balay 
40613a40ed3dSBarry Smith   PetscFunctionBegin;
4062bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4063d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4064ca44d042SBarry Smith     *flg = PETSC_FALSE;
4065ca44d042SBarry Smith     PetscFunctionReturn(0);
4066bcd2baecSBarry Smith   }
40677264ac53SSatish Balay 
40687264ac53SSatish Balay   /* if the a->i are the same */
4069d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4070abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
40717264ac53SSatish Balay 
40727264ac53SSatish Balay   /* if a->j are the same */
407397f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4074abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4075bcd2baecSBarry Smith 
4076bcd2baecSBarry Smith   /* if a->a are the same */
4077eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4078eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
4079eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
4080eeffb40dSHong Zhang       *flg = PETSC_FALSE;
40813a40ed3dSBarry Smith       PetscFunctionReturn(0);
4082eeffb40dSHong Zhang     }
4083eeffb40dSHong Zhang   }
4084eeffb40dSHong Zhang #else
4085eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4086eeffb40dSHong Zhang #endif
4087eeffb40dSHong Zhang   PetscFunctionReturn(0);
40887264ac53SSatish Balay }
408936db0b34SBarry Smith 
40904a2ae208SSatish Balay #undef __FUNCT__
40914a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
409205869f15SSatish Balay /*@
409336db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
409436db0b34SBarry Smith               provided by the user.
409536db0b34SBarry Smith 
4096c75a6043SHong Zhang       Collective on MPI_Comm
409736db0b34SBarry Smith 
409836db0b34SBarry Smith    Input Parameters:
409936db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
410036db0b34SBarry Smith .   m - number of rows
410136db0b34SBarry Smith .   n - number of columns
410236db0b34SBarry Smith .   i - row indices
410336db0b34SBarry Smith .   j - column indices
410436db0b34SBarry Smith -   a - matrix values
410536db0b34SBarry Smith 
410636db0b34SBarry Smith    Output Parameter:
410736db0b34SBarry Smith .   mat - the matrix
410836db0b34SBarry Smith 
410936db0b34SBarry Smith    Level: intermediate
411036db0b34SBarry Smith 
411136db0b34SBarry Smith    Notes:
41120551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4113292fb18eSBarry Smith     once the matrix is destroyed and not before
411436db0b34SBarry Smith 
411536db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
411636db0b34SBarry Smith 
4117bfeeae90SHong Zhang        The i and j indices are 0 based
411836db0b34SBarry Smith 
4119a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4120a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4121a4552177SSatish Balay     as shown:
4122a4552177SSatish Balay 
4123a4552177SSatish Balay         1 0 0
4124a4552177SSatish Balay         2 0 3
4125a4552177SSatish Balay         4 5 6
4126a4552177SSatish Balay 
4127a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
41289985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4129a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4130a4552177SSatish Balay 
41319985e31cSBarry Smith 
41322fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
413336db0b34SBarry Smith 
413436db0b34SBarry Smith @*/
41357087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
413636db0b34SBarry Smith {
4137dfbe8321SBarry Smith   PetscErrorCode ierr;
4138cbcfb4deSHong Zhang   PetscInt       ii;
413936db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4140cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4141cbcfb4deSHong Zhang   PetscInt       jj;
4142cbcfb4deSHong Zhang #endif
414336db0b34SBarry Smith 
414436db0b34SBarry Smith   PetscFunctionBegin;
4145a96a251dSBarry Smith   if (i[0]) {
4146e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
414736db0b34SBarry Smith   }
4148f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4149f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4150ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4151ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4152ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4153ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4154ab93d7beSBarry Smith 
415536db0b34SBarry Smith   aij->i = i;
415636db0b34SBarry Smith   aij->j = j;
415736db0b34SBarry Smith   aij->a = a;
415836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
415936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4160e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4161e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
416236db0b34SBarry Smith 
416336db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
416436db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
41652515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4166e32f2f54SBarry 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]);
41679985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4168e32f2f54SBarry 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);
4169e32f2f54SBarry 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);
41709985e31cSBarry Smith     }
417136db0b34SBarry Smith #endif
417236db0b34SBarry Smith   }
41732515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
417436db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4175e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4176e32f2f54SBarry 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]);
417736db0b34SBarry Smith   }
417836db0b34SBarry Smith #endif
417936db0b34SBarry Smith 
4180b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4181b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
418236db0b34SBarry Smith   PetscFunctionReturn(0);
418336db0b34SBarry Smith }
418436db0b34SBarry Smith 
4185cc8ba8e1SBarry Smith #undef __FUNCT__
4186ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4187dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4188cc8ba8e1SBarry Smith {
4189dfbe8321SBarry Smith   PetscErrorCode ierr;
4190cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
419136db0b34SBarry Smith 
4192cc8ba8e1SBarry Smith   PetscFunctionBegin;
41938ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4194cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4195cc8ba8e1SBarry Smith     a->coloring = coloring;
419612c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
419797f1f81fSBarry Smith     PetscInt             i,*larray;
419812c595b3SBarry Smith     ISColoring      ocoloring;
419908b6dcc0SBarry Smith     ISColoringValue *colors;
420012c595b3SBarry Smith 
420112c595b3SBarry Smith     /* set coloring for diagonal portion */
42020e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4203d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
420412c595b3SBarry Smith       larray[i] = i;
420512c595b3SBarry Smith     }
4206992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
42070e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4208d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
420912c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
421012c595b3SBarry Smith     }
421112c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4212d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
421312c595b3SBarry Smith     a->coloring = ocoloring;
421412c595b3SBarry Smith   }
4215cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4216cc8ba8e1SBarry Smith }
4217cc8ba8e1SBarry Smith 
4218dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
4219ee4f033dSBarry Smith EXTERN_C_BEGIN
4220c6db04a5SJed Brown #include <adic/ad_utils.h>
4221ee4f033dSBarry Smith EXTERN_C_END
4222cc8ba8e1SBarry Smith 
4223cc8ba8e1SBarry Smith #undef __FUNCT__
4224ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
4225dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
4226cc8ba8e1SBarry Smith {
4227cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4228d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
42294440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
423008b6dcc0SBarry Smith   ISColoringValue *color;
4231cc8ba8e1SBarry Smith 
4232cc8ba8e1SBarry Smith   PetscFunctionBegin;
4233e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
42344440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
4235cc8ba8e1SBarry Smith   color = a->coloring->colors;
4236cc8ba8e1SBarry Smith   /* loop over rows */
4237cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
4238cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
4239cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
4240cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
4241cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
4242cc8ba8e1SBarry Smith     }
42434440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
4244ee4f033dSBarry Smith   }
4245ee4f033dSBarry Smith   PetscFunctionReturn(0);
4246ee4f033dSBarry Smith }
4247ee4f033dSBarry Smith #endif
4248ee4f033dSBarry Smith 
4249ee4f033dSBarry Smith #undef __FUNCT__
4250ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
425197f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4252ee4f033dSBarry Smith {
4253ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4254d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
425554f21887SBarry Smith   MatScalar       *v = a->a;
425654f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
425708b6dcc0SBarry Smith   ISColoringValue *color;
4258ee4f033dSBarry Smith 
4259ee4f033dSBarry Smith   PetscFunctionBegin;
4260e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4261ee4f033dSBarry Smith   color = a->coloring->colors;
4262ee4f033dSBarry Smith   /* loop over rows */
4263ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4264ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4265ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
4266ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
4267ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
4268ee4f033dSBarry Smith     }
4269ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4270cc8ba8e1SBarry Smith   }
4271cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4272cc8ba8e1SBarry Smith }
427336db0b34SBarry Smith 
427481824310SBarry Smith /*
427581824310SBarry Smith     Special version for direct calls from Fortran
427681824310SBarry Smith */
4277c6db04a5SJed Brown #include <private/fortranimpl.h>
427881824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
427981824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
428081824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
428181824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
428281824310SBarry Smith #endif
428381824310SBarry Smith 
428481824310SBarry Smith /* Change these macros so can be used in void function */
428581824310SBarry Smith #undef CHKERRQ
42867adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
428781824310SBarry Smith #undef SETERRQ2
4288e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
428981824310SBarry Smith 
429081824310SBarry Smith EXTERN_C_BEGIN
429181824310SBarry Smith #undef __FUNCT__
429281824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
42931f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
429481824310SBarry Smith {
429581824310SBarry Smith   Mat            A = *AA;
429681824310SBarry Smith   PetscInt       m = *mm, n = *nn;
429781824310SBarry Smith   InsertMode     is = *isis;
429881824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
429981824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
430081824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
430181824310SBarry Smith   PetscErrorCode ierr;
430281824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
430354f21887SBarry Smith   MatScalar      *ap,value,*aa;
4304ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4305ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
430681824310SBarry Smith 
430781824310SBarry Smith   PetscFunctionBegin;
4308d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
430981824310SBarry Smith   imax = a->imax;
431081824310SBarry Smith   ai = a->i;
431181824310SBarry Smith   ailen = a->ilen;
431281824310SBarry Smith   aj = a->j;
431381824310SBarry Smith   aa = a->a;
431481824310SBarry Smith 
431581824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
431681824310SBarry Smith     row  = im[k];
431781824310SBarry Smith     if (row < 0) continue;
431881824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4319d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
432081824310SBarry Smith #endif
432181824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
432281824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
432381824310SBarry Smith     low  = 0;
432481824310SBarry Smith     high = nrow;
432581824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
432681824310SBarry Smith       if (in[l] < 0) continue;
432781824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4328d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
432981824310SBarry Smith #endif
433081824310SBarry Smith       col = in[l];
433181824310SBarry Smith       if (roworiented) {
433281824310SBarry Smith         value = v[l + k*n];
433381824310SBarry Smith       } else {
433481824310SBarry Smith         value = v[k + l*m];
433581824310SBarry Smith       }
433681824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
433781824310SBarry Smith 
433881824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
433981824310SBarry Smith       lastcol = col;
434081824310SBarry Smith       while (high-low > 5) {
434181824310SBarry Smith         t = (low+high)/2;
434281824310SBarry Smith         if (rp[t] > col) high = t;
434381824310SBarry Smith         else             low  = t;
434481824310SBarry Smith       }
434581824310SBarry Smith       for (i=low; i<high; i++) {
434681824310SBarry Smith         if (rp[i] > col) break;
434781824310SBarry Smith         if (rp[i] == col) {
434881824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
434981824310SBarry Smith           else                  ap[i] = value;
435081824310SBarry Smith           goto noinsert;
435181824310SBarry Smith         }
435281824310SBarry Smith       }
435381824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
435481824310SBarry Smith       if (nonew == 1) goto noinsert;
43557adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4356fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
435781824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
435881824310SBarry Smith       /* shift up all the later entries in this row */
435981824310SBarry Smith       for (ii=N; ii>=i; ii--) {
436081824310SBarry Smith         rp[ii+1] = rp[ii];
436181824310SBarry Smith         ap[ii+1] = ap[ii];
436281824310SBarry Smith       }
436381824310SBarry Smith       rp[i] = col;
436481824310SBarry Smith       ap[i] = value;
436581824310SBarry Smith       noinsert:;
436681824310SBarry Smith       low = i + 1;
436781824310SBarry Smith     }
436881824310SBarry Smith     ailen[row] = nrow;
436981824310SBarry Smith   }
437081824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
437181824310SBarry Smith   PetscFunctionReturnVoid();
437281824310SBarry Smith }
437381824310SBarry Smith EXTERN_C_END
437462298a1eSBarry Smith 
4375