xref: /petsc/src/mat/impls/aij/seq/aij.c (revision bf0cc55543cd83e035744be2f77202b216d1436e)
1b377110cSBarry Smith 
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
77c4f633dSBarry Smith 
8c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9c6db04a5SJed Brown #include <petscblaslapack.h>
10c6db04a5SJed Brown #include <petscbt.h>
1117ab2063SBarry Smith 
124a2ae208SSatish Balay #undef __FUNCT__
136ce1633cSBarry Smith #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
146ce1633cSBarry Smith PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
156ce1633cSBarry Smith {
166ce1633cSBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
176ce1633cSBarry Smith   const MatScalar   *aa = a->a;
186ce1633cSBarry Smith   PetscInt          i,m=A->rmap->n,cnt = 0;
196ce1633cSBarry Smith   const PetscInt    *jj = a->j,*diag;
206ce1633cSBarry Smith   PetscInt          *rows;
216ce1633cSBarry Smith   PetscErrorCode    ierr;
226ce1633cSBarry Smith 
236ce1633cSBarry Smith   PetscFunctionBegin;
246ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
256ce1633cSBarry Smith   diag = a->diag;
266ce1633cSBarry Smith   for (i=0; i<m; i++) {
276ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
286ce1633cSBarry Smith       cnt++;
296ce1633cSBarry Smith     }
306ce1633cSBarry Smith   }
316ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
326ce1633cSBarry Smith   cnt  = 0;
336ce1633cSBarry Smith   for (i=0; i<m; i++) {
346ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
356ce1633cSBarry Smith       rows[cnt++] = i;
366ce1633cSBarry Smith     }
376ce1633cSBarry Smith   }
386ce1633cSBarry Smith   ierr = ISCreateGeneral(((PetscObject)A)->comm,cnt,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
396ce1633cSBarry Smith   PetscFunctionReturn(0);
406ce1633cSBarry Smith }
416ce1633cSBarry Smith 
426ce1633cSBarry Smith #undef __FUNCT__
43b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
44b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
45b3a44c85SBarry Smith {
46b3a44c85SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
47b3a44c85SBarry Smith   const MatScalar   *aa;
48b3a44c85SBarry Smith   PetscInt          m=A->rmap->n,cnt = 0;
49b3a44c85SBarry Smith   const PetscInt    *ii;
50b3a44c85SBarry Smith   PetscInt          n,i,j,*rows;
51b3a44c85SBarry Smith   PetscErrorCode    ierr;
52b3a44c85SBarry Smith 
53b3a44c85SBarry Smith   PetscFunctionBegin;
54b3a44c85SBarry Smith   *keptrows = 0;
55b3a44c85SBarry Smith   ii        = a->i;
56b3a44c85SBarry Smith   for (i=0; i<m; i++) {
57b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
58b3a44c85SBarry Smith     if (!n) {
59b3a44c85SBarry Smith       cnt++;
60b3a44c85SBarry Smith       goto ok1;
61b3a44c85SBarry Smith     }
62b3a44c85SBarry Smith     aa  = a->a + ii[i];
63b3a44c85SBarry Smith     for (j=0; j<n; j++) {
64b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
65b3a44c85SBarry Smith     }
66b3a44c85SBarry Smith     cnt++;
67b3a44c85SBarry Smith     ok1:;
68b3a44c85SBarry Smith   }
69b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
70b3a44c85SBarry Smith   ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
71b3a44c85SBarry Smith   cnt  = 0;
72b3a44c85SBarry Smith   for (i=0; i<m; i++) {
73b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
74b3a44c85SBarry Smith     if (!n) continue;
75b3a44c85SBarry Smith     aa  = a->a + ii[i];
76b3a44c85SBarry Smith     for (j=0; j<n; j++) {
77b3a44c85SBarry Smith       if (aa[j] != 0.0) {
78b3a44c85SBarry Smith         rows[cnt++] = i;
79b3a44c85SBarry Smith         break;
80b3a44c85SBarry Smith       }
81b3a44c85SBarry Smith     }
82b3a44c85SBarry Smith   }
83b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
84b3a44c85SBarry Smith   PetscFunctionReturn(0);
85b3a44c85SBarry Smith }
86b3a44c85SBarry Smith 
87b3a44c85SBarry Smith #undef __FUNCT__
8879299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
897087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
9079299369SBarry Smith {
9179299369SBarry Smith   PetscErrorCode ierr;
9279299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
93d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
9454f21887SBarry Smith   MatScalar      *aa = aij->a;
9554f21887SBarry Smith   PetscScalar    *v;
96ace3abfcSBarry Smith   PetscBool      missing;
9779299369SBarry Smith 
9879299369SBarry Smith   PetscFunctionBegin;
9909f38230SBarry Smith   if (Y->assembled) {
10009f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
10109f38230SBarry Smith     if (!missing) {
10279299369SBarry Smith       diag = aij->diag;
10379299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
10479299369SBarry Smith       if (is == INSERT_VALUES) {
10579299369SBarry Smith 	for (i=0; i<m; i++) {
10679299369SBarry Smith 	  aa[diag[i]] = v[i];
10779299369SBarry Smith 	}
10879299369SBarry Smith       } else {
10979299369SBarry Smith 	for (i=0; i<m; i++) {
11079299369SBarry Smith 	  aa[diag[i]] += v[i];
11179299369SBarry Smith 	}
11279299369SBarry Smith       }
11379299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
11479299369SBarry Smith       PetscFunctionReturn(0);
11579299369SBarry Smith     }
11609f38230SBarry Smith   }
11709f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
11809f38230SBarry Smith   PetscFunctionReturn(0);
11909f38230SBarry Smith }
12079299369SBarry Smith 
12179299369SBarry Smith #undef __FUNCT__
1224a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
123ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
12417ab2063SBarry Smith {
125416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
126dfbe8321SBarry Smith   PetscErrorCode ierr;
12797f1f81fSBarry Smith   PetscInt       i,ishift;
12817ab2063SBarry Smith 
1293a40ed3dSBarry Smith   PetscFunctionBegin;
130d0f46423SBarry Smith   *m     = A->rmap->n;
1313a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
132bfeeae90SHong Zhang   ishift = 0;
13353e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
134d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
135bfeeae90SHong Zhang   } else if (oshift == 1) {
136d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
1373b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
138d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
139d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
140ecc77c7aSBarry Smith     if (ja) {
14197f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
1423b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
143ecc77c7aSBarry Smith     }
1446945ee14SBarry Smith   } else {
145ecc77c7aSBarry Smith     *ia = a->i;
146ecc77c7aSBarry Smith     if (ja) *ja = a->j;
147a2ce50c7SBarry Smith   }
1483a40ed3dSBarry Smith   PetscFunctionReturn(0);
149a2744918SBarry Smith }
150a2744918SBarry Smith 
1514a2ae208SSatish Balay #undef __FUNCT__
1524a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
153ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
1546945ee14SBarry Smith {
155dfbe8321SBarry Smith   PetscErrorCode ierr;
1566945ee14SBarry Smith 
1573a40ed3dSBarry Smith   PetscFunctionBegin;
1583a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
159bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
160606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
161ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
162bcd2baecSBarry Smith   }
1633a40ed3dSBarry Smith   PetscFunctionReturn(0);
16417ab2063SBarry Smith }
16517ab2063SBarry Smith 
1664a2ae208SSatish Balay #undef __FUNCT__
1674a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
168ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
1693b2fbd54SBarry Smith {
1703b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
171dfbe8321SBarry Smith   PetscErrorCode ierr;
172d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
17397f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1743b2fbd54SBarry Smith 
1753a40ed3dSBarry Smith   PetscFunctionBegin;
176899cda47SBarry Smith   *nn = n;
1773a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1783b2fbd54SBarry Smith   if (symmetric) {
179d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1803b2fbd54SBarry Smith   } else {
18197f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
18297f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
18397f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
18497f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1853b2fbd54SBarry Smith     jj = a->j;
1863b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
187bfeeae90SHong Zhang       collengths[jj[i]]++;
1883b2fbd54SBarry Smith     }
1893b2fbd54SBarry Smith     cia[0] = oshift;
1903b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1913b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1923b2fbd54SBarry Smith     }
19397f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1943b2fbd54SBarry Smith     jj   = a->j;
195a93ec695SBarry Smith     for (row=0; row<m; row++) {
196a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
197a93ec695SBarry Smith       for (i=0; i<mr; i++) {
198bfeeae90SHong Zhang         col = *jj++;
1993b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2003b2fbd54SBarry Smith       }
2013b2fbd54SBarry Smith     }
202606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2033b2fbd54SBarry Smith     *ia = cia; *ja = cja;
2043b2fbd54SBarry Smith   }
2053a40ed3dSBarry Smith   PetscFunctionReturn(0);
2063b2fbd54SBarry Smith }
2073b2fbd54SBarry Smith 
2084a2ae208SSatish Balay #undef __FUNCT__
2094a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
210ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2113b2fbd54SBarry Smith {
212dfbe8321SBarry Smith   PetscErrorCode ierr;
213606d414cSSatish Balay 
2143a40ed3dSBarry Smith   PetscFunctionBegin;
2153a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2163b2fbd54SBarry Smith 
217606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
218606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2193b2fbd54SBarry Smith 
2203a40ed3dSBarry Smith   PetscFunctionReturn(0);
2213b2fbd54SBarry Smith }
2223b2fbd54SBarry Smith 
22387d4246cSBarry Smith #undef __FUNCT__
22487d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
22587d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
22687d4246cSBarry Smith {
22787d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
22887d4246cSBarry Smith   PetscInt       *ai = a->i;
22987d4246cSBarry Smith   PetscErrorCode ierr;
23087d4246cSBarry Smith 
23187d4246cSBarry Smith   PetscFunctionBegin;
23287d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
23387d4246cSBarry Smith   PetscFunctionReturn(0);
23487d4246cSBarry Smith }
23587d4246cSBarry Smith 
2364a2ae208SSatish Balay #undef __FUNCT__
2374a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
23897f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
23917ab2063SBarry Smith {
240416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
241e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
24297f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
2436849ba73SBarry Smith   PetscErrorCode ierr;
244e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
24554f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
246ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
247ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
24817ab2063SBarry Smith 
2493a40ed3dSBarry Smith   PetscFunctionBegin;
25071fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
25117ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
252416022c9SBarry Smith     row  = im[k];
2535ef9f2a5SBarry Smith     if (row < 0) continue;
2542515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
255e32f2f54SBarry 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);
2563b2fbd54SBarry Smith #endif
257bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
25817ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
259416022c9SBarry Smith     low  = 0;
260c71e6ed7SBarry Smith     high = nrow;
26117ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
2625ef9f2a5SBarry Smith       if (in[l] < 0) continue;
2632515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
264e32f2f54SBarry 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);
2653b2fbd54SBarry Smith #endif
266bfeeae90SHong Zhang       col = in[l];
26716371a99SBarry Smith       if (v) {
2684b0e389bSBarry Smith 	if (roworiented) {
2695ef9f2a5SBarry Smith 	  value = v[l + k*n];
270bef8e0ddSBarry Smith 	} else {
2714b0e389bSBarry Smith 	  value = v[k + l*m];
2724b0e389bSBarry Smith 	}
27316371a99SBarry Smith       } else {
27475567043SBarry Smith         value = 0.;
27516371a99SBarry Smith       }
276abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
27736db0b34SBarry Smith 
2787cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
279e2ee6c50SBarry Smith       lastcol = col;
280416022c9SBarry Smith       while (high-low > 5) {
281416022c9SBarry Smith         t = (low+high)/2;
282416022c9SBarry Smith         if (rp[t] > col) high = t;
283416022c9SBarry Smith         else             low  = t;
28417ab2063SBarry Smith       }
285416022c9SBarry Smith       for (i=low; i<high; i++) {
28617ab2063SBarry Smith         if (rp[i] > col) break;
28717ab2063SBarry Smith         if (rp[i] == col) {
288416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
28917ab2063SBarry Smith           else                  ap[i] = value;
290e44c0bd4SBarry Smith           low = i + 1;
29117ab2063SBarry Smith           goto noinsert;
29217ab2063SBarry Smith         }
29317ab2063SBarry Smith       }
294abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
295c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
296e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
297fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
298c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
299416022c9SBarry Smith       /* shift up all the later entries in this row */
300416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
30117ab2063SBarry Smith         rp[ii+1] = rp[ii];
30217ab2063SBarry Smith         ap[ii+1] = ap[ii];
30317ab2063SBarry Smith       }
30417ab2063SBarry Smith       rp[i] = col;
30517ab2063SBarry Smith       ap[i] = value;
306416022c9SBarry Smith       low   = i + 1;
307e44c0bd4SBarry Smith       noinsert:;
30817ab2063SBarry Smith     }
30917ab2063SBarry Smith     ailen[row] = nrow;
31017ab2063SBarry Smith   }
31188e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3123a40ed3dSBarry Smith   PetscFunctionReturn(0);
31317ab2063SBarry Smith }
31417ab2063SBarry Smith 
31581824310SBarry Smith 
3164a2ae208SSatish Balay #undef __FUNCT__
3174a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
318a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3197eb43aa7SLois Curfman McInnes {
3207eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
32197f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
32297f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
32354f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
3247eb43aa7SLois Curfman McInnes 
3253a40ed3dSBarry Smith   PetscFunctionBegin;
3267eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3277eb43aa7SLois Curfman McInnes     row  = im[k];
328e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
329e32f2f54SBarry 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);
330bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3317eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3327eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
333e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
334e32f2f54SBarry 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);
335bfeeae90SHong Zhang       col = in[l] ;
3367eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3377eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3387eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3397eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3407eb43aa7SLois Curfman McInnes         else             low  = t;
3417eb43aa7SLois Curfman McInnes       }
3427eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3437eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
3447eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
345b49de8d1SLois Curfman McInnes           *v++ = ap[i];
3467eb43aa7SLois Curfman McInnes           goto finished;
3477eb43aa7SLois Curfman McInnes         }
3487eb43aa7SLois Curfman McInnes       }
34997e567efSBarry Smith       *v++ = 0.0;
3507eb43aa7SLois Curfman McInnes       finished:;
3517eb43aa7SLois Curfman McInnes     }
3527eb43aa7SLois Curfman McInnes   }
3533a40ed3dSBarry Smith   PetscFunctionReturn(0);
3547eb43aa7SLois Curfman McInnes }
3557eb43aa7SLois Curfman McInnes 
35617ab2063SBarry Smith 
3574a2ae208SSatish Balay #undef __FUNCT__
3584a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
359dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
36017ab2063SBarry Smith {
361416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3626849ba73SBarry Smith   PetscErrorCode ierr;
3636f69ff64SBarry Smith   PetscInt       i,*col_lens;
3646f69ff64SBarry Smith   int            fd;
36517ab2063SBarry Smith 
3663a40ed3dSBarry Smith   PetscFunctionBegin;
367b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
368d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
3690700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
370d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
371d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
372416022c9SBarry Smith   col_lens[3] = a->nz;
373416022c9SBarry Smith 
374416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
375d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
376416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
37717ab2063SBarry Smith   }
378d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
379606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
380416022c9SBarry Smith 
381416022c9SBarry Smith   /* store column indices (zero start index) */
3826f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
383416022c9SBarry Smith 
384416022c9SBarry Smith   /* store nonzero values */
3856f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3863a40ed3dSBarry Smith   PetscFunctionReturn(0);
38717ab2063SBarry Smith }
388416022c9SBarry Smith 
38909573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
390cd155464SBarry Smith 
3914a2ae208SSatish Balay #undef __FUNCT__
3924a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
393dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
394416022c9SBarry Smith {
395416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
396dfbe8321SBarry Smith   PetscErrorCode    ierr;
397d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
398e060cb09SBarry Smith   const char        *name;
399f3ef73ceSBarry Smith   PetscViewerFormat format;
40017ab2063SBarry Smith 
4013a40ed3dSBarry Smith   PetscFunctionBegin;
402b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
40371c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
40497f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
405d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
406d00d2cf4SBarry Smith       nofinalvalue = 1;
407d00d2cf4SBarry Smith     }
408d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
409d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
41077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
41177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
412b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
41317ab2063SBarry Smith 
41417ab2063SBarry Smith     for (i=0; i<m; i++) {
415416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
416aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41777431f27SBarry 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);
41817ab2063SBarry Smith #else
41977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
42017ab2063SBarry Smith #endif
42117ab2063SBarry Smith       }
42217ab2063SBarry Smith     }
423d00d2cf4SBarry Smith     if (nofinalvalue) {
424d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
425d00d2cf4SBarry Smith     }
426317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
427fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
428d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
42968369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
430cd155464SBarry Smith      PetscFunctionReturn(0);
431fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
432d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4337566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
43444cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
43577431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
43644cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
437aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
43836db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
439a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44036db0b34SBarry Smith         } else 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 (PetscRealPart(a->a[j]) != 0.0) {
443a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4446831982aSBarry Smith         }
44544cd7ae7SLois Curfman McInnes #else
446a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
44744cd7ae7SLois Curfman McInnes #endif
44844cd7ae7SLois Curfman McInnes       }
449b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45044cd7ae7SLois Curfman McInnes     }
451d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
452fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
45397f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
454d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4557566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
45697f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
457496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
458496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
459496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
460496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
461aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
46236db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
463496be53dSLois Curfman McInnes #else
464496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
465496be53dSLois Curfman McInnes #endif
466496be53dSLois Curfman McInnes         }
467496be53dSLois Curfman McInnes       }
468496be53dSLois Curfman McInnes     }
4692e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
47077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
4712e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
47277431f27SBarry 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);}
47377431f27SBarry 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);}
47477431f27SBarry 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);}
47577431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
47677431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
47777431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
478496be53dSLois Curfman McInnes     }
479b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
480606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
481496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
482496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
48377431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
484496be53dSLois Curfman McInnes       }
485b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
486496be53dSLois Curfman McInnes     }
487b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
488496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
489496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
490496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
491aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49236db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
493b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4946831982aSBarry Smith           }
495496be53dSLois Curfman McInnes #else
496b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
497496be53dSLois Curfman McInnes #endif
498496be53dSLois Curfman McInnes         }
499496be53dSLois Curfman McInnes       }
500b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
501496be53dSLois Curfman McInnes     }
502d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
503fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
50497f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
50587828ca2SBarry Smith     PetscScalar value;
50602594712SBarry Smith 
507d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5087566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
50902594712SBarry Smith     for (i=0; i<m; i++) {
51002594712SBarry Smith       jcnt = 0;
511d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
512e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
51302594712SBarry Smith           value = a->a[cnt++];
514e24b481bSBarry Smith           jcnt++;
51502594712SBarry Smith         } else {
51602594712SBarry Smith           value = 0.0;
51702594712SBarry Smith         }
518aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
519b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
52002594712SBarry Smith #else
521b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
52202594712SBarry Smith #endif
52302594712SBarry Smith       }
524b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
52502594712SBarry Smith     }
526d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5273c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
528d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5297566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5303c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5313c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5323c215bfdSMatthew Knepley #else
5333c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5343c215bfdSMatthew Knepley #endif
535d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5363c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5373c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5383c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5393c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
5403c215bfdSMatthew 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);
5413c215bfdSMatthew Knepley         } else 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 {
5443c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5453c215bfdSMatthew Knepley         }
5463c215bfdSMatthew Knepley #else
5473c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
5483c215bfdSMatthew Knepley #endif
5493c215bfdSMatthew Knepley       }
5503c215bfdSMatthew Knepley     }
551d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5523a40ed3dSBarry Smith   } else {
553d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5547566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
555d5f3da31SBarry Smith     if (A->factortype){
55616cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
55716cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
55816cd7e1dSShri Abhyankar         /* L part */
55916cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
56016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
56116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
56216cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
56316cd7e1dSShri Abhyankar           } else 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 {
56616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
56716cd7e1dSShri Abhyankar           }
56816cd7e1dSShri Abhyankar #else
56916cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
57016cd7e1dSShri Abhyankar #endif
57116cd7e1dSShri Abhyankar         }
57216cd7e1dSShri Abhyankar 	/* diagonal */
57316cd7e1dSShri Abhyankar 	j = a->diag[i];
57416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
57516cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
57616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
57716cd7e1dSShri Abhyankar           } else 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 {
58016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
58116cd7e1dSShri Abhyankar           }
58216cd7e1dSShri Abhyankar #else
58316cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
58416cd7e1dSShri Abhyankar #endif
58516cd7e1dSShri Abhyankar 
58616cd7e1dSShri Abhyankar 	/* U part */
58716cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
58816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
58916cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
59016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
59116cd7e1dSShri Abhyankar           } else 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 {
59416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
59516cd7e1dSShri Abhyankar           }
59616cd7e1dSShri Abhyankar #else
59716cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
59816cd7e1dSShri Abhyankar #endif
59916cd7e1dSShri Abhyankar }
60016cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
60116cd7e1dSShri Abhyankar         }
60216cd7e1dSShri Abhyankar     } else {
60317ab2063SBarry Smith       for (i=0; i<m; i++) {
60477431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
605416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
606aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
60736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
608a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
60936db0b34SBarry Smith           } else 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);
6113a40ed3dSBarry Smith           } else {
612a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
61317ab2063SBarry Smith           }
61417ab2063SBarry Smith #else
615a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
61617ab2063SBarry Smith #endif
61717ab2063SBarry Smith         }
618b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
61917ab2063SBarry Smith       }
62016cd7e1dSShri Abhyankar     }
621d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
62217ab2063SBarry Smith   }
623b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6243a40ed3dSBarry Smith   PetscFunctionReturn(0);
625416022c9SBarry Smith }
626416022c9SBarry Smith 
6274a2ae208SSatish Balay #undef __FUNCT__
6284a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
629dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
630416022c9SBarry Smith {
631480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
632416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
633dfbe8321SBarry Smith   PetscErrorCode    ierr;
634d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
63536db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
636b0a32e0cSBarry Smith   PetscViewer       viewer;
637f3ef73ceSBarry Smith   PetscViewerFormat format;
638cddf8d76SBarry Smith 
6393a40ed3dSBarry Smith   PetscFunctionBegin;
640480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
641b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
64219bcc07fSBarry Smith 
643b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
644416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
6450513a670SBarry Smith 
646fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
6470513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
648b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
649416022c9SBarry Smith     for (i=0; i<m; i++) {
650cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
651bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
652bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
653aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
65436db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
655cddf8d76SBarry Smith #else
656cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
657cddf8d76SBarry Smith #endif
658b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
659cddf8d76SBarry Smith       }
660cddf8d76SBarry Smith     }
661b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
662cddf8d76SBarry Smith     for (i=0; i<m; i++) {
663cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
664bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
665bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
666cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
667b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
668cddf8d76SBarry Smith       }
669cddf8d76SBarry Smith     }
670b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
671cddf8d76SBarry Smith     for (i=0; i<m; i++) {
672cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
673bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
674bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
675aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
67636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
677cddf8d76SBarry Smith #else
678cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
679cddf8d76SBarry Smith #endif
680b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
681416022c9SBarry Smith       }
682416022c9SBarry Smith     }
6830513a670SBarry Smith   } else {
6840513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
6850513a670SBarry Smith     /* first determine max of all nonzero values */
68697f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
687b0a32e0cSBarry Smith     PetscDraw   popup;
68836db0b34SBarry Smith     PetscReal scale;
6890513a670SBarry Smith 
6900513a670SBarry Smith     for (i=0; i<nz; i++) {
6910513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
6920513a670SBarry Smith     }
693b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
694b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
695b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
6960513a670SBarry Smith     count = 0;
6970513a670SBarry Smith     for (i=0; i<m; i++) {
6980513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
699bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
700bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
70197f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
702b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7030513a670SBarry Smith         count++;
7040513a670SBarry Smith       }
7050513a670SBarry Smith     }
7060513a670SBarry Smith   }
707480ef9eaSBarry Smith   PetscFunctionReturn(0);
708480ef9eaSBarry Smith }
709cddf8d76SBarry Smith 
7104a2ae208SSatish Balay #undef __FUNCT__
7114a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
712dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
713480ef9eaSBarry Smith {
714dfbe8321SBarry Smith   PetscErrorCode ierr;
715b0a32e0cSBarry Smith   PetscDraw      draw;
71636db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
717ace3abfcSBarry Smith   PetscBool      isnull;
718480ef9eaSBarry Smith 
719480ef9eaSBarry Smith   PetscFunctionBegin;
720b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
721b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
722480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
723480ef9eaSBarry Smith 
724480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
725d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
726480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
727b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
728b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
729480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7303a40ed3dSBarry Smith   PetscFunctionReturn(0);
731416022c9SBarry Smith }
732416022c9SBarry Smith 
7334a2ae208SSatish Balay #undef __FUNCT__
7344a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
735dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
736416022c9SBarry Smith {
737dfbe8321SBarry Smith   PetscErrorCode ierr;
738ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
739416022c9SBarry Smith 
7403a40ed3dSBarry Smith   PetscFunctionBegin;
7412692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
7422692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
7432692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
744c45a1595SBarry Smith   if (iascii) {
7453a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
7460f5bd95cSBarry Smith   } else if (isbinary) {
7473a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
7480f5bd95cSBarry Smith   } else if (isdraw) {
7493a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
7505cd90555SBarry Smith   } else {
751e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
75217ab2063SBarry Smith   }
7534108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
7543a40ed3dSBarry Smith   PetscFunctionReturn(0);
75517ab2063SBarry Smith }
75619bcc07fSBarry Smith 
7574a2ae208SSatish Balay #undef __FUNCT__
7584a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
759dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
76017ab2063SBarry Smith {
761416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7626849ba73SBarry Smith   PetscErrorCode ierr;
76397f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
764d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
76554f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
7663447b6efSHong Zhang   PetscReal      ratio=0.6;
76717ab2063SBarry Smith 
7683a40ed3dSBarry Smith   PetscFunctionBegin;
7693a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
77017ab2063SBarry Smith 
77143ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
77217ab2063SBarry Smith   for (i=1; i<m; i++) {
773416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
77417ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
77594a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
77617ab2063SBarry Smith     if (fshift) {
777bfeeae90SHong Zhang       ip = aj + ai[i] ;
778bfeeae90SHong Zhang       ap = aa + ai[i] ;
77917ab2063SBarry Smith       N  = ailen[i];
78017ab2063SBarry Smith       for (j=0; j<N; j++) {
78117ab2063SBarry Smith         ip[j-fshift] = ip[j];
78217ab2063SBarry Smith         ap[j-fshift] = ap[j];
78317ab2063SBarry Smith       }
78417ab2063SBarry Smith     }
78517ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
78617ab2063SBarry Smith   }
78717ab2063SBarry Smith   if (m) {
78817ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
78917ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
79017ab2063SBarry Smith   }
79117ab2063SBarry Smith   /* reset ilen and imax for each row */
79217ab2063SBarry Smith   for (i=0; i<m; i++) {
79317ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
79417ab2063SBarry Smith   }
795bfeeae90SHong Zhang   a->nz = ai[m];
79665e19b50SBarry 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);
79717ab2063SBarry Smith 
79809f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
799d0f46423SBarry 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);
800ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
801ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8028e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
803dd5f02e7SSatish Balay   a->reallocs          = 0;
8044e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
80536db0b34SBarry Smith   a->rmax              = rmax;
8064e220ebcSLois Curfman McInnes 
807cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
80888e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
80971c2f376SKris Buschelman 
8104108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
81171f1c65dSBarry Smith 
81271f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
8133a40ed3dSBarry Smith   PetscFunctionReturn(0);
81417ab2063SBarry Smith }
81517ab2063SBarry Smith 
8164a2ae208SSatish Balay #undef __FUNCT__
81799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
81899cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
81999cafbc1SBarry Smith {
82099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
82199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
82254f21887SBarry Smith   MatScalar      *aa = a->a;
82399cafbc1SBarry Smith 
82499cafbc1SBarry Smith   PetscFunctionBegin;
82599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
82699cafbc1SBarry Smith   PetscFunctionReturn(0);
82799cafbc1SBarry Smith }
82899cafbc1SBarry Smith 
82999cafbc1SBarry Smith #undef __FUNCT__
83099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
83199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
83299cafbc1SBarry Smith {
83399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
83499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
83554f21887SBarry Smith   MatScalar      *aa = a->a;
83699cafbc1SBarry Smith 
83799cafbc1SBarry Smith   PetscFunctionBegin;
83899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
83999cafbc1SBarry Smith   PetscFunctionReturn(0);
84099cafbc1SBarry Smith }
84199cafbc1SBarry Smith 
84299cafbc1SBarry Smith #undef __FUNCT__
8434a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
844dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
84517ab2063SBarry Smith {
846416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
847dfbe8321SBarry Smith   PetscErrorCode ierr;
8483a40ed3dSBarry Smith 
8493a40ed3dSBarry Smith   PetscFunctionBegin;
850d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
8513a40ed3dSBarry Smith   PetscFunctionReturn(0);
85217ab2063SBarry Smith }
853416022c9SBarry Smith 
8544a2ae208SSatish Balay #undef __FUNCT__
8554a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
856dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
85717ab2063SBarry Smith {
858416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
859dfbe8321SBarry Smith   PetscErrorCode ierr;
860d5d45c9bSBarry Smith 
8613a40ed3dSBarry Smith   PetscFunctionBegin;
862aa482453SBarry Smith #if defined(PETSC_USE_LOG)
863d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
86417ab2063SBarry Smith #endif
865e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
8666bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
8676bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
86805b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
86905b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
87071f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
87105b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
8726bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
87305b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
8746bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
87505b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
8766bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
877cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
878a30b2313SHong Zhang 
8794108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
880*bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
881901853e0SKris Buschelman 
882dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
883901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
884901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
885901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
886901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
887901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
8885a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
889901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
890901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
891a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
892901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
8933a40ed3dSBarry Smith   PetscFunctionReturn(0);
89417ab2063SBarry Smith }
89517ab2063SBarry Smith 
8964a2ae208SSatish Balay #undef __FUNCT__
8974a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
898ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
89917ab2063SBarry Smith {
900416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9014846f1f5SKris Buschelman   PetscErrorCode ierr;
9023a40ed3dSBarry Smith 
9033a40ed3dSBarry Smith   PetscFunctionBegin;
904a65d3064SKris Buschelman   switch (op) {
905a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
9064e0d8c25SBarry Smith       a->roworiented       = flg;
907a65d3064SKris Buschelman       break;
908a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
909a9817697SBarry Smith       a->keepnonzeropattern    = flg;
910a65d3064SKris Buschelman       break;
911512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
912512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
913a65d3064SKris Buschelman       break;
914a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
9154e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
916a65d3064SKris Buschelman       break;
917a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
9184e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
919a65d3064SKris Buschelman       break;
92028b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
92128b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
92228b2fa4aSMatthew Knepley       break;
923a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
9244e0d8c25SBarry Smith       a->ignorezeroentries = flg;
9250df259c2SBarry Smith       break;
926cd6b891eSBarry Smith     case MAT_CHECK_COMPRESSED_ROW:
927cd6b891eSBarry Smith       a->compressedrow.check = flg;
928d487561eSHong Zhang       break;
9293d472b54SHong Zhang     case MAT_SPD:
9303d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
9313d472b54SHong Zhang       A->spd                             = flg;
9323d472b54SHong Zhang       if (flg) {
9333d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
9343d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
9353d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
9363d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
9373d472b54SHong Zhang       }
9383d472b54SHong Zhang       break;
939b1646e73SJed Brown     case MAT_SYMMETRIC:
940b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
941b1646e73SJed Brown     case MAT_HERMITIAN:
942b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
9434e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
944a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
945a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
946290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
947a65d3064SKris Buschelman       break;
948b87ac2d8SJed Brown     case MAT_USE_INODES:
949b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
950b87ac2d8SJed Brown       break;
951a65d3064SKris Buschelman     default:
952e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
953a65d3064SKris Buschelman   }
9544108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
9553a40ed3dSBarry Smith   PetscFunctionReturn(0);
95617ab2063SBarry Smith }
95717ab2063SBarry Smith 
9584a2ae208SSatish Balay #undef __FUNCT__
9594a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
960dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
96117ab2063SBarry Smith {
962416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9636849ba73SBarry Smith   PetscErrorCode ierr;
964d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
96535e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
96617ab2063SBarry Smith 
9673a40ed3dSBarry Smith   PetscFunctionBegin;
968d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
969e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
97035e7444dSHong Zhang 
971d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
972d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
97335e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
97435e7444dSHong Zhang     for (i=0; i<n; i++) x[i] = aa[diag[i]];
97535e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
97635e7444dSHong Zhang     PetscFunctionReturn(0);
97735e7444dSHong Zhang   }
97835e7444dSHong Zhang 
9792dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
9801ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
98135e7444dSHong Zhang   for (i=0; i<n; i++) {
98235e7444dSHong Zhang     nz = ai[i+1] - ai[i];
9832f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
98435e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
98535e7444dSHong Zhang       if (aj[j] == i) {
98635e7444dSHong Zhang         x[i] = aa[j];
98717ab2063SBarry Smith         break;
98817ab2063SBarry Smith       }
98917ab2063SBarry Smith     }
99017ab2063SBarry Smith   }
9911ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
9923a40ed3dSBarry Smith   PetscFunctionReturn(0);
99317ab2063SBarry Smith }
99417ab2063SBarry Smith 
995c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
9964a2ae208SSatish Balay #undef __FUNCT__
9974a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
998dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
99917ab2063SBarry Smith {
1000416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10015c897100SBarry Smith   PetscScalar       *x,*y;
1002dfbe8321SBarry Smith   PetscErrorCode    ierr;
1003d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10045c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1005a77337e4SBarry Smith   MatScalar         *v;
1006a77337e4SBarry Smith   PetscScalar       alpha;
100704fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10083447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1009ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10105c897100SBarry Smith #endif
101117ab2063SBarry Smith 
10123a40ed3dSBarry Smith   PetscFunctionBegin;
10132e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
10141ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10151ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10165c897100SBarry Smith 
10175c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1018bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
10195c897100SBarry Smith #else
10203447b6efSHong Zhang   if (usecprow){
10213447b6efSHong Zhang     m    = cprow.nrows;
10223447b6efSHong Zhang     ii   = cprow.i;
10237b2bb3b9SHong Zhang     ridx = cprow.rindex;
10243447b6efSHong Zhang   } else {
10253447b6efSHong Zhang     ii = a->i;
10263447b6efSHong Zhang   }
102717ab2063SBarry Smith   for (i=0; i<m; i++) {
10283447b6efSHong Zhang     idx   = a->j + ii[i] ;
10293447b6efSHong Zhang     v     = a->a + ii[i] ;
10303447b6efSHong Zhang     n     = ii[i+1] - ii[i];
10313447b6efSHong Zhang     if (usecprow){
10327b2bb3b9SHong Zhang       alpha = x[ridx[i]];
10333447b6efSHong Zhang     } else {
103417ab2063SBarry Smith       alpha = x[i];
10353447b6efSHong Zhang     }
103604fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
103717ab2063SBarry Smith   }
10385c897100SBarry Smith #endif
1039dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10401ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10411ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10423a40ed3dSBarry Smith   PetscFunctionReturn(0);
104317ab2063SBarry Smith }
104417ab2063SBarry Smith 
10454a2ae208SSatish Balay #undef __FUNCT__
10465c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1047dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
10485c897100SBarry Smith {
1049dfbe8321SBarry Smith   PetscErrorCode ierr;
10505c897100SBarry Smith 
10515c897100SBarry Smith   PetscFunctionBegin;
1052170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
10535c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
10545c897100SBarry Smith   PetscFunctionReturn(0);
10555c897100SBarry Smith }
10565c897100SBarry Smith 
1057c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10585c897100SBarry Smith #undef __FUNCT__
10594a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1060dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
106117ab2063SBarry Smith {
1062416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1063d9fead3dSBarry Smith   PetscScalar       *y;
106454f21887SBarry Smith   const PetscScalar *x;
106554f21887SBarry Smith   const MatScalar   *aa;
1066dfbe8321SBarry Smith   PetscErrorCode    ierr;
1067003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1068003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
10698aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1070362ced78SSatish Balay   PetscScalar       sum;
1071ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
107217ab2063SBarry Smith 
1073b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
107497952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1075fee21e36SBarry Smith #endif
1076fee21e36SBarry Smith 
10773a40ed3dSBarry Smith   PetscFunctionBegin;
10783649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
10791ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
108097952fefSHong Zhang   aj  = a->j;
108197952fefSHong Zhang   aa  = a->a;
1082416022c9SBarry Smith   ii  = a->i;
10834eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
108497952fefSHong Zhang     m    = a->compressedrow.nrows;
108597952fefSHong Zhang     ii   = a->compressedrow.i;
108697952fefSHong Zhang     ridx = a->compressedrow.rindex;
108797952fefSHong Zhang     for (i=0; i<m; i++){
108897952fefSHong Zhang       n   = ii[i+1] - ii[i];
108997952fefSHong Zhang       aj  = a->j + ii[i];
109097952fefSHong Zhang       aa  = a->a + ii[i];
109197952fefSHong Zhang       sum = 0.0;
1092a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1093003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1094003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
109597952fefSHong Zhang       y[*ridx++] = sum;
109697952fefSHong Zhang     }
109797952fefSHong Zhang   } else { /* do not use compressed row format */
1098b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1099b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1100b05257ddSBarry Smith #else
110117ab2063SBarry Smith     for (i=0; i<m; i++) {
1102003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1103003131ecSBarry Smith       aj  = a->j + ii[i];
1104003131ecSBarry Smith       aa  = a->a + ii[i];
110517ab2063SBarry Smith       sum  = 0.0;
1106a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1107003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
110817ab2063SBarry Smith       y[i] = sum;
110917ab2063SBarry Smith     }
11108d195f9aSBarry Smith #endif
1111b05257ddSBarry Smith   }
1112dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
11133649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
11141ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11153a40ed3dSBarry Smith   PetscFunctionReturn(0);
111617ab2063SBarry Smith }
111717ab2063SBarry Smith 
1118c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
11194a2ae208SSatish Balay #undef __FUNCT__
11204a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1121dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
112217ab2063SBarry Smith {
1123416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
112454f21887SBarry Smith   PetscScalar     *x,*y,*z;
112554f21887SBarry Smith   const MatScalar *aa;
1126dfbe8321SBarry Smith   PetscErrorCode  ierr;
1127d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
1128aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
112997952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
1130362ced78SSatish Balay   PetscScalar     sum;
1131ace3abfcSBarry Smith   PetscBool       usecprow=a->compressedrow.use;
1132e36a17ebSSatish Balay #endif
11339ea0dfa2SSatish Balay 
11343a40ed3dSBarry Smith   PetscFunctionBegin;
11351ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
11361ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11372e8a6d31SBarry Smith   if (zz != yy) {
11381ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
11392e8a6d31SBarry Smith   } else {
11402e8a6d31SBarry Smith     z = y;
11412e8a6d31SBarry Smith   }
1142bfeeae90SHong Zhang 
114397952fefSHong Zhang   aj  = a->j;
114497952fefSHong Zhang   aa  = a->a;
1145cddf8d76SBarry Smith   ii  = a->i;
1146aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
114797952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
114802ab625aSSatish Balay #else
11494eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
11504eb6d288SHong Zhang     if (zz != yy){
11514eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
11524eb6d288SHong Zhang     }
115397952fefSHong Zhang     m    = a->compressedrow.nrows;
115497952fefSHong Zhang     ii   = a->compressedrow.i;
115597952fefSHong Zhang     ridx = a->compressedrow.rindex;
115697952fefSHong Zhang     for (i=0; i<m; i++){
115797952fefSHong Zhang       n  = ii[i+1] - ii[i];
115897952fefSHong Zhang       aj  = a->j + ii[i];
115997952fefSHong Zhang       aa  = a->a + ii[i];
116097952fefSHong Zhang       sum = y[*ridx];
116197952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
116297952fefSHong Zhang       z[*ridx++] = sum;
116397952fefSHong Zhang     }
116497952fefSHong Zhang   } else { /* do not use compressed row format */
116517ab2063SBarry Smith     for (i=0; i<m; i++) {
11669ea0dfa2SSatish Balay       jrow = ii[i];
11679ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
116817ab2063SBarry Smith       sum  = y[i];
11699ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
117097952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
11719ea0dfa2SSatish Balay       }
117217ab2063SBarry Smith       z[i] = sum;
117317ab2063SBarry Smith     }
117497952fefSHong Zhang   }
117502ab625aSSatish Balay #endif
1176dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11771ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11781ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11792e8a6d31SBarry Smith   if (zz != yy) {
11801ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
11812e8a6d31SBarry Smith   }
11828154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
11836b375ea7SVictor Minden   /*
1184918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1185918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1186918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
11876b375ea7SVictor Minden   */
1188918e98c3SVictor Minden #endif
11893a40ed3dSBarry Smith   PetscFunctionReturn(0);
119017ab2063SBarry Smith }
119117ab2063SBarry Smith 
119217ab2063SBarry Smith /*
119317ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
119417ab2063SBarry Smith */
11954a2ae208SSatish Balay #undef __FUNCT__
11964a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1197dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
119817ab2063SBarry Smith {
1199416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12006849ba73SBarry Smith   PetscErrorCode ierr;
1201d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
120217ab2063SBarry Smith 
12033a40ed3dSBarry Smith   PetscFunctionBegin;
120409f38230SBarry Smith   if (!a->diag) {
120509f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
12069518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
120709f38230SBarry Smith   }
1208d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
120909f38230SBarry Smith     a->diag[i] = a->i[i+1];
1210bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1211bfeeae90SHong Zhang       if (a->j[j] == i) {
121209f38230SBarry Smith         a->diag[i] = j;
121317ab2063SBarry Smith         break;
121417ab2063SBarry Smith       }
121517ab2063SBarry Smith     }
121617ab2063SBarry Smith   }
12173a40ed3dSBarry Smith   PetscFunctionReturn(0);
121817ab2063SBarry Smith }
121917ab2063SBarry Smith 
1220be5855fcSBarry Smith /*
1221be5855fcSBarry Smith      Checks for missing diagonals
1222be5855fcSBarry Smith */
12234a2ae208SSatish Balay #undef __FUNCT__
12244a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1225ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1226be5855fcSBarry Smith {
1227be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
122897f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1229be5855fcSBarry Smith 
1230be5855fcSBarry Smith   PetscFunctionBegin;
123109f38230SBarry Smith   *missing = PETSC_FALSE;
1232d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
123309f38230SBarry Smith     *missing  = PETSC_TRUE;
123409f38230SBarry Smith     if (d) *d = 0;
123509f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
123609f38230SBarry Smith   } else {
1237f1e2ffcdSBarry Smith     diag = a->diag;
1238d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1239bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
124009f38230SBarry Smith 	*missing = PETSC_TRUE;
124109f38230SBarry Smith 	if (d) *d = i;
124209f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
124309f38230SBarry Smith       }
1244be5855fcSBarry Smith     }
1245be5855fcSBarry Smith   }
1246be5855fcSBarry Smith   PetscFunctionReturn(0);
1247be5855fcSBarry Smith }
1248be5855fcSBarry Smith 
124971f1c65dSBarry Smith EXTERN_C_BEGIN
125071f1c65dSBarry Smith #undef __FUNCT__
125171f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
12527087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
125371f1c65dSBarry Smith {
125471f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
125571f1c65dSBarry Smith   PetscErrorCode ierr;
1256d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
125754f21887SBarry Smith   MatScalar      *v = a->a;
125854f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
125971f1c65dSBarry Smith 
126071f1c65dSBarry Smith   PetscFunctionBegin;
126171f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
126271f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
126371f1c65dSBarry Smith   diag = a->diag;
126471f1c65dSBarry Smith   if (!a->idiag) {
126571f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
126671f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
126771f1c65dSBarry Smith     v        = a->a;
126871f1c65dSBarry Smith   }
126971f1c65dSBarry Smith   mdiag = a->mdiag;
127071f1c65dSBarry Smith   idiag = a->idiag;
127171f1c65dSBarry Smith 
1272028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
127371f1c65dSBarry Smith     for (i=0; i<m; i++) {
127471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1275e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
127671f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
127771f1c65dSBarry Smith     }
127871f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
127971f1c65dSBarry Smith   } else {
128071f1c65dSBarry Smith     for (i=0; i<m; i++) {
128171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
128271f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
128371f1c65dSBarry Smith     }
1284dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
128571f1c65dSBarry Smith   }
128671f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
128771f1c65dSBarry Smith   PetscFunctionReturn(0);
128871f1c65dSBarry Smith }
12895a9745a3SMatthew Knepley EXTERN_C_END
129071f1c65dSBarry Smith 
1291c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
12924a2ae208SSatish Balay #undef __FUNCT__
129341f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
129441f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
129517ab2063SBarry Smith {
1296416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1297e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1298e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
129954f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1300dfbe8321SBarry Smith   PetscErrorCode     ierr;
1301d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
130297f1f81fSBarry Smith   const PetscInt     *idx,*diag;
130317ab2063SBarry Smith 
13043a40ed3dSBarry Smith   PetscFunctionBegin;
1305b965ef7fSBarry Smith   its = its*lits;
130691723122SBarry Smith 
130771f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
130871f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
130971f1c65dSBarry Smith   a->fshift = fshift;
131071f1c65dSBarry Smith   a->omega  = omega;
1311ed480e8bSBarry Smith 
131271f1c65dSBarry Smith   diag = a->diag;
131371f1c65dSBarry Smith   t     = a->ssor_work;
1314ed480e8bSBarry Smith   idiag = a->idiag;
131571f1c65dSBarry Smith   mdiag = a->mdiag;
1316ed480e8bSBarry Smith 
13171ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
13183649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
131971f1c65dSBarry Smith   CHKMEMQ;
1320ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
132117ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
132217ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1323ed480e8bSBarry Smith     bs = b;
132417ab2063SBarry Smith     for (i=0; i<m; i++) {
132571f1c65dSBarry Smith         d    = fshift + mdiag[i];
1326416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1327ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1328ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
132917ab2063SBarry Smith         sum  = b[i]*d/omega;
1330003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
133117ab2063SBarry Smith         x[i] = sum;
133217ab2063SBarry Smith     }
13331ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13343649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1335efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13363a40ed3dSBarry Smith     PetscFunctionReturn(0);
133717ab2063SBarry Smith   }
1338c783ea89SBarry Smith 
133948af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1340e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
13413a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
134217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1343887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
134417ab2063SBarry Smith 
134517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
134617ab2063SBarry Smith 
1347887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
134817ab2063SBarry Smith     */
134917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
135017ab2063SBarry Smith 
135117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
135217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1353416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1354ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1355ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
135617ab2063SBarry Smith       sum  = b[i];
1357e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1358ed480e8bSBarry Smith       x[i] = sum*idiag[i];
135917ab2063SBarry Smith     }
136017ab2063SBarry Smith 
136117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1362416022c9SBarry Smith     v = a->a;
1363ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
136417ab2063SBarry Smith 
136517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1366ed480e8bSBarry Smith     ts = t;
1367416022c9SBarry Smith     diag = a->diag;
136817ab2063SBarry Smith     for (i=0; i<m; i++) {
1369416022c9SBarry Smith       n    = diag[i] - a->i[i];
1370ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1371ed480e8bSBarry Smith       v    = a->a + a->i[i];
137217ab2063SBarry Smith       sum  = t[i];
1373003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1374ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1375733d66baSBarry Smith       /*  x = x + t */
1376733d66baSBarry Smith       x[i] += t[i];
137717ab2063SBarry Smith     }
137817ab2063SBarry Smith 
1379dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
13801ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13813649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
13823a40ed3dSBarry Smith     PetscFunctionReturn(0);
138317ab2063SBarry Smith   }
138417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
138517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
138617ab2063SBarry Smith       for (i=0; i<m; i++) {
1387416022c9SBarry Smith         n    = diag[i] - a->i[i];
1388ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1389ed480e8bSBarry Smith         v    = a->a + a->i[i];
139017ab2063SBarry Smith         sum  = b[i];
1391e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
13925c99c7daSBarry Smith         t[i] = sum;
1393ed480e8bSBarry Smith         x[i] = sum*idiag[i];
139417ab2063SBarry Smith       }
13955c99c7daSBarry Smith       xb = t;
1396efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13973a40ed3dSBarry Smith     } else xb = b;
139817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
139917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1400416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1401ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1402ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
140317ab2063SBarry Smith         sum  = xb[i];
1404e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
14055c99c7daSBarry Smith         if (xb == b) {
1406ed480e8bSBarry Smith           x[i] = sum*idiag[i];
14075c99c7daSBarry Smith         } else {
14085c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
140917ab2063SBarry Smith         }
14105c99c7daSBarry Smith       }
1411efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
141217ab2063SBarry Smith     }
141317ab2063SBarry Smith     its--;
141417ab2063SBarry Smith   }
141517ab2063SBarry Smith   while (its--) {
141617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
141717ab2063SBarry Smith       for (i=0; i<m; i++) {
1418416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1419ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1420ed480e8bSBarry Smith         v    = a->a + a->i[i];
142117ab2063SBarry Smith         sum  = b[i];
1422e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1423ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
142417ab2063SBarry Smith       }
14259f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
142617ab2063SBarry Smith     }
142717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
142817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1429416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1430ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1431ed480e8bSBarry Smith         v    = a->a + a->i[i];
143217ab2063SBarry Smith         sum  = b[i];
1433e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1434ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
143517ab2063SBarry Smith       }
14369f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
143717ab2063SBarry Smith     }
143817ab2063SBarry Smith   }
14391ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
14403649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
144171f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
144217ab2063SBarry Smith }
144317ab2063SBarry Smith 
14442af78befSBarry Smith 
14454a2ae208SSatish Balay #undef __FUNCT__
14464a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1447dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
144817ab2063SBarry Smith {
1449416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
14504e220ebcSLois Curfman McInnes 
14513a40ed3dSBarry Smith   PetscFunctionBegin;
14524e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
14534e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
14544e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
14554e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
14564e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
14578e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
14587adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1459d5f3da31SBarry Smith   if (A->factortype) {
14604e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
14614e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
14624e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
14634e220ebcSLois Curfman McInnes   } else {
14644e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
14654e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
14664e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
14674e220ebcSLois Curfman McInnes   }
14683a40ed3dSBarry Smith   PetscFunctionReturn(0);
146917ab2063SBarry Smith }
147017ab2063SBarry Smith 
14714a2ae208SSatish Balay #undef __FUNCT__
14724a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
14732b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
147417ab2063SBarry Smith {
1475416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
14763b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
14776849ba73SBarry Smith   PetscErrorCode    ierr;
147897b48c8fSBarry Smith   const PetscScalar *xx;
147997b48c8fSBarry Smith   PetscScalar       *bb;
1480ace3abfcSBarry Smith   PetscBool         missing;
148117ab2063SBarry Smith 
14823a40ed3dSBarry Smith   PetscFunctionBegin;
148397b48c8fSBarry Smith   if (x && b) {
148497b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
148597b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
148697b48c8fSBarry Smith     for (i=0; i<N; i++) {
148797b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
148897b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
148997b48c8fSBarry Smith     }
149097b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
149197b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
149297b48c8fSBarry Smith   }
149397b48c8fSBarry Smith 
1494a9817697SBarry Smith   if (a->keepnonzeropattern) {
1495f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1496e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1497bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1498f1e2ffcdSBarry Smith     }
1499f4df32b1SMatthew Knepley     if (diag != 0.0) {
150009f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1501e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1502f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1503f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1504f1e2ffcdSBarry Smith       }
1505f1e2ffcdSBarry Smith     }
150688e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1507f1e2ffcdSBarry Smith   } else {
1508f4df32b1SMatthew Knepley     if (diag != 0.0) {
150917ab2063SBarry Smith       for (i=0; i<N; i++) {
1510e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15117ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1512416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1513f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1514bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
15157ae801bdSBarry Smith         } else { /* in case row was completely empty */
1516f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
151717ab2063SBarry Smith         }
151817ab2063SBarry Smith       }
15193a40ed3dSBarry Smith     } else {
152017ab2063SBarry Smith       for (i=0; i<N; i++) {
1521e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1522416022c9SBarry Smith         a->ilen[rows[i]] = 0;
152317ab2063SBarry Smith       }
152417ab2063SBarry Smith     }
152588e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1526f1e2ffcdSBarry Smith   }
152743a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15283a40ed3dSBarry Smith   PetscFunctionReturn(0);
152917ab2063SBarry Smith }
153017ab2063SBarry Smith 
15314a2ae208SSatish Balay #undef __FUNCT__
15326e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
15336e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
15346e169961SBarry Smith {
15356e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
15366e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
15376e169961SBarry Smith   PetscErrorCode    ierr;
15382b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
15396e169961SBarry Smith   const PetscScalar *xx;
15406e169961SBarry Smith   PetscScalar       *bb;
15416e169961SBarry Smith 
15426e169961SBarry Smith   PetscFunctionBegin;
15436e169961SBarry Smith   if (x && b) {
15446e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
15456e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
15462b40b63fSBarry Smith     vecs = PETSC_TRUE;
15476e169961SBarry Smith   }
15486e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
15496e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
15506e169961SBarry Smith   for (i=0; i<N; i++) {
15516e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15526e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
15536e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
15546e169961SBarry Smith   }
15556e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
15566e169961SBarry Smith     if (!zeroed[i]) {
15576e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
15586e169961SBarry Smith         if (zeroed[a->j[j]]) {
15592b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
15606e169961SBarry Smith           a->a[j] = 0.0;
15616e169961SBarry Smith         }
15626e169961SBarry Smith       }
15632b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
15646e169961SBarry Smith   }
15656e169961SBarry Smith   if (x && b) {
15666e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
15676e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
15686e169961SBarry Smith   }
15696e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
15706e169961SBarry Smith   if (diag != 0.0) {
15716e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
15726e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
15736e169961SBarry Smith     for (i=0; i<N; i++) {
15746e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
15756e169961SBarry Smith     }
15766e169961SBarry Smith   }
15776e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
15786e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15796e169961SBarry Smith   PetscFunctionReturn(0);
15806e169961SBarry Smith }
15816e169961SBarry Smith 
15826e169961SBarry Smith #undef __FUNCT__
15834a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1584a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
158517ab2063SBarry Smith {
1586416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
158797f1f81fSBarry Smith   PetscInt   *itmp;
158817ab2063SBarry Smith 
15893a40ed3dSBarry Smith   PetscFunctionBegin;
1590e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
159117ab2063SBarry Smith 
1592416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1593bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
159417ab2063SBarry Smith   if (idx) {
1595bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1596bfeeae90SHong Zhang     if (*nz) {
15974e093b46SBarry Smith       *idx = itmp;
159817ab2063SBarry Smith     }
159917ab2063SBarry Smith     else *idx = 0;
160017ab2063SBarry Smith   }
16013a40ed3dSBarry Smith   PetscFunctionReturn(0);
160217ab2063SBarry Smith }
160317ab2063SBarry Smith 
1604bfeeae90SHong Zhang /* remove this function? */
16054a2ae208SSatish Balay #undef __FUNCT__
16064a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1607a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
160817ab2063SBarry Smith {
16093a40ed3dSBarry Smith   PetscFunctionBegin;
16103a40ed3dSBarry Smith   PetscFunctionReturn(0);
161117ab2063SBarry Smith }
161217ab2063SBarry Smith 
16134a2ae208SSatish Balay #undef __FUNCT__
16144a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1615dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
161617ab2063SBarry Smith {
1617416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
161854f21887SBarry Smith   MatScalar      *v = a->a;
161936db0b34SBarry Smith   PetscReal      sum = 0.0;
16206849ba73SBarry Smith   PetscErrorCode ierr;
162197f1f81fSBarry Smith   PetscInt       i,j;
162217ab2063SBarry Smith 
16233a40ed3dSBarry Smith   PetscFunctionBegin;
162417ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1625416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1626aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
162736db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
162817ab2063SBarry Smith #else
162917ab2063SBarry Smith       sum += (*v)*(*v); v++;
163017ab2063SBarry Smith #endif
163117ab2063SBarry Smith     }
1632064f8208SBarry Smith     *nrm = sqrt(sum);
16333a40ed3dSBarry Smith   } else if (type == NORM_1) {
163436db0b34SBarry Smith     PetscReal *tmp;
163597f1f81fSBarry Smith     PetscInt    *jj = a->j;
1636d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1637d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1638064f8208SBarry Smith     *nrm = 0.0;
1639416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1640bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
164117ab2063SBarry Smith     }
1642d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1643064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
164417ab2063SBarry Smith     }
1645606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
16463a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1647064f8208SBarry Smith     *nrm = 0.0;
1648d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1649bfeeae90SHong Zhang       v = a->a + a->i[j];
165017ab2063SBarry Smith       sum = 0.0;
1651416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1652cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
165317ab2063SBarry Smith       }
1654064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
165517ab2063SBarry Smith     }
16563a40ed3dSBarry Smith   } else {
1657e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
165817ab2063SBarry Smith   }
16593a40ed3dSBarry Smith   PetscFunctionReturn(0);
166017ab2063SBarry Smith }
166117ab2063SBarry Smith 
16624a2ae208SSatish Balay #undef __FUNCT__
16634a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1664fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
166517ab2063SBarry Smith {
1666416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1667416022c9SBarry Smith   Mat            C;
16686849ba73SBarry Smith   PetscErrorCode ierr;
1669d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
167054f21887SBarry Smith   MatScalar      *array = a->a;
167117ab2063SBarry Smith 
16723a40ed3dSBarry Smith   PetscFunctionBegin;
1673e32f2f54SBarry 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");
1674fc4dec0aSBarry Smith 
1675fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1676d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1677d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1678bfeeae90SHong Zhang 
1679bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
16807adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1681d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
16827adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1683ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1684606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1685a541d17aSBarry Smith   } else {
1686a541d17aSBarry Smith     C = *B;
1687a541d17aSBarry Smith   }
1688a541d17aSBarry Smith 
168917ab2063SBarry Smith   for (i=0; i<m; i++) {
169017ab2063SBarry Smith     len    = ai[i+1]-ai[i];
169187d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1692b9b97703SBarry Smith     array += len;
1693b9b97703SBarry Smith     aj    += len;
169417ab2063SBarry Smith   }
16956d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16966d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
169717ab2063SBarry Smith 
1698815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1699416022c9SBarry Smith     *B = C;
170017ab2063SBarry Smith   } else {
1701eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
170217ab2063SBarry Smith   }
17033a40ed3dSBarry Smith   PetscFunctionReturn(0);
170417ab2063SBarry Smith }
170517ab2063SBarry Smith 
1706cd0d46ebSvictorle EXTERN_C_BEGIN
1707cd0d46ebSvictorle #undef __FUNCT__
17085fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
17097087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1710cd0d46ebSvictorle {
1711cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
171254f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
171354f21887SBarry Smith   MatScalar      *va,*vb;
17146849ba73SBarry Smith   PetscErrorCode ierr;
171597f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1716cd0d46ebSvictorle 
1717cd0d46ebSvictorle   PetscFunctionBegin;
1718cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1719cd0d46ebSvictorle 
1720cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1721cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
17225485867bSBarry Smith   if (ma!=nb || na!=mb){
17235485867bSBarry Smith     *f = PETSC_FALSE;
17245485867bSBarry Smith     PetscFunctionReturn(0);
17255485867bSBarry Smith   }
1726cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1727cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1728cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
172997f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
173097f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1731cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1732cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1733cd0d46ebSvictorle 
1734cd0d46ebSvictorle   *f = PETSC_TRUE;
1735cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1736cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
173797f1f81fSBarry Smith       PetscInt         idc,idr;
17385485867bSBarry Smith       PetscScalar vc,vr;
1739cd0d46ebSvictorle       /* column/row index/value */
17405485867bSBarry Smith       idc = adx[aptr[i]];
17415485867bSBarry Smith       idr = bdx[bptr[idc]];
17425485867bSBarry Smith       vc  = va[aptr[i]];
17435485867bSBarry Smith       vr  = vb[bptr[idc]];
17445485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
17455485867bSBarry Smith         *f = PETSC_FALSE;
17465485867bSBarry Smith         goto done;
1747cd0d46ebSvictorle       } else {
17485485867bSBarry Smith         aptr[i]++;
17495485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1750cd0d46ebSvictorle       }
1751cd0d46ebSvictorle     }
1752cd0d46ebSvictorle   }
1753cd0d46ebSvictorle  done:
1754cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
17553aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1756cd0d46ebSvictorle   PetscFunctionReturn(0);
1757cd0d46ebSvictorle }
1758cd0d46ebSvictorle EXTERN_C_END
1759cd0d46ebSvictorle 
17601cbb95d3SBarry Smith EXTERN_C_BEGIN
17611cbb95d3SBarry Smith #undef __FUNCT__
17621cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
17637087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
17641cbb95d3SBarry Smith {
17651cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
176654f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
176754f21887SBarry Smith   MatScalar      *va,*vb;
17681cbb95d3SBarry Smith   PetscErrorCode ierr;
17691cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
17701cbb95d3SBarry Smith 
17711cbb95d3SBarry Smith   PetscFunctionBegin;
17721cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
17731cbb95d3SBarry Smith 
17741cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
17751cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
17761cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
17771cbb95d3SBarry Smith     *f = PETSC_FALSE;
17781cbb95d3SBarry Smith     PetscFunctionReturn(0);
17791cbb95d3SBarry Smith   }
17801cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
17811cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
17821cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
17831cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
17841cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
17851cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
17861cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
17871cbb95d3SBarry Smith 
17881cbb95d3SBarry Smith   *f = PETSC_TRUE;
17891cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
17901cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
17911cbb95d3SBarry Smith       PetscInt         idc,idr;
17921cbb95d3SBarry Smith       PetscScalar vc,vr;
17931cbb95d3SBarry Smith       /* column/row index/value */
17941cbb95d3SBarry Smith       idc = adx[aptr[i]];
17951cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
17961cbb95d3SBarry Smith       vc  = va[aptr[i]];
17971cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
17981cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
17991cbb95d3SBarry Smith         *f = PETSC_FALSE;
18001cbb95d3SBarry Smith         goto done;
18011cbb95d3SBarry Smith       } else {
18021cbb95d3SBarry Smith         aptr[i]++;
18031cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
18041cbb95d3SBarry Smith       }
18051cbb95d3SBarry Smith     }
18061cbb95d3SBarry Smith   }
18071cbb95d3SBarry Smith  done:
18081cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
18091cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
18101cbb95d3SBarry Smith   PetscFunctionReturn(0);
18111cbb95d3SBarry Smith }
18121cbb95d3SBarry Smith EXTERN_C_END
18131cbb95d3SBarry Smith 
18149e29f15eSvictorle #undef __FUNCT__
18159e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1816ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
18179e29f15eSvictorle {
1818dfbe8321SBarry Smith   PetscErrorCode ierr;
18199e29f15eSvictorle   PetscFunctionBegin;
18205485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
18219e29f15eSvictorle   PetscFunctionReturn(0);
18229e29f15eSvictorle }
18239e29f15eSvictorle 
18244a2ae208SSatish Balay #undef __FUNCT__
18251cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
1826ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
18271cbb95d3SBarry Smith {
18281cbb95d3SBarry Smith   PetscErrorCode ierr;
18291cbb95d3SBarry Smith   PetscFunctionBegin;
18301cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
18311cbb95d3SBarry Smith   PetscFunctionReturn(0);
18321cbb95d3SBarry Smith }
18331cbb95d3SBarry Smith 
18341cbb95d3SBarry Smith #undef __FUNCT__
18354a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1836dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
183717ab2063SBarry Smith {
1838416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
183954f21887SBarry Smith   PetscScalar    *l,*r,x;
184054f21887SBarry Smith   MatScalar      *v;
1841dfbe8321SBarry Smith   PetscErrorCode ierr;
1842d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
184317ab2063SBarry Smith 
18443a40ed3dSBarry Smith   PetscFunctionBegin;
184517ab2063SBarry Smith   if (ll) {
18463ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
18473ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1848e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1849e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
18501ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1851416022c9SBarry Smith     v = a->a;
185217ab2063SBarry Smith     for (i=0; i<m; i++) {
185317ab2063SBarry Smith       x = l[i];
1854416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
185517ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
185617ab2063SBarry Smith     }
18571ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1858efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
185917ab2063SBarry Smith   }
186017ab2063SBarry Smith   if (rr) {
1861e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1862e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
18631ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1864416022c9SBarry Smith     v = a->a; jj = a->j;
186517ab2063SBarry Smith     for (i=0; i<nz; i++) {
1866bfeeae90SHong Zhang       (*v++) *= r[*jj++];
186717ab2063SBarry Smith     }
18681ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1869efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
187017ab2063SBarry Smith   }
18713a40ed3dSBarry Smith   PetscFunctionReturn(0);
187217ab2063SBarry Smith }
187317ab2063SBarry Smith 
18744a2ae208SSatish Balay #undef __FUNCT__
18754a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
187697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
187717ab2063SBarry Smith {
1878db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
18796849ba73SBarry Smith   PetscErrorCode ierr;
1880d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
188197f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
18825d0c19d7SBarry Smith   const PetscInt *irow,*icol;
18835d0c19d7SBarry Smith   PetscInt       nrows,ncols;
188497f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
188554f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1886416022c9SBarry Smith   Mat            C;
1887ace3abfcSBarry Smith   PetscBool      stride,sorted;
188817ab2063SBarry Smith 
18893a40ed3dSBarry Smith   PetscFunctionBegin;
189014ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1891e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
189214ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1893e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
189499141d43SSatish Balay 
189517ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1896b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1897b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
189817ab2063SBarry Smith 
1899fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
19000dbe5b1eSSatish Balay   ierr = PetscTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
1901fee21e36SBarry Smith   if (stride && step == 1) {
190202834360SBarry Smith     /* special case of contiguous rows */
19030e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
190402834360SBarry Smith     /* loop over new rows determining lens and starting points */
190502834360SBarry Smith     for (i=0; i<nrows; i++) {
1906bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1907a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
190802834360SBarry Smith       for (k=kstart; k<kend; k++) {
1909bfeeae90SHong Zhang         if (aj[k] >= first) {
191002834360SBarry Smith           starts[i] = k;
191102834360SBarry Smith           break;
191202834360SBarry Smith 	}
191302834360SBarry Smith       }
1914a2744918SBarry Smith       sum = 0;
191502834360SBarry Smith       while (k < kend) {
1916bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1917a2744918SBarry Smith         sum++;
191802834360SBarry Smith       }
1919a2744918SBarry Smith       lens[i] = sum;
192002834360SBarry Smith     }
192102834360SBarry Smith     /* create submatrix */
1922cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
192397f1f81fSBarry Smith       PetscInt n_cols,n_rows;
192408480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1925e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1926d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
192708480c60SBarry Smith       C = *B;
19283a40ed3dSBarry Smith     } else {
19297adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1930f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
19317adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1932ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
193308480c60SBarry Smith     }
1934db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1935db02288aSLois Curfman McInnes 
193602834360SBarry Smith     /* loop over rows inserting into submatrix */
1937db02288aSLois Curfman McInnes     a_new    = c->a;
1938db02288aSLois Curfman McInnes     j_new    = c->j;
1939db02288aSLois Curfman McInnes     i_new    = c->i;
1940bfeeae90SHong Zhang 
194102834360SBarry Smith     for (i=0; i<nrows; i++) {
1942a2744918SBarry Smith       ii    = starts[i];
1943a2744918SBarry Smith       lensi = lens[i];
1944a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1945a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
194602834360SBarry Smith       }
194787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1948a2744918SBarry Smith       a_new      += lensi;
1949a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1950a2744918SBarry Smith       c->ilen[i]  = lensi;
195102834360SBarry Smith     }
19520e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
19533a40ed3dSBarry Smith   } else {
195402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
19550e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
195697f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
19570e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
195817ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
195902834360SBarry Smith     /* determine lens of each row */
196002834360SBarry Smith     for (i=0; i<nrows; i++) {
1961bfeeae90SHong Zhang       kstart  = ai[irow[i]];
196202834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
196302834360SBarry Smith       lens[i] = 0;
196402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1965bfeeae90SHong Zhang         if (smap[aj[k]]) {
196602834360SBarry Smith           lens[i]++;
196702834360SBarry Smith         }
196802834360SBarry Smith       }
196902834360SBarry Smith     }
197017ab2063SBarry Smith     /* Create and fill new matrix */
1971a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
1972ace3abfcSBarry Smith       PetscBool  equal;
19730f5bd95cSBarry Smith 
197499141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1975e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1976d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
19770f5bd95cSBarry Smith       if (!equal) {
1978e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
197999141d43SSatish Balay       }
1980d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
198108480c60SBarry Smith       C = *B;
19823a40ed3dSBarry Smith     } else {
19837adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1984f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
19857adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1986ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
198708480c60SBarry Smith     }
198899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
198917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
199099141d43SSatish Balay       row    = irow[i];
1991bfeeae90SHong Zhang       kstart = ai[row];
199299141d43SSatish Balay       kend   = kstart + a->ilen[row];
1993bfeeae90SHong Zhang       mat_i  = c->i[i];
199499141d43SSatish Balay       mat_j  = c->j + mat_i;
199599141d43SSatish Balay       mat_a  = c->a + mat_i;
199699141d43SSatish Balay       mat_ilen = c->ilen + i;
199717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1998bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1999ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
200099141d43SSatish Balay           *mat_a++ = a->a[k];
200199141d43SSatish Balay           (*mat_ilen)++;
200299141d43SSatish Balay 
200317ab2063SBarry Smith         }
200417ab2063SBarry Smith       }
200517ab2063SBarry Smith     }
200602834360SBarry Smith     /* Free work space */
200702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2008606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2009606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
201002834360SBarry Smith   }
20116d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20126d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
201317ab2063SBarry Smith 
201417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2015416022c9SBarry Smith   *B = C;
20163a40ed3dSBarry Smith   PetscFunctionReturn(0);
201717ab2063SBarry Smith }
201817ab2063SBarry Smith 
20191df811f5SHong Zhang #undef __FUNCT__
202082d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
202182d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
202282d44351SHong Zhang {
202382d44351SHong Zhang   PetscErrorCode ierr;
202482d44351SHong Zhang   Mat            B;
202582d44351SHong Zhang 
202682d44351SHong Zhang   PetscFunctionBegin;
202782d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
202882d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
202982d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
203082d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
203182d44351SHong Zhang   *subMat = B;
203282d44351SHong Zhang   PetscFunctionReturn(0);
203382d44351SHong Zhang }
203482d44351SHong Zhang 
203582d44351SHong Zhang #undef __FUNCT__
20364a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
20370481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2038a871dcd8SBarry Smith {
203963b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2040dfbe8321SBarry Smith   PetscErrorCode ierr;
204163b91edcSBarry Smith   Mat            outA;
2042ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
204363b91edcSBarry Smith 
20443a40ed3dSBarry Smith   PetscFunctionBegin;
2045e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
20461df811f5SHong Zhang 
2047b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2048b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2049a871dcd8SBarry Smith 
205063b91edcSBarry Smith   outA              = inA;
2051d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2052c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
20536bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2054c3122656SLisandro Dalcin   a->row = row;
2055c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
20566bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2057c3122656SLisandro Dalcin   a->col = col;
205863b91edcSBarry Smith 
205936db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
20606bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
20614c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
206252e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2063f0ec6fceSSatish Balay 
206494a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2065d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2066d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
206794a9d846SBarry Smith   }
206863b91edcSBarry Smith 
2069f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2070137fb511SHong Zhang   if (row_identity && col_identity) {
2071ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2072137fb511SHong Zhang   } else {
2073719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2074137fb511SHong Zhang   }
20753a40ed3dSBarry Smith   PetscFunctionReturn(0);
2076a871dcd8SBarry Smith }
2077a871dcd8SBarry Smith 
20784a2ae208SSatish Balay #undef __FUNCT__
20794a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2080f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2081f0b747eeSBarry Smith {
2082f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2083f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2084efee365bSSatish Balay   PetscErrorCode ierr;
20850805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
20863a40ed3dSBarry Smith 
20873a40ed3dSBarry Smith   PetscFunctionBegin;
2088f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2089efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
20903a40ed3dSBarry Smith   PetscFunctionReturn(0);
2091f0b747eeSBarry Smith }
2092f0b747eeSBarry Smith 
20934a2ae208SSatish Balay #undef __FUNCT__
20944a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
209597f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2096cddf8d76SBarry Smith {
2097dfbe8321SBarry Smith   PetscErrorCode ierr;
209897f1f81fSBarry Smith   PetscInt       i;
2099cddf8d76SBarry Smith 
21003a40ed3dSBarry Smith   PetscFunctionBegin;
2101cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2102b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2103cddf8d76SBarry Smith   }
2104cddf8d76SBarry Smith 
2105cddf8d76SBarry Smith   for (i=0; i<n; i++) {
21066a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2107cddf8d76SBarry Smith   }
21083a40ed3dSBarry Smith   PetscFunctionReturn(0);
2109cddf8d76SBarry Smith }
2110cddf8d76SBarry Smith 
21114a2ae208SSatish Balay #undef __FUNCT__
21124a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
211397f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
21144dcbc457SBarry Smith {
2115e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
21166849ba73SBarry Smith   PetscErrorCode ierr;
21175d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
21185d0c19d7SBarry Smith   const PetscInt *idx;
211997f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2120f1af5d2fSBarry Smith   PetscBT        table;
2121bbd702dbSSatish Balay 
21223a40ed3dSBarry Smith   PetscFunctionBegin;
2123d0f46423SBarry Smith   m     = A->rmap->n;
2124e4d965acSSatish Balay   ai    = a->i;
2125bfeeae90SHong Zhang   aj    = a->j;
21268a047759SSatish Balay 
2127e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
212806763907SSatish Balay 
212997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
21306831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
213106763907SSatish Balay 
2132e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2133b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2134e4d965acSSatish Balay     isz  = 0;
21356831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2136e4d965acSSatish Balay 
2137e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
21384dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2139b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2140e4d965acSSatish Balay 
2141dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2142e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2143f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
21444dcbc457SBarry Smith     }
214506763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
21466bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2147e4d965acSSatish Balay 
214804a348a9SBarry Smith     k = 0;
214904a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
215004a348a9SBarry Smith       n = isz;
215106763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2152e4d965acSSatish Balay         row   = nidx[k];
2153e4d965acSSatish Balay         start = ai[row];
2154e4d965acSSatish Balay         end   = ai[row+1];
215504a348a9SBarry Smith         for (l = start; l<end ; l++){
2156efb16452SHong Zhang           val = aj[l] ;
2157f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2158e4d965acSSatish Balay         }
2159e4d965acSSatish Balay       }
2160e4d965acSSatish Balay     }
216170b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2162e4d965acSSatish Balay   }
21636831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2164606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
21653a40ed3dSBarry Smith   PetscFunctionReturn(0);
21664dcbc457SBarry Smith }
216717ab2063SBarry Smith 
21680513a670SBarry Smith /* -------------------------------------------------------------- */
21694a2ae208SSatish Balay #undef __FUNCT__
21704a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2171dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
21720513a670SBarry Smith {
21730513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
21746849ba73SBarry Smith   PetscErrorCode ierr;
21753b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
21765d0c19d7SBarry Smith   const PetscInt *row,*col;
21775d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
217856cd22aeSBarry Smith   IS             icolp,irowp;
21793b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
21803b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
21810513a670SBarry Smith 
21823a40ed3dSBarry Smith   PetscFunctionBegin;
21834c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
218456cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
21854c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
218656cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
21870513a670SBarry Smith 
21880513a670SBarry Smith   /* determine lengths of permuted rows */
218997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
21900513a670SBarry Smith   for (i=0; i<m; i++) {
21910513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
21920513a670SBarry Smith   }
21937adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2194f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
21957adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2196ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2197606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
21980513a670SBarry Smith 
219997f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
22000513a670SBarry Smith   for (i=0; i<m; i++) {
220132ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
22020513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2203cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
220432ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
22050513a670SBarry Smith   }
2206606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
22073c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
22080513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22090513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
221056cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
221156cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
22126bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
22136bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
22143a40ed3dSBarry Smith   PetscFunctionReturn(0);
22150513a670SBarry Smith }
22160513a670SBarry Smith 
22174a2ae208SSatish Balay #undef __FUNCT__
22184a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2219dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2220cb5b572fSBarry Smith {
2221dfbe8321SBarry Smith   PetscErrorCode ierr;
2222cb5b572fSBarry Smith 
2223cb5b572fSBarry Smith   PetscFunctionBegin;
222433f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
222533f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2226be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2227be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2228be6bf707SBarry Smith 
2229700c5bfcSBarry 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");
2230d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2231cb5b572fSBarry Smith   } else {
2232cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2233cb5b572fSBarry Smith   }
2234cb5b572fSBarry Smith   PetscFunctionReturn(0);
2235cb5b572fSBarry Smith }
2236cb5b572fSBarry Smith 
22374a2ae208SSatish Balay #undef __FUNCT__
22384a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2239dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2240273d9f13SBarry Smith {
2241dfbe8321SBarry Smith   PetscErrorCode ierr;
2242273d9f13SBarry Smith 
2243273d9f13SBarry Smith   PetscFunctionBegin;
2244ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2245273d9f13SBarry Smith   PetscFunctionReturn(0);
2246273d9f13SBarry Smith }
2247273d9f13SBarry Smith 
22484a2ae208SSatish Balay #undef __FUNCT__
22494a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2250a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
22516c0721eeSBarry Smith {
22526c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
22536c0721eeSBarry Smith   PetscFunctionBegin;
22546c0721eeSBarry Smith   *array = a->a;
22556c0721eeSBarry Smith   PetscFunctionReturn(0);
22566c0721eeSBarry Smith }
22576c0721eeSBarry Smith 
22584a2ae208SSatish Balay #undef __FUNCT__
22594a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2260dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
22616c0721eeSBarry Smith {
22626c0721eeSBarry Smith   PetscFunctionBegin;
22636c0721eeSBarry Smith   PetscFunctionReturn(0);
22646c0721eeSBarry Smith }
2265273d9f13SBarry Smith 
2266ee4f033dSBarry Smith #undef __FUNCT__
2267ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2268dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2269ee4f033dSBarry Smith {
22706849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
22716849ba73SBarry Smith   PetscErrorCode ierr;
227297f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2273efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
227487828ca2SBarry Smith   PetscScalar    *vscale_array;
2275ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2276ee4f033dSBarry Smith   Vec            w1,w2,w3;
2277ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2278ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2279ee4f033dSBarry Smith 
2280ee4f033dSBarry Smith   PetscFunctionBegin;
2281ee4f033dSBarry Smith   if (!coloring->w1) {
2282ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
228352e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2284ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
228552e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2286ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
228752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2288ee4f033dSBarry Smith   }
2289ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2290ee4f033dSBarry Smith 
2291ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2292acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2293ee4f033dSBarry Smith   if (flg) {
2294ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2295ee4f033dSBarry Smith   } else {
2296ace3abfcSBarry Smith     PetscBool  assembled;
22970b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
22980b9b6f31SBarry Smith     if (assembled) {
2299ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2300ee4f033dSBarry Smith     }
23010b9b6f31SBarry Smith   }
2302ee4f033dSBarry Smith 
2303ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2304ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2305ee4f033dSBarry Smith 
2306ee4f033dSBarry Smith   /*
2307ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2308ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2309ee4f033dSBarry Smith   */
2310ee4f033dSBarry Smith   if (coloring->F) {
2311ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2312ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2313ee4f033dSBarry Smith     if (m1 != m2) {
2314ee4f033dSBarry Smith       coloring->F = 0;
2315ee4f033dSBarry Smith     }
2316ee4f033dSBarry Smith   }
2317ee4f033dSBarry Smith 
2318ee4f033dSBarry Smith   if (coloring->F) {
2319ee4f033dSBarry Smith     w1          = coloring->F;
2320ee4f033dSBarry Smith     coloring->F = 0;
2321ee4f033dSBarry Smith   } else {
232266f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2323ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
232466f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2325ee4f033dSBarry Smith   }
2326ee4f033dSBarry Smith 
2327ee4f033dSBarry Smith   /*
2328ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2329ee4f033dSBarry Smith   */
23301ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
23311ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2332ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2333ee4f033dSBarry Smith     /*
2334ee4f033dSBarry Smith        Loop over each column associated with color adding the
2335ee4f033dSBarry Smith        perturbation to the vector w3.
2336ee4f033dSBarry Smith     */
2337ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2338ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2339ee4f033dSBarry Smith       dx  = xx[col];
2340ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2341ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2342ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2343ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2344ee4f033dSBarry Smith #else
2345ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2346ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2347ee4f033dSBarry Smith #endif
2348ee4f033dSBarry Smith       dx                *= epsilon;
2349ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2350ee4f033dSBarry Smith     }
2351ee4f033dSBarry Smith   }
23521ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2353ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2354ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2355ee4f033dSBarry Smith 
2356ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2357ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2358ee4f033dSBarry Smith 
2359ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2360ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2361ee4f033dSBarry Smith 
23621ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2363ee4f033dSBarry Smith   /*
2364ee4f033dSBarry Smith       Loop over each color
2365ee4f033dSBarry Smith   */
2366ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
236749b058dcSBarry Smith     coloring->currentcolor = k;
2368ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
23691ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2370ee4f033dSBarry Smith     /*
2371ee4f033dSBarry Smith        Loop over each column associated with color adding the
2372ee4f033dSBarry Smith        perturbation to the vector w3.
2373ee4f033dSBarry Smith     */
2374ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2375ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2376ee4f033dSBarry Smith       dx  = xx[col];
23775b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2378ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2379ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2380ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2381ee4f033dSBarry Smith #else
2382ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2383ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2384ee4f033dSBarry Smith #endif
2385ee4f033dSBarry Smith       dx            *= epsilon;
2386e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2387ee4f033dSBarry Smith       w3_array[col] += dx;
2388ee4f033dSBarry Smith     }
23891ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2390ee4f033dSBarry Smith 
2391ee4f033dSBarry Smith     /*
2392ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2393ee4f033dSBarry Smith     */
2394ee4f033dSBarry Smith 
239566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2396ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
239766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2398efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2399ee4f033dSBarry Smith 
2400ee4f033dSBarry Smith     /*
2401ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2402ee4f033dSBarry Smith     */
24031ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2404ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2405ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2406ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2407ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2408ee4f033dSBarry Smith       srow   = row + start;
2409ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2410ee4f033dSBarry Smith     }
24111ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2412ee4f033dSBarry Smith   }
241349b058dcSBarry Smith   coloring->currentcolor = k;
24141ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
24151ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2416ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2417ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2418ee4f033dSBarry Smith   PetscFunctionReturn(0);
2419ee4f033dSBarry Smith }
2420ee4f033dSBarry Smith 
24218229c054SShri Abhyankar /*
24228229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
24238229c054SShri Abhyankar    have different nonzero structure.
24248229c054SShri Abhyankar */
2425ac90fabeSBarry Smith #undef __FUNCT__
24268229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
24278229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2428ec7775f6SShri Abhyankar {
24298229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2430ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2431ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2432ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2433ec7775f6SShri Abhyankar 
2434ec7775f6SShri Abhyankar   PetscFunctionBegin;
2435ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2436ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
24378af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
24388af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
24398af7cee1SJed Brown     nnz[i] = 0;
24408af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
24418af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
24428af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
24438af7cee1SJed Brown       nnz[i]++;
24448af7cee1SJed Brown     }
24458af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2446ec7775f6SShri Abhyankar   }
2447ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2448ec7775f6SShri Abhyankar }
2449ec7775f6SShri Abhyankar 
2450ec7775f6SShri Abhyankar #undef __FUNCT__
2451ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2452f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2453ac90fabeSBarry Smith {
2454dfbe8321SBarry Smith   PetscErrorCode ierr;
245597f1f81fSBarry Smith   PetscInt       i;
2456ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
24570805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2458ac90fabeSBarry Smith 
2459ac90fabeSBarry Smith   PetscFunctionBegin;
2460ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2461f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2462f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2463c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2464a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2465a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
24666bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2467a30b2313SHong Zhang     }
2468a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2469d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2470a30b2313SHong Zhang       y->XtoY = X;
2471407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2472c537a176SHong Zhang     }
2473f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
24741e2582c4SBarry 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);
2475ac90fabeSBarry Smith   } else {
24768229c054SShri Abhyankar     Mat      B;
24778229c054SShri Abhyankar     PetscInt *nnz;
247816b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2479ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2480bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
24814aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2482ec7775f6SShri Abhyankar     ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
24838229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
24848229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2485ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2486ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
24878229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2488ac90fabeSBarry Smith   }
2489ac90fabeSBarry Smith   PetscFunctionReturn(0);
2490ac90fabeSBarry Smith }
2491ac90fabeSBarry Smith 
2492521d7252SBarry Smith #undef __FUNCT__
2493521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2494521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2495521d7252SBarry Smith {
249641c166b1SJed Brown   PetscErrorCode ierr;
249741c166b1SJed Brown 
2498521d7252SBarry Smith   PetscFunctionBegin;
249941c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
250041c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2501521d7252SBarry Smith   PetscFunctionReturn(0);
2502521d7252SBarry Smith }
2503521d7252SBarry Smith 
2504354c94deSBarry Smith #undef __FUNCT__
2505354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
25067087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2507354c94deSBarry Smith {
2508354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2509354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2510354c94deSBarry Smith   PetscInt    i,nz;
2511354c94deSBarry Smith   PetscScalar *a;
2512354c94deSBarry Smith 
2513354c94deSBarry Smith   PetscFunctionBegin;
2514354c94deSBarry Smith   nz = aij->nz;
2515354c94deSBarry Smith   a  = aij->a;
2516354c94deSBarry Smith   for (i=0; i<nz; i++) {
2517354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2518354c94deSBarry Smith   }
2519354c94deSBarry Smith #else
2520354c94deSBarry Smith   PetscFunctionBegin;
2521354c94deSBarry Smith #endif
2522354c94deSBarry Smith   PetscFunctionReturn(0);
2523354c94deSBarry Smith }
2524354c94deSBarry Smith 
2525e34fafa9SBarry Smith #undef __FUNCT__
2526985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2527985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2528e34fafa9SBarry Smith {
2529e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2530e34fafa9SBarry Smith   PetscErrorCode ierr;
2531d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2532e34fafa9SBarry Smith   PetscReal      atmp;
2533985db425SBarry Smith   PetscScalar    *x;
2534e34fafa9SBarry Smith   MatScalar      *aa;
2535e34fafa9SBarry Smith 
2536e34fafa9SBarry Smith   PetscFunctionBegin;
2537e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2538e34fafa9SBarry Smith   aa   = a->a;
2539e34fafa9SBarry Smith   ai   = a->i;
2540e34fafa9SBarry Smith   aj   = a->j;
2541e34fafa9SBarry Smith 
2542985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2543e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2544e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2545e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2546e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2547e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
25489189402eSHong Zhang     x[i] = 0.0;
2549e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2550985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2551985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2552985db425SBarry Smith       aa++; aj++;
2553985db425SBarry Smith     }
2554985db425SBarry Smith   }
2555985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2556985db425SBarry Smith   PetscFunctionReturn(0);
2557985db425SBarry Smith }
2558985db425SBarry Smith 
2559985db425SBarry Smith #undef __FUNCT__
2560985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2561985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2562985db425SBarry Smith {
2563985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2564985db425SBarry Smith   PetscErrorCode ierr;
2565d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2566985db425SBarry Smith   PetscScalar    *x;
2567985db425SBarry Smith   MatScalar      *aa;
2568985db425SBarry Smith 
2569985db425SBarry Smith   PetscFunctionBegin;
2570e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2571985db425SBarry Smith   aa   = a->a;
2572985db425SBarry Smith   ai   = a->i;
2573985db425SBarry Smith   aj   = a->j;
2574985db425SBarry Smith 
2575985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2576985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2577985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2578e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2579985db425SBarry Smith   for (i=0; i<m; i++) {
2580985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2581d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2582985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2583985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2584985db425SBarry Smith       x[i] = 0.0;
2585985db425SBarry Smith       if (idx) {
2586985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2587985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2588985db425SBarry Smith           if (aj[j] > j) {
2589985db425SBarry Smith             idx[i] = j;
2590985db425SBarry Smith             break;
2591985db425SBarry Smith           }
2592985db425SBarry Smith         }
2593985db425SBarry Smith       }
2594985db425SBarry Smith     }
2595985db425SBarry Smith     for (j=0; j<ncols; j++){
2596985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2597985db425SBarry Smith       aa++; aj++;
2598985db425SBarry Smith     }
2599985db425SBarry Smith   }
2600985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2601985db425SBarry Smith   PetscFunctionReturn(0);
2602985db425SBarry Smith }
2603985db425SBarry Smith 
2604985db425SBarry Smith #undef __FUNCT__
2605c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2606c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2607c87e5d42SMatthew Knepley {
2608c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2609c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2610c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2611c87e5d42SMatthew Knepley   PetscReal      atmp;
2612c87e5d42SMatthew Knepley   PetscScalar    *x;
2613c87e5d42SMatthew Knepley   MatScalar      *aa;
2614c87e5d42SMatthew Knepley 
2615c87e5d42SMatthew Knepley   PetscFunctionBegin;
2616e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2617c87e5d42SMatthew Knepley   aa   = a->a;
2618c87e5d42SMatthew Knepley   ai   = a->i;
2619c87e5d42SMatthew Knepley   aj   = a->j;
2620c87e5d42SMatthew Knepley 
2621c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2622c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2623c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2624e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2625c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2626c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2627289a08f5SMatthew Knepley     if (ncols) {
2628289a08f5SMatthew Knepley       /* Get first nonzero */
2629289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2630289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2631289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2632289a08f5SMatthew Knepley       }
2633289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2634289a08f5SMatthew Knepley     } else {
2635289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2636289a08f5SMatthew Knepley     }
2637c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2638c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2639289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2640c87e5d42SMatthew Knepley       aa++; aj++;
2641c87e5d42SMatthew Knepley     }
2642c87e5d42SMatthew Knepley   }
2643c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2644c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2645c87e5d42SMatthew Knepley }
2646c87e5d42SMatthew Knepley 
2647c87e5d42SMatthew Knepley #undef __FUNCT__
2648985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2649985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2650985db425SBarry Smith {
2651985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2652985db425SBarry Smith   PetscErrorCode ierr;
2653d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2654985db425SBarry Smith   PetscScalar    *x;
2655985db425SBarry Smith   MatScalar      *aa;
2656985db425SBarry Smith 
2657985db425SBarry Smith   PetscFunctionBegin;
2658e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2659985db425SBarry Smith   aa   = a->a;
2660985db425SBarry Smith   ai   = a->i;
2661985db425SBarry Smith   aj   = a->j;
2662985db425SBarry Smith 
2663985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2664985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2665985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2666e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2667985db425SBarry Smith   for (i=0; i<m; i++) {
2668985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2669d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2670985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2671985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2672985db425SBarry Smith       x[i] = 0.0;
2673985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2674985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2675985db425SBarry Smith         for (j=0;j<ncols;j++) {
2676985db425SBarry Smith           if (aj[j] > j) {
2677985db425SBarry Smith             idx[i] = j;
2678985db425SBarry Smith             break;
2679985db425SBarry Smith           }
2680985db425SBarry Smith         }
2681985db425SBarry Smith       }
2682985db425SBarry Smith     }
2683985db425SBarry Smith     for (j=0; j<ncols; j++){
2684985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2685985db425SBarry Smith       aa++; aj++;
2686e34fafa9SBarry Smith     }
2687e34fafa9SBarry Smith   }
2688e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2689e34fafa9SBarry Smith   PetscFunctionReturn(0);
2690e34fafa9SBarry Smith }
26917087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2692682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
26930a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2694cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2695cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2696cb5b572fSBarry Smith        MatMult_SeqAIJ,
269797304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
26987c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
26997c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2700db4efbfdSBarry Smith        0,
2701db4efbfdSBarry Smith        0,
2702db4efbfdSBarry Smith        0,
2703db4efbfdSBarry Smith /*10*/ 0,
2704cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2705cb5b572fSBarry Smith        0,
270641f059aeSBarry Smith        MatSOR_SeqAIJ,
270717ab2063SBarry Smith        MatTranspose_SeqAIJ,
270897304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2709cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2710cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2711cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2712cb5b572fSBarry Smith        MatNorm_SeqAIJ,
271397304618SKris Buschelman /*20*/ 0,
2714cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2715cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2716cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2717d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2718db4efbfdSBarry Smith        0,
2719db4efbfdSBarry Smith        0,
2720db4efbfdSBarry Smith        0,
2721db4efbfdSBarry Smith        0,
2722d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2723db4efbfdSBarry Smith        0,
2724db4efbfdSBarry Smith        0,
27256c0721eeSBarry Smith        MatGetArray_SeqAIJ,
27266c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2727d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2728cb5b572fSBarry Smith        0,
2729cb5b572fSBarry Smith        0,
2730cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2731cb5b572fSBarry Smith        0,
2732d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2733cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2734cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2735cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2736cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2737d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2738cb5b572fSBarry Smith        MatScale_SeqAIJ,
2739cb5b572fSBarry Smith        0,
274079299369SBarry Smith        MatDiagonalSet_SeqAIJ,
27416e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
2742d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
27433b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
27443b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
27453b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2746a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2747d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2748b9617806SBarry Smith        0,
27490513a670SBarry Smith        0,
2750cda55fadSBarry Smith        MatPermute_SeqAIJ,
2751cda55fadSBarry Smith        0,
2752d519adbfSMatthew Knepley /*59*/ 0,
2753b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2754b9b97703SBarry Smith        MatView_SeqAIJ,
2755357abbc8SBarry Smith        0,
2756ee4f033dSBarry Smith        0,
2757d519adbfSMatthew Knepley /*64*/ 0,
2758ee4f033dSBarry Smith        0,
2759ee4f033dSBarry Smith        0,
2760ee4f033dSBarry Smith        0,
2761ee4f033dSBarry Smith        0,
2762d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2763c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2764ee4f033dSBarry Smith        0,
2765ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2766dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2767ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2768dcf5cc72SBarry Smith #else
2769dcf5cc72SBarry Smith        0,
2770dcf5cc72SBarry Smith #endif
2771d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
27723acb8795SBarry Smith        MatFDColoringApply_AIJ,
277397304618SKris Buschelman        0,
277497304618SKris Buschelman        0,
277597304618SKris Buschelman        0,
27766ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
277797304618SKris Buschelman        0,
277897304618SKris Buschelman        0,
277997304618SKris Buschelman        0,
2780bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2781d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
27821cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
27836284ec50SHong Zhang        0,
27846284ec50SHong Zhang        0,
2785bc011b1eSHong Zhang        0,
2786d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
278726be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
278826be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2789d439da42SKris Buschelman        MatPtAP_Basic,
27907ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2791d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2792bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2793bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2794bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
27957ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2796d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2797609c6c4dSKris Buschelman        0,
2798609c6c4dSKris Buschelman        0,
279987d4246cSBarry Smith        MatConjugate_SeqAIJ,
280087d4246cSBarry Smith        0,
2801d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
280299cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2803f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2804f5edf698SHong Zhang        0,
28052bebee5dSHong Zhang        0,
2806cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
2807985db425SBarry Smith        0,
28082af78befSBarry Smith        MatGetRowMin_SeqAIJ,
28092af78befSBarry Smith        0,
2810599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2811d519adbfSMatthew Knepley /*114*/0,
2812599ef60dSHong Zhang        0,
28133c2a7987SHong Zhang        0,
2814fe97e370SBarry Smith        0,
2815fbdbba38SShri Abhyankar        0,
2816fbdbba38SShri Abhyankar /*119*/0,
2817fbdbba38SShri Abhyankar        0,
2818fbdbba38SShri Abhyankar        0,
281982d44351SHong Zhang        0,
2820b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
2821b3a44c85SBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ
28229e29f15eSvictorle };
282317ab2063SBarry Smith 
2824fb2e594dSBarry Smith EXTERN_C_BEGIN
28254a2ae208SSatish Balay #undef __FUNCT__
28264a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
28277087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2828bef8e0ddSBarry Smith {
2829bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
283097f1f81fSBarry Smith   PetscInt   i,nz,n;
2831bef8e0ddSBarry Smith 
2832bef8e0ddSBarry Smith   PetscFunctionBegin;
2833bef8e0ddSBarry Smith 
2834bef8e0ddSBarry Smith   nz = aij->maxnz;
2835d0f46423SBarry Smith   n  = mat->rmap->n;
2836bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2837bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2838bef8e0ddSBarry Smith   }
2839bef8e0ddSBarry Smith   aij->nz = nz;
2840bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2841bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2842bef8e0ddSBarry Smith   }
2843bef8e0ddSBarry Smith 
2844bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2845bef8e0ddSBarry Smith }
2846fb2e594dSBarry Smith EXTERN_C_END
2847bef8e0ddSBarry Smith 
28484a2ae208SSatish Balay #undef __FUNCT__
28494a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2850bef8e0ddSBarry Smith /*@
2851bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2852bef8e0ddSBarry Smith        in the matrix.
2853bef8e0ddSBarry Smith 
2854bef8e0ddSBarry Smith   Input Parameters:
2855bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2856bef8e0ddSBarry Smith -  indices - the column indices
2857bef8e0ddSBarry Smith 
285815091d37SBarry Smith   Level: advanced
285915091d37SBarry Smith 
2860bef8e0ddSBarry Smith   Notes:
2861bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2862bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2863bef8e0ddSBarry Smith   of the MatSetValues() operation.
2864bef8e0ddSBarry Smith 
2865bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2866d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2867bef8e0ddSBarry Smith 
2868bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2869bef8e0ddSBarry Smith 
2870b9617806SBarry Smith     The indices should start with zero, not one.
2871b9617806SBarry Smith 
2872bef8e0ddSBarry Smith @*/
28737087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2874bef8e0ddSBarry Smith {
28754ac538c5SBarry Smith   PetscErrorCode ierr;
2876bef8e0ddSBarry Smith 
2877bef8e0ddSBarry Smith   PetscFunctionBegin;
28780700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
28794482741eSBarry Smith   PetscValidPointer(indices,2);
28804ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
2881bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2882bef8e0ddSBarry Smith }
2883bef8e0ddSBarry Smith 
2884be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2885be6bf707SBarry Smith 
2886fb2e594dSBarry Smith EXTERN_C_BEGIN
28874a2ae208SSatish Balay #undef __FUNCT__
28884a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
28897087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
2890be6bf707SBarry Smith {
2891be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
28926849ba73SBarry Smith   PetscErrorCode ierr;
2893d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2894be6bf707SBarry Smith 
2895be6bf707SBarry Smith   PetscFunctionBegin;
2896be6bf707SBarry Smith   if (aij->nonew != 1) {
2897e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2898be6bf707SBarry Smith   }
2899be6bf707SBarry Smith 
2900be6bf707SBarry Smith   /* allocate space for values if not already there */
2901be6bf707SBarry Smith   if (!aij->saved_values) {
290287828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
29039518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2904be6bf707SBarry Smith   }
2905be6bf707SBarry Smith 
2906be6bf707SBarry Smith   /* copy values over */
290787828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2908be6bf707SBarry Smith   PetscFunctionReturn(0);
2909be6bf707SBarry Smith }
2910fb2e594dSBarry Smith EXTERN_C_END
2911be6bf707SBarry Smith 
29124a2ae208SSatish Balay #undef __FUNCT__
2913b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2914be6bf707SBarry Smith /*@
2915be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2916be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2917be6bf707SBarry Smith        nonlinear portion.
2918be6bf707SBarry Smith 
2919be6bf707SBarry Smith    Collect on Mat
2920be6bf707SBarry Smith 
2921be6bf707SBarry Smith   Input Parameters:
29220e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2923be6bf707SBarry Smith 
292415091d37SBarry Smith   Level: advanced
292515091d37SBarry Smith 
2926be6bf707SBarry Smith   Common Usage, with SNESSolve():
2927be6bf707SBarry Smith $    Create Jacobian matrix
2928be6bf707SBarry Smith $    Set linear terms into matrix
2929be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2930be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2931be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2932512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2933be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2934be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2935be6bf707SBarry Smith $    In your Jacobian routine
2936be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2937be6bf707SBarry Smith $      Set nonlinear terms in matrix
2938be6bf707SBarry Smith 
2939be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2940be6bf707SBarry Smith $    // build linear portion of Jacobian
2941512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2942be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2943be6bf707SBarry Smith $    loop over nonlinear iterations
2944be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2945be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2946be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2947be6bf707SBarry Smith $       Solve linear system with Jacobian
2948be6bf707SBarry Smith $    endloop
2949be6bf707SBarry Smith 
2950be6bf707SBarry Smith   Notes:
2951be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2952512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2953be6bf707SBarry Smith     calling this routine.
2954be6bf707SBarry Smith 
29550c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
29560c468ba9SBarry Smith     and does not allocated additional space.
29570c468ba9SBarry Smith 
2958be6bf707SBarry Smith .seealso: MatRetrieveValues()
2959be6bf707SBarry Smith 
2960be6bf707SBarry Smith @*/
29617087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
2962be6bf707SBarry Smith {
29634ac538c5SBarry Smith   PetscErrorCode ierr;
2964be6bf707SBarry Smith 
2965be6bf707SBarry Smith   PetscFunctionBegin;
29660700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
2967e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2968e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
29694ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
2970be6bf707SBarry Smith   PetscFunctionReturn(0);
2971be6bf707SBarry Smith }
2972be6bf707SBarry Smith 
2973fb2e594dSBarry Smith EXTERN_C_BEGIN
29744a2ae208SSatish Balay #undef __FUNCT__
29754a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
29767087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
2977be6bf707SBarry Smith {
2978be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
29796849ba73SBarry Smith   PetscErrorCode ierr;
2980d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2981be6bf707SBarry Smith 
2982be6bf707SBarry Smith   PetscFunctionBegin;
2983be6bf707SBarry Smith   if (aij->nonew != 1) {
2984e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2985be6bf707SBarry Smith   }
2986be6bf707SBarry Smith   if (!aij->saved_values) {
2987e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2988be6bf707SBarry Smith   }
2989be6bf707SBarry Smith   /* copy values over */
299087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2991be6bf707SBarry Smith   PetscFunctionReturn(0);
2992be6bf707SBarry Smith }
2993fb2e594dSBarry Smith EXTERN_C_END
2994be6bf707SBarry Smith 
29954a2ae208SSatish Balay #undef __FUNCT__
29964a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2997be6bf707SBarry Smith /*@
2998be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2999be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3000be6bf707SBarry Smith        nonlinear portion.
3001be6bf707SBarry Smith 
3002be6bf707SBarry Smith    Collect on Mat
3003be6bf707SBarry Smith 
3004be6bf707SBarry Smith   Input Parameters:
3005be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3006be6bf707SBarry Smith 
300715091d37SBarry Smith   Level: advanced
300815091d37SBarry Smith 
3009be6bf707SBarry Smith .seealso: MatStoreValues()
3010be6bf707SBarry Smith 
3011be6bf707SBarry Smith @*/
30127087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3013be6bf707SBarry Smith {
30144ac538c5SBarry Smith   PetscErrorCode ierr;
3015be6bf707SBarry Smith 
3016be6bf707SBarry Smith   PetscFunctionBegin;
30170700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3018e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3019e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
30204ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3021be6bf707SBarry Smith   PetscFunctionReturn(0);
3022be6bf707SBarry Smith }
3023be6bf707SBarry Smith 
3024f83d6046SBarry Smith 
3025be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
30264a2ae208SSatish Balay #undef __FUNCT__
30274a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
302817ab2063SBarry Smith /*@C
3029682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
30300d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
30316e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
303251c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
30332bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
303417ab2063SBarry Smith 
3035db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3036db81eaa0SLois Curfman McInnes 
303717ab2063SBarry Smith    Input Parameters:
3038db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
303917ab2063SBarry Smith .  m - number of rows
304017ab2063SBarry Smith .  n - number of columns
304117ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
304251c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
30432bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
304417ab2063SBarry Smith 
304517ab2063SBarry Smith    Output Parameter:
3046416022c9SBarry Smith .  A - the matrix
304717ab2063SBarry Smith 
3048175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3049ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3050175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3051175b88e8SBarry Smith 
3052b259b22eSLois Curfman McInnes    Notes:
305349a6f317SBarry Smith    If nnz is given then nz is ignored
305449a6f317SBarry Smith 
305517ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
305617ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
30570002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
305844cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
305917ab2063SBarry Smith 
306017ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3061a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
30623d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
30636da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
306417ab2063SBarry Smith 
3065682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
30664fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3067682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
30686c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
30696c7ebb05SLois Curfman McInnes 
30706c7ebb05SLois Curfman McInnes    Options Database Keys:
3071698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
30729db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
307317ab2063SBarry Smith 
3074027ccd11SLois Curfman McInnes    Level: intermediate
3075027ccd11SLois Curfman McInnes 
307636db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
307736db0b34SBarry Smith 
307817ab2063SBarry Smith @*/
30797087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
308017ab2063SBarry Smith {
3081dfbe8321SBarry Smith   PetscErrorCode ierr;
30826945ee14SBarry Smith 
30833a40ed3dSBarry Smith   PetscFunctionBegin;
3084f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3085117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3086c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3087d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3088273d9f13SBarry Smith   PetscFunctionReturn(0);
3089273d9f13SBarry Smith }
3090273d9f13SBarry Smith 
30914a2ae208SSatish Balay #undef __FUNCT__
30924a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3093273d9f13SBarry Smith /*@C
3094273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3095273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3096273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3097273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3098273d9f13SBarry Smith 
3099273d9f13SBarry Smith    Collective on MPI_Comm
3100273d9f13SBarry Smith 
3101273d9f13SBarry Smith    Input Parameters:
3102117016b1SBarry Smith +  B - The matrix-free
3103273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3104273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3105273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3106273d9f13SBarry Smith 
3107273d9f13SBarry Smith    Notes:
310849a6f317SBarry Smith      If nnz is given then nz is ignored
310949a6f317SBarry Smith 
3110273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3111273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3112273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3113273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3114273d9f13SBarry Smith 
3115273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3116273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3117273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3118273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3119273d9f13SBarry Smith 
3120aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3121aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3122aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3123aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3124aa95bbe8SBarry Smith 
3125a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3126a96a251dSBarry Smith    entries or columns indices
3127a96a251dSBarry Smith 
3128273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3129273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3130273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3131273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3132273d9f13SBarry Smith 
3133273d9f13SBarry Smith    Options Database Keys:
3134698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3135698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3136273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3137273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3138273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3139273d9f13SBarry Smith 
3140273d9f13SBarry Smith    Level: intermediate
3141273d9f13SBarry Smith 
3142aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3143273d9f13SBarry Smith 
3144273d9f13SBarry Smith @*/
31457087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3146273d9f13SBarry Smith {
31474ac538c5SBarry Smith   PetscErrorCode ierr;
3148a23d5eceSKris Buschelman 
3149a23d5eceSKris Buschelman   PetscFunctionBegin;
31504ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3151a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3152a23d5eceSKris Buschelman }
3153a23d5eceSKris Buschelman 
3154a23d5eceSKris Buschelman EXTERN_C_BEGIN
3155a23d5eceSKris Buschelman #undef __FUNCT__
3156a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
31577087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3158a23d5eceSKris Buschelman {
3159273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3160ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
31616849ba73SBarry Smith   PetscErrorCode ierr;
316297f1f81fSBarry Smith   PetscInt       i;
3163273d9f13SBarry Smith 
3164273d9f13SBarry Smith   PetscFunctionBegin;
3165d5d45c9bSBarry Smith 
3166a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3167c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3168c461c341SBarry Smith     nz             = 0;
3169c461c341SBarry Smith   }
3170c461c341SBarry Smith 
317126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
317226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
317326283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
317426283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3175899cda47SBarry Smith 
3176435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3177e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3178b73539f3SBarry Smith   if (nnz) {
3179d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3180e32f2f54SBarry 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]);
3181e32f2f54SBarry 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);
3182b73539f3SBarry Smith     }
3183b73539f3SBarry Smith   }
3184b73539f3SBarry Smith 
3185273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3186273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3187273d9f13SBarry Smith 
3188ab93d7beSBarry Smith   if (!skipallocation) {
31892ee49352SLisandro Dalcin     if (!b->imax) {
3190d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3191d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
31922ee49352SLisandro Dalcin     }
3193273d9f13SBarry Smith     if (!nnz) {
3194435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3195c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3196d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3197d0f46423SBarry Smith       nz = nz*B->rmap->n;
3198273d9f13SBarry Smith     } else {
3199273d9f13SBarry Smith       nz = 0;
3200d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3201273d9f13SBarry Smith     }
3202ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3203d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3204ab93d7beSBarry Smith 
3205273d9f13SBarry Smith     /* allocate the matrix space */
32062ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3207d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3208d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3209bfeeae90SHong Zhang     b->i[0] = 0;
3210d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
32115da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
32125da197adSKris Buschelman     }
3213273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3214e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3215e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3216c461c341SBarry Smith   } else {
3217e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3218e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3219c461c341SBarry Smith   }
3220273d9f13SBarry Smith 
3221273d9f13SBarry Smith   b->nz                = 0;
3222273d9f13SBarry Smith   b->maxnz             = nz;
3223273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3224273d9f13SBarry Smith   PetscFunctionReturn(0);
3225273d9f13SBarry Smith }
3226a23d5eceSKris Buschelman EXTERN_C_END
3227273d9f13SBarry Smith 
3228a1661176SMatthew Knepley #undef  __FUNCT__
3229a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
323058d36128SBarry Smith /*@
3231a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3232a1661176SMatthew Knepley 
3233a1661176SMatthew Knepley    Input Parameters:
3234a1661176SMatthew Knepley +  B - the matrix
3235a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3236a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3237a1661176SMatthew Knepley -  v - optional values in the matrix
3238a1661176SMatthew Knepley 
3239a1661176SMatthew Knepley    Level: developer
3240a1661176SMatthew Knepley 
324158d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
324258d36128SBarry Smith 
3243a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3244a1661176SMatthew Knepley 
3245a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3246a1661176SMatthew Knepley @*/
3247a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3248a1661176SMatthew Knepley {
3249a1661176SMatthew Knepley   PetscErrorCode ierr;
3250a1661176SMatthew Knepley 
3251a1661176SMatthew Knepley   PetscFunctionBegin;
32520700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
32534ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3254a1661176SMatthew Knepley   PetscFunctionReturn(0);
3255a1661176SMatthew Knepley }
3256a1661176SMatthew Knepley 
3257a1661176SMatthew Knepley EXTERN_C_BEGIN
3258a1661176SMatthew Knepley #undef  __FUNCT__
3259a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
32607087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3261a1661176SMatthew Knepley {
3262a1661176SMatthew Knepley   PetscInt       i;
3263a1661176SMatthew Knepley   PetscInt       m,n;
3264a1661176SMatthew Knepley   PetscInt       nz;
3265a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3266a1661176SMatthew Knepley   PetscScalar    *values;
3267a1661176SMatthew Knepley   PetscErrorCode ierr;
3268a1661176SMatthew Knepley 
3269a1661176SMatthew Knepley   PetscFunctionBegin;
3270a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3271a1661176SMatthew Knepley 
327265e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3273a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3274a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3275b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3276a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
327765e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3278a1661176SMatthew Knepley     nnz[i] = nz;
3279a1661176SMatthew Knepley   }
3280a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3281a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3282a1661176SMatthew Knepley 
3283a1661176SMatthew Knepley   if (v) {
3284a1661176SMatthew Knepley     values = (PetscScalar*) v;
3285a1661176SMatthew Knepley   } else {
32860e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3287a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3288a1661176SMatthew Knepley   }
3289a1661176SMatthew Knepley 
3290a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3291b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3292b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3293a1661176SMatthew Knepley   }
3294a1661176SMatthew Knepley 
3295a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3296a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3297a1661176SMatthew Knepley 
3298a1661176SMatthew Knepley   if (!v) {
3299a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3300a1661176SMatthew Knepley   }
3301a1661176SMatthew Knepley   PetscFunctionReturn(0);
3302a1661176SMatthew Knepley }
3303a1661176SMatthew Knepley EXTERN_C_END
3304a1661176SMatthew Knepley 
3305c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3306c6db04a5SJed Brown #include <private/petscaxpy.h>
3307170fe5c8SBarry Smith 
3308170fe5c8SBarry Smith #undef __FUNCT__
3309170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3310170fe5c8SBarry Smith /*
3311170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3312170fe5c8SBarry Smith 
3313170fe5c8SBarry Smith                n                       p                          p
3314170fe5c8SBarry Smith         (              )       (              )         (                  )
3315170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3316170fe5c8SBarry Smith         (              )       (              )         (                  )
3317170fe5c8SBarry Smith 
3318170fe5c8SBarry Smith */
3319170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3320170fe5c8SBarry Smith {
3321170fe5c8SBarry Smith   PetscErrorCode     ierr;
3322170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3323170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3324170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
33251de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3326170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3327170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3328170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3329170fe5c8SBarry Smith 
3330170fe5c8SBarry Smith   PetscFunctionBegin;
3331d0f46423SBarry Smith   m = A->rmap->n;
3332d0f46423SBarry Smith   n = A->cmap->n;
3333d0f46423SBarry Smith   p = B->cmap->n;
3334170fe5c8SBarry Smith   a = sub_a->v;
3335170fe5c8SBarry Smith   b = sub_b->a;
3336170fe5c8SBarry Smith   c = sub_c->v;
3337170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3338170fe5c8SBarry Smith 
3339170fe5c8SBarry Smith   ii  = sub_b->i;
3340170fe5c8SBarry Smith   idx = sub_b->j;
3341170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3342170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3343170fe5c8SBarry Smith     while (q-->0) {
3344170fe5c8SBarry Smith       c_q = c + m*(*idx);
3345170fe5c8SBarry Smith       a_q = a + m*i;
3346be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3347170fe5c8SBarry Smith       idx++;
3348170fe5c8SBarry Smith       b++;
3349170fe5c8SBarry Smith     }
3350170fe5c8SBarry Smith   }
3351170fe5c8SBarry Smith   PetscFunctionReturn(0);
3352170fe5c8SBarry Smith }
3353170fe5c8SBarry Smith 
3354170fe5c8SBarry Smith #undef __FUNCT__
3355170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3356170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3357170fe5c8SBarry Smith {
3358170fe5c8SBarry Smith   PetscErrorCode ierr;
3359d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3360170fe5c8SBarry Smith   Mat            Cmat;
3361170fe5c8SBarry Smith 
3362170fe5c8SBarry Smith   PetscFunctionBegin;
3363e32f2f54SBarry 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);
336439804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3365170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3366170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3367170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3368170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3369170fe5c8SBarry Smith   *C = Cmat;
3370170fe5c8SBarry Smith   PetscFunctionReturn(0);
3371170fe5c8SBarry Smith }
3372170fe5c8SBarry Smith 
3373170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3374170fe5c8SBarry Smith #undef __FUNCT__
3375170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3376170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3377170fe5c8SBarry Smith {
3378170fe5c8SBarry Smith   PetscErrorCode ierr;
3379170fe5c8SBarry Smith 
3380170fe5c8SBarry Smith   PetscFunctionBegin;
3381170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3382170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3383170fe5c8SBarry Smith   }
3384170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3385170fe5c8SBarry Smith   PetscFunctionReturn(0);
3386170fe5c8SBarry Smith }
3387170fe5c8SBarry Smith 
3388170fe5c8SBarry Smith 
33890bad9183SKris Buschelman /*MC
3390fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
33910bad9183SKris Buschelman    based on compressed sparse row format.
33920bad9183SKris Buschelman 
33930bad9183SKris Buschelman    Options Database Keys:
33940bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
33950bad9183SKris Buschelman 
33960bad9183SKris Buschelman   Level: beginner
33970bad9183SKris Buschelman 
3398f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
33990bad9183SKris Buschelman M*/
34000bad9183SKris Buschelman 
3401a6175056SHong Zhang EXTERN_C_BEGIN
3402b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3403b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3404b5e56a35SBarry Smith #endif
3405ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3406af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3407af1023dbSSatish Balay #endif
34087087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
34097087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
34107087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
34117087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3412611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
34137087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3414611f576cSBarry Smith #endif
3415611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
34167087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3417611f576cSBarry Smith #endif
3418f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3419f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3420f3c0ef26SHong Zhang #endif
3421611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
34227087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3423611f576cSBarry Smith #endif
3424eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
34257087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3426eb3b5408SSatish Balay #endif
3427586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
34287087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3429586621ddSJed Brown #endif
3430719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
34317087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3432719d5645SBarry Smith #endif
3433b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
34347087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
34357087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
34367087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3437b3866ffcSBarry Smith #endif
343817667f90SBarry Smith EXTERN_C_END
343917667f90SBarry Smith 
344017667f90SBarry Smith EXTERN_C_BEGIN
34414a2ae208SSatish Balay #undef __FUNCT__
34424a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
34437087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3444273d9f13SBarry Smith {
3445273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3446dfbe8321SBarry Smith   PetscErrorCode ierr;
344738baddfdSBarry Smith   PetscMPIInt    size;
3448273d9f13SBarry Smith 
3449273d9f13SBarry Smith   PetscFunctionBegin;
34507adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3451e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3452273d9f13SBarry Smith 
345338f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3454b0a32e0cSBarry Smith   B->data             = (void*)b;
3455549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3456416022c9SBarry Smith   b->row              = 0;
3457416022c9SBarry Smith   b->col              = 0;
345882bf6240SBarry Smith   b->icol             = 0;
3459b810aeb4SBarry Smith   b->reallocs         = 0;
346036db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3461f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3462416022c9SBarry Smith   b->nonew             = 0;
3463416022c9SBarry Smith   b->diag              = 0;
3464416022c9SBarry Smith   b->solve_work        = 0;
34652a1b7f2aSHong Zhang   B->spptr             = 0;
3466be6bf707SBarry Smith   b->saved_values      = 0;
3467d7f994e1SBarry Smith   b->idiag             = 0;
346871f1c65dSBarry Smith   b->mdiag             = 0;
346971f1c65dSBarry Smith   b->ssor_work         = 0;
347071f1c65dSBarry Smith   b->omega             = 1.0;
347171f1c65dSBarry Smith   b->fshift            = 0.0;
347271f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3473a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3474a30b2313SHong Zhang   b->xtoy              = 0;
3475a30b2313SHong Zhang   b->XtoY              = 0;
347688e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
347717ab2063SBarry Smith 
347835d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3479b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3480700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3481b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3482b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3483b3866ffcSBarry Smith #endif
3484b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3485700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3486b5e56a35SBarry Smith #endif
3487ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3488700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3489719d5645SBarry Smith #endif
3490611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3491700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3492611f576cSBarry Smith #endif
3493f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3494700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3495f3c0ef26SHong Zhang #endif
3496611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3497700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3498611f576cSBarry Smith #endif
3499611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3500700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3501611f576cSBarry Smith #endif
3502eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3503700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3504eb3b5408SSatish Balay #endif
3505586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3506700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3507586621ddSJed Brown #endif
3508719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3509700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3510719d5645SBarry Smith #endif
3511700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3512700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3513700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3514700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3515700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3516700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3517700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3518700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3519700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3520700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3521700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3522700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3523700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3524700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3525700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3526700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3527700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3528700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
35294108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
353017667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
35313a40ed3dSBarry Smith   PetscFunctionReturn(0);
353217ab2063SBarry Smith }
3533273d9f13SBarry Smith EXTERN_C_END
353417ab2063SBarry Smith 
35354a2ae208SSatish Balay #undef __FUNCT__
3536b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3537b24902e0SBarry Smith /*
3538b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3539b24902e0SBarry Smith */
3540ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
354117ab2063SBarry Smith {
3542416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
35436849ba73SBarry Smith   PetscErrorCode ierr;
3544d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
354517ab2063SBarry Smith 
35463a40ed3dSBarry Smith   PetscFunctionBegin;
3547273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3548273d9f13SBarry Smith 
3549d5f3da31SBarry Smith   C->factortype     = A->factortype;
3550416022c9SBarry Smith   c->row            = 0;
3551416022c9SBarry Smith   c->col            = 0;
355282bf6240SBarry Smith   c->icol           = 0;
35536ad4291fSHong Zhang   c->reallocs       = 0;
355417ab2063SBarry Smith 
35556ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
355617ab2063SBarry Smith 
355726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
355826283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
355926283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
356026283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3561eec197d1SBarry Smith 
356233b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
35639518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
356417ab2063SBarry Smith   for (i=0; i<m; i++) {
3565416022c9SBarry Smith     c->imax[i] = a->imax[i];
3566416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
356717ab2063SBarry Smith   }
356817ab2063SBarry Smith 
356917ab2063SBarry Smith   /* allocate the matrix space */
3570f77e22a1SHong Zhang   if (mallocmatspace){
3571a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
35729518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3573f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
357497f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
357517ab2063SBarry Smith     if (m > 0) {
357697f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3577be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3578bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3579be6bf707SBarry Smith       } else {
3580bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
358117ab2063SBarry Smith       }
358208480c60SBarry Smith     }
3583f77e22a1SHong Zhang   }
358417ab2063SBarry Smith 
35856ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3586416022c9SBarry Smith   c->roworiented       = a->roworiented;
3587416022c9SBarry Smith   c->nonew             = a->nonew;
3588416022c9SBarry Smith   if (a->diag) {
358997f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
359052e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
359117ab2063SBarry Smith     for (i=0; i<m; i++) {
3592416022c9SBarry Smith       c->diag[i] = a->diag[i];
359317ab2063SBarry Smith     }
35943a40ed3dSBarry Smith   } else c->diag           = 0;
35956ad4291fSHong Zhang   c->solve_work            = 0;
35966ad4291fSHong Zhang   c->saved_values          = 0;
35976ad4291fSHong Zhang   c->idiag                 = 0;
359871f1c65dSBarry Smith   c->ssor_work             = 0;
3599a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3600e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3601e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
36026ad4291fSHong Zhang   c->xtoy                  = 0;
36036ad4291fSHong Zhang   c->XtoY                  = 0;
36046ad4291fSHong Zhang 
3605416022c9SBarry Smith   c->nz                 = a->nz;
36068ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3607273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3608754ec7b1SSatish Balay 
36096ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
36106ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3611cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3612cd6b891eSBarry Smith   if (a->compressedrow.use){
36136ad4291fSHong Zhang     i = a->compressedrow.nrows;
36140e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
36156ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
36166ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
361727ea64f8SHong Zhang   } else {
361827ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
361927ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
362027ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
36216ad4291fSHong Zhang   }
362288e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
36234108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
36244846f1f5SKris Buschelman 
36257adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
36263a40ed3dSBarry Smith   PetscFunctionReturn(0);
362717ab2063SBarry Smith }
362817ab2063SBarry Smith 
36294a2ae208SSatish Balay #undef __FUNCT__
3630b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3631b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3632b24902e0SBarry Smith {
3633b24902e0SBarry Smith   PetscErrorCode ierr;
3634b24902e0SBarry Smith 
3635b24902e0SBarry Smith   PetscFunctionBegin;
3636b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
36374b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3638b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3639f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3640b24902e0SBarry Smith   PetscFunctionReturn(0);
3641b24902e0SBarry Smith }
3642b24902e0SBarry Smith 
3643b24902e0SBarry Smith #undef __FUNCT__
36444a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3645112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3646fbdbba38SShri Abhyankar {
3647fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3648fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3649fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3650fbdbba38SShri Abhyankar   int            fd;
3651fbdbba38SShri Abhyankar   PetscMPIInt    size;
3652fbdbba38SShri Abhyankar   MPI_Comm       comm;
3653fbdbba38SShri Abhyankar 
3654fbdbba38SShri Abhyankar   PetscFunctionBegin;
3655fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3656fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3657fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3658fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3659fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3660fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
3661fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3662fbdbba38SShri Abhyankar 
3663fbdbba38SShri Abhyankar   if (nz < 0) {
3664fbdbba38SShri Abhyankar     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3665fbdbba38SShri Abhyankar   }
3666fbdbba38SShri Abhyankar 
3667fbdbba38SShri Abhyankar   /* read in row lengths */
3668fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3669fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3670fbdbba38SShri Abhyankar 
3671fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
3672fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3673fbdbba38SShri 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);
3674fbdbba38SShri Abhyankar 
3675fbdbba38SShri Abhyankar   /* set global size if not set already*/
3676f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
3677fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3678aabbc4fbSShri Abhyankar   } else {
3679fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
3680fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
3681f501eaabSShri 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);
3682aabbc4fbSShri Abhyankar   }
3683fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
3684fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
3685fbdbba38SShri Abhyankar 
3686fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
3687fbdbba38SShri Abhyankar 
3688fbdbba38SShri Abhyankar   /* read in nonzero values */
3689fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
3690fbdbba38SShri Abhyankar 
3691fbdbba38SShri Abhyankar   /* set matrix "i" values */
3692fbdbba38SShri Abhyankar   a->i[0] = 0;
3693fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
3694fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3695fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
3696fbdbba38SShri Abhyankar   }
3697fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3698fbdbba38SShri Abhyankar 
3699fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3700fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3701fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
3702fbdbba38SShri Abhyankar }
3703fbdbba38SShri Abhyankar 
3704fbdbba38SShri Abhyankar #undef __FUNCT__
3705b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3706ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
37077264ac53SSatish Balay {
37087264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3709dfbe8321SBarry Smith   PetscErrorCode ierr;
3710eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3711eeffb40dSHong Zhang   PetscInt k;
3712eeffb40dSHong Zhang #endif
37137264ac53SSatish Balay 
37143a40ed3dSBarry Smith   PetscFunctionBegin;
3715bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3716d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3717ca44d042SBarry Smith     *flg = PETSC_FALSE;
3718ca44d042SBarry Smith     PetscFunctionReturn(0);
3719bcd2baecSBarry Smith   }
37207264ac53SSatish Balay 
37217264ac53SSatish Balay   /* if the a->i are the same */
3722d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3723abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
37247264ac53SSatish Balay 
37257264ac53SSatish Balay   /* if a->j are the same */
372697f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3727abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3728bcd2baecSBarry Smith 
3729bcd2baecSBarry Smith   /* if a->a are the same */
3730eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3731eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3732eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3733eeffb40dSHong Zhang       *flg = PETSC_FALSE;
37343a40ed3dSBarry Smith       PetscFunctionReturn(0);
3735eeffb40dSHong Zhang     }
3736eeffb40dSHong Zhang   }
3737eeffb40dSHong Zhang #else
3738eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3739eeffb40dSHong Zhang #endif
3740eeffb40dSHong Zhang   PetscFunctionReturn(0);
37417264ac53SSatish Balay }
374236db0b34SBarry Smith 
37434a2ae208SSatish Balay #undef __FUNCT__
37444a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
374505869f15SSatish Balay /*@
374636db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
374736db0b34SBarry Smith               provided by the user.
374836db0b34SBarry Smith 
3749c75a6043SHong Zhang       Collective on MPI_Comm
375036db0b34SBarry Smith 
375136db0b34SBarry Smith    Input Parameters:
375236db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
375336db0b34SBarry Smith .   m - number of rows
375436db0b34SBarry Smith .   n - number of columns
375536db0b34SBarry Smith .   i - row indices
375636db0b34SBarry Smith .   j - column indices
375736db0b34SBarry Smith -   a - matrix values
375836db0b34SBarry Smith 
375936db0b34SBarry Smith    Output Parameter:
376036db0b34SBarry Smith .   mat - the matrix
376136db0b34SBarry Smith 
376236db0b34SBarry Smith    Level: intermediate
376336db0b34SBarry Smith 
376436db0b34SBarry Smith    Notes:
37650551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
3766292fb18eSBarry Smith     once the matrix is destroyed and not before
376736db0b34SBarry Smith 
376836db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
376936db0b34SBarry Smith 
3770bfeeae90SHong Zhang        The i and j indices are 0 based
377136db0b34SBarry Smith 
3772a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3773a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3774a4552177SSatish Balay     as shown:
3775a4552177SSatish Balay 
3776a4552177SSatish Balay         1 0 0
3777a4552177SSatish Balay         2 0 3
3778a4552177SSatish Balay         4 5 6
3779a4552177SSatish Balay 
3780a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
37819985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3782a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3783a4552177SSatish Balay 
37849985e31cSBarry Smith 
37852fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
378636db0b34SBarry Smith 
378736db0b34SBarry Smith @*/
37887087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
378936db0b34SBarry Smith {
3790dfbe8321SBarry Smith   PetscErrorCode ierr;
3791cbcfb4deSHong Zhang   PetscInt       ii;
379236db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3793cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3794cbcfb4deSHong Zhang   PetscInt       jj;
3795cbcfb4deSHong Zhang #endif
379636db0b34SBarry Smith 
379736db0b34SBarry Smith   PetscFunctionBegin;
3798a96a251dSBarry Smith   if (i[0]) {
3799e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
380036db0b34SBarry Smith   }
3801f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3802f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3803ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3804ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3805ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3806ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3807ab93d7beSBarry Smith 
380836db0b34SBarry Smith   aij->i = i;
380936db0b34SBarry Smith   aij->j = j;
381036db0b34SBarry Smith   aij->a = a;
381136db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
381236db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3813e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3814e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
381536db0b34SBarry Smith 
381636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
381736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
38182515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
3819e32f2f54SBarry 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]);
38209985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
3821e32f2f54SBarry 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);
3822e32f2f54SBarry 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);
38239985e31cSBarry Smith     }
382436db0b34SBarry Smith #endif
382536db0b34SBarry Smith   }
38262515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
382736db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3828e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3829e32f2f54SBarry 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]);
383036db0b34SBarry Smith   }
383136db0b34SBarry Smith #endif
383236db0b34SBarry Smith 
3833b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3834b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
383536db0b34SBarry Smith   PetscFunctionReturn(0);
383636db0b34SBarry Smith }
383736db0b34SBarry Smith 
3838cc8ba8e1SBarry Smith #undef __FUNCT__
3839ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3840dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3841cc8ba8e1SBarry Smith {
3842dfbe8321SBarry Smith   PetscErrorCode ierr;
3843cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
384436db0b34SBarry Smith 
3845cc8ba8e1SBarry Smith   PetscFunctionBegin;
38468ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3847cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3848cc8ba8e1SBarry Smith     a->coloring = coloring;
384912c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
385097f1f81fSBarry Smith     PetscInt             i,*larray;
385112c595b3SBarry Smith     ISColoring      ocoloring;
385208b6dcc0SBarry Smith     ISColoringValue *colors;
385312c595b3SBarry Smith 
385412c595b3SBarry Smith     /* set coloring for diagonal portion */
38550e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3856d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
385712c595b3SBarry Smith       larray[i] = i;
385812c595b3SBarry Smith     }
3859784ac674SJed Brown     ierr = ISGlobalToLocalMappingApply(A->cmapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
38600e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3861d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
386212c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
386312c595b3SBarry Smith     }
386412c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3865d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
386612c595b3SBarry Smith     a->coloring = ocoloring;
386712c595b3SBarry Smith   }
3868cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3869cc8ba8e1SBarry Smith }
3870cc8ba8e1SBarry Smith 
3871dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3872ee4f033dSBarry Smith EXTERN_C_BEGIN
3873c6db04a5SJed Brown #include <adic/ad_utils.h>
3874ee4f033dSBarry Smith EXTERN_C_END
3875cc8ba8e1SBarry Smith 
3876cc8ba8e1SBarry Smith #undef __FUNCT__
3877ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3878dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3879cc8ba8e1SBarry Smith {
3880cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3881d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
38824440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
388308b6dcc0SBarry Smith   ISColoringValue *color;
3884cc8ba8e1SBarry Smith 
3885cc8ba8e1SBarry Smith   PetscFunctionBegin;
3886e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
38874440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3888cc8ba8e1SBarry Smith   color = a->coloring->colors;
3889cc8ba8e1SBarry Smith   /* loop over rows */
3890cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3891cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3892cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3893cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3894cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3895cc8ba8e1SBarry Smith     }
38964440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3897ee4f033dSBarry Smith   }
3898ee4f033dSBarry Smith   PetscFunctionReturn(0);
3899ee4f033dSBarry Smith }
3900ee4f033dSBarry Smith #endif
3901ee4f033dSBarry Smith 
3902ee4f033dSBarry Smith #undef __FUNCT__
3903ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
390497f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3905ee4f033dSBarry Smith {
3906ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3907d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
390854f21887SBarry Smith   MatScalar       *v = a->a;
390954f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
391008b6dcc0SBarry Smith   ISColoringValue *color;
3911ee4f033dSBarry Smith 
3912ee4f033dSBarry Smith   PetscFunctionBegin;
3913e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3914ee4f033dSBarry Smith   color = a->coloring->colors;
3915ee4f033dSBarry Smith   /* loop over rows */
3916ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3917ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3918ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3919ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3920ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3921ee4f033dSBarry Smith     }
3922ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3923cc8ba8e1SBarry Smith   }
3924cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3925cc8ba8e1SBarry Smith }
392636db0b34SBarry Smith 
392781824310SBarry Smith /*
392881824310SBarry Smith     Special version for direct calls from Fortran
392981824310SBarry Smith */
3930c6db04a5SJed Brown #include <private/fortranimpl.h>
393181824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
393281824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
393381824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
393481824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
393581824310SBarry Smith #endif
393681824310SBarry Smith 
393781824310SBarry Smith /* Change these macros so can be used in void function */
393881824310SBarry Smith #undef CHKERRQ
39397adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
394081824310SBarry Smith #undef SETERRQ2
3941e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
394281824310SBarry Smith 
394381824310SBarry Smith EXTERN_C_BEGIN
394481824310SBarry Smith #undef __FUNCT__
394581824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
39461f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
394781824310SBarry Smith {
394881824310SBarry Smith   Mat            A = *AA;
394981824310SBarry Smith   PetscInt       m = *mm, n = *nn;
395081824310SBarry Smith   InsertMode     is = *isis;
395181824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
395281824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
395381824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
395481824310SBarry Smith   PetscErrorCode ierr;
395581824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
395654f21887SBarry Smith   MatScalar      *ap,value,*aa;
3957ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
3958ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
395981824310SBarry Smith 
396081824310SBarry Smith   PetscFunctionBegin;
3961d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
396281824310SBarry Smith   imax = a->imax;
396381824310SBarry Smith   ai = a->i;
396481824310SBarry Smith   ailen = a->ilen;
396581824310SBarry Smith   aj = a->j;
396681824310SBarry Smith   aa = a->a;
396781824310SBarry Smith 
396881824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
396981824310SBarry Smith     row  = im[k];
397081824310SBarry Smith     if (row < 0) continue;
397181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3972d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
397381824310SBarry Smith #endif
397481824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
397581824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
397681824310SBarry Smith     low  = 0;
397781824310SBarry Smith     high = nrow;
397881824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
397981824310SBarry Smith       if (in[l] < 0) continue;
398081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3981d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
398281824310SBarry Smith #endif
398381824310SBarry Smith       col = in[l];
398481824310SBarry Smith       if (roworiented) {
398581824310SBarry Smith         value = v[l + k*n];
398681824310SBarry Smith       } else {
398781824310SBarry Smith         value = v[k + l*m];
398881824310SBarry Smith       }
398981824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
399081824310SBarry Smith 
399181824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
399281824310SBarry Smith       lastcol = col;
399381824310SBarry Smith       while (high-low > 5) {
399481824310SBarry Smith         t = (low+high)/2;
399581824310SBarry Smith         if (rp[t] > col) high = t;
399681824310SBarry Smith         else             low  = t;
399781824310SBarry Smith       }
399881824310SBarry Smith       for (i=low; i<high; i++) {
399981824310SBarry Smith         if (rp[i] > col) break;
400081824310SBarry Smith         if (rp[i] == col) {
400181824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
400281824310SBarry Smith           else                  ap[i] = value;
400381824310SBarry Smith           goto noinsert;
400481824310SBarry Smith         }
400581824310SBarry Smith       }
400681824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
400781824310SBarry Smith       if (nonew == 1) goto noinsert;
40087adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4009fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
401081824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
401181824310SBarry Smith       /* shift up all the later entries in this row */
401281824310SBarry Smith       for (ii=N; ii>=i; ii--) {
401381824310SBarry Smith         rp[ii+1] = rp[ii];
401481824310SBarry Smith         ap[ii+1] = ap[ii];
401581824310SBarry Smith       }
401681824310SBarry Smith       rp[i] = col;
401781824310SBarry Smith       ap[i] = value;
401881824310SBarry Smith       noinsert:;
401981824310SBarry Smith       low = i + 1;
402081824310SBarry Smith     }
402181824310SBarry Smith     ailen[row] = nrow;
402281824310SBarry Smith   }
402381824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
402481824310SBarry Smith   PetscFunctionReturnVoid();
402581824310SBarry Smith }
402681824310SBarry Smith EXTERN_C_END
402762298a1eSBarry Smith 
4028