xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 6ce1633cb736e3bd2a11b0bc146401a5bd4cb96c)
1992c00aaSSatish Balay #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3b377110cSBarry Smith 
4d5d45c9bSBarry Smith /*
53369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
6d5d45c9bSBarry Smith   matrix storage format.
7d5d45c9bSBarry Smith */
83369ce9aSBarry Smith 
97c4f633dSBarry Smith 
107c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
11f3da1532SBarry Smith #include "petscblaslapack.h"
120a835dfdSSatish Balay #include "petscbt.h"
1317ab2063SBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
15*6ce1633cSBarry Smith #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
16*6ce1633cSBarry Smith PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
17*6ce1633cSBarry Smith {
18*6ce1633cSBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
19*6ce1633cSBarry Smith   const MatScalar   *aa = a->a;
20*6ce1633cSBarry Smith   PetscInt          i,m=A->rmap->n,cnt = 0;
21*6ce1633cSBarry Smith   const PetscInt    *jj = a->j,*diag;
22*6ce1633cSBarry Smith   PetscInt          *rows;
23*6ce1633cSBarry Smith   PetscErrorCode    ierr;
24*6ce1633cSBarry Smith 
25*6ce1633cSBarry Smith   PetscFunctionBegin;
26*6ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
27*6ce1633cSBarry Smith   diag = a->diag;
28*6ce1633cSBarry Smith   for (i=0; i<m; i++) {
29*6ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
30*6ce1633cSBarry Smith       cnt++;
31*6ce1633cSBarry Smith     }
32*6ce1633cSBarry Smith   }
33*6ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
34*6ce1633cSBarry Smith   cnt  = 0;
35*6ce1633cSBarry Smith   for (i=0; i<m; i++) {
36*6ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
37*6ce1633cSBarry Smith       rows[cnt++] = i;
38*6ce1633cSBarry Smith     }
39*6ce1633cSBarry Smith   }
40*6ce1633cSBarry Smith   ierr = ISCreateGeneral(((PetscObject)A)->comm,cnt,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
41*6ce1633cSBarry Smith   PetscFunctionReturn(0);
42*6ce1633cSBarry Smith }
43*6ce1633cSBarry Smith 
44*6ce1633cSBarry Smith #undef __FUNCT__
45b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
46b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
47b3a44c85SBarry Smith {
48b3a44c85SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
49b3a44c85SBarry Smith   const MatScalar   *aa;
50b3a44c85SBarry Smith   PetscInt          m=A->rmap->n,cnt = 0;
51b3a44c85SBarry Smith   const PetscInt    *ii;
52b3a44c85SBarry Smith   PetscInt          n,i,j,*rows;
53b3a44c85SBarry Smith   PetscErrorCode    ierr;
54b3a44c85SBarry Smith 
55b3a44c85SBarry Smith   PetscFunctionBegin;
56b3a44c85SBarry Smith   *keptrows = 0;
57b3a44c85SBarry Smith   ii        = a->i;
58b3a44c85SBarry Smith   for (i=0; i<m; i++) {
59b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
60b3a44c85SBarry Smith     if (!n) {
61b3a44c85SBarry Smith       cnt++;
62b3a44c85SBarry Smith       goto ok1;
63b3a44c85SBarry Smith     }
64b3a44c85SBarry Smith     aa  = a->a + ii[i];
65b3a44c85SBarry Smith     for (j=0; j<n; j++) {
66b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
67b3a44c85SBarry Smith     }
68b3a44c85SBarry Smith     cnt++;
69b3a44c85SBarry Smith     ok1:;
70b3a44c85SBarry Smith   }
71b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
72b3a44c85SBarry Smith   ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
73b3a44c85SBarry Smith   cnt  = 0;
74b3a44c85SBarry Smith   for (i=0; i<m; i++) {
75b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
76b3a44c85SBarry Smith     if (!n) continue;
77b3a44c85SBarry Smith     aa  = a->a + ii[i];
78b3a44c85SBarry Smith     for (j=0; j<n; j++) {
79b3a44c85SBarry Smith       if (aa[j] != 0.0) {
80b3a44c85SBarry Smith         rows[cnt++] = i;
81b3a44c85SBarry Smith         break;
82b3a44c85SBarry Smith       }
83b3a44c85SBarry Smith     }
84b3a44c85SBarry Smith   }
85b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
86b3a44c85SBarry Smith   PetscFunctionReturn(0);
87b3a44c85SBarry Smith }
88b3a44c85SBarry Smith 
89b3a44c85SBarry Smith #undef __FUNCT__
9079299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
917087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
9279299369SBarry Smith {
9379299369SBarry Smith   PetscErrorCode ierr;
9479299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
95d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
9654f21887SBarry Smith   MatScalar      *aa = aij->a;
9754f21887SBarry Smith   PetscScalar    *v;
98ace3abfcSBarry Smith   PetscBool      missing;
9979299369SBarry Smith 
10079299369SBarry Smith   PetscFunctionBegin;
10109f38230SBarry Smith   if (Y->assembled) {
10209f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
10309f38230SBarry Smith     if (!missing) {
10479299369SBarry Smith       diag = aij->diag;
10579299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
10679299369SBarry Smith       if (is == INSERT_VALUES) {
10779299369SBarry Smith 	for (i=0; i<m; i++) {
10879299369SBarry Smith 	  aa[diag[i]] = v[i];
10979299369SBarry Smith 	}
11079299369SBarry Smith       } else {
11179299369SBarry Smith 	for (i=0; i<m; i++) {
11279299369SBarry Smith 	  aa[diag[i]] += v[i];
11379299369SBarry Smith 	}
11479299369SBarry Smith       }
11579299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
11679299369SBarry Smith       PetscFunctionReturn(0);
11779299369SBarry Smith     }
11809f38230SBarry Smith   }
11909f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
12009f38230SBarry Smith   PetscFunctionReturn(0);
12109f38230SBarry Smith }
12279299369SBarry Smith 
12379299369SBarry Smith #undef __FUNCT__
1244a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
125ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
12617ab2063SBarry Smith {
127416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
128dfbe8321SBarry Smith   PetscErrorCode ierr;
12997f1f81fSBarry Smith   PetscInt       i,ishift;
13017ab2063SBarry Smith 
1313a40ed3dSBarry Smith   PetscFunctionBegin;
132d0f46423SBarry Smith   *m     = A->rmap->n;
1333a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
134bfeeae90SHong Zhang   ishift = 0;
13553e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
136d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
137bfeeae90SHong Zhang   } else if (oshift == 1) {
138d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
1393b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
140d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
141d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
142ecc77c7aSBarry Smith     if (ja) {
14397f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
1443b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
145ecc77c7aSBarry Smith     }
1466945ee14SBarry Smith   } else {
147ecc77c7aSBarry Smith     *ia = a->i;
148ecc77c7aSBarry Smith     if (ja) *ja = a->j;
149a2ce50c7SBarry Smith   }
1503a40ed3dSBarry Smith   PetscFunctionReturn(0);
151a2744918SBarry Smith }
152a2744918SBarry Smith 
1534a2ae208SSatish Balay #undef __FUNCT__
1544a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
155ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
1566945ee14SBarry Smith {
157dfbe8321SBarry Smith   PetscErrorCode ierr;
1586945ee14SBarry Smith 
1593a40ed3dSBarry Smith   PetscFunctionBegin;
1603a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
161bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
162606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
163ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
164bcd2baecSBarry Smith   }
1653a40ed3dSBarry Smith   PetscFunctionReturn(0);
16617ab2063SBarry Smith }
16717ab2063SBarry Smith 
1684a2ae208SSatish Balay #undef __FUNCT__
1694a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
170ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
1713b2fbd54SBarry Smith {
1723b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
173dfbe8321SBarry Smith   PetscErrorCode ierr;
174d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
17597f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1763b2fbd54SBarry Smith 
1773a40ed3dSBarry Smith   PetscFunctionBegin;
178899cda47SBarry Smith   *nn = n;
1793a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1803b2fbd54SBarry Smith   if (symmetric) {
181d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1823b2fbd54SBarry Smith   } else {
18397f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
18497f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
18597f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
18697f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1873b2fbd54SBarry Smith     jj = a->j;
1883b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
189bfeeae90SHong Zhang       collengths[jj[i]]++;
1903b2fbd54SBarry Smith     }
1913b2fbd54SBarry Smith     cia[0] = oshift;
1923b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1933b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1943b2fbd54SBarry Smith     }
19597f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1963b2fbd54SBarry Smith     jj   = a->j;
197a93ec695SBarry Smith     for (row=0; row<m; row++) {
198a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
199a93ec695SBarry Smith       for (i=0; i<mr; i++) {
200bfeeae90SHong Zhang         col = *jj++;
2013b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2023b2fbd54SBarry Smith       }
2033b2fbd54SBarry Smith     }
204606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2053b2fbd54SBarry Smith     *ia = cia; *ja = cja;
2063b2fbd54SBarry Smith   }
2073a40ed3dSBarry Smith   PetscFunctionReturn(0);
2083b2fbd54SBarry Smith }
2093b2fbd54SBarry Smith 
2104a2ae208SSatish Balay #undef __FUNCT__
2114a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
212ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2133b2fbd54SBarry Smith {
214dfbe8321SBarry Smith   PetscErrorCode ierr;
215606d414cSSatish Balay 
2163a40ed3dSBarry Smith   PetscFunctionBegin;
2173a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2183b2fbd54SBarry Smith 
219606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
220606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2213b2fbd54SBarry Smith 
2223a40ed3dSBarry Smith   PetscFunctionReturn(0);
2233b2fbd54SBarry Smith }
2243b2fbd54SBarry Smith 
22587d4246cSBarry Smith #undef __FUNCT__
22687d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
22787d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
22887d4246cSBarry Smith {
22987d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23087d4246cSBarry Smith   PetscInt       *ai = a->i;
23187d4246cSBarry Smith   PetscErrorCode ierr;
23287d4246cSBarry Smith 
23387d4246cSBarry Smith   PetscFunctionBegin;
23487d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
23587d4246cSBarry Smith   PetscFunctionReturn(0);
23687d4246cSBarry Smith }
23787d4246cSBarry Smith 
2384a2ae208SSatish Balay #undef __FUNCT__
2394a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
24097f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
24117ab2063SBarry Smith {
242416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
243e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
24497f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
2456849ba73SBarry Smith   PetscErrorCode ierr;
246e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
24754f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
248ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
249ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
25017ab2063SBarry Smith 
2513a40ed3dSBarry Smith   PetscFunctionBegin;
25271fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
25317ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
254416022c9SBarry Smith     row  = im[k];
2555ef9f2a5SBarry Smith     if (row < 0) continue;
2562515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
257e32f2f54SBarry 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);
2583b2fbd54SBarry Smith #endif
259bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
26017ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
261416022c9SBarry Smith     low  = 0;
262c71e6ed7SBarry Smith     high = nrow;
26317ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
2645ef9f2a5SBarry Smith       if (in[l] < 0) continue;
2652515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
266e32f2f54SBarry 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);
2673b2fbd54SBarry Smith #endif
268bfeeae90SHong Zhang       col = in[l];
26916371a99SBarry Smith       if (v) {
2704b0e389bSBarry Smith 	if (roworiented) {
2715ef9f2a5SBarry Smith 	  value = v[l + k*n];
272bef8e0ddSBarry Smith 	} else {
2734b0e389bSBarry Smith 	  value = v[k + l*m];
2744b0e389bSBarry Smith 	}
27516371a99SBarry Smith       } else {
27675567043SBarry Smith         value = 0.;
27716371a99SBarry Smith       }
278abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
27936db0b34SBarry Smith 
2807cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
281e2ee6c50SBarry Smith       lastcol = col;
282416022c9SBarry Smith       while (high-low > 5) {
283416022c9SBarry Smith         t = (low+high)/2;
284416022c9SBarry Smith         if (rp[t] > col) high = t;
285416022c9SBarry Smith         else             low  = t;
28617ab2063SBarry Smith       }
287416022c9SBarry Smith       for (i=low; i<high; i++) {
28817ab2063SBarry Smith         if (rp[i] > col) break;
28917ab2063SBarry Smith         if (rp[i] == col) {
290416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
29117ab2063SBarry Smith           else                  ap[i] = value;
292e44c0bd4SBarry Smith           low = i + 1;
29317ab2063SBarry Smith           goto noinsert;
29417ab2063SBarry Smith         }
29517ab2063SBarry Smith       }
296abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
297c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
298e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
299fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
300c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
301416022c9SBarry Smith       /* shift up all the later entries in this row */
302416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
30317ab2063SBarry Smith         rp[ii+1] = rp[ii];
30417ab2063SBarry Smith         ap[ii+1] = ap[ii];
30517ab2063SBarry Smith       }
30617ab2063SBarry Smith       rp[i] = col;
30717ab2063SBarry Smith       ap[i] = value;
308416022c9SBarry Smith       low   = i + 1;
309e44c0bd4SBarry Smith       noinsert:;
31017ab2063SBarry Smith     }
31117ab2063SBarry Smith     ailen[row] = nrow;
31217ab2063SBarry Smith   }
31388e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3143a40ed3dSBarry Smith   PetscFunctionReturn(0);
31517ab2063SBarry Smith }
31617ab2063SBarry Smith 
31781824310SBarry Smith 
3184a2ae208SSatish Balay #undef __FUNCT__
3194a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
320a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3217eb43aa7SLois Curfman McInnes {
3227eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
32397f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
32497f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
32554f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
3267eb43aa7SLois Curfman McInnes 
3273a40ed3dSBarry Smith   PetscFunctionBegin;
3287eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3297eb43aa7SLois Curfman McInnes     row  = im[k];
330e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
331e32f2f54SBarry 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);
332bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3337eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3347eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
335e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
336e32f2f54SBarry 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);
337bfeeae90SHong Zhang       col = in[l] ;
3387eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3397eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3407eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3417eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3427eb43aa7SLois Curfman McInnes         else             low  = t;
3437eb43aa7SLois Curfman McInnes       }
3447eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3457eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
3467eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
347b49de8d1SLois Curfman McInnes           *v++ = ap[i];
3487eb43aa7SLois Curfman McInnes           goto finished;
3497eb43aa7SLois Curfman McInnes         }
3507eb43aa7SLois Curfman McInnes       }
35197e567efSBarry Smith       *v++ = 0.0;
3527eb43aa7SLois Curfman McInnes       finished:;
3537eb43aa7SLois Curfman McInnes     }
3547eb43aa7SLois Curfman McInnes   }
3553a40ed3dSBarry Smith   PetscFunctionReturn(0);
3567eb43aa7SLois Curfman McInnes }
3577eb43aa7SLois Curfman McInnes 
35817ab2063SBarry Smith 
3594a2ae208SSatish Balay #undef __FUNCT__
3604a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
361dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
36217ab2063SBarry Smith {
363416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3646849ba73SBarry Smith   PetscErrorCode ierr;
3656f69ff64SBarry Smith   PetscInt       i,*col_lens;
3666f69ff64SBarry Smith   int            fd;
36717ab2063SBarry Smith 
3683a40ed3dSBarry Smith   PetscFunctionBegin;
369b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
370d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
3710700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
372d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
373d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
374416022c9SBarry Smith   col_lens[3] = a->nz;
375416022c9SBarry Smith 
376416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
377d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
378416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
37917ab2063SBarry Smith   }
380d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
381606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
382416022c9SBarry Smith 
383416022c9SBarry Smith   /* store column indices (zero start index) */
3846f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
385416022c9SBarry Smith 
386416022c9SBarry Smith   /* store nonzero values */
3876f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3883a40ed3dSBarry Smith   PetscFunctionReturn(0);
38917ab2063SBarry Smith }
390416022c9SBarry Smith 
39109573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
392cd155464SBarry Smith 
3934a2ae208SSatish Balay #undef __FUNCT__
3944a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
395dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
396416022c9SBarry Smith {
397416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
398dfbe8321SBarry Smith   PetscErrorCode    ierr;
399d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
400e060cb09SBarry Smith   const char        *name;
401f3ef73ceSBarry Smith   PetscViewerFormat format;
40217ab2063SBarry Smith 
4033a40ed3dSBarry Smith   PetscFunctionBegin;
404b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
40571c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
40697f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
407d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
408d00d2cf4SBarry Smith       nofinalvalue = 1;
409d00d2cf4SBarry Smith     }
410d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
411d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
41277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
41377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
414b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
41517ab2063SBarry Smith 
41617ab2063SBarry Smith     for (i=0; i<m; i++) {
417416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
418aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41977431f27SBarry 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);
42017ab2063SBarry Smith #else
42177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
42217ab2063SBarry Smith #endif
42317ab2063SBarry Smith       }
42417ab2063SBarry Smith     }
425d00d2cf4SBarry Smith     if (nofinalvalue) {
426d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
427d00d2cf4SBarry Smith     }
428317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
429fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
430d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
43168369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
432cd155464SBarry Smith      PetscFunctionReturn(0);
433fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
434d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4357566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
43644cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
43777431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
43844cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
439aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44036db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
441a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44236db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
443a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44436db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
445a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4466831982aSBarry Smith         }
44744cd7ae7SLois Curfman McInnes #else
448a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
44944cd7ae7SLois Curfman McInnes #endif
45044cd7ae7SLois Curfman McInnes       }
451b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45244cd7ae7SLois Curfman McInnes     }
453d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
454fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
45597f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
456d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4577566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
45897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
459496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
460496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
461496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
462496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
463aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
46436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
465496be53dSLois Curfman McInnes #else
466496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
467496be53dSLois Curfman McInnes #endif
468496be53dSLois Curfman McInnes         }
469496be53dSLois Curfman McInnes       }
470496be53dSLois Curfman McInnes     }
4712e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
47277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
4732e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
47477431f27SBarry 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);}
47577431f27SBarry 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);}
47677431f27SBarry 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);}
47777431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
47877431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
47977431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
480496be53dSLois Curfman McInnes     }
481b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
482606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
483496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
484496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
48577431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
486496be53dSLois Curfman McInnes       }
487b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
488496be53dSLois Curfman McInnes     }
489b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
490496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
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) {
495b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4966831982aSBarry Smith           }
497496be53dSLois Curfman McInnes #else
498b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
499496be53dSLois Curfman McInnes #endif
500496be53dSLois Curfman McInnes         }
501496be53dSLois Curfman McInnes       }
502b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
503496be53dSLois Curfman McInnes     }
504d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
505fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
50697f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
50787828ca2SBarry Smith     PetscScalar value;
50802594712SBarry Smith 
509d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5107566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
51102594712SBarry Smith     for (i=0; i<m; i++) {
51202594712SBarry Smith       jcnt = 0;
513d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
514e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
51502594712SBarry Smith           value = a->a[cnt++];
516e24b481bSBarry Smith           jcnt++;
51702594712SBarry Smith         } else {
51802594712SBarry Smith           value = 0.0;
51902594712SBarry Smith         }
520aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
521b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
52202594712SBarry Smith #else
523b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
52402594712SBarry Smith #endif
52502594712SBarry Smith       }
526b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
52702594712SBarry Smith     }
528d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5293c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
530d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5317566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5323c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5333c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5343c215bfdSMatthew Knepley #else
5353c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5363c215bfdSMatthew Knepley #endif
537d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5383c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5393c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5403c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5413c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
5423c215bfdSMatthew 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);
5433c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
5443c215bfdSMatthew 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);
5453c215bfdSMatthew Knepley         } else {
5463c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5473c215bfdSMatthew Knepley         }
5483c215bfdSMatthew Knepley #else
5493c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
5503c215bfdSMatthew Knepley #endif
5513c215bfdSMatthew Knepley       }
5523c215bfdSMatthew Knepley     }
553d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5543a40ed3dSBarry Smith   } else {
555d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5567566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
557d5f3da31SBarry Smith     if (A->factortype){
55816cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
55916cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
56016cd7e1dSShri Abhyankar         /* L part */
56116cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
56216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
56316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
56416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
56516cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
56616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
56716cd7e1dSShri Abhyankar           } else {
56816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
56916cd7e1dSShri Abhyankar           }
57016cd7e1dSShri Abhyankar #else
57116cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
57216cd7e1dSShri Abhyankar #endif
57316cd7e1dSShri Abhyankar         }
57416cd7e1dSShri Abhyankar 	/* diagonal */
57516cd7e1dSShri Abhyankar 	j = a->diag[i];
57616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
57716cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
57816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
57916cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
58016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
58116cd7e1dSShri Abhyankar           } else {
58216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
58316cd7e1dSShri Abhyankar           }
58416cd7e1dSShri Abhyankar #else
58516cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
58616cd7e1dSShri Abhyankar #endif
58716cd7e1dSShri Abhyankar 
58816cd7e1dSShri Abhyankar 	/* U part */
58916cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
59016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
59116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
59216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
59316cd7e1dSShri Abhyankar           } else 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 {
59616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
59716cd7e1dSShri Abhyankar           }
59816cd7e1dSShri Abhyankar #else
59916cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
60016cd7e1dSShri Abhyankar #endif
60116cd7e1dSShri Abhyankar }
60216cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
60316cd7e1dSShri Abhyankar         }
60416cd7e1dSShri Abhyankar     } else {
60517ab2063SBarry Smith       for (i=0; i<m; i++) {
60677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
607416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
608aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
60936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
610a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
61136db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
612a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6133a40ed3dSBarry Smith           } else {
614a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
61517ab2063SBarry Smith           }
61617ab2063SBarry Smith #else
617a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
61817ab2063SBarry Smith #endif
61917ab2063SBarry Smith         }
620b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
62117ab2063SBarry Smith       }
62216cd7e1dSShri Abhyankar     }
623d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
62417ab2063SBarry Smith   }
625b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6263a40ed3dSBarry Smith   PetscFunctionReturn(0);
627416022c9SBarry Smith }
628416022c9SBarry Smith 
6294a2ae208SSatish Balay #undef __FUNCT__
6304a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
631dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
632416022c9SBarry Smith {
633480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
634416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
635dfbe8321SBarry Smith   PetscErrorCode    ierr;
636d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
63736db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
638b0a32e0cSBarry Smith   PetscViewer       viewer;
639f3ef73ceSBarry Smith   PetscViewerFormat format;
640cddf8d76SBarry Smith 
6413a40ed3dSBarry Smith   PetscFunctionBegin;
642480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
643b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
64419bcc07fSBarry Smith 
645b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
646416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
6470513a670SBarry Smith 
648fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
6490513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
650b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
651416022c9SBarry Smith     for (i=0; i<m; i++) {
652cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
653bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
654bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
655aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
65636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
657cddf8d76SBarry Smith #else
658cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
659cddf8d76SBarry Smith #endif
660b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
661cddf8d76SBarry Smith       }
662cddf8d76SBarry Smith     }
663b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
664cddf8d76SBarry Smith     for (i=0; i<m; i++) {
665cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
666bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
667bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
668cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
669b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
670cddf8d76SBarry Smith       }
671cddf8d76SBarry Smith     }
672b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
673cddf8d76SBarry Smith     for (i=0; i<m; i++) {
674cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
675bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
676bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
677aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
67836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
679cddf8d76SBarry Smith #else
680cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
681cddf8d76SBarry Smith #endif
682b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
683416022c9SBarry Smith       }
684416022c9SBarry Smith     }
6850513a670SBarry Smith   } else {
6860513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
6870513a670SBarry Smith     /* first determine max of all nonzero values */
68897f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
689b0a32e0cSBarry Smith     PetscDraw   popup;
69036db0b34SBarry Smith     PetscReal scale;
6910513a670SBarry Smith 
6920513a670SBarry Smith     for (i=0; i<nz; i++) {
6930513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
6940513a670SBarry Smith     }
695b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
696b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
697b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
6980513a670SBarry Smith     count = 0;
6990513a670SBarry Smith     for (i=0; i<m; i++) {
7000513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
701bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
702bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
70397f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
704b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7050513a670SBarry Smith         count++;
7060513a670SBarry Smith       }
7070513a670SBarry Smith     }
7080513a670SBarry Smith   }
709480ef9eaSBarry Smith   PetscFunctionReturn(0);
710480ef9eaSBarry Smith }
711cddf8d76SBarry Smith 
7124a2ae208SSatish Balay #undef __FUNCT__
7134a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
714dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
715480ef9eaSBarry Smith {
716dfbe8321SBarry Smith   PetscErrorCode ierr;
717b0a32e0cSBarry Smith   PetscDraw      draw;
71836db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
719ace3abfcSBarry Smith   PetscBool      isnull;
720480ef9eaSBarry Smith 
721480ef9eaSBarry Smith   PetscFunctionBegin;
722b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
723b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
724480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
725480ef9eaSBarry Smith 
726480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
727d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
728480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
729b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
730b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
731480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7323a40ed3dSBarry Smith   PetscFunctionReturn(0);
733416022c9SBarry Smith }
734416022c9SBarry Smith 
7354a2ae208SSatish Balay #undef __FUNCT__
7364a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
737dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
738416022c9SBarry Smith {
739dfbe8321SBarry Smith   PetscErrorCode ierr;
740ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
741416022c9SBarry Smith 
7423a40ed3dSBarry Smith   PetscFunctionBegin;
7432692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
7442692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
7452692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
746c45a1595SBarry Smith   if (iascii) {
7473a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
7480f5bd95cSBarry Smith   } else if (isbinary) {
7493a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
7500f5bd95cSBarry Smith   } else if (isdraw) {
7513a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
7525cd90555SBarry Smith   } else {
753e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
75417ab2063SBarry Smith   }
7554108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
7563a40ed3dSBarry Smith   PetscFunctionReturn(0);
75717ab2063SBarry Smith }
75819bcc07fSBarry Smith 
7594a2ae208SSatish Balay #undef __FUNCT__
7604a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
761dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
76217ab2063SBarry Smith {
763416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7646849ba73SBarry Smith   PetscErrorCode ierr;
76597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
766d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
76754f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
7683447b6efSHong Zhang   PetscReal      ratio=0.6;
76917ab2063SBarry Smith 
7703a40ed3dSBarry Smith   PetscFunctionBegin;
7713a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
77217ab2063SBarry Smith 
77343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
77417ab2063SBarry Smith   for (i=1; i<m; i++) {
775416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
77617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
77794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
77817ab2063SBarry Smith     if (fshift) {
779bfeeae90SHong Zhang       ip = aj + ai[i] ;
780bfeeae90SHong Zhang       ap = aa + ai[i] ;
78117ab2063SBarry Smith       N  = ailen[i];
78217ab2063SBarry Smith       for (j=0; j<N; j++) {
78317ab2063SBarry Smith         ip[j-fshift] = ip[j];
78417ab2063SBarry Smith         ap[j-fshift] = ap[j];
78517ab2063SBarry Smith       }
78617ab2063SBarry Smith     }
78717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
78817ab2063SBarry Smith   }
78917ab2063SBarry Smith   if (m) {
79017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
79117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
79217ab2063SBarry Smith   }
79317ab2063SBarry Smith   /* reset ilen and imax for each row */
79417ab2063SBarry Smith   for (i=0; i<m; i++) {
79517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
79617ab2063SBarry Smith   }
797bfeeae90SHong Zhang   a->nz = ai[m];
79865e19b50SBarry 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);
79917ab2063SBarry Smith 
80009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
801d0f46423SBarry 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);
802ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
803ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8048e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
805dd5f02e7SSatish Balay   a->reallocs          = 0;
8064e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
80736db0b34SBarry Smith   a->rmax              = rmax;
8084e220ebcSLois Curfman McInnes 
809cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
81088e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
81171c2f376SKris Buschelman 
8124108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
81371f1c65dSBarry Smith 
81471f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
8153a40ed3dSBarry Smith   PetscFunctionReturn(0);
81617ab2063SBarry Smith }
81717ab2063SBarry Smith 
8184a2ae208SSatish Balay #undef __FUNCT__
81999cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
82099cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
82199cafbc1SBarry Smith {
82299cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
82399cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
82454f21887SBarry Smith   MatScalar      *aa = a->a;
82599cafbc1SBarry Smith 
82699cafbc1SBarry Smith   PetscFunctionBegin;
82799cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
82899cafbc1SBarry Smith   PetscFunctionReturn(0);
82999cafbc1SBarry Smith }
83099cafbc1SBarry Smith 
83199cafbc1SBarry Smith #undef __FUNCT__
83299cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
83399cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
83499cafbc1SBarry Smith {
83599cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
83699cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
83754f21887SBarry Smith   MatScalar      *aa = a->a;
83899cafbc1SBarry Smith 
83999cafbc1SBarry Smith   PetscFunctionBegin;
84099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
84199cafbc1SBarry Smith   PetscFunctionReturn(0);
84299cafbc1SBarry Smith }
84399cafbc1SBarry Smith 
84499cafbc1SBarry Smith #undef __FUNCT__
8454a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
846dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
84717ab2063SBarry Smith {
848416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
849dfbe8321SBarry Smith   PetscErrorCode ierr;
8503a40ed3dSBarry Smith 
8513a40ed3dSBarry Smith   PetscFunctionBegin;
852d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
8533a40ed3dSBarry Smith   PetscFunctionReturn(0);
85417ab2063SBarry Smith }
855416022c9SBarry Smith 
8564a2ae208SSatish Balay #undef __FUNCT__
8574a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
858dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
85917ab2063SBarry Smith {
860416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
861dfbe8321SBarry Smith   PetscErrorCode ierr;
862d5d45c9bSBarry Smith 
8633a40ed3dSBarry Smith   PetscFunctionBegin;
864aa482453SBarry Smith #if defined(PETSC_USE_LOG)
865d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
86617ab2063SBarry Smith #endif
867e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
868c38d4ed2SBarry Smith   if (a->row) {
869c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
870c38d4ed2SBarry Smith   }
871c38d4ed2SBarry Smith   if (a->col) {
872c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
873c38d4ed2SBarry Smith   }
87405b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
87505b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
87671f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
87705b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
87882bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
87905b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
880cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
88105b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
882407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
883cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
884a30b2313SHong Zhang 
8854108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
8864846f1f5SKris Buschelman 
887606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
888901853e0SKris Buschelman 
889dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
890901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
891901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
892901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
893901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
894901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
8955a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
896901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
897901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
898a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
899901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9003a40ed3dSBarry Smith   PetscFunctionReturn(0);
90117ab2063SBarry Smith }
90217ab2063SBarry Smith 
9034a2ae208SSatish Balay #undef __FUNCT__
9044a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
905ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
90617ab2063SBarry Smith {
907416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9084846f1f5SKris Buschelman   PetscErrorCode ierr;
9093a40ed3dSBarry Smith 
9103a40ed3dSBarry Smith   PetscFunctionBegin;
911a65d3064SKris Buschelman   switch (op) {
912a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
9134e0d8c25SBarry Smith       a->roworiented       = flg;
914a65d3064SKris Buschelman       break;
915a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
916a9817697SBarry Smith       a->keepnonzeropattern    = flg;
917a65d3064SKris Buschelman       break;
918512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
919512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
920a65d3064SKris Buschelman       break;
921a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
9224e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
923a65d3064SKris Buschelman       break;
924a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
9254e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
926a65d3064SKris Buschelman       break;
92728b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
92828b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
92928b2fa4aSMatthew Knepley       break;
930a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
9314e0d8c25SBarry Smith       a->ignorezeroentries = flg;
9320df259c2SBarry Smith       break;
933cd6b891eSBarry Smith     case MAT_CHECK_COMPRESSED_ROW:
934cd6b891eSBarry Smith       a->compressedrow.check = flg;
935d487561eSHong Zhang       break;
9363d472b54SHong Zhang     case MAT_SPD:
9373d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
9383d472b54SHong Zhang       A->spd                             = flg;
9393d472b54SHong Zhang       if (flg) {
9403d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
9413d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
9423d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
9433d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
9443d472b54SHong Zhang       }
9453d472b54SHong Zhang       break;
946b1646e73SJed Brown     case MAT_SYMMETRIC:
947b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
948b1646e73SJed Brown     case MAT_HERMITIAN:
949b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
9504e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
951a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
952a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
953290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
954a65d3064SKris Buschelman       break;
955b87ac2d8SJed Brown     case MAT_USE_INODES:
956b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
957b87ac2d8SJed Brown       break;
958a65d3064SKris Buschelman     default:
959e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
960a65d3064SKris Buschelman   }
9614108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
9623a40ed3dSBarry Smith   PetscFunctionReturn(0);
96317ab2063SBarry Smith }
96417ab2063SBarry Smith 
9654a2ae208SSatish Balay #undef __FUNCT__
9664a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
967dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
96817ab2063SBarry Smith {
969416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9706849ba73SBarry Smith   PetscErrorCode ierr;
971d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
97235e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
97317ab2063SBarry Smith 
9743a40ed3dSBarry Smith   PetscFunctionBegin;
975d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
976e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
97735e7444dSHong Zhang 
978d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
979d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
98035e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
98135e7444dSHong Zhang     for (i=0; i<n; i++) x[i] = aa[diag[i]];
98235e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
98335e7444dSHong Zhang     PetscFunctionReturn(0);
98435e7444dSHong Zhang   }
98535e7444dSHong Zhang 
9862dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
9871ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
98835e7444dSHong Zhang   for (i=0; i<n; i++) {
98935e7444dSHong Zhang     nz = ai[i+1] - ai[i];
9902f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
99135e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
99235e7444dSHong Zhang       if (aj[j] == i) {
99335e7444dSHong Zhang         x[i] = aa[j];
99417ab2063SBarry Smith         break;
99517ab2063SBarry Smith       }
99617ab2063SBarry Smith     }
99717ab2063SBarry Smith   }
9981ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
9993a40ed3dSBarry Smith   PetscFunctionReturn(0);
100017ab2063SBarry Smith }
100117ab2063SBarry Smith 
1002b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
10034a2ae208SSatish Balay #undef __FUNCT__
10044a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1005dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
100617ab2063SBarry Smith {
1007416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10085c897100SBarry Smith   PetscScalar       *x,*y;
1009dfbe8321SBarry Smith   PetscErrorCode    ierr;
1010d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10115c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1012a77337e4SBarry Smith   MatScalar         *v;
1013a77337e4SBarry Smith   PetscScalar       alpha;
101404fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10153447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1016ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10175c897100SBarry Smith #endif
101817ab2063SBarry Smith 
10193a40ed3dSBarry Smith   PetscFunctionBegin;
10202e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
10211ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10221ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10235c897100SBarry Smith 
10245c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1025bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
10265c897100SBarry Smith #else
10273447b6efSHong Zhang   if (usecprow){
10283447b6efSHong Zhang     m    = cprow.nrows;
10293447b6efSHong Zhang     ii   = cprow.i;
10307b2bb3b9SHong Zhang     ridx = cprow.rindex;
10313447b6efSHong Zhang   } else {
10323447b6efSHong Zhang     ii = a->i;
10333447b6efSHong Zhang   }
103417ab2063SBarry Smith   for (i=0; i<m; i++) {
10353447b6efSHong Zhang     idx   = a->j + ii[i] ;
10363447b6efSHong Zhang     v     = a->a + ii[i] ;
10373447b6efSHong Zhang     n     = ii[i+1] - ii[i];
10383447b6efSHong Zhang     if (usecprow){
10397b2bb3b9SHong Zhang       alpha = x[ridx[i]];
10403447b6efSHong Zhang     } else {
104117ab2063SBarry Smith       alpha = x[i];
10423447b6efSHong Zhang     }
104304fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
104417ab2063SBarry Smith   }
10455c897100SBarry Smith #endif
1046dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10471ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10481ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10493a40ed3dSBarry Smith   PetscFunctionReturn(0);
105017ab2063SBarry Smith }
105117ab2063SBarry Smith 
10524a2ae208SSatish Balay #undef __FUNCT__
10535c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1054dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
10555c897100SBarry Smith {
1056dfbe8321SBarry Smith   PetscErrorCode ierr;
10575c897100SBarry Smith 
10585c897100SBarry Smith   PetscFunctionBegin;
1059170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
10605c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
10615c897100SBarry Smith   PetscFunctionReturn(0);
10625c897100SBarry Smith }
10635c897100SBarry Smith 
1064a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
10655c897100SBarry Smith #undef __FUNCT__
10664a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1067dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
106817ab2063SBarry Smith {
1069416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1070d9fead3dSBarry Smith   PetscScalar       *y;
107154f21887SBarry Smith   const PetscScalar *x;
107254f21887SBarry Smith   const MatScalar   *aa;
1073dfbe8321SBarry Smith   PetscErrorCode    ierr;
1074003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1075003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
10768aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1077362ced78SSatish Balay   PetscScalar       sum;
1078ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
107917ab2063SBarry Smith 
1080b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
108197952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1082fee21e36SBarry Smith #endif
1083fee21e36SBarry Smith 
10843a40ed3dSBarry Smith   PetscFunctionBegin;
10853649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
10861ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
108797952fefSHong Zhang   aj  = a->j;
108897952fefSHong Zhang   aa  = a->a;
1089416022c9SBarry Smith   ii  = a->i;
10904eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
109197952fefSHong Zhang     m    = a->compressedrow.nrows;
109297952fefSHong Zhang     ii   = a->compressedrow.i;
109397952fefSHong Zhang     ridx = a->compressedrow.rindex;
109497952fefSHong Zhang     for (i=0; i<m; i++){
109597952fefSHong Zhang       n   = ii[i+1] - ii[i];
109697952fefSHong Zhang       aj  = a->j + ii[i];
109797952fefSHong Zhang       aa  = a->a + ii[i];
109897952fefSHong Zhang       sum = 0.0;
1099a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1100003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1101003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
110297952fefSHong Zhang       y[*ridx++] = sum;
110397952fefSHong Zhang     }
110497952fefSHong Zhang   } else { /* do not use compressed row format */
1105b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1106b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1107b05257ddSBarry Smith #else
110817ab2063SBarry Smith     for (i=0; i<m; i++) {
1109003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1110003131ecSBarry Smith       aj  = a->j + ii[i];
1111003131ecSBarry Smith       aa  = a->a + ii[i];
111217ab2063SBarry Smith       sum  = 0.0;
1113a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1114003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
111517ab2063SBarry Smith       y[i] = sum;
111617ab2063SBarry Smith     }
11178d195f9aSBarry Smith #endif
1118b05257ddSBarry Smith   }
1119dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
11203649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
11211ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11223a40ed3dSBarry Smith   PetscFunctionReturn(0);
112317ab2063SBarry Smith }
112417ab2063SBarry Smith 
1125a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
11264a2ae208SSatish Balay #undef __FUNCT__
11274a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1128dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
112917ab2063SBarry Smith {
1130416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
113154f21887SBarry Smith   PetscScalar     *x,*y,*z;
113254f21887SBarry Smith   const MatScalar *aa;
1133dfbe8321SBarry Smith   PetscErrorCode  ierr;
1134d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1135aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
113697952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1137362ced78SSatish Balay   PetscScalar     sum;
1138ace3abfcSBarry Smith   PetscBool       usecprow=a->compressedrow.use;
1139e36a17ebSSatish Balay #endif
11409ea0dfa2SSatish Balay 
11413a40ed3dSBarry Smith   PetscFunctionBegin;
11421ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
11431ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11442e8a6d31SBarry Smith   if (zz != yy) {
11451ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
11462e8a6d31SBarry Smith   } else {
11472e8a6d31SBarry Smith     z = y;
11482e8a6d31SBarry Smith   }
1149bfeeae90SHong Zhang 
115097952fefSHong Zhang   aj  = a->j;
115197952fefSHong Zhang   aa  = a->a;
1152cddf8d76SBarry Smith   ii  = a->i;
1153aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
115497952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
115502ab625aSSatish Balay #else
11564eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
11574eb6d288SHong Zhang     if (zz != yy){
11584eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
11594eb6d288SHong Zhang     }
116097952fefSHong Zhang     m    = a->compressedrow.nrows;
116197952fefSHong Zhang     ii   = a->compressedrow.i;
116297952fefSHong Zhang     ridx = a->compressedrow.rindex;
116397952fefSHong Zhang     for (i=0; i<m; i++){
116497952fefSHong Zhang       n  = ii[i+1] - ii[i];
116597952fefSHong Zhang       aj  = a->j + ii[i];
116697952fefSHong Zhang       aa  = a->a + ii[i];
116797952fefSHong Zhang       sum = y[*ridx];
116897952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
116997952fefSHong Zhang       z[*ridx++] = sum;
117097952fefSHong Zhang     }
117197952fefSHong Zhang   } else { /* do not use compressed row format */
117217ab2063SBarry Smith     for (i=0; i<m; i++) {
11739ea0dfa2SSatish Balay       jrow = ii[i];
11749ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
117517ab2063SBarry Smith       sum  = y[i];
11769ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
117797952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
11789ea0dfa2SSatish Balay       }
117917ab2063SBarry Smith       z[i] = sum;
118017ab2063SBarry Smith     }
118197952fefSHong Zhang   }
118202ab625aSSatish Balay #endif
1183dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11841ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11851ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11862e8a6d31SBarry Smith   if (zz != yy) {
11871ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
11882e8a6d31SBarry Smith   }
1189918e98c3SVictor Minden #if defined(PETSC_HAVE_CUDA)
11906b375ea7SVictor Minden   /*
1191918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1192918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1193918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
11946b375ea7SVictor Minden   */
1195918e98c3SVictor Minden #endif
11963a40ed3dSBarry Smith   PetscFunctionReturn(0);
119717ab2063SBarry Smith }
119817ab2063SBarry Smith 
119917ab2063SBarry Smith /*
120017ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
120117ab2063SBarry Smith */
12024a2ae208SSatish Balay #undef __FUNCT__
12034a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1204dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
120517ab2063SBarry Smith {
1206416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12076849ba73SBarry Smith   PetscErrorCode ierr;
1208d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
120917ab2063SBarry Smith 
12103a40ed3dSBarry Smith   PetscFunctionBegin;
121109f38230SBarry Smith   if (!a->diag) {
121209f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
12139518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
121409f38230SBarry Smith   }
1215d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
121609f38230SBarry Smith     a->diag[i] = a->i[i+1];
1217bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1218bfeeae90SHong Zhang       if (a->j[j] == i) {
121909f38230SBarry Smith         a->diag[i] = j;
122017ab2063SBarry Smith         break;
122117ab2063SBarry Smith       }
122217ab2063SBarry Smith     }
122317ab2063SBarry Smith   }
12243a40ed3dSBarry Smith   PetscFunctionReturn(0);
122517ab2063SBarry Smith }
122617ab2063SBarry Smith 
1227be5855fcSBarry Smith /*
1228be5855fcSBarry Smith      Checks for missing diagonals
1229be5855fcSBarry Smith */
12304a2ae208SSatish Balay #undef __FUNCT__
12314a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1232ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1233be5855fcSBarry Smith {
1234be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
123597f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1236be5855fcSBarry Smith 
1237be5855fcSBarry Smith   PetscFunctionBegin;
123809f38230SBarry Smith   *missing = PETSC_FALSE;
1239d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
124009f38230SBarry Smith     *missing  = PETSC_TRUE;
124109f38230SBarry Smith     if (d) *d = 0;
124209f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
124309f38230SBarry Smith   } else {
1244f1e2ffcdSBarry Smith     diag = a->diag;
1245d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1246bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
124709f38230SBarry Smith 	*missing = PETSC_TRUE;
124809f38230SBarry Smith 	if (d) *d = i;
124909f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
125009f38230SBarry Smith       }
1251be5855fcSBarry Smith     }
1252be5855fcSBarry Smith   }
1253be5855fcSBarry Smith   PetscFunctionReturn(0);
1254be5855fcSBarry Smith }
1255be5855fcSBarry Smith 
125671f1c65dSBarry Smith EXTERN_C_BEGIN
125771f1c65dSBarry Smith #undef __FUNCT__
125871f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
12597087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
126071f1c65dSBarry Smith {
126171f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
126271f1c65dSBarry Smith   PetscErrorCode ierr;
1263d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
126454f21887SBarry Smith   MatScalar      *v = a->a;
126554f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
126671f1c65dSBarry Smith 
126771f1c65dSBarry Smith   PetscFunctionBegin;
126871f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
126971f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
127071f1c65dSBarry Smith   diag = a->diag;
127171f1c65dSBarry Smith   if (!a->idiag) {
127271f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
127371f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
127471f1c65dSBarry Smith     v        = a->a;
127571f1c65dSBarry Smith   }
127671f1c65dSBarry Smith   mdiag = a->mdiag;
127771f1c65dSBarry Smith   idiag = a->idiag;
127871f1c65dSBarry Smith 
1279028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
128071f1c65dSBarry Smith     for (i=0; i<m; i++) {
128171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1282e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
128371f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
128471f1c65dSBarry Smith     }
128571f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
128671f1c65dSBarry Smith   } else {
128771f1c65dSBarry Smith     for (i=0; i<m; i++) {
128871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
128971f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
129071f1c65dSBarry Smith     }
1291dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
129271f1c65dSBarry Smith   }
129371f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
129471f1c65dSBarry Smith   PetscFunctionReturn(0);
129571f1c65dSBarry Smith }
12965a9745a3SMatthew Knepley EXTERN_C_END
129771f1c65dSBarry Smith 
1298a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
12994a2ae208SSatish Balay #undef __FUNCT__
130041f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
130141f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
130217ab2063SBarry Smith {
1303416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1304e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1305e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
130654f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1307dfbe8321SBarry Smith   PetscErrorCode     ierr;
1308d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
130997f1f81fSBarry Smith   const PetscInt     *idx,*diag;
131017ab2063SBarry Smith 
13113a40ed3dSBarry Smith   PetscFunctionBegin;
1312b965ef7fSBarry Smith   its = its*lits;
131391723122SBarry Smith 
131471f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
131571f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
131671f1c65dSBarry Smith   a->fshift = fshift;
131771f1c65dSBarry Smith   a->omega  = omega;
1318ed480e8bSBarry Smith 
131971f1c65dSBarry Smith   diag = a->diag;
132071f1c65dSBarry Smith   t     = a->ssor_work;
1321ed480e8bSBarry Smith   idiag = a->idiag;
132271f1c65dSBarry Smith   mdiag = a->mdiag;
1323ed480e8bSBarry Smith 
13241ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
13253649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
132671f1c65dSBarry Smith   CHKMEMQ;
1327ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
132817ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
132917ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1330ed480e8bSBarry Smith     bs = b;
133117ab2063SBarry Smith     for (i=0; i<m; i++) {
133271f1c65dSBarry Smith         d    = fshift + mdiag[i];
1333416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1334ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1335ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
133617ab2063SBarry Smith         sum  = b[i]*d/omega;
1337003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
133817ab2063SBarry Smith         x[i] = sum;
133917ab2063SBarry Smith     }
13401ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13413649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1342efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13433a40ed3dSBarry Smith     PetscFunctionReturn(0);
134417ab2063SBarry Smith   }
1345c783ea89SBarry Smith 
134648af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1347e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
13483a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
134917ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1350887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
135117ab2063SBarry Smith 
135217ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
135317ab2063SBarry Smith 
1354887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
135517ab2063SBarry Smith     */
135617ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
135717ab2063SBarry Smith 
135817ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
135917ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1360416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1361ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1362ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
136317ab2063SBarry Smith       sum  = b[i];
1364e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1365ed480e8bSBarry Smith       x[i] = sum*idiag[i];
136617ab2063SBarry Smith     }
136717ab2063SBarry Smith 
136817ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1369416022c9SBarry Smith     v = a->a;
1370ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
137117ab2063SBarry Smith 
137217ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1373ed480e8bSBarry Smith     ts = t;
1374416022c9SBarry Smith     diag = a->diag;
137517ab2063SBarry Smith     for (i=0; i<m; i++) {
1376416022c9SBarry Smith       n    = diag[i] - a->i[i];
1377ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1378ed480e8bSBarry Smith       v    = a->a + a->i[i];
137917ab2063SBarry Smith       sum  = t[i];
1380003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1381ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1382733d66baSBarry Smith       /*  x = x + t */
1383733d66baSBarry Smith       x[i] += t[i];
138417ab2063SBarry Smith     }
138517ab2063SBarry Smith 
1386dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
13871ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13883649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
13893a40ed3dSBarry Smith     PetscFunctionReturn(0);
139017ab2063SBarry Smith   }
139117ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
139217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
139317ab2063SBarry Smith       for (i=0; i<m; i++) {
1394416022c9SBarry Smith         n    = diag[i] - a->i[i];
1395ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1396ed480e8bSBarry Smith         v    = a->a + a->i[i];
139717ab2063SBarry Smith         sum  = b[i];
1398e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13995c99c7daSBarry Smith         t[i] = sum;
1400ed480e8bSBarry Smith         x[i] = sum*idiag[i];
140117ab2063SBarry Smith       }
14025c99c7daSBarry Smith       xb = t;
1403efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
14043a40ed3dSBarry Smith     } else xb = b;
140517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
140617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1407416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1408ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1409ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
141017ab2063SBarry Smith         sum  = xb[i];
1411e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
14125c99c7daSBarry Smith         if (xb == b) {
1413ed480e8bSBarry Smith           x[i] = sum*idiag[i];
14145c99c7daSBarry Smith         } else {
14155c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
141617ab2063SBarry Smith         }
14175c99c7daSBarry Smith       }
1418efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
141917ab2063SBarry Smith     }
142017ab2063SBarry Smith     its--;
142117ab2063SBarry Smith   }
142217ab2063SBarry Smith   while (its--) {
142317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
142417ab2063SBarry Smith       for (i=0; i<m; i++) {
1425416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1426ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1427ed480e8bSBarry Smith         v    = a->a + a->i[i];
142817ab2063SBarry Smith         sum  = b[i];
1429e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1430ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
143117ab2063SBarry Smith       }
14329f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
143317ab2063SBarry Smith     }
143417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
143517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1436416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1437ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1438ed480e8bSBarry Smith         v    = a->a + a->i[i];
143917ab2063SBarry Smith         sum  = b[i];
1440e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1441ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
144217ab2063SBarry Smith       }
14439f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
144417ab2063SBarry Smith     }
144517ab2063SBarry Smith   }
14461ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
14473649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
144871f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
144917ab2063SBarry Smith }
145017ab2063SBarry Smith 
14512af78befSBarry Smith 
14524a2ae208SSatish Balay #undef __FUNCT__
14534a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1454dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
145517ab2063SBarry Smith {
1456416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
14574e220ebcSLois Curfman McInnes 
14583a40ed3dSBarry Smith   PetscFunctionBegin;
14594e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
14604e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
14614e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
14624e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
14634e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
14648e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
14657adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1466d5f3da31SBarry Smith   if (A->factortype) {
14674e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
14684e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
14694e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
14704e220ebcSLois Curfman McInnes   } else {
14714e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
14724e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
14734e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
14744e220ebcSLois Curfman McInnes   }
14753a40ed3dSBarry Smith   PetscFunctionReturn(0);
147617ab2063SBarry Smith }
147717ab2063SBarry Smith 
14784a2ae208SSatish Balay #undef __FUNCT__
14794a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
14802b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
148117ab2063SBarry Smith {
1482416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
14833b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
14846849ba73SBarry Smith   PetscErrorCode    ierr;
148597b48c8fSBarry Smith   const PetscScalar *xx;
148697b48c8fSBarry Smith   PetscScalar       *bb;
1487ace3abfcSBarry Smith   PetscBool         missing;
148817ab2063SBarry Smith 
14893a40ed3dSBarry Smith   PetscFunctionBegin;
149097b48c8fSBarry Smith   if (x && b) {
149197b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
149297b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
149397b48c8fSBarry Smith     for (i=0; i<N; i++) {
149497b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
149597b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
149697b48c8fSBarry Smith     }
149797b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
149897b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
149997b48c8fSBarry Smith   }
150097b48c8fSBarry Smith 
1501a9817697SBarry Smith   if (a->keepnonzeropattern) {
1502f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1503e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1504bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1505f1e2ffcdSBarry Smith     }
1506f4df32b1SMatthew Knepley     if (diag != 0.0) {
150709f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1508e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1509f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1510f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1511f1e2ffcdSBarry Smith       }
1512f1e2ffcdSBarry Smith     }
151388e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1514f1e2ffcdSBarry Smith   } else {
1515f4df32b1SMatthew Knepley     if (diag != 0.0) {
151617ab2063SBarry Smith       for (i=0; i<N; i++) {
1517e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15187ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1519416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1520f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1521bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
15227ae801bdSBarry Smith         } else { /* in case row was completely empty */
1523f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
152417ab2063SBarry Smith         }
152517ab2063SBarry Smith       }
15263a40ed3dSBarry Smith     } else {
152717ab2063SBarry Smith       for (i=0; i<N; i++) {
1528e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1529416022c9SBarry Smith         a->ilen[rows[i]] = 0;
153017ab2063SBarry Smith       }
153117ab2063SBarry Smith     }
153288e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1533f1e2ffcdSBarry Smith   }
153443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15353a40ed3dSBarry Smith   PetscFunctionReturn(0);
153617ab2063SBarry Smith }
153717ab2063SBarry Smith 
15384a2ae208SSatish Balay #undef __FUNCT__
15396e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
15406e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
15416e169961SBarry Smith {
15426e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
15436e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
15446e169961SBarry Smith   PetscErrorCode    ierr;
15452b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
15466e169961SBarry Smith   const PetscScalar *xx;
15476e169961SBarry Smith   PetscScalar       *bb;
15486e169961SBarry Smith 
15496e169961SBarry Smith   PetscFunctionBegin;
15506e169961SBarry Smith   if (x && b) {
15516e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
15526e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
15532b40b63fSBarry Smith     vecs = PETSC_TRUE;
15546e169961SBarry Smith   }
15556e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
15566e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
15576e169961SBarry Smith   for (i=0; i<N; i++) {
15586e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15596e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
15606e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
15616e169961SBarry Smith   }
15626e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
15636e169961SBarry Smith     if (!zeroed[i]) {
15646e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
15656e169961SBarry Smith         if (zeroed[a->j[j]]) {
15662b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
15676e169961SBarry Smith           a->a[j] = 0.0;
15686e169961SBarry Smith         }
15696e169961SBarry Smith       }
15702b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
15716e169961SBarry Smith   }
15726e169961SBarry Smith   if (x && b) {
15736e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
15746e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
15756e169961SBarry Smith   }
15766e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
15776e169961SBarry Smith   if (diag != 0.0) {
15786e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
15796e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
15806e169961SBarry Smith     for (i=0; i<N; i++) {
15816e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
15826e169961SBarry Smith     }
15836e169961SBarry Smith   }
15846e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
15856e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15866e169961SBarry Smith   PetscFunctionReturn(0);
15876e169961SBarry Smith }
15886e169961SBarry Smith 
15896e169961SBarry Smith #undef __FUNCT__
15904a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1591a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
159217ab2063SBarry Smith {
1593416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
159497f1f81fSBarry Smith   PetscInt   *itmp;
159517ab2063SBarry Smith 
15963a40ed3dSBarry Smith   PetscFunctionBegin;
1597e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
159817ab2063SBarry Smith 
1599416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1600bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
160117ab2063SBarry Smith   if (idx) {
1602bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1603bfeeae90SHong Zhang     if (*nz) {
16044e093b46SBarry Smith       *idx = itmp;
160517ab2063SBarry Smith     }
160617ab2063SBarry Smith     else *idx = 0;
160717ab2063SBarry Smith   }
16083a40ed3dSBarry Smith   PetscFunctionReturn(0);
160917ab2063SBarry Smith }
161017ab2063SBarry Smith 
1611bfeeae90SHong Zhang /* remove this function? */
16124a2ae208SSatish Balay #undef __FUNCT__
16134a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1614a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
161517ab2063SBarry Smith {
16163a40ed3dSBarry Smith   PetscFunctionBegin;
16173a40ed3dSBarry Smith   PetscFunctionReturn(0);
161817ab2063SBarry Smith }
161917ab2063SBarry Smith 
16204a2ae208SSatish Balay #undef __FUNCT__
16214a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1622dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
162317ab2063SBarry Smith {
1624416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
162554f21887SBarry Smith   MatScalar      *v = a->a;
162636db0b34SBarry Smith   PetscReal      sum = 0.0;
16276849ba73SBarry Smith   PetscErrorCode ierr;
162897f1f81fSBarry Smith   PetscInt       i,j;
162917ab2063SBarry Smith 
16303a40ed3dSBarry Smith   PetscFunctionBegin;
163117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1632416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1633aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
163436db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
163517ab2063SBarry Smith #else
163617ab2063SBarry Smith       sum += (*v)*(*v); v++;
163717ab2063SBarry Smith #endif
163817ab2063SBarry Smith     }
1639064f8208SBarry Smith     *nrm = sqrt(sum);
16403a40ed3dSBarry Smith   } else if (type == NORM_1) {
164136db0b34SBarry Smith     PetscReal *tmp;
164297f1f81fSBarry Smith     PetscInt    *jj = a->j;
1643d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1644d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1645064f8208SBarry Smith     *nrm = 0.0;
1646416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1647bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
164817ab2063SBarry Smith     }
1649d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1650064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
165117ab2063SBarry Smith     }
1652606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
16533a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1654064f8208SBarry Smith     *nrm = 0.0;
1655d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1656bfeeae90SHong Zhang       v = a->a + a->i[j];
165717ab2063SBarry Smith       sum = 0.0;
1658416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1659cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
166017ab2063SBarry Smith       }
1661064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
166217ab2063SBarry Smith     }
16633a40ed3dSBarry Smith   } else {
1664e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
166517ab2063SBarry Smith   }
16663a40ed3dSBarry Smith   PetscFunctionReturn(0);
166717ab2063SBarry Smith }
166817ab2063SBarry Smith 
16694a2ae208SSatish Balay #undef __FUNCT__
16704a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1671fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
167217ab2063SBarry Smith {
1673416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1674416022c9SBarry Smith   Mat            C;
16756849ba73SBarry Smith   PetscErrorCode ierr;
1676d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
167754f21887SBarry Smith   MatScalar      *array = a->a;
167817ab2063SBarry Smith 
16793a40ed3dSBarry Smith   PetscFunctionBegin;
1680e32f2f54SBarry 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");
1681fc4dec0aSBarry Smith 
1682fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1683d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1684d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1685bfeeae90SHong Zhang 
1686bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
16877adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1688d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
16897adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1690ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1691606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1692a541d17aSBarry Smith   } else {
1693a541d17aSBarry Smith     C = *B;
1694a541d17aSBarry Smith   }
1695a541d17aSBarry Smith 
169617ab2063SBarry Smith   for (i=0; i<m; i++) {
169717ab2063SBarry Smith     len    = ai[i+1]-ai[i];
169887d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1699b9b97703SBarry Smith     array += len;
1700b9b97703SBarry Smith     aj    += len;
170117ab2063SBarry Smith   }
17026d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17036d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
170417ab2063SBarry Smith 
1705815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1706416022c9SBarry Smith     *B = C;
170717ab2063SBarry Smith   } else {
1708eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
170917ab2063SBarry Smith   }
17103a40ed3dSBarry Smith   PetscFunctionReturn(0);
171117ab2063SBarry Smith }
171217ab2063SBarry Smith 
1713cd0d46ebSvictorle EXTERN_C_BEGIN
1714cd0d46ebSvictorle #undef __FUNCT__
17155fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
17167087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1717cd0d46ebSvictorle {
1718cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
171954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
172054f21887SBarry Smith   MatScalar      *va,*vb;
17216849ba73SBarry Smith   PetscErrorCode ierr;
172297f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1723cd0d46ebSvictorle 
1724cd0d46ebSvictorle   PetscFunctionBegin;
1725cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1726cd0d46ebSvictorle 
1727cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1728cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
17295485867bSBarry Smith   if (ma!=nb || na!=mb){
17305485867bSBarry Smith     *f = PETSC_FALSE;
17315485867bSBarry Smith     PetscFunctionReturn(0);
17325485867bSBarry Smith   }
1733cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1734cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1735cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
173697f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
173797f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1738cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1739cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1740cd0d46ebSvictorle 
1741cd0d46ebSvictorle   *f = PETSC_TRUE;
1742cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1743cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
174497f1f81fSBarry Smith       PetscInt         idc,idr;
17455485867bSBarry Smith       PetscScalar vc,vr;
1746cd0d46ebSvictorle       /* column/row index/value */
17475485867bSBarry Smith       idc = adx[aptr[i]];
17485485867bSBarry Smith       idr = bdx[bptr[idc]];
17495485867bSBarry Smith       vc  = va[aptr[i]];
17505485867bSBarry Smith       vr  = vb[bptr[idc]];
17515485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
17525485867bSBarry Smith 	*f = PETSC_FALSE;
17535485867bSBarry Smith         goto done;
1754cd0d46ebSvictorle       } else {
17555485867bSBarry Smith 	aptr[i]++;
17565485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1757cd0d46ebSvictorle       }
1758cd0d46ebSvictorle     }
1759cd0d46ebSvictorle   }
1760cd0d46ebSvictorle  done:
1761cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
17623aeef889SHong Zhang   if (B) {
17633aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
17643aeef889SHong Zhang   }
1765cd0d46ebSvictorle   PetscFunctionReturn(0);
1766cd0d46ebSvictorle }
1767cd0d46ebSvictorle EXTERN_C_END
1768cd0d46ebSvictorle 
17691cbb95d3SBarry Smith EXTERN_C_BEGIN
17701cbb95d3SBarry Smith #undef __FUNCT__
17711cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
17727087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
17731cbb95d3SBarry Smith {
17741cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
177554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
177654f21887SBarry Smith   MatScalar      *va,*vb;
17771cbb95d3SBarry Smith   PetscErrorCode ierr;
17781cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
17791cbb95d3SBarry Smith 
17801cbb95d3SBarry Smith   PetscFunctionBegin;
17811cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
17821cbb95d3SBarry Smith 
17831cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
17841cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
17851cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
17861cbb95d3SBarry Smith     *f = PETSC_FALSE;
17871cbb95d3SBarry Smith     PetscFunctionReturn(0);
17881cbb95d3SBarry Smith   }
17891cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
17901cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
17911cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
17921cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
17931cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
17941cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
17951cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
17961cbb95d3SBarry Smith 
17971cbb95d3SBarry Smith   *f = PETSC_TRUE;
17981cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
17991cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
18001cbb95d3SBarry Smith       PetscInt         idc,idr;
18011cbb95d3SBarry Smith       PetscScalar vc,vr;
18021cbb95d3SBarry Smith       /* column/row index/value */
18031cbb95d3SBarry Smith       idc = adx[aptr[i]];
18041cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
18051cbb95d3SBarry Smith       vc  = va[aptr[i]];
18061cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
18071cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
18081cbb95d3SBarry Smith 	*f = PETSC_FALSE;
18091cbb95d3SBarry Smith         goto done;
18101cbb95d3SBarry Smith       } else {
18111cbb95d3SBarry Smith 	aptr[i]++;
18121cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
18131cbb95d3SBarry Smith       }
18141cbb95d3SBarry Smith     }
18151cbb95d3SBarry Smith   }
18161cbb95d3SBarry Smith  done:
18171cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
18181cbb95d3SBarry Smith   if (B) {
18191cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
18201cbb95d3SBarry Smith   }
18211cbb95d3SBarry Smith   PetscFunctionReturn(0);
18221cbb95d3SBarry Smith }
18231cbb95d3SBarry Smith EXTERN_C_END
18241cbb95d3SBarry Smith 
18259e29f15eSvictorle #undef __FUNCT__
18269e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1827ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
18289e29f15eSvictorle {
1829dfbe8321SBarry Smith   PetscErrorCode ierr;
18309e29f15eSvictorle   PetscFunctionBegin;
18315485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
18329e29f15eSvictorle   PetscFunctionReturn(0);
18339e29f15eSvictorle }
18349e29f15eSvictorle 
18354a2ae208SSatish Balay #undef __FUNCT__
18361cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
1837ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
18381cbb95d3SBarry Smith {
18391cbb95d3SBarry Smith   PetscErrorCode ierr;
18401cbb95d3SBarry Smith   PetscFunctionBegin;
18411cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
18421cbb95d3SBarry Smith   PetscFunctionReturn(0);
18431cbb95d3SBarry Smith }
18441cbb95d3SBarry Smith 
18451cbb95d3SBarry Smith #undef __FUNCT__
18464a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1847dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
184817ab2063SBarry Smith {
1849416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
185054f21887SBarry Smith   PetscScalar    *l,*r,x;
185154f21887SBarry Smith   MatScalar      *v;
1852dfbe8321SBarry Smith   PetscErrorCode ierr;
1853d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
185417ab2063SBarry Smith 
18553a40ed3dSBarry Smith   PetscFunctionBegin;
185617ab2063SBarry Smith   if (ll) {
18573ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
18583ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1859e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1860e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
18611ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1862416022c9SBarry Smith     v = a->a;
186317ab2063SBarry Smith     for (i=0; i<m; i++) {
186417ab2063SBarry Smith       x = l[i];
1865416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
186617ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
186717ab2063SBarry Smith     }
18681ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1869efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
187017ab2063SBarry Smith   }
187117ab2063SBarry Smith   if (rr) {
1872e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1873e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
18741ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1875416022c9SBarry Smith     v = a->a; jj = a->j;
187617ab2063SBarry Smith     for (i=0; i<nz; i++) {
1877bfeeae90SHong Zhang       (*v++) *= r[*jj++];
187817ab2063SBarry Smith     }
18791ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1880efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
188117ab2063SBarry Smith   }
18823a40ed3dSBarry Smith   PetscFunctionReturn(0);
188317ab2063SBarry Smith }
188417ab2063SBarry Smith 
18854a2ae208SSatish Balay #undef __FUNCT__
18864a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
188797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
188817ab2063SBarry Smith {
1889db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
18906849ba73SBarry Smith   PetscErrorCode ierr;
1891d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
189297f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
18935d0c19d7SBarry Smith   const PetscInt *irow,*icol;
18945d0c19d7SBarry Smith   PetscInt       nrows,ncols;
189597f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
189654f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1897416022c9SBarry Smith   Mat            C;
1898ace3abfcSBarry Smith   PetscBool      stride,sorted;
189917ab2063SBarry Smith 
19003a40ed3dSBarry Smith   PetscFunctionBegin;
190114ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1902e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
190314ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1904e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
190599141d43SSatish Balay 
190617ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1907b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1908b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
190917ab2063SBarry Smith 
1910fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
19110dbe5b1eSSatish Balay   ierr = PetscTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
1912fee21e36SBarry Smith   if (stride && step == 1) {
191302834360SBarry Smith     /* special case of contiguous rows */
19140e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
191502834360SBarry Smith     /* loop over new rows determining lens and starting points */
191602834360SBarry Smith     for (i=0; i<nrows; i++) {
1917bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1918a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
191902834360SBarry Smith       for (k=kstart; k<kend; k++) {
1920bfeeae90SHong Zhang         if (aj[k] >= first) {
192102834360SBarry Smith           starts[i] = k;
192202834360SBarry Smith           break;
192302834360SBarry Smith 	}
192402834360SBarry Smith       }
1925a2744918SBarry Smith       sum = 0;
192602834360SBarry Smith       while (k < kend) {
1927bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1928a2744918SBarry Smith         sum++;
192902834360SBarry Smith       }
1930a2744918SBarry Smith       lens[i] = sum;
193102834360SBarry Smith     }
193202834360SBarry Smith     /* create submatrix */
1933cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
193497f1f81fSBarry Smith       PetscInt n_cols,n_rows;
193508480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1936e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1937d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
193808480c60SBarry Smith       C = *B;
19393a40ed3dSBarry Smith     } else {
19407adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1941f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
19427adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1943ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
194408480c60SBarry Smith     }
1945db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1946db02288aSLois Curfman McInnes 
194702834360SBarry Smith     /* loop over rows inserting into submatrix */
1948db02288aSLois Curfman McInnes     a_new    = c->a;
1949db02288aSLois Curfman McInnes     j_new    = c->j;
1950db02288aSLois Curfman McInnes     i_new    = c->i;
1951bfeeae90SHong Zhang 
195202834360SBarry Smith     for (i=0; i<nrows; i++) {
1953a2744918SBarry Smith       ii    = starts[i];
1954a2744918SBarry Smith       lensi = lens[i];
1955a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1956a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
195702834360SBarry Smith       }
195887828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1959a2744918SBarry Smith       a_new      += lensi;
1960a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1961a2744918SBarry Smith       c->ilen[i]  = lensi;
196202834360SBarry Smith     }
19630e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
19643a40ed3dSBarry Smith   } else {
196502834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
19660e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
196797f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
19680e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
196917ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
197002834360SBarry Smith     /* determine lens of each row */
197102834360SBarry Smith     for (i=0; i<nrows; i++) {
1972bfeeae90SHong Zhang       kstart  = ai[irow[i]];
197302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
197402834360SBarry Smith       lens[i] = 0;
197502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1976bfeeae90SHong Zhang         if (smap[aj[k]]) {
197702834360SBarry Smith           lens[i]++;
197802834360SBarry Smith         }
197902834360SBarry Smith       }
198002834360SBarry Smith     }
198117ab2063SBarry Smith     /* Create and fill new matrix */
1982a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
1983ace3abfcSBarry Smith       PetscBool  equal;
19840f5bd95cSBarry Smith 
198599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1986e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1987d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
19880f5bd95cSBarry Smith       if (!equal) {
1989e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
199099141d43SSatish Balay       }
1991d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
199208480c60SBarry Smith       C = *B;
19933a40ed3dSBarry Smith     } else {
19947adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1995f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
19967adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1997ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
199808480c60SBarry Smith     }
199999141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
200017ab2063SBarry Smith     for (i=0; i<nrows; i++) {
200199141d43SSatish Balay       row    = irow[i];
2002bfeeae90SHong Zhang       kstart = ai[row];
200399141d43SSatish Balay       kend   = kstart + a->ilen[row];
2004bfeeae90SHong Zhang       mat_i  = c->i[i];
200599141d43SSatish Balay       mat_j  = c->j + mat_i;
200699141d43SSatish Balay       mat_a  = c->a + mat_i;
200799141d43SSatish Balay       mat_ilen = c->ilen + i;
200817ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2009bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2010ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
201199141d43SSatish Balay           *mat_a++ = a->a[k];
201299141d43SSatish Balay           (*mat_ilen)++;
201399141d43SSatish Balay 
201417ab2063SBarry Smith         }
201517ab2063SBarry Smith       }
201617ab2063SBarry Smith     }
201702834360SBarry Smith     /* Free work space */
201802834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2019606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2020606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
202102834360SBarry Smith   }
20226d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20236d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
202417ab2063SBarry Smith 
202517ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2026416022c9SBarry Smith   *B = C;
20273a40ed3dSBarry Smith   PetscFunctionReturn(0);
202817ab2063SBarry Smith }
202917ab2063SBarry Smith 
20301df811f5SHong Zhang #undef __FUNCT__
203182d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
203282d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
203382d44351SHong Zhang {
203482d44351SHong Zhang   PetscErrorCode ierr;
203582d44351SHong Zhang   Mat            B;
203682d44351SHong Zhang 
203782d44351SHong Zhang   PetscFunctionBegin;
203882d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
203982d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
204082d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
204182d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
204282d44351SHong Zhang   *subMat = B;
204382d44351SHong Zhang   PetscFunctionReturn(0);
204482d44351SHong Zhang }
204582d44351SHong Zhang 
204682d44351SHong Zhang #undef __FUNCT__
20474a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
20480481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2049a871dcd8SBarry Smith {
205063b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2051dfbe8321SBarry Smith   PetscErrorCode ierr;
205263b91edcSBarry Smith   Mat            outA;
2053ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
205463b91edcSBarry Smith 
20553a40ed3dSBarry Smith   PetscFunctionBegin;
2056e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
20571df811f5SHong Zhang 
2058b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2059b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2060a871dcd8SBarry Smith 
206163b91edcSBarry Smith   outA              = inA;
2062d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2063c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
2064c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
2065c3122656SLisandro Dalcin   a->row = row;
2066c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
2067c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
2068c3122656SLisandro Dalcin   a->col = col;
206963b91edcSBarry Smith 
207036db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2071b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
20724c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
207352e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2074f0ec6fceSSatish Balay 
207594a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2076d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2077d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
207894a9d846SBarry Smith   }
207963b91edcSBarry Smith 
2080f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2081137fb511SHong Zhang   if (row_identity && col_identity) {
2082ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2083137fb511SHong Zhang   } else {
2084719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2085137fb511SHong Zhang   }
20863a40ed3dSBarry Smith   PetscFunctionReturn(0);
2087a871dcd8SBarry Smith }
2088a871dcd8SBarry Smith 
20894a2ae208SSatish Balay #undef __FUNCT__
20904a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2091f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2092f0b747eeSBarry Smith {
2093f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2094f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2095efee365bSSatish Balay   PetscErrorCode ierr;
20960805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
20973a40ed3dSBarry Smith 
20983a40ed3dSBarry Smith   PetscFunctionBegin;
2099f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2100efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
21013a40ed3dSBarry Smith   PetscFunctionReturn(0);
2102f0b747eeSBarry Smith }
2103f0b747eeSBarry Smith 
21044a2ae208SSatish Balay #undef __FUNCT__
21054a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
210697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2107cddf8d76SBarry Smith {
2108dfbe8321SBarry Smith   PetscErrorCode ierr;
210997f1f81fSBarry Smith   PetscInt       i;
2110cddf8d76SBarry Smith 
21113a40ed3dSBarry Smith   PetscFunctionBegin;
2112cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2113b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2114cddf8d76SBarry Smith   }
2115cddf8d76SBarry Smith 
2116cddf8d76SBarry Smith   for (i=0; i<n; i++) {
21176a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2118cddf8d76SBarry Smith   }
21193a40ed3dSBarry Smith   PetscFunctionReturn(0);
2120cddf8d76SBarry Smith }
2121cddf8d76SBarry Smith 
21224a2ae208SSatish Balay #undef __FUNCT__
21234a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
212497f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
21254dcbc457SBarry Smith {
2126e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
21276849ba73SBarry Smith   PetscErrorCode ierr;
21285d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
21295d0c19d7SBarry Smith   const PetscInt *idx;
213097f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2131f1af5d2fSBarry Smith   PetscBT        table;
2132bbd702dbSSatish Balay 
21333a40ed3dSBarry Smith   PetscFunctionBegin;
2134d0f46423SBarry Smith   m     = A->rmap->n;
2135e4d965acSSatish Balay   ai    = a->i;
2136bfeeae90SHong Zhang   aj    = a->j;
21378a047759SSatish Balay 
2138e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
213906763907SSatish Balay 
214097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
21416831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
214206763907SSatish Balay 
2143e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2144b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2145e4d965acSSatish Balay     isz  = 0;
21466831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2147e4d965acSSatish Balay 
2148e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
21494dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2150b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2151e4d965acSSatish Balay 
2152dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2153e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2154f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
21554dcbc457SBarry Smith     }
215606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
215706763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
2158e4d965acSSatish Balay 
215904a348a9SBarry Smith     k = 0;
216004a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
216104a348a9SBarry Smith       n = isz;
216206763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2163e4d965acSSatish Balay         row   = nidx[k];
2164e4d965acSSatish Balay         start = ai[row];
2165e4d965acSSatish Balay         end   = ai[row+1];
216604a348a9SBarry Smith         for (l = start; l<end ; l++){
2167efb16452SHong Zhang           val = aj[l] ;
2168f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2169e4d965acSSatish Balay         }
2170e4d965acSSatish Balay       }
2171e4d965acSSatish Balay     }
217270b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2173e4d965acSSatish Balay   }
21746831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2175606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
21763a40ed3dSBarry Smith   PetscFunctionReturn(0);
21774dcbc457SBarry Smith }
217817ab2063SBarry Smith 
21790513a670SBarry Smith /* -------------------------------------------------------------- */
21804a2ae208SSatish Balay #undef __FUNCT__
21814a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2182dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
21830513a670SBarry Smith {
21840513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
21856849ba73SBarry Smith   PetscErrorCode ierr;
21863b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
21875d0c19d7SBarry Smith   const PetscInt *row,*col;
21885d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
218956cd22aeSBarry Smith   IS             icolp,irowp;
21903b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
21913b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
21920513a670SBarry Smith 
21933a40ed3dSBarry Smith   PetscFunctionBegin;
21944c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
219556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
21964c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
219756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
21980513a670SBarry Smith 
21990513a670SBarry Smith   /* determine lengths of permuted rows */
220097f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
22010513a670SBarry Smith   for (i=0; i<m; i++) {
22020513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
22030513a670SBarry Smith   }
22047adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2205f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
22067adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2207ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2208606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
22090513a670SBarry Smith 
221097f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
22110513a670SBarry Smith   for (i=0; i<m; i++) {
221232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
22130513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2214cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
221532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
22160513a670SBarry Smith   }
2217606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
22183c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
22190513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22200513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
222156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
222256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
222356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
222456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
22253a40ed3dSBarry Smith   PetscFunctionReturn(0);
22260513a670SBarry Smith }
22270513a670SBarry Smith 
22284a2ae208SSatish Balay #undef __FUNCT__
22294a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2230dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2231cb5b572fSBarry Smith {
2232dfbe8321SBarry Smith   PetscErrorCode ierr;
2233cb5b572fSBarry Smith 
2234cb5b572fSBarry Smith   PetscFunctionBegin;
223533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
223633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2237be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2238be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2239be6bf707SBarry Smith 
2240700c5bfcSBarry 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");
2241d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2242cb5b572fSBarry Smith   } else {
2243cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2244cb5b572fSBarry Smith   }
2245cb5b572fSBarry Smith   PetscFunctionReturn(0);
2246cb5b572fSBarry Smith }
2247cb5b572fSBarry Smith 
22484a2ae208SSatish Balay #undef __FUNCT__
22494a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2250dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2251273d9f13SBarry Smith {
2252dfbe8321SBarry Smith   PetscErrorCode ierr;
2253273d9f13SBarry Smith 
2254273d9f13SBarry Smith   PetscFunctionBegin;
2255ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2256273d9f13SBarry Smith   PetscFunctionReturn(0);
2257273d9f13SBarry Smith }
2258273d9f13SBarry Smith 
22594a2ae208SSatish Balay #undef __FUNCT__
22604a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2261a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
22626c0721eeSBarry Smith {
22636c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
22646c0721eeSBarry Smith   PetscFunctionBegin;
22656c0721eeSBarry Smith   *array = a->a;
22666c0721eeSBarry Smith   PetscFunctionReturn(0);
22676c0721eeSBarry Smith }
22686c0721eeSBarry Smith 
22694a2ae208SSatish Balay #undef __FUNCT__
22704a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2271dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
22726c0721eeSBarry Smith {
22736c0721eeSBarry Smith   PetscFunctionBegin;
22746c0721eeSBarry Smith   PetscFunctionReturn(0);
22756c0721eeSBarry Smith }
2276273d9f13SBarry Smith 
2277ee4f033dSBarry Smith #undef __FUNCT__
2278ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2279dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2280ee4f033dSBarry Smith {
22816849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
22826849ba73SBarry Smith   PetscErrorCode ierr;
228397f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2284efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
228587828ca2SBarry Smith   PetscScalar    *vscale_array;
2286ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2287ee4f033dSBarry Smith   Vec            w1,w2,w3;
2288ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2289ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2290ee4f033dSBarry Smith 
2291ee4f033dSBarry Smith   PetscFunctionBegin;
2292ee4f033dSBarry Smith   if (!coloring->w1) {
2293ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
229452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2295ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
229652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2297ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
229852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2299ee4f033dSBarry Smith   }
2300ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2301ee4f033dSBarry Smith 
2302ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2303acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2304ee4f033dSBarry Smith   if (flg) {
2305ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2306ee4f033dSBarry Smith   } else {
2307ace3abfcSBarry Smith     PetscBool  assembled;
23080b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
23090b9b6f31SBarry Smith     if (assembled) {
2310ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2311ee4f033dSBarry Smith     }
23120b9b6f31SBarry Smith   }
2313ee4f033dSBarry Smith 
2314ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2315ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2316ee4f033dSBarry Smith 
2317ee4f033dSBarry Smith   /*
2318ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2319ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2320ee4f033dSBarry Smith   */
2321ee4f033dSBarry Smith   if (coloring->F) {
2322ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2323ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2324ee4f033dSBarry Smith     if (m1 != m2) {
2325ee4f033dSBarry Smith       coloring->F = 0;
2326ee4f033dSBarry Smith     }
2327ee4f033dSBarry Smith   }
2328ee4f033dSBarry Smith 
2329ee4f033dSBarry Smith   if (coloring->F) {
2330ee4f033dSBarry Smith     w1          = coloring->F;
2331ee4f033dSBarry Smith     coloring->F = 0;
2332ee4f033dSBarry Smith   } else {
233366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2334ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
233566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2336ee4f033dSBarry Smith   }
2337ee4f033dSBarry Smith 
2338ee4f033dSBarry Smith   /*
2339ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2340ee4f033dSBarry Smith   */
23411ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
23421ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2343ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2344ee4f033dSBarry Smith     /*
2345ee4f033dSBarry Smith        Loop over each column associated with color adding the
2346ee4f033dSBarry Smith        perturbation to the vector w3.
2347ee4f033dSBarry Smith     */
2348ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2349ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2350ee4f033dSBarry Smith       dx  = xx[col];
2351ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2352ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2353ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2354ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2355ee4f033dSBarry Smith #else
2356ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2357ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2358ee4f033dSBarry Smith #endif
2359ee4f033dSBarry Smith       dx                *= epsilon;
2360ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2361ee4f033dSBarry Smith     }
2362ee4f033dSBarry Smith   }
23631ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2364ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2365ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2366ee4f033dSBarry Smith 
2367ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2368ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2369ee4f033dSBarry Smith 
2370ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2371ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2372ee4f033dSBarry Smith 
23731ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2374ee4f033dSBarry Smith   /*
2375ee4f033dSBarry Smith       Loop over each color
2376ee4f033dSBarry Smith   */
2377ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
237849b058dcSBarry Smith     coloring->currentcolor = k;
2379ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
23801ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2381ee4f033dSBarry Smith     /*
2382ee4f033dSBarry Smith        Loop over each column associated with color adding the
2383ee4f033dSBarry Smith        perturbation to the vector w3.
2384ee4f033dSBarry Smith     */
2385ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2386ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2387ee4f033dSBarry Smith       dx  = xx[col];
23885b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2389ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2390ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2391ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2392ee4f033dSBarry Smith #else
2393ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2394ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2395ee4f033dSBarry Smith #endif
2396ee4f033dSBarry Smith       dx            *= epsilon;
2397e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2398ee4f033dSBarry Smith       w3_array[col] += dx;
2399ee4f033dSBarry Smith     }
24001ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2401ee4f033dSBarry Smith 
2402ee4f033dSBarry Smith     /*
2403ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2404ee4f033dSBarry Smith     */
2405ee4f033dSBarry Smith 
240666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2407ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
240866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2409efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2410ee4f033dSBarry Smith 
2411ee4f033dSBarry Smith     /*
2412ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2413ee4f033dSBarry Smith     */
24141ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2415ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2416ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2417ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2418ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2419ee4f033dSBarry Smith       srow   = row + start;
2420ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2421ee4f033dSBarry Smith     }
24221ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2423ee4f033dSBarry Smith   }
242449b058dcSBarry Smith   coloring->currentcolor = k;
24251ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
24261ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2427ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2428ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2429ee4f033dSBarry Smith   PetscFunctionReturn(0);
2430ee4f033dSBarry Smith }
2431ee4f033dSBarry Smith 
24328229c054SShri Abhyankar /*
24338229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
24348229c054SShri Abhyankar    have different nonzero structure.
24358229c054SShri Abhyankar */
2436ac90fabeSBarry Smith #undef __FUNCT__
24378229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
24388229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2439ec7775f6SShri Abhyankar {
24408229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2441ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2442ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2443ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2444ec7775f6SShri Abhyankar 
2445ec7775f6SShri Abhyankar   PetscFunctionBegin;
2446ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2447ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
24488af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
24498af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
24508af7cee1SJed Brown     nnz[i] = 0;
24518af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
24528af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
24538af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
24548af7cee1SJed Brown       nnz[i]++;
24558af7cee1SJed Brown     }
24568af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2457ec7775f6SShri Abhyankar   }
2458ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2459ec7775f6SShri Abhyankar }
2460ec7775f6SShri Abhyankar 
2461ec7775f6SShri Abhyankar #undef __FUNCT__
2462ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2463f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2464ac90fabeSBarry Smith {
2465dfbe8321SBarry Smith   PetscErrorCode ierr;
246697f1f81fSBarry Smith   PetscInt       i;
2467ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
24680805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2469ac90fabeSBarry Smith 
2470ac90fabeSBarry Smith   PetscFunctionBegin;
2471ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2472f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2473f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2474c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2475a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2476a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2477a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2478a30b2313SHong Zhang     }
2479a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2480d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2481a30b2313SHong Zhang       y->XtoY = X;
2482407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2483c537a176SHong Zhang     }
2484f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
24851e2582c4SBarry 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);
2486ac90fabeSBarry Smith   } else {
24878229c054SShri Abhyankar     Mat      B;
24888229c054SShri Abhyankar     PetscInt *nnz;
248916b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2490ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2491bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
24924aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2493ec7775f6SShri Abhyankar     ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
24948229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
24958229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2496ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2497ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
24988229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2499ac90fabeSBarry Smith   }
2500ac90fabeSBarry Smith   PetscFunctionReturn(0);
2501ac90fabeSBarry Smith }
2502ac90fabeSBarry Smith 
2503521d7252SBarry Smith #undef __FUNCT__
2504521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2505521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2506521d7252SBarry Smith {
250741c166b1SJed Brown   PetscErrorCode ierr;
250841c166b1SJed Brown 
2509521d7252SBarry Smith   PetscFunctionBegin;
251041c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
251141c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2512521d7252SBarry Smith   PetscFunctionReturn(0);
2513521d7252SBarry Smith }
2514521d7252SBarry Smith 
2515354c94deSBarry Smith #undef __FUNCT__
2516354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
25177087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2518354c94deSBarry Smith {
2519354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2520354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2521354c94deSBarry Smith   PetscInt    i,nz;
2522354c94deSBarry Smith   PetscScalar *a;
2523354c94deSBarry Smith 
2524354c94deSBarry Smith   PetscFunctionBegin;
2525354c94deSBarry Smith   nz = aij->nz;
2526354c94deSBarry Smith   a  = aij->a;
2527354c94deSBarry Smith   for (i=0; i<nz; i++) {
2528354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2529354c94deSBarry Smith   }
2530354c94deSBarry Smith #else
2531354c94deSBarry Smith   PetscFunctionBegin;
2532354c94deSBarry Smith #endif
2533354c94deSBarry Smith   PetscFunctionReturn(0);
2534354c94deSBarry Smith }
2535354c94deSBarry Smith 
2536e34fafa9SBarry Smith #undef __FUNCT__
2537985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2538985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2539e34fafa9SBarry Smith {
2540e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2541e34fafa9SBarry Smith   PetscErrorCode ierr;
2542d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2543e34fafa9SBarry Smith   PetscReal      atmp;
2544985db425SBarry Smith   PetscScalar    *x;
2545e34fafa9SBarry Smith   MatScalar      *aa;
2546e34fafa9SBarry Smith 
2547e34fafa9SBarry Smith   PetscFunctionBegin;
2548e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2549e34fafa9SBarry Smith   aa   = a->a;
2550e34fafa9SBarry Smith   ai   = a->i;
2551e34fafa9SBarry Smith   aj   = a->j;
2552e34fafa9SBarry Smith 
2553985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2554e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2555e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2556e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2557e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2558e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
25599189402eSHong Zhang     x[i] = 0.0;
2560e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2561985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2562985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2563985db425SBarry Smith       aa++; aj++;
2564985db425SBarry Smith     }
2565985db425SBarry Smith   }
2566985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2567985db425SBarry Smith   PetscFunctionReturn(0);
2568985db425SBarry Smith }
2569985db425SBarry Smith 
2570985db425SBarry Smith #undef __FUNCT__
2571985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2572985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2573985db425SBarry Smith {
2574985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2575985db425SBarry Smith   PetscErrorCode ierr;
2576d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2577985db425SBarry Smith   PetscScalar    *x;
2578985db425SBarry Smith   MatScalar      *aa;
2579985db425SBarry Smith 
2580985db425SBarry Smith   PetscFunctionBegin;
2581e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2582985db425SBarry Smith   aa   = a->a;
2583985db425SBarry Smith   ai   = a->i;
2584985db425SBarry Smith   aj   = a->j;
2585985db425SBarry Smith 
2586985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2587985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2588985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2589e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2590985db425SBarry Smith   for (i=0; i<m; i++) {
2591985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2592d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2593985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2594985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2595985db425SBarry Smith       x[i] = 0.0;
2596985db425SBarry Smith       if (idx) {
2597985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2598985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2599985db425SBarry Smith           if (aj[j] > j) {
2600985db425SBarry Smith             idx[i] = j;
2601985db425SBarry Smith             break;
2602985db425SBarry Smith           }
2603985db425SBarry Smith         }
2604985db425SBarry Smith       }
2605985db425SBarry Smith     }
2606985db425SBarry Smith     for (j=0; j<ncols; j++){
2607985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2608985db425SBarry Smith       aa++; aj++;
2609985db425SBarry Smith     }
2610985db425SBarry Smith   }
2611985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2612985db425SBarry Smith   PetscFunctionReturn(0);
2613985db425SBarry Smith }
2614985db425SBarry Smith 
2615985db425SBarry Smith #undef __FUNCT__
2616c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2617c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2618c87e5d42SMatthew Knepley {
2619c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2620c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2621c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2622c87e5d42SMatthew Knepley   PetscReal      atmp;
2623c87e5d42SMatthew Knepley   PetscScalar    *x;
2624c87e5d42SMatthew Knepley   MatScalar      *aa;
2625c87e5d42SMatthew Knepley 
2626c87e5d42SMatthew Knepley   PetscFunctionBegin;
2627e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2628c87e5d42SMatthew Knepley   aa   = a->a;
2629c87e5d42SMatthew Knepley   ai   = a->i;
2630c87e5d42SMatthew Knepley   aj   = a->j;
2631c87e5d42SMatthew Knepley 
2632c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2633c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2634c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2635e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2636c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2637c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2638289a08f5SMatthew Knepley     if (ncols) {
2639289a08f5SMatthew Knepley       /* Get first nonzero */
2640289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2641289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2642289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2643289a08f5SMatthew Knepley       }
2644289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2645289a08f5SMatthew Knepley     } else {
2646289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2647289a08f5SMatthew Knepley     }
2648c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2649c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2650289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2651c87e5d42SMatthew Knepley       aa++; aj++;
2652c87e5d42SMatthew Knepley     }
2653c87e5d42SMatthew Knepley   }
2654c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2655c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2656c87e5d42SMatthew Knepley }
2657c87e5d42SMatthew Knepley 
2658c87e5d42SMatthew Knepley #undef __FUNCT__
2659985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2660985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2661985db425SBarry Smith {
2662985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2663985db425SBarry Smith   PetscErrorCode ierr;
2664d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2665985db425SBarry Smith   PetscScalar    *x;
2666985db425SBarry Smith   MatScalar      *aa;
2667985db425SBarry Smith 
2668985db425SBarry Smith   PetscFunctionBegin;
2669e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2670985db425SBarry Smith   aa   = a->a;
2671985db425SBarry Smith   ai   = a->i;
2672985db425SBarry Smith   aj   = a->j;
2673985db425SBarry Smith 
2674985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2675985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2676985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2677e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2678985db425SBarry Smith   for (i=0; i<m; i++) {
2679985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2680d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2681985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2682985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2683985db425SBarry Smith       x[i] = 0.0;
2684985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2685985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2686985db425SBarry Smith         for (j=0;j<ncols;j++) {
2687985db425SBarry Smith           if (aj[j] > j) {
2688985db425SBarry Smith             idx[i] = j;
2689985db425SBarry Smith             break;
2690985db425SBarry Smith           }
2691985db425SBarry Smith         }
2692985db425SBarry Smith       }
2693985db425SBarry Smith     }
2694985db425SBarry Smith     for (j=0; j<ncols; j++){
2695985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2696985db425SBarry Smith       aa++; aj++;
2697e34fafa9SBarry Smith     }
2698e34fafa9SBarry Smith   }
2699e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2700e34fafa9SBarry Smith   PetscFunctionReturn(0);
2701e34fafa9SBarry Smith }
27027087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2703682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
27040a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2705cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2706cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2707cb5b572fSBarry Smith        MatMult_SeqAIJ,
270897304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
27097c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
27107c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2711db4efbfdSBarry Smith        0,
2712db4efbfdSBarry Smith        0,
2713db4efbfdSBarry Smith        0,
2714db4efbfdSBarry Smith /*10*/ 0,
2715cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2716cb5b572fSBarry Smith        0,
271741f059aeSBarry Smith        MatSOR_SeqAIJ,
271817ab2063SBarry Smith        MatTranspose_SeqAIJ,
271997304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2720cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2721cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2722cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2723cb5b572fSBarry Smith        MatNorm_SeqAIJ,
272497304618SKris Buschelman /*20*/ 0,
2725cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2726cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2727cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2728d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2729db4efbfdSBarry Smith        0,
2730db4efbfdSBarry Smith        0,
2731db4efbfdSBarry Smith        0,
2732db4efbfdSBarry Smith        0,
2733d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2734db4efbfdSBarry Smith        0,
2735db4efbfdSBarry Smith        0,
27366c0721eeSBarry Smith        MatGetArray_SeqAIJ,
27376c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2738d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2739cb5b572fSBarry Smith        0,
2740cb5b572fSBarry Smith        0,
2741cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2742cb5b572fSBarry Smith        0,
2743d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2744cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2745cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2746cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2747cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2748d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2749cb5b572fSBarry Smith        MatScale_SeqAIJ,
2750cb5b572fSBarry Smith        0,
275179299369SBarry Smith        MatDiagonalSet_SeqAIJ,
27526e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
2753d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
27543b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
27553b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
27563b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2757a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2758d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2759b9617806SBarry Smith        0,
27600513a670SBarry Smith        0,
2761cda55fadSBarry Smith        MatPermute_SeqAIJ,
2762cda55fadSBarry Smith        0,
2763d519adbfSMatthew Knepley /*59*/ 0,
2764b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2765b9b97703SBarry Smith        MatView_SeqAIJ,
2766357abbc8SBarry Smith        0,
2767ee4f033dSBarry Smith        0,
2768d519adbfSMatthew Knepley /*64*/ 0,
2769ee4f033dSBarry Smith        0,
2770ee4f033dSBarry Smith        0,
2771ee4f033dSBarry Smith        0,
2772ee4f033dSBarry Smith        0,
2773d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2774c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2775ee4f033dSBarry Smith        0,
2776ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2777dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2778ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2779dcf5cc72SBarry Smith #else
2780dcf5cc72SBarry Smith        0,
2781dcf5cc72SBarry Smith #endif
2782d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
27833acb8795SBarry Smith        MatFDColoringApply_AIJ,
278497304618SKris Buschelman        0,
278597304618SKris Buschelman        0,
278697304618SKris Buschelman        0,
2787*6ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
278897304618SKris Buschelman        0,
278997304618SKris Buschelman        0,
279097304618SKris Buschelman        0,
2791bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2792d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
27931cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
27946284ec50SHong Zhang        0,
27956284ec50SHong Zhang        0,
2796bc011b1eSHong Zhang        0,
2797d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
279826be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
279926be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2800d439da42SKris Buschelman        MatPtAP_Basic,
28017ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2802d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2803bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2804bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2805bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
28067ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2807d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2808609c6c4dSKris Buschelman        0,
2809609c6c4dSKris Buschelman        0,
281087d4246cSBarry Smith        MatConjugate_SeqAIJ,
281187d4246cSBarry Smith        0,
2812d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
281399cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2814f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2815f5edf698SHong Zhang        0,
28162bebee5dSHong Zhang        0,
2817d519adbfSMatthew Knepley /*109*/0,
2818985db425SBarry Smith        0,
28192af78befSBarry Smith        MatGetRowMin_SeqAIJ,
28202af78befSBarry Smith        0,
2821599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2822d519adbfSMatthew Knepley /*114*/0,
2823599ef60dSHong Zhang        0,
28243c2a7987SHong Zhang        0,
2825fe97e370SBarry Smith        0,
2826fbdbba38SShri Abhyankar        0,
2827fbdbba38SShri Abhyankar /*119*/0,
2828fbdbba38SShri Abhyankar        0,
2829fbdbba38SShri Abhyankar        0,
283082d44351SHong Zhang        0,
2831b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
2832b3a44c85SBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ
28339e29f15eSvictorle };
283417ab2063SBarry Smith 
2835fb2e594dSBarry Smith EXTERN_C_BEGIN
28364a2ae208SSatish Balay #undef __FUNCT__
28374a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
28387087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2839bef8e0ddSBarry Smith {
2840bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
284197f1f81fSBarry Smith   PetscInt   i,nz,n;
2842bef8e0ddSBarry Smith 
2843bef8e0ddSBarry Smith   PetscFunctionBegin;
2844bef8e0ddSBarry Smith 
2845bef8e0ddSBarry Smith   nz = aij->maxnz;
2846d0f46423SBarry Smith   n  = mat->rmap->n;
2847bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2848bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2849bef8e0ddSBarry Smith   }
2850bef8e0ddSBarry Smith   aij->nz = nz;
2851bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2852bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2853bef8e0ddSBarry Smith   }
2854bef8e0ddSBarry Smith 
2855bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2856bef8e0ddSBarry Smith }
2857fb2e594dSBarry Smith EXTERN_C_END
2858bef8e0ddSBarry Smith 
28594a2ae208SSatish Balay #undef __FUNCT__
28604a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2861bef8e0ddSBarry Smith /*@
2862bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2863bef8e0ddSBarry Smith        in the matrix.
2864bef8e0ddSBarry Smith 
2865bef8e0ddSBarry Smith   Input Parameters:
2866bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2867bef8e0ddSBarry Smith -  indices - the column indices
2868bef8e0ddSBarry Smith 
286915091d37SBarry Smith   Level: advanced
287015091d37SBarry Smith 
2871bef8e0ddSBarry Smith   Notes:
2872bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2873bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2874bef8e0ddSBarry Smith   of the MatSetValues() operation.
2875bef8e0ddSBarry Smith 
2876bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2877d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2878bef8e0ddSBarry Smith 
2879bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2880bef8e0ddSBarry Smith 
2881b9617806SBarry Smith     The indices should start with zero, not one.
2882b9617806SBarry Smith 
2883bef8e0ddSBarry Smith @*/
28847087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2885bef8e0ddSBarry Smith {
28864ac538c5SBarry Smith   PetscErrorCode ierr;
2887bef8e0ddSBarry Smith 
2888bef8e0ddSBarry Smith   PetscFunctionBegin;
28890700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
28904482741eSBarry Smith   PetscValidPointer(indices,2);
28914ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
2892bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2893bef8e0ddSBarry Smith }
2894bef8e0ddSBarry Smith 
2895be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2896be6bf707SBarry Smith 
2897fb2e594dSBarry Smith EXTERN_C_BEGIN
28984a2ae208SSatish Balay #undef __FUNCT__
28994a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
29007087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
2901be6bf707SBarry Smith {
2902be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
29036849ba73SBarry Smith   PetscErrorCode ierr;
2904d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2905be6bf707SBarry Smith 
2906be6bf707SBarry Smith   PetscFunctionBegin;
2907be6bf707SBarry Smith   if (aij->nonew != 1) {
2908e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2909be6bf707SBarry Smith   }
2910be6bf707SBarry Smith 
2911be6bf707SBarry Smith   /* allocate space for values if not already there */
2912be6bf707SBarry Smith   if (!aij->saved_values) {
291387828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
29149518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2915be6bf707SBarry Smith   }
2916be6bf707SBarry Smith 
2917be6bf707SBarry Smith   /* copy values over */
291887828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2919be6bf707SBarry Smith   PetscFunctionReturn(0);
2920be6bf707SBarry Smith }
2921fb2e594dSBarry Smith EXTERN_C_END
2922be6bf707SBarry Smith 
29234a2ae208SSatish Balay #undef __FUNCT__
2924b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2925be6bf707SBarry Smith /*@
2926be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2927be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2928be6bf707SBarry Smith        nonlinear portion.
2929be6bf707SBarry Smith 
2930be6bf707SBarry Smith    Collect on Mat
2931be6bf707SBarry Smith 
2932be6bf707SBarry Smith   Input Parameters:
29330e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2934be6bf707SBarry Smith 
293515091d37SBarry Smith   Level: advanced
293615091d37SBarry Smith 
2937be6bf707SBarry Smith   Common Usage, with SNESSolve():
2938be6bf707SBarry Smith $    Create Jacobian matrix
2939be6bf707SBarry Smith $    Set linear terms into matrix
2940be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2941be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2942be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2943512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2944be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2945be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2946be6bf707SBarry Smith $    In your Jacobian routine
2947be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2948be6bf707SBarry Smith $      Set nonlinear terms in matrix
2949be6bf707SBarry Smith 
2950be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2951be6bf707SBarry Smith $    // build linear portion of Jacobian
2952512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2953be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2954be6bf707SBarry Smith $    loop over nonlinear iterations
2955be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2956be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2957be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2958be6bf707SBarry Smith $       Solve linear system with Jacobian
2959be6bf707SBarry Smith $    endloop
2960be6bf707SBarry Smith 
2961be6bf707SBarry Smith   Notes:
2962be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2963512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2964be6bf707SBarry Smith     calling this routine.
2965be6bf707SBarry Smith 
29660c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
29670c468ba9SBarry Smith     and does not allocated additional space.
29680c468ba9SBarry Smith 
2969be6bf707SBarry Smith .seealso: MatRetrieveValues()
2970be6bf707SBarry Smith 
2971be6bf707SBarry Smith @*/
29727087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
2973be6bf707SBarry Smith {
29744ac538c5SBarry Smith   PetscErrorCode ierr;
2975be6bf707SBarry Smith 
2976be6bf707SBarry Smith   PetscFunctionBegin;
29770700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2978e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2979e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
29804ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
2981be6bf707SBarry Smith   PetscFunctionReturn(0);
2982be6bf707SBarry Smith }
2983be6bf707SBarry Smith 
2984fb2e594dSBarry Smith EXTERN_C_BEGIN
29854a2ae208SSatish Balay #undef __FUNCT__
29864a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
29877087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
2988be6bf707SBarry Smith {
2989be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
29906849ba73SBarry Smith   PetscErrorCode ierr;
2991d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2992be6bf707SBarry Smith 
2993be6bf707SBarry Smith   PetscFunctionBegin;
2994be6bf707SBarry Smith   if (aij->nonew != 1) {
2995e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2996be6bf707SBarry Smith   }
2997be6bf707SBarry Smith   if (!aij->saved_values) {
2998e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2999be6bf707SBarry Smith   }
3000be6bf707SBarry Smith   /* copy values over */
300187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3002be6bf707SBarry Smith   PetscFunctionReturn(0);
3003be6bf707SBarry Smith }
3004fb2e594dSBarry Smith EXTERN_C_END
3005be6bf707SBarry Smith 
30064a2ae208SSatish Balay #undef __FUNCT__
30074a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3008be6bf707SBarry Smith /*@
3009be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3010be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3011be6bf707SBarry Smith        nonlinear portion.
3012be6bf707SBarry Smith 
3013be6bf707SBarry Smith    Collect on Mat
3014be6bf707SBarry Smith 
3015be6bf707SBarry Smith   Input Parameters:
3016be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3017be6bf707SBarry Smith 
301815091d37SBarry Smith   Level: advanced
301915091d37SBarry Smith 
3020be6bf707SBarry Smith .seealso: MatStoreValues()
3021be6bf707SBarry Smith 
3022be6bf707SBarry Smith @*/
30237087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3024be6bf707SBarry Smith {
30254ac538c5SBarry Smith   PetscErrorCode ierr;
3026be6bf707SBarry Smith 
3027be6bf707SBarry Smith   PetscFunctionBegin;
30280700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3029e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3030e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
30314ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3032be6bf707SBarry Smith   PetscFunctionReturn(0);
3033be6bf707SBarry Smith }
3034be6bf707SBarry Smith 
3035f83d6046SBarry Smith 
3036be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
30374a2ae208SSatish Balay #undef __FUNCT__
30384a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
303917ab2063SBarry Smith /*@C
3040682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
30410d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
30426e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
304351c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
30442bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
304517ab2063SBarry Smith 
3046db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3047db81eaa0SLois Curfman McInnes 
304817ab2063SBarry Smith    Input Parameters:
3049db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
305017ab2063SBarry Smith .  m - number of rows
305117ab2063SBarry Smith .  n - number of columns
305217ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
305351c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
30542bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
305517ab2063SBarry Smith 
305617ab2063SBarry Smith    Output Parameter:
3057416022c9SBarry Smith .  A - the matrix
305817ab2063SBarry Smith 
3059175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3060ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3061175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3062175b88e8SBarry Smith 
3063b259b22eSLois Curfman McInnes    Notes:
306449a6f317SBarry Smith    If nnz is given then nz is ignored
306549a6f317SBarry Smith 
306617ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
306717ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
30680002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
306944cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
307017ab2063SBarry Smith 
307117ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3072a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
30733d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
30746da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
307517ab2063SBarry Smith 
3076682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
30774fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3078682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
30796c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
30806c7ebb05SLois Curfman McInnes 
30816c7ebb05SLois Curfman McInnes    Options Database Keys:
3082698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
30839db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
308417ab2063SBarry Smith 
3085027ccd11SLois Curfman McInnes    Level: intermediate
3086027ccd11SLois Curfman McInnes 
308736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
308836db0b34SBarry Smith 
308917ab2063SBarry Smith @*/
30907087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
309117ab2063SBarry Smith {
3092dfbe8321SBarry Smith   PetscErrorCode ierr;
30936945ee14SBarry Smith 
30943a40ed3dSBarry Smith   PetscFunctionBegin;
3095f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3096117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3097c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3098d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3099273d9f13SBarry Smith   PetscFunctionReturn(0);
3100273d9f13SBarry Smith }
3101273d9f13SBarry Smith 
31024a2ae208SSatish Balay #undef __FUNCT__
31034a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3104273d9f13SBarry Smith /*@C
3105273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3106273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3107273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3108273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3109273d9f13SBarry Smith 
3110273d9f13SBarry Smith    Collective on MPI_Comm
3111273d9f13SBarry Smith 
3112273d9f13SBarry Smith    Input Parameters:
3113117016b1SBarry Smith +  B - The matrix-free
3114273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3115273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3116273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3117273d9f13SBarry Smith 
3118273d9f13SBarry Smith    Notes:
311949a6f317SBarry Smith      If nnz is given then nz is ignored
312049a6f317SBarry Smith 
3121273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3122273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3123273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3124273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3125273d9f13SBarry Smith 
3126273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3127273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3128273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3129273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3130273d9f13SBarry Smith 
3131aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3132aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3133aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3134aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3135aa95bbe8SBarry Smith 
3136a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3137a96a251dSBarry Smith    entries or columns indices
3138a96a251dSBarry Smith 
3139273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3140273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3141273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3142273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3143273d9f13SBarry Smith 
3144273d9f13SBarry Smith    Options Database Keys:
3145698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3146698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3147273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3148273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3149273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3150273d9f13SBarry Smith 
3151273d9f13SBarry Smith    Level: intermediate
3152273d9f13SBarry Smith 
3153aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3154273d9f13SBarry Smith 
3155273d9f13SBarry Smith @*/
31567087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3157273d9f13SBarry Smith {
31584ac538c5SBarry Smith   PetscErrorCode ierr;
3159a23d5eceSKris Buschelman 
3160a23d5eceSKris Buschelman   PetscFunctionBegin;
31614ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3162a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3163a23d5eceSKris Buschelman }
3164a23d5eceSKris Buschelman 
3165a23d5eceSKris Buschelman EXTERN_C_BEGIN
3166a23d5eceSKris Buschelman #undef __FUNCT__
3167a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
31687087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3169a23d5eceSKris Buschelman {
3170273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3171ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
31726849ba73SBarry Smith   PetscErrorCode ierr;
317397f1f81fSBarry Smith   PetscInt       i;
3174273d9f13SBarry Smith 
3175273d9f13SBarry Smith   PetscFunctionBegin;
3176d5d45c9bSBarry Smith 
3177a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3178c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3179c461c341SBarry Smith     nz             = 0;
3180c461c341SBarry Smith   }
3181c461c341SBarry Smith 
318226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
318326283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
318426283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
318526283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3186899cda47SBarry Smith 
3187435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3188e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3189b73539f3SBarry Smith   if (nnz) {
3190d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3191e32f2f54SBarry 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]);
3192e32f2f54SBarry 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);
3193b73539f3SBarry Smith     }
3194b73539f3SBarry Smith   }
3195b73539f3SBarry Smith 
3196273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3197273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3198273d9f13SBarry Smith 
3199ab93d7beSBarry Smith   if (!skipallocation) {
32002ee49352SLisandro Dalcin     if (!b->imax) {
3201d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3202d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
32032ee49352SLisandro Dalcin     }
3204273d9f13SBarry Smith     if (!nnz) {
3205435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3206c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3207d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3208d0f46423SBarry Smith       nz = nz*B->rmap->n;
3209273d9f13SBarry Smith     } else {
3210273d9f13SBarry Smith       nz = 0;
3211d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3212273d9f13SBarry Smith     }
3213ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3214d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3215ab93d7beSBarry Smith 
3216273d9f13SBarry Smith     /* allocate the matrix space */
32172ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3218d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3219d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3220bfeeae90SHong Zhang     b->i[0] = 0;
3221d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
32225da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
32235da197adSKris Buschelman     }
3224273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3225e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3226e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3227c461c341SBarry Smith   } else {
3228e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3229e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3230c461c341SBarry Smith   }
3231273d9f13SBarry Smith 
3232273d9f13SBarry Smith   b->nz                = 0;
3233273d9f13SBarry Smith   b->maxnz             = nz;
3234273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3235273d9f13SBarry Smith   PetscFunctionReturn(0);
3236273d9f13SBarry Smith }
3237a23d5eceSKris Buschelman EXTERN_C_END
3238273d9f13SBarry Smith 
3239a1661176SMatthew Knepley #undef  __FUNCT__
3240a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
324158d36128SBarry Smith /*@
3242a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3243a1661176SMatthew Knepley 
3244a1661176SMatthew Knepley    Input Parameters:
3245a1661176SMatthew Knepley +  B - the matrix
3246a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3247a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3248a1661176SMatthew Knepley -  v - optional values in the matrix
3249a1661176SMatthew Knepley 
3250a1661176SMatthew Knepley    Level: developer
3251a1661176SMatthew Knepley 
325258d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
325358d36128SBarry Smith 
3254a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3255a1661176SMatthew Knepley 
3256a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3257a1661176SMatthew Knepley @*/
3258a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3259a1661176SMatthew Knepley {
3260a1661176SMatthew Knepley   PetscErrorCode ierr;
3261a1661176SMatthew Knepley 
3262a1661176SMatthew Knepley   PetscFunctionBegin;
32630700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
32644ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3265a1661176SMatthew Knepley   PetscFunctionReturn(0);
3266a1661176SMatthew Knepley }
3267a1661176SMatthew Knepley 
3268a1661176SMatthew Knepley EXTERN_C_BEGIN
3269a1661176SMatthew Knepley #undef  __FUNCT__
3270a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
32717087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3272a1661176SMatthew Knepley {
3273a1661176SMatthew Knepley   PetscInt       i;
3274a1661176SMatthew Knepley   PetscInt       m,n;
3275a1661176SMatthew Knepley   PetscInt       nz;
3276a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3277a1661176SMatthew Knepley   PetscScalar    *values;
3278a1661176SMatthew Knepley   PetscErrorCode ierr;
3279a1661176SMatthew Knepley 
3280a1661176SMatthew Knepley   PetscFunctionBegin;
3281a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3282a1661176SMatthew Knepley 
328365e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3284a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3285a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3286b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3287a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
328865e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3289a1661176SMatthew Knepley     nnz[i] = nz;
3290a1661176SMatthew Knepley   }
3291a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3292a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3293a1661176SMatthew Knepley 
3294a1661176SMatthew Knepley   if (v) {
3295a1661176SMatthew Knepley     values = (PetscScalar*) v;
3296a1661176SMatthew Knepley   } else {
32970e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3298a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3299a1661176SMatthew Knepley   }
3300a1661176SMatthew Knepley 
3301a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3302b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3303b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3304a1661176SMatthew Knepley   }
3305a1661176SMatthew Knepley 
3306a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3307a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3308a1661176SMatthew Knepley 
3309a1661176SMatthew Knepley   if (!v) {
3310a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3311a1661176SMatthew Knepley   }
3312a1661176SMatthew Knepley   PetscFunctionReturn(0);
3313a1661176SMatthew Knepley }
3314a1661176SMatthew Knepley EXTERN_C_END
3315a1661176SMatthew Knepley 
33167c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3317be7314b0SBarry Smith #include "private/petscaxpy.h"
3318170fe5c8SBarry Smith 
3319170fe5c8SBarry Smith #undef __FUNCT__
3320170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3321170fe5c8SBarry Smith /*
3322170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3323170fe5c8SBarry Smith 
3324170fe5c8SBarry Smith                n                       p                          p
3325170fe5c8SBarry Smith         (              )       (              )         (                  )
3326170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3327170fe5c8SBarry Smith         (              )       (              )         (                  )
3328170fe5c8SBarry Smith 
3329170fe5c8SBarry Smith */
3330170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3331170fe5c8SBarry Smith {
3332170fe5c8SBarry Smith   PetscErrorCode     ierr;
3333170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3334170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3335170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
33361de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3337170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3338170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3339170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3340170fe5c8SBarry Smith 
3341170fe5c8SBarry Smith   PetscFunctionBegin;
3342d0f46423SBarry Smith   m = A->rmap->n;
3343d0f46423SBarry Smith   n = A->cmap->n;
3344d0f46423SBarry Smith   p = B->cmap->n;
3345170fe5c8SBarry Smith   a = sub_a->v;
3346170fe5c8SBarry Smith   b = sub_b->a;
3347170fe5c8SBarry Smith   c = sub_c->v;
3348170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3349170fe5c8SBarry Smith 
3350170fe5c8SBarry Smith   ii  = sub_b->i;
3351170fe5c8SBarry Smith   idx = sub_b->j;
3352170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3353170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3354170fe5c8SBarry Smith     while (q-->0) {
3355170fe5c8SBarry Smith       c_q = c + m*(*idx);
3356170fe5c8SBarry Smith       a_q = a + m*i;
3357be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3358170fe5c8SBarry Smith       idx++;
3359170fe5c8SBarry Smith       b++;
3360170fe5c8SBarry Smith     }
3361170fe5c8SBarry Smith   }
3362170fe5c8SBarry Smith   PetscFunctionReturn(0);
3363170fe5c8SBarry Smith }
3364170fe5c8SBarry Smith 
3365170fe5c8SBarry Smith #undef __FUNCT__
3366170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3367170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3368170fe5c8SBarry Smith {
3369170fe5c8SBarry Smith   PetscErrorCode ierr;
3370d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3371170fe5c8SBarry Smith   Mat            Cmat;
3372170fe5c8SBarry Smith 
3373170fe5c8SBarry Smith   PetscFunctionBegin;
3374e32f2f54SBarry 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);
337539804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3376170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3377170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3378170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3379170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3380170fe5c8SBarry Smith   *C = Cmat;
3381170fe5c8SBarry Smith   PetscFunctionReturn(0);
3382170fe5c8SBarry Smith }
3383170fe5c8SBarry Smith 
3384170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3385170fe5c8SBarry Smith #undef __FUNCT__
3386170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3387170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3388170fe5c8SBarry Smith {
3389170fe5c8SBarry Smith   PetscErrorCode ierr;
3390170fe5c8SBarry Smith 
3391170fe5c8SBarry Smith   PetscFunctionBegin;
3392170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3393170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3394170fe5c8SBarry Smith   }
3395170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3396170fe5c8SBarry Smith   PetscFunctionReturn(0);
3397170fe5c8SBarry Smith }
3398170fe5c8SBarry Smith 
3399170fe5c8SBarry Smith 
34000bad9183SKris Buschelman /*MC
3401fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
34020bad9183SKris Buschelman    based on compressed sparse row format.
34030bad9183SKris Buschelman 
34040bad9183SKris Buschelman    Options Database Keys:
34050bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
34060bad9183SKris Buschelman 
34070bad9183SKris Buschelman   Level: beginner
34080bad9183SKris Buschelman 
3409f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
34100bad9183SKris Buschelman M*/
34110bad9183SKris Buschelman 
3412a6175056SHong Zhang EXTERN_C_BEGIN
3413b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3414b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3415b5e56a35SBarry Smith #endif
341665460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3417af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3418af1023dbSSatish Balay #endif
34197087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
34207087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
34217087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
34227087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3423611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
34247087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3425611f576cSBarry Smith #endif
3426611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
34277087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3428611f576cSBarry Smith #endif
3429f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3430f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3431f3c0ef26SHong Zhang #endif
3432611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
34337087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3434611f576cSBarry Smith #endif
3435eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
34367087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3437eb3b5408SSatish Balay #endif
3438586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
34397087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3440586621ddSJed Brown #endif
3441719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
34427087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3443719d5645SBarry Smith #endif
3444b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
34457087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
34467087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
34477087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3448b3866ffcSBarry Smith #endif
344917667f90SBarry Smith EXTERN_C_END
345017667f90SBarry Smith 
3451b24902e0SBarry Smith 
345217667f90SBarry Smith EXTERN_C_BEGIN
34534a2ae208SSatish Balay #undef __FUNCT__
34544a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
34557087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3456273d9f13SBarry Smith {
3457273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3458dfbe8321SBarry Smith   PetscErrorCode ierr;
345938baddfdSBarry Smith   PetscMPIInt    size;
3460273d9f13SBarry Smith 
3461273d9f13SBarry Smith   PetscFunctionBegin;
34627adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3463e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3464273d9f13SBarry Smith 
346538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3466b0a32e0cSBarry Smith   B->data             = (void*)b;
3467549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3468416022c9SBarry Smith   b->row              = 0;
3469416022c9SBarry Smith   b->col              = 0;
347082bf6240SBarry Smith   b->icol             = 0;
3471b810aeb4SBarry Smith   b->reallocs         = 0;
347236db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3473f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3474416022c9SBarry Smith   b->nonew             = 0;
3475416022c9SBarry Smith   b->diag              = 0;
3476416022c9SBarry Smith   b->solve_work        = 0;
34772a1b7f2aSHong Zhang   B->spptr             = 0;
3478be6bf707SBarry Smith   b->saved_values      = 0;
3479d7f994e1SBarry Smith   b->idiag             = 0;
348071f1c65dSBarry Smith   b->mdiag             = 0;
348171f1c65dSBarry Smith   b->ssor_work         = 0;
348271f1c65dSBarry Smith   b->omega             = 1.0;
348371f1c65dSBarry Smith   b->fshift            = 0.0;
348471f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3485a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3486a30b2313SHong Zhang   b->xtoy              = 0;
3487a30b2313SHong Zhang   b->XtoY              = 0;
348888e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
348917ab2063SBarry Smith 
349035d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3491b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3492700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3493b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3494b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3495b3866ffcSBarry Smith #endif
3496b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3497700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3498b5e56a35SBarry Smith #endif
349965460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3500700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3501719d5645SBarry Smith #endif
3502611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3503700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3504611f576cSBarry Smith #endif
3505f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3506700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3507f3c0ef26SHong Zhang #endif
3508611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3509700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3510611f576cSBarry Smith #endif
3511611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3512700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3513611f576cSBarry Smith #endif
3514eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3515700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3516eb3b5408SSatish Balay #endif
3517586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3518700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3519586621ddSJed Brown #endif
3520719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3521700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3522719d5645SBarry Smith #endif
3523700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3524700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3525700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3526700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3527700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3528700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3529700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3530700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3531700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3532700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3533700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3534700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3535700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3536700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3537700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3538700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3539700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3540700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
35414108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
354217667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
35433a40ed3dSBarry Smith   PetscFunctionReturn(0);
354417ab2063SBarry Smith }
3545273d9f13SBarry Smith EXTERN_C_END
354617ab2063SBarry Smith 
35474a2ae208SSatish Balay #undef __FUNCT__
3548b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3549b24902e0SBarry Smith /*
3550b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3551b24902e0SBarry Smith */
3552ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
355317ab2063SBarry Smith {
3554416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
35556849ba73SBarry Smith   PetscErrorCode ierr;
3556d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
355717ab2063SBarry Smith 
35583a40ed3dSBarry Smith   PetscFunctionBegin;
3559273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3560273d9f13SBarry Smith 
3561d5f3da31SBarry Smith   C->factortype     = A->factortype;
3562416022c9SBarry Smith   c->row            = 0;
3563416022c9SBarry Smith   c->col            = 0;
356482bf6240SBarry Smith   c->icol           = 0;
35656ad4291fSHong Zhang   c->reallocs       = 0;
356617ab2063SBarry Smith 
35676ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
356817ab2063SBarry Smith 
356926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
357026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
357126283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
357226283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3573eec197d1SBarry Smith 
357433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
35759518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
357617ab2063SBarry Smith   for (i=0; i<m; i++) {
3577416022c9SBarry Smith     c->imax[i] = a->imax[i];
3578416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
357917ab2063SBarry Smith   }
358017ab2063SBarry Smith 
358117ab2063SBarry Smith   /* allocate the matrix space */
3582f77e22a1SHong Zhang   if (mallocmatspace){
3583a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
35849518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3585f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
358697f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
358717ab2063SBarry Smith     if (m > 0) {
358897f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3589be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3590bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3591be6bf707SBarry Smith       } else {
3592bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
359317ab2063SBarry Smith       }
359408480c60SBarry Smith     }
3595f77e22a1SHong Zhang   }
359617ab2063SBarry Smith 
35976ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3598416022c9SBarry Smith   c->roworiented       = a->roworiented;
3599416022c9SBarry Smith   c->nonew             = a->nonew;
3600416022c9SBarry Smith   if (a->diag) {
360197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
360252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
360317ab2063SBarry Smith     for (i=0; i<m; i++) {
3604416022c9SBarry Smith       c->diag[i] = a->diag[i];
360517ab2063SBarry Smith     }
36063a40ed3dSBarry Smith   } else c->diag           = 0;
36076ad4291fSHong Zhang   c->solve_work            = 0;
36086ad4291fSHong Zhang   c->saved_values          = 0;
36096ad4291fSHong Zhang   c->idiag                 = 0;
361071f1c65dSBarry Smith   c->ssor_work             = 0;
3611a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3612e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3613e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
36146ad4291fSHong Zhang   c->xtoy                  = 0;
36156ad4291fSHong Zhang   c->XtoY                  = 0;
36166ad4291fSHong Zhang 
3617416022c9SBarry Smith   c->nz                 = a->nz;
36188ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3619273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3620754ec7b1SSatish Balay 
36216ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
36226ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3623cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3624cd6b891eSBarry Smith   if (a->compressedrow.use){
36256ad4291fSHong Zhang     i = a->compressedrow.nrows;
36260e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
36276ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
36286ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
362927ea64f8SHong Zhang   } else {
363027ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
363127ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
363227ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
36336ad4291fSHong Zhang   }
363488e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
36354108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
36364846f1f5SKris Buschelman 
36377adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
36383a40ed3dSBarry Smith   PetscFunctionReturn(0);
363917ab2063SBarry Smith }
364017ab2063SBarry Smith 
36414a2ae208SSatish Balay #undef __FUNCT__
3642b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3643b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3644b24902e0SBarry Smith {
3645b24902e0SBarry Smith   PetscErrorCode ierr;
3646b24902e0SBarry Smith 
3647b24902e0SBarry Smith   PetscFunctionBegin;
3648b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
36494b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3650b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3651f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3652b24902e0SBarry Smith   PetscFunctionReturn(0);
3653b24902e0SBarry Smith }
3654b24902e0SBarry Smith 
3655b24902e0SBarry Smith #undef __FUNCT__
36564a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3657112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3658fbdbba38SShri Abhyankar {
3659fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3660fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3661fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3662fbdbba38SShri Abhyankar   int            fd;
3663fbdbba38SShri Abhyankar   PetscMPIInt    size;
3664fbdbba38SShri Abhyankar   MPI_Comm       comm;
3665fbdbba38SShri Abhyankar 
3666fbdbba38SShri Abhyankar   PetscFunctionBegin;
3667fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3668fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3669fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3670fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3671fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3672fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
3673fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3674fbdbba38SShri Abhyankar 
3675fbdbba38SShri Abhyankar   if (nz < 0) {
3676fbdbba38SShri Abhyankar     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3677fbdbba38SShri Abhyankar   }
3678fbdbba38SShri Abhyankar 
3679fbdbba38SShri Abhyankar   /* read in row lengths */
3680fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3681fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3682fbdbba38SShri Abhyankar 
3683fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
3684fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3685fbdbba38SShri 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);
3686fbdbba38SShri Abhyankar 
3687fbdbba38SShri Abhyankar   /* set global size if not set already*/
3688f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
3689fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3690aabbc4fbSShri Abhyankar   } else {
3691fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
3692fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
3693f501eaabSShri 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);
3694aabbc4fbSShri Abhyankar   }
3695fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
3696fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
3697fbdbba38SShri Abhyankar 
3698fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
3699fbdbba38SShri Abhyankar 
3700fbdbba38SShri Abhyankar   /* read in nonzero values */
3701fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
3702fbdbba38SShri Abhyankar 
3703fbdbba38SShri Abhyankar   /* set matrix "i" values */
3704fbdbba38SShri Abhyankar   a->i[0] = 0;
3705fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
3706fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3707fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
3708fbdbba38SShri Abhyankar   }
3709fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3710fbdbba38SShri Abhyankar 
3711fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3712fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3713fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
3714fbdbba38SShri Abhyankar }
3715fbdbba38SShri Abhyankar 
3716fbdbba38SShri Abhyankar #undef __FUNCT__
3717b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3718ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
37197264ac53SSatish Balay {
37207264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3721dfbe8321SBarry Smith   PetscErrorCode ierr;
3722eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3723eeffb40dSHong Zhang   PetscInt k;
3724eeffb40dSHong Zhang #endif
37257264ac53SSatish Balay 
37263a40ed3dSBarry Smith   PetscFunctionBegin;
3727bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3728d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3729ca44d042SBarry Smith     *flg = PETSC_FALSE;
3730ca44d042SBarry Smith     PetscFunctionReturn(0);
3731bcd2baecSBarry Smith   }
37327264ac53SSatish Balay 
37337264ac53SSatish Balay   /* if the a->i are the same */
3734d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3735abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
37367264ac53SSatish Balay 
37377264ac53SSatish Balay   /* if a->j are the same */
373897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3739abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3740bcd2baecSBarry Smith 
3741bcd2baecSBarry Smith   /* if a->a are the same */
3742eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3743eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3744eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3745eeffb40dSHong Zhang       *flg = PETSC_FALSE;
37463a40ed3dSBarry Smith       PetscFunctionReturn(0);
3747eeffb40dSHong Zhang     }
3748eeffb40dSHong Zhang   }
3749eeffb40dSHong Zhang #else
3750eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3751eeffb40dSHong Zhang #endif
3752eeffb40dSHong Zhang   PetscFunctionReturn(0);
37537264ac53SSatish Balay }
375436db0b34SBarry Smith 
37554a2ae208SSatish Balay #undef __FUNCT__
37564a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
375705869f15SSatish Balay /*@
375836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
375936db0b34SBarry Smith               provided by the user.
376036db0b34SBarry Smith 
3761c75a6043SHong Zhang       Collective on MPI_Comm
376236db0b34SBarry Smith 
376336db0b34SBarry Smith    Input Parameters:
376436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
376536db0b34SBarry Smith .   m - number of rows
376636db0b34SBarry Smith .   n - number of columns
376736db0b34SBarry Smith .   i - row indices
376836db0b34SBarry Smith .   j - column indices
376936db0b34SBarry Smith -   a - matrix values
377036db0b34SBarry Smith 
377136db0b34SBarry Smith    Output Parameter:
377236db0b34SBarry Smith .   mat - the matrix
377336db0b34SBarry Smith 
377436db0b34SBarry Smith    Level: intermediate
377536db0b34SBarry Smith 
377636db0b34SBarry Smith    Notes:
37770551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
377836db0b34SBarry Smith     once the matrix is destroyed
377936db0b34SBarry Smith 
378036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
378136db0b34SBarry Smith 
3782bfeeae90SHong Zhang        The i and j indices are 0 based
378336db0b34SBarry Smith 
3784a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3785a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3786a4552177SSatish Balay     as shown:
3787a4552177SSatish Balay 
3788a4552177SSatish Balay         1 0 0
3789a4552177SSatish Balay         2 0 3
3790a4552177SSatish Balay         4 5 6
3791a4552177SSatish Balay 
3792a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
37939985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3794a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3795a4552177SSatish Balay 
37969985e31cSBarry Smith 
37972fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
379836db0b34SBarry Smith 
379936db0b34SBarry Smith @*/
38007087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
380136db0b34SBarry Smith {
3802dfbe8321SBarry Smith   PetscErrorCode ierr;
3803cbcfb4deSHong Zhang   PetscInt       ii;
380436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3805cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3806cbcfb4deSHong Zhang   PetscInt       jj;
3807cbcfb4deSHong Zhang #endif
380836db0b34SBarry Smith 
380936db0b34SBarry Smith   PetscFunctionBegin;
3810a96a251dSBarry Smith   if (i[0]) {
3811e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
381236db0b34SBarry Smith   }
3813f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3814f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3815ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3816ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3817ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3818ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3819ab93d7beSBarry Smith 
382036db0b34SBarry Smith   aij->i = i;
382136db0b34SBarry Smith   aij->j = j;
382236db0b34SBarry Smith   aij->a = a;
382336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
382436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3825e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3826e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
382736db0b34SBarry Smith 
382836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
382936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
38302515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
3831e32f2f54SBarry 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]);
38329985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
3833e32f2f54SBarry 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);
3834e32f2f54SBarry 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);
38359985e31cSBarry Smith     }
383636db0b34SBarry Smith #endif
383736db0b34SBarry Smith   }
38382515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
383936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3840e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3841e32f2f54SBarry 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]);
384236db0b34SBarry Smith   }
384336db0b34SBarry Smith #endif
384436db0b34SBarry Smith 
3845b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3846b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
384736db0b34SBarry Smith   PetscFunctionReturn(0);
384836db0b34SBarry Smith }
384936db0b34SBarry Smith 
3850cc8ba8e1SBarry Smith #undef __FUNCT__
3851ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3852dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3853cc8ba8e1SBarry Smith {
3854dfbe8321SBarry Smith   PetscErrorCode ierr;
3855cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
385636db0b34SBarry Smith 
3857cc8ba8e1SBarry Smith   PetscFunctionBegin;
38588ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3859cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3860cc8ba8e1SBarry Smith     a->coloring = coloring;
386112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
386297f1f81fSBarry Smith     PetscInt             i,*larray;
386312c595b3SBarry Smith     ISColoring      ocoloring;
386408b6dcc0SBarry Smith     ISColoringValue *colors;
386512c595b3SBarry Smith 
386612c595b3SBarry Smith     /* set coloring for diagonal portion */
38670e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3868d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
386912c595b3SBarry Smith       larray[i] = i;
387012c595b3SBarry Smith     }
3871784ac674SJed Brown     ierr = ISGlobalToLocalMappingApply(A->cmapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
38720e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3873d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
387412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
387512c595b3SBarry Smith     }
387612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3877d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
387812c595b3SBarry Smith     a->coloring = ocoloring;
387912c595b3SBarry Smith   }
3880cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3881cc8ba8e1SBarry Smith }
3882cc8ba8e1SBarry Smith 
3883dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3884ee4f033dSBarry Smith EXTERN_C_BEGIN
388529c1e371SBarry Smith #include "adic/ad_utils.h"
3886ee4f033dSBarry Smith EXTERN_C_END
3887cc8ba8e1SBarry Smith 
3888cc8ba8e1SBarry Smith #undef __FUNCT__
3889ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3890dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3891cc8ba8e1SBarry Smith {
3892cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3893d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
38944440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
389508b6dcc0SBarry Smith   ISColoringValue *color;
3896cc8ba8e1SBarry Smith 
3897cc8ba8e1SBarry Smith   PetscFunctionBegin;
3898e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
38994440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3900cc8ba8e1SBarry Smith   color = a->coloring->colors;
3901cc8ba8e1SBarry Smith   /* loop over rows */
3902cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3903cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3904cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3905cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3906cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3907cc8ba8e1SBarry Smith     }
39084440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3909ee4f033dSBarry Smith   }
3910ee4f033dSBarry Smith   PetscFunctionReturn(0);
3911ee4f033dSBarry Smith }
3912ee4f033dSBarry Smith #endif
3913ee4f033dSBarry Smith 
3914ee4f033dSBarry Smith #undef __FUNCT__
3915ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
391697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3917ee4f033dSBarry Smith {
3918ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3919d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
392054f21887SBarry Smith   MatScalar       *v = a->a;
392154f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
392208b6dcc0SBarry Smith   ISColoringValue *color;
3923ee4f033dSBarry Smith 
3924ee4f033dSBarry Smith   PetscFunctionBegin;
3925e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3926ee4f033dSBarry Smith   color = a->coloring->colors;
3927ee4f033dSBarry Smith   /* loop over rows */
3928ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3929ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3930ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3931ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3932ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3933ee4f033dSBarry Smith     }
3934ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3935cc8ba8e1SBarry Smith   }
3936cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3937cc8ba8e1SBarry Smith }
393836db0b34SBarry Smith 
393981824310SBarry Smith /*
394081824310SBarry Smith     Special version for direct calls from Fortran
394181824310SBarry Smith */
39427087cfbeSBarry Smith #include "private/fortranimpl.h"
394381824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
394481824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
394581824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
394681824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
394781824310SBarry Smith #endif
394881824310SBarry Smith 
394981824310SBarry Smith /* Change these macros so can be used in void function */
395081824310SBarry Smith #undef CHKERRQ
39517adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
395281824310SBarry Smith #undef SETERRQ2
3953e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
395481824310SBarry Smith 
395581824310SBarry Smith EXTERN_C_BEGIN
395681824310SBarry Smith #undef __FUNCT__
395781824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
39581f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
395981824310SBarry Smith {
396081824310SBarry Smith   Mat            A = *AA;
396181824310SBarry Smith   PetscInt       m = *mm, n = *nn;
396281824310SBarry Smith   InsertMode     is = *isis;
396381824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
396481824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
396581824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
396681824310SBarry Smith   PetscErrorCode ierr;
396781824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
396854f21887SBarry Smith   MatScalar      *ap,value,*aa;
3969ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
3970ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
397181824310SBarry Smith 
397281824310SBarry Smith   PetscFunctionBegin;
3973d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
397481824310SBarry Smith   imax = a->imax;
397581824310SBarry Smith   ai = a->i;
397681824310SBarry Smith   ailen = a->ilen;
397781824310SBarry Smith   aj = a->j;
397881824310SBarry Smith   aa = a->a;
397981824310SBarry Smith 
398081824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
398181824310SBarry Smith     row  = im[k];
398281824310SBarry Smith     if (row < 0) continue;
398381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3984d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
398581824310SBarry Smith #endif
398681824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
398781824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
398881824310SBarry Smith     low  = 0;
398981824310SBarry Smith     high = nrow;
399081824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
399181824310SBarry Smith       if (in[l] < 0) continue;
399281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3993d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
399481824310SBarry Smith #endif
399581824310SBarry Smith       col = in[l];
399681824310SBarry Smith       if (roworiented) {
399781824310SBarry Smith         value = v[l + k*n];
399881824310SBarry Smith       } else {
399981824310SBarry Smith         value = v[k + l*m];
400081824310SBarry Smith       }
400181824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
400281824310SBarry Smith 
400381824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
400481824310SBarry Smith       lastcol = col;
400581824310SBarry Smith       while (high-low > 5) {
400681824310SBarry Smith         t = (low+high)/2;
400781824310SBarry Smith         if (rp[t] > col) high = t;
400881824310SBarry Smith         else             low  = t;
400981824310SBarry Smith       }
401081824310SBarry Smith       for (i=low; i<high; i++) {
401181824310SBarry Smith         if (rp[i] > col) break;
401281824310SBarry Smith         if (rp[i] == col) {
401381824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
401481824310SBarry Smith           else                  ap[i] = value;
401581824310SBarry Smith           goto noinsert;
401681824310SBarry Smith         }
401781824310SBarry Smith       }
401881824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
401981824310SBarry Smith       if (nonew == 1) goto noinsert;
40207adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4021fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
402281824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
402381824310SBarry Smith       /* shift up all the later entries in this row */
402481824310SBarry Smith       for (ii=N; ii>=i; ii--) {
402581824310SBarry Smith         rp[ii+1] = rp[ii];
402681824310SBarry Smith         ap[ii+1] = ap[ii];
402781824310SBarry Smith       }
402881824310SBarry Smith       rp[i] = col;
402981824310SBarry Smith       ap[i] = value;
403081824310SBarry Smith       noinsert:;
403181824310SBarry Smith       low = i + 1;
403281824310SBarry Smith     }
403381824310SBarry Smith     ailen[row] = nrow;
403481824310SBarry Smith   }
403581824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
403681824310SBarry Smith   PetscFunctionReturnVoid();
403781824310SBarry Smith }
403881824310SBarry Smith EXTERN_C_END
403962298a1eSBarry Smith 
4040